Automatically initialize distributed PyTorch environments
Maintained. Last release 4 months ago, commits in 2 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install torchrunxtorchrunx 🔥
By Apoorv Khandelwal and Peter Curtin
The easiest way to run PyTorch on multiple GPUs or machines.
torchrunx is a functional utility for distributing PyTorch code across devices. This is a more convenient, robust, and featureful alternative to CLI-based launchers, like torchrun, accelerate launch, and deepspeed.
It enables complex workflows within a single script and has useful features even if only using 1 GPU.
pip install torchrunx
Requires: Linux. If using multiple machines: SSH & shared filesystem.
Example: simple training loop
Suppose we have some distributed training function (needs to run on every GPU):
def distributed_training(output_dir: str, num_steps: int = 10) -> str:
# returns path to model checkpoint
Click to expand (implementation)
from __future__ import annotations
import os
import torch
import torch.nn as nn
def distributed_training(output_dir: str, num_steps: int = 10) -> str | None:
rank = int(os.environ['RANK'])
local_rank = int(os.environ['LOCAL_RANK'])
model = nn.Linear(10, 10)
model.to(local_rank)
ddp_model = nn.parallel.DistributedDataParallel(model, device_ids=[local_rank])
optimizer = torch.optim.AdamW(ddp_model.parameters())
for step in range(num_steps):
optimizer.zero_grad()
inputs = torch.randn(5, 10).to(local_rank)
labels = torch.randn(5, 10).to(local_rank)
outputs = ddp_model(inputs)
torch.nn.functional.mse_loss(outputs, labels).backward()
optimizer.step()
if rank == 0:
os.makedirs(output_dir, exist_ok=True)
checkpoint_path = os.path.join(output_dir, "model.pt")
torch.save(model, checkpoint_path)
return checkpoint_path
return None