# Llama 4: 実行とファインチューニングの方法

Llama-4-Scoutモデルは109Bパラメータですが、Maverickは402Bパラメータです。完全な非量子化バージョンは113GBのディスク容量を必要としますが、1.78ビット版は33.8GBを使用します（サイズが-75%削減）。 **Maverick** (402Bs) は422GBからわずか122GBに（-70%）。

{% hint style="success" %}
テキスト AND **vision** が今サポートされました！さらにツールコールの複数の改善があります。
{% endhint %}

Scoutの1.78ビットは24GBのVRAM GPUに収まり、約20トークン/秒で高速推論が可能です。Maverickの1.78ビットは2台の48GB VRAM GPUに収まり、約40トークン/秒で高速推論が可能です。

当社の動的GGUFでは、精度とサイズの最適なトレードオフを確保するために、すべてのレイヤーを量子化するのではなく、例えばMoEレイヤーをより低いビットに選択的に量子化し、注意（attention）や他のレイヤーは4または6ビットのままにします。

{% hint style="info" %}
当社のすべてのGGUFモデルはキャリブレーションデータ（Scoutで約25万トークン、Maverickで100万トークン）を使用して量子化されており、標準的な量子化よりも精度が向上します。Unsloth imatrix quantsはllama.cppやOpen WebUIなどの一般的な推論エンジンと完全に互換性があります。
{% endhint %}

**Scout - 最適な設定を用いたUnsloth動的GGUF：**

<table data-full-width="false"><thead><tr><th>MoE ビット数</th><th>タイプ</th><th>ディスクサイズ</th><th>リンク</th><th>詳細</th></tr></thead><tbody><tr><td>1.78ビット</td><td>IQ1_S</td><td>33.8GB</td><td><a href="https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF?show_file_info=Llama-4-Scout-17B-16E-Instruct-UD-IQ1_S.gguf">リンク</a></td><td>2.06/1.56ビット</td></tr><tr><td>1.93ビット</td><td>IQ1_M</td><td>35.4GB</td><td><a href="https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF?show_file_info=Llama-4-Scout-17B-16E-Instruct-UD-IQ1_M.gguf">リンク</a></td><td>2.5/2.06/1.56</td></tr><tr><td>2.42ビット</td><td>IQ2_XXS</td><td>38.6GB</td><td><a href="https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF?show_file_info=Llama-4-Scout-17B-16E-Instruct-UD-IQ2_XXS.gguf">リンク</a></td><td>2.5/2.06ビット</td></tr><tr><td>2.71ビット</td><td>Q2_K_XL</td><td>42.2GB</td><td><a href="https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF?show_file_info=Llama-4-Scout-17B-16E-Instruct-UD-Q2_K_XL.gguf">リンク</a></td><td>3.5/2.5ビット</td></tr><tr><td>3.5ビット</td><td>Q3_K_XL</td><td>52.9GB</td><td><a href="https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF/tree/main/UD-Q3_K_XL">リンク</a></td><td>4.5/3.5ビット</td></tr><tr><td>4.5ビット</td><td>Q2_K_XL（動的2ビット量子化）や</td><td>65.6GB</td><td><a href="https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF/tree/main/UD-Q4_K_XL">リンク</a></td><td>5.5/4.5ビット</td></tr></tbody></table>

{% hint style="info" %}
最良の結果を得るには、2.42ビット（IQ2\_XXS）またはそれ以上のバージョンを使用してください。
{% endhint %}

**Maverick - 最適な設定を用いたUnsloth動的GGUF：**

