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).
Shard a model
To run your own model with tensor or data parallelism, you give each weight
and the batch a placement on the mesh. Each op's sharding rule then returns
the placements its result may take. On this page, you choose a placement for
each weight, see the placement each layer's result gets, and check the sharded
result against an unsharded run. To serve a supported model across
GPUs without writing this code, use the
parallelism settings for max serve.
Choose a placement for each weight
Tensor parallelism splits each weight of a linear layer across the devices on a
mesh axis. A weight split on its output features is column-parallel, and a
weight split on its input features is row-parallel. The placement of x @ w
comes from the placements of both operands, so each row below also names the
placement x needs. With any other placement on x, the matmul raises
ShardingError by
default. The table assumes a 2-D activation x and a weight w shaped
[in_features, out_features]:
| Weight split | Placement on the weight | Placement on x | Placement of the result |
|---|---|---|---|
| Output features across devices | Sharded(1) | Replicated() | Sharded(1), one column block per device |
| Input features across devices | Sharded(0) | Sharded(1) | Partial(), one term of the sum per device |
| No split, weights copied | Replicated() | Replicated(), Sharded(0), or Partial() | The placement of x |
Linear stores its weight transposed as
[out_features, in_features] and computes x @ weight.T + bias, so the
placements swap on
Linear.weight:
Sharded(0) splits the output features, and Sharded(1) splits the input
features and leaves x @ weight.T as Partial(). The bias add has no row for
a Partial() sum beside a replicated bias, so a row-parallel Linear with a
bias raises ShardingError by default. Build it with bias=False and add the
bias after the allreduce.
Follow a column-parallel layer with a row-parallel one
A column-parallel layer followed by a row-parallel one needs no collective
between them: the first layer's split output columns are the second layer's
split input features. The row-parallel layer leaves a Partial() sum. Each of
these calls runs an allreduce that adds its terms:
to() with a
replicated mapping,
materialize(),
and
to_numpy().
Place x and both weights on the mesh, and print the placement of each layer's
result:
from max.driver import CPU
from max.functional import relu
from max.tensor import Tensor
from max.tensor.sharding import DeviceMapping, DeviceMesh, Replicated, Sharded
mesh = DeviceMesh(devices=(CPU(), CPU()), mesh_shape=(2,), axis_names=("tp",))
x = Tensor.ones([4, 8], device=CPU()).to(DeviceMapping(mesh, (Replicated(),)))
w1 = Tensor.ones([8, 16], device=CPU()).to(DeviceMapping(mesh, (Sharded(1),)))
h = relu(x @ w1)
print("h:", h.placements)
w2 = Tensor.ones([16, 4], device=CPU()).to(DeviceMapping(mesh, (Sharded(0),)))
y = h @ w2
print("y:", y.placements)h: (Sharded(axis=1),)
y: (Partial(),)The printed placements come from each op's sharding rule.
relu()'s rule returns a row
for a Replicated() input and a row for each Sharded() axis. Each row gives
the result the input's placement, so h stays split by column.
The matmul against the row-parallel weight contracts the split axis, so y's
placement is Partial(): each device sums eight of the sixteen terms. to()
with a replicated mapping runs the allreduce that adds them.
relu()'s rule returns no row for a Partial() input, so relu(y) raises
ShardingError by default. Inside an
auto_reshard()
block with mode="silent" or mode="warn", MAX allreduces y first, and
relu() returns a Replicated() result. Apply the activation between the
column-parallel and row-parallel layers to keep every collective at the end.
y.local_shards
Partial()
[0](4, 4)[1](4, 4)Replicated()
[0](4, 4)[1](4, 4)y.to(DeviceMapping(mesh, (Replicated(),)))Split the batch for data parallelism
Tensor parallelism splits the weights. Data parallelism splits the batch and
copies the weights. Give each weight Replicated() and the batch tensor
Sharded(0). Each device then runs the same ops on its own rows.
Whether a reduction over the split batch axis moves data depends on its rule.
sum() returns Partial()
without moving data, and to() with a replicated mapping allreduces it.
mean(),
max(),
min(),
prod(),
argmax(), and
argmin() return no row for an
input split along the reduced axis, so they raise ShardingError by default.
Inside auto_reshard(mode="silent") or auto_reshard(mode="warn"), MAX
gathers the batch first, and the result is Replicated().
A model can combine data and tensor parallelism. A mesh with a data-parallel
axis (dp) and a tensor-parallel axis (tp) gives each weight one placement
per mesh axis. MAX picks one rule row per mesh axis, in mesh-axis order, and
each axis starts from the placements the axes before it chose. When the row on
any axis moves an operand, the op raises ShardingError by default, and
auto_reshard() sets which moves each axis allows.
Check a sharded result
A sharded model is correct when its result matches the same computation,
unsharded, on a single device. In the unsharded computation, each of the 16
values in a row of h is 8, so every cell of the product is 8 × 16 = 128.
Allreduce y into the replicated tensor full_y with to(), copy it to the
host with to_numpy(), and compare it with that value:
import numpy as np
full_y = y.to(DeviceMapping(mesh, (Replicated(),)))
expected = np.full((4, 4), 8 * 16, dtype=np.float32)
print(np.allclose(full_y.to_numpy(), expected))TrueCompare logits with a reference covers the end-to-end check once a whole model runs.
Next steps
- Steering resharding: what happens when an op meets a placement it cannot use.
- Pipelines: where a pipeline architecture builds its device mesh.
- Layers: how a layer takes its weight placements from the mesh in scope.
- Modules: how MAX walks a module to find the weights these placements attach to.