SKILL.md
SKILL.mdBrowse 5 files
2,944 tokens
10,334 bytes
Token encoding: o200k_base
Snapshot 24fd22b
1---2name: torchtitan3description: Pretrain LLMs at scale with PyTorch 4D parallelism.4version: 1.0.15author: Orchestra Research6license: MIT7dependencies: [torch>=2.6.0, torchtitan>=0.2.0, torchao>=0.5.0]8platforms: [linux, macos]9metadata:10 hermes:11 tags: [Model Architecture, Distributed Training, TorchTitan, FSDP2, Tensor Parallel, Pipeline Parallel, Context Parallel, Float8, Llama, Pretraining]12 13---14 15# TorchTitan - PyTorch Native Distributed LLM Pretraining16 17## Quick start18 19TorchTitan is PyTorch's official platform for large-scale LLM pretraining with composable 4D parallelism (FSDP2, TP, PP, CP), achieving 65%+ speedups over baselines on H100 GPUs.20 21**Installation**:22```bash23# From PyPI (stable)24pip install torchtitan25 26# From source (latest features, requires PyTorch nightly)27git clone https://github.com/pytorch/torchtitan28cd torchtitan29pip install -r requirements.txt30```31 32**Download tokenizer**:33```bash34# Get HF token from https://huggingface.co/settings/tokens35python scripts/download_hf_assets.py --repo_id meta-llama/Llama-3.1-8B --assets tokenizer --hf_token=...36```37 38**Start training on 8 GPUs**:39```bash40# Configs are selected by name from the Python config registry41# (torchtitan/models/llama3/config_registry.py), not by TOML path42MODULE=llama3 CONFIG=llama3_8b ./run_train.sh43```44 45## Common workflows46 47### Workflow 1: Pretrain Llama 3.1 8B on single node48 49Copy this checklist:50 51```52Single Node Pretraining:53- [ ] Step 1: Download tokenizer54- [ ] Step 2: Configure training55- [ ] Step 3: Launch training56- [ ] Step 4: Monitor and checkpoint57```58 59**Step 1: Download tokenizer**60 61```bash62python scripts/download_hf_assets.py \63 --repo_id meta-llama/Llama-3.1-8B \64 --assets tokenizer \65 --hf_token=YOUR_HF_TOKEN66```67 68**Step 2: Configure training**69 70In torchtitan's current layout, run configs are defined in a Python **config registry**71(`torchtitan/models/llama3/config_registry.py`) and selected by name via `CONFIG=<name>`72(or `--config <name>`). To customize, register your own config in the registry, or override73individual fields on the command line (e.g. `--optimizer.lr 3e-4 --training.steps 1000`).74 75The equivalent settings for an 8B run look like this (shown as fields; set them in the76registry entry or as `--section.key value` overrides):77 78```toml79# fields for a llama3 8B run (register in config_registry.py or pass as --overrides)80[job]81dump_folder = "./outputs"82description = "Llama 3.1 8B training"83 84[model]85name = "llama3"86flavor = "8B"87hf_assets_path = "./assets/hf/Llama-3.1-8B"88 89[optimizer]90name = "AdamW"91lr = 3e-492 93[lr_scheduler]94warmup_steps = 20095 96[training]97local_batch_size = 298seq_len = 819299max_norm = 1.0100steps = 1000101dataset = "c4"102 103[parallelism]104data_parallel_shard_degree = -1 # Use all GPUs for FSDP105 106[activation_checkpoint]107mode = "selective"108selective_ac_option = "op"109 110[checkpoint]111enable = true112folder = "checkpoint"113interval = 500114```115 116**Step 3: Launch training**117 118```bash119# 8 GPUs on single node (config selected by name from the registry)120MODULE=llama3 CONFIG=llama3_8b ./run_train.sh121 122# Override individual fields on the command line123MODULE=llama3 CONFIG=llama3_8b ./run_train.sh --optimizer.lr 3e-4 --training.steps 1000124 125# Or explicitly with torchrun (run_train.sh wraps this)126torchrun --nproc_per_node=8 \127 -m torchtitan.train \128 --module llama3 --config llama3_8b129```130 131**Step 4: Monitor and checkpoint**132 133TensorBoard logs are saved to `./outputs/tb/`:134```bash135tensorboard --logdir ./outputs/tb136```137 138### Workflow 2: Multi-node training with SLURM139 140```141Multi-Node Training:142- [ ] Step 1: Configure parallelism for scale143- [ ] Step 2: Set up SLURM script144- [ ] Step 3: Submit job145- [ ] Step 4: Resume from checkpoint146```147 148**Step 1: Configure parallelism for scale**149 150For 70B model on 256 GPUs (32 nodes):151```toml152[parallelism]153data_parallel_shard_degree = 32 # FSDP across 32 ranks154tensor_parallel_degree = 8 # TP within node155pipeline_parallel_degree = 1 # No PP for 70B156context_parallel_degree = 1 # Increase for long sequences157```158 159**Step 2: Set up SLURM script**160 161```bash162#!/bin/bash163#SBATCH --job-name=llama70b164#SBATCH --nodes=32165#SBATCH --ntasks-per-node=8166#SBATCH --gpus-per-node=8167 168srun torchrun \169 --nnodes=32 \170 --nproc_per_node=8 \171 --rdzv_backend=c10d \172 --rdzv_endpoint=$MASTER_ADDR:$MASTER_PORT \173 -m torchtitan.train \174 --module llama3 --config llama3_70b175```176 177**Step 3: Submit job**178 179```bash180sbatch multinode_trainer.slurm181```182 183**Step 4: Resume from checkpoint**184 185Training auto-resumes if checkpoint exists in configured folder.186 187### Workflow 3: Enable Float8 training for H100s188 189Float8 provides 30-50% speedup on H100 GPUs.190 191```192Float8 Training:193- [ ] Step 1: Install torchao194- [ ] Step 2: Configure Float8195- [ ] Step 3: Launch with compile196```197 198**Step 1: Install torchao**199 200```bash201USE_CPP=0 pip install git+https://github.com/pytorch/ao.git202```203 204**Step 2: Configure Float8**205 206In the current torchtitan, Float8 is applied at config time via the `quantization`207parameter in your `model_registry()` call inside the config registry (not via a208`[quantize.linear.float8]` TOML section). Add a `Float8LinearConverter.Config`:209 210```python211# in torchtitan/models/llama3/config_registry.py (your model_registry(...) call)212from torchtitan.components.quantization import Float8LinearConverter213 214model_spec = model_registry(215 "8B",216 quantization=[217 Float8LinearConverter.Config(218 recipe_name="rowwise", # or "rowwise_with_gw_hp"219 filter_fqns=["output"], # skip layers too small to benefit220 model_compile_enabled=True, # requires torch.compile for competitive perf221 ),222 ],223)224```225 226Enable `torch.compile` in your run config too:227```toml228[compile]229enable = true230components = ["model", "loss"]231```232 233**Step 3: Launch with compile**234 235```bash236# Float8 config is baked into the registered config; just select it and enable compile237MODULE=llama3 CONFIG=llama3_8b ./run_train.sh --compile.enable238```239 240### Workflow 4: 4D parallelism for 405B models241 242```2434D Parallelism (FSDP + TP + PP + CP):244- [ ] Step 1: Create seed checkpoint245- [ ] Step 2: Configure 4D parallelism246- [ ] Step 3: Launch on 512 GPUs247```248 249**Step 1: Create seed checkpoint**250 251Required for consistent initialization across PP stages:252```bash253NGPU=1 MODULE=llama3 CONFIG=llama3_405b ./run_train.sh \254 --checkpoint.enable \255 --checkpoint.create_seed_checkpoint \256 --parallelism.data_parallel_shard_degree 1 \257 --parallelism.tensor_parallel_degree 1 \258 --parallelism.pipeline_parallel_degree 1259```260 261**Step 2: Configure 4D parallelism**262 263```toml264[parallelism]265data_parallel_shard_degree = 8 # FSDP266tensor_parallel_degree = 8 # TP within node267pipeline_parallel_degree = 8 # PP across nodes268context_parallel_degree = 1 # CP for long sequences269 270[training]271local_batch_size = 32272seq_len = 8192273```274 275**Step 3: Launch on 512 GPUs**276 277```bash278# 64 nodes x 8 GPUs = 512 GPUs279srun torchrun --nnodes=64 --nproc_per_node=8 \280 -m torchtitan.train \281 --module llama3 --config llama3_405b282```283 284## When to use vs alternatives285 286**Use TorchTitan when:**287- Pretraining LLMs from scratch (8B to 405B+)288- Need PyTorch-native solution without third-party dependencies289- Require composable 4D parallelism (FSDP2, TP, PP, CP)290- Training on H100s with Float8 support291- Want interoperable checkpoints with torchtune/HuggingFace292 293**Use alternatives instead:**294- **Megatron-LM**: Maximum performance for NVIDIA-only deployments295- **DeepSpeed**: Broader ZeRO optimization ecosystem, inference support296- **Axolotl/TRL**: Fine-tuning rather than pretraining297- **LitGPT**: Educational, smaller-scale training298 299## Common issues300 301**Issue: Out of memory on large models**302 303Enable activation checkpointing and reduce batch size:304```toml305[activation_checkpoint]306mode = "full" # Instead of "selective"307 308[training]309local_batch_size = 1310```311 312Or use gradient accumulation:313```toml314[training]315local_batch_size = 1316global_batch_size = 32 # Accumulates gradients317```318 319**Issue: TP causes high memory with async collectives**320 321Set environment variable:322```bash323export TORCH_NCCL_AVOID_RECORD_STREAMS=1324```325 326**Issue: Float8 training not faster**327 328Float8 only benefits large GEMMs. Filter small layers via the converter's `filter_fqns`:329```python330from torchtitan.components.quantization import Float8LinearConverter331 332Float8LinearConverter.Config(333 # add "auto_filter_small_kn" to auto-skip layers too small to benefit334 filter_fqns=["attention.wk", "attention.wv", "output", "auto_filter_small_kn"],335 model_compile_enabled=True,336)337```338 339**Issue: Checkpoint loading fails after parallelism change**340 341Use DCP's resharding capability:342```bash343# Convert sharded checkpoint to single file344python -m torch.distributed.checkpoint.format_utils \345 dcp_to_torch checkpoint/step-1000 checkpoint.pt346```347 348**Issue: Pipeline parallelism initialization**349 350Create seed checkpoint first (see Workflow 4, Step 1).351 352## Supported models353 354| Model | Sizes | Status |355|-------|-------|--------|356| Llama 3.1 | 8B, 70B, 405B | Production |357| Llama 4 | Various | Experimental |358| DeepSeek V3 | 16B, 236B, 671B (MoE) | Experimental |359| GPT-OSS | 20B, 120B (MoE) | Experimental |360| Qwen 3 | Various | Experimental |361| Flux | Diffusion | Experimental |362 363## Performance benchmarks (H100)364 365| Model | GPUs | Parallelism | TPS/GPU | Techniques |366|-------|------|-------------|---------|------------|367| Llama 8B | 8 | FSDP | 5,762 | Baseline |368| Llama 8B | 8 | FSDP+compile+FP8 | 8,532 | +48% |369| Llama 70B | 256 | FSDP+TP+AsyncTP | 876 | 2D parallel |370| Llama 405B | 512 | FSDP+TP+PP | 128 | 3D parallel |371 372## Advanced topics373 374**FSDP2 configuration**: See [references/fsdp.md](references/fsdp.md) for detailed FSDP2 vs FSDP1 comparison and ZeRO equivalents.375 376**Float8 training**: See [references/float8.md](references/float8.md) for tensorwise vs rowwise scaling recipes.377 378**Checkpointing**: See [references/checkpoint.md](references/checkpoint.md) for HuggingFace conversion and async checkpointing.379 380**Adding custom models**: See [references/custom-models.md](references/custom-models.md) for TrainSpec protocol.381 382## Resources383 384- GitHub: https://github.com/pytorch/torchtitan385- Paper: https://arxiv.org/abs/2410.06511386- ICLR 2025: https://iclr.cc/virtual/2025/poster/29620387- PyTorch Forum: https://discuss.pytorch.org/c/distributed/torchtitan/44388 389 Discovery context
Discovered by repository scan. No exact path reference found in the snapshot’s root AGENTS.md.