第 381 期·中文精读

克里斯·拉特纳:编程与 AI 的未来

Chris Lattner: Future of Programming and AI

LLVM 与 Swift 的创造者谈编程语言的演进、编译器技术,以及 AI 将如何改变写代码的方式。

官方音频00:00 / --:--
中文精读由 AI 基于原始访谈改写,非官方翻译,可能存在偏差或错误;表述以原音频与官方文稿为准。说明

Mojo 官方给出的性能数字是:某些场景下比 Python 快 35000 倍。

说这个数字的人是克里斯·拉特纳(Chris Lattner)。他造过 LLVM 和 Clang,发明了 Swift,在 Google 参与过 TensorFlow 和 TPU,在特斯拉当过 Autopilot 软件副总裁。现在他联合创办了 Modular,一家做 AI 基础设施的公司,Mojo 是这家公司的编程语言,一门 Python 的超集。

这是他第三次上 Lex Fridman 的播客。两年不见,他手里多了一门新语言。

一门语言的文件扩展名,可以是一个火苗

Mojo 还有个设计,比 35000 倍更能说明这个人的审美:源文件的后缀名可以是一个 emoji,比如 🔥。

他半开玩笑地说,这大概是这门语言最重要的决定。理由倒不全是玩笑:文件扩展名本质上是元数据,既然是元数据,为什么要让它占掉屏幕上一块地方,还要和一堆 txt、pdf 挤在一起,长得灰扑扑的。想酷一点,就得在视觉上跳出来。

实际用起来有一点小麻烦。Git 不认这个,它把火苗当成不可打印字符,在命令行里吐出一串奇怪的十六进制。GitHub、VS Code、Windows 资源管理器都没问题,因为它们早就为国际化路径做过准备,emoji 只是再往前迈一步。

拉特纳对这事的态度是:Git 可以被修好。他把整个反应看成一条光谱,一端是“哦这个挺酷”,另一端是“你在杀我的孩子”。他判断世界会走到前面那一端,而且不必赌上全部家当,两条路可以同时铺。

这个细节值得多看一眼。一个人愿意在文件扩展名这种毫秒级的体验上花心思,说明他做语言不是在做规范,是在做产品。

两头都在动,中间就成了一团乱麻

要理解 Mojo 为什么存在,得先看 Modular 想解决的问题。

拉特纳的观察是,TensorFlow 和 PyTorch 这些框架,设计于七八年前。那时候大语言模型还不存在。今天人们拿它们做的事情,和它们被造出来时的目标已经差了一大截。这是软件这一头的变化。

硬件那一头的变化更剧烈。新芯片、新加速器、各种 ASIC,几乎每家像样的公司都在发布自己的芯片,TPU、GPU、NPU、IPU,各种 P 打头的处理器。拉特纳自己在 Google 做过 TPU,那是人类部署过最大规模的 AI 计算系统之一。

一边是移动的系统,另一边也是移动的系统,中间就变成了一团巨大的乱麻。(克里斯·拉特纳)

乱麻的代价落在使用者身上。搞软件的人面对一堆自己用不上的硬件,理论上能编程和实际上能把全世界聪明人的创造力释放出来,是两件完全不同的事。

Modular 想做的就是中间那层软件栈,让研究者把想法送进生产环境的路径短一点。Mojo 是这层栈里最关键的一块砖。

Python 到底好在哪

拉特纳在节目里反问 Lex:你喜欢 Python 什么?

Lex 的回答很感性:直观,写起来像写英文,读别人的代码也比读 C++ 快,同样的意思用更少的字。还有生态,想干什么都有现成的包。

拉特纳接着往下挖了一层。他认为机器学习确实帮了 Python 大忙,TensorFlow 和 PyTorch 拥抱 Python,直接把 Python 推到了今天的位置。但真正让它长成这样的,是它扮演的角色。

Python 是那个通用连接器。它能把很多不同的系统拼在一起,让你组合出更大的东西,而不必先搞懂每个零件内部怎么转。(克里斯·拉特纳)

这个定位挺准确。Python 更像一块乐高底板,形状各异的积木都能插上去,虽然每块积木本身不是它造的。生态和易用性互相喂养,滚成一个正向循环,一旦滚起来就很难被替代。

那问题在哪?

慢。

35000 倍这个数字,一层层拆开

这个倍数听着像营销话术,其实拆开看,每一层都有具体的技术来源,加起来还真能到这个量级。

第一层,把解释器拿掉。大家平时敲 python3 跑的那个叫 CPython,是个解释器。代码要先变成字节码,再被读、被解析、被解释执行,这中间多了一整层。改用编译器,光是消掉这一层,根据代码不同就是 2 倍到 5 倍、10 倍的提升。

第二层,把数据从盒子里拿出来。CPython 有个设计决定:一切皆对象,为了让所有对象的内存表示统一,每个对象都带一个头,后面跟载荷。这意味着你传递一个整数,传的其实是一个指向数据的指针。每次分配要记账,每次引用计数要加减,现代 CPU 最讨厌的就是这种追着指针满内存跑的行为,因为缓存会被打穿。把这层间接去掉,让数字直接躺在寄存器里,又是一个 10 倍。两层乘起来,量级已经很可观。

