跳到主要内容

分布式 LLM 预训练 Torchtitan

使用 torchtitan 提供基于 PyTorch 原生的分布式 LLM 预训练,支持 4D 并行(FSDP2、TP、PP、CP)。适用于在 8 到 512+ 张 GPU 上大规模预训练 Llama 3.1、DeepSeek V3 或自定义模型,并支持 Float8、torch.compile 和分布式检查点。

技能元数据

来源可选 — 使用 hermes skills install official/mlops/torchtitan 安装
路径optional-skills/mlops/torchtitan
版本1.0.0
作者Orchestra Research
许可证MIT
依赖项torch>=2.6.0, torchtitan>=0.2.0, torchao>=0.5.0
标签Model Architecture, Distributed Training, TorchTitan, FSDP2, Tensor Parallel, Pipeline Parallel, Context Parallel, Float8, Llama, Pretraining

参考:完整 SKILL.md

信息

以下是 Hermes 在触发此技能时加载的完整技能定义。这是技能激活时代理看到的指令。

TorchTitan - PyTorch 原生分布式 LLM 预训练

快速开始

TorchTitan 是 PyTorch 的官方大规模 LLM 预训练平台,支持可组合的 4D 并行(FSDP2、TP、PP、CP),在 H100 GPU 上相比基线实现 65%+ 的加速。

安装

# From PyPI (stable)
pip install torchtitan

# From source (latest features, requires PyTorch nightly)
git clone https://github.com/pytorch/torchtitan
cd torchtitan
pip install -r requirements.txt

下载 tokenizer

# Get HF token from https://huggingface.co/settings/tokens
python scripts/download_hf_assets.py --repo_id meta-llama/Llama-3.1-8B --assets tokenizer --hf_token=...

在 8 张 GPU 上启动训练

CONFIG_FILE="./torchtitan/models/llama3/train_configs/llama3_8b.toml" ./run_train.sh

常见工作流

工作流 1:在单节点上预训练 Llama 3.1 8B

复制此检查清单:

Single Node Pretraining:
- [ ] Step 1: Download tokenizer
- [ ] Step 2: Configure training
- [ ] Step 3: Launch training
- [ ] Step 4: Monitor and checkpoint

步骤 1:下载 tokenizer

python scripts/download_hf_assets.py \
--repo_id meta-llama/Llama-3.1-8B \
--assets tokenizer \
--hf_token=YOUR_HF_TOKEN

步骤 2:配置训练

编辑或创建 TOML 配置文件:

# llama3_8b_custom.toml
[job]
dump_folder = "./outputs"
description = "Llama 3.1 8B training"

[model]
name = "llama3"
flavor = "8B"
hf_assets_path = "./assets/hf/Llama-3.1-8B"

[optimizer]
name = "AdamW"
lr = 3e-4

[lr_scheduler]
warmup_steps = 200

[training]
local_batch_size = 2
seq_len = 8192
max_norm = 1.0
steps = 1000
dataset = "c4"

[parallelism]
data_parallel_shard_degree = -1 # Use all GPUs for FSDP

[activation_checkpoint]
mode = "selective"
selective_ac_option = "op"

[checkpoint]
enable = true
folder = "checkpoint"
interval = 500

步骤 3:启动训练

# 8 GPUs on single node
CONFIG_FILE="./llama3_8b_custom.toml" ./run_train.sh

# Or explicitly with torchrun
torchrun --nproc_per_node=8 \
-m torchtitan.train \
--job.config_file ./llama3_8b_custom.toml

步骤 4:监控与检查点

TensorBoard 日志保存至 ./outputs/tb/

tensorboard --logdir ./outputs/tb

工作流 2:使用 SLURM 进行多节点训练

Multi-Node Training:
- [ ] Step 1: Configure parallelism for scale
- [ ] Step 2: Set up SLURM script
- [ ] Step 3: Submit job
- [ ] Step 4: Resume from checkpoint

步骤 1:为大规模配置并行策略

对于 256 张 GPU(32 个节点)上的 70B 模型:

[parallelism]
data_parallel_shard_degree = 32 # FSDP across 32 ranks
tensor_parallel_degree = 8 # TP within node
pipeline_parallel_degree = 1 # No PP for 70B
context_parallel_degree = 1 # Increase for long sequences

步骤 2:设置 SLURM 脚本

#!/bin/bash
#SBATCH --job-name=llama70b
#SBATCH --nodes=32
#SBATCH --ntasks-per-node=8
#SBATCH --gpus-per-node=8

srun torchrun \
--nnodes=32 \
--nproc_per_node=8 \
--rdzv_backend=c10d \
--rdzv_endpoint=$MASTER_ADDR:$MASTER_PORT \
-m torchtitan.train \
--job.config_file ./llama3_70b.toml

步骤 3:提交作业

sbatch multinode_trainer.slurm

步骤 4:从检查点恢复

如果配置文件夹中存在检查点,训练将自动恢复。

工作流 3:在 H100 上启用 Float8 训练

