IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/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. /max/get-started.md).

Python module

max.experimental.sharding

Distributed-tensor sharding: how a tensor is laid out across a device mesh.

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), scored by a single cost model, with one pluggable Solver making the choice at each dispatch. There is no whole-graph trace.

A mode(...) block selects the solver for the ops inside it:

import numpy as np
from max.driver import CPU
from max.dtype import DType
from max.experimental.functional import full, matmul, relu, transfer_to
from max.experimental.sharding import (
    DeviceMesh,
    GreedyReshard,
    PlacementMapping,
    Sharded,
    mode,
)

# 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 picker resolves the result back to replicated.
a = transfer_to(
    full([4, 8], 1.0, dtype=DType.float32, device=mesh.devices[0]),
    PlacementMapping(mesh, (Sharded(1),)),
)
b = transfer_to(
    full([8, 2], 1.0, dtype=DType.float32, device=mesh.devices[0]),
    PlacementMapping(mesh, (Sharded(0),)),
)

with mode(GreedyReshard(on_reshard="warn")):
    y = relu(matmul(a, b))

Shipped solvers: GreedyReshard (cheapest feasible action), NoReshard (passthrough only; errors on any reshard), and PartialsOnly (only Partial -> Replicated resolutions).

This module avoids the overloaded word “rank”. 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.

Device mesh

DeviceMeshAn N-dimensional logical grid of devices.
get_active_meshReturns the mesh from the current mesh_context(), or None.
mesh_contextPublishes mesh to spec-first NamedMapping constructions.

Placements

PartialEvery device holds a partial result that must be reduced.
PlacementAbstract base for all placement types.
ReduceOpReduction operations for partial placements.
ReplicatedEvery device on this mesh axis holds the same copy of the data.
ShardedEvery device on this mesh axis holds a slice along axis.
CollectiveThe collectives the cost model understands.

Distributed types

DistributedTensorTypeA symbolic type for a tensor distributed across a device mesh.
TensorLayoutMetadata snapshot of a distributed tensor for rule evaluation.

Per-op decisions

ActionA rule's picked decision for one op call.
ActionSetA rule's menu of per-axis sharding options for one op call.
AxisAssignmentOne per-mesh-axis row in an ActionSet.
PerShardA distinct value per mesh shard.

Pickers

GreedyReshardDefault per-op picker: enumerate → cheapest.
NoReshardPassthrough-only picker: returns the first zero-reshard action.
PartialsOnlyCost-model-free picker that only resolves Partial → Replicated.
ReshardBehavioralias of Literal['silent', 'warn', 'raise']
Solveralias of Callable[[ActionSet, Sequence[TensorLayout]], Action]

Exceptions

ShardingErrorRaised when a sharding constraint cannot be satisfied.

Functions

build_action_setWraps rule-emitted rows into a feasibility-filtered ActionSet.
force_replicated_action_setSingle-row (R,…,R) -> R ActionSet for ops that do not expose sharding.
modeBinds solver for the duration of a with block or function call.