怡心湖

PyTorch是如何变成“AI界的POSIX”的 How PyTorch Became the "POSIX of AI"

PyTorch是如何变成“AI界的POSIX”的

How PyTorch Became the "POSIX of AI"

编者按 / Editor’s Note

在CUDA构筑的“计算封建制”高墙之下,一场静悄悄的革命正在发生。英伟达控制了GPU硬件,但PyTorch正在控制“如何用GPU”。它不直接生产芯片,却定义了几乎所有AI模型的源代码结构;它不销售算力,却通过抽象层把硬件厂商的护城河变成了“可插拔的后端”。这种“定义接口、而非占领设备”的策略,像极了当年Unix世界的POSIX标准——让软件无视底层硬件差异,自由流淌。本文将深度拆解PyTorch是如何一步步篡夺芯片厂商的生态权力,成为AI界事实上的“可移植操作系统接口”(POSIX)的。

Beneath the towering walls of NVIDIA's "computational feudalism," a quiet revolution is underway. NVIDIA controls the GPU hardware, but PyTorch is mastering "how to use the GPU." It doesn't manufacture chips, yet it defines the source code structure of virtually every AI model; it doesn't sell compute power, yet it transforms hardware vendors' moats into "pluggable backends" via abstraction layers. This strategy—"defining interfaces, not occupying devices"—bears a striking resemblance to the POSIX standard in the Unix world: allowing software to flow freely regardless of underlying hardware differences. This article dissects how PyTorch has gradually usurped the ecological power of chipmakers, becoming the de facto "Portable Operating System Interface" (POSIX) of the AI realm.


一、 POSIX时刻:当AI模型开始“无视”硬件

I. The POSIX Moment: When AI Models Begin to "Ignore" Hardware

在计算机历史上,POSIX标准的出现解决了“软件移植噩梦”。在POSIX之前,为IBM大型机写的程序无法在DEC小型机上运行,开发者必须重写代码。POSIX定义了一套操作系统应该提供的API(如fork(), read(), write()),从此,只要系统遵守POSIX,软件就能无缝迁移。

In computing history, the emergence of the POSIX standard solved the "software portability nightmare." Before POSIX, programs written for IBM mainframes couldn't run on DEC minicomputers; developers had to rewrite code. POSIX defined a set of APIs an OS should provide (e.g., fork(), read(), write()). Henceforth, as long as a system complied with POSIX, software could migrate seamlessly.

PyTorch正在AI领域复刻这一幕:

五年前,写一个PyTorch模型几乎等于写“CUDA代码”。如果你想在AMD显卡或英特尔Gaudi上运行,必须手动修改底层Kernel,痛苦不堪。

今天,情况变了。Meta的Llama 3、DeepSeek-V3、OpenAI的开源权重,它们都有一个共同点:代码里没有一行CUDA。

开发者写的仍然是tensor.matmul()loss.backward()optimizer.step()。至于这些指令最终是在NVIDIA的Tensor Core上执行,还是在AMD的Matrix Core、Intel的AMX单元,或是华为的达芬奇核上运行,PyTorch已经替你屏蔽了。

PyTorch is replicating this scene in AI:

Five years ago, writing a PyTorch model was almost synonymous with writing "CUDA code." If you wanted to run it on an AMD card or an Intel Gaudi, you had to manually tweak the underlying kernels—a painful ordeal.

Today, things have changed. Meta's Llama 3, DeepSeek-V3, and OpenAI's open weights share a common trait: there isn't a single line of CUDA in their code.

Developers still write tensor.matmul(), loss.backward(), and optimizer.step(). Whether these instructions execute on NVIDIA's Tensor Cores, AMD's Matrix Cores, Intel's AMX units, or Huawei's Da Vinci cores is now abstracted away by PyTorch.

这就是AI界的POSIX时刻:硬件差异被抽象层吞没,算法逻辑成为唯一的主角。

This is the POSIX Moment for AI: hardware differences are swallowed by the abstraction layer, leaving algorithmic logic as the sole protagonist.


二、 解剖PyTorch:三层架构的“劫持”艺术

