server : clean up built-in template detection#11026
Merged
ngxson merged 4 commits intoggml-org:masterfrom Dec 31, 2024
Merged
Conversation
slaren
reviewed
Dec 31, 2024
common/common.cpp
Outdated
| static const char * template_key = "tokenizer.chat_template"; | ||
| // call with NULL buffer to get the total size of the string | ||
| int32_t res = llama_model_meta_val_str(model, template_key, NULL, 0); | ||
| if (res < 2) { |
Member
There was a problem hiding this comment.
What's the reason for the 2 here? llama_model_meta_val_str returns either -1 if the key is not found, or the length of the string.
Collaborator
Author
There was a problem hiding this comment.
Hmm yeah someone added this in the PR to fix the null terminator. I suppose that it's to make sure model_template.size() - 1 stay positive.
In any case, we can change it to
Suggested change
| if (res < 2) { | |
| if (res < 0) { |
Then below:
return std::string(model_template.data(), res);
What do you think?
Member
There was a problem hiding this comment.
model_template.size() should always be at least 1, since the size is res + 1. So I don't expect the -1 to cause issues, but that should also work.
Collaborator
Author
There was a problem hiding this comment.
I ended up just flipping the condition to if (res > 0), it's more readable this way.
slaren
approved these changes
Dec 31, 2024
arthw
pushed a commit
to arthw/llama.cpp
that referenced
this pull request
Feb 26, 2025
* server : clean up built-in template detection * fix compilation * add chat template test * fix condition
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.
Ref: #10900 (comment)
Bug: when starting the server, templates longer than 2048 bytes is shown as "not supported" in the log output, while it's still being formatted correctly in
/v1/chat/completions