> 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/zh/mo-xing/qwen3.8/train.md).

# Qwen3.8 微调指南

学习如何使用 Unsloth 微调 Qwen3.8-27B。

Qwen3.8-27B 现在可以通过以下方式进行微调和使用强化学习（RL）训练 [Unsloth](https://github.com/unslothai/unsloth)。它是一个稠密的 27B 统一视觉-语言模型，原生支持文本、图像和视频，具备思考控制和 262K 上下文窗口。

* Unsloth 训练 Qwen3.8 **速度快约 1.5 倍** 使用 **约少 50% 的 VRAM** 比 FA2 配置更省（且不损失准确率）
* 微调 Qwen3.8-27B 通过我们的 **免费的** **Kaggle 笔记本**:

| [**对话式**](https://www.kaggle.com/notebooks/welcome?src=https://github.com/unslothai/notebooks/blob/main/nb/Kaggle-Qwen3.8_\(27B\)-Conversational.ipynb\&accelerator=nvidiaTeslaT4) （你可以启用视觉） | [**RL GRPO**](https://www.kaggle.com/notebooks/welcome?src=https://github.com/unslothai/notebooks/blob/main/nb/Kaggle-Muse_Glimmer_\(30B\)-GRPO.ipynb\&accelerator=nvidiaTeslaT4) |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

* **QLoRA 可在 24GB 上运行** 而 LoRA 需要 >36GB
* 你也可以使用以下方式训练 Qwen3.8 [强化学习](#reinforcement-learning-rl) （RL）免费或在 24GB VRAM 上。
* **全量微调（FFT）** 也可以，但会使用 4 倍更多的 VRAM。
* Unsloth 使用 **Flash Linear Attention 内核** 以高效训练 Qwen3.8
* 使用 [`Qwen3.8-27B-unsloth-bnb-4bit`](https://huggingface.co/unsloth/Qwen3.8-27B-unsloth-bnb-4bit) 用于 4 位 QLoRA，然后 **导出** 为 NVFP4、FP8、GGUF 等格式。
* 如果你想保留推理能力，请将推理风格示例与直接答案混合，并至少保留 75% 的推理数据。

请使用最新的 Transformers v5。Qwen3.8 使用 `qwen3_5` 架构。首次运行可能会更久，因为 Gated DeltaNet 内核需要编译。

{% hint style="success" %}
通过 [Unsloth](https://github.com/unslothai/unsloth)，我们提供多个 Kaggle 笔记本，提供 **30 小时的免费 GPU 使用时长，配备 2× Tesla T4 GPU**。Kaggle 是 Google 的产品，类似于 Google Colab，并提供了一种无需自备 GPU 硬件即可运行微调工作流的便捷方式。
{% endhint %}

### 🦥 Unsloth 指南

{% columns %}
{% column %}
Qwen3.8 可以在以下环境中运行并微调 [Unsloth](/docs/zh/xin/studio.md) Desktop，我们面向本地 AI 的新开源网页 UI。

使用 Unsloth Studio，你可以在 **MacOS、Windows**、Linux，并在 NVIDIA GPU 上训练。本月将提供 Intel、MLX 和 AMD 训练支持。
{% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FD7qpbGJLY5YMv0E0QgrB%2Ftrainingrun_LLM-unsloth.gif?alt=media&amp;token=feb766a7-1a7b-4f47-b5f4-b38e3a0d1470" alt=""><figcaption></figcaption></figure></div>
{% endcolumn %}
{% endcolumns %}

{% stepper %}
{% step %}

#### 安装 Unsloth

开始使用的最简单方式是下载 [Unsloth Desktop 应用](/docs/zh/desktop.md)。适用于 [macOS](/docs/zh/kai-shi-shi-yong/install/mac.md), [Windows](/docs/zh/kai-shi-shi-yong/install/windows-installation.md)，以及 [Linux](/docs/zh/kai-shi-shi-yong/install/linux.md).

<a href="https://unsloth.ai/download" class="button primary" data-icon="down-to-bracket">下载 Unsloth</a>

* <i class="fa-apple">:apple:</i> [下载 macOS 版](https://unsloth.ai/download/mac)
* <i class="fa-windows">:windows:</i> [下载 Windows 版](https://unsloth.ai/download/windows)
* <i class="fa-linux">:linux:</i> [下载 Linux 版](https://unsloth.ai/download/linux)

或者，如果你更喜欢手动安装：

MacOS、Linux、WSL：

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

Windows PowerShell：

```bash
irm https://unsloth.ai/install.ps1 | iex
```

{% endstep %}

{% step %}

#### 训练 Qwen3.8

进入“训练”选项卡，然后在搜索栏中搜索 Qwen3.8-27B，并选择你想要的模型和数据集。接着，按需调整超参数和上下文长度。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FD7qpbGJLY5YMv0E0QgrB%2Ftrainingrun_LLM-unsloth.gif?alt=media&amp;token=feb766a7-1a7b-4f47-b5f4-b38e3a0d1470" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 监控训练进度

点击开始训练后，你将能够监控和观察模型的训练进度。训练损失应当持续下降。\
完成后，模型将自动保存。
{% endstep %}

{% step %}

#### 导出你的微调模型

完成后，Unsloth Studio 允许你将模型导出为 GGUF、safetensor 等格式。
{% endstep %}

{% step %}

#### 比较微调模型与原始模型

点击 `比较模式` 以比较 LoRA 适配器和原始模型。
{% endstep %}
{% endstepper %}

### SFT 配方

以下是一个用于纯文本微调的最小 SFT 配方。你的数据集需要一个 `文本` 列，且已使用 Qwen 聊天模板渲染好。

```python
from unsloth import FastModel
from datasets import load_dataset
from trl import SFTTrainer, SFTConfig

max_seq_length = 2048

dataset = load_dataset(
    "json",
    data_files = "train.jsonl",
    split = "train",
)

model, tokenizer = FastModel.from_pretrained(
    model_name = "unsloth/Qwen3.8-27B-unsloth-bnb-4bit",
    max_seq_length = max_seq_length,
    load_in_4bit = True,
    full_finetuning = False,
    offload_embedding = True,
)

model = FastModel.get_peft_model(
    model,
    finetune_vision_layers     = False,
    finetune_language_layers   = True,
    finetune_attention_modules = True,
    finetune_mlp_modules       = True,

    r = 16,
    lora_alpha = 16,
    lora_dropout = 0,
    bias = "none",
    use_gradient_checkpointing = "unsloth",
    random_state = 3407,
    use_rslora = False,
    loftq_config = None,
)

trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    args = SFTConfig(
        dataset_text_field = "text",
        max_seq_length = max_seq_length,
        per_device_train_batch_size = 1,
        gradient_accumulation_steps = 4,
        warmup_steps = 10,
        max_steps = 100,
        learning_rate = 2e-4,
        logging_steps = 1,
        optim = "adamw_8bit",
        output_dir = "outputs_qwen38",
        seed = 3407,
        dataset_num_proc = 1,
        report_to = "none",
    ),
)

trainer.train()
```

`offload_embedding=True` 是可选项，它通过将大型、未绑定的输入嵌入保留在 RAM 中来减少常驻 VRAM。Unsloth 会在不受支持的平台上自动禁用它。

如果你发生 OOM，请降低 `max_seq_length` 并将批大小保持为 1，并使用 `"unsloth"` 梯度检查点。

### 视觉微调

Qwen3.8 原生支持图像和视频输入。对于多模态训练，请启用视觉层，并使用一个对话式视觉数据集以及 `UnslothVisionDataCollator`.

```python
model = FastModel.get_peft_model(
    model,
    finetune_vision_layers     = True,
    finetune_language_layers   = True,
    finetune_attention_modules = True,
    finetune_mlp_modules       = True,

    r = 16,
    lora_alpha = 16,
    lora_dropout = 0,
    bias = "none",
    use_gradient_checkpointing = "unsloth",
    random_state = 3407,
    use_rslora = False,
    loftq_config = None,
)
```

设置 `finetune_vision_layers=False` 当你的数据集仅为文本时。

### 强化学习（RL）

Qwen3.8 使用相同的 `qwen3_5` 架构，因此请使用 Qwen3.5 的 Unsloth RL 路径并禁用快速 vLLM 推理：

```python
from unsloth import FastModel

model, tokenizer = FastModel.from_pretrained(
    model_name = "unsloth/Qwen3.8-27B-unsloth-bnb-4bit",
    max_seq_length = 2048,
    load_in_4bit = True,
    fast_inference = False,
)
```

### 保存 / 导出微调模型

在部署时使用与你训练时相同的聊天模板和 EOS token。

**保存为 GGUF**

```python
model.save_pretrained_gguf(
    "qwen38_gguf",
    tokenizer,
    quantization_method = "q4_k_m",
)

# model.push_to_hub_gguf(
#     "hf_username/qwen38_gguf",
#     tokenizer,
#     quantization_method = "q4_k_m",
# )
```

**保存为 vLLM**

```python
model.save_pretrained_merged(
    "qwen38_finetuned",
    tokenizer,
    save_method = "merged_16bit",
)
```

若只保存 LoRA 适配器：

```python
model.save_pretrained("qwen38_lora")
tokenizer.save_pretrained("qwen38_lora")
```


---

# 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/zh/mo-xing/qwen3.8/train.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.
