{"record":{"id":"1b6bd1d605152343","repo":"Lightning-AI/pytorch-lightning","slug":"expected-lengths-lengths-to-be-greater-or-equa","errorCode":null,"errorMessage":"Expected lengths ({lengths}) to be greater or equal than samples ({samples})","messagePattern":"Expected lengths \\((.+?)\\) to be greater or equal than samples \\((.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/utilities/throughput.py","lineNumber":161,"sourceCode":"            time: Total elapsed time in seconds. It should monotonically increase by the iteration time with each\n                call.\n            batches: Total batches seen per device. It should monotonically increase with each call.\n            samples: Total samples seen per device. It should monotonically increase by the batch size with each call.\n            lengths: Total length of the samples seen. It should monotonically increase by the lengths of a batch with\n                each call.\n            flops: Flops elapased per device since last ``update()`` call. You can easily compute this by using\n                :func:`measure_flops` and multiplying it by the number of batches that have been processed.\n                The value might be different in each device if the batch size is not the same.\n\n        \"\"\"\n        self._time.append(time)\n        if samples < batches:\n            raise ValueError(f\"Expected samples ({samples}) to be greater or equal than batches ({batches})\")\n        self._batches.append(batches)\n        self._samples.append(samples)\n        if lengths is not None:\n            if lengths < samples:\n                raise ValueError(f\"Expected lengths ({lengths}) to be greater or equal than samples ({samples})\")\n            self._lengths.append(lengths)\n            if len(self._samples) != len(self._lengths):\n                raise RuntimeError(\n                    f\"If lengths are passed ({len(self._lengths)}), there needs to be the same number of samples\"\n                    f\" ({len(self._samples)})\"\n                )\n        if flops is not None:\n            # sum of flops across ranks\n            self._flops.append(flops * self.world_size)\n\n    def compute(self) -> _THROUGHPUT_METRICS:\n        \"\"\"Compute throughput metrics.\"\"\"\n        metrics = {\n            \"time\": self._time[-1],\n            \"batches\": self._batches[-1],\n            \"samples\": self._samples[-1],\n        }\n        if self._lengths:","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/utilities/throughput.py#L143-L179","documentation":"ThroughputMonitor.update() validates that the reported tensor lengths (e.g. token counts for variable-length sequences) are at least as large as the sample count, since each sample must have at least one element of length. It raises ValueError when lengths < samples because the stats would be internally inconsistent.","triggerScenarios":"Calling throughput.update(batch=..., samples=N, lengths=M) with M < N, e.g. samples=64 but lengths=32 (lengths not reduced per-rank consistently, or passing padded token counts smaller than batch size, or mixing up argument order between samples and lengths).","commonSituations":"User computes lengths as an int that lost its batch dimension (e.g. passing seq_len instead of seq_len * batch_size), or aggregates lengths only over a subset of the batch, or confuses samples (batch size) with total elements when using variable-length data (packed sequences, tokenized corpora).","solutions":["Check the values you pass: lengths must be >= samples (e.g. sum of sequence lengths over the batch, not per-sample length)","If sequences are variable length, pass lengths as the total number of tokens in the batch, not the max/mean sequence length","Verify you did not swap the samples and lengths arguments"],"exampleFix":"# before\nthroughput.update(batch=(x := next(dl))[0].shape[0], samples=x[0].shape[0], lengths=seq_len)  # seq_len < batch size\n\n# after\nthroughput.update(batch=batch_idx, samples=batch_size, lengths=int(lengths_tensor.sum().item()))","handlingStrategy":"validation","validationCode":"samples = batch_size\nlengths = int(lengths_tensor.sum().item()) if lengths_tensor is not None else None\nassert lengths is None or lengths >= samples, f\"lengths {lengths} < samples {samples}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always compute lengths as the batch-wide total (sum over samples), never a per-sample scalar","Add an assert before throughput.update() in debug builds"],"tags":["pytorch-lightning","throughput","validation","argument-mismatch"],"backgroundTag":"invalid-argument-validation","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}