Chapter 02

安装与硬件支持矩阵

📌 commit 1cf145c070ea install.sh + 硬件兼容

Unsloth 的核心优势是手写 Triton kernel 加速,这意味着对 GPU 硬件 / CUDA / PyTorch / Triton 版本要求非常具体。 本章给你一份"哪些 GPU 能用、用哪条命令装"的全套指南。

2.1支持的 GPU 矩阵

Unsloth 通过手写 Triton kernel 提速,不是所有 GPU 都支持。看 unsloth/_gpu_init.py 检测逻辑:

GPU 系架构支持说明
H100 / H200 Hopper SM90 ✅ 一等公民 fp8 + 所有 fused kernel
A100 / A800 / A30 Ampere SM80 bf16 + 全部 fused kernel
4090 / 3090 / RTX A6000Ampere/Ada SM86/89✅ 主力消费卡bf16 优化好;Unsloth 营销主战场
L40 / L4 Ada SM89
V100 Volta SM70 ⚠️ 部分支持 仅 fp16(无 bf16);Triton kernel 兼容性差
T4 Turing SM75 ⚠️ Colab 免费档必须 fp16;速度比 4090 慢但勉强能跑
P100 / K80 Pascal / Kepler 太旧不支持
AMD GPU ROCm Unsloth 是 NVIDIA-only(截至 v2026.5)
Apple Silicon (M 系) MLX ✅ 实验性 v2026 新加 MLX 后端;走 unsloth-zoo
注意 Unsloth 开源版只支持单 GPU。多 GPU 训练要付费的 Unsloth Pro 或社区 patch(第 9 章详讲)。

2.2三条安装路线

路线 A:官方 install.sh / install.ps1(推荐)

仓库自带 install.sh(macOS / Linux)和 install.ps1(Windows),是当前 v2026 后的标准入口

# macOS / Linux / WSL
curl -fsSL https://unsloth.ai/install.sh | sh

# Windows PowerShell
irm https://unsloth.ai/install.ps1 | iex

它会做这些事:

  1. 检测 OS / Python / CUDA 版本;
  2. 建一个独立的 ~/.unsloth/ 目录;
  3. 装 PyTorch(按 CUDA 选 wheel);
  4. 装 Triton(CUDA 上是 triton,Windows 是 triton-windows);
  5. unsloth + unsloth-zoo(依赖库);
  6. 安装 Unsloth Studio(可选 GUI);
  7. unsloth CLI shim。

常用 flag:

--local           # 用当前目录的源码装(开发者用)
--no-torch        # 跳过 PyTorch(仅装 GGUF inference 部分)
--python 3.12     # 强制 Python 版本

路线 B:纯 pip 安装

不想用 install.sh,直接 pip:

# 先装好 PyTorch(按 CUDA 12.x 选)
pip install torch==2.5.1 --index-url https://download.pytorch.org/whl/cu126

# 装 Triton
pip install triton             # Linux / WSL
# 或 Windows:
pip install triton-windows

# 装 Unsloth
pip install unsloth

# 可选:装最新版(commit 级)
pip install --upgrade --force-reinstall --no-cache-dir \
  "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"

路线 C:从本地 repo 装

cd /Users/zjw/Documents/LLM/train/repos/unsloth
pip install -e .

2.3关键依赖版本

组件推荐版本说明
PyTorch 2.4 / 2.5 / 2.6 / 2.7 选最近的稳定版
CUDA 12.1 / 12.4 / 12.6 / 12.8必须和 PyTorch wheel 匹配
Triton 3.0+ Unsloth kernel 的命脉,不要降级
Python 3.10 / 3.11 / 3.12 3.13 部分依赖未跟上
flash-attn 2.7+(可选) 装上有额外加速
bitsandbytes 0.43+ 4bit 量化必需
transformers 跟着 Unsloth 自己 pin Unsloth 经常因 transformers 升级而 break
trl 跟着 Unsloth 自己 pin 同上
peft 跟着 Unsloth 自己 pin LoRA 算法本体
unsloth-zoo ≥ 2026.5.4 Unsloth 的支撑库
关于版本"为什么 pin" Unsloth 通过 monkey-patch HuggingFace 模型(见第 4 章),对 transformers 版本极敏感。 每次 transformers 升级都可能让 patch 失效。Unsloth 团队会发版跟上,但有滞后, 所以最佳实践是用 Unsloth 推荐的版本组合,不要手动升 transformers。