Float8 在 H100 GPU 上可提供 30-50% 的加速。

Float8 Training:
- [ ] Step 1: Install torchao
- [ ] Step 2: Configure Float8
- [ ] Step 3: Launch with compile

步骤 1:安装 torchao

USE_CPP=0 pip install git+https://github.com/pytorch/ao.git

步骤 2:配置 Float8

添加到你的 TOML 配置中:

[model]
converters = ["quantize.linear.float8"]

[quantize.linear.float8]
enable_fsdp_float8_all_gather = true
precompute_float8_dynamic_scale_for_fsdp = true
filter_fqns = ["output"] # Exclude output layer

[compile]
enable = true
components = ["model", "loss"]

步骤 3:使用 compile 启动

CONFIG_FILE="./llama3_8b.toml" ./run_train.sh \
--model.converters="quantize.linear.float8" \
--quantize.linear.float8.enable_fsdp_float8_all_gather \
--compile.enable

工作流 4:针对 405B 模型的 4D 并行

4D Parallelism (FSDP + TP + PP + CP):
- [ ] Step 1: Create seed checkpoint
- [ ] Step 2: Configure 4D parallelism
- [ ] Step 3: Launch on 512 GPUs

步骤 1:创建种子检查点

PP 阶段间一致初始化所必需:

NGPU=1 CONFIG_FILE=./llama3_405b.toml ./run_train.sh \
--checkpoint.enable \
--checkpoint.create_seed_checkpoint \
--parallelism.data_parallel_shard_degree 1 \
--parallelism.tensor_parallel_degree 1 \
--parallelism.pipeline_parallel_degree 1

步骤 2:配置 4D 并行

[parallelism]
data_parallel_shard_degree = 8 # FSDP
tensor_parallel_degree = 8 # TP within node
pipeline_parallel_degree = 8 # PP across nodes
context_parallel_degree = 1 # CP for long sequences

[training]
local_batch_size = 32
seq_len = 8192

步骤 3:在 512 张 GPU 上启动

# 64 nodes x 8 GPUs = 512 GPUs
srun torchrun --nnodes=64 --nproc_per_node=8 \
-m torchtitan.train \
--job.config_file ./llama3_405b.toml

何时使用及替代方案对比

在以下情况使用 TorchTitan:

  • 从头预训练 LLM(8B 到 405B+)
  • 需要无需第三方依赖的 PyTorch 原生解决方案
  • 需要可组合的 4D 并行(FSDP2、TP、PP、CP)
  • 在支持 Float8 的 H100 上进行训练
  • 希望与 torchtune/HuggingFace 具有互操作性的检查点

改用替代方案:

  • Megatron-LM:针对纯 NVIDIA 部署的最大性能
  • DeepSpeed:更广泛的 ZeRO 优化生态系统,支持推理
  • Axolotl/TRL:用于微调而非预训练
  • LitGPT:教育用途,小规模训练

常见问题

问题:大模型显存不足

启用激活检查点并减小批量大小:

[activation_checkpoint]
mode = "full" # Instead of "selective"

[training]
local_batch_size = 1

或使用梯度累积:

[training]
local_batch_size = 1
global_batch_size = 32 # Accumulates gradients

问题:TP 导致异步集合通信显存占用高

设置环境变量:

export TORCH_NCCL_AVOID_RECORD_STREAMS=1

问题:Float8 训练未提速

Float8 仅受益于大型 GEMM。过滤小层:

[quantize.linear.float8]
filter_fqns = ["attention.wk", "attention.wv", "output", "auto_filter_small_kn"]

问题:并行策略更改后检查点加载失败

使用 DCP 的重分片(resharding)功能:

# Convert sharded checkpoint to single file
python -m torch.distributed.checkpoint.format_utils \
dcp_to_torch checkpoint/step-1000 checkpoint.pt

问题:流水线并行初始化

首先创建种子检查点(参见工作流 4,步骤 1)。

支持的模型

模型规模状态
Llama 3.18B, 70B, 405B生产环境
Llama 4多种实验性
DeepSeek V316B, 236B, 671B (MoE)实验性
GPT-OSS20B, 120B (MoE)实验性
Qwen 3多种实验性
Flux扩散模型实验性

性能基准测试 (H100)

模型GPU 数量并行策略TPS/GPU技术
Llama 8B8FSDP5,762基线
Llama 8B8FSDP+compile+FP88,532+48%
Llama 70B256FSDP+TP+AsyncTP8762D 并行
Llama 405B512FSDP+TP+PP1283D 并行

高级主题

FSDP2 配置:请参阅 references/fsdp.md 以获取详细的 FSDP2 与 FSDP1 对比以及 ZeRO 等效项。

Float8 训练:请参阅 references/float8.md 以了解按张量缩放与按行缩放的方案。

检查点保存:请参阅 references/checkpoint.md 以了解 HuggingFace 转换和异步检查点保存。

添加自定义模型:请参阅 references/custom-models.md 以了解 TrainSpec 协议。

资源