fix: remove off-by-one in inference prompt truncation#3890
Open
Chessing234 wants to merge 1 commit into
Open
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Summary
When the prompt must be truncated to fit
context_len,max_src_lensubtracted an extra token (- 1), soinput_ids[-max_src_len:]dropped one more prompt token than needed.Root cause
With
max_new_tokens == context_len,max_src_lenbecame-1, andinput_ids[-(-1):]isinput_ids[1:], dropping the first prompt token (e.g.<s>). Slicing the generated output withinput_echo_lenthen 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 = 0and the full truncated prompt is kept; whenmax_new_tokensexceedscontext_len, only the minimum required tokens are dropped.Fixes #1912
Made with Cursor