{"record":{"id":"484ef125d601d83a","repo":"huggingface/candle","slug":"batch-size-mismatch-between-inp-inp-b-sz-and-t","errorCode":null,"errorMessage":"batch size mismatch between inp ({inp_b_sz}) and target ({b_sz})","messagePattern":"batch size mismatch between inp \\((.+?)\\) and target \\((.+?)\\)","errorType":"exception","errorClass":"candle::Error","httpStatus":null,"severity":"error","filePath":"candle-nn/src/loss.rs","lineNumber":22,"sourceCode":"\n/// The negative log likelihood loss.\n///\n/// Arguments\n///\n/// * [inp]: The input tensor of dimensions `N, C` where `N` is the batch size and `C` the number\n///   of categories. This is expected to contain log probabilities.\n/// * [target]: The ground truth labels as a tensor of u32 of dimension `N`.\n///\n/// The resulting tensor is a scalar containing the average value over the batch.\npub fn nll(inp: &Tensor, target: &Tensor) -> Result<Tensor> {\n    let b_sz = match target.dims() {\n        &[b_sz] => b_sz,\n        dims => candle::bail!(\"the target tensor should have a single dimension ({dims:?})\"),\n    };\n    match inp.dims() {\n        &[inp_b_sz, _] => {\n            if inp_b_sz != b_sz {\n                candle::bail!(\"batch size mismatch between inp ({inp_b_sz}) and target ({b_sz})\")\n            }\n        }\n        dims => candle::bail!(\"the target tensor should have two dimensions ({dims:?})\"),\n    }\n    inp.gather(&target.unsqueeze(1)?, 1)?\n        .sum_all()?\n        .affine(-1f64 / b_sz as f64, 0.)\n}\n\n/// The cross-entropy loss.\n///\n/// Arguments\n///\n/// * [inp]: The input tensor of dimensions `N, C` where `N` is the batch size and `C` the number\n///   of categories. This is expected to raw logits.\n/// * [target]: The ground truth labels as a tensor of u32 of dimension `N`.\n///\n/// The resulting tensor is a scalar containing the average value over the batch.","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-nn/src/loss.rs#L4-L40","documentation":"Inside nll, after confirming the target is 1-D of length b_sz, the input's first dimension must equal that batch size. This bail fires when the number of rows in the (log-probability) input differs from the number of target labels, since gather cannot align per-sample losses.","triggerScenarios":"Calling nll or cross_entropy where inp.dims()[0] != target.dims()[0] — e.g. logits for a truncated final batch paired with targets from a dropped-last batch, or targets mis-split across micro-batches.","commonSituations":"DataLoader drop_last differing between inputs and labels pipelines; off-by-one in manual batch slicing; targets accumulated over the epoch while logits are per-batch; combining teacher-forcing sequences of different lengths.","solutions":["Slice both tensors to the same batch size before the loss: inp = inp.narrow(0, 0, min_b)?; target = target.narrow(0, 0, min_b)?;","Fix the dataloader so inputs and targets use identical batching settings (same batch_size and drop_last).","Verify you are not pairing per-epoch logits with per-batch targets (or vice versa).","Print inp.dims() and target.dims() at the call site to find which pipeline produced the extra/missing samples."],"exampleFix":"// before\nlet loss = cross_entropy(&logits, &labels)?; // logits [63, C], labels [64]\n// after\nlet b = logits.dim(0)?;\nlet loss = cross_entropy(&logits, &labels.narrow(0, 0, b)?)?;","handlingStrategy":"validation","validationCode":"let (bi, bt) = (inp.dim(0)?, target.dim(0)?);\nif bi != bt {\n    return Err(anyhow!(\"batch mismatch: inp {bi} vs target {bt}\"));\n}","typeGuard":null,"tryCatchPattern":"let b = inp.dim(0)?.min(target.dim(0)?);\nlet loss = cross_entropy(\n    &inp.narrow(0, 0, b)?,\n    &target.narrow(0, 0, b)?\n)?;","preventionTips":["Use identical batch_size/drop_last settings for input and label dataloaders","Slice batches from a single (input, target) pair, never two independent iterators","Log both dims once per epoch to catch drift from pipeline changes","Unit-test the last (short) batch of your dataloader"],"tags":["loss","shape","batching","rust"],"backgroundTag":"batch-size-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}