Skip to content

[Bug] Batched generation with left-padding and caching produces incorrect output #3699

Closed

Description

@afspies

Ran into this issue when doing batched inference with a finetuned version of JetBrain's Mellum. Claude spent a while digging

When using FastLanguageModel.from_pretrained() with model.generate() on left-padded batches, sequences that require padding produce garbage/incorrect output. The longest sequence (which requires no padding) generates correctly.

Reproduction

import torch
from unsloth import FastLanguageModel

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="Qwen/Qwen2.5-0.5B",
    max_seq_length=2048,
    load_in_4bit=True,
)
FastLanguageModel.for_inference(model)

examples = [
    "Short prompt",
    "This is a longer prompt with more tokens",
]

# Single sequence generation works correctly
for ex in examples:
    enc = tokenizer.encode(ex, add_special_tokens=True)
    out = model.generate(torch.tensor([enc]).to(model.device), max_new_tokens=30)
    print(tokenizer.decode(out[0][len(enc):]))  # Correct output

# Batched generation with left-padding produces garbage for shorter sequences
tokenizer.padding_side = 'left'
tokenizer.pad_token = tokenizer.eos_token

batch = tokenizer(examples, padding=True, return_tensors='pt', add_special_tokens=True)
out_batch = model.generate(
    batch['input_ids'].to(model.device),
    attention_mask=batch['attention_mask'].to(model.device),
    max_new_tokens=30,
)
# Shorter sequence (index 0) produces garbage, longer sequence (index 1) is correct

Expected Behavior

Batched generation should produce the same output as single-sequence generation for each example.

Actual Behavior

  • Sequences requiring padding produce garbage/repetitive output
  • The longest sequence (no padding needed) generates correctly
  • Using pure Transformers (without Unsloth) produces correct output for all sequences

Root Cause Analysis

The issue appears to be in unsloth/models/llama.py:

Lines 185-186 in _fast_prepare_inputs_for_generation:

if "cache_position" in kwargs:
    kwargs["position_ids"] = kwargs["cache_position"]

cache_position is a single tensor that's the same for all sequences in the batch. With left-padded batches, each sequence needs different position_ids computed from its attention_mask, like HuggingFace does:

position_ids = attention_mask.long().cumsum(-1) - 1
position_ids.masked_fill_(attention_mask == 0, 1)

Environment

  • Unsloth: 2025.10.12
  • Transformers: 4.57.1
  • PyTorch: 2.8.0+cu128
  • GPU: NVIDIA A10G

Workarounds

Option 1: Disable KV cache (slower but works with Unsloth)

out_batch = model.generate(
    batch['input_ids'].to(model.device),
    attention_mask=batch['attention_mask'].to(model.device),
    max_new_tokens=30,
    use_cache=False,  # Disables KV cache, fixes the issue
)

Option 2: Use pure Transformers (faster, no Unsloth)

from transformers import AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel

base_model = AutoModelForCausalLM.from_pretrained(
    model_name,
    quantization_config=BitsAndBytesConfig(load_in_4bit=True, ...),
    device_map="auto",
)
# Works correctly for batched generation with left-padding and use_cache=True

Activity

  1. changed the title [-]Batched generation with left-padding produces incorrect output[/-] [+]Batched generation with left-padding and caching produces incorrect output[/+] on Dec 9, 2025
  2. changed the title [-]Batched generation with left-padding and caching produces incorrect output[/-] [+][Bug] Batched generation with left-padding and caching produces incorrect output[/+] on Dec 10, 2025
  3. numb3r33 commented on Dec 25, 2025

    @numb3r33
    Contributor

    Thanks @afspies for the excellent report + repro, I can also reproduce the behaviour: left-padded batched generation with use_cache=True produces incorrect output for the shorter (padded) rows, while the longest row is fine. This matches your hypothesis.

    Root cause looks correct: in unsloth/models/llama.py::_fast_prepare_inputs_for_generation, we’re doing position_ids = cache_position, but cache_position is 1D and shared across the batch, while position_ids must be per-row when the batch is left-padded (RoPE positions should start at 0 for the first real token of each row).

    Proposed fix: when cache_position is present and we have a 2D attention_mask, compute position_ids the same way Transformers does:

    position_ids = attention_mask.long().cumsum(-1) - 1; position_ids.masked_fill_(attention_mask == 0, 1)

    During decode we only feed the last token (input_ids[:, -1:]), so we should pass the corresponding last-step position id per row (shape [bs, 1]).

    Edge cases: if attention_mask is None (or already converted to a 4D causal mask), we should not try to cumsum it, in that case we can safely fall back to existing behavior because there’s no padding signal (or padding shouldn’t be used without an attention mask anyway).

    @danielhanchen if the reasoning and proposed fix looks alright shall I’ll put up a PR with the fix + a regression test that fails on main and passes with the patch.

  4. added a commit that references this issue on Dec 29, 2025
    82f7fcb
  5. added a commit that references this issue on Feb 17, 2026
    b5753bb
  6. mmathew23 commented on Feb 20, 2026

    @mmathew23
    Contributor

    @afspies i submitted #4083 which should fix your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions