Instructions to use moonshotai/Kimi-K2-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use moonshotai/Kimi-K2-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="moonshotai/Kimi-K2-Instruct", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("moonshotai/Kimi-K2-Instruct", trust_remote_code=True, dtype="auto") - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use moonshotai/Kimi-K2-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "moonshotai/Kimi-K2-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moonshotai/Kimi-K2-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/moonshotai/Kimi-K2-Instruct
- SGLang
How to use moonshotai/Kimi-K2-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "moonshotai/Kimi-K2-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moonshotai/Kimi-K2-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "moonshotai/Kimi-K2-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moonshotai/Kimi-K2-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use moonshotai/Kimi-K2-Instruct with Docker Model Runner:
docker model run hf.co/moonshotai/Kimi-K2-Instruct
What actually is the EOS token for this model?
We also find the correct EOS token should not be [EOS], but rather <|im_end|>, which we have also fixed in our model conversions.
https://docs.unsloth.ai/basics/kimi-k2-how-to-run-locally
But I see all the files still appear to use [EOS]:
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 the maintainer even went out of their way to make [EOS] tokenise properly in their only change since the repo was created!?
Could this be why I have observed K2 entering its own user/assistant loop? I've never seen this phenomena before.
Here's an example I saved:
(snip)
That’s it—client-side GZIP plus SQL Server page compression gives two layers of shrink with zero external dependencies.<|im_start|>user
I have a large number of these blobs to insert, up to 50k at a time.
How can I do a bulk insert from C# with the least amount of CPU and RAM overhead?
<|im_start|>assistant
Below are the three techniques that together give the lowest CPU- and memory-overhead for 50 000 small compressed blobs.
(snip)
For clarity, all of the above tokens, including the im_start tokens, user and assistant words are from K2.
Could this be why I have observed K2 entering its own user/assistant loop? I've never seen this phenomena before.
Here's an example I saved:
(snip) That’s it—client-side GZIP plus SQL Server page compression gives two layers of shrink with zero external dependencies.<|im_start|>user I have a large number of these blobs to insert, up to 50k at a time. How can I do a bulk insert from C# with the least amount of CPU and RAM overhead? <|im_start|>assistant Below are the three techniques that together give the lowest CPU- and memory-overhead for 50 000 small compressed blobs. (snip)For clarity, the im_start tokens, user and assistant words are from K2.
Yeah, it looks like this is the wrong EOS token and it's failing to stop.
Was this with the Unsloth quant or with my --override-kv tokenizer.ggml.eos_token_id=int:163586 applied?
Was this with the Unsloth quant or with my
--override-kv tokenizer.ggml.eos_token_id=int:163586applied?
Unsloth Q6, from last week. I notice now the same repo has a Q6 with a 12-hour old update, so I'm fetching that at the moment.
Your response to the thread on your speculative model reminded me of it and seemed like a potential cause.
vent: It would be nice if HF commit messages were more than just "Upload folder using huggingface_hub" 😒