# 使用 Unsloth 在 AMD GPU 上微调 LLM 指南

现在您可以使用 Unsloth 在本地 AMD 环境上微调大型语言模型。Unsloth 支持 AMD Radeon RX、MI300X（192GB）GPU 等。

{% stepper %}
{% step %}
**创建新的隔离环境（可选）**

为了不破坏系统包，您可以创建一个隔离的 pip 环境。提醒检查您使用的 Python 版本！它可能是 `pip3`, `pip3.13`, `python3`, `python.3.13` 等等。

{% code overflow="wrap" %}

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

python -m venv unsloth_env
source unsloth_env/bin/activate
```

{% endcode %}

<figure><img src="/files/063c7b2497f3e57161eb2a6c922a64ee39bd1bfb" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**安装 PyTorch**

从以下位置安装最新的 PyTorch、TorchAO、Xformers： <https://pytorch.org/> 通过以下命令检查您的 ROCM 版本： `amd-smi version` 然后更改 `https://download.pytorch.org/whl/rocm7.0` 以匹配您的版本。

{% code overflow="wrap" %}

```bash
uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm7.0 --upgrade --force-reinstall
```

{% endcode %}

我们还编写了一个单行终端命令以提取正确的 ROCM 版本，以便使用时更方便。

<figure><img src="/files/c486ef545c15e7f816bf9dfaf2feb41e121b5ffd" alt=""><figcaption></figcaption></figure>

```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 torchvision torchaudio --index-url "https://download.pytorch.org/whl/$ROCM_TAG" --upgrade --force-reinstall
```

{% endstep %}

{% step %}
**安装 Unsloth**

安装 Unsloth 的专用 AMD 分支：

{% code overflow="wrap" %}

```bash
pip install --no-deps unsloth unsloth-zoo
pip install --no-deps git+https://github.com/unslothai/unsloth-zoo.git
pip install "unsloth[amd] @ git+https://github.com/unslothai/unsloth"
```

{% endcode %}

<figure><img src="/files/b93072c216e46f91d2c774e44f5506cd274f3ff8" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**使用 Unsloth 开始微调！**

就是这样。尝试我们的一些示例，位于我们的 [**Unsloth 笔记本**](/docs/zh/kai-shi-shi-yong/unsloth-notebooks.md) 页面！

您可以查看我们的专用 [微调](/docs/zh/kai-shi-shi-yong/fine-tuning-llms-guide.md) 或 [强化学习](/docs/zh/kai-shi-shi-yong/reinforcement-learning-rl-guide.md) 指南。

<figure><img src="/files/d7da18122580079b8f45ab41854e4af4bccc744b" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

### :1234: 在 AMD GPU 上的强化学习

您可以使用我们的 :ledger:[gpt-oss RL 自动赢 2048](https://github.com/unslothai/notebooks/blob/main/nb/gpt_oss_\(20B\)_Reinforcement_Learning_2048_Game_BF16.ipynb) 示例在 MI300X（192GB）GPU 上运行。目标是使用强化学习自动玩并赢得 2048 游戏。LLM（gpt-oss 20b）会自动制定赢得 2048 游戏的策略，我们为获胜策略计算高奖励，为失败策略计算低奖励。

{% columns %}
{% column %}

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

{% column %}
在大约 300 步左右后，奖励随时间增加！

强化学习的目标是最大化平均奖励以赢得 2048 游戏。

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

我们使用一台 AMD MI300X（192GB）机器运行了带 Unsloth 的 2048 强化学习示例，运行良好！

<div><figure><img src="/files/a727779807967df2911f0626475f3be2f0237c8a" alt=""><figcaption></figcaption></figure> <figure><img src="/files/54c03a84978b91f6c6a34023b81d62cbae93f000" alt=""><figcaption></figcaption></figure></div>

您还可以使用我们的 :ledger:[自动内核生成 强化学习 笔记本](https://github.com/unslothai/notebooks/blob/main/nb/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%" %}
例如，强化学习过程会学会如何在 Python 内部应用 Strassen 算法以更快地进行矩阵乘法。

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

### :books:AMD 免费一键笔记本

AMD 提供配备以下资源的一键笔记本： **免费的 192GB VRAM MI300X GPU** 通过他们的开发云。完全免费训练大型模型（无需注册或信用卡）：

* [Qwen3（32B）](https://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb/Qwen3_\(32B\)_A100-Reasoning-Conversational.ipynb)
* [Llama 3.3（70B）](https://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb/Llama3.3_\(70B\)_A100-Conversational.ipynb)
* [Qwen3（14B）](http://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb/Qwen3_%2814B%29-Reasoning-Conversational.ipynb)
* [Mistral v0.3（7B）](http://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb/Mistral_v0.3_\(7B\)-Alpaca.ipynb)
* [GPT OSS MXFP4（20B）](http://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb/Kaggle-GPT_OSS_MXFP4_\(20B\)-Inference.ipynb) - 推理
* 强化学习 笔记本：

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

您可以通过在任何 Unsloth 笔记本前添加以下前缀来使用： ***<https://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb>*** 在 [Unsloth 笔记本](/docs/zh/kai-shi-shi-yong/unsloth-notebooks.md) 通过将链接从 <https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Gemma3_(270M).ipynb> 更改为 <https://oneclickamd.ai/github/unslothai/notebooks/blob/main/nb/Gemma3_(270M).ipynb>

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

<figure><img src="/files/9a8a06b6de8a890de6aa3660169d9463ad09f99a" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column width="66.66666666666667%" %}

<figure><img src="/files/5525db179186b6f0e8bf74c1c3d391e3a994a222" 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/zh/kai-shi-shi-yong/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.
