Fix truncation not keeping the last message per the documented rule - #268
Open
Osamaali313 wants to merge 1 commit into
Open
Fix truncation not keeping the last message per the documented rule#268Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
`_truncate_for_max_tokens` documents that the last message must be kept, but
the inner `drop()` only guards system messages and the last *user* message
(`last_user_message_index`); it never guards the last message itself. When
the final message is an assistant or trailing tool message (finetuning
examples always end with the assistant target), the cascade that clears
everything until the next user message can silently exclude it.
The result is a corrupted example that ends at a user message with no
assistant response, and a `Tokenized.prefix_ids` that references tokens no
longer present. Add the missing guard so the last message is always kept;
when it genuinely cannot fit, `to_drop` stays positive and the method raises
`TokenizerException("Input couldn't fit in truncate_at_max_token")`, the
documented loud failure, instead of returning a corrupted result.
Existing truncation tests always retain the last message, so behavior is
unchanged for them.
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.
Problem
_truncate_for_max_tokens(tokens/tokenizers/instruct.py) documents three rules for fitting a conversation intomax_tokens, including that the last message must be kept:But the inner
drop()only guards two cases — system messages and the last user message (last_user_message_index). The last message itself is never guarded. When the final message is an assistant or trailing tool message — finetuning examples always end with the assistant target — the "clear everything until the next user message" cascade can silently exclude it. The result is a truncated example that ends at a user message with no assistant response, plus aTokenized.prefix_idsthat references tokens no longer present intokens.This is reachable from the public API:
MistralTokenizer.encode_chat_completion(request, max_model_input_len=N)withrequest.truncate_for_context_length=True.Fix
Add the missing guard so
drop()honors the documented rule:When the last message genuinely cannot fit,
to_dropstays positive and the method raisesTokenizerException("Input couldn't fit in truncate_at_max_token")— the documented loud failure — instead of returning a corrupted result.Reproduction
[user, FINAL assistant][user, asst, user, FINAL asst]Existing
test_truncationcases all retain the last message, so they are unaffected; the all-or-nothing case now matchestest_truncation_failed's raise.