Mojo – a new programming language for AI developers

Mojo combines the usability of Python with the performance of C, unlocking unparalleled programmability of AI hardware and extensibility of AI models.

def softmax(lst):
  norm = np.exp(lst - np.max(lst))
  return norm / norm.sum()
def softmax(lst):
  norm = np.exp(lst - np.max(lst))
  return norm / norm.sum()

struct NDArray:
  def max(self) -> NDArray:
    return self.pmap(SIMD.max)

struct SIMD[type: DType, width: Int]:
  def max(self, rhs: Self) -> Self:
    return (self >= rhs).select(self, rhs)

Usability & Programmability

Write Python or scale all the way down to the metal. Program the multitude of low-level AI hardware. No C++ or CUDA required.

Take a tour of Mojo

def sort(v: ArraySlice[Int]):
  for i in range(len(v)):
    for j in range(len(v) - i - 1):
      if v[j] > v[j + 1]:
        swap(v[j], v[j + 1])
struct MyPair:
  var first: Int
  var second: F32
  
  def __init__(self, first: Int, second: F32):
    self.first = first
    self.second = second
def reorder_and_process(owned x: HugeArray):
  sort(x)	# Update in place
  
  give_away(x^)	# Transfer ownership
  
  print(x[0])	# Error: ‘x’ moved away!
def exp[dt: DType, elts: Int]
    (x: SIMD[dt, elts]) -> SIMD[dt, elts]:
  x = clamp(x, -88.3762626647, 88.37626266)
  k = floor(x * INV_LN2 + 0.5)
  r = k * NEG_LN2 + x
  return ldexp(_exp_taylor(r), k)
def exp_buffer[dt: DType](data: ArraySlice[dt]):

  # Search for the best vector length
  alias vector_len = autotune(1, 4, 8, 16, 32)
  
  # Use it as the vectorization length
  vectorize[exp[dt, vector_len]](data)

Progressive Types

Leverage types for better performance and error checking.

Zero Cost Abstractions

Take control of storage by inline-allocating values into structures.

Ownership + borrow checker

Take advantage of memory safety without the rough edges.

Portable parametric algorithms

Leverage compile-time meta-programming to write hardware-agnostic algorithms and reduce boilerplate.

LANGUAGE INTEGRATED Auto-tuning

Automatically find the best values for your parameters to take advantage of target hardware.

The full power of MLIR

Parallel heterogenous runtime

Fast compile times

def sort(v: ArraySlice[Int]):
  for i in range(len(v)):
    for j in range(len(v) - i - 1):
      if v[j] > v[j + 1]:
        swap(v[j], v[j + 1])

Features include:

Progressive Types

Leverage types for better performance and error checking.

struct MyPair:
  var first: Int
  var second: F32
  
  def __init__(self, first: Int, second: F32):
    self.first = first
    self.second = second

Features include:

Zero Cost Abstractions

Take control of storage by inline-allocating values into structures.

def reorder_and_process(owned x: HugeArray):
  sort(x)	# Update in place
  
  give_away(x^)	# Transfer ownership
  
  print(x[0])	# Error: ‘x’ moved away!

Features include:

Ownership + borrow checker

Take advantage of memory safety without the rough edges.

def exp[dt: DType, elts: Int]
    (x: SIMD[dt, elts]) -> SIMD[dt, elts]:
  x = clamp(x, -88.3762626647, 88.37626266)
  k = floor(x * INV_LN2 + 0.5)
  r = k * NEG_LN2 + x
  return ldexp(_exp_taylor(r), k)

Features include:

Portable parametric algorithms

Leverage compile-time meta-programming to write hardware-agnostic algorithms and reduce boilerplate.

def exp_buffer[dt: DType](data: ArraySlice[dt]):

  # Search for the best vector length
  alias vector_len = autotune(1, 4, 8, 16, 32)
  
  # Use it as the vectorization length
  vectorize[exp[dt, vector_len]](data)

Features include:

LANGUAGE INTEGRATED Auto-tuning

Automatically find the best values for your parameters to take advantage of target hardware.

Utilize the full power of the hardware, including multiple cores, vector units, and exotic accelerator units, with the world’s most advanced compiler and heterogenous runtime. Achieve performance on par with C++ and CUDA without the complexity.

Parallelization

Mojo leverages MLIR, which enables Mojo developers to take advantage of vectors, threads, and AI hardware units.

PYTHON

Single-threaded execution

Mojo 🔥

Parallel processing across multiple cores

Instance

AWS r7iz.metal-16xl

Intel Xeon

Experience true interoperability with the Python ecosystem. Seamlessly intermix arbitrary libraries like Numpy and Matplotlib and your custom code with Mojo.

Read the programming manual

def make_plot(m: Matrix):
  plt = Python.import_module("matplotlib.pyplot")
  fig = plt.figure(1, [10, 10 * yn // xn], 64)
  ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], False, 1)
  plt.imshow(image)
  plt.show()

make_plot(compute_mandelbrot())

Easily extend your models with pre and post-processing operations, or replace operations with custom ones. Take advantage of kernel fusion, graph rewrites, shape functions, and more.

Model extensibility

Mojo can upgrade the existing operations in your model.

Input layer

Hidden layers

Output layer

Mojo is still a work in progress, but it’s available to try today in our JupyterHub-based Playground. Run through tutorials and write your own Mojo code.

Sign up for access

01.

EASY TO GET STARTED

We have plenty of easy-to-use Jupyter notebooks to help you get started learning Mojo 🔥.

02.

Unleash your mojo

Our docs will help you quickly discover why Mojo is such a powerful extension to Python, and the future of AI programming.

Read More

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.