-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bugfix] Fix edge cases for MistralTokenizer #9625
[Bugfix] Fix edge cases for MistralTokenizer #9625
Conversation
👋 Hi! Thank you for contributing to the vLLM project. Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging. To run CI, PR reviewers can do one of these:
🚀 |
@patrickvonplaten can you help take a look? thank you! 🙏 |
I will be adding tests to it shortly! |
return self._vocab | ||
# Convert to a Dict[str, int] to match protocol, but this is a lossy | ||
# conversion. There may be multiple token ids that decode to the same | ||
# string due to partial UTF-8 byte sequences being converted to � | ||
return {token: idx for idx, token in enumerate(self._vocab)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the advantage of doing this instead of doing:
self._vocab = {
token: idx
for idx, token in enumerate(tokenizer_.vocab())
}
in the init and just returning self._vocab? Seems like we don't have to recreate the dict every time no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that's because the conversion to a Dict[str, int]
is a lossy conversion. This results in a vocab size that's less than what the actual vocab is. If you look at #9557 (comment), you will see that if the model tries to print something outside of this lossy vocab, it will result in an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is just for vocab_size though, that could just stored as a separate field.
Another option is to memoize this method with @functools.cache
.
I am wondering though if there may be a better option that avoids string-encoding incomplete byte sequences altogether
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering though if there may be a better option that avoids string-encoding incomplete byte sequences altogether
Yeah, I think there is a refactor that could happen here, but also wanted to create a quick fix. I'll need to spend some more time looking at how the tokenizers are used to propose a refactor.
Hey @tjohnson31415, Thanks a lot for the PR, could you post an example of an LLM generation where mistral-nemo fails? I'm seeing a failure case here: #9557 (comment) but it does not include a model generation example. It would be good to have an example where a model generation leads to a failure case and not just the I'm not sure it's a good idea to ensure that the |
@patrickvonplaten I commented on the issue #9557 (comment) Unfortunately I still don't have a simple way of reproducing the failures (apart from a custom prompt which we've been using internally but I have to confirm if I can share it), will keep on trying. It usually happens on load testing while sampling. |
@patrickvonplaten @prashantgupta24 I expanded on the PR description to give full repro examples going through |
Checking now - sorry for the delay! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only managed now to look into it. I can reproduce the error and you are right in that I missed the edge case where a token is converted not only into a "a�"
e.g. a non-complete unicode sequence including another character in the beginning!
Great catch!
The changes done to the def convert_tokens_to_string
make a lot of sense to me.
The only thing I have still trouble understanding why we can't just save the created dict at init - I don't see how this would change anything (and it also worked in my experiments)
Thanks a ton @tjohnson31415 and @prashantgupta24 for providing the examples and fixing it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed-off-by: Travis Johnson <[email protected]>
Signed-off-by: Travis Johnson <[email protected]>
Signed-off-by: Travis Johnson <[email protected]>
Signed-off-by: Prashant Gupta <[email protected]>
Signed-off-by: Prashant Gupta <[email protected]>
Signed-off-by: Prashant Gupta <[email protected]>
Co-authored-by: Patrick von Platen <[email protected]> Signed-off-by: Travis Johnson <[email protected]>
2e6978d
to
5dbe494
Compare
5dbe494
to
fbd98cc
Compare
… with API of other tokenizers Signed-off-by: Travis Johnson <[email protected]>
fbd98cc
to
516d39b
Compare
Signed-off-by: Travis Johnson <[email protected]>
Signed-off-by: Travis Johnson <[email protected]>
516d39b
to
b7b2116
Compare
I did a little bit of refactoring,rebased on
@njhill Is there a way to re-run just the failing test? In updating the tests, I found another bug... The |
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Signed-off-by: Linkun Chen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Signed-off-by: Richard Liu <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Signed-off-by: Loc Huynh <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Signed-off-by: Sumit Dubey <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Signed-off-by: Maxime Fournioux <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]> Signed-off-by: Tyler Michael Smith <[email protected]>
Signed-off-by: Travis Johnson <[email protected]> Signed-off-by: Prashant Gupta <[email protected]> Co-authored-by: Prashant Gupta <[email protected]> Co-authored-by: Patrick von Platen <[email protected]>
We have encountered a couple of bugs when using
--tokenizer-mode mistral
withmistralai/Pixtral-12B-2409
(i.e. the V3-Tekken tokenizer) that cause the engine to crash with an error when certain tokens are generated. The exception is aKeyError
when converting from strings to token ids, after converting from ids to string. Both bugs found occur becaue the code expects that each token id maps to a unique string and vice-versa, but the MistralTokenizer is byte-based and there are collisions between id and string due to the replacement character, �, replacing bytes of a partial UTF-8 codepoint.One case we've seen is due to an empty string:
Which can be triggered with:
The empty string is mapped from a token when the token id exceeds the
len()
of the tokenizer REF. With the Mistral Tokenizer, there is a mismatch in the length of the vLLMMistralTokenizer
and the underlying Tekkenizer tokenizer:vs
len(tokenizer)
is computed aslen(tokenizer._vocab)
where _vocab is constructed as aDict[str, int]
, i.e. text to id. The underlying Tekkenizer’svocab()
is aList[str]
. The construction of the Dict is collapsing entries where the text matches. This occurs when the text contains the�
character replacing an incomplete codepoint:For the repro case above, it happens that the token id for
CY
is 130282 which is greater than 130044 and hence maps to an empty string. The '🌶️' is necessary in the repro to put bytes in the set of tokens being incrementally detokenized to go down the code path that maps from token strings back to ids hereThe other crash we have seen is for a KeyError containing bytes:
Which can be triggered with:
(The input is "Tell me a story\n" in Burmese.)
The cause of this one is when a token id is generated that has the replacement character as well as a valid codepoint. The stringified version of such a token will be like
င်�
. If these token strings go down the code path and are converted to bytes, the replacement character is replaced with its bytes and the full byte string,b'\xe1\x80\x84\xe1\x80\xba\xef\xbf\xbd'
no longer maps to a token in the tokenizer. Tokens that stringify to a single�
are already handled with the fix in #8640.FIX #9557
BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE
PR Checklist (Click to Expand)
Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.
PR Title and Classification
Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:
[Bugfix]
for bug fixes.[CI/Build]
for build or continuous integration improvements.[Doc]
for documentation fixes and improvements.[Model]
for adding a new model or improving an existing model. Model name should appear in the title.[Frontend]
For changes on the vLLM frontend (e.g., OpenAI API server,LLM
class, etc.)[Kernel]
for changes affecting CUDA kernels or other compute kernels.[Core]
for changes in the core vLLM logic (e.g.,LLMEngine
,AsyncLLMEngine
,Scheduler
, etc.)[Hardware][Vendor]
for hardware-specific changes. Vendor name should appear in the prefix (e.g.,[Hardware][AMD]
).[Misc]
for PRs that do not fit the above categories. Please use this sparingly.Note: If the PR spans more than one category, please include all relevant prefixes.
Code Quality
The PR need to meet the following code quality standards:
format.sh
to format your code.docs/source/
if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.Adding or changing kernels
Each custom kernel needs a schema and one or more implementations to be registered with PyTorch.
Tensors
require meta-functions. Meta-functions should be implemented and registered in python so that dynamic dims can be handled automatically. See above documents for a description of meta-functions.torch.libary.opcheck()
to test the function registration and meta-function for any registered ops. Seetests/kernels
for examples.Notes for Large Changes
Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with
rfc-required
and might not go through the PR.What to Expect for the Reviews
The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:
action-required
label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.Thank You
Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!