Skip to content

fix(vllm): keep each row's original prefix width in generate output (#3921) - #3941

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-3921
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-3921

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3921

Root cause

vllm_generate (gptqmodel/utils/vllm.py) hands vLLM the unpadded token rows, because a vLLM token prompt is a bare ID sequence and _normalize_vllm_inputs strips the padding that vLLM cannot represent. It then rebuilt each returned row from vLLM's unpadded prompt echo:

for result in req_results:
    prompt_token_ids = list(result.prompt_token_ids)   # unpadded
    for output in result.outputs:
        outputs.append(prompt_token_ids + list(output.token_ids))

A Hugging Face caller slices the result at its own input width, output[:, input_ids.shape[1]:]. For a padded row the completion therefore starts original_width - unpadded_width tokens too early, so the caller loses the head of the completion (or reads prompt IDs). The issue's repro:

input_ids   [[0, 0, 11, 12], [21, 22, 23, 24]]
attention   [[0, 0, 1, 1], [1, 1, 1, 1]]
output      [[11, 12, 101, 102, 0, 0], [21, 22, 23, 24, 101, 102]]
slice[:, 4] [[0, 0], [101, 102]]          # first completion is gone

Fix

Capture the caller's original token rows before normalization strips the padding, and use them as the prefix:

original_rows = _original_token_rows(prompts, input_ids)   # None for a text prompt
...
prefix = list(original_rows[index]) if ... else prompt_token_ids
outputs.append(prefix + list(output.token_ids))

original_rows reuses _coerce_token_batch, the same coercion _normalize_vllm_inputs applies, so validation and error messages are unchanged. A text prompt has no caller-side width, so it keeps vLLM's prompt echo. vLLM still receives the unpadded rows.

Before / after

# before
output    [[11, 12, 101, 102, 0, 0], [21, 22, 23, 24, 101, 102]]
slice     [[0, 0], [101, 102]]

# after
output    [[0, 0, 11, 12, 101, 102], [21, 22, 23, 24, 101, 102]]
slice     [[101, 102], [101, 102]]

Tests

New tests/test_vllm_generate_alignment.py drives vllm_generate with a fake engine (vLLM is not installed in these venvs; _require_vllm and _build_sampling_params are stubbed), and asserts both the tensor that reaches vLLM and the returned alignment:

Test Pins
test_left_padded_batch_keeps_the_completion_at_the_input_width Issue repro: vLLM sees [[11, 12], [21, 22, 23, 24]], slice is exact
test_right_padded_batch_keeps_the_completion_at_the_input_width Right padding restores the same way
test_unpadded_batch_is_unchanged No padding means no change
test_text_prompts_keep_the_prompt_echo Text prompts keep the old prefix
test_multiple_return_sequences_share_the_restored_prefix Every n > 1 row keeps the width
test_single_prompt_token_rows_keep_the_width Single-row batch
test_pad_token_falls_back_to_the_tokenizer Unequal widths still pad from the tokenizer

Validation

Command Result
pytest tests/test_vllm_generate_alignment.py -q 7 passed
same file, source reverted 5 failed, 2 passed (the 2 no-change cases)
pytest tests/test_vllm_input_normalization.py tests/test_vllm_generate_alignment.py -q (4090, cu130) 15 passed
same on R9700 (rocm10.1) 15 passed
pytest tests/test_vllm.py -q skipped (vLLM is not installed in these venvs)
ruff check gptqmodel/utils/vllm.py tests/test_vllm_generate_alignment.py All checks passed

CPU-only change; no GPU required. Documented the returned-row contract in the existing docs/runtime-prompt-input-normalization.md page (it already owns the input-side contract from #3900) and extended its validation table.

Notes

  • The flow and dispatch order are unchanged — this is a localized output-construction fix — so the logic-flow diagram requirement does not apply; the row layout is recorded as a table in the existing docs page instead.
  • gptqmodel/utils/openai_server.py consumes the same generate output and needs no change: it now receives the aligned tensor.

The adapter strips per-row padding before handing token prompts to vLLM (a
vLLM token prompt is a bare ID sequence), then rebuilt each output row from
vLLM's unpadded prompt echo plus the completion. A Hugging Face caller slices
the result at the width of its own input_ids, so for a padded row the
completion started original_width - unpadded_width tokens too early: the
caller lost the head of the completion or read prompt IDs instead.

Restore the caller's own row as the prefix, which keeps the returned tensor
aligned with the input width for left-padded, right-padded, and unpadded
batches. vLLM still receives the unpadded rows. A text prompt has no
caller-side width, so it keeps the prompt echo.

Fixes #3921
@Qubitium
Qubitium merged commit 656291a into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3921 branch November 14, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vLLM generate output loses completions for shorter padded prompts when sliced at the input width

1 participant