# Grok 2

你现在可以运行 **Grok 2** （又名 Grok 2.5），这是 xAI 的 270B 参数模型。完整精度需要 **539GB**，而 Unsloth Dynamic 3-bit 版本将体积缩小到仅 **118GB** （减少 75%）。GGUF： [Grok-2-GGUF](https://huggingface.co/unsloth/grok-2-GGUF)

该 **3-bit Q3\_K\_XL** 模型可在单个 **128GB Mac** 或 **24GB VRAM + 128GB RAM**上运行，并实现 **5+ tokens/s** 推理。感谢 llama.cpp 团队和社区 [对 Grok 2 的支持](https://github.com/ggml-org/llama.cpp/pull/15539) ，使这一切成为可能。我们也很高兴能在过程中提供了一点帮助！

所有上传都使用 Unsloth [Dynamic 2.0](/docs/zh/ji-chu/unsloth-dynamic-2.0-ggufs.md) 在 SOTA 5-shot MMLU 和 KL 散度性能上表现出色，这意味着你可以在几乎不损失准确率的情况下运行量化的 Grok LLM。

<a href="#run-in-llama.cpp" class="button secondary">在 llama.cpp 中运行教程</a>

## :gear: 推荐设置

3-bit 动态量化占用 118GB（126GiB）磁盘空间——这在配备 128GB RAM 统一内存的 Mac 上，或在 1 张 24GB 显卡加 128GB RAM 的机器上都能很好运行。建议至少拥有 120GB RAM 来运行此 3-bit 量化。

{% hint style="warning" %}
你必须使用 `--jinja` 用于 Grok 2。如果你不使用，可能会得到错误结果 `--jinja`
{% endhint %}

8-bit 量化大小约为 300GB，可装入 1 张 80GB GPU（MoE 层卸载到 RAM）。如果你还额外有 200GB RAM，预计在这种配置下速度约为 5 tokens/s。要了解如何提高生成速度并适配更长上下文， [请阅读这里](#improving-generation-speed).

{% hint style="info" %}
虽然不是必须，但为了获得最佳性能，建议你的 VRAM + RAM 总和等于你下载的量化模型大小。如果不满足这一点，硬盘 / SSD 卸载也可以与 llama.cpp 一起工作，只是推理会更慢。
{% endhint %}

### 采样参数

* Grok 2 的最大上下文长度为 128K，因此请使用 `131,072` 上下文或更少。
* 使用 `--jinja` 适用于 llama.cpp 变体

运行该模型没有官方采样参数，因此对于大多数模型你可以使用标准默认值：

* 设置 <mark style="background-color:green;">**temperature = 1.0**</mark>
* <mark style="background-color:green;">**Min\_P = 0.01**</mark> （可选，但 0.01 效果很好，llama.cpp 默认值是 0.1）

## 运行 Grok 2 教程：

目前你只能在 llama.cpp 中运行 Grok 2。

### ✨ 在 llama.cpp 中运行

{% stepper %}
{% step %}
安装特定的 `llama.cpp` 用于 Grok 2 的 PR，位于 [GitHub 这里](https://github.com/ggml-org/llama.cpp/pull/15539)。你也可以按照下面的构建说明操作。将 `-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
cd llama.cpp && git fetch origin pull/15539/head:MASTER && git checkout MASTER && cd ..
cmake 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-quantize llama-cli llama-gguf-split llama-mtmd-cli llama-server
cp llama.cpp/build/bin/llama-* llama.cpp
```

{% endstep %}

{% step %}
如果你想使用 `llama.cpp` 要直接加载模型，你可以使用下面的方法：（:Q3\_K\_XL）是量化类型。你也可以通过 Hugging Face 下载（第 3 点）。这与以下方式类似 `ollama run` 。使用 `export LLAMA_CACHE="folder"` 来强制 `llama.cpp` 保存到特定位置。请记住，该模型的最大上下文长度只有 128K。

{% hint style="info" %}
请尝试 `-ot ".ffn_.*_exps.=CPU"` 将所有 MoE 层卸载到 CPU！这实际上允许你将所有非 MoE 层放到 1 张 GPU 上，从而提高生成速度。如果你有更多 GPU 容量，可以自定义正则表达式以适配更多层。

如果你的 GPU 内存再多一些，尝试 `-ot ".ffn_(up|down)_exps.=CPU"` 这会卸载上、下投影 MoE 层。

再试试 `-ot ".ffn_(up)_exps.=CPU"` 如果你的 GPU 内存更多一些。这样只会卸载上投影 MoE 层。

最后通过以下方式卸载所有层： `-ot ".ffn_.*_exps.=CPU"` 这使用的 VRAM 最少。

你也可以自定义正则表达式，例如 `-ot "\.(6|7|8|9|[0-9][0-9]|[0-9][0-9][0-9])\.ffn_(gate|up|down)_exps.=CPU"` 表示从第 6 层开始卸载 gate、up 和 down MoE 层。
{% endhint %}

```bash
export LLAMA_CACHE="unsloth/grok-2-GGUF"
./llama.cpp/llama-cli \
    -hf unsloth/grok-2-GGUF:Q3_K_XL \\
    --jinja \
    --n-gpu-layers 99 \\
    --temp 1.0 \
    --top-p 0.95 \
    --min-p 0.01 \
    --ctx-size 16384 \
    --seed 3407 \
    -ot ".ffn_.*_exps.=CPU"
```

{% endstep %}

{% step %}
通过以下方式下载模型（在安装 `pip install huggingface_hub hf_transfer` 之后）。你可以选择 `UD-Q3_K_XL` （动态 3-bit 量化）或其他量化版本，如 `Q4_K_M` 。我们 <mark style="background-color:green;">**建议使用我们的 2.7bit 动态量化**</mark><mark style="background-color:green;">**&#x20;**</mark><mark style="background-color:green;">**`UD-Q2_K_XL`**</mark><mark style="background-color:green;">**&#x20;**</mark><mark style="background-color:green;">**或更高，以平衡体积和准确率**</mark>.

```python
# !pip install huggingface_hub hf_transfer
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0" # 有时会触发速率限制，因此设为 0 以禁用
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id = "unsloth/grok-2-GGUF",
    local_dir = "unsloth/grok-2-GGUF",
    allow_patterns = ["*UD-Q3_K_XL*"], # 动态 3bit
)
```

{% endstep %}

{% step %}
你可以编辑 `--threads 32` 来设置 CPU 线程数， `--ctx-size 16384` 来设置上下文长度， `--n-gpu-layers 2` 来设置 GPU 卸载多少层。如果你的 GPU 显存不足，请尝试调整它。如果你只进行 CPU 推理，也请移除它。

{% code overflow="wrap" %}

```bash
./llama.cpp/llama-cli \
    --model unsloth/grok-2-GGUF/UD-Q3_K_XL/grok-2-UD-Q3_K_XL-00001-of-00003.gguf \\
    --jinja \
    --threads -1 \\
    --n-gpu-layers 99 \\
    --temp 1.0 \
    --top-p 0.95 \
    --min-p 0.01 \
    --ctx-size 16384 \
    --seed 3407 \
    -ot ".ffn_.*_exps.=CPU"
```

{% endcode %}
{% endstep %}
{% endstepper %}

## 模型上传

**我们所有的上传** ——包括那些不是基于 imatrix 或动态的——都使用我们的校准数据集，该数据集专门针对对话、编码和语言任务进行了优化。

| MoE 比特数 | 类型 + 链接                                                                             | 磁盘大小        | 详情            |
| ------- | ----------------------------------------------------------------------------------- | ----------- | ------------- |
| 1.66bit | [TQ1\_0](https://huggingface.co/unsloth/grok-2-GGUF/blob/main/grok-2-UD-TQ1_0.gguf) | **81.8 GB** | 1.92/1.56bit  |
| 1.78bit | [IQ1\_S](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-IQ1_S)             | **88.9 GB** | 2.06/1.56bit  |
| 1.93bit | [IQ1\_M](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-IQ1_M)             | **94.5 GB** | 2.5/2.06/1.56 |
| 2.42bit | [IQ2\_XXS](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-IQ2_XXS)         | **99.3 GB** | 2.5/2.06bit   |
| 2.71bit | [Q2\_K\_XL](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-Q2_K_XL)        | **112 GB**  | 3.5/2.5bit    |
| 3.12bit | [IQ3\_XXS](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-IQ3_XXS)         | **117 GB**  | 3.5/2.06bit   |
| 3.5bit  | [Q3\_K\_XL](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-Q3_K_XL)        | **126 GB**  | 4.5/3.5bit    |
| 4.5bit  | [Q4\_K\_XL](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-Q4_K_XL)        | **155 GB**  | 5.5/4.5bit    |
| 5.5bit  | [Q5\_K\_XL](https://huggingface.co/unsloth/grok-2-GGUF/tree/main/UD-Q5_K_XL)        | **191 GB**  | 6.5/5.5bit    |

## :snowboarder: 提升生成速度

如果你有更多 VRAM，可以尝试卸载更多 MoE 层，或直接卸载整个层。

通常， `-ot ".ffn_.*_exps.=CPU"` 会将所有 MoE 层卸载到 CPU！这实际上允许你将所有非 MoE 层放到 1 张 GPU 上，从而提高生成速度。如果你有更多 GPU 容量，可以自定义正则表达式以适配更多层。

如果你的 GPU 内存再多一些，尝试 `-ot ".ffn_(up|down)_exps.=CPU"` 这会卸载上、下投影 MoE 层。

再试试 `-ot ".ffn_(up)_exps.=CPU"` 如果你的 GPU 内存更多一些。这样只会卸载上投影 MoE 层。

你也可以自定义正则表达式，例如 `-ot "\.(6|7|8|9|[0-9][0-9]|[0-9][0-9][0-9])\.ffn_(gate|up|down)_exps.=CPU"` 表示从第 6 层开始卸载 gate、up 和 down MoE 层。

该 [最新的 llama.cpp 版本](https://github.com/ggml-org/llama.cpp/pull/14363) 还引入了高吞吐模式。使用 `llama-parallel`。了解更多 [这里](https://github.com/ggml-org/llama.cpp/tree/master/examples/parallel)。你还可以 **将 KV 缓存量化为 4bits** ，例如减少 VRAM / RAM 传输，这也可以加快生成过程。

## 📐如何适配长上下文（完整 128K）

要适配更长上下文，你可以使用 **KV 缓存量化** 将 K 和 V 缓存量化到更低位数。由于减少了 RAM / VRAM 数据移动，这也可以提高生成速度。允许的 K 量化选项（默认是 `f16`）如下。

`--cache-type-k f32, f16, bf16, q8_0, q4_0, q4_1, iq4_nl, q5_0, q5_1`

你应该使用 `_1` 这些变体，以略微提高准确率，尽管会稍慢一些。例如 `q4_1, q5_1`

你也可以量化 V 缓存，但你需要 **通过以下方式编译带有 Flash Attention 支持的 llama.cpp** ： `-DGGML_CUDA_FA_ALL_QUANTS=ON`，并使用 `--flash-attn` 来启用它。然后你可以将其与 `--cache-type-k` :

`--cache-type-v f32, f16, bf16, q8_0, q4_0, q4_1, iq4_nl, q5_0, q5_1`


---

# Agent Instructions: 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:

```
GET https://unsloth.ai/docs/zh/mo-xing/tutorials/grok-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
