Unleashing the Power of AI with Mojo: A New Programming Language

Bridging the Gap between Research and Production with High Usability and Performance

Marco Sanguineti
6 min readJun 9, 2023
Image by Modular

Mojo is a novel programming language that seeks to blend the simplicity of Python with the performance of C, presenting a platform that caters to AI developers with varying needs. It is designed to enhance the programmability of AI hardware and the extensibility of AI models, thereby bridging the gap between research and production​.

Usability and Programmability

Mojo provides the flexibility to write code that scales from the simplicity of Python to the performance-oriented low-level programming required for AI hardware, eliminating the need for C++ or CUDA. This makes it possible to write everything in one language, increasing productivity and reducing the complexity associated with managing multiple languages.

Why is Python programming “slow”?

Python is often considered “slow” in comparison to other languages like C, C++, or Java. This is due to several reasons:

  1. Interpreted Language: Python is an interpreted language, which means that it is not converted to machine code before execution. Instead, each line of code is interpreted and executed one at a time during runtime. This process is slower than executing pre-compiled machine code.
  2. Dynamic Typing: Python is dynamically typed, which means that the type of a variable is checked during runtime. This adds to the processing time, making Python slower than statically typed languages where the type of a variable is known at compile time.
  3. Global Interpreter Lock (GIL): Python uses a Global Interpreter Lock (GIL) which allows only one thread to execute at a time in a single process. This means Python doesn’t allow for true multi-core concurrency, which can slow down execution speed, especially for CPU-bound programs.
  4. High-Level Language: Python is a high-level language, which means it includes many abstractions to make coding easier. These abstractions, while useful for developers, can lead to slower execution times.
  5. Garbage Collection: Python uses garbage collection for memory management, which can cause additional overhead and slow down execution.

It’s important to note that while Python may be slower in terms of raw execution speed, it is often faster in terms of development time due to its simplicity and readability. Furthermore, for many applications, the difference in execution speed is not noticeable or relevant. When speed is critical, Python programmers often use extensions written in C or C++, or use Python in combination with optimized libraries like NumPy and SciPy, which perform computationally heavy tasks in compiled C code.

Performance

Mojo unlocks Python performance by leveraging the full power of the hardware, including multiple cores, vector units, and exotic accelerator units. It achieves this through its advanced compiler and heterogeneous runtime, thereby achieving performance on par with C++ and CUDA but without complexity.

Interoperability

Mojo offers seamless interoperability with the Python ecosystem, allowing developers to intermix libraries like Numpy and Matplotlib with custom Mojo code. This interoperability aspect ensures that developers can leverage the rich set of resources available within the Python ecosystem.

Extensibility

Mojo allows developers to easily extend their models with pre and post-processing operations or replace operations with custom ones. This extends to the ability to leverage kernel fusion, graph rewrites, shape functions, and more, providing more ways to optimize and customize AI models.

Progress and Community Involvement

Currently, Mojo is a work in progress and is available to try in a JupyterHub-based Playground. Developers can run tutorials and write their own Mojo code in this environment. The Mojo team plans to open-source the language progressively over time and has already set up a GitHub repository to gather issues and feedback from users​.

A Peek into Mojo Syntax

Mojo syntax is quite similar to Python, with some differences aimed at enhancing performance and type safety. For example, Mojo uses let and var declarations for value assignments. While let is used to declare immutable values, var is used for mutable ones. These declarations support type specifiers as well as patterns and late initialization, providing more control to the developers. However, the use of let and var is completely optional when declaring function scopes; developers can still use implicitly declared values as with Python.

Structures, or structs, in Mojo are similar to Python classes, supporting methods, fields, operator overloading, and decorators for metaprogramming. However, unlike Python classes, Mojo structs are static and bound at compile-time, which means methods cannot be added at runtime. The static nature of structs allows for better performance and safety, while still being easy to use. Moreover, all standard types in Mojo, such as Int, Bool, String, and Tuple, are created using structs, providing more flexibility and control to the developers.

Photo by Ilya Pavlov on Unsplash

An interesting difference to note is the use of Int instead of int. While Python's int type can handle large numbers and has extra features, it also carries extra baggage that can slow down the execution. On the other hand, Mojo's Int is designed to be simple, fast, and optimized for hardware performance.

This video is an interesting interview with Chris Lattner, the creator of Swift and LLVM, discussing a new language he’s working on called Mojo. Here’s a comprehensive summary of the video:

  1. Mojo: Mojo is a new programming language that Chris Lattner is working on. It’s designed to be a superset of Python, meaning it’s fully compatible with Python but also includes additional features. Mojo is statically typed, which can help catch errors at compile time rather than runtime. However, it also supports dynamic typing for flexibility, similar to Python.
  2. Inspiration: Mojo is inspired by the work of Jeremy Howard, a prominent figure in the machine learning community. Jeremy’s work on fast.ai and his approach to machine learning, which includes progressive image sizing and a deep understanding of the underlying algorithms, influenced the development of Mojo.
  3. Functionality: Mojo supports function overloading, which allows multiple functions with the same name but different parameters. This feature is not available in Python. It also introduces a new way to define functions using fn for stricter control, in addition to Python's def.
  4. Error Handling: In Mojo, exceptions are called errors. The language implements a different approach to error handling compared to traditional languages like C++. Instead of using zero-cost exceptions, Mojo treats throwing an error as fast as returning a result, which can lead to more predictable and efficient code.
  5. Packaging: The Mojo team is aware of the challenges with Python packaging, especially when dealing with hybrid packages that include both Python and C code. They hope that Mojo can help alleviate some of these issues by reducing the amount of C in the ecosystem.
  6. Future Plans: The Mojo team plans to work with the community to reevaluate packaging and potentially bring other innovations to the table. They also aim to make Mojo scalable, from small embedded systems to GPUs and other accelerators.

Mojo is designed to be a superset of Python, meaning that it includes all of Python’s features and adds new ones. It is designed to be easy to learn and use, and to be efficient and powerful for AI and machine learning tasks. Lattner believes that the future of programming lies in reducing complexity and making tools and technologies easier to use. He sees AI as a new programming paradigm that should be integrated into programming languages and normal toolchains. He also discussed the impact of large language models (LLMs) on programming. He sees LLMs as a tool that can help automate a lot of the mechanical aspects of programming, making programmers more productive. However, he doesn’t believe that LLMs will replace human programmers. Lattner is optimistic about the future of AI and believes that it will unlock many new possibilities. However, he also acknowledges the potential challenges and risks, such as job displacement and the concentration of AI capabilities in a few large companies. He advises young people interested in programming to work on something they’re excited about and to learn by building things. He also encourages them to be curious and not be afraid to go against the herd.

Conclusion

Mojo represents an exciting development in the field of AI programming languages, blending Python’s ease of use with C’s high performance. This balance makes it a versatile tool for AI developers, providing the power and flexibility to effectively bridge the gap between research and production. With ongoing development and community involvement, Mojo is expected to become a powerful asset in the AI development landscape.

--

--

Marco Sanguineti

Graduated in Mechanical Engineering, I work in the world of AI, Deep Learning and Software Development. Passionate about Technology, Videogames and AI.