[ PROMPT_NODE_22412 ]
Fine Tuning Axolotl 其他
[ SKILL_DOCUMENTATION ]
# Axolotl - Other
**页数:** 26
---
## 混合精度训练
**URL:** https://docs.axolotl.ai/docs/mixed_precision.html
**内容:**
- 混合精度训练
- 1 FP16 混合精度
- 1.1 概述
- 1.2 配置
- 1.3 FP16 注意事项
- 2 BF16 混合精度
- 2.1 概述
- 2.2 配置
- 3 FP8 混合精度
- 3.1 什么是 FP8?
混合精度训练使用较低精度的数据类型来减少内存使用并提高训练速度,同时保持模型质量。Axolotl 支持多种混合精度格式:
FP16 是传统的半精度格式,在旧 GPU 上受支持,但数值稳定性可能不如 BF16。
BF16 (Brain Float 16) 提供比 FP16 更好的数值稳定性,是现代 GPU 推荐的混合精度格式。它提供与 FP32 相同的动态范围,同时仅使用一半的内存。
FP8 支持是实验性的,需要兼容的硬件 (H100, H200) 和带有 TorchAO 的最新 PyTorch 版本。
FP8(8 位浮点)与 FP16/BF16 相比可以节省大量时间,同时保持训练稳定性。Axolotl 的实现使用 PyTorch 的 TorchAO 库和“tensorwise”缩放策略。
添加到您的 YAML 配置中:
torch.compile 对 FP8 性能至关重要
FP8 训练需要 torch_compile: true 才能看到明显的加速。如果不进行编译,FP8 的速度可能比 FP16/BF16 更慢,且占用更多内存。
对于 FSDP (Fully Sharded Data Parallel) 训练:
始终验证您的混合精度设置:
请参阅 examples/llama-3/3b-fp8-fsdp2.yaml 获取优化后的配置示例。启用 FP8 混合精度 + FP8 all-gather 训练,对于相对较小(3B 参数)的模型,其每秒迭代速度比 BF16 快约 10%
有关多 GPU 训练的更多信息,请参阅我们的多 GPU 指南。
**示例:**
示例 1 (yaml):
yaml
# 自动 BF16 检测(推荐)
bf16: auto
# 或显式启用
bf16: true
# 用于 BF16 评估
bf16: full # 等同于 HF 训练器中的 bf16_full_eval
示例 2 (yaml):
yaml
# 启用 FP8 混合精度
fp8: true
# 可选:为 FSDP all-gather 操作启用 FP8
fp8_enable_fsdp_float8_all_gather: true
# 启用 torch.compile(对于 FP8 加速几乎总是必要的)
torch_compile: true
示例 3 (yaml):
yaml
fp8: true
fp8_enable_fsdp_float8_all_gather: true
torch_compile: true
# FSDP 配置
fsdp_version: 2
fsdp_config:
offload_params: false
cpu_ram_efficient_lo