torchtitan¶
What it is. Google's fork of TorchTitan, the PyTorch reference platform for large-scale training, adapted to Cloud TPU. Upstream TorchTitan (trainer, configuration, parallelism, models) is kept; the TPU work lives in one directory, torchtitan/experiments/tpu, which the book calls the TPU lane. It ships recipes for dense and MoE language models and FLUX on v6e and v7x. Upstream: https://github.com/google-pytorch/torchtitan. The book teaches it in chapter 14 and the TorchTitan guide.
Layout¶
| Path | What lives there |
|---|---|
torchtitan/train.py, trainer.py |
Upstream entry point and Trainer |
torchtitan/config/ |
manager.py (--module, --config, tyro overrides), configs.py (job, training, parallelism, compile, profiling fields), configurable.py |
torchtitan/distributed/ |
parallel_dims.py (device meshes), fsdp.py, tensor, context and pipeline parallelism, utils.py (mesh construction) |
torchtitan/models/, components/, protocols/ |
Upstream model families and shared blocks (models/common/); checkpointing, data, optimizers, metrics; model and sharding protocols |
torchtitan/experiments/tpu/ |
The TPU lane: train.py (TPUTrainer), tpu_job_config.py, tpu_flags.py, graph_trainer/ (TpuGraphTrainer), kernels/, model packages (llama3/, qwen3/, qwen3_5/, deepseek_v3/, flux/, muse_glimmer/), examples/, recipes/ |
torchtitan/experiments/tpu/recipes/ |
trillium/ (v6e) and ironwood/ (v7x): one directory per model and slice with its command and expected metrics |
torchtitan/experiments/tpu/kernels/ |
Pallas and Tokamax kernels: splash_attention.py, linear_softmax_cross_entropy_loss.py, gmm.py, segment matmuls, route_score.py |
torchtitan/experiments/graph_trainer/ |
Upstream whole-step tracing trainer that TpuGraphTrainer builds on |
tests/, torchtitan/experiments/tpu/tests/ |
Upstream unit and integration tests; TPU lane tests |
run_train.sh, .ci/ |
Upstream GPU launcher; CI scripts including TPU test discovery |
Entry points¶
| Command, flag or symbol | What it does | Source | Taught in |
|---|---|---|---|
pip install torch==2.15.0 torch_tpu==0.2.0 -r torchtitan/experiments/tpu/requirements.txt (plus the CPU index) |
Installs the TPU lane; then pip install -r requirements.txt separately (a typeguard conflict warning is expected) |
experiments/tpu/README.md |
TorchTitan guide |
torchrun --nproc_per_node=4 -m torchtitan.experiments.tpu.train --module=torchtitan.experiments.tpu.qwen3 --config=qwen3_1_7b --training.steps=10 |
A first job on one v6e-4 host with the bundled tokenizer and data |
experiments/tpu/train.py |
Ch 14, guide |
--module, --config |
Recipe package and the function in its config_registry.py, such as qwen3_1_7b() or llama3_8b() |
config/manager.py, qwen3/config_registry.py |
Ch 14 |
--section.field=value, --debug.print-config |
Override any config field; print the merged config, libtpu and XLA flags included | config/configs.py |
guide |
TPUTrainer and TpuGraphTrainer |
Eager loop with per-block torch.compile, or the whole step traced; train.py dispatches on the recipe's config type |
experiments/tpu/train.py, graph_trainer/tpu_graph_trainer.py |
Ch 14 |
--parallelism.use_simple_fsdp, --parallelism.enable_tpu_topo_mesh |
SimpleFSDP instead of FSDP2; rank layout from topology_aware_mesh (both default off) |
config/configs.py |
Ch 14, FSDP guide |
--parallelism.data_parallel_replicate_degree=$MEGASCALE_NUM_SLICES, --parallelism.enable_min_cost_redistribute |
Several slices: replicate across slices, shard within | config/configs.py |
Ch 14 |
--tpu_config.eager_mode=DEFER_AND_FUSE |
Eager mode for the parts of the step outside compiled regions | tpu_job_config.py |
guide |
--libtpu-init-flags, --xla-flags, --extra-libtpu-init-flags, --extra-xla-flags |
libtpu and XLA flags as config fields; an exported LIBTPU_INIT_ARGS or XLA_FLAGS still wins |
tpu_flags.py |
Ch 14 |
--compile.enable, --splash_attention_kernel.use_splash_attention_kernel |
Compile the model; use the Splash attention kernel | kernels/splash_attention.py |
guide |
--profiler.enable_profiling, --profiler.profile_freq, --profiler.profiler_warmup, --profiler.profiler_active |
XProf capture during training | experiments/tpu/README.md |
guide, Ch 10 |
tpu_config.enable_mfu_benchmark, mfu_benchmark_start_step (default 5) |
MFU measured from a steady-state step | tpu_job_config.py |
benchmark guide, Ch 24 |
get_peak_flops(device_name) |
Peak bf16 FLOP/s per device used for MFU | tools/utils.py |
Ch 14 |
gcluster job submit ... --command "torchrun ... -m torchtitan.experiments.tpu.train ..." |
Runs a recipe on a GKE slice built with Cluster Toolkit | experiments/tpu/README.md |
guide |
MODULE=llama3 CONFIG=llama3_debugmodel NGPU=8 ./run_train.sh |
The upstream GPU launcher; the TPU lane uses torchrun -m torchtitan.experiments.tpu.train instead |
run_train.sh |
Ch 14 |
Extension points¶
| You want to | Do | Taught in |
|---|---|---|
| Add a model upstream already has | experiments/tpu/<name>/ with a registry wrapper, config_registry.py, infra/parallelize.py and tests/ |
Ch 25 |
| Port a model upstream lacks | experiments/tpu/examples/<name>/, subclassing torchtitan/models/common/, with a StateDictAdapter (from_hf, to_hf) |
Ch 25 |
| Register it | Import the package in experiments/tpu/train.py and train_minimal.py |
Ch 25 |
| Follow the procedure | The onboarding skill in torchtitan/experiments/tpu/.claude/skills/tpu_model_onboarding/: one feature behind one flag per rung, each with a parity test |
Ch 25 |
Tests¶
CI classifies a test module by its base class: BaseDistributedDeviceTest goes to the multi-chip shard (file name ending _distributed.py), BaseAcceleratorDeviceTest to the single-TPU shard, anything else to a CPU shard. A plain TestCase that skips without a TPU lands on the CPU shard and skips every run. Run TPU lane modules the way CI does:
python -m <module> --device=tpu # on a chip
python -m <module> --device=cpu # CPU shard
pytest tests/unit_tests # upstream pytest-style tests
.ci/discover_tpu_tests.py builds the shards; "CI green" means the jobs in .github/workflows/tpu-test.yml. The reference-parity test for a model compares logits, loss and every parameter gradient against its reference (chapter 23).