把 Kimi K2 装进笔记本:一份写给普通人的本地运行全记录

“我想试试那个 1 万亿参数的开源模型,但不知道从哪里开始。”
如果你也有同样的疑问,这篇文章就是给你的。
Unsloth AI 发布了 1.8bit 量化的 Kimi K2 模型
模型大小由原来的 1.1T 变成了 245G,部署成本大幅下降


1. Kimi K2 到底是什么?

一句话:它是 Moonshot AI 放出的开源大模型,官方宣称在知识、推理、代码、代理任务上都刷新了 SOTA。
完整版有 1 T(1 000 000 000 000)参数,原始体积 1.09 TB;经过 Unsloth 的 动态 1.8-bit 量化 之后,瘦身到 245 GB,体积砍了 80 %,仍能保住 5-shot MMLU 的精度。


2. 运行前,我需要准备什么?

2.1 硬件底线

项目 最低要求 建议配置 备注
磁盘剩余空间 250 GB 400 GB 解压后还会膨胀
RAM + VRAM 总和 ≥ 250 GB ≥ 381 GB 总和够就行,不要求全放显卡里
显卡 1×24 GB(如 RTX 4090) 8×H200(全精度 1.09 TB 版) 量化版 24 GB 也能跑
系统 Linux / WSL2 / macOS Linux Windows 用 WSL2 更省心

如果内存或显存不够怎么办?
llama.cpp 会自动把多余权重 mmap 到磁盘,只是速度会从 5 tokens/s 掉到 < 1 token/s,依旧能跑。


3. 模型下载:三个办法任选其一

3.1 直接用 Hugging Face Hub(最方便)

pip install huggingface_hub hf_transfer
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id="unsloth/Kimi-K2-Instruct-GGUF",
    local_dir="Kimi-K2-Instruct-GGUF",
    allow_patterns=["*UD-TQ1_0*"]   # 245 GB 1.8-bit
    # allow_patterns=["*UD-Q2_K_XL*"]  # 381 GB 2-bit,想再稳点选这个
)

3.2 浏览器手动下载

打开 https://huggingface.co/unsloth/Kimi-K2-Instruct-GGUF,挑一个 .gguf 分卷下完即可。

3.3 命令行 wget/aria2c

复制文件直链,配合多线程下载工具,适合服务器。


4. 安装 llama.cpp

官方主线还没完全合并,先用 PR 14654 或 Unsloth 的 fork。

4.1 一键编译脚本(Linux / WSL2)

sudo apt-get update
sudo apt-get install -y pciutils build-essential cmake curl libcurl4-openssl-dev git

git clone https://github.com/unslothai/llama.cpp
cmake -S llama.cpp -B llama.cpp/build \
  -DBUILD_SHARED_LIBS=OFF \
  -DGGML_CUDA=ON \
  -DLLAMA_CURL=ON
cmake --build llama.cpp/build --config Release -j \
  --clean-first --target llama-cli llama-quantize llama-gguf-split llama-mtmd-cli

cp llama.cpp/build/bin/llama-* llama.cpp/

没有 NVIDIA 显卡?把 -DGGML_CUDA=ON 改成 -DGGML_CUDA=OFF,纯 CPU 也能跑,只是更慢。


5. 启动模型:两种常见场景

5.1 我只有一张 24 GB 卡

export LLAMA_CACHE="Kimi-K2-Instruct-GGUF"
./llama.cpp/llama-cli \
  -hf unsloth/Kimi-K2-Instruct-GGUF:UD-TQ1_0 \
  --cache-type-k q4_0 \
  --threads -1 \
  --n-gpu-layers 99 \
  --temp 0.6 \
  --min_p 0.01 \
  --ctx-size 16384 \
  --seed 3407 \
  -ot ".ffn_.*_exps.=CPU"

解释

  • -ot ".ffn_.*_exps.=CPU":把所有 MoE 专家层扔到内存或磁盘,显卡只留主干,24 GB 就能塞下。
  • --temp 0.6 --min_p 0.01:官方推荐值,降低胡说八道的概率。
  • --ctx-size 16384:上下文 16 K,日常够用。

