Skip to content

fix: remove off-by-one in inference prompt truncation#3890

Open
Chessing234 wants to merge 1 commit into
lm-sys:mainfrom
Chessing234:fix/inference-truncate-max-src-len
Open

fix: remove off-by-one in inference prompt truncation#3890
Chessing234 wants to merge 1 commit into
lm-sys:mainfrom
Chessing234:fix/inference-truncate-max-src-len

Conversation

@Chessing234

Copy link
Copy Markdown

Summary

When the prompt must be truncated to fit context_len, max_src_len subtracted an extra token (- 1), so input_ids[-max_src_len:] dropped one more prompt token than needed.

Root cause

With max_new_tokens == context_len, max_src_len became -1, and input_ids[-(-1):] is input_ids[1:], dropping the first prompt token (e.g. <s>). Slicing the generated output with input_echo_len then left part of the prompt in the decoded response.

Fix

Use max_src_len = context_len - max_new_tokens. When the budget is exactly full, max_src_len = 0 and the full truncated prompt is kept; when max_new_tokens exceeds context_len, only the minimum required tokens are dropped.

Fixes #1912

Made with Cursor

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

inference truncate causes “output_ids” to be incorrectly sliced

1 participant