1 of 36

Efficient E-Graph Extraction via Linear Programming

Haichen Dong

Deyuan (Mike) He

2 of 36

Background

2

Term rewriting is extremely common in Compilers:

a * 2

a << 1

a / pot

a >> log2(pot)

a / const

a * (1 / const)

3 of 36

Background

3

However, determining the order of applying rewrite rules is HARD!

M1

[N, M]

M2

[M, K]

M3

[K, P]

M4

[K, Q]

4 of 36

Background

4

However, determining the order of applying rewrite rules is HARD!

M1

[N, M]

M2

[M, K]

M3

[K, P]

M4

[K, Q]

5 of 36

Background

5

M1

M2

M3

*

*

M1

M2

M4

*

*

  1. Common Subexpression Elimination (CSE)
  2. Associativity of Matrix Multiplication

2NMK + NKP + NKQ multiplications

6 of 36

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

7 of 36

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

*

*

8 of 36

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

9 of 36

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

10 of 36

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

11 of 36

Background

3

Compilers may have hundreds of passes.

How to determine the order to ensure the product program is Optimal ?

12 of 36

Background

4

Compilers may have hundreds of passes.

How to determine the order to ensure the product program is Optimal ?

Phase Ordering Problem

13 of 36

E-Graph

5

A compact, tree data structure that maintains equivalences over terms with respect to a set of axioms (rewrite rules)

  1. E-Classes (dashed boxes): A set of equivalent terms
  2. E-Nodes (solid boxes): Operators, variables or literals

x

a

2

+

1

Represents the term “2”

Represents the term “1+1”

14 of 36

E-Graph

6

Syntactic Rewrites: an initial pattern and a target pattern

x

a

2

+

1

Apply

with ?x ⇒ a

15 of 36

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

16 of 36

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

17 of 36

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)

18 of 36

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

19 of 36

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)

20 of 36

The Problem

12

Greedy does not always work!

Yang, Yichen et al. "Equality Saturation for Tensor Graph Superoptimization." (2021).

21 of 36

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

22 of 36

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).

23 of 36

Extraction as ILP

15

For each E-Class k, create a binary variable wk

wR

wA

wB

wC

24 of 36

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

25 of 36

Extraction as ILP

17

wR

wA

wB

wC

wa

wb

wc

we

wd

Constraints:

  1. We must pick at least one E-Node from the root E-Class (in this case, R)

  • If an E-Class K is picked, then at least one E-Node in K must also be picked

  • If an E-Node n is picked, then all of its children E-Classes must be picked

For any E-Class/E-Node, wx = 1 denotes the E-Class/E-Node is active (i.e. they are picked in the extraction)

26 of 36

Extraction as ILP

18

wR

wA

wB

wC

wa

wb

wc

we

wd

Constraints:

  • Root constraints
    1. wa = 1

  • Node constraints
    • -wR + wa > 0
    • -wA + wb + wc > 0
    • -wB + wd > 0
    • -wC + we > 0

  • Children constraints
    • wB ≥ wa wA ≥ wa
    • wB ≥ wb
    • wC ≥ wc

For any E-Class/E-Node, wx = 1 denotes the E-Class/E-Node is active (i.e. they are picked in the extraction)

27 of 36

Extraction as LP

19

28 of 36

LP Relaxation

20

29 of 36

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!

30 of 36

Evaluation

22

Setup:

  • Implemented LP encoding in egg library (~200 LoC), invoking CPLEX solver
  • Adapted Tensat (Yang et al.) evaluation pipeline to test our rounding scheme
  • Evaluated graph rewrites on
    • BERT, VGG-16, ResNexT-50, NasRNN, InceptionV3

31 of 36

Optimization Comparison

23

Costs of extracted program (normalized by the cost of inputs)

32 of 36

Optimization Comparison

24

Cases LP extraction doesn’t work well

(due to the drawback we discussed)

33 of 36

Solver Time Comparison

25

Solver time comparison on solving LP/ILP extractions

34 of 36

Solver Time Comparison

26

Solver time comparison on solving LP/ILP extractions

35 of 36

Solver Time Comparison

27

For cases LP extraction works, it has up to 200x~ speed-up

36 of 36

Discussion

28

  1. If it is not guaranteed to work, why do we do this?
    1. It is natural to try LP relaxation for ILP formulations
    2. The proof of “It may not work” is also a result
    3. It may worth to try LP first before ILP if the approximation works well since most of the cases, E-Graph does not saturates
  2. Can it be improved? If so, how?
    • LP itself probably does not have a good space for improvement.
    • However, this finding led us to explore other strategies: Non-convex optimization and Weighted partial MaxSAT. (See report for more details)
  3. What’s the future plan?
    • Try implementing other extraction algorithms and compare with current ones.
    • We are considering a workshop paper next year.