Why I Switched from Python to Rust for AI Deployment

  Рет қаралды 36,492

Code In a Jiffy

Code In a Jiffy

Күн бұрын

In this video, I share my journey of deploying the same AI project with both Rust and Python, highlighting the challenges, successes, and performance differences. I also compare The performance difference between Polars and Pandas.
🤔 Key Points:
Polars VS Pandas
Speed comparison between Rust VS Python
Scalability comparison between Rust VS Python

Пікірлер: 85
@zendr0
@zendr0 Жыл бұрын
So what's the conclusion? Is it ready for ML or datascience production systems
@codeinajiffy
@codeinajiffy Жыл бұрын
I wouldn't say so. Python still has more libraries for ML and is easier to develop. I would use Rust for only deployment and only in the condition that Python is too slow for your use case.
@zendr0
@zendr0 Жыл бұрын
@@codeinajiffy gotcha. Have you tried the candle crate by huggingface. They say it is Rust alternative to Pytorch. You should give it a shot. May be make a video on that if you like it.
@codeinajiffy
@codeinajiffy Жыл бұрын
@@zendr0 That sounds super interesting. I will consider it in my next videos if I find it to be a useful.
@berkes
@berkes 10 ай бұрын
It is. But very much "it depends". Rust isn't just "faster to run" (less CPU and less memory, which the video dies not go into). It's above all stricter and thus protects you against many mistakes and bugs. Not all, but many. While that may even slow down a developer in the initial days or hours, it starts paying back within weeks. And seeing that we typically do some POC or exploration at first, Python (or even bash) is probably a much better fit there. But the moment we put something into production, make it a team project and/or start maintenance and continued development, the strictness of rust pays off. Often withind days. I typically cludge together a PoC in python. Once/if I'm happy with the direction and setup, I'll throw out that hodgepodge of python, bash/sed/awk, JavaScript and whatnot. And write it in Rust.
@julyort6762
@julyort6762 10 ай бұрын
​@@berkes Yeah so design in python and deploy in Rust seems really good
@Mn16Cr45
@Mn16Cr45 11 ай бұрын
Now you can switch from Rust to Mojo and make new video about why I switched
@jorge1869
@jorge1869 10 ай бұрын
Agree❤
@421048
@421048 7 ай бұрын
damn, I searched about mojo, and tbh I cannot wait to it become stable to use in serious project man. fucking sh111t
@huvineshrajendran6809
@huvineshrajendran6809 5 ай бұрын
As much as I would like to agree with you, Mojo is not quite ready yet
@DataPastor
@DataPastor Ай бұрын
And Mojo’s license is quite restricting, hindering the adoption of the language.
@blaisepascal3905
@blaisepascal3905 Ай бұрын
Is Mojo mature enough though?
@MrRavaging
@MrRavaging 11 күн бұрын
@3:21 What about using Gating to handle tasklist management and resource allocation to prevent some requests from hogging all of the CPU and starving out other requests? Even as I'm writing this, I'm realizing that you can also apply background monitoring services and machine-learning to optimize this allocation process and prevent errors and bottlenecks.
@julyort6762
@julyort6762 10 ай бұрын
Anyway polar can run also in 1 thread, maybe you were using only the default which is multi-thread and that's bad for non intensive request
@francismatsika3727
@francismatsika3727 11 ай бұрын
I have been trying rust and the learning curve it steep . Unfortunately python has a huge ecosystem and u cant just jump it. Thats why i am looking carefully at Mojo which has similar memory management features to rust , the borrower checker and how referencing is done in rust. This can be a game changer. Mojo can bring in C perforance, Rust security and python versatility in one language
@kevinmcfarlane2752
@kevinmcfarlane2752 8 ай бұрын
From what I can see, Mojo is going to clean up in this area in the long run. Rust isn’t going anywhere but it will clean up outside of AI and scientific computing tasks. Right now, F# is more mature for AI and can play nicely in the existing ecosystem.
@TheWuhaner
@TheWuhaner 12 күн бұрын
i am not clear at 7:3 in the video, that you said "squeeze out of rust by removing polars". Do you mean you program to implement functions like Map , filter, aggregation in pure rust? I am asking because I found a similar issue when benchmarking the performance of Pandas in Python and Polars in Rust. I didn't obvious improvement switch from Pandas to Polars.
@codeinajiffy
@codeinajiffy 12 күн бұрын
Yes, I reimplemented the aggregations I wanted with only RUST using Maps. Because it's a specific use case where I know exactly what keys to put in the maps, I was able to increase performance a little here.
@TheWuhaner
@TheWuhaner 11 күн бұрын
@@codeinajiffy Thank you so much for the explanation.
@NagenBaruah-rg8mi
@NagenBaruah-rg8mi 10 күн бұрын
Chaining increases polars performance dramatically. And use polars specific syntax as far as you can. That will easily give ~60℅ boost.
@vectoralphaSec
@vectoralphaSec 11 ай бұрын
What about Mojo? The language that was literally designed for AI from the ground up? Plus Mojo's speed could be tested too.
@codeinajiffy
@codeinajiffy 10 ай бұрын
Yup, I will look into that very soon. It's just a new language that might not yet be ready for production, but we can give it a try.
@mahakasem810
@mahakasem810 10 ай бұрын
​@@codeinajiffy Their benchmark was recently revealed a lie after someone uncompleted the binary. Turns out something shady is happening in their compiler that doesn't match their claims. Big disappointment.
@houstonbova3136
@houstonbova3136 9 ай бұрын
Mojo is low-key trash.
@vectoralphaSec
@vectoralphaSec 9 ай бұрын
@@houstonbova3136 Its better than Rust in every way for AI though.
@houstonbova3136
@houstonbova3136 9 ай бұрын
@@vectoralphaSec Having done Data Science in a mix of professional and hobby for the last 5 years I’m going to have to hard disagree with that. I strongly prefer just using Python for exploration and then subbing out to raw Rust when I need gains. But, I also understand Rust. Mojo rarely matches its performance in practice and reduces my ability to create effective FFIs or directly manage the data when needed.
@factsnacksnat
@factsnacksnat 5 күн бұрын
Although Rust is powerful programming language, I still use python for make sure for being hired in industry.
@CoolDude911
@CoolDude911 3 ай бұрын
Using two packages is not comparing the two languages. There are plenty of dataframe libraries that make different compromises on the backend that can do better than pandas.
@djthedadiofficial
@djthedadiofficial 10 ай бұрын
look at virtually any published benchmarking of pandas vs polars and polars is many times faster. Without seeing your code it's impossible to prove, but this was almost certainly a skill issue in terms of writing the rust. I am also extremely skeptical about your Golang conclusions, were you using goroutines? as for your last slide, you can spawn threads in Python too but, for all the newbies out there, alas you're unlikely to see almost any of these options. Much more likely to see something like Java, Kafka, Flink, or even Postgres with plugins. For applications that need super quick inference speed, very likely to see C or CPP
@Mbd3Bal7dod
@Mbd3Bal7dod 11 ай бұрын
running multi workers for the api will solve the concrent request issue ,
@codeinajiffy
@codeinajiffy 11 ай бұрын
Nice suggestion, but multi-workers here also increased resource consumption by quite a bit. It was quite close to increasing k8s pods.
@gamex-animex6289
@gamex-animex6289 2 ай бұрын
where did you get this animation at 0:25?
@ajaypatro1554
@ajaypatro1554 3 ай бұрын
What if you used dask dataframe and dask array or maybe cython
@dimensionalcookie
@dimensionalcookie 6 ай бұрын
Hi, if possible, can you please do a tutorial how to deploy AI on production? Thank you. I know you when im interested in Go and Rust, and now im working in AI industry too. Those banger videos about AI project on your channel really help, but its a coincident when I realize this video is on the same channel.
@bacharsaleh6984
@bacharsaleh6984 5 ай бұрын
I think not is time to show us how you created that recommendation system :)
@johanngambolputty5351
@johanngambolputty5351 11 ай бұрын
The only thing that was stopping me from moving from python to something like c/c++ before was ergonomics, not that c++ can't be concise, but a lot of libraries seem to require verbose interfacing. Rust, like python, seems to be very concise, so it offers a best of both, in brevity and performance, that I always thought were mutually exclusive. I was never under the illusion that python was efficient, it just had great syntax, which made prototyping a joy. It feels like I can prototype in rust same as I used to in python, and then get the proper implementation for free at the end. All that said, wasn't the GIL getting removed and couldn't you use multiprocessing.pool?
@codeinajiffy
@codeinajiffy 11 ай бұрын
I didn't try multiprocessing.pool but that is a good suggestion. It might not be consistent between requests, but I will try it out.
@jnorris32
@jnorris32 7 ай бұрын
So did you switch from Python to Go or Rust?
@cloudguru3018
@cloudguru3018 10 ай бұрын
Now you can switch to Mojo
@aaronelmquist8607
@aaronelmquist8607 3 ай бұрын
Yeah relying on just FastAPI operating asynchronously is just the wrong approach for python under a heavy CPU workload. Those operations really would need to move off the main thread with something like celery to keep things from blocking.
@houstonbova3136
@houstonbova3136 9 ай бұрын
I’m curious if in the Rust implementation of polars, did you use lazy execution or eager?
@codeinajiffy
@codeinajiffy 8 ай бұрын
I used Lazy mostly to optimize the execution as much as possible. But yes you will need to understand lazy and eager to make this work fast.
@nurmohammed9642
@nurmohammed9642 11 ай бұрын
Biggest advantage of using rust is Zero-Cost-Abstraction, For example `sumation()` function could be write as: (1..=n).sum() and it would still out performed.
@izidorobaltazar907
@izidorobaltazar907 4 ай бұрын
Interesting. I tried the simple counting at 4:54 mark Python vs Rust. Python with numba decorator wins on my system - Rust is 3x slower.
@chats-bug
@chats-bug Ай бұрын
Numba vectorizes the operation, you can do something similar in rust as well.
@its_code
@its_code 11 ай бұрын
Why not mojo language
@codeinajiffy
@codeinajiffy 11 ай бұрын
I am considering trying it out next actually. It seems very interesting.
@its_code
@its_code 11 ай бұрын
@@codeinajiffy 🥰🥰😝
@jasonshen
@jasonshen 3 ай бұрын
to be honest rust is cool but when it comes to ai python is still the best option, speed of the development and the libs available, i did turn to rust from python, trust me i regret as the project progresses
@Singlton
@Singlton 9 ай бұрын
when trying to learn rust polars functionality , each time I run the code the compilation time takes around 10 seconds! any body knows how to reduce the time spent in compilation to make it ideal to learn and try polars functionality as a newbie ?
@LibreGlider
@LibreGlider 9 ай бұрын
Are you doing debug builds or release builds? Debug builds will be faster you can also set your debug builds to be incremental. You can also use sccache to cache compiler output. You could change the default linker or send -Z threads=8 to the rustic compiler. That is an unstable option but it will break up the front-end compilation of each package into 8 slices. But a 10 sec compile time isn't that bad considering the performance gains you get. Oh there is also cargo-watch, it will recompile your code when the file is saved. It also depends on how you are running/testing this code. Are you reading in your data from a file or stdin or are you hardcoding data in the source code?
@vuhuy8952
@vuhuy8952 5 ай бұрын
polars is doppelganger of spark when write code...
@anon_y_mousse
@anon_y_mousse 11 ай бұрын
Well, I didn't learn anything from the video, but it did add yet another data point confirming what I already knew. Python makes it immensely difficult to write efficient code and requires external libraries written in better languages to achieve any degree of speed and general usefulness. Rust is basically a more obtuse C++ that nearly everyone in the future will use because the propaganda machine surrounding it is exceedingly effective. I know that a lot of people will claim that Rust is easier to learn, but since you can write your code in C++ exactly the same way as in Rust to achieve the same goal the same way and it will be equally as fast, the ease of use of C++ for simple projects shouldn't be understated. If you write idiomatic modern C++, it's as safe as Rust.
@mahakasem810
@mahakasem810 10 ай бұрын
Sad but true reality.
@Codotaku
@Codotaku 10 ай бұрын
"it's as safe as Rust", sorry but that's an absurd statement if you knew what you're talking about
@anon_y_mousse
@anon_y_mousse 10 ай бұрын
@@Codotaku There were some qualifiers on that, but if you missed them and all the rest of what I said, then you must be a Rust user.
@LibreGlider
@LibreGlider 9 ай бұрын
@@anon_y_mousse Idiomatic C++ won't guarantee memory safety because it's unreasonable to expect devs not to make mistakes. Not to mention the safety you get out of the box with Rust, thits a fact. 100% idiomatic C++ 100% of the time?
@anon_y_mousse
@anon_y_mousse 9 ай бұрын
@@LibreGlider If you write idiomatic C++, then the compiler will catch most errors. The more insidious errors that aren't logic errors can be caught by external tools like the open source and often used `valgrind`, which catches everything the compiler misses and more than `rustc` will catch. If you had learned to program 20 years ago when they were still teaching people how to actually program instead of merely use a tool, then you would know these things. So I feel sorry for anyone learning how to use tools now instead of how to actually solve problems like an actual programmer, but we get what we allow to exist instead of what should because we've slacked off and not done our job for future generations. Sorry, my generation's intransigence in doing our duty for your generation has royally screwed you.
@guttenbergovitz
@guttenbergovitz 7 ай бұрын
You nailed it. Rust shall it be... 🦀
9 ай бұрын
👏👍👍
@andrew.derevo
@andrew.derevo 5 күн бұрын
Rust is definitely great programming language, but it absolutely doesn’t work great for AI stuff. You don’t have any libraries, extensions, manuals, almost nothing. you have to do everything by yourself. This is great if you study, but if you need things to be done fast, the python is an only choice for AI right now. Praying for mojo😅
@martincontrerasu
@martincontrerasu 10 ай бұрын
is there any git code link to check your recommendation project?
@codeinajiffy
@codeinajiffy 10 ай бұрын
no github link because this was company code. I am thinking of doing another project that I can share.
@UnbenutzerKanalname
@UnbenutzerKanalname 7 ай бұрын
Try Julia for ML
@enchantedshit
@enchantedshit 10 ай бұрын
please provide free resources for rust
@codeinajiffy
@codeinajiffy 9 ай бұрын
I learned Rust from this video: kzbin.info/www/bejne/r5ivkKuZltuth5Y . Then just jump on project and figure things out along the way. This should enough if you have some background in programming.
@RoyAAD
@RoyAAD 9 ай бұрын
Where's the AI?
@ArminMahyar
@ArminMahyar 10 ай бұрын
Only c++, rust is a copy of that, dont be naive
@SupremelyAverage
@SupremelyAverage 10 ай бұрын
I agree. But it's quite better imo
@herkerr
@herkerr 8 ай бұрын
At the end it’s all assembly. Why complicate it?
@slip-shape994
@slip-shape994 8 ай бұрын
That means you even don't know C++
@ArminMahyar
@ArminMahyar 8 ай бұрын
@@slip-shape994 How old R U? U Tell me what c++ is?
@Miles-co5xm
@Miles-co5xm 8 ай бұрын
C--
@Singlton
@Singlton 9 ай бұрын
do you have discord ?
@codeinajiffy
@codeinajiffy 8 ай бұрын
Not yet, I am thinking of creating one.
Neural Networks From Scratch in Rust
9:12
codemoon
Рет қаралды 21 М.
Why I Switched from Python to Go Lang for AI Deployment
6:12
Code In a Jiffy
Рет қаралды 66 М.
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Is Rust the New King of Data Science?
15:38
Code to the Moon
Рет қаралды 141 М.
Should you learn Rust in 2025?
11:11
Let's Get Rusty
Рет қаралды 48 М.
why rust libraries may never exist.
7:26
Low Level
Рет қаралды 297 М.
How To Make Your Python Packages Really Fast With RUST
5:16
Isaac Harris-Holt
Рет қаралды 44 М.
Why I’m Learning Rust in 2024 (and new dev environment)
16:53
ForrestKnight
Рет қаралды 72 М.
PyO3: From Python to Rust and Back Again (with David Hewitt)
1:34:30
Developer Voices
Рет қаралды 13 М.
A New Era for C and C++? Goodbye, Rust?
9:08
Travis Media
Рет қаралды 141 М.
When Optimisations Work, But for the Wrong Reasons
22:19
SimonDev
Рет қаралды 1,2 МЛН
Why JavaScript Devs are Switching to Rust in 2024
10:35
warpdotdev
Рет қаралды 265 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 1,6 МЛН
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН