Skip to content

Viking tokenizer support #7328

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions convert-hf-to-gguf-update.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class TOKENIZER_TYPE(IntEnum):
{"name": "jina-v2-es", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/jinaai/jina-embeddings-v2-base-es", },
{"name": "jina-v2-de", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/jinaai/jina-embeddings-v2-base-de", },
{"name": "smaug-bpe", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/abacusai/Smaug-Llama-3-70B-Instruct", },
{"name": "viking", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/LumiOpen/Viking-7B", }, # Same for 13B and 33B
]


Expand Down
3 changes: 3 additions & 0 deletions convert-hf-to-gguf.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ def get_vocab_base_pre(self, tokenizer) -> str:
if chkhsh == "c136ed14d01c2745d4f60a9596ae66800e2b61fa45643e72436041855ad4089d":
# ref: https://huggingface.co/abacusai/Smaug-Llama-3-70B-Instruct
res = "smaug-bpe"
if chkhsh == "7fc505bd3104ca1083b150b17d088b59534ede9bde81f0dd2090967d7fe52cee":
# ref: https://huggingface.co/LumiOpen/Viking-7B
res = "viking"

if res is None:
logger.warning("\n")
Expand Down
12 changes: 9 additions & 3 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4549,9 +4549,10 @@ static void llm_load_vocab(
tokenizer_pre == "default") {
vocab.type_pre = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
} else if (
tokenizer_pre == "llama3" ||
tokenizer_pre == "llama-v3" ||
tokenizer_pre == "llama-bpe") {
tokenizer_pre == "llama3" ||
tokenizer_pre == "llama-v3" ||
tokenizer_pre == "llama-bpe" ||
tokenizer_pre == "viking-7b") {
Copy link

Choose a reason for hiding this comment

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

This needs two changes:

  • tokenizer_pre needs to be updated to match the "viking" in convert-hf-to-gguf.py
  • this needs its own if statement block which sets vocab.type_pre = LLAMA_VOCAB_PRE_TYPE_VIKING

vocab.type_pre = LLAMA_VOCAB_PRE_TYPE_LLAMA3;
} else if (
tokenizer_pre == "deepseek-llm") {
Expand Down Expand Up @@ -12580,6 +12581,11 @@ struct llm_tokenizer_bpe {
"(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
});
break;
case LLAMA_VOCAB_PRE_TYPE_VIKING:
word_collection = unicode_regex_split(text, {
" ?[^(\\s|[.,!?…。,、।۔،])]+",
Copy link

Choose a reason for hiding this comment

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

I think this regex works for the first test that fails, but I'm still left with one failing test I don't understand.

                        " ?[^\\s.,!?…。,、।۔،]+",

});
break;
default:
// default regex for BPE tokenization pre-processing
word_collection = unicode_regex_split(text, {
Expand Down
1 change: 1 addition & 0 deletions llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ extern "C" {
LLAMA_VOCAB_PRE_TYPE_OLMO = 12,
LLAMA_VOCAB_PRE_TYPE_DBRX = 13,
LLAMA_VOCAB_PRE_TYPE_SMAUG = 14,
LLAMA_VOCAB_PRE_TYPE_VIKING = 15,
};

// note: these values should be synchronized with ggml_rope
Expand Down
35 changes: 22 additions & 13 deletions tests/test-tokenizer-0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int main(int argc, char **argv) {
atexit([]() { console::cleanup(); });
#endif

bool success = true;
int n_failed = 0;

const auto k_tests = [&]() -> llama_tests {
if (!fname_text.empty()) {
Expand Down Expand Up @@ -214,22 +214,27 @@ int main(int argc, char **argv) {
}

if (!correct) {
fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
fprintf(stderr, "%s : detokenized to: '%s' instead of '%s'\n", __func__,
llama_detokenize_bpe(ctx, res).c_str(),
llama_detokenize_bpe(ctx, test_kv.second).c_str());
fprintf(stderr, "%s : expected tokens: ", __func__);
fprintf(stderr, "failed test: '%s'\n", test_kv.first.c_str());
auto detok = llama_detokenize_bpe(ctx, res).c_str();
auto expected = llama_detokenize_bpe(ctx, test_kv.second).c_str();
fprintf(stderr, "detokenized to: '%s'\n", detok);
if(detok != expected) {
fprintf(stderr, "but we wanted: '%s'\n", expected);
} else {
fprintf(stderr, "(which matches the expected output)\n", expected);
}
fprintf(stderr, "expected tokens: \n");
for (const auto & t : test_kv.second) {
fprintf(stderr, "%6d '%s', ", t, llama_token_to_piece(ctx, t).c_str());
fprintf(stderr, "%6d '%s'\n", t, llama_token_to_piece(ctx, t).c_str());
}
fprintf(stderr, "\n");
fprintf(stderr, "%s : got tokens: ", __func__);
fprintf(stderr, "got tokens: \n");
for (const auto & t : res) {
fprintf(stderr, "%6d '%s', ", t, llama_token_to_piece(ctx, t).c_str());
fprintf(stderr, "%6d '%s'\n", t, llama_token_to_piece(ctx, t).c_str());
}
fprintf(stderr, "\n");
fprintf(stderr, "\n====================\n");

success = false;
n_failed ++;
}
}

Expand Down Expand Up @@ -286,7 +291,11 @@ int main(int argc, char **argv) {
llama_backend_free();

printf("\n");
printf("Tests %s\n", success ? "passed" : "failed");
if(n_failed) {
printf("%d tests failed\n", n_failed);
} else {
printf("Tests passed\n");
}

return success ? 0 : 3;
return !n_failed ? 0 : 3;
}
Loading