# UnslothガイドでAMD GPU上のLLMをファインチューニング

AMD ハードウェア上で、NVIDIA 不要、メモリ使用量を約70%削減しつつ、LLM を最大 2 倍高速にファインチューニングできます。Unsloth は AMD Radeon RDNA 2/3/3.5/4（RX 6000〜9000 シリーズ）と、MI300X（192GB）を含むデータセンター向け GPU をサポートしています。

{% stepper %}
{% step %}

#### **ワンラインインストーラー**

**最も簡単なインストール：** 以下の手順をすべてスキップして、ワンラインインストーラーを使ってください。AMD GPU を自動検出し、ROCm 最適化版 PyTorch と bitsandbytes をインストールして、Unsloth Studio を起動します：

```
curl -fsSL https://unsloth.ai/install.sh | sh
```

<div data-with-frame="true"><figure><img src="/files/30d197e6dae5d0447f0b3225e01b025c2aa418e4" alt="" width="563"><figcaption></figcaption></figure></div>

以下の手動手順は、Studio なしで Python ライブラリのみをインストールしたいユーザー向けです。
{% endstep %}

{% step %}

#### **新しい分離環境を作成する（任意）**

システムパッケージを壊さないために、分離された pip 環境を作成できます。お使いの Python バージョンを確認するのを忘れないでください！ たとえば次のようになっているかもしれません `pip3`, `pip3.13`, `python3`, `python.3.13` など。

{% code overflow="wrap" %}

```bash
apt update && apt install python3.10-venv python3.11-venv python3.12-venv python3.13-venv -y

python3 -m venv unsloth_env
source unsloth_env/bin/activate
pip install uv
```

{% endcode %}

<div data-with-frame="true"><figure><img src="/files/226a78e7dbf6b1ff03465ad7dd91261b1dd1d1dc" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/d9b53e311a9bac5c36b47c296c2b9f86f688dbf5" alt="" width="365"><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}

#### **PyTorch をインストール**

*ROCm 対応の PyTorch を以下からインストールします* <https://pytorch.org/> \`amd-smi --version\` で ROCm のバージョンを確認し、それに合わせて `https://download.pytorch.org/whl/rocm7.1` を変更してください。 **ROCm 6.0 以降が必要です。** ROCm 5.x 以下には PyTorch の wheel はありません。

{% code overflow="wrap" %}

```bash
uv pip install "torch>=2.4,<2.11.0" "torchvision<0.26.0" "torchaudio<2.11.0" \
    --index-url https://download.pytorch.org/whl/rocm7.1 --upgrade --force-reinstall
```

{% endcode %}

<div data-with-frame="true"><figure><img src="/files/9d43f621418f492f888bfd00045512844675a2f4" alt="" width="563"><figcaption></figcaption></figure></div>

*バージョン上限は、誤って torch 2.11 以降を取得しないためのものです。torch 2.11+ には ROCm 7.2 の wheel しかなく、動作が壊れます。 `rocm7.1` を、先ほど検出したバージョンに合わせて変更してください。*

正しい ROCM バージョンを抽出するための単一のターミナルコマンドも用意しました。必要なら使ってください。

```bash
ROCM_TAG="$({ command -v amd-smi >/dev/null 2>&1 && amd-smi version 2>/dev/null | awk -F'ROCm version: ' 'NF>1{split($2,a,"."); print "rocm"a[1]"."a[2]; ok=1; exit} END{exit !ok}'; } || { [ -r /opt/rocm/.info/version ] && awk -F. '{print "rocm"$1"."$2; exit}' /opt/rocm/.info/version; } || { command -v hipconfig >/dev/null 2>&1 && hipconfig --version 2>/dev/null | awk -F': *' '/HIP version/{split($2,a,"."); print "rocm"a[1]"."a[2]; ok=1; exit} END{exit !ok}'; } || { command -v dpkg-query >/dev/null 2>&1 && ver="$(dpkg-query -W -f="${Version}\n" rocm-core 2>/dev/null)" && [ -n "$ver" ] && awk -F'[.-]' '{print "rocm"$1"."$2; exit}' <<<"$ver"; } || { command -v rpm >/dev/null 2>&1 && ver="$(rpm -q --qf '%{VERSION}\n' rocm-core 2>/dev/null)" && [ -n "$ver" ] && awk -F'[.-]' '{print "rocm"$1"."$2; exit}' <<<"$ver"; })"; [ -n "$ROCM_TAG" ] && uv pip install "torch>=2.4,<2.11.0" "torchvision<0.26.0" "torchaudio<2.11.0" --index-url "https://download.pytorch.org/whl/$ROCM_TAG" --upgrade --force-reinstall
```

