# TorchTitan

> PyTorch-native platform for large-scale generative AI model training

- Category: Training & Fine-Tuning / Efficient Training
- Language: Python
- Licence: BSD-3-Clause
- Repository: https://github.com/pytorch/torchtitan
- Canonical page: https://ai-tldr.dev/tools/torchtitan/

## In short

TorchTitan is a PyTorch-native platform from the PyTorch team for pretraining and large-scale training of generative AI models such as Llama 3.1. It is built as a minimal, clean-room implementation of PyTorch's native scaling techniques, so the model code stays readable while multi-dimensional parallelism is applied around it. It sits in the Efficient Training category of the Training & Fine-Tuning tools and is open-source, written in Python and licensed BSD-3-Clause.

## Overview

TorchTitan is a PyTorch-native platform from the PyTorch team for pretraining and large-scale training of generative AI models such as Llama 3.1. It is built as a minimal, clean-room implementation of PyTorch's native scaling techniques, so the model code stays readable while multi-dimensional parallelism is applied around it.

It is aimed at researchers and engineers who want to train large models across many GPUs and experiment with new architectures or distributed-training techniques. Rather than hiding the internals, it keeps a small codebase with reusable, swappable components and documented extension points you can build on.

Within the efficient-training space, TorchTitan's focus is combining FSDP2, tensor parallel, pipeline parallel, and context parallel in a single composable stack, alongside features like torch.compile, Float8 training, distributed checkpointing, and activation checkpointing.

## What it does

- Composable multi-dimensional parallelism: FSDP2 per-parameter sharding, Tensor Parallel (incl. async TP), Pipeline Parallel, and Context Parallel
- torch.compile support plus Float8 and MXFP8 (Blackwell) training for dense and MoE models
- Distributed checkpointing, including async checkpointing and checkpoints loadable in torchtune for fine-tuning
- Per-op selective and full activation checkpointing, plus meta-device initialization for large models
- Checkpointable data loading with the C4 dataset preconfigured (144M entries) and support for custom datasets
- Metrics for loss, GPU memory, throughput, TFLOPs, and MFU logged to TensorBoard or Weights & Biases

## Getting started

Install TorchTitan from source (recommended to pair with a recent PyTorch nightly), download a tokenizer, then launch a training run.

1. Install from source

   Clone the repo and install requirements. The README also recommends installing the nightly torchdata.

```bash
git clone https://github.com/pytorch/torchtitan
cd torchtitan
pip install -r requirements.txt
pip install --pre torchdata --index-url https://download.pytorch.org/whl/nightly/cpu
```

2. Download a tokenizer

   Fetch the tokenizer assets from Hugging Face (supply your own HF token).

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

3. Launch a training run

   Run the provided launch script, selecting a model module and config.

```bash
MODULE=llama3 CONFIG=llama3_8b ./run_train.sh
```

## When to use it

- Pretraining Llama 3.1 LLMs of various sizes across multiple GPUs or nodes
- Experimenting with combinations of FSDP, tensor, pipeline, and context parallelism on your own model
- Training long-context models using context parallel for very long sequence lengths
- Producing distributed checkpoints that can later be loaded into torchtune for fine-tuning
