Efficient E-Graph Extraction via Linear Programming
Haichen Dong
Deyuan (Mike) He
Background
2
Term rewriting is extremely common in Compilers:
a * 2
a << 1
a / pot
a >> log2(pot)
a / const
a * (1 / const)
Background
3
However, determining the order of applying rewrite rules is HARD!
M1
[N, M]
M2
[M, K]
M3
[K, P]
M4
[K, Q]
Background
4
However, determining the order of applying rewrite rules is HARD!
M1
[N, M]
M2
[M, K]
M3
[K, P]
M4
[K, Q]
Background
5
M1
M2
M3
*
*
M1
M2
M4
*
*
2NMK + NKP + NKQ multiplications
Background
6
M1
M2
M3
*
*
M1
M2
M4
*
*
M3
*
M4
*
M1
M2
*
M12
=
M12
M12
2NMK + NKP + NKQ multiplications
NMK + NKP + NKQ multiplications
CSE Rewrite
Background
7
M1
M2
M3
*
*
M1
M2
M4
*
*
2NMK + NKP + NKQ multiplications
NKP + NKQ + MKP + MKQ multiplications
Associativity Rewrite
M1
M2
M3
*
*
M1
M2
M4
*
*
Background
8
NKP + NKQ + MKP + MKQ
Associativity Rewrite
M1
M2
M3
*
*
M1
M2
M4
*
*
M3
*
M4
*
M1
M2
*
M12
=
M12
M12
NMK + NKP + NKQ
CSE Rewrite
Background
9
NKP + NKQ + MKP + MKQ
Associativity Rewrite
M1
M2
M3
*
*
M1
M2
M4
*
*
M3
*
M4
*
M1
M2
*
M12
=
M12
M12
NMK + NKP + NKQ
CSE Rewrite
Background
10
Case 1 (Assoc better than CSE):
NMK + NKP + NKQ > NKP + NKQ + MKP + MKQ
⇒ NMK > (P+Q)MK
⇒ N > P+Q
Case 2 (CSE better than Assoc):
NMK + NKP + NKQ < NKP + NKQ + MKP + MKQ
⇒ NMK < (P+Q)MK
⇒ N < P+Q
Background
3
Compilers may have hundreds of passes.
How to determine the order to ensure the product program is Optimal ?
Background
4
Compilers may have hundreds of passes.
How to determine the order to ensure the product program is Optimal ?
Phase Ordering Problem
E-Graph
5
A compact, tree data structure that maintains equivalences over terms with respect to a set of axioms (rewrite rules)
x
a
2
+
1
Represents the term “2”
Represents the term “1+1”
E-Graph
6
Syntactic Rewrites: an initial pattern and a target pattern
x
a
2
+
1
Apply
with ?x ⇒ a
E-Graph
7
Syntactic Rewrites: initiate and add new expressions based on the matched bindings
x
a
2
+
1
x
a
2
+
1
<<
Apply
with ?x ⇒ a
instantiate
?x << 1 to a << 1
E-Graph
8
Syntactic Rewrites: initiate and add new expressions based on the matched bindings
x
a
2
+
1
x
a
2
+
1
<<
Apply
with ?x ⇒ a
instantiate
?x << 1 to a << 1
Non-destructive Rewrite
E-Graph
9
x
a
2
+
1
<<
10
1
0
5
0
1
Numbers in E-Nodes are example costs
Extraction: Given a root E-Class, pick the “best” term
(minimizing the sum of costs of E-Nodes given by a cost model)
E-Graph
10
Extraction: Given a root E-Class, pick the “best” term
(minimizing the sum of costs of E-Nodes given by a cost model)
x
a
2
+
1
<<
10
1
0
5
0
1
Numbers in E-Nodes are example costs
E-Graph
11
x
a
2
+
1
<<
10
1
0
5
0
1
Numbers in E-Nodes are example costs
Extraction: Given a root E-Class, pick the “best” term
(minimizing the sum of costs of E-Nodes given by a cost model)
The Problem
12
Greedy does not always work!
Yang, Yichen et al. "Equality Saturation for Tensor Graph Superoptimization." (2021).
The Problem
13
Greedy does not always work!
Yang, Yichen et al. "Equality Saturation for Tensor Graph Superoptimization." (2021).
Suppose
b + d > c + e
a + b + d < a + c + e + d
The Problem
14
Greedy does not always work!
Suppose
b + d > c + e
a + b + d < a + c + e + d
Yang, Yichen et al. "Equality Saturation for Tensor Graph Superoptimization." (2021).
Extraction as ILP
15
For each E-Class k, create a binary variable wk
wR
wA
wB
wC
Extraction as ILP
16
For each E-Class k, create a binary variable wk
wR
wA
wB
wC
For each E-Node n, create a binary variable wn
wa
wb
wc
we
wd
Extraction as ILP
17
wR
wA
wB
wC
wa
wb
wc
we
wd
Constraints:
For any E-Class/E-Node, wx = 1 denotes the E-Class/E-Node is active (i.e. they are picked in the extraction)
Extraction as ILP
18
wR
wA
wB
wC
wa
wb
wc
we
wd
Constraints:
For any E-Class/E-Node, wx = 1 denotes the E-Class/E-Node is active (i.e. they are picked in the extraction)
Extraction as LP
19
LP Relaxation
20
Analysis
21
Counter-example:
All operations have uniform costs
Observation: OPTILP = k
Furthermore, the cost of any valid solution to this extraction problem is at least k!
Possible solution for LP:
Halve weights in every layer.
The total cost is only 3!
Evaluation
22
Setup:
Optimization Comparison
23
Costs of extracted program (normalized by the cost of inputs)
Optimization Comparison
24
Cases LP extraction doesn’t work well
(due to the drawback we discussed)
Solver Time Comparison
25
Solver time comparison on solving LP/ILP extractions
Solver Time Comparison
26
Solver time comparison on solving LP/ILP extractions
Solver Time Comparison
27
For cases LP extraction works, it has up to 200x~ speed-up
Discussion
28