Repository navigation
fix(vllm): keep each row's original prefix width in generate output (#3921) - #3941
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_inputsstrips the padding that vLLM cannot represent. It then rebuilt each returned row from vLLM's unpadded prompt echo:A Hugging Face caller slices the result at its own input width,
output[:, input_ids.shape[1]:]. For a padded row the completion therefore startsoriginal_width - unpadded_widthtokens too early, so the caller loses the head of the completion (or reads prompt IDs). The issue's repro:Fix
Capture the caller's original token rows before normalization strips the padding, and use them as the prefix:
original_rowsreuses_coerce_token_batch, the same coercion_normalize_vllm_inputsapplies, 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
Tests
New
tests/test_vllm_generate_alignment.pydrivesvllm_generatewith a fake engine (vLLM is not installed in these venvs;_require_vllmand_build_sampling_paramsare stubbed), and asserts both the tensor that reaches vLLM and the returned alignment:test_left_padded_batch_keeps_the_completion_at_the_input_width[[11, 12], [21, 22, 23, 24]], slice is exacttest_right_padded_batch_keeps_the_completion_at_the_input_widthtest_unpadded_batch_is_unchangedtest_text_prompts_keep_the_prompt_echotest_multiple_return_sequences_share_the_restored_prefixn > 1row keeps the widthtest_single_prompt_token_rows_keep_the_widthtest_pad_token_falls_back_to_the_tokenizerValidation
pytest tests/test_vllm_generate_alignment.py -qpytest tests/test_vllm_input_normalization.py tests/test_vllm_generate_alignment.py -q(4090, cu130)pytest tests/test_vllm.py -qruff check gptqmodel/utils/vllm.py tests/test_vllm_generate_alignment.pyCPU-only change; no GPU required. Documented the returned-row contract in the existing
docs/runtime-prompt-input-normalization.mdpage (it already owns the input-side contract from #3900) and extended its validation table.Notes
gptqmodel/utils/openai_server.pyconsumes the samegenerateoutput and needs no change: it now receives the aligned tensor.