forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transform_cse.py
More file actions
341 lines (291 loc) · 11.4 KB
/
test_transform_cse.py
File metadata and controls
341 lines (291 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Test eliminate common subexpr pass"""
import tvm
import tvm.testing
from tvm.relax.transform import EliminateCommonSubexpr
from tvm.script.parser import ir as I, relax as R, tir as T
import numpy as np
def verify(input, expected, call_only=False):
tvm.ir.assert_structural_equal(EliminateCommonSubexpr(call_only)(input), expected)
def test_simple():
@I.ir_module
class Before:
@R.function
def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")):
with R.dataflow():
lv0 = R.add(x, y)
lv1 = R.add(x, y)
gv = R.multiply(lv0, lv1)
R.output(gv)
return gv
@I.ir_module
class Expected:
@R.function
def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")):
with R.dataflow():
lv0 = R.add(x, y)
# can combine with canonicalizing bindings
# and getting rid of unused bindings to eliminate this line too
lv1 = lv0
gv = R.multiply(lv0, lv1)
R.output(gv)
return gv
verify(Before, Expected)
def test_constants():
@I.ir_module
class Before:
@R.function
def foo() -> R.Tuple(R.Tensor((), dtype="int32"), R.Tensor((2, 2), dtype="int32")):
with R.dataflow():
# we are not going to bind the constant 1 to a var
lv0 = R.add(R.const(1, dtype="int32"), R.const(1, dtype="int32"))
# we expect to bind the repeated large constants
lv1 = R.add(
R.const(tvm.nd.array(np.zeros((2, 2), dtype="int32"))),
R.const(tvm.nd.array(np.zeros((2, 2), dtype="int32"))),
)
gv = (lv0, lv1)
R.output(gv)
return gv
@I.ir_module
class Expected:
@R.function
def foo() -> R.Tuple(R.Tensor((), dtype="int32"), R.Tensor((2, 2), dtype="int32")):
with R.dataflow():
lv0 = R.add(R.const(1, dtype="int32"), R.const(1, dtype="int32"))
lv1 = R.const(tvm.nd.array(np.zeros((2, 2), dtype="int32")))
lv2 = R.add(lv1, lv1)
gv = (lv0, lv2)
R.output(gv)
return gv
verify(Before, Expected)
def test_repeated_inner_tuples():
@I.ir_module
class Before:
@R.function
def foo(x: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"):
with R.dataflow():
# repeated units: (x, x), (x, (x, x)), ((x, x), (x, (x, x)))
tup = (((x, x), (x, (x, x))), ((x, x), (x, (x, x))), (x, (x, x)))
gv = tup[0][0][1]
R.output(gv)
return gv
@I.ir_module
class Expected:
@R.function
def foo(x: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"):
with R.dataflow():
t1 = (x, x)
t2 = (x, t1)
t3 = (t1, t2)
t4 = (t3, t3, t2)
gv = t4[0][0][1]
R.output(gv)
return gv
verify(Before, Expected)
def test_inner_function():
@I.ir_module
class Before:
@R.function
def foo(x: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"):
with R.dataflow():
# we are going to do CSE inside the local function
@R.function
def bar(y: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"):
with R.dataflow():
# writing this out in ANF to illustrate why CSE behaves as it does
# result of ANF transforming R.add(R.add(y, y), R.add(y, y))
lv0 = R.add(y, y)
lv1 = R.add(y, y)
lv2 = R.add(lv0, lv1)
gv = lv2
R.output(gv)
return R.add(gv, gv)
# also making the ANF explicit to better illustrate the result of CSE
# result of ANF transforming R.add(R.add(bar(x), bar(x)), R.add(bar(x), bar(x)))
lv0 = bar(x)
lv1 = bar(x)
lv2 = R.add(lv0, lv1)
lv3 = bar(x)
lv4 = bar(x)
lv5 = R.add(lv3, lv4)
lv6 = R.add(lv2, lv5)
gv = lv6
R.output(gv)
return gv
@I.ir_module
class Expected:
@R.function
def foo(x: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"):
with R.dataflow():
@R.function
def bar(y: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"):
with R.dataflow():
lv0 = R.add(y, y)
lv1 = lv0
lv2 = R.add(lv0, lv1)
gv = lv2
R.output(gv)
return R.add(gv, gv)
# can further clean this up
# using canonicalize bindings, eliminate unused bindings, and CSE again
lv0 = bar(x)
lv1 = lv0
lv2 = R.add(lv0, lv1)
lv3 = lv0
lv4 = lv0
lv5 = R.add(lv3, lv4)
lv6 = R.add(lv2, lv5)
gv = lv6
R.output(gv)
return gv
verify(Before, Expected)
def test_call_only():
@I.ir_module
class Before:
@R.function
def foo(x: R.Tensor((160,), dtype="float32")):
with R.dataflow():
lv1 = R.arange(R.prim_value(0), R.prim_value(160), R.prim_value(1), dtype="float32")
lv2 = R.arange(R.prim_value(0), R.prim_value(160), R.prim_value(1), dtype="float32")
lv3 = R.add(x, lv1)
out = R.add(lv3, lv2)
R.output(out)
return out
@I.ir_module
class Expected:
@R.function
def foo(x: R.Tensor((160,), dtype="float32")) -> R.Tensor((160,), dtype="float32"):
with R.dataflow():
lv1 = R.arange(R.prim_value(0), R.prim_value(160), R.prim_value(1), dtype="float32")
lv2 = lv1
lv3 = R.add(x, lv1)
out = R.add(lv3, lv2)
R.output(out)
return out
verify(Before, Expected, call_only=True)
def test_cse_outside_dataflow():
# same example as previously but it will work without a dataflow wrapper
@I.ir_module
class Before:
@R.function
def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")):
lv0 = R.add(x, y)
lv1 = R.add(x, y)
gv = R.multiply(lv0, lv1)
return gv
@I.ir_module
class Expected:
@R.function
def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")):
lv0 = R.add(x, y)
lv1 = lv0
gv = R.multiply(lv0, lv1)
return gv
verify(Before, Expected)
def test_do_not_eliminate_impure():
@I.ir_module
class Before:
@R.function(pure=False)
def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")):
# it's a repeated subexpression but it would be wrong to deduplicate it
p1 = R.print(format="Message")
p2 = R.print(format="Message")
a1 = R.assert_op(R.const(False), format="Always fails")
lv0 = R.add(x, y)
lv1 = R.add(x, y)
gv = R.multiply(lv0, lv1)
a2 = R.assert_op(R.const(False), format="Always fails")
return gv
@I.ir_module
class Expected:
@R.function(pure=False)
def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")):
p1 = R.print(format="Message")
p2 = R.print(format="Message")
a1 = R.assert_op(R.const(False), format="Always fails")
lv0 = R.add(x, y)
lv1 = lv0
gv = R.multiply(lv0, lv1)
a2 = R.assert_op(R.const(False), format="Always fails")
return gv
verify(Before, Expected)
def test_do_not_eliminate_shape_expr():
@I.ir_module
class Before:
@R.function
def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")):
x = R.reshape(x, [6])
y = R.reshape(y, [6])
z = R.add(x, y)
return z
Expected = Before
verify(Before, Expected)
def test_do_not_eliminate_extern_func():
@I.ir_module
class Before:
@R.function(pure=False)
def foo(x: R.Tensor((2, 3), dtype="float32")):
y = R.call_packed("extern_func_name", x, sinfo_args=R.Tensor([2, 3]))
z = R.call_packed("extern_func_name", y, sinfo_args=R.Tensor([2, 3]))
return z
Expected = Before
verify(Before, Expected)
def test_call_tir_tuple_arg():
@I.ir_module
class Before:
@R.function
def main(A: R.Tensor([16, 16], "int32"), B: R.Tensor([16, 16], "int32")):
cls = Before
Prod = R.call_tir(cls.product, [A, B], out_sinfo=R.Tensor([16, 16], "int32"))
Sum = R.call_tir(cls.sum, [A, B], out_sinfo=R.Tensor([16, 16], "int32"))
return (Prod, Sum)
@T.prim_func(private=True)
def product(
A: T.Buffer([16, 16], "int32"),
B: T.Buffer([16, 16], "int32"),
C: T.Buffer([16, 16], "int32"),
):
for iters in T.grid(*A.shape):
with T.block("compute"):
i, j = T.axis.remap("SS", iters)
C[i, j] = A[i, j] * B[i, j]
@T.prim_func(private=True)
def sum(
A: T.Buffer([16, 16], "int32"),
B: T.Buffer([16, 16], "int32"),
C: T.Buffer([16, 16], "int32"),
):
for iters in T.grid(*A.shape):
with T.block("compute"):
i, j = T.axis.remap("SS", iters)
C[i, j] = A[i, j] + B[i, j]
Expected = Before
# If EliminateCommonSubexpr produces unnormalized expressions,
# normalization of those expressions may produce additional
# variables bindings. This test case should be agnostic to those
# additional bindings, so DCE is applied after CSE.
After = tvm.ir.transform.Sequential(
[
EliminateCommonSubexpr(),
tvm.relax.transform.DeadCodeElimination(),
]
)(Before)
tvm.ir.assert_structural_equal(Expected, After)
if __name__ == "__main__":
tvm.testing.main()