## ✨ 免费训练
Notebooks 对初学者非常友好。阅读我们的[指南](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide)。添加数据集,运行,然后部署你训练好的模型。
| 模型 | 免费 Notebooks | 性能 | 显存占用 |
|-----------|---------|--------|----------|
| **Qwen3.5 (4B)** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_5_(4B)_Vision.ipynb) | 快 1.5 倍 | 减少 60% |
| **gpt-oss (20B)** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-(20B)-Fine-tuning.ipynb) | 快 2 倍 | 减少 70% |
| **gpt-oss (20B): GRPO** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-(20B)-GRPO.ipynb) | 快 2 倍 | 减少 80% |
| **Qwen3: Advanced GRPO** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_(4B)-GRPO.ipynb) | 快 2 倍 | 减少 50% |
| **Gemma 3 (4B) Vision** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Gemma3_(4B)-Vision.ipynb) | 快 1.7 倍 | 减少 60% |
| **embeddinggemma (300M)** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/EmbeddingGemma_(300M).ipynb) | 快 2 倍 | 减少 20% |
| **Mistral Ministral 3 (3B)** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Ministral_3_VL_(3B)_Vision.ipynb) | 快 1.5 倍 | 减少 60% |
| **Llama 3.1 (8B) Alpaca** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.1_(8B)-Alpaca.ipynb) | 快 2 倍 | 减少 70% |
| **Llama 3.2 Conversational** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.2_(1B_and_3B)-Conversational.ipynb) | 快 2 倍 | 减少 70% |
| **Orpheus-TTS (3B)** | [▶️ 免费开始](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Orpheus_(3B)-TTS.ipynb) | 快 1.5 倍 | 减少 50% |
- 查看我们所有的 Notebooks:[Kaggle](https://github.com/unslothai/notebooks?tab=readme-ov-file#-kaggle-notebooks)、[GRPO](https://unsloth.ai/docs/get-started/unsloth-notebooks#grpo-reasoning-rl-notebooks)、[TTS](https://unsloth.ai/docs/get-started/unsloth-notebooks#text-to-speech-tts-notebooks)、[embedding](https://unsloth.ai/docs/new/embedding-finetuning) 和 [Vision](https://unsloth.ai/docs/get-started/unsloth-notebooks#vision-multimodal-notebooks)
- 查看[我们所有的模型](https://unsloth.ai/docs/get-started/unsloth-model-catalog)和[所有的 Notebooks](https://unsloth.ai/docs/get-started/unsloth-notebooks)
- 点击此处查看 Unsloth 的[详细文档](https://unsloth.ai/docs)
## ⚡ 快速开始
### Linux 或 WSL
```
pip install unsloth
```
### Windows
对于 Windows,只有安装了 Pytorch,`pip install unsloth` 才能正常工作。请阅读我们的 [Windows 指南](https://unsloth.ai/docs/get-started/install/windows-installation)。
### Docker
使用我们官方的 [Unsloth Docker 镜像](https://hub.docker.com/r/unsloth/unsloth) ```unsloth/unsloth``` 容器。阅读我们的 [Docker 指南](https://unsloth.ai/docs/get-started/install/docker)。
### AMD, Intel, Blackwell & DGX Spark
对于 RTX 50x, B200, 6000 GPU:`pip install unsloth`。阅读我们的指南:[Blackwell](https://unsloth.ai/docs/blog/fine-tuning-llms-with-blackwell-rtx-50-series-and-unsloth) 和 [DGX Spark](https://unsloth.ai/docs/blog/fine-tuning-llms-with-nvidia-dgx-spark-and-unsloth)。
要在 **AMD** 和 **Intel** GPU 上安装 Unsloth,请遵循我们的 [AMD 指南](https://unsloth.ai/docs/get-started/install/amd) 和 [Intel 指南](https://unsloth.ai/docs/get-started/install/intel)。
## 🦥 Unsloth 动态
- **Qwen3.5** - 现已支持 0.8B, 2B, 4B, 9B, 27B, 35-A3B, 112B-A10B。[指南 + Notebooks](https://unsloth.ai/docs/models/qwen3.5/fine-tune)
- 训练 **MoE LLMs 速度快 12 倍**,VRAM 占用减少 35% - 支持 DeepSeek, GLM, Qwen 和 gpt-oss。[博客](https://unsloth.ai/docs/new/faster-moe)
- **Embedding 模型**:Unsloth 现支持约 1.8-3.3 倍速的 embedding 微调。[博客](https://unsloth.ai/docs/new/embedding-finetuning) • [Notebooks](https://unsloth.ai/docs/get-started/unsloth-notebooks#embedding-models)
- 相比其他所有设置,新的 **7 倍超长上下文 RL**,通过我们新的批处理算法实现。[博客](https://unsloth.ai/docs/new/grpo-long-context)
- 新的 RoPE & MLP **Triton Kernels** & **Padding Free + Packing**:训练速度提升 3 倍,VRAM 占用减少 30%。[博客](https://unsloth.ai/docs/new/3x-faster-training-packing)
- **500K 上下文**:现在可以在 80GB GPU 上训练具有 >500K 上下文的 20B 模型。[博客](https://unsloth.ai/docs/blog/500k-context-length-fine-tuning)
- **FP8 & Vision RL**:现在可以在消费级 GPU 上进行 FP8 和 VLM GRPO。[FP8 博客](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/fp8-reinforcement-learning) • [Vision RL](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/vision-reinforcement-learning-vlm-rl)
- **Docker**:使用我们的新镜像,无需设置即可使用 Unsloth,且无环境问题。[指南](https://unsloth.ai/docs/blog/how-to-fine-tune-llms-with-unsloth-and-docker) • [Docker 镜像](https://hub.docker.com/r/unsloth/unsloth)
- OpenAI 的 **gpt-oss**:阅读我们的 [RL 博客](https://unsloth.ai/docs/models/gpt-oss-how-to-run-and-fine-tune/gpt-oss-reinforcement-learning)、[Flex Attention](https://unsloth.ai/docs/models/gpt-oss-how-to-run-and-fine-tune/long-context-gpt-oss-training) 博客和 [指南](https://unsloth.ai/docs/models/gpt-oss-how-to-run-and-fine-tune)。
点击查看更多动态
- **量化感知训练 (Quantization-Aware Training)**:我们与 Pytorch 合作,恢复了约 70% 的准确率。[阅读博客](https://unsloth.ai/docs/blog/quantization-aware-training-qat)
- **内存高效的 RL**:我们推出了更好的 RL。我们的新内核和算法实现了更快的 RL,VRAM 占用减少 50%,上下文长度增加 10 倍。[阅读博客](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/memory-efficient-rl)
- **Mistral 3**:运行 Ministral 3 或 Devstral 2,并使用 vision/RL 数独 Notebooks 进行微调。[指南](https://unsloth.ai/docs/models/tutorials/ministral-3) • [Notebooks](https://unsloth.ai/docs/models/ministral-3#fine-tuning-ministral-3)
- Google 的 **Gemma 3n**:[阅读博客](https://unsloth.ai/docs/models/gemma-3-how-to-run-and-fine-tune/gemma-3n-how-to-run-and-fine-tune)。我们[上传了 GGUFs 和 4-bit 模型](https://huggingface.co/collections/unsloth/gemma-3n-685d3874830e49e1c93f9339)。
- 现已支持 **[Text-to-Speech (TTS)](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning)**,包括 `sesame/csm-1b` 和 STT `openai/whisper-large-v3`。
- 现已支持 **[Qwen3](https://unsloth.ai/docs/models/qwen3-how-to-run-and-fine-tune)**。Qwen3-30B-A3B 可在 17.5GB VRAM 上运行。
- 推出 **[Dynamic 2.0](https://unsloth.ai/docs/basics/unsloth-dynamic-2.0-ggufs)** 量化,在 5-shot MMLU 和 Aider Polyglot 上设立了新基准。
- [**所有功能**现已支持](https://unsloth.ai/blog/gemma3#everything) - 所有模型 (TTS, BERT, Mamba), FFT 等。现已支持 [MultiGPU](https://unsloth.ai/docs/basics/multi-gpu-training-with-unsloth)。通过 `full_finetuning = True` 启用 FFT,通过 `load_in_8bit = True` 启用 8-bit。
- 📣 [DeepSeek-R1](https://unsloth.ai/blog/deepseek-r1) - [通过我们的指南](https://unsloth.ai/blog/deepseek-r1)运行或微调它们。所有模型上传:[在此](https://huggingface.co/collections/unsloth/deepseek-r1-all-versions-678e1c48f5d2fce87892ace5)。
- 📣 在 Unsloth 中推出长上下文 [推理 (GRPO)](https://unsloth.ai/blog/grpo)。仅需 5GB VRAM 即可训练你自己的推理模型。将 Llama、Phi、Mistral 等转化为推理 LLM!
- 📣 推出 Unsloth [动态 4-bit 量化 (Dynamic 4-bit Quantization)](https://unsloth.ai/blog/dynamic-4bit)!我们动态地选择不对某些参数进行量化,这大大提高了准确率,而 VRAM 占用仅比 BnB 4-bit 增加 <10%。在 [Hugging Face 此处查看我们的集合。](https://huggingface.co/collections/unsloth/unsloth-4-bit-dynamic-quants-67503bb873f89e15276c44e7)
- 📣 Meta 的 **[Llama 4](https://unsloth.ai/blog/llama4)**(包括 Scout 和 Maverick)现已支持。
- 📣 Microsoft 的 [Phi-4](https://unsloth.ai/blog/phi4):我们还[修复了 Phi-4 中的错误](https://unsloth.ai/blog/phi4)并[上传了 GGUFs, 4-bit](https://huggingface.co/collections/unsloth/phi-4-all-versions-677eecf93784e61afe762afa)。
- 📣 现已支持 [Vision 模型](https://unsloth.ai/blog/vision)-Vision.ipynb)、[Qwen 2.5 VL (7B)](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen2_VL_(7B)-Vision.ipynb) 和 [Pixtral (12B) 2409](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Pixtral_(12B)-Vision.ipynb)
- 📣 [Llama 3.3 (70B)](https://huggingface.co/collections/unsloth/llama-33-all-versions-67535d7d994794b9d7cf5e9f),Meta 的最新模型现已支持。
- 📣 我们与 Apple 合作添加了 [Cut Cross Entropy](https://arxiv.org/abs/2411.09009)。Unsloth 现在在 80GB GPU 上为 Meta 的 Llama 3.3 (70B) 支持 89K 上下文 - 比 HF+FA2 长 13 倍。对于 Llama 3.1 (8B),Unsloth 支持高达 342K 上下文,超越了其原生的 128K 支持。
- 📣 我们发现并协助修复了一个[梯度累积错误](https://unsloth.ai/blog/gradient)!请更新 Unsloth 和 transformers。
- 📣 我们[进一步减少了 30%](https://unsloth.ai/blog/long-context) 的内存占用,并支持 [4 倍长的上下文窗口](https://unsloth.ai/blog/long-context)!
## 🔗 链接与资源
| 类型 | 链接 |
| ----------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
|

**r/unsloth Reddit** | [加入 Reddit 社区](https://reddit.com/r/unsloth) |
| 📚 **文档与 Wiki** | [阅读文档](https://unsloth.ai/docs) |
|
_logo_late_2025.svg)
**Twitter (aka X)** | [在 X 上关注我们](https://twitter.com/unslothai) |
| 💾 **安装** | [Pip & Docker 安装](https://unsloth.ai/docs/get-started/install) |
| 🔮 **我们的模型** | [Unsloth 目录](https://unsloth.ai/docs/get-started/unsloth-model-catalog) |
| ✍️ **博客** | [阅读我们的博客](https://unsloth.ai/blog) |
## ⭐ 主要特性
* 支持完全微调 (**full-finetuning**)、预训练、4-bit、16-bit 和 **FP8** 训练
* 支持**所有模型**,包括 [TTS](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning)、多模态、[embedding](https://unsloth.ai/docs/new/embedding-finetuning) 等!任何在 中可用的模型,都能在 Unsloth 中运行。
* 最高效的[强化学习 (RL)](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide) 库,VRAM 占用减少 80%。支持 GRPO, GSPO, DrGRPO, DAPO 等。
* **0% 准确率损失** - 无近似方法 - 全部精确计算。
* 将模型导出并[部署](https://unsloth.ai/docs/basics/inference-and-deployment)到 [GGUF](https://unsloth.ai/docs/basics/inference-and-deployment/saving-to-gguf) llama.cpp、[vLLM](https://unsloth.ai/docs/basics/inference-and-deployment/vllm-guide)、[SGLang](https://unsloth.ai/docs/basics/inference-and-deployment/sglang-guide) 和 Hugging Face。
* 支持 NVIDIA (自 2018 年起)、[AMD](https://unsloth.ai/docs/get-started/install/amd) 和 [Intel](https://unsloth.ai/docs/get-started/install/intel) GPU。最低 CUDA 能力 7.0 (V100, T4, Titan V, RTX 20, 30, 40x, A100, H100, L40 等)
* 可在 **Linux**、WSL 和 **[Windows](https://unsloth.ai/docs/get-started/install/windows-installation)** 上运行
* 所有内核均使用 OpenAI 的 Triton 语言编写。手动反向传播引擎。
* 如果你用 🦥Unsloth 训练了一个模型,可以使用这个很酷的贴纸!

## 💾 安装 Unsloth
你也可以点击[这里](https://unsloth.ai/docs/get-started/install)查看我们的文档,获取更详细的安装和更新说明。
Unsloth 支持 Python 3.13 或更低版本。
### Pip 安装
**通过 pip 安装(推荐用于 Linux 设备):**
```
pip install unsloth
```
**更新 Unsloth:**
```
pip install --upgrade --force-reinstall --no-cache-dir unsloth unsloth_zoo
```
点击[这里](#advanced-pip-installation)查看高级 pip 安装说明。
### Windows 安装
对于此方法,我们将使用 Anaconda。你可以在[此处查看包含截图的完整指南](https://unsloth.ai/docs/get-started/install/windows-installation)。
1. **安装 Miniconda(或 Anaconda):** 推荐 Miniconda。安装 [Miniconda](https://www.anaconda.com/docs/getting-started/miniconda/install) 或 [Anaconda](https://www.anaconda.com/download),然后打开 Anaconda PowerShell Prompt 继续。
2. **创建 Conda 环境:** 为 Unsloth 创建并激活一个全新的 Python 3.12 环境。
conda create --name unsloth_env python==3.12 -y
conda activate unsloth_env
3. **检查你的 GPU 和 CUDA 版本:** 运行 `nvidia-smi` 确认检测到你的 NVIDIA GPU,并注意输出中显示的 CUDA 版本。如果 `nvidia-smi` 不起作用,请重新安装最新的 [NVIDIA 驱动](https://www.nvidia.com/en-us/drivers/)。
4. **安装 PyTorch:** 安装与你的 CUDA 版本匹配的 PyTorch Windows pip 构建版本。使用 [Install PyTorch](https://pytorch.org/get-started/locally/) 为你的系统选择正确的命令,然后验证 PyTorch 能否看到你的 GPU。
import torch
print(torch.cuda.is_available())
A = torch.ones((10, 10), device="cuda")
B = torch.ones((10, 10), device="cuda")
A @ B
5. **安装 Unsloth:** 仅在 PyTorch 正常工作后才安装 Unsloth。
pip install unsloth
#### 进阶/故障排除
对于**高级安装说明**,或者如果你在安装过程中遇到奇怪的错误:
首先尝试通过 `pip install unsloth` 使用隔离环境
```
python -m venv unsloth
source unsloth/bin/activate
pip install unsloth
```
1. 安装 `torch` 和 `triton`。前往 https://pytorch.org 进行安装。例如 `pip install torch torchvision torchaudio triton`
2. 确认 CUDA 是否安装正确。尝试运行 `nvcc`。如果失败,你需要安装 `cudatoolkit` 或 CUDA 驱动。
3. 手动安装 `xformers`,通过:
```
pip install ninja
pip install -v --no-build-isolation -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers
```
```
Check if `xformers` succeeded with `python -m xformers.info` Go to https://github.com/facebookresearch/xformers. Another option is to install `flash-attn` for Ampere GPUs and ignore `xformers`
```
4. 对于 GRPO 运行,你可以尝试安装 `vllm`,看看 `pip install vllm` 是否成功。
5. 仔细检查你的 Python, CUDA, CUDNN, `torch`, `triton`, 和 `xformers` 版本是否相互兼容。[PyTorch 兼容性矩阵](https://github.com/pytorch/pytorch/blob/main/RELEASE.md#release-compatibility-matrix)可能会很有用。
6. 最后,安装 `bitsandbytes` 并使用 `python -m bitsandbytes` 进行检查
### Conda 安装(可选)
`⚠️仅在你拥有 Conda 时使用。如果没有,请使用 Pip`。我们支持 `python=3.10,3.11,3.12,3.13`。
```
conda create --name unsloth_env python==3.12 -y
conda activate unsloth_env
```
使用 `nvidia-smi` 获取正确的 CUDA 版本,例如 13.0,即 `cu130`
```
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu130
pip3 install unsloth
```
如果你想在 Linux 环境中安装 Conda,请阅读此处,或运行下面的命令 🔽
```
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh
```
### 高级 Pip 安装
`⚠️如果你有 Conda,请**不要**使用此方法。` 由于存在依赖性问题,Pip 稍微复杂一些。Pip 命令对于 `torch 2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,2.10` 和 CUDA 版本是不同的。
对于其他 torch 版本,我们支持 `torch211`, `torch212`, `torch220`, `torch230`, `torch240`, `torch250`, `torch260`, `torch270`, `torch280`, `torch290`, `torch2100`,对于 CUDA 版本,我们支持 `cu118`、`cu121` 和 `cu124`。对于 Ampere 设备 (A100, H100, RTX3090) 及更新设备,使用 `cu118-ampere` 或 `cu121-ampere` 或 `cu124-ampere`。注意:torch 2.10 仅支持 CUDA 12.6, 12.8, 和 13.0。
例如,如果你有 `torch 2.4` 和 `CUDA 12.1`,请使用:
```
pip install --upgrade pip
pip install "unsloth[cu121-torch240] @ git+https://github.com/unslothai/unsloth.git"
```
另一个例子,如果你有 `torch 2.9` 和 `CUDA 13.0`,请使用:
```
pip install --upgrade pip
pip install "unsloth[cu130-torch290] @ git+https://github.com/unslothai/unsloth.git"
```
另一个例子,如果你有 `torch 2.10` 和 `CUDA 12.6`,请使用:
```
pip install --upgrade pip
pip install "unsloth[cu126-torch2100] @ git+https://github.com/unslothai/unsloth.git"
```
以及其他例子:
```
pip install "unsloth[cu121-ampere-torch240] @ git+https://github.com/unslothai/unsloth.git"
pip install "unsloth[cu118-ampere-torch240] @ git+https://github.com/unslothai/unsloth.git"
pip install "unsloth[cu121-torch240] @ git+https://github.com/unslothai/unsloth.git"
pip install "unsloth[cu118-torch240] @ git+https://github.com/unslothai/unsloth.git"
pip install "unsloth[cu121-torch230] @ git+https://github.com/unslothai/unsloth.git"
pip install "unsloth[cu121-ampere-torch230] @ git+https://github.com/unslothai/unsloth.git"
pip install "unsloth[cu121-torch250] @ git+https://github.com/unslothai/unsloth.git"
pip install "unsloth[cu124-ampere-torch250] @ git+https://github.com/unslothai/unsloth.git"
```
或者,在终端中运行以下命令以获取**最佳** pip 安装命令:
```
wget -qO- https://raw.githubusercontent.com/unslothai/unsloth/main/unsloth/_auto_install.py | python -
```
或者,在 Python REPL 中手动运行以下命令:
```
try: import torch
except: raise ImportError('Install torch via `pip install torch`')
from packaging.version import Version as V
import re
v = V(re.match(r"[0-9\.]{3,}", torch.__version__).group(0))
cuda = str(torch.version.cuda)
is_ampere = torch.cuda.get_device_capability()[0] >= 8
USE_ABI = torch._C._GLIBCXX_USE_CXX11_ABI
if cuda not in ("11.8", "12.1", "12.4", "12.6", "12.8", "13.0"): raise RuntimeError(f"CUDA = {cuda} not supported!")
if v <= V('2.1.0'): raise RuntimeError(f"Torch = {v} too old!")
elif v <= V('2.1.1'): x = 'cu{}{}-torch211'
elif v <= V('2.1.2'): x = 'cu{}{}-torch212'
elif v < V('2.3.0'): x = 'cu{}{}-torch220'
elif v < V('2.4.0'): x = 'cu{}{}-torch230'
elif v < V('2.5.0'): x = 'cu{}{}-torch240'
elif v < V('2.5.1'): x = 'cu{}{}-torch250'
elif v <= V('2.5.1'): x = 'cu{}{}-torch251'
elif v < V('2.7.0'): x = 'cu{}{}-torch260'
elif v < V('2.7.9'): x = 'cu{}{}-torch270'
elif v < V('2.8.0'): x = 'cu{}{}-torch271'
elif v < V('2.8.9'): x = 'cu{}{}-torch280'
elif v < V('2.9.1'): x = 'cu{}{}-torch290'
elif v < V('2.9.2'): x = 'cu{}{}-torch291'
elif v < V('2.10.1'): x = 'cu{}{}-torch2100'
else: raise RuntimeError(f"Torch = {v} too new!")
if v > V('2.6.9') and cuda not in ("11.8", "12.6", "12.8", "13.0"): raise RuntimeError(f"CUDA = {cuda} not supported!")
if v >= V('2.10.0') and cuda not in ("12.6", "12.8", "13.0"): raise RuntimeError(f"Torch 2.10 requires CUDA 12.6, 12.8, or 13.0! Got CUDA = {cuda}")
x = x.format(cuda.replace(".", ""), "-ampere" if False else "") # is_ampere is broken due to flash-attn
print(f'pip install --upgrade pip && pip install --no-deps git+https://github.com/unslothai/unsloth-zoo.git && pip install "unsloth[{x}] @ git+https://github.com/unslothai/unsloth.git" --no-build-isolation')
```
### Docker 安装
你可以使用我们预构建的 Docker 容器(包含所有依赖项),无需任何设置即可立即使用 Unsloth。
[阅读我们的指南](https://unsloth.ai/docs/get-started/install/docker)。
此容器需要安装 [NVIDIA 的容器工具包](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)。
```
docker run -d -e JUPYTER_PASSWORD="mypassword" \
-p 8888:8888 -p 2222:22 \
-v $(pwd)/work:/workspace/work \
--gpus all \
unsloth/unsloth
```
在 `http://localhost:8888` 访问 Jupyter Lab 并开始微调!
## 📜 文档
* 前往我们的官方[文档](https://unsloth.ai/docs)了解[运行模型](https://unsloth.ai/docs/basics/inference-and-deployment)、[保存为 GGUF](https://unsloth.ai/docs/basics/inference-and-deployment/saving-to-gguf)、[断点续训 (checkpointing)](https://unsloth.ai/docs/basics/finetuning-from-last-checkpoint)、[评估](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide#evaluation)等更多内容!
* 阅读我们的指南:[微调 (Fine-tuning)](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide)、[强化学习](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide)、[文本转语音 (TTS)](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning)、[视觉 (Vision)](https://unsloth.ai/docs/basics/vision-fine-tuning) 和[任意模型](https://unsloth.ai/docs/models/tutorials)。
* 我们支持 Huggingface 的 transformers, TRL, Trainer, Seq2SeqTrainer 和 Pytorch 代码。
Unsloth 微调 gpt-oss-20b 的示例代码:
```
from unsloth import FastLanguageModel, FastModel, FastVisionModel
import torch
from trl import SFTTrainer, SFTConfig
from datasets import load_dataset
max_seq_length = 2048 # Supports RoPE Scaling internally, so choose any!
# 获取 LAION dataset
url = "https://huggingface.co/datasets/laion/OIG/resolve/main/unified_chip2.jsonl"
dataset = load_dataset("json", data_files = {"train" : url}, split = "train")
# 我们支持的 4bit pre quantized models,下载速度快 4 倍 + 不会出现 OOMs。
fourbit_models = [
"unsloth/gpt-oss-20b-unsloth-bnb-4bit", #or choose any model
] # More models at https://huggingface.co/unsloth
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "unsloth/gpt-oss-20b",
max_seq_length = max_seq_length, # Choose any for long context!
load_in_4bit = True, # 4-bit quantization. False = 16-bit LoRA.
load_in_8bit = False, # 8-bit quantization
load_in_16bit = False, # 16-bit LoRA
full_finetuning = False, # Use for full fine-tuning.
trust_remote_code = False, # Enable to support new models
# token = "hf_...", # use one if using gated models
)
# 进行 model patching 并添加 fast LoRA weights
model = FastLanguageModel.get_peft_model(
model,
r = 16,
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",],
lora_alpha = 16,
lora_dropout = 0, # Supports any, but = 0 is optimized
bias = "none", # Supports any, but = "none" is optimized
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
random_state = 3407,
max_seq_length = max_seq_length,
use_rslora = False, # We support rank stabilized LoRA
loftq_config = None, # And LoftQ
)
trainer = SFTTrainer(
model = model,
train_dataset = dataset,
tokenizer = tokenizer,
args = SFTConfig(
max_seq_length = max_seq_length,
per_device_train_batch_size = 2,
gradient_accumulation_steps = 4,
warmup_steps = 10,
max_steps = 60,
logging_steps = 1,
output_dir = "outputs",
optim = "adamw_8bit",
seed = 3407,
),
)
trainer.train()
# 访问 https://unsloth.ai/docs 获取高级技巧,例如
# (1) 保存为 GGUF / 合并为 16bit 以用于 vLLM 或 SGLang
# (2) 从保存的 LoRA adapter 继续训练
# (3) 添加评估循环 / OOMs
# (4) Customized chat templates
```
## 💡 强化学习
[RL](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide) 包括 [GRPO](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide#training-with-grpo)、[GSPO](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/gspo-reinforcement-learning)、[**FP8** 训练](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/fp8-reinforcement-learning)、DrGRPO, DAPO, PPO, 奖励建模 (Reward Modelling), 在线 DPO 均可在 Unsloth 中运行。
阅读我们的[强化学习指南](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide)或我们的[高级 RL 文档](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/advanced-rl-documentation)以了解批处理、生成和训练参数。
RL Notebooks 列表:
- gpt-oss GRPO notebook:[链接](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-(20B)-GRPO.ipynb)
- ***FP8*** Qwen3-8B GRPO notebook (L4):[链接](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_8B_FP8_GRPO.ipynb)
- Qwen3-VL GSPO notebook:[链接](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_VL_(8B)-Vision-GRPO.ipynb)
- Advanced Qwen3 GRPO notebook:[链接](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_(4B)-GRPO.ipynb)
- ORPO notebook:[链接](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3_(8B)-ORPO.ipynb)
- DPO Zephyr notebook:[链接](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Zephyr_(7B)-DPO.ipynb)
- KTO notebook:[链接](https://colab.research.google.com/drive/1MRgGtLWuZX4ypSfGguFgC-IblTvO2ivM?usp=sharing)
- SimPO notebook:[链接](https://colab.research.google.com/drive/1Hs5oQDovOay4mFA6Y9lQhVJ8TnbFLFh2?usp=sharing)
## 🥇 性能基准测试
- 有关我们最详细的基准测试,请阅读我们的 [Llama 3.3 博客](https://unsloth.ai/blog/llama3-3)。
- [🤗Hugging Face](https://huggingface.co/blog/unsloth-trl) 也对 Unsloth 进行了基准测试。
我们使用 Alpaca 数据集、批次大小为 2、梯度累积步数为 4、rank = 32 进行了测试,并在所有线性层(q, k, v, o, gate, up, down)上应用了 QLoRA:
| 模型 | VRAM | 🦥 Unsloth 速度 | 🦥 VRAM 减少 | 🦥 更长上下文 | 😊 Hugging Face + FA2 |
|----------------|-------|-----------------|----------------|----------------|--------------------|
| Llama 3.3 (70B)| 80GB | 2x | >75% | 13 倍更长 | 1x |
| Llama 3.1 (8B) | 80GB | 2x | >70% | 12 倍更长 | 1x |
### 上下文长度基准
#### Llama 3.1 (8B) 最大上下文长度
我们测试了 Llama 3.1 (8B) Instruct,并在所有线性层(Q, K, V, O, gate, up 和 down)上进行了 4bit QLoRA,rank = 32,批次大小为 1。我们将所有序列填充到特定的最大序列长度,以模拟长上下文微调工作负载。
| GPU VRAM | 🦥Unsloth 上下文长度 | Hugging Face + FA2 |
|----------|-----------------------|-----------------|
| 8 GB | 2,972 | OOM |
| 12 GB | 21,848 | 932 |
| 16 GB | 40,724 | 2,551 |
| 24 GB | 78,475 | 5,789 |
| 40 GB | 153,977 | 12,264 |
| 48 GB | 191,728 | 15,502 |
| 80 GB | 342,733 | 28,454 |
#### Llama 3.3 (70B) 最大上下文长度
我们在 80GB A100 上测试了 Llama 3.3 (70B) Instruct,并在所有线性层(Q, K, V, O, gate, up 和 down)上进行了 4bit QLoRA,rank = 32,批次大小为 1。我们将所有序列填充到特定的最大序列长度,以模拟长上下文微调工作负载。
| GPU VRAM | 🦥Unsloth 上下文长度 | Hugging Face + FA2 |
|----------|------------------------|------------------|
| 48 GB | 12,106 | OOM |
| 80 GB | 89,389 | 6,916 |

### 引用
你可以按如下方式引用 Unsloth 仓库:
```
@software{unsloth,
author = {Daniel Han, Michael Han and Unsloth team},
title = {Unsloth},
url = {https://github.com/unslothai/unsloth},
year = {2023}
}
```
### 致谢
- [llama.cpp 库](https://github.com/ggml-org/llama.cpp),让用户可以使用 Unsloth 保存模型
- Hugging Face 团队及其库:[transformers](https://github.com/huggingface/transformers) 和 [TRL](https://github.com/huggingface/trl)
- Pytorch 和 [Torch AO](https://github.com/unslothai/unsloth/pull/3391) 团队的贡献
- 当然,还有每一位贡献或使用过 Unsloth 的人!