-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Ignore invalid UTF-8 input in the BPE tokenizer #11729
Merged
ggerganov
merged 1 commit into
ggml-org:master
from
cfillion:bpe-tokenize-mojibake-noexcept
Feb 7, 2025
Merged
Ignore invalid UTF-8 input in the BPE tokenizer #11729
ggerganov
merged 1 commit into
ggml-org:master
from
cfillion:bpe-tokenize-mojibake-noexcept
Feb 7, 2025
Conversation
This file contains 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
ggerganov
approved these changes
Feb 7, 2025
slaren
reviewed
Feb 7, 2025
src/unicode.cpp
Outdated
Comment on lines
621
to
628
try { | ||
result.push_back(unicode_cpt_from_utf8(utf8, offset)); | ||
} | ||
catch (const std::invalid_argument & /*ex*/) { | ||
++offset; | ||
result.emplace_back(0xFFFD); // replacement character | ||
} |
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.
Please add a comment explaining this logic. This is a hack and we will need to deal with this properly at some point.
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.
Done.
Silently insert U+FFFD(s) (Unicode replacement character) instead until the next valid codepoint can be found. This fixes `llama_tokenize` throwing an exception across the C API boundary or libllama's module boundary (the caller's runtime might be incompatible!) Returing a proper error code might be desirable, however the signature of `llama_tokenize` doesn't allow it as all return values already have existing meaning.
6c70a3a
to
cff1c3b
Compare
slaren
approved these changes
Feb 7, 2025
tinglou
pushed a commit
to tinglou/llama.cpp
that referenced
this pull request
Feb 13, 2025
Silently insert U+FFFD(s) (Unicode replacement character) instead until the next valid codepoint can be found. This fixes `llama_tokenize` throwing an exception across the C API boundary or libllama's module boundary (the caller's runtime might be incompatible!) Returing a proper error code might be desirable, however the signature of `llama_tokenize` doesn't allow it as all return values already have existing meaning.
orca-zhang
pushed a commit
to orca-zhang/llama.cpp
that referenced
this pull request
Feb 26, 2025
Silently insert U+FFFD(s) (Unicode replacement character) instead until the next valid codepoint can be found. This fixes `llama_tokenize` throwing an exception across the C API boundary or libllama's module boundary (the caller's runtime might be incompatible!) Returing a proper error code might be desirable, however the signature of `llama_tokenize` doesn't allow it as all return values already have existing meaning.
arthw
pushed a commit
to arthw/llama.cpp
that referenced
this pull request
Feb 26, 2025
Silently insert U+FFFD(s) (Unicode replacement character) instead until the next valid codepoint can be found. This fixes `llama_tokenize` throwing an exception across the C API boundary or libllama's module boundary (the caller's runtime might be incompatible!) Returing a proper error code might be desirable, however the signature of `llama_tokenize` doesn't allow it as all return values already have existing meaning.
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.
This fixes
llama_tokenize
throwing an exception across the C API boundary or libllama's module boundary (the caller's C++ runtime might be incompatible, similarly to #11727!) by silently insertingU+FFFD
(s) (Unicode replacement character) until the next valid codepoint can be found.Non-C++ callers cannot reasonably catch that, C++ callers across the DLL boundary might invoke undefined behavior, and the documentation doesn't state that the input must be fully valid UTF-8 or else face unavoidable
std::terminate()
.Returning a proper error code might be desirable, however
llama_tokenize
already assigns meaning to all return values.Other areas of llama.cpp already behave similarly when encountering invalid input:
https://github.com/ggerganov/llama.cpp/blob/b7552cfcbc7defccd8bdefd0a7b9c47d145ed3d7/src/llama-vocab.cpp#L1041-L1048