Skip to main content

spmd_types

A type system for distributed (SPMD) tensor computations in PyTorch. This package provides two type systems:

  • Local SPMD types, which allow you to use Megatron-style differentiable collectives in a safe way by tracking whether or not your backward gradients are pending reduction or not.

  • Global SPMD types, a DTensor-like abstraction for writing code that has the same semantics whether run on a single device or in a distributed fashion, but with explicit communication operations so you are never guessing when a redistribute occurs.

In both cases, the SPMD types makes it possible for you to check that your code computes correct gradients (local SPMD) or gives equivalent results across different parallelizations (global SPMD), without having to actually run a full E2E distributed training run to check for loss matching.

The goal of this package is to provide a flexible type system that can typecheck realistic training code. We have used local SPMD types to typecheck a realistic pretraining codebase, and global SPMD types is actively under construction!

Installation

pip install spmd_types

Quick start

import torch
import torch.distributed as dist
import spmd_types as spmd
import spmd_types.checker
from torch.distributed.device_mesh import init_device_mesh

# Set up a fake process group (no GPUs needed)
dist.init_process_group(backend="fake", rank=0, world_size=8)
mesh = init_device_mesh("cpu", (2, 4), mesh_dim_names=("dp", "tp"))
dp = mesh.get_group("dp")
tp = mesh.get_group("tp")

with spmd.set_current_mesh(mesh), spmd.checker.typecheck():
    x = torch.randn(4)
    spmd.assert_type(x, {dp: spmd.R, tp: spmd.P})       # R on dp, partial on tp
    y = spmd.all_reduce(x, tp, src=spmd.P, dst=spmd.R)  # sum across tp ranks
    spmd.assert_type(y, {dp: spmd.R, tp: spmd.R})       # now replicated everywhere
    z = torch.mul(y, y)                                 # type inference: R * R -> R
    spmd.assert_type(z, {dp: spmd.R, tp: spmd.R})

dist.destroy_process_group()

Documentation

See Local SPMD types for a hands-on guide on porting Megatron-derived training frameworks, including the Megatron-to-spmd_types function mapping table and advice on Invariant vs Replicate.

See Design for the full type system specification, including local vs global SPMD modes, collective signatures with diagrams, forward-backward pairs, expert mode, cross-mesh compatibility, and partition spec redistribute.

License

BSD 3-Clause License. See CONTRIBUTING.md for how to contribute.

Download files

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

Source Distribution

spmd_types-0.2.4.tar.gz (110.2 kB view details)

Uploaded Source

Built Distribution

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

spmd_types-0.2.4-py3-none-any.whl (123.7 kB view details)

Uploaded Python 3

File details

Details for the file spmd_types-0.2.4.tar.gz.

File metadata

  • Download URL: spmd_types-0.2.4.tar.gz
  • Upload date:
  • Size: 110.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for spmd_types-0.2.4.tar.gz
Algorithm Hash digest
SHA256 2541599fb94af5a1b77718ba0e3d5bf6b9465f47592f97ba529e6d6d2ad21ab4
MD5 abc5dc34be2e0858265d2e279f029164
BLAKE2b-256 aaf50860c8da35cca9989211c3350f408457b2b19f57681ef174f9fea2b279e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for spmd_types-0.2.4.tar.gz:

Publisher: publish_release.yml on meta-pytorch/spmd_types

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spmd_types-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: spmd_types-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 123.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for spmd_types-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6e1f518a7624a753a9500ccf11401595535f9894125ebb606d0bc2330cc6c9a8
MD5 4fffd0b44f2a3fee81c1f122e1bc114e
BLAKE2b-256 f366e54fbd78dc3f0037142a73f98e97f0ee3b4163297d863854e5d1e147fe65

See more details on using hashes here.

Provenance

The following attestation bundles were made for spmd_types-0.2.4-py3-none-any.whl:

Publisher: publish_release.yml on meta-pytorch/spmd_types

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.5

2 files

This release

0.2.4 This release

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.1

1 file

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page