# Leitfaden zur Feinabstimmung von LLMs auf AMD-GPUs mit Unsloth

LLMs bis zu 2x schneller feinabstimmen mit \~70 % weniger Speicher auf AMD-Hardware, keine NVIDIA erforderlich. Unsloth unterstützt AMD Radeon RDNA 2/3/3.5/4 (RX-6000–9000-Serie) und Rechenzentrums-GPUs einschließlich der MI300X (192 GB).

{% stepper %}
{% step %}

#### **Einzeiliger Installer**

**Einfachste Installation:** Überspringen Sie alle untenstehenden Schritte mit dem Einzeilen-Installer; er erkennt Ihre AMD-GPU automatisch, installiert ROCm-optimiertes PyTorch, bitsandbytes und startet Unsloth Studio:

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

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

Die manuellen Schritte unten sind für Nutzer gedacht, die nur eine Python-Bibliotheksinstallation ohne Studio möchten.
{% endstep %}

{% step %}

#### **Neue isolierte Umgebung erstellen (optional)**

Um keine Systempakete zu beschädigen, können Sie eine isolierte pip-Umgebung erstellen. Denken Sie daran, zu prüfen, welche Python-Version Sie haben! Es könnte sein `pip3`, `pip3.13`, `python3`, `python.3.13` usw.