II. Anatomy of PyTorch: The Art of "Hijacking" via Three-Tier Architecture

PyTorch之所以能成为POSIX,核心在于其精妙的三层架构设计。它不是简单地做一个翻译器,而是构建了一个从“数学语义”到“硬件指令”的完整编译管道。

The core reason PyTorch became POSIX lies in its exquisite three-tier architecture. It doesn't merely act as a translator; it constructs a complete compilation pipeline from "mathematical semantics" to "hardware instructions."

1. 前端层(Python API):算法的“普通话”

这是开发者直接接触的部分。torch.Tensor, nn.Module, autograd。这一层极其稳定,十年来变化不大。它定义了AI开发的“语法”。

关键策略:​ 保持极简。不让硬件细节污染前端。就像POSIX的printf不需要知道显示器是CRT还是LCD,PyTorch的conv2d不需要知道卷积是在哪款芯片上算的。

1. Frontend Layer (Python API): The "Mandarin" of Algorithms

This is the layer developers interact with directly: torch.Tensor, nn.Module, autograd. This layer is remarkably stable, with minimal changes over the past decade. It defines the "syntax" of AI development.

Key Strategy:​ Keep it minimalist. Prevent hardware details from polluting the frontend. Just as POSIX's printf doesn't need to know if the monitor is CRT or LCD, PyTorch's conv2d doesn't need to know which chip executes the convolution.

2. 中间表示层(Intermediate Representation, IR):通往未来的桥梁

这是PyTorch最关键的“中场发动机”。主要包括TorchScriptATen(A Tensor Library)

  • ATen:​ 这是PyTorch的“汇编语言”。它定义了约2000个基础算子(Operation),如加法、卷积、归一化等。所有硬件厂商想要接入PyTorch,只需要实现这2000个算子。

  • Dynamo & Inductor:​ 这是PyTorch 2.0的革命性创新。torch.compile利用Dynamo捕获Python代码,将其转换为FX Graph(一种计算图IR),然后通过Inductor编译优化。

    权力转移:​ 以前,算子是用CUDA C++写的(.cu文件)。现在,Inductor可以将计算图转换为Triton语言(一种类似Python的DSL)或者直接生成目标硬件的机器码。这意味着,PyTorch掌握了“代码生成”的权力,而不仅仅是“调度”的权力。

2. Intermediate Representation (IR) Layer: The Bridge to the Future

This is the critical "midfield engine" of PyTorch. It primarily comprises TorchScript​ and ATen (A Tensor Library).

  • ATen:​ This is PyTorch's "assembly language." It defines approximately 2,000 primitive operators (Ops), such as addition, convolution, and normalization. For hardware vendors to integrate with PyTorch, they only need to implement these 2,000 ops.

  • Dynamo & Inductor:​ This is the revolutionary innovation in PyTorch 2.0. torch.compile uses Dynamo to capture Python code, converts it into an FX Graph (a computational graph IR), and optimizes it via Inductor.

    Power Shift:​ Previously, operators were written in CUDA C++ (.cu files). Now, Inductor can convert the computational graph into Triton​ (a Python-like DSL) or directly generate machine code for the target hardware. This means PyTorch wields the power of "code generation," not just "dispatch."

3. 后端层(Dispatch & Backend):硬件的“方言翻译”

这是PyTorch的“设备驱动”层。通过Dispatcher机制,PyTorch可以根据Tensor所在的设备(CPU, CUDA, MPS, XPU, NPU...),动态地将算子调用路由到对应的内核实现。

  • PrivateUse1:​ PyTorch预留了扩展接口,允许厂商注册自定义后端(如Intel的XPU,华为的NPU)。

  • CUDA Fallback:​ 如果某个算子没有针对新硬件的实现,PyTorch甚至可以尝试回退到CPU或CUDA(尽管性能不佳)。

    生态绑架:​ 对于芯片厂商来说,如果不给PyTorch写Backend,你的芯片在AI界就等于“不存在”。这迫使所有硬件厂商(AMD, Intel, Qualcomm, Huawei, AWS)不得不投入巨资维护PyTorch适配器。PyTorch由此获得了定义硬件交互规范的权力。

