> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monostate.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Unsloth 集成

> 使用 Unsloth 进行更快的 LoRA 训练

# Unsloth 集成

Unsloth 为 LoRA 微调提供优化的训练，显著减少训练时间和内存使用。

## 要求

| 要求         | 详情                                |
| ---------- | --------------------------------- |
| **安装**     | `pip install unsloth`             |
| **支持的训练器** | 仅 `default`、`sft`                 |
| **支持的模型**  | `llama`、`mistral`、`gemma`、`qwen2` |
| **平台**     | 推荐 Linux                          |

<Warning>
  Unsloth 仅适用于 **SFT 训练**（`--trainer sft` 或 `--trainer default`）。不支持 DPO、ORPO、PPO 和其他训练器。
</Warning>

## 支持的模型架构

Unsloth 针对特定模型系列进行了优化：

| 架构        | 示例模型                                |
| --------- | ----------------------------------- |
| `llama`   | Llama 2、Llama 3、Llama 3.1、Llama 3.2 |
| `mistral` | Mistral 7B、Mistral Nemo             |
| `gemma`   | Gemma、Gemma 2                       |
| `qwen2`   | Qwen 2、Qwen 2.5                     |

其他模型架构将回退到标准训练并显示警告。

## 快速开始

```bash theme={null}
aitraining llm --train \
  --model meta-llama/Llama-3.2-1B \
  --data-path ./data \
  --project-name fast-model \
  --trainer sft \
  --unsloth \
  --peft \
  --lora-r 16
```

## 参数

| 参数                     | CLI 标志                   | 默认值     | 描述                            |
| ---------------------- | ------------------------ | ------- | ----------------------------- |
| `unsloth`              | `--unsloth`              | `False` | 启用 Unsloth 以加快训练              |
| `use_sharegpt_mapping` | `--use-sharegpt-mapping` | `False` | 使用 Unsloth 的 ShareGPT 映射而不是转换 |

## Python API

```python theme={null}
from autotrain.trainers.clm.params import LLMTrainingParams
from autotrain.project import AutoTrainProject

params = LLMTrainingParams(
    model="meta-llama/Llama-3.2-1B",
    data_path="./data",
    project_name="fast-model",

    trainer="sft",
    unsloth=True,

    peft=True,
    lora_r=16,
    lora_alpha=32,

    epochs=3,
    batch_size=4,
)

project = AutoTrainProject(params=params, backend="local", process=True)
project.create()
```

## 与量化结合

Unsloth 与 int4 和 int8 量化配合使用以减少内存使用：

```bash theme={null}
aitraining llm --train \
  --model meta-llama/Llama-3.2-3B \
  --data-path ./data \
  --project-name quantized-model \
  --trainer sft \
  --unsloth \
  --peft \
  --quantization int4 \
  --lora-r 16
```

## 工作原理

当启用 Unsloth 并满足要求时：

1. 使用 Unsloth 库的 `FastLanguageModel` 进行优化的模型加载
2. 应用优化的梯度检查点（`use_gradient_checkpointing="unsloth"`）
3. 自动配置 LoRA 目标模块：`q_proj`、`k_proj`、`v_proj`、`o_proj`、`gate_proj`、`up_proj`、`down_proj`
4. 与 PEFT 集成以进行高效的适配器训练

## 回退行为

如果无法使用 Unsloth，训练将继续使用标准 transformers/PEFT：

* **Unsloth 未安装**：记录警告，继续不使用 Unsloth
* **不支持的模型类型**：记录警告，继续不使用 Unsloth
* **不支持的训练器**：不应用 Unsloth（仅支持 SFT）

```
WARNING: Unsloth not available, continuing without it...
```

## ShareGPT 映射

使用 `--use-sharegpt-mapping` 保留 ShareGPT 格式而不是转换：

```bash theme={null}
aitraining llm --train \
  --model meta-llama/Llama-3.2-1B \
  --data-path ./sharegpt_data.json \
  --project-name model \
  --trainer sft \
  --unsloth \
  --use-sharegpt-mapping \
  --peft
```

## 下一步

<CardGroup cols={2}>
  <Card title="LoRA/PEFT" href="/advanced/lora-peft">
    高效微调技术
  </Card>

  <Card title="Quantization" href="/advanced/quantization">
    使用量化减少内存
  </Card>
</CardGroup>