{% 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/50c64418656e292daff6a058c2ef750ae7f4b38b" alt="" width="563"><figcaption></figcaption></figure></div>

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

{% step %}

#### **PyTorch installieren**

*Installieren Sie PyTorch mit ROCm-Unterstützung von* <https://pytorch.org/> Prüfen Sie Ihre ROCm-Version mit \`amd-smi --version\` und ändern Sie dann `https://download.pytorch.org/whl/rocm7.1` so, dass sie dazu passt. **ROCm 6.0 oder neuer ist erforderlich.** Für ROCm 5.x und darunter gibt es keine PyTorch-Wheels.

{% 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/d8907b2e4c08de8a702f171234f2ea90123a93ad" alt="" width="563"><figcaption></figcaption></figure></div>

*Die Versionsobergrenzen verhindern, dass versehentlich torch 2.11+ installiert wird, für das es nur ROCm-7.2-Wheels gibt und das Probleme verursachen würde. Aktualisieren Sie `rocm7.1` wie zuvor, damit es zu Ihrer erkannten Version passt.*

Wir haben auch einen einzelnen Terminalbefehl geschrieben, um die richtige ROCM-Version zu ermitteln, falls das hilfreich ist.

```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
```

*Hinweis: Wenn Ihre ROCm-Version 7.2 oder höher ist, ersetzen Sie `$ROCM_TAG` im obigen Befehl durch `rocm7.1,` für 7.2+ gibt es noch keine PyTorch-Wheels.*

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

{% step %}

#### **Unsloth installieren**

Installieren Sie Unsloth mit AMD-Extras:

{% code overflow="wrap" %}

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

{% endcode %}

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

⚠️ Für AMD erforderlich: ROCm-kompatibles bitsandbytes installieren\
Alle ROCm-Systeme benötigen einen Pre-Release-Build von bitsandbytes; Versionen ≤ 0.49.2 haben auf jeder AMD-GPU einen NaN-Fehler beim 4-Bit-Decoding. Hinweis: Verwenden Sie `pip` nicht `uv` für diesen Schritt, `uv` lehnt das Pre-Release-Wheel wegen einer Versionsabweichung im Dateinamen ab.

{% code overflow="wrap" %}

```bash
# x86_64-Systeme:
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-Systeme: Ersetzen Sie x86_64 in der obigen URL durch aarch64

# Fallback, falls die URL nicht erreichbar ist:
# pip install --force-reinstall --no-cache-dir --no-deps "bitsandbytes>=0.49.1"
```

{% endcode %}

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

{% step %}

#### **Beginnen Sie mit der Feinabstimmung mit Unsloth!**

Und das war's. Probieren Sie einige Beispiele auf unserer [**Unsloth-Notebooks**](/docs/de/loslegen/unsloth-notebooks.md) Seite aus!

Sie können unsere speziellen [Feinabstimmungs-](/docs/de/loslegen/fine-tuning-llms-guide.md) oder [Reinforcement-Learning-](/docs/de/loslegen/reinforcement-learning-rl-guide.md) Anleitungen ansehen. Hier ist ebenfalls ein kurzes Beispiel:

**1. Umgebungsvariablen setzen**

{% code overflow="wrap" %}

```bash
export HSA_OVERRIDE_GFX_VERSION=9.4.2  # Erforderlich für AMD MI300X
export HF_HUB_DISABLE_XET=1            # Behebt Download-Probleme bei HuggingFace auf AMD
```

{% endcode %}

***Hinweis:*** *`HSA_OVERRIDE_GFX_VERSION=9.4.2` weist ROCm an, Ihre GPU als gfx942 (MI300X) zu behandeln. Ohne dies können einige Kerne möglicherweise nicht kompiliert werden oder nicht laufen.*

**2. Modell laden und konfigurieren**

{% 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. Trainieren**

{% 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/adc3354d4438612b113d9e3aa6c15f4c030c852b" alt="" width="375"><figcaption></figcaption></figure></div>

***Hinweis:** Auf AMD-GPUs ist Flash Attention 2 nicht verfügbar. Unsloth fällt automatisch auf Xformers zurück, das auf ROCm eine gleichwertige Leistung bietet. Die Warnung kann sicher ignoriert werden.*
{% endstep %}
{% endstepper %}

### :1234: Reinforcement Learning auf AMD-GPUs

Sie können unser :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) Beispiel auf einer MI300X-(192-GB)-GPU verwenden. Das Ziel ist es, das 2048-Spiel automatisch zu spielen und es mit RL zu gewinnen. Das LLM (gpt-oss 20b) entwickelt automatisch eine Strategie, um das 2048-Spiel zu gewinnen, und wir berechnen eine hohe Belohnung für gewinnbringende Strategien und niedrige Belohnungen für scheiternde Strategien.

{% columns %}
{% column %}

<figure><img src="/files/3a46018e75f28a684632e53082674223e1c00819" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column %}
Die Belohnung über die Zeit steigt nach etwa 300 Schritten oder so an!

Das Ziel von RL ist es, die durchschnittliche Belohnung zu maximieren, um das 2048-Spiel zu gewinnen.

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

Wir haben eine AMD-MI300X-Maschine (192 GB) verwendet, um das 2048-RL-Beispiel mit Unsloth auszuführen, und es hat gut funktioniert!

<div><figure><img src="/files/fe87d41f3ff4deabf6b13560e8bd2f344fa0af51" alt=""><figcaption></figcaption></figure> <figure><img src="/files/3598937ce61c786c1830333e7644c2ebbd1371cc" alt=""><figcaption></figcaption></figure></div>

Sie können auch unser :ledger:[Notebook für automatisierte Kernel-Generierung im RL](https://github.com/unslothai/notebooks/blob/main/nb/AMD-gpt_oss_\(20B\)_GRPO_BF16.ipynb) ebenfalls mit gpt-oss verwenden, um automatisch Matrixmultiplikations-Kerne in Python zu erstellen. Das Notebook verwendet außerdem mehrere Methoden, um Reward Hacking entgegenzuwirken.

{% columns %}
{% column width="50%" %}
Der Prompt, den wir zur automatischen Erstellung dieser Kerne verwendet haben, war:

{% code overflow="wrap" %}

````
Erstelle eine neue schnelle Matrixmultiplikationsfunktion, die nur nativen Python-Code verwendet.
Dir wird eine Liste von Listen mit Zahlen gegeben.
Gib deine neue Funktion in Backticks im folgenden Format aus:
```
python
def matmul(A, B):
    return ...
```
````

{% endcode %}
{% endcolumn %}

{% column width="50%" %}
Der RL-Prozess lernt beispielsweise, wie man den Strassen-Algorithmus für schnellere Matrixmultiplikation in Python anwendet.

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

### :books:Kostenlose AMD-Notebooks mit einem Klick

AMD stellt Ein-Klick-Notebooks mit **kostenlosen MI300X-GPUs mit 192 GB VRAM** über ihre Dev Cloud bereit. Trainieren Sie große Modelle völlig kostenlos (keine Anmeldung und keine Kreditkarte erforderlich):

* [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) - Inferenz
* [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>" %}

Sie können jedes Unsloth-Notebook verwenden, indem Sie <https://amd-ai-academy.com/github/unslothai/notebooks/blob/main/nb> voranstellen [Unsloth-Notebooks](/docs/de/loslegen/unsloth-notebooks.md) indem Sie den Link ändern von <https://github.com/unslothai/notebooks/blob/main/nb/AMD-gpt_oss_(20B)_Reinforcement_Learning_2048_Game_BF16.ipynb>\
zu <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/e04e1a3a8c617ebd14dc3ba2cc8c7428d2161cd7" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column width="66.66666666666667%" %}

<figure><img src="/files/f12dc985f881b756f2c7e47ad7dd222c0cea0586" 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/de/loslegen/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.
