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).

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​

Was this page helpful?