Skip to content
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

server : clean up built-in template detection #11026

Merged
merged 4 commits into from
Dec 31, 2024

Conversation

ngxson
Copy link
Collaborator

@ngxson ngxson commented Dec 31, 2024

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

@github-actions github-actions bot added the python python script changes label Dec 31, 2024
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) {
Copy link
Collaborator

@slaren slaren Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

@ngxson ngxson Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up just flipping the condition to if (res > 0), it's more readable this way.

@ngxson ngxson merged commit 45095a6 into ggerganov:master Dec 31, 2024
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples python python script changes server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants