TNS
VOXPOP
Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
0%
At work, but not for production apps
0%
I don’t use WebAssembly but expect to when the technology matures
0%
I have no plans to use WebAssembly
0%
No plans and I get mad whenever I see the buzzword
0%
AI / C++ / Python

Mojo Combines Python’s Ease with Performance of C++, Rust

Mojo is a new programming language by Chris Lattner. It's a superset of Python but with the performance benefits of C++ or Rust.
Dec 4th, 2023 11:35am by
Featued image for: Mojo Combines Python’s Ease with Performance of C++, Rust
Image via Modular.

Developers have had to choose between Python’s ease of use or the high performance of C, C++ or Rust. A new language called Mojo aims to give developers a way to have both — Python’s ease of use plus the high performance.

“The way to look at it is, Mojo’s a member of the Python family, which takes all this cool language and compiler and other technology and brings Python several steps forward,” said Mojo creator and CEO of Modular Chris Lattner. “What we say is it supercharges Python, it gives Python programmers superpowers, and that allows somebody that knows Python to be able to learn new things and be able to express and conquer new ground without having to switch to C++ to do it.”

Lattner, who also wrote Swift, admits it’s an ambitious project that tries to solve some of the complexities around artificial intelligence. Mojo combines the usability of Python with the performance of C++ to unlock the programmability of AI hardware and the extensibility of AI models, according to its website. It’s drawn comparisons to Julia, which was also written for high performance.

“One of the other challenges that machine learning people have is that there’s a really big difference between the research phase of a project and then the production deployment phase,” he said. “In production deployment, people care a lot about efficiency, and a lot about if you have hundreds of 1000s, or millions or billions of users […] and so researchers always love Python, because it’s super hackable. And it’s dynamic, and it’s flexible, and so they love Python. But production people love C++.”

Solving the Complexity of AI Development

With Mojo, the Modular team wanted to solve some of the complexity of AI deployments, Lattner explained. If developers want to write for GPU — which is used when models are trained — they have to use Cuda or SYCL or “one of many other really weird systems,” he said. If developers want to then write for a CPU — which is used to serve AI models — then developers have to know C++. But if developers want people to use their tools, they have to build in the Python ecosystem, he noted.

“What we realized is that there’s actually three very different worlds all coming together and AI … it’s making progress, but it’s making progress despite this complexity,” he said. “Along the way, we decided to solve our problem, to be able to build this AI tech, we need to build a new programming language, and that’s where Mojo came from.”

Mojo’s documentation notes that the Modular AI team wanted “an innovative and scalable programming model that could target accelerators and other heterogeneous systems that are pervasive in the AI field.” The programming language needed powerful compile-time metaprogramming, integration of adaptive compilation techniques, caching throughout the compilation flow, and other features that are not supported by existing languages, it continued.

Modular, which was founded by Lattner and Google machine learning thought leader Tim Davis, used Mojo to build its inference engine, which includes a compiler and runtime system. The company released an internal initial build of Mojo last year. It publicly released it for browsers in May of this year, and then it released a Mojo SDK for Ubuntu Linux and MacOS, with plans to release an SDK for Windows in the future. There’s also an online playground and Visual Studio Code extension for Mojo, which means developers can use the code completion, code formatting and debugger functions with Mojo.

Already, Mojo has more than 100,000 users, he added. Although it is not currently open source, the plan is to make it so eventually.

Giving Python Programmers ‘Superpowers’

Mojo’s use case is for the AI space, but Lattner said that people are talking about how to use it in the backend with Django and potentially for game development. It’s still early days, he added, so it remains to be seen how developers will leverage Mojo.

Mojo is a superset of Python, he explained. It allows someone who knows Python to be able to “conquer new ground” without having to switch to C++.

“One of the other really important things about Mojo is that it works with all of the Python libraries that already exist,” he said. “That means you don’t have to rewrite everything [or] relearn everything.”

Mojo also borrows from C++ as well. For instance, it doesn’t force developers to use types, although when types are used, it improves performance, Lattner said. Another example: While the Python community is working on adding support for threads, Mojo already natively supports that in a “super-efficient” way, he added.

AI uses GPUs in training but when the model is served, that’s handled on CPUs, he explained. The team at Modular worked back from the full capabilities of hardware, rather than from a position of making Python a little bit better.

“What that means is we say, ‘What can hardware do?’ Hardware has lots of cores. So you need threads. It has lots of parallelism so you can execute many operations at a time,” he explained. “It needs to be directly accessed, like a C compiler does, through a compiler, instead of being an interpreter.”

Lattner has been working on compilers for decades, he added. Modular drew on that experience and “then built the world’s best compiler and technology stack that can actually use all these cool features and expose it in that usable way that the Python programmers like,” Lattner added.

Mojo is faster than Python as well, he claimed. While Modular says it’s 68,000 times faster, that number is a little “cherry-picked,” he acknowledged, adding that people on the Internet have reported that it’s at least 1000 times faster.

“Within a CPU, you get a lot of these accelerator features now. You get vectors, which gives you the ability to do more than one operation at a time, and Mojo directly exposes them so there’s a whole bunch of stuff that has been very common in performance-oriented worlds, but has never existed for a whole bunch of different reasons in the Python community,” he said.

Mojo also gives Python developers a chance to learn new features without having to go learn C++ or abandon Python, he added.

“C++, it’s a wonderful language in many ways, but it’s also old and it’s also full of sharp edges and it’s very challenging for folks,” Lattner said. “A major point of this is we can allow people to continue to grow within one ecosystem.”

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.