server: fix the disappearance of the end of the text when streaming with stop strings#9867
Merged
Merged
Conversation
ggerganov
reviewed
Oct 14, 2024
|
|
||
| // check if there is any token to predict | ||
| if (stop_pos == std::string::npos || (!slot.has_next_token && !is_stop_full && stop_pos > 0)) { | ||
| if (stop_pos == std::string::npos || is_stop_full || (!slot.has_next_token && !is_stop_full && stop_pos > 0)) { |
Member
There was a problem hiding this comment.
I think this if will always evaluate to true:
- if
stop_pos == std::string::npos->true - if
stop_pos != std::string::npos, thenis_stop_full == truedue to line 1075 ->true
Contributor
Author
There was a problem hiding this comment.
I have attempted to simplify this logic in the new commit. The text will not be sent only when the partial match is found. And partial match will be searched for only when the current token is not the last one.
ggerganov
approved these changes
Oct 16, 2024
drollings
pushed a commit
to drollings/llama.cpp
that referenced
this pull request
Oct 18, 2024
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
dsx1986
pushed a commit
to dsx1986/llama.cpp
that referenced
this pull request
Oct 29, 2024
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
arthw
pushed a commit
to arthw/llama.cpp
that referenced
this pull request
Nov 15, 2024
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
arthw
pushed a commit
to arthw/llama.cpp
that referenced
this pull request
Nov 18, 2024
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
Seunghhon
pushed a commit
to Seunghhon/llama.cpp
that referenced
this pull request
Apr 26, 2026
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
ljubomirj
pushed a commit
to ljubomirj/llama.cpp
that referenced
this pull request
May 6, 2026
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
my-other-github-account
pushed a commit
to my-other-github-account/llama.cpp
that referenced
this pull request
May 15, 2026
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
my-other-github-account
pushed a commit
to my-other-github-account/llama.cpp
that referenced
this pull request
May 15, 2026
* server: fix the disappearance of the end of the text when streaming with stop strings * simplify "send text" checks
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.
The problem: the end of the text may not be pushed to the API-client when streaming with stop strings enabled.
For example, let's test the following prompt:
The stop string will be
\n(stopping at the end of a dialog line). The next predicted character should be the question mark (it actually depends on the model and the probabilities, but in this case, let's assume that theseedis chosen so that the next character is?).The bug depends heavily on the model's tokenizer. In my case the bug can be seen on
Llama-3.2-3B-Instruct-Q8_0.ggufmodel.First, let's try it without streaming:
It gives the correct result:
Now let's try it with streaming:
The question mark disappeared. And it's also not featured in the
stopping_word. So it's completely lost and the API-client won't be able to restore it.It happens because the next token returned by the model contains both a question mark and the stop string:
?\n\n. And the current code skips sending the current token completely if it contains the stop string.The change in this PR sends the remainder of the token to the API-client in this case. When
is_stop_full=trueit's safe to send the response, because the stop string and everything after it will already be truncated from thegenerated_textat this point.The response with this PR applied: