为什么 Rust 在 AI 领域越来越受欢迎:OpenAI 的 tiktoken 用 Rust 实现,分词速度超 Python 版 20 倍
Why Rust Is Gaining Ground in AI!🦀 Most AI code is written in Python, but companies building AI tools keep moving their performance-critical code to Rust. Before a model can answer a chat message, the server has to split the text into tokens and turn each token into a number. OpenAI’s tokenizer, tiktoken, does this step in Rust. In a laptop test, it tokenized 1,000 questions more than 20 times faster than OpenAI’s Python teaching version, while still being called from Python. Python remains the language for training and research. Rust runs the code that has to be fast and safe on every request. Would you learn Rust next, or stay with Python? Comment below. #Rust #Python #AI #RustLang #MachineLearning #AIEngineering #Tokenization #OpenAI #MemorySafety #Programming #SoftwareEngineering #DevOps #KodeKloud #Shorts
OpenAI 的分词器 tiktoken 用 Rust 实现,在笔记本测试中处理 1000 个问题的速度比其 Python 教学版快 20 多倍,且仍可从 Python 调用。
粉丝 11万
- 浏览
- 7.7万
- 点赞
- 5594
- 评论
- 60
- 分享
- 252
- 收藏
- 1486
数字来自平台,为最近一次抓取时的数据。
以下是视频口播的自动转写,可能有错字。
Why is rust getting more adoption in ai? If you start learning ai today, almost everything you touch is in python, but companies building ai tools, keep moving more of their code to rust.
Let's take a simple example. Let's say you run an ai chat app and in one second, 1000 people send it a question. The ai model cannot read words only numbers.
So first your server has to cut every question into small pieces and turn each piece into a number. Only then the gpu can run the model on these numbers.
These small pieces are called tokens. Now, let's say you write this cutting step in plain python. Python is easy to write, but it is slow when it repeats the same small step millions of times.
OpenAI's tokenizer called tick token is written in rust, but OpenAI also published a simple teaching version in python, so you can compare the two.
On a laptop, the python version cut a thousand questions in about a tenth of a second. The rust version cut them in about 5000 of a second, so it was more than 20 times faster.
You call it from python, but the cutting runs in compiled rust code, roughly as fast as c plus plus, but c plus plus was already this fast.
So why not just use c plus plus? The answer is memory safety c and c plus plus give you full control over memory, which also makes mistakes easy.
Let's say your program gives a piece of memory back and later by mistake, it uses that same piece again. This is called use after free and it can crash the program or an attacker can use it to run their own code a few years ago microsoft and google is chrome team each reported that roughly 百 分 之 7 0 of their serious security bugs were memory mistakes like this one languages like java and go avoid most of these mistakes by finding and cleaning up unused memory for you and this is called garbage collection but that cleanup costs extra cpu work while your program runs rust does something else.
Every piece of data has one owner, and when the owner is done, rust frees the memory on its own, so there is no garbage collector at all.
And before your program even runs, the compiler checks that nothing uses memory after it was freed. If something does, the compiler refuses to build the program, and this check is called the borrow checker.
This also gives rust an interesting advantage now that ai coding agents write a lot of code. Let's say an agent writes a program in c, and makes that same use after free mistake.
The c compiler may build it without complaint, and the bug shows up only when the program runs. In rust, the borrow checker refuses to build it and prints an error that says which line broke which rule.
The agent reads that error, fixes the line, and tries again. So, this kind of mistake is caught before the code goes live.
It won't catch every bug, but it does catch this kind. Earlier this year, two engineers at OpenAI used codex to rewrite the storage service behind ChatGPT from python to rust, and the rust version uses about 6 times less cpu and 15 times less memory.
So, should you learn rust instead of python for ai? Usually not, because training a model and doing research still happen in python.
The heavy math runs on the gpu anyway, and python is much quicker to write when you are changing your code every day. Easy way to remember is you write your ai code in python, but the code that has to be fast and safe on every request is more and more often rust.
You can learn rust on codecloud, enroll now.
来源:TikTok · 搜「ai tools」 · tiktok.com