*注意: ROCm のバージョンが 7.2 以上の場合は、 `$ROCM_TAG` を上のコマンド内で `rocm7.1,` に置き換えてください。7.2 以降向けの PyTorch wheel はまだありません。*

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

{% step %}

#### **Unsloth をインストール**

AMD 向け追加機能付きで Unsloth をインストール：

{% code overflow="wrap" %}

```bash
uv pip install unsloth[amd]
```

{% endcode %}

<div data-with-frame="true"><figure><img src="/files/f46b5626b837e6c796b8916e80aa41e1895f154c" alt="" width="547"><figcaption></figcaption></figure></div>

⚠️ AMD で必須: ROCm 互換の bitsandbytes をインストール\
すべての ROCm システムではプレリリース版の bitsandbytes ビルドが必要です。0.49.2 以下のバージョンには、すべての AMD GPU で 4-bit decode の NaN バグがあります。注意: この手順では `pip` ではなく `uv` を使用してください。 `uv` ファイル名のバージョン不一致のため、プレリリース wheel が拒否されます。

{% code overflow="wrap" %}

```bash
# x86_64 システム:
pip install --force-reinstall --no-cache-dir --no-deps \
    "https://github.com/bitsandbytes-foundation/bitsandbytes/releases/download/continuous-release_main/bitsandbytes-1.33.7.preview-py3-none-manylinux_2_24_x86_64.whl"

# aarch64 システム: 上の URL の x86_64 を aarch64 に置き換えてください

# URL にアクセスできない場合のフォールバック:
# pip install --force-reinstall --no-cache-dir --no-deps "bitsandbytes>=0.49.1"
```

{% endcode %}

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

{% step %}

#### **Unsloth でファインチューニングを始めましょう！**

以上です。私たちの [**Unsloth Notebooks**](/docs/jp/meru/unsloth-notebooks.md) ページでいくつかの例を試してみてください！

専用の [ファインチューニング](/docs/jp/meru/fine-tuning-llms-guide.md) または [強化学習](/docs/jp/meru/reinforcement-learning-rl-guide.md) ガイドもご覧いただけます。簡単な例も以下に示します：

**1. 環境変数を設定**

{% code overflow="wrap" %}

```bash
export HSA_OVERRIDE_GFX_VERSION=9.4.2  # AMD MI300X に必須
export HF_HUB_DISABLE_XET=1            # AMD 上での HuggingFace ダウンロード問題を修正
```

{% endcode %}

***注意：*** *`HSA_OVERRIDE_GFX_VERSION=9.4.2` は、ROCm に GPU を gfx942（MI300X）として扱わせます。これがないと、一部のカーネルがコンパイルまたは実行に失敗することがあります。*

**2. モデルを読み込み、設定**

{% code overflow="wrap" %}

```python
from unsloth import FastModel

model, tokenizer = FastModel.from_pretrained(
    model_name = "unsloth/gemma-4-26b-a4b-it",
    max_seq_length = 2048,
    load_in_4bit = True,
)

model = FastModel.get_peft_model(
    model,
    r = 16,
    lora_alpha = 16,
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj"],
)
```

{% endcode %}

**3. 学習**

{% code overflow="wrap" %}

```python
from trl import SFTTrainer, SFTConfig

trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    formatting_func = formatting_func,
    args = SFTConfig(
        per_device_train_batch_size = 1,
        gradient_accumulation_steps = 4,
        max_steps = 60,
        output_dir = "outputs",
        report_to = "none",
    ),
)

trainer_stats = trainer.train()
```

{% endcode %}

<div data-with-frame="true"><figure><img src="/files/fc7c13a7e44ccc61ea444191af65add90a0a81ae" alt="" width="375"><figcaption></figcaption></figure></div>

***注意：** AMD GPU では Flash Attention 2 は利用できません。Unsloth は自動的に Xformers にフォールバックし、ROCm 上で同等の性能を提供します。この警告は無視して問題ありません。*
{% endstep %}
{% endstepper %}

### :1234: AMD GPU での強化学習

