Skip to content

Mojo and MAX

You’ve typed uv add max a few times now, for parallelize and for every GPU page. So what is MAX? It’s Modular’s platform for running AI models: serving them behind a web API, building model architectures in Python, and the GPU kernels that make them run. Mojo is the language; MAX is built on it (its kernels are written in Mojo), and its package also carries the Mojo libraries for GPUs and parallel code. This page is a short map, so you know what’s there and when you’d reach for it.

The max package bundles four things:

Part Language What it’s for
MAX accelerator library Mojo GPU programming (max.gpu), parallel CPU code (max.algorithm), tensors and layouts (layout), compute kernels
MAX Python library Python Building and running models, eager tensors, graphs
max command-line tool (shell) max serve, max generate, max benchmark and more
MAX C API C Running models from C programs

The first row is the part this tutorial used. Since Mojo 1.0, those libraries aren’t in the standard library any more; they come with max, which is why GPU code imports from max.gpu and layout.

There are several sizes of the package. The packages page has the full picture:

Install with uv You get
uv add max The core: the libraries above and the max command
uv add "max[serve]" Plus what max serve needs to run a model endpoint
uv add "max[benchmark]" Plus what max benchmark needs
uv add "max[all]" Everything, including the full mojo package

You can check which version you have:

Terminal window
uv run max --version

On our machine that printed MAX 26.7.0.

MAX’s main job is to run open AI models (Llama, Gemma and others from Hugging Face) on NVIDIA and AMD GPUs, and some of them on CPUs and Macs. max serve starts a local web server with an OpenAI-compatible API, so code written for OpenAI’s API can talk to your own model instead. From Modular’s quickstart, after uv add "max[all]":

Terminal window
export HF_TOKEN="hf_..."
max serve --model meta-llama/Llama-3.1-8B-Instruct

HF_TOKEN is your Hugging Face access token; the quickstart also asks you to accept the model’s license on Hugging Face first. The quickstart lists this model as needing more than 15 GiB of RAM and suggests it for Macs; most other models there need a GPU with a lot of memory. We didn’t run this on our test machine.

The Python side has an eager tensor API that feels a bit like NumPy or PyTorch. It’s in max.experimental, and Modular notes that its names can still change between releases. This example adds and multiplies two small tensors on the CPU:

from max.driver import CPU
from max.experimental.tensor import Tensor
a = Tensor([1.0, 2.0, 3.0], device=CPU())
b = Tensor([4.0, 5.0, 6.0], device=CPU())
print(a + b)
print(a * b)

We saved it as tensors.py and ran it with uv run python tensors.py. Output:

Tensor([5 7 9], dtype=DType.float32, device=Device(type=cpu,id=0))
Tensor([ 4 10 18], dtype=DType.float32, device=Device(type=cpu,id=0))

The first run also printed a note like compiled binary_AddOp_cpu_0_float32 on the fly in 25.7s and took about 40 seconds on our VM: MAX compiles each operation the first time it sees it. The second run took about a second. Without device=CPU(), MAX puts tensors on a GPU when there is one.

MAX runs models as graphs of operations. If an operation you need is missing or too slow, you can write it in Mojo as a custom op: a kernel like the ones in the GPU matrix multiply lesson, registered so a MAX graph can call it. Modular’s intro to custom ops also shows how to use Mojo kernels from PyTorch code, without moving your whole project to MAX. That’s the main bridge between “I can write a Mojo kernel” and “my model uses it”.

You want to… Do you need MAX?
Write normal Mojo programs on the CPU No, mojo is enough
Use parallelize across CPU cores Yes, uv add max (for max.algorithm)
Write GPU kernels in Mojo Yes, uv add max (for max.gpu and layout)
Run an open model behind an API, on your own hardware Yes, max[serve] or max[all]
Build or port a model architecture in Python Yes, the MAX Python library
Speed up one slow operation in a PyTorch model with Mojo Possibly, through MAX custom ops

If none of these apply, you don’t need it. The package is a bigger download than mojo alone, so leave it out of projects that don’t use it.

For each project, which install command fits best? (a) A command-line tool that counts words in files. (b) A program that adds two big arrays on an NVIDIA GPU. (c) A chat app that talks to a Llama model on your own server.

Hint: look at the “When you’d use MAX” table.

Show solution

(a) uv add mojo: plain Mojo, no MAX needed. (b) uv add max: GPU code needs max.gpu. (c) uv add "max[serve]" (or max[all]), so you can run max serve and talk to it through the OpenAI-compatible API.

With the eager API, make a tensor of prices [10.0, 20.0, 30.0] and one of discounts [1.0, 5.0, 3.0] on the CPU. Print the prices minus the discounts, and the prices times 2.

Hint: Python operators like - and * work on Tensors, also with a plain number.

Show solution
from max.driver import CPU
from max.experimental.tensor import Tensor
prices = Tensor([10.0, 20.0, 30.0], device=CPU())
discounts = Tensor([1.0, 5.0, 3.0], device=CPU())
print(prices - discounts)
print(prices * 2)

Output (from uv run python, on our machine):

Tensor([ 9 15 27], dtype=DType.float32, device=Device(type=cpu,id=0))
Tensor([20 40 60], dtype=DType.float32, device=Device(type=cpu,id=0))

The tensors hold float32 values even though the printout shows no decimals. Expect a pause on the first run while MAX compiles the subtraction.

  • MAX is Modular’s platform for running AI models: serving (max serve), building models in Python, and the kernels that run them.
  • The max package also holds the Mojo libraries for GPUs (max.gpu), parallel CPU code (max.algorithm) and tensors (layout). That’s why GPU and parallelize code needs uv add max.
  • Bigger variants add tools: max[serve], max[benchmark], max[all].
  • max serve gives an OpenAI-compatible endpoint for open models; most models need a GPU.
  • The Python eager API (max.experimental) works like simple tensor math and runs on CPU or GPU.
  • Custom ops let your own Mojo kernels run inside MAX graphs or PyTorch.

Next up: Mojo Calling C, using C libraries directly from Mojo.

Quick check

1. Which install do you need to write GPU kernels in Mojo 1.2?
2. What does max serve give you?
3. What is a MAX custom op?
4. You are writing a Mojo CLI tool that parses log files on the CPU. Do you need MAX?