Skip to content

fix(infer): exclude padding from Transformers prompt usage - #10094

Merged
hjh0119 merged 1 commit into
modelscope:mainfrom
Excelius-Wang:fix/transformers-prompt-usage
Sep 10, 2026
Merged

hjh0119 merged 1 commit into
modelscope:mainfrom
Excelius-Wang:fix/transformers-prompt-usage

Conversation

@Excelius-Wang

Copy link
Copy Markdown
Contributor

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.

@hjh0119
hjh0119 merged commit 1ed6173 into modelscope:main Sep 10, 2026
2 of 3 checks passed
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.

2 participants