# GGUFへの保存

モデルを16bitにしてGGUFに保存すれば、次の用途に使えます [Unsloth Studio](/docs/jp/xin-zhe/studio.md)、Ollama、llama.cpp など！

{% tabs %}
{% tab title="ローカルで" %}
GGUFに保存するには、以下を使ってローカルに保存します：

```python
model.save_pretrained_gguf("directory", tokenizer, quantization_method = "q4_k_m")
model.save_pretrained_gguf("directory", tokenizer, quantization_method = "q8_0")
model.save_pretrained_gguf("directory", tokenizer, quantization_method = "f16")
```

Hugging Face Hub にプッシュするには：

```python
model.push_to_hub_gguf("hf_username/directory", tokenizer, quantization_method = "q4_k_m")
model.push_to_hub_gguf("hf_username/directory", tokenizer, quantization_method = "q8_0")
```

の対応している量子化オプションはすべて `quantization_method` として以下に一覧表示されています：

```python
# https://github.com/ggml-org/llama.cpp/blob/master/examples/quantize/quantize.cpp#L19
ALLOWED_QUANTS = \
{
    "not_quantized"  : "推奨。高速な変換。推論は遅く、ファイルは大きい。",
    "fast_quantized" : "推奨。高速な変換。推論はまずまず、ファイルサイズもまずまず。",
    "quantized"      : "推奨。変換は遅い。推論は高速で、ファイルは小さい。",
    "f32"     : "非推奨。精度は100%保持するが、非常に遅くメモリを大量に消費する。",
    "f16"     : "最速の変換 + 精度100%を保持。遅く、メモリを大量に消費する。",
    "q8_0"    : "高速な変換。リソース使用量は多いが、一般的には許容範囲。",
    "q4_k_m"  : "推奨。attention.wv と feed_forward.w2 テンソルの半分に Q6_K を使用し、それ以外は Q4_K",
    "q5_k_m"  : "推奨。attention.wv と feed_forward.w2 テンソルの半分に Q6_K を使用し、それ以外は Q5_K",
    "q2_k"    : "attention.vw と feed_forward.w2 テンソルに Q4_K、他のテンソルに Q2_K を使用します。",
    "q3_k_l"  : "attention.wv、attention.wo、feed_forward.w2 テンソルに Q5_K を使用し、それ以外は Q3_K",
    "q3_k_m"  : "attention.wv、attention.wo、feed_forward.w2 テンソルに Q4_K を使用し、それ以外は Q3_K",
    "q3_k_s"  : "すべてのテンソルに Q3_K を使用します",
    "q4_0"    : "元の量子化方式、4-bit。",
    "q4_1"    : "q4_0 より精度が高いが、q5_0 ほどではない。ただし q5 モデルより推論は速い。",
    "q4_k_s"  : "すべてのテンソルに Q4_K を使用します",
    "q4_k"    : "q4_k_m の別名",
    "q5_k"    : "q5_k_m の別名",
    "q5_0"    : "精度が高く、リソース使用量も多く、推論は遅い。",
    "q5_1"    : "さらに精度が高く、リソース使用量も多く、推論はさらに遅い。",
    "q5_k_s"  : "すべてのテンソルに Q5_K を使用します",
    "q6_k"    : "すべてのテンソルに Q8_K を使用します",
    "iq2_xxs" : "2.06 bpw の量子化",
    "iq2_xs"  : "2.31 bpw の量子化",
    "iq3_xxs" : "3.06 bpw の量子化",
    "q3_k_xs" : "3-bit の超小型量子化",
}
```

{% endtab %}

{% tab title="手動で保存" %}
まずモデルを16bitで保存します：

```python
model.save_pretrained_merged("merged_model", tokenizer, save_method = "merged_16bit",)
```

次にターミナルで以下を実行します：

{% code overflow="wrap" %}

```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 -DLLAMA_CURL=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

python llama.cpp/convert-hf-to-gguf.py FOLDER --outfile OUTPUT --outtype f16
```

{% endcode %}

または、<https://rentry.org/llama-cpp-conversions#merging-loras-into-a-model> の手順に従い、モデル名「merged\_model」を使って GGUF にマージしてください。
{% endtab %}
{% endtabs %}

### Unsloth での実行はうまくいきますが、エクスポート後に他のプラットフォームで実行すると結果が悪くなります

モデルは Unsloth 上ではうまく動いて良い結果を出すのに、Ollama や vLLM など別のプラットフォームで使うと結果が悪かったり、文字化け、無限/終わらない生成になったりすることがあります *または* 繰り返し出&#x529B;**.**

* このエラーの最も一般的な原因は、 <mark style="background-color:blue;">**不正なチャットテンプレート**</mark>**.** Unsloth でモデルを学習したときに使ったのと同じチャットテンプレートを、後で llama.cpp や Ollama など別のフレームワークで実行するときにも使うことが重要です。保存済みモデルから推論する場合は、正しいテンプレートを適用することが不可欠です。
* 正しい `eos トークン`を使わなければなりません。そうしないと、長い生成で文字化けすることがあります。
* 推論エンジンが不要な「開始シーケンス」トークンを追加している（または逆にそれが足りない）ことが原因の可能性もあるので、両方の仮説を確認してください！
* <mark style="background-color:green;">**チャットテンプレートを強制適用するには、会話用ノートブックを使ってください。これでほとんどの問題は解決します。**</mark>
  * Qwen-3 14B 会話用ノートブック [**Colab で開く**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_\(14B\)-Reasoning-Conversational.ipynb)
  * Gemma-3 4B 会話用ノートブック [**Colab で開く**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Gemma3_\(4B\).ipynb)
  * Llama-3.2 3B 会話用ノートブック [**Colab で開く**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.2_\(1B_and_3B\)-Conversational.ipynb)
  * Phi-4 14B 会話用ノートブック [**Colab で開く**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Phi_4-Conversational.ipynb)
  * Mistral v0.3 7B 会話用ノートブック [**Colab で開く**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Mistral_v0.3_\(7B\)-Conversational.ipynb)
  * **さらに多くのノートブックは** [**ノートブックのドキュメント**](/docs/jp/meru/unsloth-notebooks.md)

### GGUF / vLLM 16bit への保存でクラッシュする

保存中の GPU 使用量の上限を次のように変更して下げてみてください `maximum_memory_usage`.

デフォルトは `model.save_pretrained(..., maximum_memory_usage = 0.75)`です。これを 0.5 などに下げると、GPU ピークメモリの50%以下を使うようにできます。これにより保存中の OOM クラッシュを減らせます。

### GGUF に手動で保存するにはどうすればいいですか？

まず、以下の方法でモデルを16bitで保存します：

```python
model.save_pretrained_merged("merged_model", tokenizer, save_method = "merged_16bit",)
```

以下のように llama.cpp をソースからコンパイルします：

{% code overflow="wrap" %}

```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 -DLLAMA_CURL=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
```

{% endcode %}

次に、モデルを F16 で保存します：

```bash
python llama.cpp/convert_hf_to_gguf.py merged_model \
    --outfile model-F16.gguf --outtype f16 \
    --split-max-size 50G
```

```bash
# BF16 の場合：
python llama.cpp/convert_hf_to_gguf.py merged_model \
    --outfile model-BF16.gguf --outtype bf16 \
    --split-max-size 50G
    
# Q8_0 の場合：
python llama.cpp/convert_hf_to_gguf.py merged_model \
    --outfile model-Q8_0.gguf --outtype q8_0 \
    --split-max-size 50G
```


---

# 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/jp/ji-ben/inference-and-deployment/saving-to-gguf.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.
