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).
Parallelism overview
Use these pages to run your own model code across several devices on one machine. You place each tensor on a grid of devices, and MAX runs each op on every device's part of its inputs. After reading them, you can split a model's weights for tensor or data parallelism, check a sharded result against an unsharded one, and decide when MAX may move data between devices.
To serve a supported model across GPUs, use the
parallelism settings for max serve,
which split the model for you.
Meshes, placements, and mappings
Each object below describes one part of how a tensor spreads across devices, and each builds on the one before it:
- A device mesh
(
DeviceMesh) names the devices and arranges them in a grid, with a name for each axis of the grid. - A placement says how the devices along one mesh axis share the tensor.
You choose one of these placements:
Replicated(): every device holds a full copy.Sharded(axis): one tensor axis is split across the devices.Partial(): each device holds a part, and the parts sum to the tensor.
- A device mapping
(
DeviceMapping) pairs a mesh with one placement per mesh axis. - A tensor layout
(
TensorLayout) pairs a dtype and a global shape, the shape of the whole tensor, with a device mapping. It holds no element data. A tensor'slayoutproperty returns it.
A mapping can use a different placement on each mesh axis, such as copying a tensor along one axis and splitting it along the other.
Split a tensor across a mesh
To give every device a 4 x 4 block of an 8 x 4 tensor, split the tensor
along one mesh axis and copy it along the other. Build the mesh, place the
tensor on it, print each shard's shape, and draw the layout:
from max.driver import CPU
from max.functional import full
from max.tensor.sharding import (
DeviceMapping,
DeviceMesh,
Replicated,
Sharded,
visualize,
)
mesh = DeviceMesh(
devices=tuple(CPU() for _ in range(4)),
mesh_shape=(2, 2),
axis_names=("dp", "tp"),
)
x = full([8, 4], 1.0, device=DeviceMapping(mesh, (Replicated(), Sharded(0))))
for shard in x.local_shards:
print(tuple(int(d) for d in shard.shape))
visualize(x, title="x")(4, 4)
(4, 4)
(4, 4)
(4, 4)
x: DType.float32 [8, 4] with DeviceMapping(DeviceMesh(dp=2, tp=2), [Replicated(), Sharded(axis=0)])
┌─────────────┐ tensor[:, :]
│ devices 0,2 │ ├── dp=0 - tensor[:, :]
│ [:4, :] │ │ ├── tp=0 device 0 - tensor[:4, :]
├─────────────┤ │ └── tp=1 device 1 - tensor[4:, :]
│ devices 1,3 │ └── dp=1 - tensor[:, :]
│ [4:, :] │ ├── tp=0 device 2 - tensor[:4, :]
└─────────────┘ └── tp=1 device 3 - tensor[4:, :]The header line lists the placements in mesh-axis order, so the entry for dp
comes first and the entry for tp second. Sharded(0) names a tensor axis: the
rows of the tensor split across the devices on tp, and each dp group keeps a
full copy.
Each panel shows one device's rows of x. A dimmed row sits on another device. Devices 0 and 2 hold the same rows, as do devices 1 and 3.
device 0dp=0 · tp=0 · (4, 4)device 1dp=0 · tp=1 · (4, 4)device 2dp=1 · tp=0 · (4, 4)device 3dp=1 · tp=1 · (4, 4)DeviceMapping(DeviceMesh(dp=2, tp=2), [Replicated(), Sharded(axis=0)])local_shards
returns each device's value as an unsharded tensor that wraps that device's
buffer, and
visualize() draws
which value every device holds.
Move data between devices
When an op runs, MAX places its results for you. If the op can run with its
operands where they are, it runs. If an operand would first have to move to
another placement, MAX raises
ShardingError by
default and moves nothing.
To let MAX move the data, run the op inside an
auto_reshard()
block with mode="warn" or mode="silent". The block's arguments also set
which moves are allowed.
Steering resharding covers those settings.
An op's sharding rule returns the placement combinations the op may run with, and MAX picks one of them on each mesh axis. Dispatch and rules covers how MAX picks.
Next steps
Read these guides in order:
- Distributed tensors: the global shape, the per-device shards, and what each placement records.
- Shard a model: placing weights for data and tensor parallelism, and checking the result.
- Steering resharding: the
auto_reshard()settings that decide whether an op may move its inputs. - Dispatch and rules: the op wrapper, the rows a rule returns, and how to write a rule of your own.