Repository navigation
Description
Summary
After a short LoRA fine-tune, greedy generation at batch size 1 and at batch sizes 2, 4 and 8 produce different text for the same prompts, with left padding in place and the padding demonstrably happening.
Reproduces on two different models, on a Tesla T4 (sm_75), across two fresh processes each. This is the same family as #1066 and #3699, on a current stack.
Environment
torch 2.10.0+cu128 transformers 5.15.1
trl 1.10.0 peft 0.19.1
accelerate 1.13.0 bitsandbytes 0.50.1
datasets 5.0.1 triton 3.6.0
GPU Tesla T4, sm_75 load_in_4bit True, dtype float16
Reproduction
Eight prompts of deliberately different lengths, greedy (do_sample=False), generated one at a time and then in batches, decoded past the prompt width.
unsloth/gemma-4-E2B-it
prompt token lengths [13, 13, 14, 15, 13, 12, 15, 16] (5 distinct, so padding happened)
tokenizer.padding_side after generating: left
batch 1: ['__UNSLOTH__!!!'] x 8
batch 2: ['__UNSLOTH__!!!',
'__UNSLOTH__!!!',
':\n### Answer:\n### Answer:\n__UNSLOTH__!!!', <-- differs
'__UNSLOTH__!!!',
'__UNSLOTH__!!!',
':\n__UNSLOTH__UNSLOTH!!!', <-- differs
'?\n### Answer:\n### Answer?\n__UNSLOTH__UN', <-- differs
'__UNSLOTH__!!!']
unsloth/Qwen3.5-2B
prompt token lengths [13, 13, 14, 15, 13, 13, 15, 16] (4 distinct)
tokenizer.padding_side after generating: left
batch 1: ['__UNSLOTH__!!!'] x 8
batch 2: ['__UNSLOTH__!!!',
'__UNSLOTH__!!!',
'?UNSLOTH__UNSLOTH__UNSLOTH__UNSLOTH', <-- differs
'__UNSLOTH__!!!',
'__UNSLOTH__!!!',
'__UNSLOTH__!!!',
' I__UNSLOTH__UNSLOTH__UNSLOTH__UNSLO', <-- differs
'__UNSLOTH__!!!']
Batch 4 and batch 8 disagree at least as much, and both repeats of each model agree with each other, so this is reproducible rather than noise.
What has already been ruled out
- Padding really happens.
distinct_lengthsis 5 and 4, so the batches are genuinely ragged. An earlier run of this harness reported every prompt as 1 token because of a bug on my side, and that run is not the evidence above. - Padding side is left, read back off the tokenizer after generating rather than merely set beforehand (which is the shape of Newest Unsloth version silently FORCES Qwen2VL tokenizer padding side to right in inference, while training is left #2138).
- The models train fine. Losses fall, the adapter updates, and batch-1 output is the learned canary exactly.
The caveat, stated up front
Some divergence between batched and unbatched fp16 greedy decoding is possible from ordinary numerics: a near-tie in the argmax can flip when the batch changes the reduction order. That would normally show as an occasional single-token difference.
What is here is larger than that. The differing outputs gain a whole prefix (:\n### Answer:\n### Answer:\n, ?UNSLOTH__UNSLOTH__), which is what attending to padding looks like rather than a tie-break flip. The same positions differ in both repeats, and the same two models differ at the same prompt indices.
Where it was found
The Kaggle T4 CI legs, which assert the #3699 / #1066 / #1456 / #2138 contract on every run. Note that unsloth/Qwen3-0.6B passes the identical check on the same harness (batch 2, 4 and 8 all agreeing with batch 1), so this is not the harness being wrong about every model.
Activity
A confirmed upstream bug must not block a leg, and must not outlive i…
Stop a left-padded row that attends to nothing returning NaN (#9708) (#…
danielhanchen commentedon Sep 6, 2026
Root cause, results and a standalone repro
This was closed when #9848 merged, and that is only half the story, so the findings go here for the record. #9848 fixed a real defect: a left padded row that attends to nothing produced NaN logits in fp16 and decoded to an empty string. The rest of what this issue reports, batch sizes 2, 4 and 8 producing different greedy text from batch size 1, is not an unsloth bug and not a bug in any one library. It is bf16 and fp16 rounding that depends on the shape of the batch, and it reproduces with plain transformers and unsloth never imported. In float32 every batch size agrees.
Setup
All arms: greedy decoding with sampling off, 8 prompts with distinct token lengths so left padding is real (
[13, 13, 14, 15, 13, 12, 15, 16]on the payload prompts), TF32 off, deterministic cuBLAS workspace, deterministic algorithms on, every arm run twice. Run to run every arm was byte identical, so none of this is nondeterminism. Hardware for the sweep was one B200 with torch 2.13.0+cu130 and transformers 5.16.1; the original report was on a T4, so this is not a T4 property either.Plain transformers, no unsloth
agreesis whether every row at that batch size reproduces its own batch size 1 output.prefill deltais the max absolute logit difference at the last prompt position between the prompt alone and the same prompt inside the padded batch of 8.Which batch sizes disagree moves around with the model, the prompt set and the dtype, which is what shape dependent rounding looks like. What never moves: fp32 agrees everywhere, and the fp32 prefill delta is four orders of magnitude smaller.
unsloth versus plain transformers on the same checkpoint
Matched A/B on
gemma-4-e2b-it-unsloth-bnb-4bit, same prompts, same generation settings, both arms sdpa:They differ in which sizes disagree, not in whether they do. unsloth holds 397 of the parameters in fp32 where plain holds all of them in bf16, so the two arms are not running identical arithmetic and were never expected to match each other row for row.
Also ruled out on unsloth's side, each by measurement:
UNSLOTH_COMPILE_DISABLE=1(flips the untrained gemma-4 and Qwen3.5-2B arms to agreement and leaves the trained gemma-4 arm disagreeing, so it is another shape knob, not a fix), the static cache the generate wrapper selects (disagrees with DynamicCache too), and the bitsandbytes dequant path (fused and unfused give identical results; NF4 versus 16 bit changes nothing in fp32).transformers version sweep
39 releases from 4.57.0 to 5.16.1 through the unsloth loader in 4 bit. gemma-4 needs 5.5.0 or newer and Qwen3.5 needs 5.2.0 or newer, so older releases refuse the model and are recorded as such rather than as agreement.
There is no version to go back to. The one boundary that shows up, 5.14.0 changing gemma-4 from "2 and 8 agree, 4 disagrees" to "all disagree", is the rewrite of
masking_utils._ignore_causal_mask_sdpa, which skips the materialised mask for a static cache prefill at offset 0. Vetoing that skip changes the batch size 1 outputs, not the batched ones. It moves the rounding, it does not cause it.Mechanism
Layer by layer traces on gemma-4 in bf16, plain transformers, comparing each row inside the batch of 8 against the same prompt alone. Three independent sources:
is_causalfast path, which SDPA serves with the flash kernel. A padded batch carries a mask and is served by the memory efficient kernel. Different kernels, different accumulation order, and the first difference appears in layer 0 attention for every row, including the longest prompt, which receives no padding at all.attn_implementation="eager"removes this source (gemma-4 then agrees at 2 and 4) and exposes the next one.o_proj, which receives identical inputs. The projection sees 16 token rows alone and 128 inside the batch, and cuBLAS picks a different tile for the two shapes. Replayingo_projon the equal input reproduces the difference exactly. 277 equal input replays of decode step linears, by contrast, never differed: batch 1 versus 8 at M=1 versus M=8 is bit identical in bf16, so this is a prefill effect, not a decode one.Forcing all three to their batch size 1 shapes makes every decoder output and both logit vectors bit identical across all 8 rows. Forcing only one of them does not. The bf16 hidden state deltas peak at 1.5 in prefill and 1.0 in decode; the same traces in fp32 peak at 9.6e-5 and 6.5e-5.
What this means in practice
Standalone repro
Needs only torch and transformers, plus bitsandbytes for
--load-in-4bit. Prints agreement per batch size, the rows that differ, the prefill logit delta, and whether the run was deterministic across two repeats.repro_batched_greedy_divergence.py
Kaggle T4: describe the batched-generation expectation as what it is (#…
Left padded training: fully masked pad queries attend only their own …