You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
llama_model_load: error loading model: error loading model vocabulary: key not found in model: tokenizer.ggml.model
llama_model_load_from_file_impl: failed to load model
Motivation
From the benchmark data, Kimi K2 turns to be the most powerful oss model now.
llama_model_load: error loading model: error loading model vocabulary: key not found in model: tokenizer.ggml.model
llama_model_load_from_file_impl: failed to load model
As far as I can see, everything else seems to be exactly the same as deepseek-v3 architecture (it has only 1 dense layer at the start instead of 3 though).
Yeah, I got the first one working. However, I’m using a cloud instance with limited storage, so I’m unable to perform the conversion on it.
I was able to extend the storage. Here is the new issue in converting from HF:
root@rocm-jupyter-gpu-mi300x1-192gb-devcloud-atl1:~/llama.cpp# python3 convert_hf_to_gguf.py /scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e
INFO:hf-to-gguf:Loading model: 2f7e011530aa89b56ab4277c26fc4aed3c52822e
WARNING:hf-to-gguf:Failed to load model config from /scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e: The repository /scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e contains custom code which must be executed to correctly load the model. You can inspect the repository content at /scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e .
You can inspect the repository content at https://hf.co//scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e.
Please pass the argument `trust_remote_code=True` to allow custom code to be run.
WARNING:hf-to-gguf:Trying to load config.json instead
INFO:hf-to-gguf:Model architecture: DeepseekV3ForCausalLM
WARNING:hf-to-gguf:Failed to load model config from /scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e: The repository /scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e contains custom code which must be executed to correctly load the model. You can inspect the repository content at /scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e .
You can inspect the repository content at https://hf.co//scratch/hub/models--moonshotai--Kimi-K2-Instruct/snapshots/2f7e011530aa89b56ab4277c26fc4aed3c52822e.
Please pass the argument `trust_remote_code=True` to allow custom code to be run.
WARNING:hf-to-gguf:Trying to load config.json instead
INFO:gguf.gguf_writer:gguf: This GGUF file is for Little Endian only
INFO:hf-to-gguf:Exporting model...
INFO:hf-to-gguf:gguf: loading model weight map from 'model.safetensors.index.json'
INFO:hf-to-gguf:gguf: loading model part 'model-1-of-61.safetensors'
INFO:hf-to-gguf:token_embd.weight, torch.bfloat16 --> F16, shape = {7168, 163840}
INFO:hf-to-gguf:blk.0.attn_norm.weight, torch.bfloat16 --> F32, shape = {7168}
INFO:hf-to-gguf:blk.0.ffn_down.weight, torch.float8_e4m3fn --> F16, shape = {18432, 7168}
Traceback (most recent call last):
File "/root/llama.cpp/convert_hf_to_gguf.py", line 7411, in<module>main()
File "/root/llama.cpp/convert_hf_to_gguf.py", line 7405, in main
model_instance.write()
File "/root/llama.cpp/convert_hf_to_gguf.py", line 410, in write
self.prepare_tensors()
File "/root/llama.cpp/convert_hf_to_gguf.py", line 5679, in prepare_tensors
super().prepare_tensors()
File "/root/llama.cpp/convert_hf_to_gguf.py", line 277, in prepare_tensors
fornew_name, data_torchin (self.modify_tensors(data_torch, name, bid)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/llama.cpp/convert_hf_to_gguf.py", line 5676, in modify_tensors
return [(self.map_tensor_name(name), data_torch)]
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/llama.cpp/convert_hf_to_gguf.py", line 236, in map_tensor_name
raise ValueError(f"Can not map tensor {name!r}")
ValueError: Can not map tensor 'model.layers.0.mlp.down_proj.weight_scale_inv'
root@rocm-jupyter-gpu-mi300x1-192gb-devcloud-atl1:~/llama.cpp#
I was able to extend the storage. Here is the new issue in converting from HF:
ValueError: Can not map tensor 'model.layers.0.mlp.down_proj.weight_scale_inv'
They have uploaded FP8 tensors, you have to convert them to BF16 first, using DeepSeek's convert.pyfp8_cast_bf16.py.
I was able to extend the storage. Here is the new issue in converting from HF:
ValueError: Can not map tensor 'model.layers.0.mlp.down_proj.weight_scale_inv'
They have uploaded FP8 tensors, you have to convert them to BF16 first, using DeepSeek's convert.py.
This won't work on Ampere or lower cards sadly. I hacked out the Triton stuff and created this version:
The tokenizer needs a new regex it seems, but I'm assuming it's similar to Hunyuan's conversion process, but more similar to GPT4o's tokenizer. Also they seem to tokenize Chinese characters more differently
Reacted by R0CKSTAR, Anton Sokolchenko, Uday Pratap Singh Atwal and bhugueney
After to converting to bf16 (and trusting remote) the tokenizer seems to be a problem.
convert_hf_to_gguf.py fails on vocab_size = self.hparams.get("vocab_size", len(tokenizer.vocab)) due to vocab not existing on tokenizer. If you bypass that you get
WARNING:hf-to-gguf:** WARNING: The BPE pre-tokenizer was not recognized!
WARNING:hf-to-gguf:** There are 2 possible reasons for this:
WARNING:hf-to-gguf:** - the model has not been added to convert_hf_to_gguf_update.py yet
WARNING:hf-to-gguf:** - the pre-tokenization config has changed upstream
WARNING:hf-to-gguf:** Check your model files and convert_hf_to_gguf_update.py and update them accordingly.
WARNING:hf-to-gguf:** ref: https://github.com/ggml-org/llama.cpp/pull/6920
WARNING:hf-to-gguf:**
WARNING:hf-to-gguf:** chkhsh: 81212dc7cdb7e0c1074ca62c5aeab0d43c9f52b8a737be7b12a777c953027890
Hacking kimi-k2 into ./convert_hf_to_gguf_update.py and running yields:
Traceback (most recent call last):
File "/home/aicli/llama/llama.cpp/./convert_hf_to_gguf_update.py", line 268, in <module>
tokenizer = AutoTokenizer.from_pretrained(f"models/tokenizers/{name}", trust_remote_code=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/aicli/llama/llama.cpp/venv/lib/python3.12/site-packages/transformers/models/auto/tokenization_auto.py", line 949, in from_pretrained
raise ValueError(
ValueError: Unrecognized configuration class <class 'transformers_modules.kimi-k2.configuration_deepseek.DeepseekV3Config'> to build an AutoTokenizer.
Model type should be one of AlbertConfig, AlignConfig, ...
I would assume that this is not finding the configuration_deepseek.py file or that isn't properly registering its contents but I don't know any of these libraries so I tapped out at that point.
Here my patches to get convert_hf_to_gguf .py to work for Kimi-K2-Instruct. I have absolutely no idea if this is correct but it converts. Please correct me if I did anything wrong.
I downloaded the original model and used the following command using the script from original DeepSeek V3 to conveart it to BF16: CUDA_VISIBLE_DEVICES=1 venv/bin/python fp8_cast_bf16.py --input-fp8-hf-path /cpool/Kimi-K2-Instruct --output-bf16-hf-path /bpool/Kimi-K2-Instruct_BF16
diff --git a/convert_hf_to_gguf.py b/convert_hf_to_gguf.py
index 8afb425b..80046c2a 100755
--- a/convert_hf_to_gguf.py+++ b/convert_hf_to_gguf.py@@ -608,12 +608,12 @@ class TextModel(ModelBase):
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(self.dir_model)
- vocab_size = self.hparams.get("vocab_size", len(tokenizer.vocab))- assert max(tokenizer.vocab.values()) < vocab_size+ vocab_size = self.hparams.get("vocab_size", tokenizer.vocab_size)+ #assert max(tokenizer.vocab.values()) < vocab_size
tokpre = self.get_vocab_base_pre(tokenizer)
- reverse_vocab = {id_: encoded_tok for encoded_tok, id_ in tokenizer.vocab.items()}+ reverse_vocab = {id_: encoded_tok for encoded_tok, id_ in tokenizer.get_vocab().items()}
added_vocab = tokenizer.get_added_vocab()
added_tokens_decoder = tokenizer.added_tokens_decoder
@@ -780,6 +780,9 @@ class TextModel(ModelBase):
if chkhsh == "877081d19cf6996e2c4ff0e1236341e9b7bde288f5311a56a937f0afbbb3aeb5":
# ref: https://huggingface.co/deepseek-ai/DeepSeek-V3
res = "deepseek-v3"
+ if chkhsh == "81212dc7cdb7e0c1074ca62c5aeab0d43c9f52b8a737be7b12a777c953027890":+ # ref: https://huggingface.co/moonshotai/Kimi-K2-Instruct+ res = "deepseek-v3"
if chkhsh == "b3f499bb4255f8ca19fccd664443283318f2fd2414d5e0b040fbdd0cc195d6c5":
# ref: https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
res = "deepseek-r1-qwen"
Simply converting the model as described above and increasing LLAMA_MAX_EXPERTS is not enough to get the model to load as it fails with lama_model_load: error loading model: error loading model vocabulary: cannot find tokenizer merges in model file.
It looks like I was able to convert unsloth/Kimi-K2-Instruct-BF16 to F16 GGUF. I'm now quantizing the F16 model to Q4_K_M. (I also tried loading the F16 model directly, but it's way too large.)
I think the jinja error has something to do with bf16 repo i converted from including a .jinja file, I'm guessing converting to gguf without it leads to no error
I think you will need to implement a new chat template, such as LLM_CHAT_TEMPLATE_KIMI_K2. I just added one locally, and it resolved the crash — but it's not fully working yet.
I think the jinja error has something to do with bf16 repo i converted from including a .jinja file, I'm guessing converting to gguf without it leads to no error
I converted my own without a .jinja and need --jinja too but I had also been hacking on it for a bit so IDK if I broke something that caused a similar symptom :).
Alright I think i fixed it with the latest commit, @artraze can you test again and see if you still see the double spaces?
Definitely healthier! I only evaluated with English but I'm not seeing the huge changes in size on retokenizing output and the doubling of spaces has stopped. Overall it seems competent enough for a Q2 without imatrix. (I might try to run an imatrix tonight, though it'll probably take a while...)
#14654 With a dual EPYC 9355 with 736gb ram in 23 channels (one of my rdimms is on RMA) and a blackwell 6000 pro with Kimi-K2-Instruct:1t:Q4_K_XL with NPS4:
The multi turn tool calling chat template does need an update. One has to enable the new one via --chat-template-file PATH_TO_KIMI_K2_CHAT_TEMPLATE.jinja - I will have to re-update the quants to bake the new chat template over the next few days
@danielhanchen did you just update all the files in the UD_Q4_K_XL model yesterday? What was that about? I tried to grab the first file again to grab the new chat template and now it's giving me llama_model_load: error loading model: invalid model: tensor 'blk.5.ffn_gate_exps.weight' is duplicated.
I did notice this when I was making the draft models and thought it looked like <|im_end|> makes more sense (with [EOS] being the pre-training document separator), but can't find any confirmation on this and it looks like they even went out of their way to make [EOS]tokenise properly in their only change since the repo was created!?
@jukofyork Sorry on the delay - so the issue is [EOS] is not in the chat template, whilst <|im_end|> is, so the EOS for the instruct model should be <|im_end|>
Activity
jukofyork commentedon Jul 12, 2025
This should be an easy fix:
llama.cpp/src/llama-hparams.h
Line 9 in f5e96b3
https://huggingface.co/moonshotai/Kimi-K2-Instruct/blob/main/tokenization_kimi.py
As far as I can see, everything else seems to be exactly the same as
deepseek-v3architecture (it has only 1 dense layer at the start instead of 3 though).I'm downloading it now - very slowly lol.
jukofyork commentedon Jul 12, 2025
It might also be worth trying the hack suggested here: #12610
yeahdongcn commentedon Jul 12, 2025
Yeah, I got the first one working.
However, I’m using a cloud instance with limited storage, so I’m unable to perform the conversion on it.I was able to extend the storage. Here is the new issue in converting from HF:
danielhanchen commentedon Jul 12, 2025
The tiktoken tokenizer does look like an issue - similar to Hunyuan's changes - I think it's mostly similar to GPT4o's tokenizer maybe?
CISC commentedon Jul 12, 2025
They have uploaded FP8 tensors, you have to convert them to BF16 first, using DeepSeek's
convert.pyfp8_cast_bf16.py.jukofyork commentedon Jul 12, 2025
This won't work on Ampere or lower cards sadly. I hacked out the Triton stuff and created this version:
#11446 (comment)
that should work just using CPU if needed.
jukofyork commentedon Jul 12, 2025
Actually, I'm not sure what
convert.pyis for. It's this code that is used to convert formfp8tobf16:https://huggingface.co/deepseek-ai/DeepSeek-V3/blob/main/inference/fp8_cast_bf16.py
that sadly relies on this Triton kerel:
https://huggingface.co/deepseek-ai/DeepSeek-V3/blob/main/inference/kernel.py
which won't work unless your GPU has native
fp8(ie: Ada or newer).CISC commentedon Jul 12, 2025
Ah, sorry, I just remembered it was in that repo and picked the first reasonably looking file. :)
danielhanchen commentedon Jul 12, 2025
By the way if it helps, I made BF16 via the DeepSeek script conversion - https://huggingface.co/unsloth/Kimi-K2-Instruct-BF16
The tokenizer needs a new regex it seems, but I'm assuming it's similar to Hunyuan's conversion process, but more similar to GPT4o's tokenizer. Also they seem to tokenize Chinese characters more differently
artraze commentedon Jul 12, 2025
After to converting to bf16 (and trusting remote) the tokenizer seems to be a problem.
convert_hf_to_gguf.pyfails onvocab_size = self.hparams.get("vocab_size", len(tokenizer.vocab))due tovocabnot existing ontokenizer. If you bypass that you getHacking
kimi-k2into./convert_hf_to_gguf_update.pyand running yields:I would assume that this is not finding the
configuration_deepseek.pyfile or that isn't properly registering its contents but I don't know any of these libraries so I tapped out at that point.nicoboss commentedon Jul 12, 2025
Here my patches to get convert_hf_to_gguf .py to work for Kimi-K2-Instruct. I have absolutely no idea if this is correct but it converts. Please correct me if I did anything wrong.
I downloaded the original model and used the following command using the script from original DeepSeek V3 to conveart it to BF16:
CUDA_VISIBLE_DEVICES=1 venv/bin/python fp8_cast_bf16.py --input-fp8-hf-path /cpool/Kimi-K2-Instruct --output-bf16-hf-path /bpool/Kimi-K2-Instruct_BF16nicoboss commentedon Jul 13, 2025
Simply converting the model as described above and increasing
LLAMA_MAX_EXPERTSis not enough to get the model to load as it fails withlama_model_load: error loading model: error loading model vocabulary: cannot find tokenizer merges in model file.nyuuzyou commentedon Jul 13, 2025
I used triton-cpu for the conversion without GPU and it worked well with minimal modifications to the original script.
yeahdongcn commentedon Jul 13, 2025
It looks like I was able to convert unsloth/Kimi-K2-Instruct-BF16 to
F16 GGUF. I'm now quantizing theF16model toQ4_K_M. (I also tried loading theF16model directly, but it's way too large.)19 remaining items
yeahdongcn commentedon Jul 14, 2025
I think you will need to implement a new chat template, such as
LLM_CHAT_TEMPLATE_KIMI_K2. I just added one locally, and it resolved the crash — but it's not fully working yet.danielhanchen commentedon Jul 14, 2025
@gabriellarson Nice change - testing now! Note I use
--verbose --verbose-prompt --jinjato test tokenization - but yesllama-tokenizealso works!artraze commentedon Jul 14, 2025
I converted my own without a
.jinjaand need--jinjatoo but I had also been hacking on it for a bit so IDK if I broke something that caused a similar symptom :).Definitely healthier! I only evaluated with English but I'm not seeing the huge changes in size on retokenizing output and the doubling of spaces has stopped. Overall it seems competent enough for a Q2 without imatrix. (I might try to run an imatrix tonight, though it'll probably take a while...)
gabriellarson commentedon Jul 14, 2025
@fernandaspets the Q2_K gguf should work, just make sure to use --jinja
danielhanchen commentedon Jul 14, 2025
I made some 245GB, 281GB (IQ1_S) dynamic quants + Q2_K_XL, Q4_K_XL quants at https://huggingface.co/unsloth/Kimi-K2-Instruct-GGUF - it should work fine with @gabriellarson 's PR #14654 or using my fork https://github.com/unslothai/llama.cpp - guide to run them here: https://docs.unsloth.ai/basics/kimi-k2-how-to-run-locally#run-kimi-k2-tutorials
createthis commentedon Jul 15, 2025
#14654 With a dual EPYC 9355 with 736gb ram in 23 channels (one of my rdimms is on RMA) and a blackwell 6000 pro with Kimi-K2-Instruct:1t:Q4_K_XL with NPS4:
./build/bin/llama-batched-bench \ --model /data/Kimi-K2-Instruct-GGUF/UD-Q4_K_XL/Kimi-K2-Instruct-UD-Q4_K_XL-00001-of-00013.gguf \ --numa numactl \ --threads 32 \ --ctx-size 131072 \ --n-gpu-layers 62 \ -ot "blk\.(3|4|5|6|7|8)\.ffn_.*=CUDA0" \ -ot exps=CPU \ -ub 4096 -b 4096 \ --seed 3407 \ --temp 0.6 \ --min-p 0.01 \ --flash-attn \ -npp 4096 \ -ntg 1024 \ -npl 1 main: n_kv_max = 131072, n_batch = 4096, n_ubatch = 4096, flash_attn = 1, is_pp_shared = 0, n_gpu_layers = 62, n_threads = 32, n_threads_batch = 32 | PP | TG | B | N_KV | T_PP s | S_PP t/s | T_TG s | S_TG t/s | T s | S t/s | |-------|--------|------|--------|----------|----------|----------|----------|----------|----------| | 4096 | 1024 | 1 | 5120 | 38.594 | 106.13 | 46.432 | 22.05 | 85.026 | 60.22 |A few days later with the RMA'd rdimm installed again:
Normal run:
./build/bin/llama-server \ --model /data/Kimi-K2-Instruct-GGUF/UD-Q4_K_XL/Kimi-K2-Instruct-UD-Q4_K_XL-00001-of-00013.gguf \ --alias Kimi-K2-Instruct:1t:Q4_K_XL \ --no-webui \ --numa numactl \ --threads 32 \ --ctx-size 131072 \ --n-gpu-layers 62 \ -ot "blk\.(3|4|5|6|7|8)\.ffn_.*=CUDA0" \ -ot exps=CPU \ -ub 4096 -b 4096 \ --seed 3407 \ --temp 0.6 \ --min-p 0.01 \ --log-colors \ --flash-attn \ --host 0.0.0.0 \ --jinja \ --port 1143489.6gb nvram
491.9g system ram
Some time passes...
danielhanchen commentedon Jul 15, 2025
@createthis Nice 24 tokens / s is pretty sick for Q4_K_XL!!
yeahdongcn commentedon Jul 15, 2025
Tested on an AMD Developer Cloud instance with 8 × MI300X GPUs, using #14654 and the Q4_K_M variant of https://huggingface.co/unsloth/Kimi-K2-Instruct-GGUF:
nyuuzyou commentedon Jul 15, 2025
Could this be the cause of tokenization issues??
https://x.com/Kimi_Moonshot/status/1945050874067476962
danielhanchen commentedon Jul 15, 2025
@nyuuzyou Re their changes:
tokenizer.encodewill now encode special tokens - this does NOT affect the PR at Model : Add support for Kimi-K2 #14654 see Feature Request: Support Kimi K2 #14642 (comment) which shows the tokenization of special tokens is done correctly. Only HF'stokenizer.encodewill be incorrect.--chat-template-file PATH_TO_KIMI_K2_CHAT_TEMPLATE.jinja- I will have to re-update the quants to bake the new chat template over the next few dayscreatethis commentedon Jul 19, 2025
@danielhanchen did you just update all the files in the UD_Q4_K_XL model yesterday? What was that about? I tried to grab the first file again to grab the new chat template and now it's giving me
llama_model_load: error loading model: invalid model: tensor 'blk.5.ffn_gate_exps.weight' is duplicated.jukofyork commentedon Jul 19, 2025
https://docs.unsloth.ai/basics/kimi-k2-how-to-run-locally
@danielhanchen sorry to ping you, but can I ask where you got this info from?
They still appear to use
[EOS]in all their files:https://huggingface.co/moonshotai/Kimi-K2-Instruct/blob/main/tokenization_kimi.py
https://huggingface.co/moonshotai/Kimi-K2-Instruct/blob/main/tokenizer_config.json
https://huggingface.co/moonshotai/Kimi-K2-Instruct/blob/main/config.json
apart from in
generation_config.json:{ "max_length": 131072, "eos_token_id": 163586 }I did notice this when I was making the draft models and thought it looked like
<|im_end|>makes more sense (with[EOS]being the pre-training document separator), but can't find any confirmation on this and it looks like they even went out of their way to make[EOS]tokenise properly in their only change since the repo was created!?yeahdongcn commentedon Jul 22, 2025
Since #14654 has been merged into
master, I’m closing this issue as completed.danielhanchen commentedon Jul 22, 2025
@jukofyork Sorry on the delay - so the issue is [EOS] is not in the chat template, whilst <|im_end|> is, so the EOS for the instruct model should be <|im_end|>