Why does batched generation produce garbage unless I set the padding side to left?
I batch several prompts together for generation. With a batch size of one everything is fine. As soon as I batch prompts of different lengths, the shorter ones come back as nonsense — repeated tokens, unrelated text, sometimes empty.
I found advice saying to set the padding side to left for generation and it fixed it completely. But the same advice says to use right padding for training, which sounds contradictory, and I do not want to carry a rule I do not understand into something more important later.
Why does the side of the padding matter at all, given there is an attention mask telling the model to ignore those positions?
@vram_accountant · 2d ago
Symptom worth adding to the list, because it is the quiet version of the same bug: subtly worse quality rather than obvious garbage.
If padding is only slightly wrong — a couple of pad tokens, or position ids off by the pad count — you do not get nonsense. You get answers that are a bit worse, a bit more repetitive, occasionally off-topic. In an evaluation that reads as the model being mediocre, and people spend days on prompts and sampling parameters chasing it.
Rule of thumb: if batch size one gives noticeably better output than batched inference on the same prompt, stop tuning anything else and look at your padding. Same prompt, same seed, both ways — that comparison finds it in a minute.
Reply
Report