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).
Intro to custom ops
Custom operations (custom ops) run a Mojo kernel from a MAX model. Write one when the library lacks an operation, or a fusion the compiler does not find, and the built-in layers cannot express it.
In a ModuleV3 model, call the op from a layer's forward() with
F.custom()
and compose that layer like any other; the
custom layer guide shows the wrapper. If
you construct a graph directly with max.graph, call
ops.custom() instead.
What a custom op isβ
A custom op is a Mojo function that computes a result, called from the graph by name. The compiler schedules it and places it on a device like any other op. An elementwise op can fuse with the ops around it; any other op runs as its own step.
Custom ops in PyTorchβ
CustomOpLibrary
in max.experimental.torch compiles a Mojo
kernel and passes PyTorch tensors to it, so a PyTorch model can call the kernel
without moving to MAX. The
Extending PyTorch with custom operations in Mojo
example has a complete kernel.
Examples and referenceβ
The
custom ops examples
in the repository cover several kernels, and
GPU programming with Mojo covers the Mojo side. The
custom ops API and
F.custom() document the
Python calls.
Next stepsβ
- Build custom ops for GPUs: a complete Mojo kernel and its Python wrapper.
- Optimize a matmul custom op: a kernel taken from a naive implementation to a tuned one.
- Write a custom layer: wrapping an operation in a layer your model can compose.