Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtest_codegen_coreml.py
More file actions
331 lines (271 loc) · 10.8 KB
/
Copy pathtest_codegen_coreml.py
File metadata and controls
331 lines (271 loc) · 10.8 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
# 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.
import importlib.util
import numpy as np
import pytest
import tvm
import tvm.testing
from tvm import relax
target, dev = "llvm", tvm.cpu()
def _has_xcode():
try:
import tvm.support.xcode
tvm.support.xcode.xcrun([])
return True
except FileNotFoundError:
pass
return False
requires_coreml_runtime = pytest.mark.skipif(
not (importlib.util.find_spec("coremltools") and _has_xcode()),
reason="coreml is not enabled.",
)
def test_partition_for_coreml_uses_current_relax_passes():
from tvm.relax.backend.metal.coreml import partition_for_coreml
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
y = relax.Var("y", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x, y]):
with bb.dataflow():
lv0 = bb.emit(relax.op.add(x, y))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
partitioned = partition_for_coreml(bb.get())
relax.analysis.well_formed(partitioned)
assert any(
getattr(func, "attrs", None) is not None
and "Codegen" in func.attrs
and str(func.attrs["Codegen"]) == "coreml"
for func in partitioned.functions.values()
)
def verify(mod, inputs):
from tvm.relax.backend.metal.coreml import partition_for_coreml
mod1 = partition_for_coreml(mod)
mod1 = relax.transform.RunCodegen()(mod1)
relax.analysis.well_formed(mod1)
assert mod1.attrs, "Should exist if offloaded successfully."
assert "external_mods" in mod1.attrs, "Should exist if offloaded successfully."
mod1 = relax.transform.LegalizeOps()(mod1)
relax.analysis.well_formed(mod1)
ex1 = tvm.compile(mod1, target=target)
vm1 = relax.VirtualMachine(ex1, dev)
out1 = vm1["main"](*inputs)
mod2 = relax.transform.LegalizeOps()(mod)
ex2 = tvm.compile(mod2, target=target)
vm2 = relax.VirtualMachine(ex2, dev)
out2 = vm2["main"](*inputs)
tvm.testing.assert_allclose(out1.numpy(), out2.numpy(), rtol=1e-3, atol=1e-3)
@requires_coreml_runtime
def test_add():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
y = relax.Var("y", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x, y]):
with bb.dataflow():
lv0 = bb.emit(relax.op.add(x, y))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
y_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data, y_data])
@requires_coreml_runtime
def test_add_const():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
y = relax.const(np.ones([10, 10]), "float32")
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.add(x, y))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data])
@requires_coreml_runtime
def test_multiply():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
y = relax.Var("y", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x, y]):
with bb.dataflow():
lv0 = bb.emit(relax.op.multiply(x, y))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
y_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data, y_data])
@requires_coreml_runtime
def test_matmul():
x = relax.Var("x", relax.TensorType([8, 10], "float32"))
y = relax.Constant(tvm.runtime.tensor(np.random.rand(10, 8).astype("float32"), dev))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.matmul(x, y))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(8, 10).astype("float32"), dev)
verify(mod, [x_data])
x = relax.Var("x", relax.TensorType([8, 10], "float32"))
y = relax.Var("y", relax.TensorType([10, 8], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x, y]):
with bb.dataflow():
lv0 = bb.emit(relax.op.matmul(x, y))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(8, 10).astype("float32"), dev)
y_data = tvm.runtime.tensor(np.random.rand(10, 8).astype("float32"), dev)
verify(mod, [x_data, y_data])
@requires_coreml_runtime
def test_clip():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.clip(x, 0, 4))
gv0 = bb.emit_output(lv0)
bb.emit_func_output(gv0)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data])
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.clip(x, 0, 4))
lv1 = bb.emit(relax.op.clip(x, 1, 3))
gv0 = bb.emit_output(lv0)
gv1 = bb.emit_output(lv1)
bb.emit_func_output([gv0, gv1])
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data])
@requires_coreml_runtime
def test_expand_dims():
def get_mod(axis):
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.expand_dims(x, axis=axis))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
return bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(get_mod(axis=0), [x_data])
verify(get_mod(axis=1), [x_data])
@requires_coreml_runtime
def test_relu():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.nn.relu(x))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data])
@requires_coreml_runtime
def test_batch_flatten():
x = relax.Var("x", relax.TensorType([10, 10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.nn.batch_flatten(x))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10, 10).astype("float32"), dev)
verify(mod, [x_data])
@requires_coreml_runtime
def test_softmax():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.nn.softmax(x))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data])
@requires_coreml_runtime
def test_conv2d():
x = relax.Var("x", relax.TensorType([1, 3, 224, 224], "float32"))
w = relax.const(np.zeros((16, 3, 3, 3), dtype="float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.nn.conv2d(x, w, strides=[2, 2], padding=[1, 1, 1, 1]))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(1, 3, 224, 224).astype("float32"), dev)
verify(mod, [x_data])
@requires_coreml_runtime
def test_global_avg_pool2d():
x = relax.Var("x", relax.TensorType([1, 1, 10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x]):
with bb.dataflow():
lv0 = bb.emit(relax.op.nn.avg_pool2d(x))
gv = bb.emit_output(lv0)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(1, 1, 10, 10).astype("float32"), dev)
verify(mod, [x_data])
@requires_coreml_runtime
def test_subgraph1():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
y = relax.Var("y", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x, y]):
with bb.dataflow():
lv0 = bb.emit(relax.op.multiply(x, y))
lv1 = bb.emit(relax.op.nn.softmax(lv0))
gv = bb.emit_output(lv1)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
y_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data, y_data])
@requires_coreml_runtime
def test_subgraph2():
x = relax.Var("x", relax.TensorType([10, 10], "float32"))
y = relax.Var("y", relax.TensorType([10, 10], "float32"))
bb = relax.BlockBuilder()
with bb.function("main", [x, y]):
with bb.dataflow():
# multiply+relu will be offloaded to coreml
lv0 = bb.emit(relax.op.multiply(x, y))
lv1 = bb.emit(relax.op.nn.relu(lv0))
# gelu wouldn't be offloaded to coreml
lv2 = bb.emit(relax.op.nn.gelu(lv1))
# relu would be offloaded to coreml
lv3 = bb.emit(relax.op.nn.relu(lv2))
gv = bb.emit_output(lv3)
bb.emit_func_output(gv)
mod = bb.get()
x_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
y_data = tvm.runtime.tensor(np.random.rand(10, 10).astype("float32"), dev)
verify(mod, [x_data, y_data])
if __name__ == "__main__":
pytest.main([__file__])
You can’t perform that action at this time.
