Stochastic superoptimization
Eric Schkufza, Rahul Sharma, Alex Aiken
Stanford University
(ASPLOS'13)
Presenter: Xingyu Xie
Index
Target:待优化的程序
Super Optimization:尝试在程序空间中找到和 target 语义等价但性能最(极)好的程序。
程序空间过大?通过搜索来寻找近似最优解!
Synthesis:尝试搜索出“正确”(和 target 等价等价)的程序
Optimization:从“正确”的程序出发搜索效率更高的程序
MCMC Sampling
优化问题:最小化 cost function
基本方法:爬山算法
缺点:容易陷入局部极值
如何避免陷入局部极值?
Metropolis-Hastings Algorithm!
核心想法:“以一定概率允许走到比当前位置更差的点”。
Metropolis-Hastings algorithm
- \( c \) 是 cost function
- \( T \) 是 target,即待优化的程序
- \( \mathcal{R} \) 是搜索到当前得到的程序版本
- \( \mathcal{R}^* \) 是搜索的下一步可能要前往的程序版本
- 想法:当 \( \mathcal{R}^* \) 不如 \( \mathcal{R} \) 更优时,根据二者的 cost function 的比较 \( \alpha \)和一个 [0, 1] 间随机生成的数 p,来决定是否前往 \( \mathcal{R}^* \)
Move | Meaning |
---|---|
Opcode | 随机选择一条指令,将它的运算随机换成另外一个 |
Operand | 随机选择一条指令,将它的运算数随机换成另外一个 |
Swap | 随机选择两个指令,将它们交换 |
Instruction | 随机选择一条指令,将它随机换成另一条指令(包括特殊的 UNUSED) |
Mutation (move)
- 对程序的要求:没有循环,有恰好 L 条指令(包括特殊的 UNUSED)。
- mutation 的性质:ergodicity(任意两个程序之间都有一个 mutation 序列);symmetry(如果 A 能以 p 的概率变异出 B,那么 B 也会以 p 的概率变异出 A)。
Cost function
分为正确性和 performance 两项。(2)
我们的目标就是要求出这样的 \( \mathcal{R}' \)。(3)
performance
假设每一种指令会有一个固定的 latency,整个程序的 performance 就是 latency 之和。(13)
correctness
Synthesis
From 1 phase to 2 phases:
- Synthesis: correctness
- Optimization: correctness + performance
实验发现:只考虑正确性的话,可以有效地从一个 random program 得到一个(几乎)正确的程序。
Validation
- spec:target and rewrite 在 live output 上有相同的 side effect
- theory:bit-vector arithmetic
- solver:STP
- memory:addr1 = addr2 => val1 = val2
- assumption:
- stack address 总是 stack pointer 加一个常数
- 把 64 位的乘法和除法视为未解释函数
Evaluation
LImitations
对循环的处理不佳,只能在每一个基本块里独立地去做 superoptimization。
It's hard to synthesize correct result for programs that produce results which differ by only a single bit from a simple yet completely incorrect alternative.
// Round Up to
// the Next Highest
// Power of 2
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
Stochastic Superoptimization
By Xingyu Xie
Stochastic Superoptimization
- 2