IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python module

max.experimental.sharding

Defines how a tensor is laid out across a device mesh and how ops reshard it.

Describes, for every op, what redistribution to perform before the op runs. The pipeline is deliberately local: per-op rules over a placement vocabulary (Replicated, Sharded, Partial, Unknown), scored by a single cost model, with the cheapest allowed row picked on each mesh axis. A dispatch whose every row needs a transition the current policy disallows raises rather than picking one. There is no whole-graph trace.

An op redistributes its inputs whenever the plan it picks needs it, so a model can contain collectives it never wrote. Inside an auto_reshard() block, mode="warn" reports each one and "raise" refuses it, both naming the collective and the transfer_to that would replace it:

from max.driver import CPU
from max.experimental.functional import full, matmul, relu
from max.experimental.sharding import (
    DeviceMesh,
    DeviceMapping,
    Sharded,
    auto_reshard,
)

# A simulated two-device mesh (both slots are the same CPU).
mesh = DeviceMesh(
    devices=(CPU(), CPU()), mesh_shape=(2,), axis_names=("tp",)
)

# ``a`` is column-sharded, ``b`` is row-sharded: a @ b contracts the
# sharded dimension, so the product is a partial sum on every device.
a = full([4, 8], 1.0, device=DeviceMapping(mesh, (Sharded(1),)))
b = full([8, 2], 1.0, device=DeviceMapping(mesh, (Sharded(0),)))

# ``relu`` has no row for a partial sum, so its input moves to Replicated.
# The default transition set makes that an allreduce: the cheaper
# reduce-scatter into Sharded is not allowed outside an
# ``auto_reshard(ALL_TRANSITIONS)`` block. This block reports the move.
with auto_reshard(mode="warn"):
    y = relu(matmul(a, b))

This module avoids the overloaded word “rank” in prose. A device is one accelerator; a mesh axis is one named dimension of the DeviceMesh grid; a shard is one device’s piece of a tensor; a tensor axis is a dimension of the tensor itself. TensorLayout.rank is the number of tensor axes.

Device mesh​

DeviceMeshAn N-dimensional logical grid of devices.

Placements​

PartialEvery device holds a part of the value, and the value is their sum.
PlacementAbstract base for all placement types.
ReplicatedEvery device on this mesh axis holds the same copy of the data.
ShardedEvery device on this mesh axis holds a slice along axis.
UnknownPer-device values on this mesh axis with no known relation.

Layouts​

BufferLayoutA buffer a compiled callable may store through.
TensorLayoutA tensor's dtype and global shape, distributed across a device mesh.

Tensor-to-mesh mappings​

DeviceMappingHow a tensor is distributed across a device mesh.
NamedMappingBuilds a DeviceMapping from a JAX-style named spec.

Sharding rules​

A sharding rule receives the op’s arguments with each tensor replaced by its TensorLayout, and returns the AxisAssignment rows the op accepts: what each tensor operand must hold and what the op produces. These functions build common rules and rows.

AxisAssignmentOne placement choice that a sharding rule offers for its op.
match_operand_placementReturns a sharding rule whose results take operand name's placement.
replicated_rowsReturns the one row of an op that runs only on whole tensors.

Resharding​

auto_reshardSets how ops in this block may change the placement of their inputs.
ALL_TRANSITIONSEvery transition the picker can plan with.
DEFAULT_TRANSITIONSEvery transition except (Partial, Sharded), so a partial sum resolves to Replicated by allreduce; ALL_TRANSITIONS also allows reduce-scatter.
TransitionA placement change on one mesh axis, as (from, to) placement types.

Visualization​

visualizeDraws a mesh or a tensor's slices as blocks beside the tree they share.

Exceptions​

ConversionErrorRaised when a named spec cannot be represented on its mesh.
ShardingErrorRaised when a sharding rule cannot place a call.