{"record":{"id":"e7e6d6c1a6177ebe","repo":"Lightning-AI/pytorch-lightning","slug":"prefetch-batches-should-at-least-be-0","errorCode":null,"errorMessage":"`prefetch_batches` should at least be 0.","messagePattern":"`prefetch_batches` should at least be 0\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/loops/fetchers.py","lineNumber":99,"sourceCode":"        if self._combined_loader is not None:\n            self._combined_loader.reset()\n        self.iterator = None\n\n\nclass _PrefetchDataFetcher(_DataFetcher):\n    \"\"\"This class is used to control batch fetching flow.\n\n    Args:\n        prefetch_batches: Number of batches to pre-fetch. Pre-fetching at least 1 batch is necessary to properly track\n            whether a batch is the last one (available with :attr:`self.done`) when the length is not available. The\n            value of this argument is ignored when the length is available.\n\n    \"\"\"\n\n    def __init__(self, prefetch_batches: int = 1) -> None:\n        super().__init__()\n        if prefetch_batches < 0:\n            raise ValueError(\"`prefetch_batches` should at least be 0.\")\n        self.prefetch_batches = prefetch_batches\n        self.batches: list[Any] = []\n\n    @override\n    def __iter__(self) -> \"_PrefetchDataFetcher\":\n        super().__iter__()\n        if self.length is not None:\n            # ignore pre-fetching, it's not necessary\n            return self\n        # prefetch batches to know when the iterator will be exhausted in advance\n        for _ in range(self.prefetch_batches):\n            try:\n                batch = super().__next__()\n                self.batches.append(batch)\n            except StopIteration:\n                # this would only happen when prefetch_batches > the number of batches available and makes\n                # `__next__` jump directly to the empty iterator case without trying to fetch again\n                break","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/loops/fetchers.py#L81-L117","documentation":"_PrefetchDataFetcher.__init__ validates that prefetch_batches >= 0; a negative value raises ValueError immediately since negative prefetching is meaningless and would break the buffering logic.","triggerScenarios":"Constructing _PrefetchDataFetcher(prefetch_batches=-1), or a config/CLI value that flows in unvalidated (e.g. a negative int from a YAML config or an off-by-one computation).","commonSituations":"Experiments tuning prefetch depth where a computed value goes negative (e.g. prefetch = num_workers - something), or CLI arg parsing accepting negatives.","solutions":["Pass 0 to disable prefetching (that's the minimum, no negative needed)","Clamp config values: max(0, prefetch_batches)","Validate the CLI/config type with a non-negative constraint"],"exampleFix":"# before\nfetcher = _PrefetchDataFetcher(prefetch_batches=cfg.prefetch - 2)  # may be negative\n# after\nfetcher = _PrefetchDataFetcher(prefetch_batches=max(0, cfg.prefetch - 2))","handlingStrategy":"validation","validationCode":"prefetch_batches = max(0, int(prefetch_batches))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp tunable prefetch values to >= 0","Validate numeric config ranges at load time"],"tags":["lightning","data-loading","prefetch","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}