第三层,用上向量。现代 CPU 一条指令能同时处理 4 个、8 个、16 个甚至 32 个数据。Python 不暴露这个能力。Mojo 暴露了。

第四层,用上多线程。这里牵扯到 GIL。很多人抱怨 Python 的全局解释器锁让并行变得半残,拉特纳指出,GIL 和引用计数是死死绑在一起的,你没法只扔一个留一个。

第五层,控制内存层次。数据放在哪一级缓存、怎么布局,直接决定跑多快。这些在 Python 里根本没法表达。

这几层叠起来,就是那个吓人的数字。Mojo 给这些能力的语法还相当省事,比如你想让一段循环并行,写一句 parallelize 就行。

性能的意义不止是快。拉特纳给了两个理由,第二个更值得琢磨。

对企业和环境来说,性能等于成本和能耗。他开了个玩笑:你可以说 Python 对环境不好,换到 Mojo 至少立刻好上十倍。

对产品来说,性能等于体验。模型延迟降下来,产品团队就敢把模型做大;模型做大,用户感受到的东西就更好。这条因果链在机器学习行业里反复出现,很多时候模型能力的上限不取决于算法,取决于推理成本。

把解释器搬进编译器

Mojo 最有意思的设计在这里。

Python 的表达力很大一部分来自动态特性。你定义一个类,重载它的 __add__,加号就能用在这个类上。PyTorch 的自动微分、eager 模式,底下全是这类运行期的魔法。

问题是这些魔法在 GPU 上跑不了,就算能跑,性能也不划算。

Mojo 的做法是:既然 Python 内部本来就有一个解释器,那就让这个解释器在编译期运行。于是你得到编译期元编程。API 写法一点不变,那些动态特性被整体抬到编译期执行,生成的代码再去跑在加速器上。

他拿 Lisp 做类比。Lisp 最强大的地方在于,宏和普通代码是同一套东西,同一套语法、同一套概念。这让它简单、一致、可组合,你写的库在运行期和编译期都能用。

C++ 走的是反方向。模板系统跟运行期编程是两个平行宇宙,语法不同,概念不同,调试方式也不同。

它是意外变得强大的。它并不是被设计成图灵完备的,那是后来一路上被发现的事情。(克里斯·拉特纳)

他说自己写过足够多的 C++,有资格抱怨几句。这话不假,一个维护过 Clang 的人对 C++ 的感情大概最复杂。

他提到 Zig 也在这个方向上探索,把运行期能做的事挪到编译期。这条路这几年确实在聚拢共识。

技术上难在哪?Mojo 底下是一套全新的编译器设计,建在 MLIR 之上,还塞进了缓存、解释器、JIT。也就是说,编译器内部装了一个解释器,这个解释器现在还得把代码拿到目标机器上真跑一遍,看哪个版本最快,再缝回编译流程里继续。

编译过程本身变成了机器相关的搜索。这在传统编译器里是很少见的形态。

让人去记 TPU 的 tile size 是 128,本来就不合理

自动调优(auto-tuning)是 Mojo 另一个被低估的特性。

先看现实。硬件专家脑子里装着一堆魔数:缓存多大、寄存器多少个、向量多长最划算、GPU 有多少个 SM、warp size 是多少、TPU 的 tile 是 128。这些数字确实重要,写对了代码能快好几倍。

我的看法是,大多数普通程序员其实不想知道这些东西。(克里斯·拉特纳)

而且这些魔数还在变。A100、H100、Volta,算法是同一套,参数各不相同。再往远处看,你的代码半年前写在笔记本上,今天要搬到服务器上跑,明天可能换了张显卡。每次都要重调一遍,谁受得了。

自动调优的思路很朴素:既然外面有的是算力,何必让人去猜。你把算法结构写清楚,比如切成块、并行处理、按 128 或者别的粒度切,然后告诉编译器“这些方案你挑一个”,编译器在真实硬件上跑一遍,谁快用谁,结果缓存下来,下次直接用。

一步到位,写完就不用管了。换机器的时候它自己重新试。

这套机制解决的其实是个老问题:可移植性和极致性能长期矛盾。以前的做法是 #ifdef 一层套一层,代码越来越难看,测试的组合数爆炸。自动调优把这件事从“人手写分支”变成“机器做实验”,本质上是用算力换人的注意力。

缩进这件事,他愿意吵

中间有一段岔得很远,但挺有意思。

Lex 提到自己从 Emacs 换到 VS Code,顺带说了句考虑过 Vim 但没选,结果收到一堆 hate mail。拉特纳接话说,缩进就是比花括号好,而且他能给出理性论证。

他的论证有三条。Python 有几百万人用,占住了机器学习,这是事实层面的理由。花括号语言最后也要跑格式化工具,跑完的结果是缩进加花括号两套并存,既然要分组,为什么留两套。还有那些“只有一条语句时可以省略花括号”的设计空间,Python 式的缩进根本不需要面对。

Lex 想看数据:因为缩进写错导致的 bug,Python 里多还是 C++ 里多。两个人都觉得大概率不多,因为编辑器会替你缩进,你甚至不需要跟它较劲。

拉特纳承认这是宗教问题,但他乐于捅一下熊。捅完还是回到正题:

我们不想破坏 Python,也不想改它,或者打引号地“修复”它。我们爱 Python 本来的样子。我们的看法是,Python 只是还没写完。(克里斯·拉特纳)

这句话是理解 Mojo 定位的钥匙。它不打算取代 Python,它打算接着写。

类型是可选项,不是教条

Python 有类型,字符串、整数、字典,但全都活在运行期。所以你写函数不用标类型,代价是所有东西必须是同一种表示:指向带对象头的对象的指针。方法分发永远走同一条路径,不管接收者实际是什么。

Mojo 允许你渐进式地加类型。这个整数是 32 位还是 64 位,这个浮点是双精度还是单精度,编译器拿到这些信息就能做更好的优化,代码补全也更准,因为它知道这个对象上有哪些操作。

整套设计的重心落在“渐进”两个字上。你可以在任何地方加任意多的类型,不想加就不加,Python 那套动态语义照样完整保留。

拉特纳的立场是:类型不是对的东西,也不是错的东西,它是有用的东西。这个说法比两边阵营的吵架都更接近工程现实。

Mojo 到底是什么

回到最基本的问题。它是编译型还是解释型?

答案是全都干。它可以是解释执行的,可以是 JIT 编译的,也可以是静态编译的。三种模式共存,因为不同场景的需求不一样:试验阶段要快,部署阶段要极致。

它是不是只服务 AI?AI first,是的,AI 的需求在推着它往前走。但目标是通用语言。判断标准他给的很简单:只要某个地方出现“上面一层 Python 做 API、下面一层 C 和 C++ 做计算”这种结构,那就是 Mojo 的机会。

这个模式在今天的软件世界里到处都是。科学计算、数据处理、金融建模、图像处理,全是这个形状。用一门语言从头写到尾,把两层合成一层,收益不只是省掉一门语言的学习成本,还有调试、部署、团队协作上的简化。

至于那个火苗后缀,拉特纳说得轻描淡写:不必赌上全部家当,两条路可以同时铺。这话听着像在讲文件扩展名,其实也是在讲 Mojo 和 Python 的关系。

这场对话发生的时候,Mojo 还没开放。但从他描述的技术路径看,方向是清楚的:不要让使用者为了性能离开 Python,而是让 Python 长出自己的性能。

至于能不能成,得看接下来几年有多少人愿意把生产代码搬过去。

  • 去 mojolang.org 申请早期访问,找一段你熟悉的 Python 数值代码,原样跑一遍,再给热点函数加上类型标注跑一遍,自己看差距,别信二手数字。
  • 翻一遍你手上的项目,找出“Python 调 C++ 扩展”的地方。这些地方就是最值得试点的位置,收益最直接,风险最可控。
  • 如果暂时用不上 Mojo,也可以先借它的思路:把代码里那些手调的魔数(分块大小、向量长度、线程数)抽出来,写成可配置的参数,让程序自己在目标机器上试。哪怕用 Python 写脚本做这件事,也比凭经验拍脑袋强。
  • 想理解编译期元编程,别从 C++ 模板入手,先看 Lisp 的宏,理解“代码即数据”之后再看 Mojo 的设计会顺很多。
  • 把 GIL 和引用计数当成同一个问题看。这样你就能判断某个并发方案的宣传是真解决了问题,还是绕过去了。
  • 缩进还是花括号,别在这上面花时间吵架了。真的,两个人都说这是宗教问题,信哪个都不影响你写出好代码。
文稿由本站用语音识别模型(基于官方发布音频)机器转写,可能有识别错误,人名与数字请以音频为准。
Lex Fridman Podcast #381 – Chris Lattner: Future of Programming and AI Machine transcription by Qwen3-ASR 0.6B (MLX, local, Apache-2.0). May contain recognition errors. The following is a conversation with Chris Latner, his third time on this podcast. As I've said many times before, he's one of the most brilliant engineers in modern computing, having created LLM compiler infrastructure project, the clang compiler, the Swift programming language, a lot of key contributions to TensorFlow and TPU's as part of Google. He served as vice president of autopilot software at Tesla. Was a software innovator and leader at Apple, and now he co-created a new full stack AI infrastructure for distributed training, inference, and deployment on all kinds of hardware called Modular and a new programming language called Mojo that is a superset of Python, giving you all the usability of Python but with the performance of C C plus plus. In many cases, Mojo code has demonstrated over thirty thousand X speedup over Python. If you love machine learning, if you love Python, you should definitely give Mojo a try. This programming language, this new AI framework and infrastructure, and this conversation with Chris is mind blowing. I love it. It gets pretty technical at times, so I hope you hang on for the ride. And now a quick few second mention of each sponsor. Check them out in the description. It's the best way to support this podcast. We got iHerb for health, Numai for world's hardest data science tournament, and Inside Tracker for tracking your biological data. Choose wisely, my friends. Also, if you want to work with our team, our amazing team, we're always hiring. Could Alex Friedman dot com slash hiring. And now onto the full ad reads. As always, no ads in the middle. I try to make this interesting, but if you skip them, if you must, my friends, please still check out the sponsors. I enjoy their stuff, maybe you will too. This show is brought to you by iHerb, a platform, a website, a place we can go and get high quality selected just for you health and wellness products. For great value, inexpensive, affordable. I get fish oil over there. It's one of the main supplements I've taken for a long, long, long time in pill form. Makes me feel like I'm oiling the machine that is the human body and the human mind. Even just saying that makes me wonder what is the power of the placebo effect in all of this. I'm actually a big believer in the power of the human mind, coupled with the effectiveness of medication and supplements and nutrition and diet and exercise. All of it. If you couple the belief that the thing will work with stuff that actually works. It's like a supercharge. It's the mind. There's something about the mind allowing the thing to work. And maybe the belief that it works reduces stress and has kind of secondary and tertiary effects that you can't even comprehend on the entirety of the biological system that is the human body. It's so fascinating and it's so difficult to do good studies on that. Because the whole point is, you want to study the effect of the entirety of the lifestyle and diet decisions you make on the entirety of the human organism—the billions of organisms that make up a single organism that is you. Anyway, get 22% off with promo code Lex when you go to iHerb dot com slash Lex. This show is also brought to you by Numai, a hedge fund that uses AI and machine learning to make investment decisions. They created a tournament, a challenge for all machine learning gurus to come and to compete against each other. To build the best predictive models for financial markets, the stakes are high. This is the kind of problems in the machine learning space that I really care about—real world problems with high stakes, not toy problems, not ImageNet. Now ImageNet and all that kind of stuff is good for exploring little ideas, the nuances of architectures, training procedures, of. Cool little ideas of the entirety of the pipeline of how to do machine learning, or for education purposes. But if you want to really develop ideas that work in the real world, you should be working on real world data where the stakes are high. And this is probably one of the hardest tournaments for machine learning in the world. Head over to numai dot com slash lex to sign up for a tournament and hone your machine learning skills. That's numai dot com slash lex for a chance to play against me and win a share of the tournament prize pool. This show is also brought to you by Inside Tracker, a service I use to track biological data. As I was saying, the entirety of the biological organism that is you—the individual cells, all the life force that makes up the details of the cells and the proteins. And the blood and the organs and the individual systems that are interconnected to each other in these incredible complex ways that are all asynchronously communicating with each other through chemistry, through physics, through electrical signals, through mechanical signals, through whatever the hell signals that I can't even find the right words for. It's an incredible system. How is it possible that this thing works at all? I'm looking out into the distance now as I say these words and my visual cortex is processing all of it and somehow makes sense of all of it. This is incredible. Anyway, measuring data on this incredible organism is good. And companies that allow you to measure it in order to make lifestyle decisions is obviously the future. That's why I support Inside Tracker. You can get special savings for a limited time when you go to insidetracker dot com slash lex. This is the Lex Friedman podcast to support it. Please check out our sponsors in the description. And now your friends here's Chris Latner. It's been I think two years since we last talked, and in that time you somehow went and co-created a new programming language called Mojo. So it's optimized for AI. It's a superset of Python. Let's look at the big picture. What is the vision for Mojo? For Mojo, well, so I mean I think you have to zoom out. So I've been working on a lot of related technologies for many many years. So I've worked on LLM and a lot of things and mobile and servers and things like this. But the world's changing, and what's happened with AI is we have new GPUs and new machine learning accelerators and other ASICs and things like that that make AI go real fast. At Google, I worked on TPU's. That's one of the biggest, largest scale deployed systems that exist for AI. And really, what you see is if you look across all of the things that are happening in the industry, there's this new compute platform coming, and it's not just about. CPUs or GPUs or TPU's or NPU's or IPU's or whatever all the PUs, right? It's about how do we program these things, right? And so for software folks like us, right, it doesn't do us any good if there's this amazing hardware that we can't use. And one of the things you find out really quick is that having the theoretical capability of programming something and then having the world's power and the innovation of all the all the smart people in the world get unleashed on something can be quite different. And so really, where Mojo came from was starting from a problem of we need to be able to take machine learning, take the infrastructure underneath it and make it way more accessible, way more usable, way more understandable by normal people and researchers and other folks that are not themselves like experts in GPUs and things like this. And then through that journey, we realized, hey, we need syntax for this, we need to program language. So one one of the the main features of the language, I say so fully in jest, is that it allows you to have the file extension to be an emoji or the fire emoji, which is. One of the first emojis used as a file extension I've ever seen in my life, and then you ask yourself the question, "Why in the 21st century are we not using Unicode for file extensions?" I mean it's an epic decision. I think clearly the most important decision in the most, but you could also just use Mojo as the file extension. Well, so okay, so take a step back. I mean, come on, Lex, do you think that the world's ready for this? This is a big moment in the world, right? This is we're releasing this onto the world. This is innovation. I mean, it really is kind of brilliant. Emoji is such a big part of our daily lives. Why isn't it not in programming? Well, and and like you take a step back and look look at what file extensions are, right? They're basically metadata, right? And so why are we spending all the screen space on them and all the stuff? Also, you know you have them stacked up next to text files and PDF files and whatever else. Like if you're going to do something cool, you want to stand out, right? Emojis are colorful, they're visual, they're beautiful. What's been the response so far? From is there support on like Windows on operating system in displaying like file explorer? Yeah, the one problem I've seen is that Git doesn't escape it right, and so it thinks that the fire emoji is unprintable and so it like prints out weird hex things if you use the command line Git tool. But everything else, as far as I'm aware, works fine. And I have faith that Git can be improved, so I'm not Git Hub is fine. Git Hub is fine, yeah, Git Hub is fine, Visual Studio Code, Windows, like all the stuff totally ready because people have internationalization in their normal part of their path. So this is just like taking the next step, right? Somewhere between, oh wow, that makes sense, cool. I like new things too. Oh my god, you're killing my baby! Like what are you talking about? This can never be like I can never handle this. How am I going to type this? Like all these things, and so this is something where I think that the world will get there. We don't have to bet the whole farm on this. I think we can provide both paths, but I think it'll be great. When can we have emojis as part of the code? I wonder. Yeah, so I mean lots of languages provide that. So I think that we have partial support for that. It's probably not fully done yet, but but yeah, you can you can do that. For example, in Swift, you can do that for sure. So example we give gave it Apple was the the dog cow. Yeah, so that's a classical Mac heritage thing, and so use the dog and the cow emoji together and that could be your variable name. But of course, the internet went and made pile of poop for everything. Yeah, so you know if you want to name your function pile of poop, then you can totally go to town and see how that gets through code review. Okay, so let me just ask a bunch of random questions. So is Mojo primarily designed for AI's or is it a general purpose programming language? Good question. So it's AI first, and so AI is driving a lot of the requirements. And so Modular is building and designing and driving Mojo forward, and it's not because it's an interesting project theoretically to build. It's because we need it. And so Modular, we're really tackling the AI infrastructure landscape and the big problems in AI and the reasons that it is so difficult to use and scale and adopt and deploy and like all these big problems in AI. And so we're coming at it from that perspective. Now, when you do that, when you start tackling these problems, you realize that the solution to these problems isn't actually an AI specific solution. And so while we're doing this, we're building Mojo to be a fully general programming language, and that means that you can obviously tackle GPUs and CPUs and like these AI things, but it's also a really great way to build. Numai and other things like that, or you know just if you look at what many Python libraries are today, often there's a layer of Python for the API and they end up being C and C plus plus code underneath them. That's very true in AI, that's true in lots of other demands as well. And so anytime you see this pattern, that's an opportunity for Mojo to help simplify the world and help people have one thing. Optimized through simplification by having one thing. So you mentioned Modular Mojo is the programming language, Modular is the whole software stack. So just over a year ago, we started this company called Modular. Yeah, okay. What Modular's about is it's about taking AI and upleveling it into the next generation, right? And so if you take a step back, what's gone on in last five, six, seven, eight years is that we've had things like TensorFlow and PyTorch and these other systems come in. You've used them, you know this, and what's happened is these things have grown like crazy. They get tons of users. It's in production deployment scenarios. It's being used to power so many systems. I mean AI is all around us now. Now it used to be controversial years ago, but now it's a thing. But the challenge with these systems is that they haven't always been thought out with current demands in mind. And so you think about it when where were LLMs eight years ago? Well, they didn't exist, right? AI has changed so much, and a lot of what people are doing today are very different than when these systems were built. And meanwhile, the hardware side of this has gone into a huge mess. There's tons of new chips and accelerators, and every every big company's announcing a new chip every day. It feels like, and so between that you have like moving system on one side, moving system on the other side, and it just turns into this gigantic mess, which makes it very difficult for people to actually use AI, particularly in production deployment scenarios. And so what Modular's doing is we're helping build out that software stack, help solve some of those problems, so then people can be more productive and get more AI research into production. Now what Mojo does is it's a really really really important piece of that, and so that is you know part of that engine and part of the technology that allows us to solve these problems. So Mojo is a programming language that allows you to do the high level programming, the low level programming, like do all kinds of programming. In that spectrum that gets you closer and closer to the hardware. So take a step back, so Lex. What do you love about Python? Oh boy, where do I begin? What is love? What do I love about Python? You're guy who knows love. I know this. Yes, how intuitive it is. How it feels like I'm writing natural language English. How when I can not just write but read other people's code somehow I can understand it faster. It's more condensed than other languages like ones I'm really familiar with like C plus plus. And C, there's a bunch of sexy little features. Yeah, we probably talk about some of them, but list comprehensions and stuff like this. Also, and and don't forget the entire ecosystem of all the packages. Oh yeah, that's probably huge. Because there's always something if you want to do anything, there's always a package. Yeah, so it's not just the ecosystem of the packages and the ecosystem of the humans that do it. That that's a really that's an interesting dynamic. Because I think something about the the usability and the ecosystem makes the thing viral. It grows and then it's a virtuous cycle. I think. Well, and there's many things that went into that. Like so, I think that ML was very good for Python, and so I think that TensorFlow and PyTorch and these systems embracing Python really took and helped Python grow. But I think that the major thing underlying it is that Python's like the universal connector. Right, it really helps bring together lots of different systems so you can compose them and build out larger systems without having to understand how it works. But then what is the problem with Python? Well, I guess you could say several things, but probably that it's slow. I think that's usually what people complain about, right? And so slow. I mean, other people complain about tabs and spaces versus curly braces or whatever, but I mean those people are just wrong because it is actually just better to use indentation. Wow, strong words. So actually, I'm a small tangent. Let's actually take that. Let's take all kinds of tangents. Oh come on, Lex, you can push me on it. I can take design design. Listen, I recently left Emacs for VS Code, and the kind of hate mail I had to receive because on the way to doing that I also said I've considered Vim. Yep, and chose not to and went with VS Code and just touching on deep religions, right? Anyway, tabs is an interesting design decision, and so you really written a new programming language here. Yes, it is a a superset of Python, but you can make a bunch of different interesting decisions here. Totally, yeah, and you chose actually to stick with Python as a. In terms of some of the syntax, well, so let me explain why, right? So I mean, you can explain this in many rational ways. I think that the indentation is beautiful, but that's not a rational explanation, right? So, but I can defend it rationally, right? So first of all, Python One has millions of programmers. Yeah, it is huge. It's everywhere. It owns machine learning, right? So factually, it is the thing, right? Second of all, if you look at it, C code, C plus plus code, Java, whatever Swift, curly brace languages, also run through formatting tools and get indented. And so if they're not indented correctly, first of all, we'll twist your brain around, it can lead to bugs. There's notorious bugs that have happened across time where the indentation was wrong or misleading and it wasn't formatted right, and so it turned into an issue, right? And so what ends up happening in modern large scale code bases is people run automatic formatters. So now what you end up with is indentation and curly braces. Well, if you're going to have you know the notion of grouping, why not have one thing, right? And get rid of all the clutter and have a more beautiful thing, right? Also, you look at many of these languages, it's like okay, well you can have curly braces or you can omit them if there's one statement or you just like enter this entire world of complicated design space that. Objectively, you don't need if you have Python style indentation. So yeah, I would love to actually see statistics on errors made because of indentation, like how many errors are made in Python versus in C plus plus that have to do with basic formatting all that kind of stuff. I'd love to see. I think it's it's probably pretty minor because once you get like you use VS Code I do too. So if you get VS Code setup, it does the indentation for you generally, right? And so you don't you know it's actually really nice to not have to fight it, and then what you can see is the editor is telling you how your code will work by indenting it, which I think is pretty cool. I honestly don't think I've ever I don't remember having an error in Python because I indented stuff wrong. So I mean I think that there's again this is a religious thing, and so I can joke about it and I love I love to kind of you know I realize that this is such a polarizing thing and everybody wants to argue about it, so I like poking at the bear a little bit, right? But but frankly, right come back to the first point Python One like it's huge, it's an AI, it's the right thing for us. Like we see Mojo as being an incredible part of the Python ecosystem. We're not looking to break Python or change it or quote unquote fix it. We love Python for what it is. Our view is that Python is just not done yet. And so if you look at you know you mentioned Python being slow, well there's a couple of different things that go into that which we can talk about if you want. But one of them is it just doesn't have those features that you would use to do C like programming. And so if you say okay, well I'm forced out of Python into C for certain use cases. Well then what we're doing is we're saying okay well why why is that can we just add those features that are missing from Python back up to Mojo and then you can have everything that's great about Python all the things you are talking about that you love plus not be forced out of it when you do something a little bit more computationally intense or weird or hardwarey or whatever it is that you are doing well a million questions I want to ask what high level again is it compiled or is it an interpreter language so Python is just in time compilation what's what's Mojo so Mojo a complicated answer does all the things so it's interpreted it's JIT compiled and it's statically compiled um and so this is for a variety of reasons um so one of the things that makes Python beautiful is that it's very dynamic and because it's dynamic one of the things they added is that it has this powerful meta programming feature and so if you look at something like PyTorch or TensorFlow or or um I mean even a simple simple use case like you define a class that has the plus method right you can overload the Dunder methods like Dunder add for example and then the plus method works on your class and so it has very nice and very expressive dynamic meta programming features in Mojo we want all those features come in like we don't want to break Python we want it all to work but the problem is is you can't run those super dynamic features on an embedded processor or on a GPU right or if you could you probably don't want to just because of the performance and so we entered this question of saying okay how do you get the power of this dynamic meta programming into a language that has to be super efficient in specific cases and so what we did was we said okay we'll take that interpreter Python has an interpreter in it right take that interpreter and allow it to run it compile time and so now what you get is you get compile time meta programming and so this is super interesting super powerful because one of the big advantages you get is you get Python style expressive APIs you get the ability to have overloaded operators and if you look at what happens inside of like PyTorch for example with automatic differentiation and eager mode like all these things they're using these really dynamic and powerful features at runtime but we can take those features and lift them so they run at compile time so you're because C plus plus has meta programming with with templates but it's really messy it's super messy it's it's always it was accidentally I mean different people have different interpretations my interpretation is that it was made accidentally powerful it was not designed to be Turing complete for example but that was discovered kind of along the way accidentally um and so there have been a number of languages in the space and so they usually have templates or code instantiation code copying features of various sorts um some more modern languages or some more newer languages let's say like you know they're fairly unknown like Zig for example says okay well let's take all of those types you can run it all those things you can do at runtime and allow them to happen at compile time and so one of the problems with C plus plus I mean which is one of one of the problems with C plus plus there we go is wrong words we're going to offend everybody too that's okay I mean everybody hates me for a variety of reasons anyways I am sure right I've written enough the way they show love I have written enough C plus plus code to earn a little bit of grumpiness with C plus plus but um but one of the problems with it is that the meta programming system templates is just a completely different universe from the normal runtime programming world and so if you do meta programming and programming it's just like a different universe different syntax different concepts different stuff going on and so again one of our goals with Mojo is to make things really easy to use easy to learn and so there is a natural stepping stone and so as you do this you say okay well I have to do programming at runtime after programming at compile time why are these different things how hard is that to pull it out because that sounds to me as a fan of meta programming in C plus plus even how how hard is it to pull that off that sounds really really exciting because you can do the same style programming at compile time and at runtime that's really really exciting and so I mean in terms of the compiler implementation details it's hard I won't be shy about that it's super hard it requires I mean what Mojo has underneath the covers is a completely new approach to the design of the compiler itself and so this builds on these technologies like MLIR that you mentioned that also includes other like caching and other interpreters and JIT compilers and other stuff like you have like an interpreter inside the within the compiler yes and so it really takes the standard model of programming languages and kind of twists it and unifies it with the runtime model right which I think is really cool and to me the value of that is that again many of these languages have meta programming features like they grow macros or something right list right I know your roots right you know and this is a powerful thing right and so you know if you go back to list one of the most powerful things about is that it said that the meta programming and the programming are the same right and so that made it way simpler way more consistent way easier to understand reason about and it made it more composable so if you build a library you can use it both at runtime and compile time which is pretty cool yeah and and for machine learning I think meta programming I think we could generally say is extremely useful and so you you get features I mean I'll jump around but there is the feature of auto tuning and adaptive compilation just blows my mind well so okay so let's come back to that all right so so what what is what is what is machine learning like what is a machine learning model like you take a PyTorch model off the internet right um it's really interesting to me because what a what PyTorch and what TensorFlow and all these frameworks are kind of pushing compute into is they're pushing into like this abstract specification of a compute problem which then gets mapped in a whole bunch of different ways right and so this is why it became a meta programming problem is that you want to be able to say cool I have I have this neural net now run it with batch size a thousand right do do do a mapping across batch or okay I want to take this problem now running across a thousand CPUs or GPUs right and so like this this problem of like describe the compute and then map it and do things and transform it or or like actually it's very profound and that's one of the things that makes machine learning systems really special maybe can you describe auto tuning and how do you pull off I mean I guess adaptive compilation is what we're talking about as meta programming how do you pull off auto tuning I mean is that is that as profound as I think it is it seems like a really like you know we mentioned list comprehensions to me from a quick glance at Mojo which by the way I have to absolutely like dive in as I realize how amazing this is I absolutely must dive in that looks like just an incredible feature for machine learning people yeah well so so what is auto tuning so take a step back auto tuning is a feature in Mojo it's not so very very little of what we're doing is actually research like many of these ideas have existed in other systems in other places and so what we're doing is we're pulling together good ideas remixing them and making them into a hopefully a beautiful system right and so auto tuning the observation is that turns out hardware systems algorithms are really complicated turns out maybe you don't actually want to know how the hardware works right a lot of people don't right and so there are lots of really smart hardware people I know a lot of them where they know everything about okay the the cache size is this and the number of registers is that and if you use this what length of vector it's going to be super efficient because it maps directly onto what it can do and like all this kind of stuff or the GPU has SMs and it has a warp size of whatever right all this stuff that goes into these things or the tile size of a TPU is one twenty eight like these these factoids right my belief is that most normal people and I love hardware people also I am not trying to offend literally everybody in the internet but most programmers actually don't want to know this stuff right and so if you come at it from the perspective of how do we allow people to build both more abstracted but also more portable code because you know could be that the vector length changes or the cache size changes or it could be that the tile size of your matrix changes or the number you know an A100 versus an H100 versus a Volta versus a whatever GPU have different characteristics right a lot of the algorithms that you run are actually the same but the parameters these magic numbers you have to fill in end up being really fiddly numbers that an expert has to go figure out and so what auto tuning does is says okay well guess what there is a lot of compute out there right so instead of having humans go randomly try all the things or do a grid search or go search some complicated multidimensional space how about we have computers do that right and so what auto tuning does is you can say hey here is my algorithm if it's a a matrix operation or something like that you can say okay I'm going to carve it up into blocks I'm going to do those blocks in parallel and I want this this with one hundred twenty eight things that I'm running on I want to cut it this way or that way or whatever and you can say hey go see which one's actually empirically better on the system and then the result of that you cache for that system you save it and so come back to twisting your compiler brain right so not only does the compiler have an interpreter that uses to do meta programming that compiler that interpreter that meta programming now has to actually take your code and go run it on a target machine see see which one it likes the best and then stitch it in and then keep going right so part of the compilation is machine specific yeah well so I mean this is an optional feature right so you don't have to use it for everything but yeah if if you're so one one of one of the things that we're in the quest of is ultimate performance yes right ultimate performance is important for a couple of reasons right so if you are an enterprise you are looking to save costs and compute and things like this ultimate performance translates to you know fewer servers like if you care about the environment hey better performance leads to more efficiency right I mean you could joke and say like you know Python is bad for the environment right and so if you move to Mojo it's like at least tenx better just out of the box and keep going right um but but performance is also interesting because it leads to better products and so in the space of machine learning right if you reduce the latency of a model so it runs faster so every time you query the server running the model it takes less time well then the product team can go and make the model bigger well that actually makes it so you have a better experience as a customer and so a lot of people care about that so for auto tuning for like tile size you mentioned one twenty eight for TPU you would specify like a bunch of options to try just in the code just simple statement and then you can just set and forget and know depending wherever it compiles it'll actually be the fastest and yeah exactly and the beauty of this is that it helps you in a whole bunch of different ways right so if you are building so often what will happen is that you know you've written a bunch of software yourself right you you wake up one day you say I have an idea I'm going to go cut up some code I get to work I forget about it I move on with life I come back six months or a year or two years or three years later you dust it off and you go use it again in a new environment and maybe your GPU is different maybe you are running on a server instead of a laptop maybe whatever right and so the problem now is you say okay well I mean again not everybody cares about performance but if you do you say okay well I want to take advantage of all these new features I don't want to break the old thing though right and so the typical way of handling this kind of stuff before is you know if you are talking about C plus plus templates or you are talking about C with macros you end up with if defs you get like all these weird things get layered in make the code super complicated and then how do you test it right becomes this this crazy complexity multidimensional space you have to worry about and you know that just doesn't scale very well actually let me just jump around before I go to some specific features like the increase in performance here that we're talking about can be just insane you write that Mojo can provide a thirty five thousand x speed up over Python how does it do that yeah so you can even do more but we'll get to that so so first of all when we say that we're talking about what's called C Python it's the default Python that everybody uses when you type Python three that's like typically the one you use right C Python is an interpreter and so interpreters they have an extra layer of like byte codes and things like this that they have to go read parse interpret and it makes them kind of slow from that perspective and so one of the first things we do is we move to a compiler and so just moving to a compiler getting the interpreter out of the loop is two to five to tenx speed up depending on the code so just out of the gate just using more modern techniques right now if you do that one of the things you can do is you can start to look at how C Python started to lay out data and so one of the things that that C Python did and this isn't part of the Python spec necessarily but this is just sets of decisions is that if you take an integer for example it'll put it in an object because in Python everything is an object and so they do the very logical thing of keeping the memory representation of all objects the same so all objects have a header they have like payload data and and what this means is that every time you pass around an object you are passing around a pointer to the data well this has overhead right turns out that modern computers don't like chasing pointers very much and things like this it means that you have to allocate the data means you have to reference count it which is another way that Python uses to keep track of memory and so this has a lot of overhead and so if you say okay let's try to get that out of the heap out of a box out of an indirection and into the registers that's that's another tenx so it adds up if you if you are reference counting every single every every single thing you create that adds up yeah and if you look at you know people complain about the Python gil this is one of the things that hurts parallelism that's because of the reference counting right and so the gil and reference counting are very tightly intertwined in Python it's not the only thing but it's very tightly intertwined and so then you lean into this and you say okay cool well modern computers they can do more than one operation at a time and so they have vectors what is a vector well a vector allows you to take one instead of taking one piece of data doing an add or a multiply and then pick up the next one you can now do a four or eight or sixteen or thirty two at a time right well Python doesn't expose that because of reasons and so now you can say okay well you can adopt that now you have threads now you have like additional things like you can control memory hierarchy and so what Mojo allows you to do is it allows you to start taking advantage of all these powerful things that have been built into the hardware over time and it gives the library gives very nice features so you can say just parallelize do this in parallel right so it's very very powerful weapons against slowness which is why people have been I think having fun like just taking code and making it go fast because it's just kind of an adrenaline rush to see like how fast you can get things before I talk about some of the interesting stuff with parallelization all that let's let's first talk about like the basics we talk to indentation right so this thing looks like Python it's sexy and beautiful like Python as we mentioned is it a typed language so what's the role of types yeah good question so Python has types it has strings it has integers it has dictionaries and like all that stuff but they all live at runtime right and so because all those types live at runtime in Python you never you don't have to spell them Python also has like this whole typing thing going on now and a lot of people use it yeah I am not talking about that that's that's kind of a different thing we can go back to that if you want but but typically the you know you just say I take I have a def and my def takes two parameters I am going to call them a and b and I don't have to write a type okay so that is great but what that does is that forces what's called a consistent representation so these things have to be a pointer to an object with the object header and they all have to look the same and then when you dispatch a method you go through all the same different paths no matter what the the receiver whatever that type is so what Mojo does is it allows you to have more than one kind of type and so what it does is allows you to say okay cool I have I have an object and objects behave like Python does and so it's fully dynamic and that's all great and for many things classes like that's all very powerful and very important but if you want to say hey it's an integer and it's thirty two bits or it's six four bits or whatever it is or it's a floating point value and it's six four bits well then the compiler can take that and it can use that to do way better optimization and turns out again getting rid of the indirections it's huge means you can get better code completion because you have because compiler knows what the type is and so it knows what operations work on it and so that's actually pretty huge and so what Mojo does is allows you to progressively adopt types into your program and so you can start again it's compatible with Python and so then you can add however many types you want wherever you want them and if you don't want to deal with it you don't have to deal with it right and so one of one of you know our opinions on this is that it's not that types are the right thing or the wrong thing it's that they're a useful thing wait so it's kind of optional it's not strict typing you don't have to specify a type exactly okay so it's starting from the thing that Python is kind of reaching towards right now with

本集资源与出处