私たちの :ledger:[gpt-oss RL auto win 2048](https://github.com/unslothai/notebooks/blob/main/nb/AMD-gpt_oss_\(20B\)_Reinforcement_Learning_2048_Game_BF16.ipynb) の例を MI300X（192GB）GPU で使えます。目的は 2048 ゲームを自動でプレイし、RL で勝つことです。LLM（gpt-oss 20b）は、2048 ゲームで勝つための戦略を自動で考案し、勝利につながる戦略には高い報酬、失敗する戦略には低い報酬を与えます。

{% columns %}
{% column %}

<figure><img src="/files/47c681ba63182ba589dab3be7465261120e51ceb" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column %}
約 300 ステップほどで、時間とともに報酬が増加しています！

RL の目的は、2048 ゲームに勝つための平均報酬を最大化することです。

<figure><img src="/files/724d0ba8845a74f874a43c02b5e78ddadfce5e8f" alt=""><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

Unsloth を使って 2048 の RL 例を実行するために AMD MI300X マシン（192GB）を使用しましたが、うまく動作しました！

<div><figure><img src="/files/7aae1a9b79c965b5a329beabe219a299e0177dae" alt=""><figcaption></figcaption></figure> <figure><img src="/files/4401b8aa1cbca218a641373bf7da190f7468865d" alt=""><figcaption></figcaption></figure></div>

また、私たちの :ledger:[自動カーネル生成 RL ノートブック](https://github.com/unslothai/notebooks/blob/main/nb/AMD-gpt_oss_\(20B\)_GRPO_BF16.ipynb) も gpt-oss で使って、Python で行列乗算カーネルを自動生成できます。このノートブックには、報酬ハッキングを防ぐための複数の手法も用意されています。

{% columns %}
{% column width="50%" %}
これらのカーネルを自動生成するために使ったプロンプトは次のとおりです：

{% code overflow="wrap" %}

````
ネイティブの Python コードのみを使って、新しい高速な行列乗算関数を作成してください。
数値のリストのリストが与えられます。
以下の形式で、バッククォート内に新しい関数を出力してください：
```
python
def matmul(A, B):
    return ...
```
````

{% endcode %}
{% endcolumn %}

{% column width="50%" %}
RL プロセスは、たとえば Python 内でより高速な行列乗算のために Strassen アルゴリズムを適用する方法を学習します。

<figure><img src="/files/3967c2d6677e628cfe3a38577e7581a406b1cdb4" alt="" width="375"><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

### :books:AMD 無料ワンクリックノートブック

AMD は、以下を備えたワンクリックノートブックを提供しています **無料の 192GB VRAM 搭載 MI300X GPU** を Dev Cloud 経由で利用できます。サインアップやクレジットカード不要で、大規模モデルを完全無料で学習できます：

* [Qwen3 (32B)](https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb/Qwen3_\(32B\)_A100-Reasoning-Conversational.ipynb)
* [Llama 3.3 (70B)](https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb/AMD-Llama3.3_\(70B\)_A100-Conversational.ipynb)
* [Qwen3 (14B)](https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb/AMD-Qwen3_\(14B\)-Reasoning-Conversational.ipynb)
* [Mistral v0.3 (7B)](https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb/AMD-Mistral_v0.3_\(7B\)-Alpaca.ipynb)
* [GPT OSS MXFP4 (20B)](https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb/AMD-GPT_OSS_MXFP4_\(20B\)-Inference.ipynb) - 推論
* [Gemma4 (E2B)](https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb/Gemma4_\(E2B\)_Reinforcement_Learning_Sudoku_Game.ipynb) - RL Sudoku
* Unsloth Studio

{% embed url="<https://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb/gpt_oss_(20B)_Reinforcement_Learning_2048_Game_BF16.ipynb>" %}

任意の Unsloth ノートブックは、先頭に <https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb> を付けることで [Unslothノートブック](/docs/jp/meru/unsloth-notebooks.md) にあるリンクを <https://github.com/unslothai/notebooks/blob/main/nb/AMD-gpt_oss_(20B)_Reinforcement_Learning_2048_Game_BF16.ipynb>\
から <https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb/AMD-Gemma4_(E2B)_Reinforcement_Learning_Sudoku_Game.ipynb>

{% columns %}
{% column width="33.33333333333333%" %}

<figure><img src="/files/1a908ec12f8aba179888392f9dbb2705b28a09b5" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column width="66.66666666666667%" %}

<figure><img src="/files/8b51d00648f468258eb6498421050e4af68cf307" alt=""><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}


---

# 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/meru/install/amd.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.
