> 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/moderu/gemma-4/qat.md).

# Gemma 4 QAT

Gemma 4 QAT（Quantization-Aware Training）は、Google DeepMindの新しい [Gemma 4](/docs/jp/moderu/gemma-4.md) 〜するように設計された **モデル品質を保ちながらメモリ要件を削減する**。これにより、たとえば次のようなより大きなモデルをローカルで実行できます： **Gemma 4 26B-A4B**を、コンシューマー向けGPU上でローカルに、わずか **16GBのRAM**.

Gemma 4 QATは量子化を前提に学習されており、4ビット形式で約**72%少ないメモリ使用量** で **元の性能に近い**。E2BとE4B向けの特別なモバイル量子化版2種も提供されており、量子化幅の混合を使用しています。

〜に変換すると `Q4_0` QATから素朴に変換すると、26B-A4Bではtop-1精度がわずか70.2%しか得られません。 [私たちはUnsloth Dynamic手法を適用し](#qat-analysis) それを〜まで引き上げるために **85.6%（+15.6%）まで向上させ、さらに** [**200MB小さく**](#usage-guide)!

Gemma 4 QATには以下が含まれます： **E2B**, **E4B**, **12B, 26B-A4B**、および **31B。** これらは、140以上の言語と最大 **256Kコンテキスト。**

{% columns %}
{% column %} <a href="/pages/136e446d64d842f3384bec65ba2ec312454e6e91#run-gemma-4-qat-tutorials" class="button primary">Gemma 4 QATを実行</a><a href="/pages/136e446d64d842f3384bec65ba2ec312454e6e91#qat-analysis" class="button secondary">QAT解析</a>

**Gemma-4-E2B** QATは3GBのRAMで動作し、 **E4B** 5GB&#x3067;**、12B** 7GB&#x3067;**、26-A4B** 15GBで、さらに **31B** 18GB&#x3067;**.**

Gemma 4 QAT GGUFを次のように命名しています `UD-Q4_K_XL` q4\_0はより大きいにもかかわらず精度を低下させることが分かったためです。以下をご覧ください： [Gemma 4 QAT GGUF](https://huggingface.co/collections/unsloth/gemma-4-qat).

比較のために `int4` 量子化については、以下の元版とQAT版のサイズ差をご覧ください。QATは元の精度をほぼすべて維持しながら、約72%少ないメモリを使用します：
{% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="/files/f0d3196b1e43e97d3fb6ebef2f1e0aafda402146" alt="" width="563"><figcaption><p>Gemma 4 Mobile QATの仕組みの可視化。</p></figcaption></figure></div>
{% endcolumn %}
{% endcolumns %}

| Gemma 4     | QAT（int4）GGUF |  元のBF16 |    変化率 |
| ----------- | ------------: | ------: | -----: |
| **E2B**     |       2.62 GB | 9.31 GB | 71.86% |
| **E4B**     |       4.22 GB | 15.1 GB | 72.05% |
| **12B**     |       6.72 GB | 23.8 GB | 71.76% |
| **26B A4B** |       14.2 GB | 50.5 GB | 71.88% |
| **31B**     |       17.3 GB | 61.4 GB | 71.82% |

### 使用ガイド

E2BとE4B向けのGemma 4 QAT派生版はスマートフォンやノートPC向けに設計されており、より大きな26B-A4Bと31Bは QAT モデルは、強力な家庭用GPUだけでなく、ノートPCでも動作するようになりました。

〜があります **GGUFファイルは1つだけ** 各Gemma 4モデルについて、アップロードされた版より高い精度は精度を向上させるどころか低下させることが分かったためです。元の非QATのQ4\_0量子化版を使用してください `UD-Q4_K_XL` こちら [ハードウェア要件](https://huggingface.co/collections/unsloth/gemma-4).

<figure><img src="/files/11009f671b38e83f6a98086ccc2186228ada90bd" alt="" width="563"><figcaption></figcaption></figure>

### 表: Gemma 4 QAT推論GGUFの推奨ハードウェア要件

**（単位 = 総メモリ: RAM + VRAM、またはユニファイドメモリ）。** (units = total memory: RAM + VRAM, or unified memory).

| Gemma 4 QAT     |    要件 |
| --------------- | ----: |
| **E2B** QAT     |  3 GB |
| **E4B** QAT     |  5 GB |
| **12B** QAT     |  7 GB |
| **26B A4B** QAT | 15 GB |
| **31B** QAT     | 18 GB |

### 推奨設定

QATチェックポイントは、Gemma 4の推奨設定をそのまま使用します：

* `temperature = 1.0`
* `top_p = 0.95`
* `top_k = 64`

{% hint style="info" %}
Gemma 4の最大コンテキスト長は **128K** 〜向けは **E2B**, **E4B** および **256K** 〜向けは **12B**, **26B A4B**, **31B**.
{% endhint %}

## QAT解析

QAT BF16をllama.cppのQ4\_0形式へ素朴に変換すると、実際には精度が低下し、Q4\_0におけるBF16 QATラティスとも実際には整合していませんでした。私たちはUnsloth Dynamic手法を適用し、llama.cpp互換のQ4\_0形式と真のBF16 QAT Q4\_0形式の間の一致をより良く強制し、その結果、量子化版をより小さくし（埋め込みにはQ6\_Kは不要でした）、さらに高精度化することに成功しました！

<figure><img src="/files/212a02662c38d25abe7bbfbbb6732591bf7c1f54" alt=""><figcaption></figcaption></figure>

以下にKLD、Top 1%精度、ディスク容量の表を示します。私たちの版が99.9% KLDと平均KLDを劇的に改善しているのが分かります。 **たとえばE2Bでは、素朴なQ4\_0量子化の平均KLDが0.05109なのに対し、私たちのものは0.00173（相対的に29倍良い）で、さらに22%小さいです！**

主な問題は、QAT BF16からllama.cppのQ4\_0形式への変換がロスレスではないことです。llama.cppはF16スケールを使うのに対し、QAT BF16はBF16スケールを使い、さらにスケールはllama.cpp側では最適に決定されません。

素朴な変換ではBF16 QATとのバイト一致率が24.77%ですが、いくつかのハックを使うことで99.96%まで押し上げられることが分かりました！

<table><thead><tr><th width="100">モデル</th><th>手法</th><th>ディスク（GB）</th><th>99.9% KLD</th><th>平均KLD</th><th width="77.5999755859375">Top-1 %</th></tr></thead><tbody><tr><td>E2B</td><td>Unsloth</td><td><strong>2.62</strong></td><td>0.0557</td><td>0.00173</td><td><strong>98.16</strong></td></tr><tr><td>E2B</td><td>Q4_0</td><td>3.35</td><td>1.0513</td><td>0.05109</td><td>89.29</td></tr><tr><td>E4B</td><td>Unsloth</td><td><strong>4.22</strong></td><td>0.0536</td><td>0.00121</td><td><strong>98.54</strong></td></tr><tr><td>E4B</td><td>Q4_0</td><td>5.15</td><td>0.6722</td><td>0.03778</td><td>90.94</td></tr><tr><td>26B</td><td>Unsloth</td><td><strong>14.25</strong></td><td>2.7087</td><td>0.09788</td><td><strong>85.63</strong></td></tr><tr><td>26B</td><td>Q4_0</td><td>14.44</td><td>4.5420</td><td>0.36094</td><td>70.20</td></tr><tr><td>31B</td><td>Unsloth</td><td><strong>17.29</strong></td><td>1.3659</td><td>0.01403</td><td><strong>96.67</strong></td></tr><tr><td>31B</td><td>Q4_0</td><td>17.65</td><td>3.0030</td><td>0.09349</td><td>87.91</td></tr><tr><td>12B</td><td>Unsloth</td><td><strong>6.72</strong></td><td>9.2740</td><td>0.13288</td><td><strong>88.76</strong></td></tr><tr><td>12B</td><td>Q4_0</td><td>6.98</td><td>14.7323</td><td>0.50702</td><td>74.08</td></tr></tbody></table>

## モバイル混合QAT

Gemma-4チームは、Gemma-4-E2B-itとGemma-4-E4B-itの特別なモバイル混合QAT版もリリースしました。私たちはそれらもllama.cpp互換形式に忠実に変換し、ほぼすべての精度も回復しました。2ビット層にはTQ2\_0を使い、ネガティブスケーラーを使用しました。

E2BとE4Bの両方についてUD-Q2\_K\_XL量子化版を作成しました。

|                  | E2Bモバイル         | E4Bモバイル   |
| ---------------- | --------------- | --------- |
| サイズ              | 2.19 GB         | 3.22 GB   |
| 2ビット（TQ2\_0）テンソル | 61（deep MLPを含む） | 2（埋め込みのみ） |
| BF16に対する平均KLD    | 0.00409         | 0.00102   |
| Top-1 %          | 97.82%          | 98.76%    |
| ベースPPL           | \~103           | 42.4      |

参照 [gemma-4-E2B-it-qat-GGUF](https://huggingface.co/unsloth/gemma-4-E2B-it-qat-GGUF) および [gemma-4-E4B-it-qat-GGUF](https://huggingface.co/unsloth/gemma-4-E4B-it-qat-GGUF) 〜向けは `UD-Q2_K_XL`.

## Gemma 4 QATチュートリアルを実行

Gemma 4 GGUFは複数のサイズがあるため、小さいモデルの推奨開始点は8ビットで、大きいモデルの推奨開始点は **動的4ビット**. [Gemma 4 GGUF](https://huggingface.co/collections/unsloth/gemma-4-qat):

| [E2B](https://huggingface.co/unsloth/gemma-4-E2B-it-qat-GGUF) | [E4B](https://huggingface.co/unsloth/gemma-4-E4B-it-qat-GGUF) | [12b](https://huggingface.co/unsloth/gemma-4-12b-it-qat-GGUF) | [26B-A4B](https://huggingface.co/unsloth/gemma-4-26B-A4B-it-qat-GGUF) | [31B](https://huggingface.co/unsloth/gemma-4-31B-it-qat-GGUF) |
| ------------------------------------------------------------- | ------------------------------------------------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------- | ------------------------------------------------------------- |

<a href="/pages/136e446d64d842f3384bec65ba2ec312454e6e91#unsloth-studio-guide" class="button primary">🦥 Unsloth Studioガイド</a><a href="/pages/136e446d64d842f3384bec65ba2ec312454e6e91#llama.cpp-guide" class="button primary">🦙 Llama.cppガイド</a>

{% columns %}
{% column %}
**私たちの** [**Unsloth Studio**](/docs/jp/xin-gui/studio.md)✨ **ノートブック：**
{% endcolumn %}

{% column %}
{% embed url="<https://colab.research.google.com/github/unslothai/unsloth/blob/main/studio/Unsloth_Studio_Colab.ipynb>" %}
{% endcolumn %}
{% endcolumns %}

### 🦥 Unsloth Studioガイド

Gemma 4 QATは現在、 [Unsloth Studio](/docs/jp/xin-gui/studio.md)。これはローカルAI向けの新しいオープンソースWeb UIです。Unsloth Studioでは、モデルをローカルで次の環境上で実行できます： **MacOS、Windows**、Linux、そして：

{% columns %}
{% column %}

* 検索、ダウンロード、 [GGUFを実行](/docs/jp/xin-gui/studio.md#run-models-locally) およびsafetensorモデル
* [**自己修復** ツール呼び出し](/docs/jp/xin-gui/studio.md#execute-code--heal-tool-calling) + **ウェブ検索**
* [**コード実行**](/docs/jp/xin-gui/studio.md#run-models-locally) (Python、Bash)
* [自動推論](https://unsloth.ai/docs/desktop#feature-deep-dive) パラメータ調整（temp、top-pなど）
* llama.cppによる高速なCPU+GPU推論
* [LLMを学習](/docs/jp/xin-gui/studio.md#no-code-training) VRAMを70%削減しつつ2倍高速
  {% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="/files/15781d66f25558946794c4025a5ece324f0db186" 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
```

{% endstep %}

{% step %}

#### Unslothを起動

**MacOS、Linux、WSL、およびWindows：**

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

次に開く `http://127.0.0.1:8888` （またはお使いのURL）をブラウザで。
{% endstep %}

{% step %}

#### Gemma 4 QATを検索してダウンロード

初回起動時には、アカウントを保護するためのパスワードを作成し、再度サインインする必要があります。

次に〜へ移動し [Unsloth Chat](/docs/jp/xin-gui/studio/chat.md) タブで検索バーにGemma 4を入力し、目的のモデルと量子化版をダウンロードしてください。
{% endstep %}

{% step %}

#### Gemma 4 QATを実行

Unsloth Studioを使うと推論パラメータは自動設定されますが、手動でも変更できます。コンテキスト長、チャットテンプレート、その他の設定も編集できます。

詳細は、次をご覧ください： [Unsloth Studio推論ガイド](/docs/jp/xin-gui/studio/chat.md).

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

### 🦙 Llama.cppガイド

このガイドでは量子化タイプを選択する必要はありません。1つしかないためです： `UD-Q4_K_XL`。参照： [Gemma 4 QATコレクション](https://huggingface.co/collections/unsloth/gemma-4-qat)。これらのチュートリアルでは、次を使用します： [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 %}
使用したい場合は `llama.cpp` モデルを直接読み込むには、各モデルに応じて以下のコマンドに従ってください。 `UD-Q4_K_XL` は唯一の量子化タイプです。Hugging Face（ステップ3）からもダウンロードできます。これは次に似ています： `ollama run` 。使用してください： `export LLAMA_CACHE="folder"` を強制して `llama.cpp` 特定の場所に保存します。

**26B-A4B：**

```bash
export LLAMA_CACHE="unsloth/gemma-4-26B-A4B-it-qat-GGUF"
./llama.cpp/llama-cli \
    -hf unsloth/gemma-4-26B-A4B-it-qat-GGUF:UD-Q4_K_XL \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64
```

**31B：**

```bash
export LLAMA_CACHE="unsloth/gemma-4-31B-it-qat-GGUF"
./llama.cpp/llama-cli \
    -hf unsloth/gemma-4-31B-it-qat-GGUF:UD-Q4_K_XL \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64
```

**E4B：**

```bash
export LLAMA_CACHE="unsloth/gemma-4-E4B-it-qat-GGUF"
./llama.cpp/llama-cli \
    -hf unsloth/gemma-4-E4B-it-qat-GGUF:UD-Q4_K_XL \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64
```

**E2B：**

```bash
export LLAMA_CACHE="unsloth/gemma-4-E2B-it-GGUF"
./llama.cpp/llama-cli \
    -hf unsloth/gemma-4-E2B-it-qat-GGUF:UD-Q4_K_XL \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64
```

{% endstep %}

{% step %}
（以下をインストールした後）モデルを次の方法でダウンロードしてください `pip install huggingface_hub hf_transfer` ）。次を選択できます： `UD-Q4_K_XL` または、次のような他の量子化版： `Q8_0` 。ダウンロードが止まる場合は、次をご覧ください： [Hugging Face Hub、XETのデバッグ](/docs/jp/ji-chu/troubleshooting-and-faqs/hugging-face-hub-xet-debugging.md)

```bash
hf download unsloth/gemma-4-26B-A4B-it-qat-GGUF \
    --local-dir unsloth/gemma-4-26B-A4B-it-qat-GGUF \
    --include "*mmproj-BF16*" \
    --include "*UD-Q4_K_XL*" # Dynamic 2bitでは"*UD-Q2_K_XL*"を使用
```

{% endstep %}

{% step %}
次に、会話モードでモデルを実行します（vision対応で `mmproj-F16`):

{% code overflow="wrap" %}

```bash
./llama.cpp/llama-cli \
    --model unsloth/gemma-4-26B-A4B-it-qat-GGUF/gemma-4-26B-A4B-it-qat-UD-Q4_K_XL.gguf \
    --mmproj unsloth/gemma-4-26B-A4B-it-qat-GGUF/mmproj-BF16.gguf \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64
```

{% endcode %}
{% endstep %}

{% step %}

### Llama-serverへのデプロイ

llama-serverでGemma-4をデプロイするには、次を使用します：

```bash
./llama.cpp/llama-server \
    --model unsloth/gemma-4-26B-A4B-it-qat-GGUF/gemma-4-26B-A4B-it-qat-UD-Q4_K_XL.gguf \
    --mmproj unsloth/gemma-4-26B-A4B-it-qat-GGUF/mmproj-BF16.gguf \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64 \
    --alias "unsloth/gemma-4-26B-A4B-it-qat-GGUF" \
    --port 8001 \
    --chat-template-kwargs '{"enable_thinking":true}'
```

{% hint style="warning" %}
〜するには [思考 / 推論を無効化する](#how-to-enable-or-disable-reasoning-and-thinking)、次を使用します： `--chat-template-kwargs '{"enable_thinking":false}'`

もし〜をお使いなら **Windows** PowerShellでは、次を使用します： `--chat-template-kwargs "{\"enable_thinking\":false}"`

「true」と「false」は互換的に使えます。
{% endhint %}
{% endstep %}
{% endstepper %}


---

# 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/moderu/gemma-4/qat.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.
