An introduction to Mojo (for Python developers)
Skills:
ML Pipelines70%
Key Takeaways
This video introduces Mojo for Python developers
Full Transcript
hey there so here is an unconventional introduction to moo and the reason I call it unconventional is because I don't want to start with a Hello World application to show you the first steps your first steps in the modu language instead I'm going to start with a working python script and show you how easy it is to turn it into modu and how fast we can run that script after we run it using modu here's the thing Mojo will be a super set of python and I say will be because Mojo is not ready yet we still working on it but when Mojo is done uh and you can use it to run any python script right now we have to make small modifications and that is the goal of today's video I want to get you excited with Mojo I want you to see what a fast python looks like and over the next few weeks I'm going to be publishing a series videos uh showing you uh the different aspects of modu as I learn the language I want you to follow along and see how cool it is so let's get started with uh with this script here before we look at the code I want you to go and search for the get started with Mojo guide you're going to find this online and here it's there is a section called requirements where you're going to see how to install mojo on your Mac computer your Linux computer or your Windows computer you're going to need to WSL for Windows and you're going to need to have an apple silicon Mac computer if you want to run modu and obviously uh here you're going to be able to run your first hello world Modo application there is an entire guide about the language Basics so you can start right here after you do this then I want you to open the script that I'm sharing should be somewhere down the description of this video below there is a GitHub repo I'm going to start adding all of my code related to learning moo into that repo so you can follow along and you're going to start with the hello world section and you're going to see there is folder hello world and there is a search. py inside that is the script that I want to show you um I want to translate into moo to so you can see the differences in performance uh like I mentioned before Mojo will be a super set so in a future we should be able to get this script and run it in Mojo with no modifications whatsoever right now that is not possible okay so right now we're going to have to translate the script uh not a big deal but you're going to have to do that right now just because the language is not ready yet so here's what the script does very simple script contains a binary search function that is going to take a sorted array and an element and it's going to find the position of that element in that array that's it it's a very very straightforward binary search implementation okay that's the function then there is a second function that's called main which is going to set up the whole experiment that we're trying to do here I start with declaring this constant and it's going to be 1 million and then I I initialize an array it's an empty array and I start adding numbers to that array so the array is going to come out sorted on the other side again nothing fancy I just want to create something that's very very simple I initialize uh results as Another Empty array I'm going to use this variable to accumulate the results of the binary search function and then I'm going to time this portion here okay this code is the one that I want to time to see how long it takes to execute this functionality this is what I'm timing I'm going to go 1 million times and I'm going to call the binary search function consecutively 1 million times and I'm just going to accumulate those values in the result array execute in this is what matters here that's what I want to measure in Python and then when we P this code over moo I want to measure that on modu okay I'm using the time function here to capture the time and then I'm using it again here to capture the time after we execute the function and obviously I compute how much time has passed and I multiply by a th because the result of time is in seconds and I want to return Mill milliseconds that's what I want to print that's it at the end of the program the only thing that I do is I print how many results I added I should get a million just to make sure everything is working fine and then I call the main function that's my code so let's now execute this code going to use Python 3 going to call the search function and it takes 14 14 milliseconds to run let's do it again 1405 let's do it one more time 1406 all right so that's how long it takes so 1.4 seconds total to run the binary search function 1 million times now what I want to do is take this code and translate it Loosely using the word translated into modu I want to run this script in modu again the translation happens because Mojo even though will be a super set is not a superet yet so we're going to have to make small modifications to it so I'm going to just take this search File copy and paste it again so I'm just basically duplicating the search file and then I'm going to rename search I'm going to use search. modu by the way you can also use em Mojo the fire icon Emoji as the extension of the file which I think is super fun I'm not going to do that here but you can do it uh notice how Visual Studio code recognizes the more your extension what when you install Mojo you will find a section explaining how to install the Mojo extension if you're using visual studio code you should do that obviously okay so now that we have our search. module let me close this one here you will notice that there are certain errors in the code this is what I was saying Mojo is not super set yet so we going to have to do a little bit of translation here let's start with the easiest one and is this one here so this is what we call a p level uh instruction or statement or call right we're calling the function main from a file at the file level here Mojo does not support that right now so you cannot have state you cannot have a plus b here you cannot have any statement at the file level so fortunately for us what moo the way moo works is it's going to find a main function inside your script okay so if you're trying to run a moo script you need to have a main main function and that will be the entry point of everything you cannot have anything at the file level you need to have anything that you want to execute inside a main function and that's exactly what we have here so we just we can just get rid of the main call and everything is going to happen starting from this point on okay so that's the first problem that problem uh is out the second problem are with the arrays okay so moju has a standard library and I have it right here here it is so the documentation of the standard Library you're going to find it here where it says apis in the Mojo website and the standard library is open source which is great every time modu uh releases something new they have a ton of changes to this standard Library they're making this standard Library as close to python as possible so obviously as they make progress uh all of those little mistakes and little discrepancies will go away because moo will become more connected to python so right now in the standard Library if you go to collections uh they offer a bunch of different collections that you can use I want to use the list collection so this is how you import it right you're going to import it from collections import the list and this list this is really important the list type is a dynamically allocated list which is exactly what I want to use here because that's the way python works when we Define an array in Python what we want to do is you don't Define a size for it just Define a dynamically uh Dynamic array so as you start adding elements to that array the language is going to start uh requesting more memory and allocating that memory just so it can fit all of those elements so that's exactly what I'm going to do here I'm going to come and I'm going to say hey I want a list okay the types that I'm going to be storing into this list are going to be in 32 and that's it that's how I'm going to Define my list by the way I'm doing that here I'm also doing that right here right in these results I'm going to be using those lists instead of using just the little the the open and and close bracket array little so that will get rid of all of the problems that I had appending values to that array so after that the second or the next problem is with the time function if we go to the standard library and look for time let's see here it is so time inside this this library or this package we have a Now function which does exactly the same work as time but in this case it Returns the time in nanc so we're going to have to change our conversion so first of all let's just find time here and let's replace it for now not this one here so we're going to use oops sorry we're going to use the Now function I don't know what happened here but let me just go back okay yeah now it's better time okay now I'm using the Now function but this conversion is not longer accurate because we're going from nanc to millisecond so instead of multiplying by thousand we have to divide by a million okay so now this is going to print the timing nanocs awesome and the next top is right here there is a problem called in binary search let's see what's going on in volid called to binary search argument zero cannot be converted from a list to an object here's what's going on so modu assumes that these two arguments are object so because we're not defining the type modu is making the assumption that they are objects and now it's telling us that it cannot convert a list in 32 to an object it doesn't know how to make that conversion okay probably the same thing or something similar will happen with the with the second with the second argument here let's start with the first argument and let's fix it by specifying that the function is going to be receiving at least in 32 okay all right so I fixed that going back let's see what the problem is right now okay so now the problem is invalid call to appen Method argument zero cannot be converted from an object to an in32 okay so what's happening right now is that results is expecting an in32 but the output of binary search because we have not specified the type of the return value is assumed by moo to be an object and it cannot convert that object into an n32 to upen it to the result so we fix that by specifying the output of the binary search the type of the output of the binary search which is going to be an n32 so now this problem is gone but we have another problem here let's see what it is it says okay we cannot call the less than because the call expects at least two positional parameters but zero were specify the problem here again is going to be with this the type of this x here that is supposed to be an object so if I change that to an in32 now we can do a comparison with this value here which is an n32 and this value here which is an n32 so now this seems to be ready to go by doing those small modifications as a summary we had to specify the types in order to call the binary search we had to ch hange some of the libraries that we were using uh like we were using time and that is not yet supported they have now we had to translate that we had to keep uh change this conversion we had to change the type of the arrays by doing those now the code is ready to go in module notice that everything else the way we run the loops the conditions all of that stays the same because modu again tries to be python as much as possible all right so how do we run this well we're going to use the modu command to run this modu script so let's do search. Modo and Brace yourselves when we execute this it only takes 68 milliseconds so 68 instead of 1406 let's let me run it a couple more times yes it's 68 instead of 1406 I'm going to bring my calculator 1406 divided by 68 that is 20 times faster by just moving the code from one language to the other we got a 20x Improvement which is pretty significant and we're not even starting yet I'm not going to go through all of those details I'm going to leave those for a separate video but there are a bunch of new features that Mojo brings to the table that can improve this a little bit more like we can turn this function into an FN function which is a little bit different we can declare all of these variables when we do that uh all of these parameters we're not going to need to make copies for them because they're going to become immutable so there is a bunch of little tricks that we are going to be covering on future videos that are going to let let us take something like this and make it even more performance so I don't know about you I'm super super stoked about Mojo I can not wait for the language to keep improving so we can start using it in production applications and that is exactly what I'm recording these videos because I want to be ready when the time comes I want to be ready to start taking my Python scripts and start changing them to modu so subscribe for more videos and I'll see you in the next one bye-bye
Original Description
This is an unconventional introduction to Mojo. I'll start with a Python script, port it to Mojo, and finish with an implementation that's 20 times faster than the initial code.
Mojo is incredible!
Here is the GitHub repository where you'll find the code in this video: https://github.com/svpino/mojo/
I teach a live, interactive program that'll help you build production-ready Machine Learning systems from the ground up. Check it out here:
https://www.ml.school
To keep up with my content:
• Twitter/X: https://www.twitter.com/svpino
• LinkedIn: https://www.linkedin.com/in/svpino
🔔 Subscribe for more stories: https://www.youtube.com/@underfitted?sub_confirmation=1
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Underfitted · Underfitted · 51 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
▶
52
53
54
55
56
57
58
59
60
Test-Time Augmentation In Machine Learning.
Underfitted
Don't Replace Missing Values In Your Dataset.
Underfitted
Introduction to Adversarial Validation In Machine Learning.
Underfitted
Introduction To Autoencoders In Machine Learning.
Underfitted
Active Learning. The Secret of Training Models Without Labels.
Underfitted
Early Stopping. The Most Popular Regularization Technique In Machine Learning.
Underfitted
The Confusion Matrix in Machine Learning
Underfitted
3 Tips to Build a Career in Machine Learning (Unconventional Advice)
Underfitted
I can predict cars CRASHING. And it's 99% accurate!
Underfitted
A Critical Skill People Learn Too LATE: Learning Curves In Machine Learning.
Underfitted
The BEST Machine Learning Interview Strategy.
Underfitted
OpenAI’s Whisper is AMAZING!
Underfitted
5 Lessons You’re NOT Taught in School
Underfitted
TensorFlow On Apple Silicon. Step-by-Step Instructions
Underfitted
Generating Images From Text. Stable Diffusion, Explained
Underfitted
The Wrong Batch Size Will Ruin Your Model
Underfitted
8 Mistakes Holding Your Career Back | Machine Learning
Underfitted
AI Just Solved a 53-Year-Old Problem! | AlphaTensor, Explained
Underfitted
Bias and Variance, Simplified
Underfitted
Should You Stop Splitting Your Data Like This?
Underfitted
The Function That Changed Everything
Underfitted
This Model Caused A Nuclear Disaster
Underfitted
Will Your Code Write Itself?
Underfitted
The Simplest Encoding You’ve Never Heard Of
Underfitted
Superhuman AI Cracked An Impossible Game! | DeepNash, Explained
Underfitted
Can you become a Data Scientist without a Ph.D?
Underfitted
How to 10x your productivity with ChatGPT?
Underfitted
Cheating the Prisoner's Dilemma
Underfitted
We integrated OpenAI's Whisper with Spot
Underfitted
The Machine Learning School program
Underfitted
We integrated ChatGPT with our robots
Underfitted
Solving complex tasks using a Large Language Model (LLM)
Underfitted
5 problems when using a Large Language Model
Underfitted
We just discovered faster sorting algorithms!
Underfitted
The 3 most important updates to OpenAI's API.
Underfitted
People are divided! Does GPT-4 understand what it says?
Underfitted
How much should you charge hourly as a Machine Learning freelancer?
Underfitted
Building a RAG application from scratch using Python, LangChain, and the OpenAI API
Underfitted
Building a RAG application using open-source models (Asking questions from a PDF using Llama2)
Underfitted
How to evaluate an LLM-powered RAG application automatically.
Underfitted
Step by step no-code RAG application using Langflow.
Underfitted
I built a simple game using Langchain. Here is a step by step tutorial.
Underfitted
I used the first AI Software Engineer for a week. This is happening.
Underfitted
I deployed a recommendation model. Testing Models In Production using Interleaving Experiments.
Underfitted
How to run PyTorch, TensorFlow, and JAX on your Mac (Apple Silicon)
Underfitted
How to train a model to generate image embeddings from scratch
Underfitted
Building an AI assistant that listens and sees the world (Step by step tutorial)
Underfitted
Why are vector databases so FAST?
Underfitted
A Machine Learning roadmap (the one I recommend to my students)
Underfitted
How to build a real-time AI assistant (with voice and vision)
Underfitted
An introduction to Mojo (for Python developers)
Underfitted
How does Lexical Scoping in Mojo 🔥 works (under 3 minutes)
Underfitted
Building a CI workflow for those who hate it (using GitHub Actions)
Underfitted
How to run Python Code in Mojo 🔥
Underfitted
AI will not take your job. Here is what I think will happen instead.
Underfitted
How to fine-tune a model using LoRA (step by step)
Underfitted
Late initialization in Mojo🔥 (Python doesn't support this)
Underfitted
The $1,000,000 problem AI can't solve
Underfitted
A gentle introduction to RAG (using open-source models)
Underfitted
Automating feedback using ChatGPT and Zapier
Underfitted
More on: ML Pipelines
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI