{"record":{"id":"17c01f91bf5dcaac","repo":"tensorflow/models","slug":"input-fn-should-be-a-closure-that-returns-a-data","errorCode":null,"errorMessage":"`input_fn` should be a closure that returns a dataset.","messagePattern":"`input_fn` should be a closure that returns a dataset\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/bert/model_training_utils.py","lineNumber":63,"sourceCode":"    logging.info('Saving model as TF checkpoint: %s', saved_path)\n  else:\n    # In multi worker training we need every worker to save checkpoint, because\n    # variables can trigger synchronization on read and synchronization needs\n    # all workers to participate. To avoid workers overriding each other we save\n    # to a temporary directory on non-chief workers.\n    tmp_dir = tempfile.mkdtemp()\n    checkpoint.save(os.path.join(tmp_dir, 'ckpt'))\n    tf.io.gfile.rmtree(tmp_dir)\n  return\n\n\ndef _get_input_iterator(input_fn, strategy):\n  \"\"\"Returns distributed dataset iterator.\"\"\"\n  # When training with TPU pods, datasets needs to be cloned across\n  # workers. Since Dataset instance cannot be cloned in eager mode, we instead\n  # pass callable that returns a dataset.\n  if not callable(input_fn):\n    raise ValueError('`input_fn` should be a closure that returns a dataset.')\n  iterator = iter(strategy.distribute_datasets_from_function(input_fn))\n  return iterator\n\n\ndef _float_metric_value(metric):\n  \"\"\"Gets the value of a float-value keras metric.\"\"\"\n  return metric.result().numpy().astype(float)\n\n\ndef clip_by_global_norm_callback(grads_and_vars):\n  \"\"\"Performs gradient clipping.\"\"\"\n  grads, variables = zip(*grads_and_vars)\n  (clipped_grads, _) = tf.clip_by_global_norm(grads, clip_norm=1.0)\n  return zip(clipped_grads, variables)\n\n\ndef steps_to_run(current_step, steps_per_epoch, steps_per_loop):\n  \"\"\"Calculates steps to run on device.\"\"\"","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/bert/model_training_utils.py#L45-L81","documentation":"Error \"`input_fn` should be a closure that returns a dataset.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/legacy/bert/model_training_utils.py:63 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}