Discover more from Arkinfo Notes
Should you switch from Python to Mojo?
The answer is more complicated than you think...
Developed by Modular, a company focussed on making AI products, Mojo promises to be a replacement for Python in the domain of AI. Although still in its alpha phase of testing (not yet available publicly), Mojo has generated a lot of buzz in the tech space already with many engineers signing up for early trials of the language.
In this article, we will consider whether shifting from Python to Mojo may or may NOT be in your best interest as a software developer. Although the internet may be quick to give a verdict on this, the answer is more nuanced and depends on your own circumstances and requirements. Changing your tech stack can be a make or break decision for your product. So it is always advisable to consider all tradeoffs before making any shifts.
This article is not sponsored by Modular and is not aimed at publicising any language or framework. Its purely general purpose, aimed at helping software developers make better decisions.
With that said, let’s jump right into the topic.
What is Mojo?
In Modular’s own words:
Mojo combines the usability of Python with the performance of C, unlocking unparalleled programmability of AI hardware and extensibility of AI models.
In simpler terms, Mojo lets you code with the simplicity of Python while providing you with the runtime efficiency of C. It also lets you code bottom-up, i.e., code the high level Python related stuff while also coding the low-level hardware without having to use C++ or CUDA. You can code the entire AI system from tweaking the hardware code for optimal performance to designing the software using just one language.
Why you should use Mojo
Now that we understand what Mojo is, let us look at some of its starring features that make it attractive for developers.
Modular claims that Mojo can be as much as 35K times faster than Python at runtime. Python is an interpreted language, which means it runs the code line by line. This makes it easy to write and debug, but also slows down the execution speed. Mojo, on the other hand, is a compiled language, which means it converts the code into machine-readable instructions before running it. This makes it run much faster and more efficient than Python.
Python is a dynamically typed language, which means it does not check the types of variables and values until runtime. This can lead to unexpected errors and bugs that are hard to catch and fix. Mojo, on the other hand, is a statically typed language, which means it checks the types of variables and values at compile time. This makes it catch errors and bugs early and prevent them from happening at runtime.
Python is first and foremost, known for its syntactic simplicity. However, compared to Mojo, Python can be seen as a more verbose language, which means it requires a lot of code to express complex ideas and logic. This can make the code hard to read and maintain. Mojo, on the other hand, is a concise language, which means it requires less code to express the same ideas and logic. This makes the code easier to read and maintain.
Python is a single threaded programming language, which means that execution of a program occurs in a sequential manner, one instruction at a time in a single thread. This can be computationally expensive when it comes to running larger more complex programs. Unlike Python, Mojo is multithreaded which means multiple threads can be executed concurrently during runtime. This is a major boost to system efficiency especially for running large-scale AI models.
A superset of Python
Mojo is a superset of Python, which means you get to use all features and libraries of Python within Mojo. This is a great asset as it saves a lot of time in migrating massive codebases with minimal changes required. It also makes sure you get to use the new extended and advanced features of Mojo while not missing out on any of the features that made Python important for your tech stack.
Why you should NOT use Mojo
Having listed all the reasons to use Mojo, you might be tempted to think of Mojo as the perfect replacement for Python. But I wouldn’t be so quick to arrive at that conclusion just yet. Here are a couple of points to consider before you make the switch.
Mojo is both intended for and advertised as a replacement of Python in AI development. This means that its features are designed specifically for large-scale AI projects. Python on the other hand, is a general purpose programming language. You can use Python to develop a GUI using TKinter, a web app using Django, or a CLI using argparse. Can you develop all these things using Mojo? Sure, as Mojo is a superset of Python. But it makes no sense to migrate your entire codebase to Mojo for projects that do not require its expertise. Doing so is only useful if your project is a data heavy and complex AI model that requires high efficiency low latency computation to run in the backend.
Mojo brings in some great features for the development and deployment of AI models on the new age AI-first chips. But it is important to note that its potential is yet to be realised as it is still in the Alpha-test phase. It is necessary to let a programming language or framework mature before deploying them to real-time applications. While I highly recommend experimenting with Mojo for projects that are not currently in production, I would definitely recommend against deploying Mojo for projects that are already in production with active users. Not only could you run into an unforeseen brick-wall in your development but it might be very tricky to find help from the community with very limited documentation.
I know I listed ‘Multithreaded’ as a plus for Mojo. But in certain circumstances, being multithreaded can also be a problem. Multithreading adds a lot of power to your code, but it comes with its fair share of problems such as race conditions, deadlocks, thread synchronisation, context-switching, and resource bottlenecks.
These bugs can be resilient, hard to detect and replicate, adding a lot of developer overhead. Ask any serious Java developer and they will have stories to tell. So even if your project is AI-based, as long as computational complexity is not the major concern for you, my advise would be to stay away from making things unnecessarily complex and utilise the simplicity of Python.
Effort vs Reward
In every coding project, it’s always important to consider the effort vs reward ratio of any changes you make. In some projects, putting in 6 months of effort to increase the project efficiency by just 5% could be worth the time and resources. In other cases, it might prove detrimental for the project. All I can is, consider the requirements of your project carefully and assess whether a migration in your tech stack really adds enough value to make it worth the effort.
Thanks for reading Arkinfo Notes! Subscribe for free to receive new posts.