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

# GGUFに保存

GGUF 用にモデルを 16bit で保存するので、以下に使えます [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.wv と 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ビット。",
    "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ビットの超小型量子化",
}
```

{% 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 に保存します:

{% code overflow="wrap" %}

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

{% endcode %}

以下のように 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
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/jp/ji-ben/inference-and-deployment/saving-to-gguf.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.
