The world of technology is always moving. The newest kid in the block is Mojo programming language. It takes the mantle to improve execution speeds for Python-based projects, giving developers speeds similar to C.

Python is one of the top programming languages. It is a versatile easy-to-learn programming language that offers new learners a gateway to programming/computer science.

Moreover, it is an excellent programming language for the hands of competent developers who can use it to create complex apps. However, one of the most significant drawbacks of Python is its execution speed. And that’s where Mojo comes in.

This article covers Mojo and how it relates to the Python ecosystem. Let’s get started.

What is Mojo?

YouTube video

Mojo is a high-level modern programming language. It offers an intuitive design to help developers create applications fast. In addition, it aims to bridge the gap between production and research by allowing users to use metaprogramming features and systems programming with Python’s syntax and ecosystem.

It borrows heavily from Rust and provides fast execution speeds to the Python ecosystem. Technically, Mojo is a superset of Python, which gives you access. 

The team behind Mojo is from Modular, an AI infrastructure company. And that also means it is a programming language for AI developers. Alongside the Mojo programming language, they also introduced Interference Engine, which lets developers improve their workflow, scale AI products, and reduce inference latency (more on it later).

According to the CEO of Modular, Chris Lattner, Mojo is 35,000 times faster than Python. He is also behind the swift programing language development. The speed gains are achieved because of how Mojo uses the LLVM compiler toolchain and MILR ( Multi-level Intermediate Representation Overview) compiler infrastructure. 

The goals of Mojo programming languages include:

  • Work with complete compatibility with the Python ecosystem.
  • Enable developers to deploy code subsets to accelerators.
  • Low-level control to ensure predictable performance.
  • Ensure no ecosystem fragmentation.

To try Mojo, you’ll need to use it through their cloud-based hosted environment, the Mojo Playground. It’ll ask you to log in, and you can get a working environment!

online-mojo-playground

Why Do We Need Mojo?

The core idea behind Mojo is to unify ML/AI infrastructure by providing a programming language that works across the stack. Furthermore, it ensures ease of use by removing the need to write MLIR code. 

According to Modular, Mojo will offer a scalable and innovative programming model. By doing so, the AI field users will find it easy to work with accelerators and heterogeneous systems.

Technically, it makes Mojo a programming language that supports compile-time metaprogramming. It also supports other features, such as caching during compilation flow, adaptive compilation techniques, etc. These features are not present in other programming languages.

If you want to read more about Mojo’s philosophy, check out Modular Docs – Why Mojo🔥

Features of Mojo Programming Language

In this section, we’ll look at key features of the Mojo programming language.

#1. Complete Compatibility with Python

Mojo aims to work with Python’s ecosystem and not against it. This is evident from the fact that Mojo uses the same functions, libraries, and features that Python offers. So, you can use any Python library you want within Mojo.

To import, you’ll need to use the following code:

from PythonInterface import Python

Once done, you can use Python.import_module() to import any Python library.

For example, to import numpy, you’ll need to use the following line of code.

let np = Python.import_module(“numpy”)

In Python, you’ll need to do “import numpy as np”

As it is imported, you can now use it to create arrays, do computations, etc.

array = np.array([1, 2, 3])

print(array)

Similarly, you can import matplotlib.pyplot o create a plot in Mojo.

This is how it looks when I run the code in Mojo Playground.

running-code-Mojo-playground

If you want to try out the code, copy-paste it from below.

from PythonInterface import Python

let np = Python.import_module("numpy")

array = np.array([1, 2, 3, 4, 5])

print(array)

#2. MILR

MILR stands for Multi-Level Intermediate Representation. Mojo supports MILR. This, in return, enables developers to take off a full array of new advanced features. These features include AI hardware units, threads, and vectors.

MILR improves performance due to concurrency, making Mojo faster than Python. In addition, it allows developers to take advantage of multiple cores.

#3. Ownership and Borrower Checker

Python’s memory management is safe. It uses a garbage collector, so programmers must ensure the code won’t run into race conditions.

Mojo, similar to Rust, implements a strict ownership and borrower checker model. Currently, it is partially implemented. The idea behind using the model is to improve concurrency and provide excellent memory management.

The ownership model also ensures a thread-safe approach, which is ideal for providing excellent concurrency support. This way, programs don’t run into race conditions. Also, the borrower checker ensures that variables are always checked during run-time. 

#4. Zero Cost of Abstraction

Mojo offers zero-cost abstractions, allowing developers to take complete control of storage. Here, programmers can do inline allocation values to structures.

#5. Auto-tuning

Mojo also offers to auto-tune. This ensures automatic allocation of the best values is set for the parameters, depending on the target hardware. 

Auto-tuning removes the need to manually optimize your code according to the target hardware.

Speed: How Fast is Mojo Language?

Python is a high-level programming language that aims for ease of use and maintainability. Unfortunately, this makes it slow compared to other solutions or programming languages. 

In Modular’s test, they found Mojo to be 35000x. They used the Mandelbrot algorithm and ran it on an AWS instance with an Intel Xeon processor powering it. They tested PYPY,  SCALAR C++, and MOJO, along with Python. The results were super fast, and you can see the results below.

speed-mojo-programming-language
Source: modular.com

To read more about Mojo speed, check out this post on the Julialang community.

Modular Inference Engine – Run AI Models Cheaply

Modular is also developing a Modular Interference Engine that makes it cheaper to run AI models in production. Mojo supports Modular Interfence Engine by default. It enables teams to simplify their workflow. It also lets developers reduce inference latency, making it easy to scale AI products.

Also, developers don’t have to change their model to use the Engine. Once loaded, it can supercharge PyTorch and TensorFlow models, capable of running a high performance with wide hardware support.

Will it Replace Python?

Mojo is new. It looks promising. So, it’ll take time to reach its target audiences, such as data scientists or programming languages. And, yes, it solves particular problems for AI enthusiasts and learners. However, plenty of similar solutions are out there that improve Python language speed. For example, you’ll find Jax, Codon, and Julia — a data science-oriented language.

So, two things can happen. First, it grows exponentially regarding features, and the community adopts it. Another outcome is that it becomes an ad-hoc programming language that utilizes Python libraries and the Modular Interference Engine.

So, will Mojo replace Python? Only time can tell.

Next, check out useful Python one-liners to simplify common tasks.