> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/mo-xing/kimi-k2.7-code.md).

# Kimi K2.7 Code - 如何本地运行

Kimi K2.7 Code 是 Moonshot AI 的智能体编码模型，建立在 [K2.6](/docs/zh/mo-xing/kimi-k2.6.md) 之上，以使用约 30% 更少的思考 token 来提升任务完成度。这款 1T 参数（32B 激活）的 MoE 模型仅支持思考、视觉和 256K 上下文。它在视觉、编码、智能体、长上下文和聊天任务上提供 SOTA 开放性能。完整精度需要 605GB 磁盘空间；Unsloth [动态](/docs/zh/ji-chu-zhi-shi/unsloth-dynamic-2.0-ggufs.md) 2 位需要 **325GB（-48%）**。运行 [**Kimi-K2.7-Code-GGUF**](https://huggingface.co/unsloth/Kimi-K2.7-Code-GGUF) 可通过 Unsloth Studio 或 llama.cpp 运行。

[**Unsloth 动态**](/docs/zh/ji-chu-zhi-shi/unsloth-dynamic-2.0-ggufs.md) **量化** 会将重要层提升到 8 位，而 1 位需要 **310GB+ VRAM/RAM** 配&#x7F6E;**.** 对于 **无损** Kimi K2.6，使用 Q8（`UD-Q8_K_XL`），它只比 **大 10GB** 比 Q4（`UD-Q4_K_XL`）。你可以通过 Mac Studio 或 [DGX Station](/docs/zh/bo-ke/dgx-station.md).

**表：硬件要求** （单位 = 总内存：RAM + VRAM，或统一内存）

| 动态 1 位 | 动态 2 位    | 动态 Q3      | Q8（无损） |
| ------ | --------- | ---------- | ------ |
| 310 GB | 325-350GB | 385-470 GB | 605 GB |

### 📊 量化分析

类似于 [Kimi-K2.6](/docs/zh/mo-xing/kimi-k2.6.md), `UD-Q8_K_XL` 是无损的，因为 Kimi 对 MoE 权重使用 int4，对其他一切使用 BF16，并且 `Q8_K_XL` 遵循这一点。因此，我们使用相同的 Dynamic 方法来转换 Kimi-K2.6。 `UD-Q4_K_XL` 与之类似，只是其余张量是 `Q8_0`，因此它接近完整精度，并且需要 600GB RAM/VRAM。 `UD-Q8_K_XL` 是“真正无损”的。

| 测量   | UD-Q2\_K\_XL | UD-Q4\_K\_XL | UD-Q8\_K\_XL（无损） |
| ---- | ------------ | ------------ | ---------------- |
| 磁盘空间 | 339 GB       | 584 GB       | 595 GB           |
| 困惑度  | \~2.4131     | \~1.8420     | \~1.8419         |

我们遵循了 [jukofyork](https://github.com/jukofyork)的发现，即 `const float d = max / -7;` 而不是默认的 `const float d = max / -8;` 在量化过程中仅对 MoE 层进行。这个针对 INT4 原生 MoE 的双射补丁使 `Q4_0` 量化类型把绝对误差从 1.8% 降低到接近 0%（epsilon）。下面例如是 Kimi-K2.7-Code 的直方图，你可以看到 -8 完全未被使用：

<figure><img src="/files/b33a99d2b48d14e6cf3ad8ce1857b6d2aa5757d1" alt=""><figcaption></figcaption></figure>

注意我们还必须将其他层保持为 BF16，而不能使用智能的“Q4\_0”。下面我们展示两者相对于 BF16 基线的误差图。 `UD-Q8-K_XL` 在将 Q4\_0 转换为 BF16 时，仅有机器 epsilon 级别的差异，因此它确实是“无损”的。因此，由于使用了 Q8\_0，Q4\_K\_XL 确实存在一些量化误差，而 Q8\_K\_XL 除了 BF16 舍入外几乎无损。

<figure><img src="/files/014507a7ae665f5503fa36754d7a2ef083118842" alt=""><figcaption></figcaption></figure>

对于 Q4\_K\_XL，我们也绘制了每个张量相对于 BF16 的 Q8\_0 误差。总体而言，Q8\_K\_XL（接近无损）与 Q4\_K\_XL 之间存在一些误差，但不多。

<figure><img src="/files/247b941bad087c2d2c19b506eb4689169a20a3e3" alt=""><figcaption></figcaption></figure>

### :gear: 使用指南

Kimi K2.7 Code 是 **仅思考**，并且 **`preserve_thinking` 始终启用**。不支持即时模式。

| 默认（思考模式）          |
| ----------------- |
| temperature = 1.0 |
| top\_p = 0.95     |

* 建议上下文长度 = `98,304` （最高可达 `262,144`)

如果模型能装下，在使用 B200 时你将获得 >100 tokens/s。我们推荐 `UD-Q2_K_XL` （345GB）作为一个尺寸/质量的良好平衡。最佳经验法则：RAM+VRAM ≈ 量化大小；否则它仍然可以运行，只是由于卸载会更慢。

#### Kimi K2.7-Code 的聊天模板

运行 `tokenizer.apply_chat_template([{"role": "user", "content": "What is 1+1?"},])` 会得到：

{% code overflow="wrap" %}

```
<|im_user|>user<|im_middle|>What is 1+1?<|im_end|><|im_assistant|>assistant<|im_middle|><think>
```

{% endcode %}

如果我们也像在 [Tool Calling Guide](/docs/zh/ji-chu-zhi-shi/tool-calling-guide-for-local-llms.md)中所引用的那样输入工具，那么我们会看到如下内容：

{% code overflow="wrap" expandable="true" %}

```
<|im_system|>tool_declare<|im_middle|># 工具

## 函数
namespace functions {
// 加两个数。
type add_number = (_: {
  // 第一个数字。
  a: string,
  // 第二个数字。
  b: string
}) => any;
// 乘两个数。
type multiply_number = (_: {
  // 第一个数字。
  a: string,
  // 第二个数字。
  b: string
}) => any;
// 减两个数。
type subtract_number = (_: {
  // 第一个数字。
  a: string,
  // 第二个数字。
  b: string
}) => any;
// 写一个随机故事。
type write_a_story = (_: {}) => any;
// 执行终端中的操作。
type terminal = (_: {
  // 你希望启动的命令，例如 `ls`、`rm` 等。
  command: string
}) => any;
// 调用一个 Python 解释器并执行一些 Python 代码。
type python = (_: {
  // 要运行的 Python 代码
  code: string
}) => any;
}
<|im_end|><|im_user|>user<|im_middle|>What is 1+1?<|im_end|><|im_assistant|>assistant<|im_middle|><think>
```

{% endcode %}

## Kimi K2.7 Code 运行指南

### 🦥 在 Unsloth Studio 中运行 Kimi-K2.7-Code

Kimi K2.7 Code 可以运行在 [Unsloth Studio](/docs/zh/xin-de/studio.md)，一个用于本地 AI 的开源 Web UI。 **Unsloth Studio 会自动卸载到 RAM，并检测多 GPU 配置**。借助 Unsloth Studio，您可以在本地运行模型 **MacOS、Windows**、Linux，以及：

{% columns %}
{% column %}

* 搜索、下载， [运行 GGUF](/docs/zh/xin-de/studio.md#run-models-locally) 以及 safetensor 模型
* [**自修复** 工具调用](/docs/zh/xin-de/studio.md#execute-code--heal-tool-calling) + **网页搜索**
* [**代码执行**](/docs/zh/xin-de/studio.md#run-models-locally) （Python、Bash）
* [自动推理](/docs/zh/xin-de/studio.md#model-arena) 参数调优（temp、top-p 等）
* 通过 llama.cpp 实现快速 CPU + GPU 推理
* [训练 LLM](/docs/zh/xin-de/studio.md#no-code-training) 速度提升 2 倍，VRAM 减少 70%
  {% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="/files/5af4df407c8134f1ff75a4d7535569361c049e51" alt=""><figcaption></figcaption></figure></div>
{% endcolumn %}
{% endcolumns %}

{% stepper %}
{% step %}
**安装并启动 Unsloth**

要安装，请在终端中运行：

MacOS、Linux、WSL：

```bash
curl -fsSL https://unsloth.ai/install.sh | sh
```

Windows PowerShell：

```bash
irm https://unsloth.ai/install.ps1 | iex
```

**启动 Unsloth**

MacOS、Linux、WSL 和 Windows：

```bash
unsloth studio -H 0.0.0.0 -p 8888
```

然后打开 `http://127.0.0.1:8888` （或你的具体 URL）在浏览器中。
{% endstep %}

{% step %}
**搜索并下载 Kimi K2.7-Code**

Unsloth Studio 会自动卸载到 RAM，并检测多 GPU 配置。首次启动时，你需要创建一个密码来保护你的账户，并在以后重新登录。

然后前往 [Unsloth Chat](/docs/zh/xin-de/studio/chat.md) 选项卡并搜索 **Kimi-K2.7 Code** 在搜索栏中并下载你想要的模型和量化。请确保你有足够的计算资源来运行该模型。

<div data-with-frame="true"><figure><img src="/files/2f194f813fa4c536ff0b36b94556ba05416f0b54" alt="" width="563"><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**运行 Kimi-K2.7-Code**

使用 Unsloth Studio 时，推理参数应会自动设置，不过你仍然可以手动更改。你也可以编辑上下文长度、聊天模板和其他设置。

更多信息，你可以查看我们的 [Unsloth Studio 推理指南](/docs/zh/xin-de/studio/chat.md).

<div data-with-frame="true"><figure><img src="/files/23fb40df7f876a1d75f48943c38802ab9e31027f" alt="" width="563"><figcaption><p>使用工具调用运行的 Qwen3.6 示例</p></figcaption></figure></div>
{% endstep %}
{% endstepper %}

### 🦙 在 llama.cpp 中运行 Kimi K2.7 Code

在本指南中，我们将运行 `UD-Q2_K_XL` 该量化版本，这将至少需要 345GB RAM。你可以随意更改量化类型。GGUF： [**Kimi-K2.7-Code-GGUF**](https://huggingface.co/unsloth/Kimi-K2.7-Code-GGUF)

对于这些教程，我们将使用 [llama.cpp](llama.cpphttps://github.com/ggml-org/llama.cpp) 进行快速本地推理，尤其是如果你有 CPU。

{% stepper %}
{% step %}
获取最新的 `llama.cpp` **在** [**GitHub 这里**](https://github.com/ggml-org/llama.cpp)。你也可以按照下面的构建说明进行操作。将 `-DGGML_CUDA=ON` 更改为 `-DGGML_CUDA=OFF` 如果你没有 GPU，或者只想进行 CPU 推理。 **对于 Apple Mac / Metal 设备**，设置 `-DGGML_CUDA=OFF` 然后像往常一样继续——Metal 支持默认开启。

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \\
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-mtmd-cli llama-server llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

{% endstep %}

{% step %}
**我们先获取一张图片！** 你也可以上传图片。我们将使用 <https://raw.githubusercontent.com/unslothai/unsloth/refs/heads/main/images/unsloth%20made%20with%20love.png>，这只是我们的小徽标，用来展示如何借助 Unsloth 制作微调：

{% code overflow="wrap" %}

```bash
wget https://raw.githubusercontent.com/unslothai/unsloth/refs/heads/main/images/unsloth%20made%20with%20love.png -O unsloth.png
```

{% endcode %}

<figure><img src="/files/99e6294684309cf45754d5010bc1bff5c7da4548" alt="" width="188"><figcaption></figcaption></figure>

我们再获取第二张图片： <https://files.worldwildlife.org/wwfcmsprod/images/Sloth_Sitting_iStock_3_12_2014/story_full_width/8l7pbjmj29_iStock_000011145477Large_mini__1_.jpg>

{% code overflow="wrap" %}

```bash
wget https://files.worldwildlife.org/wwfcmsprod/images/Sloth_Sitting_iStock_3_12_2014/story_full_width/8l7pbjmj29_iStock_000011145477Large_mini__1_.jpg -O picture.png
```

{% endcode %}

<figure><img src="/files/dfadcc899fed5f74e2966aa94681fc7f2b43caa2" alt="" width="188"><figcaption></figcaption></figure>
{% endstep %}

{% step %}
你现在可以使用 `llama.cpp` 直接加载和下载模型，就像 `ollama run`。首先，选择你想要的量化类型，例如 `Q2_K_XL`。同时使用 `export LLAMA_CACHE="folder"` 以强制 `llama.cpp` 保存到特定位置。请注意，此下载过程可能非常慢，因此最好使用下一节中的手动下载流程。

```bash
export LLAMA_CACHE="unsloth/Kimi-K2.7-Code-GGUF"
./llama.cpp/llama-cli \\
    -hf unsloth/Kimi-K2.7-Code-GGUF:UD-Q2_K_XL \\
    --temp 1.0 \\
    --top-p 0.95
```

{% endstep %}

{% step %}
如果你想手动下载模型，我们可以在安装 `pip install huggingface_hub`之后通过下面的代码下载模型。如果下载卡住，请参见： [Hugging Face Hub、XET 调试](/docs/zh/ji-chu-zhi-shi/troubleshooting-and-faqs/hugging-face-hub-xet-debugging.md)

```bash
hf download unsloth/Kimi-K2.7-Code-GGUF \\
    --local-dir unsloth/Kimi-K2.7-Code-GGUF \\
    --include "*mmproj-F16*" \
    --include "*UD-Q2_K_XL*" # 使用 "*UD-Q8_K_XL*" 以获得完整精度
```

{% endstep %}

{% step %}
然后以对话模式运行模型：

{% code overflow="wrap" %}

```bash
./llama.cpp/llama-cli \\
    --model unsloth/Kimi-K2.7-Code-GGUF/UD-Q2_K_XL/Kimi-K2.7-Code-UD-Q2_K_XL-00001-of-00008.gguf \\
    --mmproj unsloth/Kimi-K2.7-Code-GGUF/mmproj-F16.gguf \\
    --temp 1.0 \\
    --top-p 0.95
```

{% endcode %}

然后你会看到如下内容：\
![](/files/4c2b0aee7e43965b1b1cbb1f94840a75adaf8c27)
{% endstep %}

{% step %}
然后使用 `/image` 来加载两张图片并提问“这是什么图片”：

<figure><img src="/files/6da149caf2f95cf698ed9aa49812f066bd570822" alt=""><figcaption></figcaption></figure>

你会得到如下结果：

<figure><img src="/files/5f40d04c24773b8ee3c5ccd746c359203b2ca6fa" alt=""><figcaption></figcaption></figure>

在第二张树懒图片中：

<figure><img src="/files/23efe86f68c74f15b5ac7a8159a7df30d5508d12" alt=""><figcaption></figcaption></figure>

这将得到：

<figure><img src="/files/704c99bf86e9b97bb343ba242ef04a91163e9baf" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

### 📊 基准测试

你可以在下方查看表格式基准测试：

<div data-with-frame="true"><figure><img src="/files/25c421585fb91f674304d23d3aec53b424c8766b" alt="" width="563"><figcaption></figcaption></figure></div>

|         基准        | Kimi K2.7 Code | Kimi K2.6 | GPT-5.5 | Claude Opus 4.8 |
| :---------------: | :------------: | :-------: | :-----: | :-------------: |
|       **编程**      |                |           |         |                 |
|  Kimi Code 基准 v2  |      62.0      |    50.9   |   69.0  |       67.4      |
|        程序基准       |      53.6      |    48.3   |   69.1  |       63.8      |
|     MLS 基准精简版     |      35.1      |    26.7   |   35.5  |       42.8      |
|      **智能体**      |                |           |         |                 |
| Kimi Claw 24/7 基准 |      46.9      |    42.9   |   52.8  |       50.4      |
|     MCP Atlas     |      76.0      |    69.4   |   79.4  |       81.3      |
|    MCP Mark 已验证   |      81.1      |    72.8   |   92.9  |       76.4      |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://unsloth.ai/docs/zh/mo-xing/kimi-k2.7-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
