{"record":{"id":"189ce6cb78a6add5","repo":"Lightning-AI/pytorch-lightning","slug":"f-you-requested-to-check-limit-batches-of-the","errorCode":null,"errorMessage":"f\"You requested to check {limit_batches} of the `{stage.dataloader_prefix}_dataloader` but\" f\" {limit_batches} * {length} < 1. Please increase the\" f\" `limit_{stage.dataloader_prefix}_batches` argument. Try at least\" f\" `limit_{stage.dataloader_prefix}_batches={min_percentage}`\"","messagePattern":"f\"You requested to check (.+?) of the `(.+?)_dataloader` but\" f\" (.+?) \\* (.+?) < 1\\. Please increase the\" f\" `limit_(.+?)_batches` argument\\. Try at least\" f\" `limit_(.+?)_batches=(.+?)`\"","errorType":"validation","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/data_connector.py","lineNumber":469,"sourceCode":") -> Union[int, float]:\n    if length == 0:\n        return int(length)\n\n    num_batches = length\n    # limit num batches either as a percent or num steps\n    if isinstance(limit_batches, int):\n        num_batches = min(length, limit_batches)\n    elif isinstance(limit_batches, float) and length != float(\"inf\"):\n        num_batches = int(length * limit_batches)\n    elif limit_batches != 1.0:\n        raise MisconfigurationException(\n            f\"When using an `IterableDataset`, `Trainer(limit_{stage.dataloader_prefix}_batches)` must be\"\n            f\" `1.0` or an int. An int specifies `num_{stage.dataloader_prefix}_batches` to use.\"\n        )\n\n    if num_batches == 0 and limit_batches > 0.0 and isinstance(limit_batches, float) and length != float(\"inf\"):\n        min_percentage = 1.0 / length\n        raise MisconfigurationException(\n            f\"You requested to check {limit_batches} of the `{stage.dataloader_prefix}_dataloader` but\"\n            f\" {limit_batches} * {length} < 1. Please increase the\"\n            f\" `limit_{stage.dataloader_prefix}_batches` argument. Try at least\"\n            f\" `limit_{stage.dataloader_prefix}_batches={min_percentage}`\"\n        )\n    return num_batches\n\n\ndef _process_dataloader(\n    trainer: \"pl.Trainer\", trainer_fn: TrainerFn, stage: RunningStage, dataloader: object\n) -> object:\n    if stage != RunningStage.TRAINING:\n        is_shuffled = _is_dataloader_shuffled(dataloader)\n        # limit this warning only for samplers assigned automatically when shuffle is set\n        if is_shuffled:\n            rank_zero_warn(\n                f\"Your `{stage.dataloader_prefix}_dataloader`'s sampler has shuffling enabled,\"\n                \" it is strongly recommended that you turn shuffling off for val/test dataloaders.\",","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/data_connector.py#L451-L487","documentation":"After applying a fractional limit_batches to a finite dataloader length, the computed number of batches rounds down to zero while the user requested a positive fraction. Lightning raises this MisconfigurationException because zero batches means the stage would silently do nothing, which is almost never intended.","triggerScenarios":"_parse_num_batches computes int(length * limit_batches) == 0 while limit_batches > 0.0 and length is finite, e.g. limit_val_batches=0.1 with a val dataloader of length 5 (0.1*5 = 0.5 -> 0); limit_test_batches=0.25 with 2 batches.","commonSituations":"Using percentage-based limit_val_batches (the Trainer default is 1.0 but users often set 0.1 or 0.25) with a small validation set; sanity-check configs with tiny datasets; unit tests with 1-2 batches per epoch.","solutions":["Increase limit_batches to at least the suggested min_percentage (1/length), e.g. limit_val_batches=0.3 for length 3","Use an integer count instead of a percentage: Trainer(limit_val_batches=1)","Enlarge the dataloader length (larger val set or smaller batch_size) so the fraction yields >=1 batch"],"exampleFix":"# before (length=5 val dataloader)\ntrainer = Trainer(limit_val_batches=0.1)  # 0.1*5 = 0 batches\n\n# after\ntrainer = Trainer(limit_val_batches=1)  # int count\n# or\ntrainer = Trainer(limit_val_batches=0.4)","handlingStrategy":"validation","validationCode":"length = len(dl)  # dataloader length\nif isinstance(limit_batches, float) and limit_batches > 0 and int(length * limit_batches) == 0:\n    raise ValueError(f\"limit_batches too small for length={length}; need >= {1.0/length}\")","typeGuard":"def is_valid_fraction(limit_batches: float, length: int) -> bool:\n    return int(length * limit_batches) >= 1","tryCatchPattern":null,"preventionTips":["Use integer batch limits for small datasets/val sets","Compute ceil(length * fraction) mentally when configuring sanity_val_batches/limit_val_batches","Add a unit test asserting trainer fits with your config on a 2-batch dummy dataloader"],"tags":["pytorch-lightning","limit-batches","validation-loop","misconfiguration"],"backgroundTag":"zero-batches-from-fractional-limit","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}