Skip to main content
Log in

Mojo reference

This section includes the Mojo API references:

  • Standard library: Common Mojo APIs.
  • Kernels library. Mojo APIs for writing high-performance computational kernels and custom operations.
  • MAX library. MAX Mojo APIs, including tensor APIs for custom operations, and legacy MAX APIs.
  • Decorators. Mojo decorators reference.

How to read the Mojo API docs

Mojo syntax is covered in detail in the Mojo manual. Here's a quick cheat-sheet on reading struct and function signatures.

Arguments

Function arguments appear in parentheses after the function name:

fn example_fn(pos: Int, /, pos_or_kw: Int, *, kw_only: Bool = False):
...
fn example_fn(pos: Int, /, pos_or_kw: Int, *, kw_only: Bool = False):
...

Here's a quick overview of some special syntax in the argument list:

You may also see argument names prefixed with one or two stars (*):

def myfunc2(*names, **attributes):
def myfunc2(*names, **attributes):

An argument may also be preceded by an argument convention, which indicates how the value is passed:

fn sort(mut names: List[String]):
fn sort(mut names: List[String]):

The most common conventions are:

  • read(default): the callee receives an immutable reference to the value.
  • mut: the callee receives a mutable reference to the value.
  • owned: the callee receives ownership of a value.

For details and a complete list of argument conventions, see Argument conventions.

Parameters

Mojo structs and functions can take parameters. Parameters are evaluated at compilation time, and act as constants at runtime. Parameter lists are enclosed in square brackets:

struct ExampleStruct[size: Int, //, thing: Thing[size]]:
struct ExampleStruct[size: Int, //, thing: Thing[size]]:

Parameters that occur before a double-slash (//) in the parameter list are infer-only parameters. You usually don't need to specify infer-only parameters; as the name suggests, they're usually inferred.

Like arguments, parameters can be positional-only, keyword-or-positional, or keyword-only, and they can be required or optional. The /, *, and = characters have the same meaning in parameter lists as they do in argument lists.

Standard library

The Mojo standard library provides nearly everything you'll need for writing Mojo programs, including basic data types like Int and SIMD, collection types like List, reusable algorithms and modules to support GPU programming.

Top-level packages:

algorithm

Implements the algorithm package.

base64

Implements the base64 package.

benchmark

Implements the benchmark package for runtime benchmarking.

bit

Implements the bit package.

buffer

Implements the buffer package.

builtin

Implements the builtin package.

collections

Implements the collections package.

compile

Provides utilities for compiling and inspecting Mojo code at runtime.

complex

Provides types and functions for working with complex numbers.

gpu

Provides low-level programming constructs for working with GPUs.

hashlib

Implements the hashlib package that provides various hash algorithms.

logger

Provides logging functionality with different severity levels.

math

Implements the math package.

memory

The memory package provides several pointer types, as well as utility functions for dealing with memory.

os

Provides access to operating-system dependent functionality.

pathlib

Implements the pathlib package.

pwd

Provides access to user and group information from the password database.

python

Implements the python package.

random

Implements the random package.

runtime

Implements the runtime package.

stat

Implements the stat package.

subprocess

Implements the subprocess package.

sys

Implements the sys package.

tempfile

Implements the tempfile package.

testing

Implements the testing package.

time

Implements the time package.

utils

Implements the utils package.

Kernels library

The Mojo kernels library provides APIs for writing computational kernels that run on CPU and GPU. You can use these APIs when developing MAX custom operations or standalone GPU kernels.

Top-level packages:

layout

Provides layout and layout tensor types that abstract memory layout for multidimensional data.

register

Provides APIs for registering MAX Graph operations.

MAX library

The Mojo MAX library provides APIs to interact with the MAX graph compiler and runtime.

Top-level packages:

driver

DEPRECATED: The max.driver Mojo API is being deprecated in favor of the open source gpu.host API, which has better ergonomics, and is more feature complete.

engine

DEPRECATED: The Mojo graph and engine APIs are being deprecated. Internally we build graphs using the Python APIs, and our engineering efforts have been focused on that. As a result, the Mojo version has not kept pace with new features and language improvements. These APIs will be open sourced for the community in a future patch prior to being removed.

graph

DEPRECATED: The Mojo graph and engine APIs are being deprecated. Internally we build graphs using the Python APIs, and our engineering efforts have been focused on that. As a result, the Mojo version has not kept pace with new features and language improvements. These APIs will be open sourced for the community in a future patch prior to being removed.

tensor

APIs to create and manage tensors in a graph.

Decorators

A Mojo decorator is a higher-order function that modifies or extends the behavior of a struct, a function, or some other code.

@__copy_capture

Captures register-passable typed values by copy.

@always_inline

Copies the body of a function directly into the body of the calling function.

@compiler.register

Registers a custom operation for use with the MAX Graph API.

@implicit

Marks a constructor as eligible for implicit conversion.

@nonmaterializable

Declares that a type should exist only in the parameter domain.

@parameter

Executes a function or if statement at compile time.

@register_passable

Declares that a type should be passed in machine registers.

@staticmethod

Declares a struct method as static.

@value

Generates boilerplate lifecycle methods for a struct.

Was this page helpful?