{"record":{"id":"117f70792bb46c33","repo":"tensorflow/models","slug":"batch-size-must-be-divisible-by-number-of-replicas","errorCode":null,"errorMessage":"Batch size must be divisible by number of replicas : {}","messagePattern":"Batch size must be divisible by number of replicas : (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/image_classification/resnet/resnet_runnable.py","lineNumber":39,"sourceCode":"from official.legacy.image_classification.resnet import resnet_model\nfrom official.modeling import grad_utils\nfrom official.modeling import performance\nfrom official.utils.flags import core as flags_core\n\n\nclass ResnetRunnable(orbit.StandardTrainer, orbit.StandardEvaluator):\n  \"\"\"Implements the training and evaluation APIs for Resnet model.\"\"\"\n\n  def __init__(self, flags_obj, time_callback, epoch_steps):\n    self.strategy = tf.distribute.get_strategy()\n    self.flags_obj = flags_obj\n    self.dtype = flags_core.get_tf_dtype(flags_obj)\n    self.time_callback = time_callback\n\n    # Input pipeline related\n    batch_size = flags_obj.batch_size\n    if batch_size % self.strategy.num_replicas_in_sync != 0:\n      raise ValueError(\n          'Batch size must be divisible by number of replicas : {}'.format(\n              self.strategy.num_replicas_in_sync))\n\n    # As auto rebatching is not supported in\n    # `distribute_datasets_from_function()` API, which is\n    # required when cloning dataset to multiple workers in eager mode,\n    # we use per-replica batch size.\n    self.batch_size = int(batch_size / self.strategy.num_replicas_in_sync)\n\n    if self.flags_obj.use_synthetic_data:\n      self.input_fn = common.get_synth_input_fn(\n          height=imagenet_preprocessing.DEFAULT_IMAGE_SIZE,\n          width=imagenet_preprocessing.DEFAULT_IMAGE_SIZE,\n          num_channels=imagenet_preprocessing.NUM_CHANNELS,\n          num_classes=imagenet_preprocessing.NUM_CLASSES,\n          dtype=self.dtype,\n          drop_remainder=True)\n    else:","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/image_classification/resnet/resnet_runnable.py#L21-L57","documentation":"Error \"Batch size must be divisible by number of replicas : {}\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/legacy/image_classification/resnet/resnet_runnable.py:39 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"}