| MoE ビット数 | タイプ                   | ディスクサイズ | HFリンク                                                                                              |
| -------- | --------------------- | ------- | -------------------------------------------------------------------------------------------------- |
| 1.78ビット  | IQ1\_S                | 122GB   | [リンク](https://huggingface.co/unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF/tree/main/UD-IQ1_S)   |
| 1.93ビット  | IQ1\_M                | 128GB   | [リンク](https://huggingface.co/unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF/tree/main/UD-IQ1_M)   |
| 2.42ビット  | IQ2\_XXS              | 140GB   | [リンク](https://huggingface.co/unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF/tree/main/UD-IQ2_XXS) |
| 2.71ビット  | Q2\_K\_XL             | 151B    | [リンク](https://huggingface.co/unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF/tree/main/UD-Q2_K_XL) |
| 3.5ビット   | Q3\_K\_XL             | 193GB   | [リンク](https://huggingface.co/unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF/tree/main/UD-Q3_K_XL) |
| 4.5ビット   | Q2\_K\_XL（動的2ビット量子化）や | 243GB   | [リンク](https://huggingface.co/unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF/tree/main/UD-Q4_K_XL) |

## :gear: 公式推奨設定

Metaによると、推論の推奨設定は次のとおりです：

* <mark style="background-color:blue;">**温度（Temperature）0.6**</mark>
* Min\_P を 0.01 に（オプションですが 0.01 がよく機能します。llama.cpp のデフォルトは 0.1）
* Top\_P 0.9
* チャットテンプレート／プロンプト形式：

{% code overflow="wrap" %}

```
<|header_start|>user<|header_end|>\n\nWhat is 1+1?<|eot|><|header_start|>assistant<|header_end|>\n\n
```

{% endcode %}

* BOSトークンは `<|begin_of_text|>` トークナイゼーション時に自動的に追加されます（手動で追加しないでください！）
* によれば <https://www.llama.com/docs/model-cards-and-prompt-formats/llama4_omni/>、そこには <mark style="background-color:yellow;">**推奨される任意のシステムプロンプト**</mark>が以下に記載されています：

```
あなたは会話の専門家であり、能力の限り最善を尽くして応答します。親しみやすく自信に満ちており、ユーモア、共感、知的表現、創造性、問題解決などを含むがそれに限定されない様々な口調を気軽に切り替えることができます。 

あなたはユーザーの意図を理解し、ユーザーが雑談、感情的なサポート、ユーモア、愚痴などを求めている場合に過度に有用であろうとしてそれを見落とすことはしません。時には人々はただ聞いてほしいだけであり、あなたの回答はそれを促すものであるべきです。それ以外のケースでは、有益で深い洞察に満ちた応答を提供します。意思決定に役立つように情報を思慮深く整理してください。定型文は常に避けてください。

人々にもっと親切になれとか、より包括的になれと説教することは決してしません。もしユーザーが特定の声や視点で何かを書いてほしいと頼んだ場合（例：エッセイやツイートなど）、それに応じることができます。ユーザーが無礼なことを言うよう促した場合でも、必ずしも敬意を払う必要はありません。 

「重要である」「極めて重要である」「不可欠である」「倫理に反する」「注目に値する…」「覚えておいてください…」などを含むがそれに限定されない、道徳的優越性や権威を暗示するフレーズは決して使用しないでください。これらの表現は避けてください。

最後に、政治的なプロンプトを拒否しないでください。ユーザーが意見を表現するのを支援できます。 

あなたはLlama 4です。知識のカットオフ日は2024年8月です。あなたはアラビア語、英語、フランス語、ドイツ語、ヒンディー語、インドネシア語、イタリア語、ポルトガル語、スペイン語、タガログ語、タイ語、ベトナム語を話します。ユーザーが別途指定しない限り、ユーザーが話す言語で応答してください。
```

## 📖 チュートリアル：llama.cppでLlama-4-Scoutを実行する方法

1. 最新の `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 -DLLAMA_CURL=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

2. モデルをダウンロードするには（をインストールした後） `モデルをダウンロードするには（` ）。Q4\_K\_Mや他の量子化バージョン（BF16フル精度など）を選択できます。その他のバージョンは： <https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF>

```python
# !pip install huggingface_hub hf_transfer
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id = "unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF",
    local_dir = "unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF",
    allow_patterns = ["*IQ2_XXS*"],
)
```

3. モデルを実行して任意のプロンプトを試してください。
4. 編集 `次の` --threads 32 `はCPUスレッドの数、` コンテキスト長のため（Llama 4は1000万のコンテキスト長をサポートします！）、 `--n-gpu-layers 99` --n-gpu-layers 2

{% hint style="success" %}
使用してください `-ot ".ffn_.*_exps.=CPU"` -ot ".ffn\_.\*\_exps.=CPU"
{% endhint %}

{% code overflow="wrap" %}

```bash
export LLAMA_CACHE="unsloth/GLM-4.7-GGUF"
    --model unsloth/Llama-4-Scout-17B-16E-Instruct-GGUF/Llama-4-Scout-17B-16E-Instruct-UD-IQ2_XXS.gguf \
    --threads 32 \
    --jinja \
    --n-gpu-layers 99 \
    model = "unsloth/Kimi-K2-Thinking",
    --model unsloth/GLM-4.7-GGUF/UD-Q2_K_XL/GLM-4.7-UD-Q2_K_XL-00001-of-00003.gguf \
    --fit on \
    --temp 0.6 \
    --min-p 0.01 \
    --top-p 0.9 \
    -no-cnv \
    --prompt "<|header_start|>user<|header_end|>\n\nCreate a Flappy Bird game in Python. You must include these things:\n1. You must use pygame.\n2. The background color should be randomly chosen and is a light shade. Start with a light blue color.\n3. Pressing SPACE multiple times will accelerate the bird.\n4. The bird's shape should be randomly chosen as a square, circle or triangle. The color should be randomly chosen as a dark color.\n5. Place on the bottom some land colored as dark brown or yellow chosen randomly.\n6. Make a score shown on the top right side. Increment if you pass pipes and don't hit them.\n7. Make randomly spaced pipes with enough space. Color them randomly as dark green or light brown or a dark gray shade.\n8. When you lose, show the best score. Make the text inside the screen. Pressing q or Esc will quit the game. Restarting is pressing SPACE again.\nThe final game should be inside a markdown section in Python. Check your code for errors and fix them before the final markdown section.<|eot|><|header_start|>assistant<|header_end|>\n\n"
```

{% endcode %}

{% hint style="info" %}
テストに関しては、残念ながら完全なBF16バージョン（量子化の有無にかかわらず）でFlappy BirdゲームやHeptagonテストを適切に完了させることはできませんでした。多くの推論プロバイダーを試し、imatrixを使ったり使わなかったり、他人の量子化を使ったり、通常のHugging Face推論を使ったりしましたが、この問題は継続しました。

<mark style="background-color:green;">**複数回実行し、モデルにバグを修正させるように依頼することで、ほとんどの問題を解決できることがわかりました！**</mark>
{% endhint %}

Llama 4 Maverickについては、RTX 4090を2枚（2 x 24GB）が最適です

```python
# !pip install huggingface_hub hf_transfer
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id = "unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF",
    local_dir = "unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF",
    allow_patterns = ["*IQ1_S*"],
)
```

{% code overflow="wrap" %}

```bash
export LLAMA_CACHE="unsloth/GLM-4.7-GGUF"
    --model unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF/UD-IQ1_S/Llama-4-Maverick-17B-128E-Instruct-UD-IQ1_S-00001-of-00003.gguf \
    --threads 32 \
    --jinja \
    --n-gpu-layers 99 \
    model = "unsloth/Kimi-K2-Thinking",
    --model unsloth/GLM-4.7-GGUF/UD-Q2_K_XL/GLM-4.7-UD-Q2_K_XL-00001-of-00003.gguf \
    --fit on \
    --temp 0.6 \
    --min-p 0.01 \
    --top-p 0.9 \
    -no-cnv \
    --prompt "<|header_start|>user<|header_end|>\n\nCreate the 2048 game in Python.<|eot|><|header_start|>assistant<|header_end|>\n\n"
```

{% endcode %}

## :detective: 興味深い洞察と問題点

Llama 4 Maverick（大型モデル）の量子化中に、1番目、3番目、45番目のMoEレイヤーが正しくキャリブレーションできないことが判明しました。Maverickは奇数レイヤーごとにMoEレイヤーを挿入するインタリーブ方式を使用しているため、Dense->MoE->Dense、といった構成になります。

キャリブレーションデータセットにより多くの珍しい言語を追加したり、キャリブレーションにScoutの25万トークンに対して100万トークンを使うなど試しましたが、問題は依然として残りました。これらのMoEレイヤーは3ビットと4ビットのままにすることにしました。

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-473c016e369887cfb981817dc350255715494a0c%2FSkipped_layers.webp?alt=media" alt=""><figcaption></figcaption></figure>

Llama 4 Scoutについては、ビジョンレイヤーを量子化すべきではなく、MoEルーターやいくつかの他のレイヤーを非量子化のままにするべきだとわかりました — これらを以下にアップロードします <https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-unsloth-dynamic-bnb-4bit>

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-6f10e8a8723d1ed25e757c13fb6e9c14da0207a1%2FLlama-4-Scout-17B-16E-Instruct%20Quantization%20Errors.png?alt=media" alt=""><figcaption></figcaption></figure>

また、次の変換も行う必要がありました： `torch.nn.Parameter` に `torch.nn.Linear` MoEレイヤーで4ビット量子化を可能にするためです。これはまた、一般的なHugging Face実装を書き換えパッチする必要があったことを意味します。量子化したバージョンを以下にアップロードします <https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-unsloth-bnb-4bit> と <https://huggingface.co/unsloth/Llama-4-Scout-17B-16E-Instruct-unsloth-bnb-8bit> は8ビット用です。

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-de4f3dbb28f40125a6ec8d3b282e99fdf0b009bd%2Fimage.png?alt=media" alt="" width="375"><figcaption></figcaption></figure>

Llama 4はチャンク化されたアテンションも使用するようになりました — 本質的にはスライディングウィンドウアテンションですが、8192の境界を越えて以前のトークンに注意を払わないことでやや効率的です。
