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

# GGUFに保存

GGUF 用にモデルを 16bit で保存して、次の用途に使えるようにします [Unsloth Studio](/docs/jp/shii/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 トークン`を使う必要があります。そうでないと、長い生成で意味不明な出力になることがあります。
* 推論エンジンが不要な「start of sequence」トークンを追加している（または逆に必要なものが欠けている）可能性もあるので、両方の可能性を確認してください！
* <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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/jp/ji-chu/inference-and-deployment/saving-to-gguf.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