3. Backend Layer (Dispatch & Backend): The "Dialect Translation" for Hardware

This is PyTorch's "device driver" layer. Via the Dispatcher​ mechanism, PyTorch dynamically routes operator calls to corresponding kernel implementations based on the tensor's device (CPU, CUDA, MPS, XPU, NPU...).

  • PrivateUse1:​ PyTorch reserves extension interfaces, allowing vendors to register custom backends (e.g., Intel's XPU, Huawei's NPU).

  • CUDA Fallback:​ If an operator lacks an implementation for new hardware, PyTorch can even attempt to fall back to CPU or CUDA (albeit with poor performance).

    Ecosystem Capture:​ For chip vendors, failing to write a PyTorch backend renders their chip "invisible" in the AI world. This forces all hardware vendors (AMD, Intel, Qualcomm, Huawei, AWS) to invest heavily in maintaining PyTorch adapters. Thus, PyTorch acquires the power to define hardware interaction specifications.


三、 围剿CUDA:Triton与编译器战争的胜利

III. Besieging CUDA: The Victory of Triton and the Compiler Wars

如果说ATen是阵地战,那么Triton就是PyTorch发动的“闪电战”。

If ATen represents positional warfare, Triton is the "blitzkrieg" launched by PyTorch.

1. CUDA的软肋

CUDA难学、难写、难调试,且绑定英伟达硬件。对于AI研究者来说,写CUDA是为了“让模型跑起来”,而不是为了“学习CUDA语法”。这是巨大的认知负担。

1. CUDA's Achilles' Heel

CUDA is hard to learn, write, and debug, and it's tied to NVIDIA hardware. For AI researchers, writing CUDA is a means to "make the model run," not an end to "learn CUDA syntax." This imposes a massive cognitive burden.

2. Triton的降维打击

Triton是一种接近Python的高级语言,专门用于编写GPU内核。它隐藏了线程块、共享内存、寄存器分配等底层细节。更重要的是,Triton是硬件中立的

PyTorch 2.0的Inductor默认使用Triton生成内核代码。当开发者调用torch.compile时,PyTorch会自动将Python代码转换成Triton代码,再由Triton编译器针对不同硬件生成PTX(N卡)或AMDGPU ISA(A卡)。

后果:​ 英伟达引以为傲的CUDA Kernel库(如cutlass)不再是必需品。AI社区开始用Triton重写一切高性能算子。这不仅削弱了CUDA的粘性,还让AMD显卡第一次在软件易用性上追平了N卡。

2. Triton's Dimensionality Reduction Strike

Triton is a Python-esque high-level language designed specifically for writing GPU kernels. It abstracts away low-level details like thread blocks, shared memory, and register allocation. Crucially, Triton is hardware-agnostic.

PyTorch 2.0's Inductor defaults to using Triton for kernel code generation. When a developer invokes torch.compile, PyTorch automatically converts Python code into Triton code, which the Triton compiler then translates into PTX (for NVIDIA) or AMDGPU ISA (for AMD).

Consequence:​ NVIDIA's prized CUDA kernel libraries (like CUTLASS) are no longer indispensable. The AI community is rewriting high-performance operators in Triton. This not only weakens CUDA's stickiness but also allows AMD cards to match NVIDIA's software usability for the first time.


四、 开源中立:Meta的阳谋与UXL的合围

IV. Open Source Neutrality: Meta's Grand Strategy and the UXL Encirclement

PyTorch由Meta主导,但采取了基金会托管模式(Linux Foundation)。这避免了谷歌TensorFlow因“母公司控制”而遭忌惮的命运。

PyTorch is spearheaded by Meta but operates under the Linux Foundation's stewardship. This avoids the fate of Google's TensorFlow, which faced skepticism due to perceived "parent company control."

1. 标准制定者的红利

作为AI框架的事实标准,PyTorch拥有定义“什么是算子”、“什么是张量”、“什么是梯度”的权力。任何试图挑战这一标准的行为(如谷歌的JAX试图推广XLA),都会因为生态碎片化而举步维艰。

