MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / llm_load_vocab

Function llm_load_vocab

llama.cpp:2473–2701  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2471static llama_token llama_byte_to_token(const llama_vocab & vocab, uint8_t ch);
2472
2473static void llm_load_vocab(
2474 llama_model_loader & ml,
2475 llama_model & model) {
2476 auto & vocab = model.vocab;
2477
2478 struct gguf_context * ctx = ml.ctx_gguf;
2479
2480 const auto kv = LLM_KV(model.arch);
2481
2482 const int token_idx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_LIST).c_str());
2483 if (token_idx == -1) {
2484 throw std::runtime_error("cannot find tokenizer vocab in model file\n");
2485 }
2486
2487 const float * scores = nullptr;
2488 const int score_idx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_SCORES).c_str());
2489 if (score_idx != -1) {
2490 scores = (const float * ) gguf_get_arr_data(ctx, score_idx);
2491 }
2492
2493 const int * toktypes = nullptr;
2494 const int toktype_idx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_TOKEN_TYPE).c_str());
2495 if (toktype_idx != -1) {
2496 toktypes = (const int * ) gguf_get_arr_data(ctx, toktype_idx);
2497 }
2498
2499 // determine vocab type
2500 {
2501 std::string tokenizer_name;
2502
2503 GGUF_GET_KEY(ctx, tokenizer_name, gguf_get_val_str, GGUF_TYPE_STRING, true, kv(LLM_KV_TOKENIZER_MODEL));
2504
2505 if (tokenizer_name == "llama") {
2506 vocab.type = LLAMA_VOCAB_TYPE_SPM;
2507
2508 // default special tokens
2509 vocab.special_bos_id = 1;
2510 vocab.special_eos_id = 2;
2511 vocab.special_unk_id = 0;
2512 vocab.special_sep_id = -1;
2513 vocab.special_pad_id = -1;
2514 } else if (tokenizer_name == "gpt2") {
2515 vocab.type = LLAMA_VOCAB_TYPE_BPE;
2516
2517 // read bpe merges and populate bpe ranks
2518 const int merges_keyidx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_MERGES).c_str());
2519 if (merges_keyidx == -1) {
2520 throw std::runtime_error("cannot find tokenizer merges in model file\n");
2521 }
2522
2523 const int n_merges = gguf_get_arr_n(ctx, merges_keyidx);
2524
2525 for (int i = 0; i < n_merges; i++) {
2526 const std::string word = gguf_get_arr_str(ctx, merges_keyidx, i);
2527 GGML_ASSERT(codepoints_from_utf8(word).size() > 0);
2528
2529 std::string first;
2530 std::string second;

Callers 1

llama_model_loadFunction · 0.85

Calls 15

codepoints_from_utf8Function · 0.85
llama_byte_to_tokenFunction · 0.85
llama_tokenize_internalFunction · 0.85
findMethod · 0.80
substrMethod · 0.80
lengthMethod · 0.80
LLM_KVClass · 0.70
gguf_find_keyFunction · 0.70
gguf_get_arr_dataFunction · 0.70
gguf_get_arr_nFunction · 0.70
gguf_get_arr_strFunction · 0.70
utf8_lenFunction · 0.70

Tested by

no test coverage detected