fix(infer): exclude padding from Transformers prompt usage - #10094
Merged
hjh0119 merged 1 commit intoSep 10, 2026
Merged
Conversation
hjh0119
approved these changes
Sep 10, 2026
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.
TransformersEngine uses the padded batch width as prompt_tokens for every response. When differently sized prompts are batched together, shorter requests report padding as input tokens in both streaming and non-streaming responses. Forward-only tasks share the same accounting issue.
Allow the existing token-count helper to count one batch row using its standard 2D attention mask, and use that count when constructing response usage. Calls without a batch index keep returning the padded width for generation slicing and maximum-token calculations. Streaming counts are computed once before generation starts. Missing or higher-dimensional masks retain the existing fallback behavior.
Validation: five CPU regression tests cover left/right padding, inputs_embeds, missing/4D-mask fallback, generation slicing/budgets, valid tokens sharing the pad ID, multiple returned sequences, streaming usage and forward-only usage. A separate actual tiny-Qwen2 probe uses 30 unchanged SQuAD, CMRC 2018 and HumanEval prompts: 27 requests were overcounted upstream (4,100 reported prompt tokens versus 3,349 when run individually). After the fix, batched and streamed counts match individual requests, and generated token IDs and streamed text match upstream. Applicable pre-commit checks pass. This uses a local tokenizer and random tiny model, not pretrained quality evaluation or a GPU benchmark.
The same 30 public prompts were also interleaved across datasets and rerun in batches of 7 (including a two-request tail) and 30. Upstream reported 6,855 and 7,500 prompt tokens respectively; both fixed runs report the single-request total of 3,349, with unchanged generation outputs.