PyTorch:一种参考语言
PyTorch: A Reference Language

原始链接: https://docs.pytorch.org/devlogs/compiler/2026-07-25-pytorch-a-reference-language/

Edward Z. Yang 提出,我们需要转变处理深度学习架构的方式,即区分“参考实现”与“生产实现”。 传统上,PyTorch 同时承担了这两种角色。然而,随着扩展需求对高度优化、定制化内核的依赖日益增加,这种双重责任成为了瓶颈。Yang 提出了一种新范式: 1. **参考实现**:保留可读的“即时模式”(eager-mode)PyTorch 代码作为功能规范。它作为正确性的基准。 2. **生产实现**:利用人工智能生成的代码或内核领域特定语言(DSL)来编写相同逻辑的高性能版本。 3. **验证器**:不再依赖脆弱的编译器图匹配,而是使用自动化验证来确保优化后的代码在数学上与参考实现等价。 通过将两者解耦,开发者可以在不牺牲 PyTorch 清晰度和易调试性的前提下,获得在大规模场景下优化和融合操作的灵活性。这种方法使 PyTorch 能够继续作为业界的“参考语言”,同时将高性能执行的复杂性转移到专门的、可验证的生产实现中。最终,这平衡了即时模式开发的便捷性与图级抽象的高性能。

这篇 Hacker News 讨论探讨了一个具有启发性的观点:“将 PyTorch 作为参考语言”。讨论的焦点在于,人工智能驱动的代码生成是否会让传统的编译器开发变得过时。 支持这种转变的人认为,PyTorch 已经成为机器学习研究的通用语言(lingua franca),工具开发应该适应研究人员的自然工作流程,而不是强迫他们去适应底层复杂的系统限制。一些参与者认为,随着大语言模型(LLM)的进步,它们可能会通过高效处理局部内核级任务,来取代对传统手动优化的需求。 然而,许多贡献者捍卫了编译器持续存在的意义。他们认为,对于全局程序分析——例如管理内存分配、调度和优化跨内核数据流——编译器仍然至关重要,这是局部人工智能优化难以轻易复制的。其他人则指出,人工智能与编译器已经在融合,基于机器学习的启发式算法正在成为现代优化的标准。 最终,共识倾向于认为:虽然大语言模型将承担越来越多的代码生成工作,但编译器作为高性能执行的关键验证者和赋能者,仍将持续存在。该领域的未来很可能在于一种混合方法,即既利用 PyTorch 的表达灵活性,又利用传统编译器技术的结构严谨性。
相关文章

原文

Edward Z. Yang (@ezyang) · July 25, 2026 · 4 min read
compilertorch.compileautogradverificationllm

A reference implementation is a simplified but complete version of a system that trades performance in return for clarity. We might then say a reference “language” is the fabric of APIs and conventions from which these implementations are cut. At first glance, PyTorch obviously is a reference language: it is, after all, commonly called the lingua franca of modern deep learning. But upon a closer look, there is confusion:

  • Reference implementations usually aren’t deployed to production. But I do my training jobs with PyTorch!

  • Everyone’s writing kernels with kernel DSLs. What is the role of PyTorch if it’s just gluing kernels together?

  • AI coding will eventually mean that any stack can be rewritten from scratch; why does being written in PyTorch matter?

So for me, recently, an unusually clarifying perspective has been to think of PyTorch as playing a dual role: as both the reference language and the implementation language. When the scale is not too large or the compiler is working well, the reference implementation can ship to production. But increasingly, I think it will be more and more natural to think of the reference implementation as a software artifact that stands apart from the actual production implementation, by which we can verify the correctness of the production implementation. One implementation to research in, one implementation to scale with, and one verifier to, in the darkness, bind them.

The clearest demonstration of this is in the modern usage of kernel DSLs. The traditional, compiler-maximalist view argues that end users should write implementations of NN modules using a high level API (e.g., a Numpy/PyTorch-style API) which a compiler then determines how to compile into an optimized form. But for the most important operations like matrix multiplies and attention, it is not easy for compilers to guarantee peak performance; the proliferation of kernel DSLs has made it dramatically simpler for people to achieve optimal performance by explicitly spelling out tiling and data movement. Does this eliminate the high level API? Usually not: it’s pretty useful to have a reference implementation in plain PyTorch, and most kernel authors will maintain one in parallel with the optimized kernel, verifying correctness with numerical tests.

In the same way kernel DSLs have changed how production implementations of operators can be written, I think coding agents change the way production implementations of train steps can be written. Traditionally, we think of autograd as a core part of PyTorch’s value proposition, because it guarantees you will get correct derivatives. However, at scale, the implicit backwards graph becomes an albatross around one’s neck: the majority of your compute is hidden away, with no opportunity to interact with it with normal debugging tools or apply fusions to it in the same way you can do it in eager forwards code. With a compiler, it is possible to modify the backwards graph with, e.g., a pattern match, but this is brittle and a less nice experience than just swapping a call from a reference implementation to a hand-written kernel. This is not a new observation: the now defunct Tangent library was built on the proposition that source-to-source automatic differentiation could be useful.

The new recipe looks like this. Keep the traditional PyTorch autograd-friendly code as the reference implementation. Use LLMs to generate an explicit forward-backward version of the code, which can be optimized separately from the reference implementation. Unlike pattern matching, you never have to worry about your optimizations failing to apply. The cost is that the reference and the real implementation can diverge: we need a verifier that shows us they are equivalent. This verifier can be implemented simply with a bitwise equivalence test, or implemented as some sort of graph capture and structural equivalence, in the tradition of translation validation. To ensure the verifier works when one side has a fusion the other doesn’t, you only need to provide a reference implementation of the fusion (an inverse pattern match, if you will!).

I am not going to claim that this recipe is right for everyone. It turns out PyTorch, the reference language, is a pretty good executable spec, and at the end of the day what really matters is how quickly you get the experimental results you need. But, having spent a lot of my time recently thinking about what it means for PyTorch to excel at frontier training–and in particular whether or not it is necessary for PyTorch to disrupt itself as scaling continues–I feel that this perspective helps bridge the old and the new. An open question Horace He posed last year was this: “How can we get all of the control of eager-mode execution with some of the conveniences of graph-level abstraction?” I think this recipe is a pretty promising answer, and PyTorch continues to be at the center of it.

联系我们 contact @ memedata.com