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.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.legacyNeural network modules for MAX.
max.nn.normNormalization layers for MAX neural networks.
max.nn.ropePositional embedding modules and functions.

Base classes​

ModuleThe core unit of composition for modeling in MAX.
ModuleListA Module subclass which is locally a list container.
PinnedDeviceTensorType alias for a Tensor parameter that Module.to will leave on its current device.
SequentialA Module subclass which holds a sequence of unary modules.
TransparentModuleA Module that drops its own name from its descendants' paths.

Linear layers​

Conv2dA 2D convolution layer.
EmbeddingA vector embedding.
LinearA unary linear transformation over an input tensor.

Attention and rotary embeddings​

AttentionWithRopeImplementation of attention that uses Rotary Position Embedding (RoPE).
RotaryEmbeddingRotaryEmbedding layer to calculate and apply the frequency tensor for complex exponentials.
YarnRotaryEmbeddingGeneric YaRN (Yet another RoPE eNhancement) Rotary Position Embedding layer.

LoRA​

LoRAWraps a projection module with an additive multi-adapter LoRA term.
lora_layersYields (qualified_name, layer) for every LoRA in model.
lora_parametersYields (qualified_name, slot) for every adapter stack in model.

Hyper-connection​

HyperConnectionMixes hc_mult parallel residual streams around a transformer sublayer.

Functions​

as_subgraphTraces a function into a subgraph once and calls it from every call site.
module_dataclassApplies dataclass() to a Module subclass.

Mesh axes​

DPMesh axis name for data parallelism.
EPMesh axis name for expert parallelism.
TPMesh axis name for tensor parallelism.