输入“/”快速插入内容

动态 shape 的挑战与解决现状

2024年1月14日创建
1682
1830
概述
动态 shape 是什么
动态 shape,一般是 2 个问题:
1.
Tensor 的 shape 是动态的。
2.
Control flow 的动态性。即 if-else / loop 类的结构。比如,where,while,Switch 等。
Tensor shape 的动态性,类型比较多。比如:
1.
变化的 batch size。
2.
变化的 input shape。CV 模型的 image resolution,NLP 模型的 input sequence length。
3.
算子逻辑导致的。unique,nonzero。
4.
稀疏矩阵导致的。输入本身可以是静态的,但为了节约存储空间,使用稀疏矩阵表示输入。搜推模型用的多。
5.
其他特殊情况。比如,random 算子,可能间接导致动态 shape。
解决方案,集中在 compiler 层面。核心是如何 static 化,基本靠 padding。
“解决” 基本是不可能的,只能 case by case 的绕过去。
动态 shape 的挑战
Compiler 不擅长处理动态 shape。
静态 shape 语义下比较确定性的问题,动态 shape 场景很复杂。比如:
1.
算子占用的显存预估 & 调度策略,
2.
3.
指令层的向量化,
4.
codegen 模版选择。
Broadcast 的例子
A one dimensional array added to a two dimensional array results in broadcasting
Hardware 层面的困难:
1.
最终产生数据依赖、跳转/条件分支 类指令,导致更多的 流水线气泡 (pipeline stall)。
2.
(还有别的吗?)
pipeline stall on Control Flow 的例子