2.4验证安装

# 1. import 不报错
python -c "import unsloth; print(unsloth.__version__)"

# 2. CUDA + Triton 都识别
python -c "
import torch, triton
print('PyTorch:', torch.__version__, 'CUDA:', torch.version.cuda)
print('Triton:', triton.__version__)
print('GPU:', torch.cuda.get_device_name(0))
"

# 3. 跑一个最小的 FastLanguageModel
python -c "
from unsloth import FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
    'unsloth/Llama-3.2-1B',
    max_seq_length=2048,
    load_in_4bit=True,
)
print('Model OK')
"

第 3 条会下载一个 1B 模型(约 800 MB),是验证 Triton kernel 能用的"金标准"。

2.5显存矩阵(典型微调)

模型纯 LoRA bf16+ UnslothQLoRA 4bit+ Unsloth
Llama-3.2-1B 5 GB 3 GB 2 GB 1.5 GB
Llama-3-8B 20 GB 14 GB 10 GB 7 GB
Llama-3-70B 200 GB 140 GB 48 GB 34 GB
Qwen3-4B 12 GB 8 GB 5 GB 3.5 GB
Qwen3-32B 80 GB 56 GB 22 GB 16 GB
Mistral-7B 18 GB 13 GB 8 GB 6 GB
Gemma-3-9B 22 GB 15 GB 11 GB 7.5 GB

对照"4090 24 GB"这条硬件线,QLoRA + Unsloth 让你能训到 70B(极限),常规训 8B-13B 完全宽松。

2.6Colab / Kaggle 免费档

Unsloth 大量营销做在免费 GPU 上微调大模型这件事上。官方维护了 50+ 个 Colab notebook:

平台GPU能跑
Colab 免费 T4 16 GB Llama-3-8B QLoRA SFT
Colab Pro A100 40 GB Llama-3-13B / Qwen3-14B QLoRA
Kaggle P100 / T4 ×2同 Colab 免费
RunPod A100 A100 80 GB Llama-3-70B QLoRA

Colab notebook 入口

仓库 tests/notebooks/ 下有大量参考 notebook,对应每个模型 + 每种训练算法:

tests/notebooks/
  ├── Llama3_(8B)-Alpaca.ipynb              # 经典 SFT
  ├── Llama3_(8B)-ORPO.ipynb                # 偏好对齐
  ├── Llama3_(8B)-Inference.ipynb           # 推理
  ├── Qwen3_(8B)-GRPO.ipynb                 # GRPO RL
  ├── Gemma-3_(9B)-Vision.ipynb             # 多模态
  ├── ...

Colab 直接打开(仓库 README 列了 URL),运行环境 install + 数据预处理 + 训练全包,不要去自己 pip install

2.7常见安装错误

现象原因修复
ImportError: cannot import name 'XXX' from 'transformers' transformers 升级后破坏 Unsloth patch 用 Unsloth 推荐的 transformers 版本;或升 Unsloth 到匹配版
RuntimeError: Triton requires CUDA > 12.x CUDA 太旧 升级 CUDA 至少 12.1
bitsandbytes: GPU not detected bitsandbytes 装的是 CPU-only 版 pip install -U bitsandbytes;Windows 用 jllllll 预编译 whl
Windows 上 Triton 装不上 没装 triton-windows pip install triton-windows(不是 triton
flash-attn 编译失败 未限制并行编译 MAX_JOBS=4 pip install flash-attn --no-build-isolation;或不装 flash-attn,Unsloth 默认走 fa2 fallback
跑训练时报 Triton kernel 错误 Triton 版本和 Unsloth 不匹配 降到 Triton 3.0.0 试
多卡训练 Unsloth 不工作 开源版只支持单 GPU 看第 9 章,社区有 patch

2.8装完后第一件事

tests/notebooks/Llama3_(8B)-Alpaca.ipynb 第一个 cell,确认能下模型 + Triton 能 compile:

from unsloth import FastLanguageModel
import torch

max_seq_length = 2048
dtype = None  # 自动选 bf16 / fp16
load_in_4bit = True

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/llama-3-8b-bnb-4bit",
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
)
print("Unsloth setup OK!")

如果这一段能跑 30 秒内打印 "Unsloth setup OK!",整套环境就 ready 了。

2.9这章带走的