torchrunx 🔥
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
We can distribute and run this function (e.g. on 2 machines x 2 GPUs) using torchrunx!
import logging
import torchrunx
logging.basicConfig(level=logging.INFO)
launcher = torchrunx.Launcher(
hostnames = ["localhost", "second_machine"], # or IP addresses
workers_per_host = "gpu" # default, or just: 2
)
results = launcher.run(
distributed_training,
output_dir = "outputs",
num_steps = 10,
)
Once completed, you can retrieve the results and process them as you wish.
checkpoint_path: str = results.rank(0)
# or: results.index(hostname="localhost", local_rank=0)
# and continue your script
model = torch.load(checkpoint_path, weights_only=False)
model.eval()
See more examples where we fine-tune LLMs using:
Refer to our API, Features, and Usage for many more capabilities!
Metadata
Release files for torchrunx 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torchrunx-0.4.0.tar.gz | 41.6 kB | Without provenance | Details for torchrunx-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torchrunx-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for torchrunx-0.4.0-py3-none-any.whl |
Total release size: 75.8 kB
Release files / torchrunx-0.4.0.tar.gz
| Download URL | torchrunx-0.4.0.tar.gz |
|---|---|
| Size | 41.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
adcdbfd82ef633e549ba6e7694bc6eb258f55a6a64c8c59776794b922ae7b3ff
|
|
BLAKE2b-256 checksum How to use checksums |
cc80b760a13db3e41fc2645911c30717cef05f1b200367d98f979226802ab10e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.5
|
Release files / torchrunx-0.4.0-py3-none-any.whl
| Download URL | torchrunx-0.4.0-py3-none-any.whl |
|---|---|
| Size | 34.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a8aa42292941ddb7d68fe42a5eabe25800176a12250d417192bd7501b839e52f
|
|
BLAKE2b-256 checksum How to use checksums |
4381c03ed239f02e1612f6b60525602b8d7f22c22ef332ba47d55178b1b5694e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.5
|
Security for torchrunx 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.