Mojo reference
This section includes the Mojo API references:
- Standard library: Common Mojo APIs.
- MAX AI Kernels library. Mojo APIs for writing high-performance computational kernels and custom operations for AI models.
- 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:
def 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:
-
Slash (
/): arguments declared before a slash are positional-only arguments. -
Star (
*): a star by itself in place of an argument indicates that the arguments after the star are keyword-only. -
An equals sign (
=) introduces a default value for an optional argument.
You may also see argument names prefixed with one or two stars (*):
def myfunc2(*names, **attributes) raises:-
An argument name prefixed by a single star character, like
*namesidentifies a variadic argument. -
An argument name prefixed with a double star, like
**attributesidentifies a variadic keyword-only argument.
An argument may also be preceded by an argument convention, which indicates how the value is passed:
def 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]]: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
4 items
ποΈ base64
1 item
ποΈ benchmark
5 items
ποΈ bit
1 item
ποΈ builtin
33 items
ποΈ collections
11 items
ποΈ compile
1 item
ποΈ complex
1 item
ποΈ documentation
1 item
ποΈ ffi
5 items
ποΈ format
4 items
ποΈ gpu
8 items
ποΈ hashlib
2 items
ποΈ io
4 items
ποΈ iter
9 items
ποΈ itertools
1 item
ποΈ logger
1 item
ποΈ math
5 items
ποΈ memory
9 items
ποΈ os
7 items
ποΈ pathlib
1 item
ποΈ prelude
Standard library prelude: fundamental types, traits, and operations auto-imported.
ποΈ pwd
1 item
ποΈ python
4 items
ποΈ random
2 items
ποΈ reflection
4 items
ποΈ runtime
2 items
ποΈ stat
1 item
ποΈ subprocess
1 item
ποΈ sys
8 items
ποΈ tempfile
1 item
ποΈ testing
3 items
ποΈ time
1 item
ποΈ utils
7 items
MAX AI kernels libraryβ
The MAX AI kernels library provides a collection of highly optimized, reusable compute kernels for high-performance numerical and AI workloads. These kernels serve as the foundational building blocks for writing MAX custom operations or standalone GPU kernels that are portable across CPUs and GPUs.
Top-level packages:
ποΈ comm
8 items
ποΈ extensibility
1 item
ποΈ kv_cache
2 items
ποΈ layout
15 items
ποΈ linalg
25 items
ποΈ nn
82 items
ποΈ nvml
1 item
ποΈ quantization
4 items
Decoratorsβ
A Mojo decorator is a higher-order function that modifies or extends the behavior of a struct, a function, or some other code.
ποΈ @align
Specifies a minimum alignment for a struct.
ποΈ @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.
ποΈ @__copy_capture
Captures register-passable typed values by copy.
ποΈ @deprecated
Mojo's `@deprecated` decorator marks outdated APIs and schedules them for removal. When used with the `use` parameter, it also provides migration suggestions.
ποΈ @explicit_destroy
Prevents automatic destruction by a `__del__()` method and requires explicit cleanup through named destructor methods.
ποΈ @export
Marks a function for export.
ποΈ @fieldwise_init
Generates fieldwise constructor for a struct.
ποΈ @implicit
Marks a constructor as eligible for implicit conversion.
ποΈ @no_inline
Prevents a function from being inlined.
ποΈ @parameter
Executes a function or if statement at compile time.
ποΈ @staticmethod
Declares a struct method as static.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!