Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtest_expr.py
More file actions
375 lines (280 loc) · 10.5 KB
/
Copy pathtest_expr.py
File metadata and controls
375 lines (280 loc) · 10.5 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# 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.
# ruff: noqa: F811
import numpy as np
import pytest
import tvm_ffi
import tvm
from tvm import relax as rx
from tvm import tirx
from tvm.relax.expr import make_shape
from tvm.script import relax as R
def _check_equal(x, y, map_free_vars=False):
tvm.ir.assert_structural_equal(x, y, map_free_vars)
tvm.ir.assert_structural_equal(y, x, map_free_vars)
xhash = tvm_ffi.structural_hash(x, map_free_vars)
yhash = tvm_ffi.structural_hash(y, map_free_vars)
assert xhash == yhash
def _check_json_roundtrip(x):
xret = tvm.ir.load_json(tvm.ir.save_json(x))
_check_equal(x, xret, map_free_vars=True)
return xret
def _check_type_missing(ty):
assert isinstance(ty, tvm.ir.Type)
assert ty.is_missing()
def test_var() -> None:
v0 = rx.Var("v0")
assert v0.name_hint == "v0"
_check_type_missing(v0.ty)
shape = [54, 96]
v1 = rx.Var("v1", R.Tensor(shape, "float32"))
assert v1.name_hint == "v1"
for s0, s1 in zip(v1.ty.shape, shape):
assert s0 == s1
tvm.ir.assert_structural_equal(v1.ty, rx.TensorType(shape, "float32"))
def test_tensor_type_empty_dtype_is_unknown() -> None:
tvm.ir.assert_structural_equal(rx.TensorType([1, 2], dtype=""), rx.TensorType([1, 2], None))
def test_relax_expr_ty_running_example() -> None:
m = tirx.Var("m", "int64")
x = rx.Var("x", R.Tensor([m, 16], "float32"))
assert isinstance(x.ty, tvm.ir.Type)
assert x.ty.dtype == "float32"
assert x.ty.ndim == 2
call = rx.op.add(x, x)
_check_type_missing(call.ty)
bb = rx.BlockBuilder()
normalized = bb.normalize(call)
assert isinstance(normalized.ty, tvm.ir.Type)
tvm.ir.assert_structural_equal(normalized.ty, x.ty)
def test_dataflow_var() -> None:
v0 = rx.DataflowVar("v0")
assert v0.name_hint == "v0"
_check_type_missing(v0.ty)
shape = [54, 96]
v1 = rx.DataflowVar("v1", R.Tensor(shape, "float16"))
assert v1.name_hint == "v1"
assert isinstance(v1, rx.DataflowVar)
tvm.ir.assert_structural_equal(v1.ty, rx.TensorType(shape, "float16"))
def test_tuple() -> None:
v0 = rx.Var("v0")
v1 = rx.Var("v1")
t = rx.Tuple((v0, v1))
assert t.fields[0] == v0
assert t.fields[1] == v1
assert t[0] == v0
assert t[1] == v1
assert t[-1] == v1
assert t[-2] == v0
with pytest.raises(IndexError, match="Tuple index out of range"):
t[2]
with pytest.raises(IndexError, match="Tuple index out of range"):
t[-3]
def test_tuple_ty_inferred_on_construction():
v0 = rx.Var("v0", rx.AnyType())
v1 = rx.Var("v1", rx.AnyType())
tup = rx.Tuple((v0, v1))
assert tup.ty is not None
tvm.ir.assert_structural_equal(tup.ty, rx.TupleType([rx.AnyType(), rx.AnyType()]))
def test_tuple_ty_requires_fields_with_known_ty():
v0 = rx.Var("v0", rx.AnyType())
v1 = rx.Var("v1")
tup = rx.Tuple((v0, v1))
_check_type_missing(tup.ty)
def test_match_cast() -> None:
# match_cast([16, 8], [m, n])
m = tirx.Var("m", dtype="int64")
n = tirx.Var("n", dtype="int64")
shape = rx.const([16, 8], "int32")
var = rx.Var("v0", R.Shape())
b0 = rx.MatchCast(var, shape, R.Tensor([m, n], "int32"))
assert b0.value == shape
assert b0.pattern[0] == m
assert b0.pattern[1] == n
assert b0.var is not None
# var1: R.Tensor((m, n), "float32") =
# match_cast(var0: R.Tensor("float32", ndim=-1), R.Tensor((m, n), "float32"))
value = rx.Var("value", R.Tensor("float32", ndim=-1))
var = rx.Var("v1", R.Tensor([m, n], "float32"))
b1 = rx.MatchCast(var, value, R.Tensor([m, n], "float32"))
assert b1.value == value
assert b1.pattern[0] == m
assert b1.pattern[1] == n
assert b1.var is not None
def test_match_cast() -> None:
m = tirx.Var("m", dtype="int64")
n = tirx.Var("n", dtype="int64")
ivalue = rx.Var("input_value")
ty = rx.TensorType([n, m], "float32")
b0 = rx.MatchCast(rx.Var("v"), ivalue, ty)
assert b0.value.same_as(ivalue)
assert b0.ty == ty
_check_json_roundtrip(b0)
def test_var_binding() -> None:
v0 = rx.Var("v0")
val = rx.const(np.random.rand(24, 56))
b0 = rx.VarBinding(v0, val)
assert b0.var.name_hint == "v0"
assert b0.value == val
def test_binding_block() -> None:
m = tirx.Var("m", dtype="int64")
n = tirx.Var("n", dtype="int64")
shape = rx.const([16, 8], "int32")
b0 = rx.MatchCast(rx.Var("v0"), shape, R.Tensor([m, n], "int32"))
v0 = rx.Var("v0")
val = rx.const(np.random.rand(24, 56))
b1 = rx.VarBinding(v0, val)
block0 = rx.BindingBlock([b0, b1])
assert block0.bindings[0] == b0
assert block0.bindings[1] == b1
def test_dataflow_block() -> None:
m = tirx.Var("m", dtype="int64")
n = tirx.Var("n", dtype="int64")
shape = rx.const([16, 8], "int32")
b0 = rx.MatchCast(rx.Var("v0"), shape, R.Tensor([m, n], "int32"))
v0 = rx.Var("v0")
val = rx.const(np.random.rand(24, 56))
b1 = rx.VarBinding(v0, val)
block0 = rx.DataflowBlock([b0, b1])
assert block0.bindings[0] == b0
assert block0.bindings[1] == b1
assert isinstance(block0, rx.DataflowBlock)
def test_seq_expr() -> None:
x = rx.Var("foo")
bindings = [rx.VarBinding(x, rx.const(1))]
blocks = [rx.BindingBlock(bindings)]
seqe = rx.SeqExpr(blocks, x)
assert seqe.blocks[0] == blocks[0]
assert seqe.body == x
def test_func():
x = rx.Var("foo", R.Tensor(dtype="float32", ndim=2))
bindings = [rx.VarBinding(x, rx.const(1))]
blocks = [rx.BindingBlock(bindings)]
seqe = rx.SeqExpr(blocks, x)
ret_ty = R.Tensor(dtype="float32", ndim=-1)
func = rx.Function([x], seqe, ret_ty)
func = func.with_attr("global_symbol", "func")
assert func.params[0] == x
assert func.body == seqe
assert func.ret_ty == ret_ty
assert func.attrs["global_symbol"] == "func"
def test_shape_of():
shape = [96, 54]
v1 = rx.Var("v1", R.Tensor(shape))
s1 = rx.get_shape_of(v1)
for x, y in zip(shape, s1):
assert x == y
def test_shape_expr():
m = tirx.Var("m", dtype="int64")
n = tirx.Var("n", dtype="int64")
s = rx.ShapeExpr([m, n])
assert s.values[0] == m
assert s.values[1] == n
assert s[0] == m
assert s[1] == n
assert s[-1] == n
assert s[-2] == m
assert isinstance(s.ty, rx.ShapeType)
with pytest.raises(IndexError, match="ShapeExpr index out of range"):
s[2]
with pytest.raises(IndexError, match="ShapeExpr index out of range"):
s[-3]
shape_expr = rx.ShapeExpr([10, 20])
assert shape_expr.values[0] == 10
assert shape_expr.values[1] == 20
tvm.ir.assert_structural_equal(shape_expr.ty, R.Shape((10, 20)))
x = rx.Var("v0", R.Tensor((10, 20), "float32"))
assert x.ty.shape[0] == 10
assert x.ty.shape[1] == 20
tvm.ir.assert_structural_equal(x.ty.shape.ty, R.Shape((10, 20)))
m = tirx.Var("m", "int32")
with pytest.raises(RuntimeError, match="the value in ShapeType can only have dtype of int64"):
rx.ShapeExpr([m, 3])
def test_prim_value_helper_from_python_scalar():
int_value = rx.prim_value(1)
assert isinstance(int_value, tirx.IntImm)
tvm.ir.assert_structural_equal(int_value.ty, tvm.ir.PrimType("int64"))
assert int_value.value == 1
np_int_value = rx.prim_value(np.int32(2))
assert isinstance(np_int_value, tirx.IntImm)
tvm.ir.assert_structural_equal(np_int_value.ty, tvm.ir.PrimType("int64"))
assert np_int_value.value == 2
float_value = rx.prim_value(1.0)
assert isinstance(float_value, tirx.FloatImm)
tvm.ir.assert_structural_equal(float_value.ty, tvm.ir.PrimType("float64"))
assert float_value.value == 1.0
np_float_value = rx.prim_value(np.float32(1.5))
assert isinstance(np_float_value, tirx.FloatImm)
tvm.ir.assert_structural_equal(np_float_value.ty, tvm.ir.PrimType("float64"))
assert np_float_value.value == 1.5
def test_prim_value_helper_preserves_prim_expr():
float_imm = tirx.FloatImm("float32", 1.0)
assert rx.prim_value(float_imm).same_as(float_imm)
assert R.prim_value(float_imm).same_as(float_imm)
n = tirx.Var("n", "int64")
expr = n + 1
assert rx.prim_value(n).same_as(n)
assert rx.prim_value(expr).same_as(expr)
def test_prim_value_helper_rejects_relax_expr():
with pytest.raises(TypeError, match="Cannot convert"):
rx.prim_value(rx.Var("x"))
def test_string_imm():
s0 = rx.StringImm("hello")
s1 = rx.StringImm("hello")
assert s0.value == "hello"
_check_equal(s0, s1)
_check_json_roundtrip(s0)
def test_datatype_imm():
d0 = rx.DataTypeImm("int32")
d1 = rx.DataTypeImm("int32")
assert d0.value == "int32"
_check_equal(d0, d1)
_check_json_roundtrip(d0)
def test_call():
dtype = tvm.ir.PrimType("int32")
func = rx.Var("func", rx.FuncType([dtype], dtype))
arg = rx.Var("arg", dtype)
call = rx.Call(func, [arg])
assert call.op.same_as(func)
assert len(call.args) == 1
assert call.args[0].same_as(arg)
def test_call_accepts_core_expr_operator():
"""relax.Call aliases the core ir.Call constructor."""
dtype = tvm.ir.PrimType("int32")
func = rx.Var("func", dtype)
arg = rx.Var("arg", dtype)
call = rx.Call(func, [arg])
assert call.op.same_as(func)
assert len(call.args) == 1
assert call.args[0].same_as(arg)
_check_type_missing(call.ty)
def test_call_raises_error_for_missing_operator():
"""relax::Call requires a defined operator."""
with pytest.raises(ValueError, match="defined operator"):
rx.Call(None, [])
if __name__ == "__main__":
tvm.testing.main()
def test_make_shape_invalid_type():
with pytest.raises(TypeError):
make_shape(123)
def test_make_shape_valid_list():
shape = make_shape([1, 2, 3])
assert len(shape) == 3
def test_make_shape_valid_tuple():
shape = make_shape((4, 5))
assert len(shape) == 2
if __name__ == "__main__":
tvm.testing.main()
You can’t perform that action at this time.