5.2 我有 256 GB 内存 + 48 GB 显存

-ot 去掉,或者改成只扔部分层,速度能到 5 tokens/s 以上。


6. 与模型对话:格式长什么样?

Kimi K2 的聊天模板和 GPT 系列略有不同,它用 <|im_middle|> 做角色分隔:

<|im_system|>system<|im_middle|>You are a helpful assistant<|im_end|>
<|im_user|>user<|im_middle|>What is 1+1?<|im_end|>
<|im_assistant|>assistant<|im_middle|>2<|im_end|>

一行写完也行,换行写也行,llama-cli 会自动处理。


7. FAQ:90 % 的人会遇到的问题

Q1 下载太慢/被限速?

把环境变量 HF_HUB_ENABLE_HF_TRANSFER 设为 0,回退到官方下载逻辑,虽然慢一点但稳定。

Q2 提示 “CUDA out of memory”?

--n-gpu-layers 从 99 往下调,或者扩大 -ot 里扔 CPU 的正则范围。

Q3 能直接在 Windows 裸机跑吗?

可以,但编译 llama.cpp 时要装 MSVC + CUDA Toolkit,步骤比 Linux 繁琐,建议用 WSL2。

Q4 量化版精度掉得严重吗?

官方用 5-shot MMLU 测过,UD-TQ1_0 掉点 < 2 %,日常对话几乎无感。


8. 动手实验:让 Kimi K2 写个 Flappy Bird

下面这串 prompt 是官方示例,复制即可复现:

Create a Flappy Bird game in Python. You must include these things:
1. You must use pygame.
2. The background color should be randomly chosen and is a light shade. Start with a light blue color.
3. Pressing SPACE multiple times will accelerate the bird.
4. The bird's shape should be randomly chosen as a square, circle or triangle. The color should be randomly chosen as a dark color.
5. Place on the bottom some land colored as dark brown or yellow chosen randomly.
6. Make a score shown on the top right side. Increment if you pass pipes and don't hit them.
7. Make randomly spaced pipes with enough space. Color them randomly as dark green or light brown or a dark gray shade.
8. When you lose, show the best score. Make the text inside the screen. Pressing q or Esc will quit the game. Restarting is pressing SPACE again.
The final game should be inside a markdown section in Python. Check your code for errors and fix them before the final markdown section.

运行命令:

./llama-cli \
  --model Kimi-K2-Instruct-GGUF/UD-TQ1_0/Kimi-K2-Instruct-UD-TQ1_0-00001-of-00005.gguf \
  --temp 0.6 --min_p 0.01 --ctx-size 16384 --seed 3407 \
  -no-cnv \
  --prompt "<|im_system|>system<|im_middle|>You are a helpful assistant<|im_end|><|im_user|>user<|im_middle|>${上面的 prompt}<|im_end|><|im_assistant|>assistant<|im_middle|>"

稍等片刻,终端会吐出完整可运行的 Python 源码,复制粘贴即可玩。


9. 高级玩法:动态量化怎么做到 1.8 bit?

Unsloth 把传统静态量化升级成 Dynamic 2.0,思路大致是:

  1. 先用校准集跑一遍,拿到每一层的分布。
  2. 给重要通道多分 bit,不重要通道少分 bit,平均下来 1.8 bit。
  3. 通过 KL 散度约束,让量化后分布与原始分布差距最小。

好处是:

  • 同样体积,精度比旧版静态量化高。
  • 支持 MoE 大模型,专家层可以单独量化。

10. Tokenizer 为什么要改?

Kimi K2 的词表和 GPT-4o 很像,但多了中文 Unicode 块(\p{Han})。
llama.cpp 的 PR 14654 把这部分差异补齐,否则中文会切成奇怪的子词。
同时 EOS 从 [EOS] 改成 <|im_end|>,防止对话提前结束。


11. 一句话总结

如果你有一块 24 GB 显卡 + 250 GB 剩余硬盘,就能在本地体验 1 T 参数的大模型;
如果再加 256 GB 内存,速度可以跑到 5 tokens/s 以上。
剩下的,只是复制命令、下载模型、喝一口咖啡,然后看着它帮你写 Flappy Bird 的代码。