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.nn
Module framework for models built on max.tensor.
Provides the Module base class, the module_dataclass()
decorator, and Module.compile(), which traces forward against
symbolic inputs and returns an ahead-of-time-compiled CompiledCallable. The
same forward body runs eagerly, traces to a Graph, or
compiles, depending on the active realization context.
Example:
from max.driver import CPU
from max.dtype import DType
from max.tensor import Tensor, default_device
from max.nn import Module, module_dataclass
from max.graph import TensorType
@module_dataclass
class MyLayer(Module):
weight: Tensor
bias: Tensor
def forward(self, x: Tensor) -> Tensor:
return x @ self.weight.T + self.bias
with default_device(CPU()):
model = MyLayer(weight=Tensor.zeros([10, 5]), bias=Tensor.zeros([10]))
y = model(Tensor.ones([3, 5]))
input_type = TensorType(DType.float32, ["batch", 5], device=model.device)
compiled = model.compile(input_type)
result = compiled(Tensor.ones([3, 5]))Submodules
max.nn.ep | Expert parallelism over a device mesh. |
|---|---|
max.nn.kernels | MAX kernels as Tensor ops. |
max.nn.kv_cache | Key-value cache types for MAX layers. |
max.nn.legacy | Neural network modules for MAX. |
max.nn.norm | Normalization layers for MAX neural networks. |
max.nn.rope | Positional embedding modules and functions. |
Base classes
Module | The core unit of composition for modeling in MAX. |
|---|---|
ModuleDict | A Module subclass that stores child modules by string key. |
ModuleList | A Module subclass which is locally a list container. |
PinnedDeviceTensor | Type alias for a Tensor parameter that Module.to will leave on its current device. |
Sequential | A Module that applies its children in sequence. |
TransparentModule | A Module that drops its own name from its descendants' paths. |
Linear layers
ColumnParallelLinear | A linear layer whose weight is sharded across its output features. |
|---|---|
Conv2d | A 2D convolution layer. |
Embedding | A vector embedding. |
Linear | A unary linear transformation over an input tensor. |
QKVLinear | A fused q/k/v projection with two checkpoint layouts. |
RowParallelLinear | A linear layer whose weight is sharded across its input features. |
col_parallel | Shards a linear layer's weight across its output features. |
|---|---|
row_parallel | Shards a linear layer's weight across its input features. |
Attention and rotary embeddings
AttentionWithRope | Implementation of attention that uses Rotary Position Embedding (RoPE). |
|---|---|
RotaryEmbedding | RotaryEmbedding layer to calculate and apply the frequency tensor for complex exponentials. |
YarnRotaryEmbedding | Generic YaRN (Yet another RoPE eNhancement) Rotary Position Embedding layer. |
LoRA
LoRA | Wraps a projection module with an additive multi-adapter LoRA term. |
|---|
lora_layers | Yields (qualified_name, layer) for every LoRA in model. |
|---|---|
lora_parameters | Yields (qualified_name, slot) for every adapter stack in model. |
Hyper-connection
HyperConnection | Mixes hc_mult parallel residual streams around a transformer sublayer. |
|---|
Functions
as_subgraph | Traces a function into a subgraph once and calls it from every call site. |
|---|---|
module_dataclass | Applies dataclass() to a Module subclass. |
Mesh axes
DCP | Mesh axis name for decode context parallelism. |
|---|---|
DP | Mesh axis name for data parallelism. |
EP | Mesh axis name for expert parallelism. |
TP | Mesh axis name for tensor parallelism. |
dcp_axis | Returns the index of the mesh axis a DCP group spans. |
|---|
Context parallelism
PCP_MAX_BATCH_CONTEXT | A step takes the PCP path only while every replica's page-aligned summed context is at most this many tokens. |
|---|---|
PCP_MIN_ROWS_PER_REQUEST | A step takes the PCP path when its rows average more than this per request. |
PCP_MIN_STEP_TOKENS | A step takes the PCP path only with at least this many rows. |
check_dcp_mesh | Raises unless DCP groups of dcp ranks tile the attention TP axis. |
|---|---|
combine_partial_attention | Merges each rank's partial attention and returns its own heads. |
dcp_group_heads | Returns the query heads each DCP group of mesh attends. |
dcp_rank_tensor | Returns each device's DCP rank as an int32 [1] device tensor. |
dcp_ranks | Returns each device's rank in its DCP group, for call_on_mesh. |
dcp_view | Returns mesh with its attention TP axis split into DCP groups. |
gather_columns | All-gathers each rank's [T, c] columns into [T, dcp * c]. |
merge_topk | Returns each rank's share of the group-wide top-k as one selection. |
split_selection | Splits a packed selection into its rows and [T] counts. |
to_dcp_mesh | Moves t between a mesh and its DCP view, moving no data. |
Mixture of experts
SumsOverDevices | A feed-forward block whose devices may each hold a partial result. |
|---|
moe_forward | Runs a feed-forward block on h and returns its output placed as h. |
|---|
Quantized tensors
FP8BlockActivation | FP8 block-quantized activation tensor. |
|---|---|
FP8BlockTensor | FP8 block-scaled quantized tensor. |
MXFP4Tensor | MXFP4 (OCP microscaling) block-scaled quantized tensor. |
MXFP8Activation | MXFP8-quantized activation tensor, rows sorted by expert. |
MXFP8Tensor | MXFP8 (OCP microscaling) block-scaled quantized tensor. |
NVFP4Activation | NVFP4-quantized activation tensor. |
NVFP4Tensor | NVFP4 (modelopt) block-scaled quantized tensor. |
QTensor | Base class for quantized tensor wrappers. |
QuantAwareTensor | A weight that a quantized layer accepts. |
KV cache inputs
PagedCacheValues | Distributed view of KV cache inputs. |
|---|---|
TensorLike | One device's shard of a cache field: a graph value, or a single-device tensor in the graph being built. |
cache_tensor | Returns one cache input's shards as a tensor, keeping real ones. |
|---|---|
kv_input_values | Returns a decoder's KV cache inputs for its params to unflatten. |
per_device_caches | Returns a decoder's KV cache inputs as one record per device. |