Python Compatible Language with C Performance – by Chris Lattner

May 2, 2023 modular (one of the leading companies in the field of AI infrastructure) has announced the Mojo programming language, which promises full compatibility with Python, combined with ultra-high performance.

The company claims that automatic low-level parallelization, strong typing capabilities, and static compilation allow you to achieve speeds that exceed Python by 35,000 times from the earliest stages of development of the language!

So what kind of a miracle language is this, will it really replace Python – and where can you feel it?

Let’s figure it out!

Before reading, after reading or instead of reading

Since the language is only available in the authors’ JupyterHub sandbox strictly upon request (I sent it, of course, but I don’t know how long to wait), this article is just a brief analysis of the promised features, documentation, and code examples from the official site.

I am a student and this is my first article on Habré, so if anyone wants to skip the spreading of thought along the tree and immediately plunge into the documentation, please:

https://www.modular.com/get-started – request for access, we run here first of all

https://www.modular.com/mojo — landing page of the project, here are all the loudest promises and beautiful animations

https://docs.modular.com/mojo/ — documentation, surprisingly good for such a young language

https://youtu.be/-3Kf2ZZU-dg?t=1545 – presentation of the language (there after that Mojo shows the demo itself Jeremy Howard)

And who are the authors?

First of all, a suspicion arises: isn’t this a passing project on the wave of hype around AI?

Here, of course, time will put everything in its place, but the creators of the language are still what:

Chris Lattner (co-founder and director of Modular)

In the past, he was one of the key developers of the Swift language, the Clang compiler, as well as LLVM and MLIR technologies. Prior to founding Modular AI, he worked at Google, Tesla, Apple.

Tim Davis (co-founder and product leader)

He made a significant contribution to the development and scaling of Google Brain projects, as well as TensorFlow.

… and many other experienced developers.

So what are they promising us?

Python syntax with minimal changes

Even the innovations in Mojo, the developers tried to make them as similar to Python as possible:

Sample code on Mojo.  I would upload it through a code block, but Habr's syntax highlighting still does not recognize the language of three days ago.  In addition, all the code is in the documentation

Sample code on Mojo. I would upload it through a code block, but Habr’s syntax highlighting still does not recognize the language of three days ago. In addition, all the code is in the documentation

Accessing Python Libraries

Honestly, access is still far from being available to everyone, but the developers have managed to establish it for several of the most famous (Numpy, Matplotlob).

An example of accessing pyplot via module imports

An example of accessing pyplot via module imports

In general, it works like this: first, any Python module is imported, then it can be used in any way.

Example with NumPy

Example with NumPy

Unfortunately, now it is possible to import only the entire module (grabbing just a couple of functions will not work).

You can even import your own modules:

yes, python print will also have to be imported

yes, python print will also have to be imported

And other big words

Thanks to this flexibility in relation to Python, Mojo will be able to point-to-point embed into ready-made models, redefine operations with its help, use super-fast implementations of many algorithms, etc.

All the details of low-level improvements, parallelization, static compilation and other features that affect performance are described in detail in the documentation.

It is claimed, for example, that on the algorithm for calculating and constructing the Mandelbrot set, Mojo copes faster than Python by 35,000 (!) Times:

this algorithm, by the way, lies with them here

this algorithm, by the way, lies with them Here

A bit about syntax

Enough about the specifics, let’s get down to specifics and look at a few key features of the Mojo language code.

Here, only a brief description of a few major programming innovations in Python syntax will be given. All this, and even more, is described in detail in documentation.

let and var

Mojo allows you to declare scoped environment variables.

  • let is an immutable variable,

  • var – changeable.

This syntax also supports type specifiers and lazy initialization:

it is worth noting that "implicit" Python variables are also supported

it is worth noting that “implicit” variables from Python are also supported

struct as class replacement

struct in Mojo, like class in Python, it can contain methods, fields, decorators, operator overloading – but there is a significant difference.

  • Classes in Python dynamic – they allow the addition and substitution of methods and values ​​at runtime, while sacrificing performance.

  • Structures in Mojo static – they are formed at compile time, not allowing changes to be made at runtime. In addition to better security, this approach allows you to win in performance.

the simplest example of a structure

the simplest example of a structure

It is also important to note that the structure obliges to declare fields in it through let or var.

Strong typing

Mojo allows you to use Python’s flexible type system, but also provides a type system and a mechanism for controlling operations on them.

The easiest way to use this control system is to simply write code in structs (as mentioned, this binds variables to their types at compile time), which will prevent situations like this from happening:

example of an attempt to use an incompatible operator

example of an attempt to use an incompatible operator

By the way why intbut not int?

Good question! The point is that Python int “burdened” with many additional features, like working with large numbers or comparing values. Type int in Mojo, on the contrary, it is very simple and easy, which allows you to significantly increase the speed of working with it.

fn as strict version of def

Mojo has its own version of function syntax − fn. It is completely interchangeable with defhowever, imposes a number of restrictions:

  1. Argument values ​​are by default immutable in the function body, which prevents accidental changes and allows immutable types to be used as arguments.

  2. All arguments (except self) and the return value require a type.

  3. All local variables must be declared explicitly (wow, tautology).

Finally

Although this certainly interesting language is now in version 0.1, does not support many Python concepts and was announced a few days ago, it already shows itself to be quite serious. In this article, I described only the very tip of the iceberg, but more about aliases, passing by reference, new decorators and much, much more can be found in surprisingly detailed for such a young language documentation (yes, I threw this link five times already).

To finish on a funny note, I want to tell you that Mojo source code can be stored not only in files with the extension .mojobut also … with the extension .🔥 !

No, this is not a joke.

The authors wrote that they “consider the IT industry already mature enough to use Unicode characters in filename extensions.” I don’t know how it is with practicality, but the marketing move is certainly excellent.

Thanks for reading!

PS: as I wrote, this is my first article so if I did something wrong, please write in the comments, I will definitely correct it!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *