def test_let_as_subexpr(): def on_cpu(x): return relay.annotation.on_device(x, tvm.device("cpu"), constrain_result=True) x = relay.Var("x", relay.IncompleteType()) c = relay.const(1) l = relay.Let(x, on_cpu(c + c), x) body = l * l anf = run_opt_pass(body, [transform.ToANormalForm(), transform.InferType()]) v0 = relay.Var("v0", relay.IncompleteType()) v1 = relay.Var("v1", relay.IncompleteType()) v2 = relay.Var("v2", relay.IncompleteType()) expected_output = relay.Let( v0, on_cpu(c), relay.Let( x, on_cpu(v0 + v0), relay.Let(v1, x, relay.Let(v2, v1 * v1, v2)), ), ) expected_output = run_opt_pass(expected_output, transform.InferType()) tvm.ir.assert_structural_equal(anf, expected_output)
def test_incomplete_type_alpha_equal(): t1 = relay.IncompleteType(relay.Kind.Shape) t2 = relay.IncompleteType(relay.Kind.Type) t3 = relay.IncompleteType(relay.Kind.Type) # only equal when there is pointer equality assert t2 == t2 assert t1 == t1 assert t1 != t2 assert t2 != t3
def test_incompatible_typecall_var_unification(): solver = make_solver() gtv1 = relay.GlobalTypeVar("gtv1") gtv2 = relay.GlobalTypeVar("gtv2") t1 = relay.IncompleteType() t2 = relay.IncompleteType() tc1 = relay.TypeCall(gtv1, [t1]) tc2 = relay.TypeCall(gtv2, [t2]) solver.Unify(tc1, tc2)
def test_incompatible_typecall_args_unification(): solver = make_solver() gtv = relay.GlobalTypeVar("gtv1") t1 = relay.IncompleteType() t2 = relay.IncompleteType() tensor1 = relay.TensorType((1, 2, 3), "float32") tensor2 = relay.TensorType((2, 3), "float32") tensor3 = relay.TensorType((3, ), "float32") tc1 = relay.TypeCall(gtv, [relay.TupleType([t1, t1]), t2]) tc2 = relay.TypeCall(gtv, [relay.TupleType([tensor1, tensor2]), tensor3]) solver.Unify(tc1, tc2)
def test_if(): cond = relay.const(True) x = relay.If(cond, relay.const(2), relay.const(3)) anf = infer_type(to_a_normal_form(x)) a = relay.Var('a', relay.IncompleteType()) b = relay.Var('b', relay.IncompleteType()) c = relay.Var('c', relay.IncompleteType()) d = relay.Var('d', relay.IncompleteType()) true_branch = relay.Let(a, relay.const(2), a) false_branch = relay.Let(b, relay.const(3), b) expected_output = relay.If(c, true_branch, false_branch) expected_output = relay.Let(d, expected_output, d) expected_output = relay.Let(c, cond, expected_output) expected_output = infer_type(expected_output) assert alpha_equal(anf, expected_output)
def test_if(): cond = relay.const(True) x = relay.If(cond, relay.const(2), relay.const(3)) anf = run_opt_pass(x, [transform.ToANormalForm(), transform.InferType()]) a = relay.Var('a', relay.IncompleteType()) b = relay.Var('b', relay.IncompleteType()) c = relay.Var('c', relay.IncompleteType()) d = relay.Var('d', relay.IncompleteType()) true_branch = relay.Let(a, relay.const(2), a) false_branch = relay.Let(b, relay.const(3), b) expected_output = relay.If(c, true_branch, false_branch) expected_output = relay.Let(d, expected_output, d) expected_output = relay.Let(c, cond, expected_output) expected_output = run_opt_pass(expected_output, transform.InferType()) assert tvm.ir.structural_equal(anf, expected_output)
def test_conv2d_transpose_infer_type(): # symbolic in batch dimension n, c, h, w = tvm.var("n"), 10, 10, 12 x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) w = relay.var("w", relay.IncompleteType()) y = relay.nn.conv2d_transpose(x, w, kernel_size=(3, 3), padding=(1, 1), channels=15) assert "channels=15" in y.astext() yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, 15, 10, 12), "float32") assert yy.args[1].checked_type == relay.TensorType((10, 15, 3, 3), "float32") # infer by shape of w, mixed precision n, h, w, c = tvm.var("n"), 10, 10, 12 x = relay.var("x", relay.TensorType((n, h, w, c), "float32")) w = relay.var("w", relay.TensorType((12, 11, 5, 5), "float32")) y = relay.nn.conv2d_transpose(x, w, output_padding=(1, 1), channels=11, data_layout="NHWC") yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, 15, 15, 11), "float32")
def verify_infer_type_prelu(data, alpha, axis, output, dtype='float32'): x = relay.var('data', relay.TensorType(data, dtype)) if alpha: y = relay.var('alpha', relay.TensorType(alpha, dtype)) else: y = relay.var('alpha', relay.IncompleteType()) z = relay.nn.prelu(x, y, axis=axis) zz = run_infer_type(z) if (axis != 1): assert ('axis' in z.astext()) assert (zz.checked_type == relay.ty.TensorType(output, dtype)) if (not alpha): axis = (axis if axis else 1) alpha_shape = (data[axis],) assert (zz.args[1].checked_type == relay.TensorType(alpha_shape, 'float32')) if (all(((isinstance(v, tvm.tir.Var) == 1) for v in data)) or (not alpha)): return func = relay.Function([x, y], z) x_data = np.random.uniform(low=(- 1), high=1, size=data).astype(dtype) a_data = np.random.uniform(low=(- 1), high=1, size=alpha).astype(dtype) if (axis == 1): ref_res = (((x_data < 0) * (x_data * a_data.reshape(3, 1, 1))) + ((x_data >= 0) * x_data)) else: ref_res = (((x_data < 0) * (x_data * a_data.reshape(1, 1, 3))) + ((x_data >= 0) * x_data)) for (target, ctx) in tvm.testing.enabled_targets(): intrp1 = relay.create_executor('graph', ctx=ctx, target=target) intrp2 = relay.create_executor('debug', ctx=ctx, target=target) op_res1 = intrp1.evaluate(func)(x_data, a_data) tvm.testing.assert_allclose(op_res1.asnumpy(), ref_res, rtol=1e-05) op_res2 = intrp2.evaluate(func)(x_data, a_data) tvm.testing.assert_allclose(op_res2.asnumpy(), ref_res, rtol=1e-05)
def verify_infer_type_prelu(data, alpha, axis, output, dtype="float32"): x = relay.var("data", relay.TensorType(data, dtype)) if alpha: y = relay.var("alpha", relay.TensorType(alpha, dtype)) else: y = relay.var("alpha", relay.IncompleteType()) z = relay.nn.prelu(x, y, axis=axis) zz = relay.ir_pass.infer_type(z) if axis != 1: assert "axis" in z.astext() assert zz.checked_type == relay.ty.TensorType(output, dtype) if not alpha: axis = axis if axis else 1 alpha_shape = (data[axis],) assert zz.args[1].checked_type == relay.TensorType(alpha_shape, "float32") if all(isinstance(v, tvm.expr.Var) == 1 for v in data) or not alpha: return func = relay.Function([x, y], z) x_data = np.random.uniform(low=-1, high=1, size=data).astype(dtype) a_data = np.random.uniform(low=-1, high=1, size=alpha).astype(dtype) if axis == 1: ref_res = (x_data < 0) * (x_data * a_data.reshape(3, 1, 1)) + (x_data>=0) * x_data else: ref_res = (x_data < 0) * (x_data * a_data.reshape(1, 1, 3)) + (x_data>=0) * x_data for target, ctx in ctx_list(): intrp1 = relay.create_executor("graph", ctx=ctx, target=target) intrp2 = relay.create_executor("debug", ctx=ctx, target=target) op_res1 = intrp1.evaluate(func)(x_data, a_data) tvm.testing.assert_allclose(op_res1.asnumpy(), ref_res, rtol=1e-5) op_res2 = intrp2.evaluate(func)(x_data, a_data) tvm.testing.assert_allclose(op_res2.asnumpy(), ref_res, rtol=1e-5)
def before(): x = relay.var("x", shape=(1, 64, 56, 56)) weight = relay.var("weight") alpha = relay.var("alpha", relay.IncompleteType()) y = relay.nn.conv2d(x, weight, channels=64, kernel_size=(3, 3), padding=(1, 1)) y = relay.nn.prelu(y, alpha) y = relay.Function(analysis.free_vars(y), y) return y
def test_if(): cond = relay.const(True) x = relay.If(cond, relay.const(2), relay.const(3)) anf = transform.OptimizeOnExpr( x, [transform.ToANormalForm(), transform.InferType()]) a = relay.Var('a', relay.IncompleteType()) b = relay.Var('b', relay.IncompleteType()) c = relay.Var('c', relay.IncompleteType()) d = relay.Var('d', relay.IncompleteType()) true_branch = relay.Let(a, relay.const(2), a) false_branch = relay.Let(b, relay.const(3), b) expected_output = relay.If(c, true_branch, false_branch) expected_output = relay.Let(d, expected_output, d) expected_output = relay.Let(c, cond, expected_output) expected_output = transform.OptimizeOnExpr(expected_output, transform.InferType()) assert alpha_equal(anf, expected_output)
def test_order(): z = relay.const(3) y = relay.const(2) x = relay.const(1) val = x + y * z check_eval(val, 7.0) anf = run_opt_pass(val, [transform.ToANormalForm(), transform.InferType()]) a = relay.Var('a', relay.IncompleteType()) b = relay.Var('b', relay.IncompleteType()) c = relay.Var('c', relay.IncompleteType()) d = relay.Var('d', relay.IncompleteType()) e = relay.Var('e', relay.IncompleteType()) expected_output = e expected_output = relay.Let(e, a + d, expected_output) expected_output = relay.Let(d, b * c, expected_output) expected_output = relay.Let(c, z, expected_output) expected_output = relay.Let(b, y, expected_output) expected_output = relay.Let(a, x, expected_output) expected_output = run_opt_pass(expected_output, transform.InferType()) assert tvm.ir.structural_equal(anf, expected_output)
def test_order(): z = relay.const(3) y = relay.const(2) x = relay.const(1) val = x + y * z check_eval(val, 7.0) anf = infer_type(to_a_normal_form(val)) a = relay.Var('a', relay.IncompleteType()) b = relay.Var('b', relay.IncompleteType()) c = relay.Var('c', relay.IncompleteType()) d = relay.Var('d', relay.IncompleteType()) e = relay.Var('e', relay.IncompleteType()) expected_output = e expected_output = relay.Let(e, a + d, expected_output) expected_output = relay.Let(d, b * c, expected_output) expected_output = relay.Let(c, z, expected_output) expected_output = relay.Let(b, y, expected_output) expected_output = relay.Let(a, x, expected_output) expected_output = infer_type(expected_output) assert alpha_equal(anf, expected_output)
def expected(): x = relay.var("x", shape=(1, 64, 56, 56)) w = relay.var("weight") alpha = relay.var("alpha", relay.IncompleteType()) y = relay.layout_transform(x, "NCHW", "NCHW16c") y = relay.nn.conv2d( y, w, channels=64, kernel_size=(3, 3), padding=(1, 1), data_layout="NCHW16c" ) y = relay.layout_transform(y, "NCHW16c", "NCHW") y = relay.nn.prelu(y, alpha) y = relay.Function(analysis.free_vars(y), y) return y
def test_order(): z = relay.const(3) y = relay.const(2) x = relay.const(1) val = x + y * z check_eval(val, 7.0) anf = transform.OptimizeOnExpr( val, [transform.ToANormalForm(), transform.InferType()]) a = relay.Var('a', relay.IncompleteType()) b = relay.Var('b', relay.IncompleteType()) c = relay.Var('c', relay.IncompleteType()) d = relay.Var('d', relay.IncompleteType()) e = relay.Var('e', relay.IncompleteType()) expected_output = e expected_output = relay.Let(e, a + d, expected_output) expected_output = relay.Let(d, b * c, expected_output) expected_output = relay.Let(c, z, expected_output) expected_output = relay.Let(b, y, expected_output) expected_output = relay.Let(a, x, expected_output) expected_output = transform.OptimizeOnExpr(expected_output, transform.InferType()) assert alpha_equal(anf, expected_output)
def test_dense(): for dtype in ["float16", "float32"]: # Dense accuracy for float16 is poor if dtype == "float16": continue n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var( "h"), te.size_var("w") x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) w = relay.var("w", relay.TensorType((2, w), dtype)) y = relay.nn.dense(x, w, units=2) assert "units=2" in y.astext() yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), dtype) n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) wh, ww = te.size_var("wh"), te.size_var("ww") w = relay.var("w", relay.TensorType((ww, wh), dtype)) y = relay.nn.dense(x, w) yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, ww), dtype) n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) w = relay.var("w", relay.IncompleteType()) y = relay.nn.dense(x, w, units=2) yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), dtype) x = relay.var("x", shape=(10, 5), dtype=dtype) w = relay.var("w", shape=(2, 5), dtype=dtype) z = relay.nn.dense(x, w) # Check result. func = relay.Function([x, w], z) x_data = np.random.rand(10, 5).astype(dtype) w_data = np.random.rand(2, 5).astype(dtype) ref_res = np.dot(x_data, w_data.T) for target, dev in tvm.testing.enabled_targets(): op_res1 = relay.create_executor( "graph", device=dev, target=target).evaluate(func)(x_data, w_data) tvm.testing.assert_allclose(op_res1.numpy(), ref_res, rtol=1e-5) op_res2 = relay.create_executor( "debug", device=dev, target=target).evaluate(func)(x_data, w_data) tvm.testing.assert_allclose(op_res2.numpy(), ref_res, rtol=1e-5)
def verify_infer_type_prelu(data, alpha, axis, output, dtype="float32"): x = relay.var("data", relay.TensorType(data, dtype)) if alpha: y = relay.var("alpha", relay.TensorType(alpha, dtype)) else: y = relay.var("alpha", relay.IncompleteType()) z = relay.nn.prelu(x, y, axis=axis) zz = relay.ir_pass.infer_type(z) if axis != 1: assert "axis" in z.astext() assert zz.checked_type == relay.ty.TensorType(output, dtype) if not alpha: axis = axis if axis else 1 alpha_shape = (data[axis], ) assert zz.args[1].checked_type == relay.TensorType( alpha_shape, "float32")
def test_dense(): for dtype in ['float16', 'float32']: # Dense accuracy for float16 is poor if dtype == 'float16': return n, c, h, w = tvm.size_var("n"), tvm.size_var("c"), tvm.size_var( "h"), tvm.size_var("w") x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) w = relay.var("w", relay.TensorType((2, w), dtype)) y = relay.nn.dense(x, w, units=2) assert "units=2" in y.astext() yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), dtype) n, c, h, w = tvm.size_var("n"), tvm.size_var("c"), tvm.size_var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) wh, ww = tvm.size_var("wh"), tvm.size_var("ww") w = relay.var("w", relay.TensorType((ww, wh), dtype)) y = relay.nn.dense(x, w) yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, ww), dtype) n, c, h, w = tvm.size_var("n"), tvm.size_var("c"), tvm.size_var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) w = relay.var("w", relay.IncompleteType()) y = relay.nn.dense(x, w, units=2) yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), dtype) x = relay.var("x", shape=(10, 5), dtype=dtype) w = relay.var("w", shape=(2, 5), dtype=dtype) z = relay.nn.dense(x, w) # Check result. func = relay.Function([x, w], z) x_data = np.random.rand(10, 5).astype(dtype) w_data = np.random.rand(2, 5).astype(dtype) ref_res = np.dot(x_data, w_data.T) for target, ctx in ctx_list(): intrp1 = relay.create_executor("graph", ctx=ctx, target=target) intrp2 = relay.create_executor("debug", ctx=ctx, target=target) op_res1 = intrp1.evaluate(func)(x_data, w_data) tvm.testing.assert_allclose(op_res1.asnumpy(), ref_res, rtol=1e-5) op_res2 = intrp2.evaluate(func)(x_data, w_data) tvm.testing.assert_allclose(op_res2.asnumpy(), ref_res, rtol=1e-5)
def test_matmul(executor_kind): for dtype in ["float16", "float32"]: # Matmul accuracy for float16 is poor if dtype == "float16": continue n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var( "h"), te.size_var("w") x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) w = relay.var("w", relay.TensorType((2, w), dtype)) y = relay.nn.matmul(x, w, units=2, transpose_b=True) assert "units=2" in y.astext() yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), dtype) n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var("h"), 2 x = relay.var("x", relay.TensorType((n, c, w, h), dtype)) wh, ww = te.size_var("wh"), te.size_var("ww") w = relay.var("w", relay.TensorType((wh, ww), dtype)) y = relay.nn.matmul(x, w, transpose_a=True) yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, ww), dtype) n, c, h, w = te.size_var("n"), te.size_var("c"), te.size_var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), dtype)) w = relay.var("w", relay.IncompleteType()) y = relay.nn.matmul(x, w, units=2) yy = run_infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), dtype) x = relay.var("x", shape=(5, 10), dtype=dtype) w = relay.var("w", shape=(5, 2), dtype=dtype) z = relay.nn.matmul(x, w, transpose_a=True) # Check result. func = relay.Function([x, w], z) x_data = np.random.rand(5, 10).astype(dtype) w_data = np.random.rand(5, 2).astype(dtype) ref_res = np.dot(x_data.transpose(), w_data) for target, dev in tvm.testing.enabled_targets(): op_res = relay.create_executor(executor_kind, device=dev, target=target).evaluate(func)( x_data, w_data) tvm.testing.assert_allclose(op_res.numpy(), ref_res, rtol=1e-5)
def test_dense(): n, c, h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), tvm.var("w") x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) w = relay.var("w", relay.TensorType((2, w), "float32")) y = relay.nn.dense(x, w, units=2) "units=2" in y.astext() yy = relay.ir_pass.infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), "float32") n, c, h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) wh, ww = tvm.var("wh"), tvm.var("ww") w = relay.var("w", relay.TensorType((ww, wh), "float32")) y = relay.nn.dense(x, w) yy = relay.ir_pass.infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, ww), "float32") n, c, h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) w = relay.var("w", relay.IncompleteType()) y = relay.nn.dense(x, w, units=2) yy = relay.ir_pass.infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), "float32") x = relay.var("x", shape=(10, 5)) w = relay.var("w", shape=(2, 5)) z = relay.nn.dense(x, w) # Check result. func = relay.Function([x, w], z) x_data = np.random.rand(10, 5).astype('float32') w_data = np.random.rand(2, 5).astype('float32') ref_res = np.dot(x_data, w_data.T) for target, ctx in ctx_list(): intrp1 = relay.create_executor("graph", ctx=ctx, target=target) intrp2 = relay.create_executor("debug", ctx=ctx, target=target) op_res1 = intrp1.evaluate(func)(x_data, w_data) tvm.testing.assert_allclose(op_res1.asnumpy(), ref_res, rtol=1e-5) op_res2 = intrp2.evaluate(func)(x_data, w_data) tvm.testing.assert_allclose(op_res2.asnumpy(), ref_res, rtol=1e-5)
def test_dense_infer_type(): n, c , h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), tvm.var("w") x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) w = relay.var("w", relay.TensorType((w, 2), "float32")) y = relay.nn.dense(x, w, units=2) "units=2" in y.astext() yy = relay.ir_pass.infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), "float32") n, c , h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) wh, ww = tvm.var("wh"), tvm.var("ww") w = relay.var("w", relay.TensorType((wh, ww), "float32")) y = relay.nn.dense(x, w) yy = relay.ir_pass.infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, ww), "float32") n, c , h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), 2 x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) w = relay.var("w", relay.IncompleteType()) y = relay.nn.dense(x, w, units=2) yy = relay.ir_pass.infer_type(y) assert yy.checked_type == relay.TensorType((n, c, h, 2), "float32")