1. Dividends of Standard Setting

As the de facto standard for AI frameworks, PyTorch dictates what constitutes an "operator," a "tensor," and a "gradient." Any attempt to challenge this standard (such as Google's JAX promoting XLA) stumbles due to ecosystem fragmentation.

2. UXL基金会的合围

为了彻底摆脱CUDA,Intel联合Arm、高通、三星成立了UXL基金会,旨在推广oneAPI标准。但他们并没有选择另起炉灶,而是选择了拥抱PyTorch

UXL的目标是将PyTorch的ATen算子映射到oneAPI的DPC++实现上。这意味着,PyTorch不仅是AI界的POSIX,还是硬件厂商组建“反CUDA联盟”的共同基石。

2. The UXL Foundation's Encirclement

To completely break free from CUDA, Intel, alongside Arm, Qualcomm, and Samsung, established the UXL Foundation to promote the oneAPI standard. Instead of building a rival stack, they chose to embrace PyTorch.

The UXL's goal is to map PyTorch's ATen operators to oneAPI's DPC++ implementations. This implies that PyTorch is not only the POSIX of AI but also the common bedrock for hardware vendors forming an "anti-CUDA alliance."


五、 结语:接口即权力

V. Conclusion: Interfaces Are Power

在传统的计算世界里,Intel定义了x86指令集,微软定义了Win32 API,二者联手统治了PC时代。在移动时代,ARM定义了指令集,谷歌定义了Android API。

In the traditional computing world, Intel defined the x86 instruction set, and Microsoft defined the Win32 API; together, they ruled the PC era. In the mobile era, ARM defined the instruction set, and Google defined the Android API.

在AI时代,英伟达试图通过CUDA定义指令集,但PyTorch截胡了API的定义权。

PyTorch成为了AI模型的“操作系统内核”。它让算法开发者只关心数学,让硬件厂商只关心执行效率。它不拥有硬件,却决定了硬件如何被使用;它不生产数据,却定义了数据如何流动。

In the AI era, NVIDIA attempts to define the instruction set via CUDA, but PyTorch has intercepted the authority to define the API.

PyTorch has become the "OS kernel" for AI models. It allows algorithm developers to focus solely on mathematics and hardware vendors to concentrate on execution efficiency. It doesn't own the hardware, yet it dictates how hardware is utilized; it doesn't produce data, yet it defines how data flows.

当未来某一天,AI开发者在简历上写下“精通PyTorch”而非“精通CUDA”时,我们就知道,权力的交接已经完成。PyTorch已经成为了那个让所有硬件厂商既爱又恨的“AI界POSIX”——爱其生态之广,恨其釜底抽薪。

When, in the future, AI developers list "Proficient in PyTorch" on their resumes instead of "Proficient in CUDA," we will know the torch has been passed. PyTorch has become the "POSIX of AI"—loved by hardware vendors for its vast ecosystem, yet resented for snatching away their foundation.


词汇表 / Glossary

  • POSIX (Portable Operating System Interface):​ 可移植操作系统接口,IEEE制定的一系列标准,旨在保证软件在不同Unix-like系统间的可移植性。

  • ATen (A Tensor Library):​ PyTorch的C++张量运算库,包含了所有后端必须实现的原生算子集合。

  • Triton:​ 一种开源的类似于Python的编程语言,用于编写高效的GPU内核,由OpenAI发起,现为PyTorch编译栈的核心组件。

  • Inductor:​ PyTorch 2.0引入的默认编译器后端,负责将TorchScript/FX Graph转换为高效的机器码(通常通过Triton)。

  • Dispatcher:​ PyTorch中的算子分发机制,根据输入张量的类型和设备,将函数调用路由到正确的实现函数。

  • UXL Foundation:​ 一个致力于定义异构编程开放标准的行业组织,旨在提供CUDA的替代方案,核心项目是oneAPI。

此文由 怡心湖 编辑,若您觉得有益,欢迎分享转发!:首页 > 常识论 » PyTorch是如何变成“AI界的POSIX”的 How PyTorch Became the "POSIX of AI"

()
分享到: