Skip to main content

torchrunx 🔥

Python Version PyTorch Version PyPI - Version Documentation Tests GitHub License

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!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchrunx-0.3.3.tar.gz (428.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchrunx-0.3.3-py3-none-any.whl (45.9 kB view details)

Uploaded Python 3

File details

Details for the file torchrunx-0.3.3.tar.gz.

File metadata

  • Download URL: torchrunx-0.3.3.tar.gz
  • Upload date:
  • Size: 428.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.29

File hashes

Hashes for torchrunx-0.3.3.tar.gz
Algorithm Hash digest
SHA256 33df33bea7d8b71be11d7e75f76042b83db66f06c6a169fb1ed694b1a9144786
MD5 1870a1ccb1f5dc085d44cbf698399a76
BLAKE2b-256 faf53b1901e3646813c5ad9beba15686f5ada46a9c0a680b892010bb6a6a3a86

See more details on using hashes here.

File details

Details for the file torchrunx-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: torchrunx-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 45.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.29

File hashes

Hashes for torchrunx-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ebf342af585fa048f34e66b8a913f073219970fe21596a9ba6be3e7079b9cbb2
MD5 adc8ba23f5545961191f9668589bd462
BLAKE2b-256 fb98cd663f168ef160df0ce9defbf9383f3873a6d9caf155cbc6d2722db3668a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page