This repository implements techniques for doing a per-step fault tolerance so
you can keep training if errors occur without interrupting the entire training
job.
torchft is designed to provide the primitives required to implement fault
tolerance in any application/train script as well as the primitives needed to
implement custom fault tolerance strategies.
Out of the box, torchft provides the following algorithms:
Fault Tolerant DDP
Fault Tolerant HSDP: fault tolerance across the replicated dimension with any mix of FSDP/TP/etc across the other dimensions.
LocalSGD
DiLoCo
To implement these, torchft provides some key reusable components:
Coordination primitives that can determine which workers are healthy via
heartbeating on a per-step basis
Fault tolerant ProcessGroup implementations that report errors sanely and be
reinitialized gracefully.
Checkpoint transports that can be used to do live recovery from a healthy
peer when doing scale up operations.
The following component diagram shows the high level components and how they
relate to each other:
torchft is designed to allow for fault tolerance when using training with replicated weights such as in DDP or HSDP (FSDP with DDP).
See the design doc for the most detailed explanation.
Lighthouse
torchft implements a lighthouse server that coordinates across the different
replica groups and then a per replica group manager and fault tolerance library
that can be used in a standard PyTorch training loop.
This allows for membership changes at the training step granularity which can
greatly improve efficiency by avoiding stopping the world training on errors.
Fault Tolerant HSDP Algorithm
torchft provides an implementation of a fault tolerant HSDP/DDP algorithm. The
following diagram shows the high level operations that need to happen in the
train loop to ensure everything stays consistent during a healing operation.
If you want all development dependencies you can install:
pip install torchft-nightly[dev]
Installing from Source
Prerequisites
Before proceeding, ensure you have the following installed:
Rust (with necessary dependencies)
protobuf-compiler and the corresponding development package for Protobuf.
PyTorch 2.7 RC+ or Nightly
Note that the Rust versions available in many conda environments may be outdated. To install the latest version of Rust, we recommend downloading it directly from the official website as shown in the below command:
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
To install the required packages on a Debian-based system (such as Ubuntu) using apt, run:
This uses pyo3+maturin to build the package, you'll need maturin installed.
If the installation command fails to invoke cargo update due to an inability to fetch the manifest, it may be caused by the proxy, proxySSLCert, and proxySSLKey settings in your .gitconfig file affecting the cargo command. To resolve this issue, try temporarily removing these fields from your .gitconfig before running the installation command.
To install in editable mode w/ the Rust extensions and development dependencies, you can use the normal pip install command:
pip install -e '.[dev]'
Usage
Lighthouse
The lighthouse is used for fault tolerance across replicated workers (DDP/FSDP)
when using synchronous training.
from torchft import Manager, DistributedDataParallel, Optimizer, ProcessGroupGloo
manager = Manager(
pg=ProcessGroupGloo(),
load_state_dict=...,
state_dict=...,
)
m = nn.Linear(2, 3)
m = DistributedDataParallel(manager, m)
optimizer = Optimizer(manager, optim.AdamW(m.parameters()))
for i in range(1000):
batch = torch.rand(2, 2, device=device)
optimizer.zero_grad()
out = m(batch)
loss = out.sum()
loss.backward()
optimizer.step()
torchx.py could also launch HSDP jobs when workers_per_replica is set > 1, if the training script supports it. For an example HSDP training implementation with torchFT enabled, see torchtitan.
Alternatively, to test on a node with two GPUs, you can launch two replica groups running train_ddp.py by:
On shell 1 (one replica groups starts initial training):
By observing the outputs from both shells, you should observe process group reconfiguration and live checkpoint recovery.
Example Parameter Server
torchft has a fault tolerant parameter server implementation built on it's
reconfigurable ProcessGroups. This does not require/use a Lighthouse server.