AI 编译器的五大挑战与未来:从 TVM、XLA 到自动并行
上一篇我们拆开了 AI 编译器的通用架构:前端图优化 + 后端算子优化 + 两级 IR。
这篇换个视角------看真实产品 长什么样,以及这套架构在 2024 年之后卡在哪、往哪走。
文章目录
- [AI 编译器的五大挑战与未来:从 TVM、XLA 到自动并行](#AI 编译器的五大挑战与未来:从 TVM、XLA 到自动并行)
-
- [一、五家主流 AI 编译器横向对比](#一、五家主流 AI 编译器横向对比)
- 二、逐个看:五家的设计选择
-
- [2.1 TVM:两层分工,最"教科书"](#2.1 TVM:两层分工,最"教科书")
- [2.2 XLA:把大算子拆开,再拼回去](#2.2 XLA:把大算子拆开,再拼回去)
- [2.3 nGraph:Intel 的"推理+训练"基石](#2.3 nGraph:Intel 的"推理+训练"基石)
- [2.4 TC:用多面体模型干掉"手写 Schedule"](#2.4 TC:用多面体模型干掉"手写 Schedule")
- [2.5 Glow:主打推理的"运行时优化派"](#2.5 Glow:主打推理的"运行时优化派")
- 三、五大挑战
-
- [3.1 挑战一:动态 Shape 泛化](#3.1 挑战一:动态 Shape 泛化)
- [3.2 挑战二:Python 编译静态化](#3.2 挑战二:Python 编译静态化)
- [3.3 挑战三:充分发挥硬件性能](#3.3 挑战三:充分发挥硬件性能)
- [3.4 挑战四:特殊算法优化方法](#3.4 挑战四:特殊算法优化方法)
- [3.5 挑战五:易用性与性能兼顾](#3.5 挑战五:易用性与性能兼顾)
- 四、三个值得认真想的开放问题
-
- Q1:图算能否统一表达和优化?
- Q2:完全自动并行是否可行?
- [Q3:AI 芯片需要(AI)编译器吗?](#Q3:AI 芯片需要(AI)编译器吗?)
- [五、AI 编译器的五个未来方向](#五、AI 编译器的五个未来方向)
- 六、小结
- 参考资料
一、五家主流 AI 编译器横向对比
业界常被拿来横向对比的有五家:TVM、XLA、nGraph、Tensor Comprehensions(TC)、Glow。
先给一张总表(后文逐个展开):
| 编译器 | 出身 | 核心分层 | 调度方式 | 特色 | 现状 |
|---|---|---|---|---|---|
| TVM | Apache 基金会(开源社区) | Relay(图)+ TVM(算子) | Compute / Schedule 分离 | 跨平台极广,可扩展性强 | 活跃 |
| XLA | HLO → LLVM IR | 手工指定优化 Pass | 与 TensorFlow/JAX 深度集成,透明加速 | 活跃(OpenXLA) | |
| nGraph | Intel | 图 → 后端代码 | 图优化 + 硬件后端 | 面向 Intel 硬件(CPU/GPU/FPGA) | 已并入 OpenVINO 生态 |
| TC | Meta(原 Facebook) | DSL → GPU kernel | 多面体模型自动调度 | 自动生成 schedule,减少手工调优 | 已归档,停止维护 |
| Glow | Meta | 图 → 后端 | 图优化 + 运行时优化 | 主打推理,内存预分配/异步/低精度 | 早期活跃,现已边缘化 |
先破一个常见误解 :这张对比图出自 2020 年的综述 The Deep Learning Compiler: A Comprehensive Survey 。它记录的是那个时间点的格局,不是今天的。TC 和 Glow 都已淡出,XLA 变成了 OpenXLA,TVM 依然是学术/工业两栖的主力。
二、逐个看:五家的设计选择
2.1 TVM:两层分工,最"教科书"
TVM 的架构几乎是上一篇文章的"标准答案":
#mermaid-svg-b8dCcO6XRArTpA6Z{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-b8dCcO6XRArTpA6Z .error-icon{fill:#552222;}#mermaid-svg-b8dCcO6XRArTpA6Z .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-b8dCcO6XRArTpA6Z .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-b8dCcO6XRArTpA6Z .marker{fill:#333333;stroke:#333333;}#mermaid-svg-b8dCcO6XRArTpA6Z .marker.cross{stroke:#333333;}#mermaid-svg-b8dCcO6XRArTpA6Z svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-b8dCcO6XRArTpA6Z p{margin:0;}#mermaid-svg-b8dCcO6XRArTpA6Z .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-b8dCcO6XRArTpA6Z .cluster-label text{fill:#333;}#mermaid-svg-b8dCcO6XRArTpA6Z .cluster-label span{color:#333;}#mermaid-svg-b8dCcO6XRArTpA6Z .cluster-label span p{background-color:transparent;}#mermaid-svg-b8dCcO6XRArTpA6Z .label text,#mermaid-svg-b8dCcO6XRArTpA6Z span{fill:#333;color:#333;}#mermaid-svg-b8dCcO6XRArTpA6Z .node rect,#mermaid-svg-b8dCcO6XRArTpA6Z .node circle,#mermaid-svg-b8dCcO6XRArTpA6Z .node ellipse,#mermaid-svg-b8dCcO6XRArTpA6Z .node polygon,#mermaid-svg-b8dCcO6XRArTpA6Z .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-b8dCcO6XRArTpA6Z .rough-node .label text,#mermaid-svg-b8dCcO6XRArTpA6Z .node .label text,#mermaid-svg-b8dCcO6XRArTpA6Z .image-shape .label,#mermaid-svg-b8dCcO6XRArTpA6Z .icon-shape .label{text-anchor:middle;}#mermaid-svg-b8dCcO6XRArTpA6Z .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-b8dCcO6XRArTpA6Z .rough-node .label,#mermaid-svg-b8dCcO6XRArTpA6Z .node .label,#mermaid-svg-b8dCcO6XRArTpA6Z .image-shape .label,#mermaid-svg-b8dCcO6XRArTpA6Z .icon-shape .label{text-align:center;}#mermaid-svg-b8dCcO6XRArTpA6Z .node.clickable{cursor:pointer;}#mermaid-svg-b8dCcO6XRArTpA6Z .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-b8dCcO6XRArTpA6Z .arrowheadPath{fill:#333333;}#mermaid-svg-b8dCcO6XRArTpA6Z .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-b8dCcO6XRArTpA6Z .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-b8dCcO6XRArTpA6Z .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-b8dCcO6XRArTpA6Z .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-b8dCcO6XRArTpA6Z .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-b8dCcO6XRArTpA6Z .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-b8dCcO6XRArTpA6Z .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-b8dCcO6XRArTpA6Z .cluster text{fill:#333;}#mermaid-svg-b8dCcO6XRArTpA6Z .cluster span{color:#333;}#mermaid-svg-b8dCcO6XRArTpA6Z div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-b8dCcO6XRArTpA6Z .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-b8dCcO6XRArTpA6Z rect.text{fill:none;stroke-width:0;}#mermaid-svg-b8dCcO6XRArTpA6Z .icon-shape,#mermaid-svg-b8dCcO6XRArTpA6Z .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-b8dCcO6XRArTpA6Z .icon-shape p,#mermaid-svg-b8dCcO6XRArTpA6Z .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-b8dCcO6XRArTpA6Z .icon-shape .label rect,#mermaid-svg-b8dCcO6XRArTpA6Z .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-b8dCcO6XRArTpA6Z .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-b8dCcO6XRArTpA6Z .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-b8dCcO6XRArTpA6Z :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} TensorFlow / PyTorch / MXNet / ONNX
Relay 层
图层全局优化
算子融合 / 常量折叠 / CSE
优化后的子图
TVM 层
算子级优化 + kernel 生成
CPU / GPU / FPGA / 加速器
| 层 | 职责 |
|---|---|
| Relay | 接入各种前端框架、做图级优化,输出优化后的子图 |
| TVM | 接收子图,逐算子做底层优化与 kernel 生成,按硬件定制 |
最关键的设计:Compute 与 Schedule 分离
#mermaid-svg-2lCU4QEsjBcafFIS{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-2lCU4QEsjBcafFIS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2lCU4QEsjBcafFIS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2lCU4QEsjBcafFIS .error-icon{fill:#552222;}#mermaid-svg-2lCU4QEsjBcafFIS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2lCU4QEsjBcafFIS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2lCU4QEsjBcafFIS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2lCU4QEsjBcafFIS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2lCU4QEsjBcafFIS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2lCU4QEsjBcafFIS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2lCU4QEsjBcafFIS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2lCU4QEsjBcafFIS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2lCU4QEsjBcafFIS .marker.cross{stroke:#333333;}#mermaid-svg-2lCU4QEsjBcafFIS svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2lCU4QEsjBcafFIS p{margin:0;}#mermaid-svg-2lCU4QEsjBcafFIS .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-2lCU4QEsjBcafFIS .cluster-label text{fill:#333;}#mermaid-svg-2lCU4QEsjBcafFIS .cluster-label span{color:#333;}#mermaid-svg-2lCU4QEsjBcafFIS .cluster-label span p{background-color:transparent;}#mermaid-svg-2lCU4QEsjBcafFIS .label text,#mermaid-svg-2lCU4QEsjBcafFIS span{fill:#333;color:#333;}#mermaid-svg-2lCU4QEsjBcafFIS .node rect,#mermaid-svg-2lCU4QEsjBcafFIS .node circle,#mermaid-svg-2lCU4QEsjBcafFIS .node ellipse,#mermaid-svg-2lCU4QEsjBcafFIS .node polygon,#mermaid-svg-2lCU4QEsjBcafFIS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2lCU4QEsjBcafFIS .rough-node .label text,#mermaid-svg-2lCU4QEsjBcafFIS .node .label text,#mermaid-svg-2lCU4QEsjBcafFIS .image-shape .label,#mermaid-svg-2lCU4QEsjBcafFIS .icon-shape .label{text-anchor:middle;}#mermaid-svg-2lCU4QEsjBcafFIS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-2lCU4QEsjBcafFIS .rough-node .label,#mermaid-svg-2lCU4QEsjBcafFIS .node .label,#mermaid-svg-2lCU4QEsjBcafFIS .image-shape .label,#mermaid-svg-2lCU4QEsjBcafFIS .icon-shape .label{text-align:center;}#mermaid-svg-2lCU4QEsjBcafFIS .node.clickable{cursor:pointer;}#mermaid-svg-2lCU4QEsjBcafFIS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-2lCU4QEsjBcafFIS .arrowheadPath{fill:#333333;}#mermaid-svg-2lCU4QEsjBcafFIS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2lCU4QEsjBcafFIS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2lCU4QEsjBcafFIS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2lCU4QEsjBcafFIS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-2lCU4QEsjBcafFIS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2lCU4QEsjBcafFIS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-2lCU4QEsjBcafFIS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2lCU4QEsjBcafFIS .cluster text{fill:#333;}#mermaid-svg-2lCU4QEsjBcafFIS .cluster span{color:#333;}#mermaid-svg-2lCU4QEsjBcafFIS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2lCU4QEsjBcafFIS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-2lCU4QEsjBcafFIS rect.text{fill:none;stroke-width:0;}#mermaid-svg-2lCU4QEsjBcafFIS .icon-shape,#mermaid-svg-2lCU4QEsjBcafFIS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2lCU4QEsjBcafFIS .icon-shape p,#mermaid-svg-2lCU4QEsjBcafFIS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-2lCU4QEsjBcafFIS .icon-shape .label rect,#mermaid-svg-2lCU4QEsjBcafFIS .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2lCU4QEsjBcafFIS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-2lCU4QEsjBcafFIS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-2lCU4QEsjBcafFIS :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Compute
这个算子算什么
(数学逻辑)
同一个算子
Schedule
怎么算最快
(分块/并行/向量化/内存布局)
针对目标硬件
生成高效 kernel
大白话 :Compute 是菜谱,Schedule 是厨房动线。菜谱不变,你可以按不同厨房(CPU/GPU)重新安排切菜、下锅的顺序,做出同样一道菜但快得多。
这个分离让 TVM 有两个能力:
- 用户能自己定义新算子(写 Compute)并指定优化策略(写 Schedule)
- 同一份算法可复用到多种硬件,只为每种硬件换 Schedule
一句话记住 TVM :图优化交给 Relay,算子优化交给 TVM,两者靠"优化后子图"衔接。
2.2 XLA:把大算子拆开,再拼回去
XLA(Accelerated Linear Algebra)是 Google 为 TensorFlow 做的线性代数领域编译器,后来也成为 JAX 的底座。
它最大的卖点是透明:不用改模型源码,直接和 TensorFlow 集成就能加速。
但它的优化过程有个很有意思的"拆了再拼"逻辑:
#mermaid-svg-ba27GL39wPapQUQ3{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ba27GL39wPapQUQ3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ba27GL39wPapQUQ3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ba27GL39wPapQUQ3 .error-icon{fill:#552222;}#mermaid-svg-ba27GL39wPapQUQ3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ba27GL39wPapQUQ3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ba27GL39wPapQUQ3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ba27GL39wPapQUQ3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ba27GL39wPapQUQ3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ba27GL39wPapQUQ3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ba27GL39wPapQUQ3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ba27GL39wPapQUQ3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ba27GL39wPapQUQ3 .marker.cross{stroke:#333333;}#mermaid-svg-ba27GL39wPapQUQ3 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ba27GL39wPapQUQ3 p{margin:0;}#mermaid-svg-ba27GL39wPapQUQ3 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ba27GL39wPapQUQ3 .cluster-label text{fill:#333;}#mermaid-svg-ba27GL39wPapQUQ3 .cluster-label span{color:#333;}#mermaid-svg-ba27GL39wPapQUQ3 .cluster-label span p{background-color:transparent;}#mermaid-svg-ba27GL39wPapQUQ3 .label text,#mermaid-svg-ba27GL39wPapQUQ3 span{fill:#333;color:#333;}#mermaid-svg-ba27GL39wPapQUQ3 .node rect,#mermaid-svg-ba27GL39wPapQUQ3 .node circle,#mermaid-svg-ba27GL39wPapQUQ3 .node ellipse,#mermaid-svg-ba27GL39wPapQUQ3 .node polygon,#mermaid-svg-ba27GL39wPapQUQ3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ba27GL39wPapQUQ3 .rough-node .label text,#mermaid-svg-ba27GL39wPapQUQ3 .node .label text,#mermaid-svg-ba27GL39wPapQUQ3 .image-shape .label,#mermaid-svg-ba27GL39wPapQUQ3 .icon-shape .label{text-anchor:middle;}#mermaid-svg-ba27GL39wPapQUQ3 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ba27GL39wPapQUQ3 .rough-node .label,#mermaid-svg-ba27GL39wPapQUQ3 .node .label,#mermaid-svg-ba27GL39wPapQUQ3 .image-shape .label,#mermaid-svg-ba27GL39wPapQUQ3 .icon-shape .label{text-align:center;}#mermaid-svg-ba27GL39wPapQUQ3 .node.clickable{cursor:pointer;}#mermaid-svg-ba27GL39wPapQUQ3 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ba27GL39wPapQUQ3 .arrowheadPath{fill:#333333;}#mermaid-svg-ba27GL39wPapQUQ3 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ba27GL39wPapQUQ3 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ba27GL39wPapQUQ3 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ba27GL39wPapQUQ3 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ba27GL39wPapQUQ3 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ba27GL39wPapQUQ3 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ba27GL39wPapQUQ3 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ba27GL39wPapQUQ3 .cluster text{fill:#333;}#mermaid-svg-ba27GL39wPapQUQ3 .cluster span{color:#333;}#mermaid-svg-ba27GL39wPapQUQ3 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ba27GL39wPapQUQ3 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ba27GL39wPapQUQ3 rect.text{fill:none;stroke-width:0;}#mermaid-svg-ba27GL39wPapQUQ3 .icon-shape,#mermaid-svg-ba27GL39wPapQUQ3 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ba27GL39wPapQUQ3 .icon-shape p,#mermaid-svg-ba27GL39wPapQUQ3 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ba27GL39wPapQUQ3 .icon-shape .label rect,#mermaid-svg-ba27GL39wPapQUQ3 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ba27GL39wPapQUQ3 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ba27GL39wPapQUQ3 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ba27GL39wPapQUQ3 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} ① 打开
拆成小算子
② 逐个优化
③ 重新融合
④ HLO → LLVM IR
原始计算图
(大算子为主)
一堆小算子
优化后的小算子
新的大算子 / 融合 kernel
目标硬件代码
原文点出了这里面最难的两步:
| 难点 | 为什么难 |
|---|---|
| ① 打开大算子 | 要深入理解计算逻辑,找到合适的切分点和拆解方式 |
| ③ 重新融合 | 要评估每个小算子的优化效果,再决定怎么融、融成什么 |
大白话 :就像把一台整机拆成零件来逐个打磨,再重新组装------拆得好不好、装得对不对,决定了最后性能。
XLA 的代价 (原文也提到了):所有优化 Pass 规则都得手工提前指定。编译器不会自己发现新优化,工程师写一条它才会一条。
术语纠正 :原文说 XLA 用 "HLO/LLO" 两种高级语言。XLA 的实际层级是 HLO (High Level Optimizer IR)→ 缓冲分配(buffer assignment)→ LLVM IR ;中间还有 LHLO/LMHLO 等变体。"LLO" 不是 XLA 官方术语,通常就是指降到 LLVM IR 这一步。
2.3 nGraph:Intel 的"推理+训练"基石
nGraph 是 Intel 的开源 DNN 编译器框架,思路是最朴素的那一版:
#mermaid-svg-tGzGfs9XnXG7q9hC{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-tGzGfs9XnXG7q9hC .error-icon{fill:#552222;}#mermaid-svg-tGzGfs9XnXG7q9hC .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-tGzGfs9XnXG7q9hC .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-tGzGfs9XnXG7q9hC .marker{fill:#333333;stroke:#333333;}#mermaid-svg-tGzGfs9XnXG7q9hC .marker.cross{stroke:#333333;}#mermaid-svg-tGzGfs9XnXG7q9hC svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-tGzGfs9XnXG7q9hC p{margin:0;}#mermaid-svg-tGzGfs9XnXG7q9hC .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-tGzGfs9XnXG7q9hC .cluster-label text{fill:#333;}#mermaid-svg-tGzGfs9XnXG7q9hC .cluster-label span{color:#333;}#mermaid-svg-tGzGfs9XnXG7q9hC .cluster-label span p{background-color:transparent;}#mermaid-svg-tGzGfs9XnXG7q9hC .label text,#mermaid-svg-tGzGfs9XnXG7q9hC span{fill:#333;color:#333;}#mermaid-svg-tGzGfs9XnXG7q9hC .node rect,#mermaid-svg-tGzGfs9XnXG7q9hC .node circle,#mermaid-svg-tGzGfs9XnXG7q9hC .node ellipse,#mermaid-svg-tGzGfs9XnXG7q9hC .node polygon,#mermaid-svg-tGzGfs9XnXG7q9hC .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-tGzGfs9XnXG7q9hC .rough-node .label text,#mermaid-svg-tGzGfs9XnXG7q9hC .node .label text,#mermaid-svg-tGzGfs9XnXG7q9hC .image-shape .label,#mermaid-svg-tGzGfs9XnXG7q9hC .icon-shape .label{text-anchor:middle;}#mermaid-svg-tGzGfs9XnXG7q9hC .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-tGzGfs9XnXG7q9hC .rough-node .label,#mermaid-svg-tGzGfs9XnXG7q9hC .node .label,#mermaid-svg-tGzGfs9XnXG7q9hC .image-shape .label,#mermaid-svg-tGzGfs9XnXG7q9hC .icon-shape .label{text-align:center;}#mermaid-svg-tGzGfs9XnXG7q9hC .node.clickable{cursor:pointer;}#mermaid-svg-tGzGfs9XnXG7q9hC .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-tGzGfs9XnXG7q9hC .arrowheadPath{fill:#333333;}#mermaid-svg-tGzGfs9XnXG7q9hC .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-tGzGfs9XnXG7q9hC .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-tGzGfs9XnXG7q9hC .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tGzGfs9XnXG7q9hC .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-tGzGfs9XnXG7q9hC .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tGzGfs9XnXG7q9hC .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-tGzGfs9XnXG7q9hC .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-tGzGfs9XnXG7q9hC .cluster text{fill:#333;}#mermaid-svg-tGzGfs9XnXG7q9hC .cluster span{color:#333;}#mermaid-svg-tGzGfs9XnXG7q9hC div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-tGzGfs9XnXG7q9hC .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-tGzGfs9XnXG7q9hC rect.text{fill:none;stroke-width:0;}#mermaid-svg-tGzGfs9XnXG7q9hC .icon-shape,#mermaid-svg-tGzGfs9XnXG7q9hC .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tGzGfs9XnXG7q9hC .icon-shape p,#mermaid-svg-tGzGfs9XnXG7q9hC .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-tGzGfs9XnXG7q9hC .icon-shape .label rect,#mermaid-svg-tGzGfs9XnXG7q9hC .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tGzGfs9XnXG7q9hC .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-tGzGfs9XnXG7q9hC .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-tGzGfs9XnXG7q9hC :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 神经网络模型
转换为优化的计算图
生成针对硬件的代码
CPU / GPU / FPGA
它的定位很明确------AI 框架的"计算基石",要同时服务两个阶段:
| 阶段 | 目标 |
|---|---|
| 推理 | 最低延迟、最高吞吐 |
| 训练 | 更快收敛、更高训练效率(并行化、内存优化) |
现状提醒 :nGraph 后来并入 OpenVINO 生态,作为独立项目的存在感已经很低。看这类对比表时一定要确认年份。
2.4 TC:用多面体模型干掉"手写 Schedule"
Tensor Comprehensions 是 Meta 做的工具,核心是一套叫 tensor comprehension 的 DSL------用张量表达式描述计算,再编译成高效 kernel。
它最有价值的地方,是原文那段关于"Schedule 为什么难"的分析:
| 任务 | 难度 | 原因 |
|---|---|---|
| 实现算子的计算逻辑 | 相对容易 | 本质就是数学运算 + 基本计算图操作 |
| 开发算子的 Schedule | 很难 | 必须同时懂算法逻辑和硬件体系结构(访存模式、并行策略、数据布局) |
更麻烦的是:图优化把大算子拆开、把小算子融合之后,会生成"新算子"和新 kernel 。这些新算子没有现成的 Schedule 模板,只能手写------耗时、费力,还难保证跨硬件都最优。
TC 的解法:用 Polyhedral Model(多面体模型) 做自动调度。
多面体模型怎么理解? 把多层循环嵌套看成多维空间里的一个几何体 ,每次迭代是几何体里的一个点。优化就变成"在这个几何体上找一种遍历顺序和切分方式",让访存局部性最好、并行度最高。数学上严谨,能自动推导,不靠人肉调参。
现状提醒 :TC 仓库已归档(archived) ,不再维护。它提出的"自动调度"思想被 TVM AutoTVM / Ansor、以及后来的 MLIR 生态继承了下来------项目死了,思想活着。
2.5 Glow:主打推理的"运行时优化派"
Glow 是 Meta 开源的推理 框架,特点是不只做编译期优化,还重仓运行时优化:
| 能力 | 作用 |
|---|---|
| 内存预分配 | 避免运行时反复 malloc,减少抖动 |
| 异步执行 | 让计算和搬运重叠 |
| 低精度计算 | FP16/INT8,换吞吐 |
| 图优化 + 跨硬件 | 常规操作 |
部署场景覆盖嵌入式、云端、边缘。
一句话记住 Glow :编译期省一点,运行时再省一点------它的很多收益来自"跑起来之后"的调度与内存管理,而不只是生成更好的 kernel。
三、五大挑战
原文把当前 AI 编译器的困难归纳为五条:
#mermaid-svg-roMse3EgL0SiHwy0{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-roMse3EgL0SiHwy0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-roMse3EgL0SiHwy0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-roMse3EgL0SiHwy0 .error-icon{fill:#552222;}#mermaid-svg-roMse3EgL0SiHwy0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-roMse3EgL0SiHwy0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-roMse3EgL0SiHwy0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-roMse3EgL0SiHwy0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-roMse3EgL0SiHwy0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-roMse3EgL0SiHwy0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-roMse3EgL0SiHwy0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-roMse3EgL0SiHwy0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-roMse3EgL0SiHwy0 .marker.cross{stroke:#333333;}#mermaid-svg-roMse3EgL0SiHwy0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-roMse3EgL0SiHwy0 p{margin:0;}#mermaid-svg-roMse3EgL0SiHwy0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-roMse3EgL0SiHwy0 .cluster-label text{fill:#333;}#mermaid-svg-roMse3EgL0SiHwy0 .cluster-label span{color:#333;}#mermaid-svg-roMse3EgL0SiHwy0 .cluster-label span p{background-color:transparent;}#mermaid-svg-roMse3EgL0SiHwy0 .label text,#mermaid-svg-roMse3EgL0SiHwy0 span{fill:#333;color:#333;}#mermaid-svg-roMse3EgL0SiHwy0 .node rect,#mermaid-svg-roMse3EgL0SiHwy0 .node circle,#mermaid-svg-roMse3EgL0SiHwy0 .node ellipse,#mermaid-svg-roMse3EgL0SiHwy0 .node polygon,#mermaid-svg-roMse3EgL0SiHwy0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-roMse3EgL0SiHwy0 .rough-node .label text,#mermaid-svg-roMse3EgL0SiHwy0 .node .label text,#mermaid-svg-roMse3EgL0SiHwy0 .image-shape .label,#mermaid-svg-roMse3EgL0SiHwy0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-roMse3EgL0SiHwy0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-roMse3EgL0SiHwy0 .rough-node .label,#mermaid-svg-roMse3EgL0SiHwy0 .node .label,#mermaid-svg-roMse3EgL0SiHwy0 .image-shape .label,#mermaid-svg-roMse3EgL0SiHwy0 .icon-shape .label{text-align:center;}#mermaid-svg-roMse3EgL0SiHwy0 .node.clickable{cursor:pointer;}#mermaid-svg-roMse3EgL0SiHwy0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-roMse3EgL0SiHwy0 .arrowheadPath{fill:#333333;}#mermaid-svg-roMse3EgL0SiHwy0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-roMse3EgL0SiHwy0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-roMse3EgL0SiHwy0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-roMse3EgL0SiHwy0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-roMse3EgL0SiHwy0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-roMse3EgL0SiHwy0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-roMse3EgL0SiHwy0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-roMse3EgL0SiHwy0 .cluster text{fill:#333;}#mermaid-svg-roMse3EgL0SiHwy0 .cluster span{color:#333;}#mermaid-svg-roMse3EgL0SiHwy0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-roMse3EgL0SiHwy0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-roMse3EgL0SiHwy0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-roMse3EgL0SiHwy0 .icon-shape,#mermaid-svg-roMse3EgL0SiHwy0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-roMse3EgL0SiHwy0 .icon-shape p,#mermaid-svg-roMse3EgL0SiHwy0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-roMse3EgL0SiHwy0 .icon-shape .label rect,#mermaid-svg-roMse3EgL0SiHwy0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-roMse3EgL0SiHwy0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-roMse3EgL0SiHwy0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-roMse3EgL0SiHwy0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} AI 编译器五大挑战
① 动态 Shape 泛化
② Python 编译静态化
③ 充分发挥硬件性能
④ 特殊算法优化方法
⑤ 易用性与性能兼顾
3.1 挑战一:动态 Shape 泛化
AI 编译器最擅长静态形状------形状已知,就能在编译期算出内存大小、决定分块策略、做完整优化。
但现实中大量输入是动态形状:
| 场景 | 动态来源 |
|---|---|
| NLP | 句子长度不定(有的 5 个 token,有的 500 个) |
| 目标检测 | 金字塔结构要同时处理多个尺度,形状随输入变化 |
| 带控制流的模型 | 图结构在运行时才确定 |
为什么难?
#mermaid-svg-L23aq3SA4IjdwQPL{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-L23aq3SA4IjdwQPL .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-L23aq3SA4IjdwQPL .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-L23aq3SA4IjdwQPL .error-icon{fill:#552222;}#mermaid-svg-L23aq3SA4IjdwQPL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-L23aq3SA4IjdwQPL .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-L23aq3SA4IjdwQPL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-L23aq3SA4IjdwQPL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-L23aq3SA4IjdwQPL .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-L23aq3SA4IjdwQPL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-L23aq3SA4IjdwQPL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-L23aq3SA4IjdwQPL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-L23aq3SA4IjdwQPL .marker.cross{stroke:#333333;}#mermaid-svg-L23aq3SA4IjdwQPL svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-L23aq3SA4IjdwQPL p{margin:0;}#mermaid-svg-L23aq3SA4IjdwQPL .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-L23aq3SA4IjdwQPL .cluster-label text{fill:#333;}#mermaid-svg-L23aq3SA4IjdwQPL .cluster-label span{color:#333;}#mermaid-svg-L23aq3SA4IjdwQPL .cluster-label span p{background-color:transparent;}#mermaid-svg-L23aq3SA4IjdwQPL .label text,#mermaid-svg-L23aq3SA4IjdwQPL span{fill:#333;color:#333;}#mermaid-svg-L23aq3SA4IjdwQPL .node rect,#mermaid-svg-L23aq3SA4IjdwQPL .node circle,#mermaid-svg-L23aq3SA4IjdwQPL .node ellipse,#mermaid-svg-L23aq3SA4IjdwQPL .node polygon,#mermaid-svg-L23aq3SA4IjdwQPL .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-L23aq3SA4IjdwQPL .rough-node .label text,#mermaid-svg-L23aq3SA4IjdwQPL .node .label text,#mermaid-svg-L23aq3SA4IjdwQPL .image-shape .label,#mermaid-svg-L23aq3SA4IjdwQPL .icon-shape .label{text-anchor:middle;}#mermaid-svg-L23aq3SA4IjdwQPL .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-L23aq3SA4IjdwQPL .rough-node .label,#mermaid-svg-L23aq3SA4IjdwQPL .node .label,#mermaid-svg-L23aq3SA4IjdwQPL .image-shape .label,#mermaid-svg-L23aq3SA4IjdwQPL .icon-shape .label{text-align:center;}#mermaid-svg-L23aq3SA4IjdwQPL .node.clickable{cursor:pointer;}#mermaid-svg-L23aq3SA4IjdwQPL .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-L23aq3SA4IjdwQPL .arrowheadPath{fill:#333333;}#mermaid-svg-L23aq3SA4IjdwQPL .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-L23aq3SA4IjdwQPL .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-L23aq3SA4IjdwQPL .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-L23aq3SA4IjdwQPL .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-L23aq3SA4IjdwQPL .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-L23aq3SA4IjdwQPL .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-L23aq3SA4IjdwQPL .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-L23aq3SA4IjdwQPL .cluster text{fill:#333;}#mermaid-svg-L23aq3SA4IjdwQPL .cluster span{color:#333;}#mermaid-svg-L23aq3SA4IjdwQPL div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-L23aq3SA4IjdwQPL .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-L23aq3SA4IjdwQPL rect.text{fill:none;stroke-width:0;}#mermaid-svg-L23aq3SA4IjdwQPL .icon-shape,#mermaid-svg-L23aq3SA4IjdwQPL .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-L23aq3SA4IjdwQPL .icon-shape p,#mermaid-svg-L23aq3SA4IjdwQPL .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-L23aq3SA4IjdwQPL .icon-shape .label rect,#mermaid-svg-L23aq3SA4IjdwQPL .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-L23aq3SA4IjdwQPL .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-L23aq3SA4IjdwQPL .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-L23aq3SA4IjdwQPL :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 能
不能
输入形状
编译时能确定?
静态图
编译期全量优化
(内存/分块/融合都能定)
动态图
运行时跟踪执行
只能做有限优化
性能好
性能受限
依赖运行时系统
大白话 :静态形状像"按尺寸定做校服 ",一次做好穿三年;动态形状像"每天身高都可能变",只能买可调节的,怎么调都不如定做的合身。
原文特别点出一个很难绕过的场景 :金字塔结构(如 FPN)的多尺度处理,不是"人工改改就能静态化"的------它天生就是动态的。
补充 :工业界的常见缓解手段有 shape specialization(按若干典型形状分别编译) 、bucketing(把长度归到若干档) 、以及 dynamic shape kernel(kernel 内部按实际形状计算)。它们都是"逼近"而不是"解决"。
3.2 挑战二:Python 编译静态化
AI 框架的宿主语言基本都是 Python------动态类型、解释执行 。而编译器需要静态类型、静态图。这中间的鸿沟就是"Python 静态化"。
先看 Python 的两种执行方式:
#mermaid-svg-yJAReKwRWmqRk3Yo{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yJAReKwRWmqRk3Yo .error-icon{fill:#552222;}#mermaid-svg-yJAReKwRWmqRk3Yo .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yJAReKwRWmqRk3Yo .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yJAReKwRWmqRk3Yo .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yJAReKwRWmqRk3Yo .marker.cross{stroke:#333333;}#mermaid-svg-yJAReKwRWmqRk3Yo svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yJAReKwRWmqRk3Yo p{margin:0;}#mermaid-svg-yJAReKwRWmqRk3Yo .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yJAReKwRWmqRk3Yo .cluster-label text{fill:#333;}#mermaid-svg-yJAReKwRWmqRk3Yo .cluster-label span{color:#333;}#mermaid-svg-yJAReKwRWmqRk3Yo .cluster-label span p{background-color:transparent;}#mermaid-svg-yJAReKwRWmqRk3Yo .label text,#mermaid-svg-yJAReKwRWmqRk3Yo span{fill:#333;color:#333;}#mermaid-svg-yJAReKwRWmqRk3Yo .node rect,#mermaid-svg-yJAReKwRWmqRk3Yo .node circle,#mermaid-svg-yJAReKwRWmqRk3Yo .node ellipse,#mermaid-svg-yJAReKwRWmqRk3Yo .node polygon,#mermaid-svg-yJAReKwRWmqRk3Yo .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yJAReKwRWmqRk3Yo .rough-node .label text,#mermaid-svg-yJAReKwRWmqRk3Yo .node .label text,#mermaid-svg-yJAReKwRWmqRk3Yo .image-shape .label,#mermaid-svg-yJAReKwRWmqRk3Yo .icon-shape .label{text-anchor:middle;}#mermaid-svg-yJAReKwRWmqRk3Yo .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yJAReKwRWmqRk3Yo .rough-node .label,#mermaid-svg-yJAReKwRWmqRk3Yo .node .label,#mermaid-svg-yJAReKwRWmqRk3Yo .image-shape .label,#mermaid-svg-yJAReKwRWmqRk3Yo .icon-shape .label{text-align:center;}#mermaid-svg-yJAReKwRWmqRk3Yo .node.clickable{cursor:pointer;}#mermaid-svg-yJAReKwRWmqRk3Yo .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yJAReKwRWmqRk3Yo .arrowheadPath{fill:#333333;}#mermaid-svg-yJAReKwRWmqRk3Yo .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yJAReKwRWmqRk3Yo .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yJAReKwRWmqRk3Yo .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yJAReKwRWmqRk3Yo .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yJAReKwRWmqRk3Yo .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yJAReKwRWmqRk3Yo .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yJAReKwRWmqRk3Yo .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yJAReKwRWmqRk3Yo .cluster text{fill:#333;}#mermaid-svg-yJAReKwRWmqRk3Yo .cluster span{color:#333;}#mermaid-svg-yJAReKwRWmqRk3Yo div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yJAReKwRWmqRk3Yo .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yJAReKwRWmqRk3Yo rect.text{fill:none;stroke-width:0;}#mermaid-svg-yJAReKwRWmqRk3Yo .icon-shape,#mermaid-svg-yJAReKwRWmqRk3Yo .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yJAReKwRWmqRk3Yo .icon-shape p,#mermaid-svg-yJAReKwRWmqRk3Yo .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yJAReKwRWmqRk3Yo .icon-shape .label rect,#mermaid-svg-yJAReKwRWmqRk3Yo .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yJAReKwRWmqRk3Yo .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yJAReKwRWmqRk3Yo .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yJAReKwRWmqRk3Yo :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} PyPy:JIT 编译
.py 源码
JIT 编译
热点代码转机器码
硬件执行
CPython:字节码 + 虚拟机
.py 源码
编译为字节码
.pyc
Python 虚拟机
while 循环逐条 dispatch
硬件执行
| 解释器 | 机制 | 特点 |
|---|---|---|
| CPython | 源码 → 字节码 → 虚拟机逐条匹配执行 | 通用、灵活;字节码平台无关 |
| PyPy | JIT 把部分代码直接编译成机器码 | 计算密集型程序快很多;兼容 CPython |
CPython 内部长什么样? 一个巨大的
while循环 + 一个庞大的 C 函数库,每条字节码对应一个 C 函数 (LOAD_CONST→ 加载常量,CALL_FUNCTION→ 调用函数)。这就是"switch-case 式解释器"。
三条静态化路线:
| 路线 | 做法 | 代表 | 问题 |
|---|---|---|---|
| ① Python JIT 虚拟机 | 在解释执行基础上加 JIT 加速 | PyPy | 历史设计包袱重,兼容改造极难 |
| ② Tracing(函数提取) | 跑一遍,记录实际执行路径,提取成函数图 | torch.fx.symbolic_trace |
只覆盖跑到的分支;动态控制流会丢 |
| ③ AST Transform(源码转换) | 直接改抽象语法树,转成静态 IR | torch.jit.script |
要支持完整 Python 语法,工程量巨大 |
原文的示例代码(保留):
python
# 路线 ②:Tracing ------ 先"包装"再"追踪"
@torch.fx.wrap
def torch_randn(x, shape):
return torch.randn(shape)
def f(x):
return x + torch_randn(x, 5)
fx.symbolic_trace(f) # 追踪一次执行,产出函数图
python
# 路线 ③:AST 转换 ------ script 解析源码
def foo(x, y):
return 2 * x + y
traced_foo = torch.jit.trace(foo, (torch.rand(3), torch.rand(3)))
@torch.jit.script
def bar(x):
return traced_foo(x, x)
四类具体难点(原文归纳):
| 难点 | 具体表现 |
|---|---|
| 类型推导 | 动态类型 → 静态类型:运行时收集信息、处理类型不确定性,在精度与性能间权衡 |
| 控制流表达 | if/else/while/for 要静态化:条件分支推导、循环终止条件、循环不变量、迭代器/生成器、异常处理 |
| 灵活语法与数据类型 | Slice(切片)、Dict(字典)、Index(索引) 这些动态特性很难静态分析 |
| JIT 编译性能 | Tracing 有追踪开销,AST Transform 有建树/转换成本,编译开销 vs 执行收益要权衡 |
原文的两处修正(很重要,容易被照抄):
torch.jit.trace不是 AST Transform 方法------它是 Tracing (跑一遍记录算子)。torch.jit.script才是 AST 层面的源码转换。fx常被解释为 "Function eXtraction",但 PyTorch 官方并没有这个正式展开,这更像社区说法,别当官方定义引用。
3.3 挑战三:充分发挥硬件性能
新框架想在各种硬件上都跑得好,不可能要求硬件反过来适配框架------只能靠编译器去适配各种硬件。
但难度在变大,原文点出两个核心矛盾:
#mermaid-svg-iphy4LhosSaSBXZe{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-iphy4LhosSaSBXZe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-iphy4LhosSaSBXZe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-iphy4LhosSaSBXZe .error-icon{fill:#552222;}#mermaid-svg-iphy4LhosSaSBXZe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-iphy4LhosSaSBXZe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-iphy4LhosSaSBXZe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-iphy4LhosSaSBXZe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-iphy4LhosSaSBXZe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-iphy4LhosSaSBXZe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-iphy4LhosSaSBXZe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-iphy4LhosSaSBXZe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-iphy4LhosSaSBXZe .marker.cross{stroke:#333333;}#mermaid-svg-iphy4LhosSaSBXZe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-iphy4LhosSaSBXZe p{margin:0;}#mermaid-svg-iphy4LhosSaSBXZe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-iphy4LhosSaSBXZe .cluster-label text{fill:#333;}#mermaid-svg-iphy4LhosSaSBXZe .cluster-label span{color:#333;}#mermaid-svg-iphy4LhosSaSBXZe .cluster-label span p{background-color:transparent;}#mermaid-svg-iphy4LhosSaSBXZe .label text,#mermaid-svg-iphy4LhosSaSBXZe span{fill:#333;color:#333;}#mermaid-svg-iphy4LhosSaSBXZe .node rect,#mermaid-svg-iphy4LhosSaSBXZe .node circle,#mermaid-svg-iphy4LhosSaSBXZe .node ellipse,#mermaid-svg-iphy4LhosSaSBXZe .node polygon,#mermaid-svg-iphy4LhosSaSBXZe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-iphy4LhosSaSBXZe .rough-node .label text,#mermaid-svg-iphy4LhosSaSBXZe .node .label text,#mermaid-svg-iphy4LhosSaSBXZe .image-shape .label,#mermaid-svg-iphy4LhosSaSBXZe .icon-shape .label{text-anchor:middle;}#mermaid-svg-iphy4LhosSaSBXZe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-iphy4LhosSaSBXZe .rough-node .label,#mermaid-svg-iphy4LhosSaSBXZe .node .label,#mermaid-svg-iphy4LhosSaSBXZe .image-shape .label,#mermaid-svg-iphy4LhosSaSBXZe .icon-shape .label{text-align:center;}#mermaid-svg-iphy4LhosSaSBXZe .node.clickable{cursor:pointer;}#mermaid-svg-iphy4LhosSaSBXZe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-iphy4LhosSaSBXZe .arrowheadPath{fill:#333333;}#mermaid-svg-iphy4LhosSaSBXZe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-iphy4LhosSaSBXZe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-iphy4LhosSaSBXZe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-iphy4LhosSaSBXZe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-iphy4LhosSaSBXZe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-iphy4LhosSaSBXZe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-iphy4LhosSaSBXZe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-iphy4LhosSaSBXZe .cluster text{fill:#333;}#mermaid-svg-iphy4LhosSaSBXZe .cluster span{color:#333;}#mermaid-svg-iphy4LhosSaSBXZe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-iphy4LhosSaSBXZe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-iphy4LhosSaSBXZe rect.text{fill:none;stroke-width:0;}#mermaid-svg-iphy4LhosSaSBXZe .icon-shape,#mermaid-svg-iphy4LhosSaSBXZe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-iphy4LhosSaSBXZe .icon-shape p,#mermaid-svg-iphy4LhosSaSBXZe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-iphy4LhosSaSBXZe .icon-shape .label rect,#mermaid-svg-iphy4LhosSaSBXZe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-iphy4LhosSaSBXZe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-iphy4LhosSaSBXZe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-iphy4LhosSaSBXZe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 性能优化要有全局视野
图层优化 + 算子层优化
各自独立做,收益有限
需要图算融合
子图切分 + 垂直融合 + 水平并行
硬件架构越来越复杂
专有优化越来越多
想做出通用/泛化的优化
越来越难
当前无统一完善方案
当前的探索方向:
| 方向 | 具体做法 |
|---|---|
| 打开图与算子的边界 | 重新组合计算图结构,让计算和数据在各层次更紧密配合,减少访存与浪费 |
| 垂直融合 | 如 Buffer Fusion,减少中间结果落盘 |
| 水平并行 | 如 Data Parallel,把并行度铺满 |
| 自动生成子图 kernel | scheduling、tiling(切片)、vectorizing 自动完成,按硬件定制 |
大白话 :以前是"图层管全局、算子层管局部,各管一段 ";现在要的是"两层一起看",否则中间会漏掉大量优化机会。
3.4 挑战四:特殊算法优化方法
(1)大模型训练:并行策略
| 维度 | 手段 |
|---|---|
| Scale out(横向扩展) | 多维混合并行:数据并行 + 张量并行 + 流水线并行,把任务铺到多节点/多设备 |
| Scale up(纵向提升) | 重计算(recompute)、混合精度、异构并行,把单设备效率榨干 |
最大的拦路虎是"效率墙" ,而效率墙取决于切分策略:
| 现状 | 问题 |
|---|---|
| 手工配置切分 | 需要深厚领域知识 + 丰富经验;成本高,还容易优化不足或过度优化 |
| 半自动并行 | 能解决一部分,但还没"解放开发者" |
| 目标 | 靠编译技术 + 凸优化,自动找到最优并行策略 |
本质 :自动并行是一个策略搜索问题------在巨大搜索空间里找一个"够好"的解。这就是为什么它跟 AI 编译器的自动调优是同一类问题。
(2)HPC 场景:自动微分
| 难点 | 说明 |
|---|---|
| 动态图控制流 | 用 Python 执行控制流,循环次数多时性能明显下降 |
| 静态图自动微分 | 要解决逻辑拼接 / 计算图展开问题,才能高效微分 |
| 高阶微分 | 要灵活控制图的展开方式,还要高效计算 Jacobian / Hessian |
3.5 挑战五:易用性与性能兼顾
这一条最"产品化",也最容易被技术文章忽略。原文列了五个子问题:
| 子问题 | 具体表现 |
|---|---|
| 与框架的边界和对接 | 各框架抽象/API 不同;不保证全算子支持的前提下,怎么透明支持用户的计算图 |
| 对用户透明性 | 很多编译器并非全自动------TVM 的性能依赖用户写的高层抽象/模板;要二次开发甚至重构架构,门槛极高 |
| 编译开销 | 编译收益必须明显大于编译成本;很多场景对编译时间敏感,编译器反而拖慢调试验证 |
| 性能问题 | 编译器的本质是"把人工优化沉淀成泛化方法";只有真正追上或超过手工优化才有价值 |
| 鲁棒性 | 多数还在研究阶段:能不能编过、结果对不对、错了怎么调试,都还是问题 |
一句话 :编译器不是"免费的午餐"------它用"编译时间 + 学习成本 + 不确定性"换"运行性能"。这笔账不合算时,用户宁可用 PyTorch eager 直接跑。
四、三个值得认真想的开放问题
原文在挑战之后抛了三问,我觉得比结论更有价值:
#mermaid-svg-P9dJaHNWRf8Ig9PJ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .error-icon{fill:#552222;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .marker.cross{stroke:#333333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ p{margin:0;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .cluster-label text{fill:#333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .cluster-label span{color:#333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .cluster-label span p{background-color:transparent;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .label text,#mermaid-svg-P9dJaHNWRf8Ig9PJ span{fill:#333;color:#333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .node rect,#mermaid-svg-P9dJaHNWRf8Ig9PJ .node circle,#mermaid-svg-P9dJaHNWRf8Ig9PJ .node ellipse,#mermaid-svg-P9dJaHNWRf8Ig9PJ .node polygon,#mermaid-svg-P9dJaHNWRf8Ig9PJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .rough-node .label text,#mermaid-svg-P9dJaHNWRf8Ig9PJ .node .label text,#mermaid-svg-P9dJaHNWRf8Ig9PJ .image-shape .label,#mermaid-svg-P9dJaHNWRf8Ig9PJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .rough-node .label,#mermaid-svg-P9dJaHNWRf8Ig9PJ .node .label,#mermaid-svg-P9dJaHNWRf8Ig9PJ .image-shape .label,#mermaid-svg-P9dJaHNWRf8Ig9PJ .icon-shape .label{text-align:center;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .node.clickable{cursor:pointer;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .arrowheadPath{fill:#333333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-P9dJaHNWRf8Ig9PJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-P9dJaHNWRf8Ig9PJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-P9dJaHNWRf8Ig9PJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .cluster text{fill:#333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .cluster span{color:#333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-P9dJaHNWRf8Ig9PJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .icon-shape,#mermaid-svg-P9dJaHNWRf8Ig9PJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .icon-shape p,#mermaid-svg-P9dJaHNWRf8Ig9PJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .icon-shape .label rect,#mermaid-svg-P9dJaHNWRf8Ig9PJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-P9dJaHNWRf8Ig9PJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-P9dJaHNWRf8Ig9PJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-P9dJaHNWRf8Ig9PJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Q1 图算能否统一表达?
统一 IR 打破图层/算子层 GAP
现状:两层分开表达
算法工程师在图层
Kernel 工程师在算子层
Q2 完全自动并行可行吗?
策略搜索能否真正最优
现状:搜索只能找次优
统一分布式图 + 全局代价模型
Q3 AI 芯片需要编译器吗?
越灵活的芯片越依赖编译器
CISC 风格把优化放进硬件
Q1:图算能否统一表达和优化?
| 今天的分工 | 谁负责 |
|---|---|
| 图层表达 | 算法工程师 |
| 算子层表达 | AI 框架 / 芯片 / Kernel 工程师 |
原文的展望是:未来会不会有一种 IR 打破图与算子之间的 GAP ?尤其在 AI + 科学计算、AI + 大数据这些新场景下,图层和算子层的界限本来就在模糊。
现实进展 :MLIR 正是朝着"统一、可扩展的多层 IR"去的------它不强行统一成一种方言,而是提供"方言(dialect)体系",让图级、算子级、硬件级 IR 能在同一个框架内共存并逐层下降。这算是这个问题目前最接近的答案。
Q2:完全自动并行是否可行?
原文的表述很克制:
自动并行本质是策略搜索 ;策略搜索能在有限空间里找到次优解 (原文用词"次有",应为"次优"),但真正意义上的自动并行能否做到,还需要验证。
它的设想是:统一分布式计算图 + 统一资源图 + 基于全局代价模型的规划器 ,自动为训练任务选择硬件感知的并行策略。
难点在哪 :搜索空间随节点数、模型层数组合爆炸;代价模型不准,搜出来的"最优"在真实集群上就跑不出来。
Q3:AI 芯片需要(AI)编译器吗?
原文的判断很干脆:
越灵活的芯片,对编译器的依赖越大。
| 芯片风格 | 优化放在哪 | 对编译器依赖 |
|---|---|---|
| CISC 风格 | 大量优化做进硬件内部 | 较低 |
| 灵活架构(保留张量指令集 + 特殊内存,但整体更可编程) | 优化交给软件/编译器 | 高 |
未来趋势是:架构师要做芯片与系统协同设计 ,自动化会越来越多地被用到专用芯片里。
五、AI 编译器的五个未来方向
#mermaid-svg-PvHiLdKYO1Iumis3{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-PvHiLdKYO1Iumis3 .error-icon{fill:#552222;}#mermaid-svg-PvHiLdKYO1Iumis3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-PvHiLdKYO1Iumis3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-PvHiLdKYO1Iumis3 .marker.cross{stroke:#333333;}#mermaid-svg-PvHiLdKYO1Iumis3 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-PvHiLdKYO1Iumis3 p{margin:0;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge{stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .section--1 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section--1 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section--1 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section--1 text{fill:#ffffff;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth--1{stroke-width:17;}#mermaid-svg-PvHiLdKYO1Iumis3 .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:#ffffff;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-0 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-0 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-0 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-0 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-0{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-0{stroke-width:14;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-1 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-1 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-1 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-1 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-1{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-1{stroke-width:11;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-2 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-2 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-2 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-2 text{fill:#ffffff;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-2{stroke-width:8;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:#ffffff;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-3 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-3 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-3 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-3 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-3{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-3{stroke-width:5;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-4 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-4 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-4 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-4 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-4{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-4{stroke-width:2;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-5 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-5 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-5 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-5 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-5{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-5{stroke-width:-1;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-6 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-6 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-6 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-6 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-6{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-6{stroke-width:-4;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-7 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-7 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-7 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-7 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-7{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-7{stroke-width:-7;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-8 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-8 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-8 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-8 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-8{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-8{stroke-width:-10;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-9 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-9 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-9 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-9 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-9{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-9{stroke-width:-13;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-10 rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-10 path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-10 circle,#mermaid-svg-PvHiLdKYO1Iumis3 .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-10 text{fill:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .node-icon-10{font-size:40px;color:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .edge-depth-10{stroke-width:-16;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-PvHiLdKYO1Iumis3 .lineWrapper line{stroke:black;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled circle,#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:lightgray;}#mermaid-svg-PvHiLdKYO1Iumis3 .disabled text{fill:#efefef;}#mermaid-svg-PvHiLdKYO1Iumis3 .section-root rect,#mermaid-svg-PvHiLdKYO1Iumis3 .section-root path,#mermaid-svg-PvHiLdKYO1Iumis3 .section-root circle{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-PvHiLdKYO1Iumis3 .section-root text{fill:#ffffff;}#mermaid-svg-PvHiLdKYO1Iumis3 .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-PvHiLdKYO1Iumis3 .edge{fill:none;}#mermaid-svg-PvHiLdKYO1Iumis3 .eventWrapper{filter:brightness(120%);}#mermaid-svg-PvHiLdKYO1Iumis3 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 编译器形态 推理用 AOT 训练用 JIT 按任务与资源选最优 IR 形态 类似 MLIR 的统一 IR 多语言多框架 跨框架优化转换 自动并行 跨机器跨节点 自动发现并行机会 按资源自动分配调度 自动微分 支持高阶微分 丰富的图操作接口 高效 Jacobian/Hessian Kernel 生成 自动化生成 按硬件优化 降低算子开发门槛 未来方向(原文展望)
| 方向 | 具体内容 |
|---|---|
| 编译器形态 | 推理 AOT + 训练 JIT:推理预编译求高效,训练即时编译应对动态需求 |
| IR 形态 | 一个类似 MLIR 的统一 IR,支持多语言多框架,跨框架优化转换更高效 |
| 自动并行 | 跨机器、跨节点自动并行;自动检测并行机会,按任务特性与资源自动分配调度 |
| 自动微分 | 支持高阶微分 ;提供丰富的图操作接口,方便改结构 |
| Kernel 自动生成 | 按算法 + 硬件环境自动生成优化 kernel,降低算子开发门槛、提升泛化性 |
这五条要连起来看 :它们其实在回答同一个问题------"能不能让开发者只描述'算什么',剩下的全交给编译器?" 自动并行、自动微分、自动 kernel 生成,都是这个方向的子目标。
六、小结
| # | 结论 |
|---|---|
| 1 | 五家对比来自 2020 年综述,反映的是当时格局:TC/Glow 已淡出,nGraph 并入 OpenVINO,XLA 走向 OpenXLA |
| 2 | TVM :Relay(图)+ TVM(算子)两层;核心是 Compute / Schedule 分离 |
| 3 | XLA :打开大算子 → 优化小算子 → 重新融合 ;优化规则全手工指定 |
| 4 | TC :用多面体模型 自动调度,解决"Schedule 开发难";项目已归档,思想被继承 |
| 5 | Glow :主打推理,重仓运行时优化(内存预分配、异步、低精度) |
| 6 | 五大挑战:动态 Shape、Python 静态化、硬件性能、特殊算法、易用性 vs 性能 |
| 7 | Python 静态化三条路:JIT 虚拟机 / Tracing(torch.fx)/ AST Transform(torch.jit.script) |
| 8 | 性能瓶颈的根源:图层与算子层各自优化,缺少"图算融合"的全局视野 |
| 9 | 三个开放问题中,MLIR 是"统一 IR"目前最接近的答案 ;自动并行本质是组合爆炸下的策略搜索 |
| 10 | 未来五方向共同指向:开发者只描述"算什么",编译器接管"怎么算" |
附:原文修正
| # | 原文 | 修正 |
|---|---|---|
| 1 | "TVM 是 Apache 公司的开源项目" | 是 Apache 软件基金会(ASF)下的开源项目,不是"公司" |
| 2 | "torch.jit.trace 属于 AST Transform 方法" | 它是 Tracing (执行记录);torch.jit.script 才是 AST/源码转换 |
| 3 | "PyTorch.fx 中 fx 代表 Function Extraction" | 官方无此正式展开,属社区说法,不宜当定义引用 |
| 4 | "XLA 使用 HLO/LLO" | XLA 是 HLO → LLVM IR (中间有 LHLO 等变体);"LLO" 非官方术语 |
| 5 | "策略搜索能找到次有的答案" | 笔误,应为次优(sub-optimal) |
| 6 | 五家对比未标注时间 | 出自 2020 年 综述,TC、Glow 现已归档/边缘化,nGraph 已并入 OpenVINO |
| 7 | 图注错位(XLA 小节下标注 "TVM logo",nGraph 小节下标注 "TC/Glow") | 已按正确归属重新描述 |
参考资料
- 原文:AISystem / 03Compiler / 02AICompiler / 04Future.md(作者 ZOMI)
- 综述:The Deep Learning Compiler: A Comprehensive Survey(五家对比图的出处)
- Apache TVM:https://tvm.apache.org/
- OpenXLA:https://openxla.org/
- MLIR:https://mlir.llvm.org/
- Tensor Comprehensions(归档):https://github.com/facebookresearch/TensorComprehensions
- 配套视频:BV1Hv4y1R7uc