def test_any_shape_of(): x = relay.var("x", shape=any_dims(2), dtype="float32") y = relay.shape_of(x) mod = tvm.IRModule() mod["main"] = relay.Function([x], y) data = np.random.uniform(size=(3, 4)).astype("float32") check_result([data], mod, np.array([3, 4]).astype("int64")) x = relay.var("x", shape=any_dims(3), dtype="float32") y0 = relay.shape_of(x) y1 = relay.take(y0, relay.const(1, "int32")) mod = tvm.IRModule() mod["main"] = relay.Function([x], y1) data = np.random.uniform(size=(2, 3, 4)).astype("float32") check_result([data], mod, np.array(3).astype("int64"))
def verify_reshape(shape, newshape): x = relay.var("x", relay.TensorType(shape, "float32")) y = relay.var("y", relay.TensorType(newshape, "float32")) z = relay.reshape(x, relay.shape_of(y)) z = relay.reshape(z, relay.shape_of(x)) func = run_infer_type(relay.Function([x, y], z)) func2 = run_opt_pass(run_opt_pass(func, transform.DynamicToStatic()), transform.InferType()) zz = func2.body assert isinstance(zz, relay.Call) assert zz.op == relay.op.get("reshape") assert "newshape=" in zz.astext() assert zz.checked_type == relay.ty.TensorType(shape, "float32") x_data = np.random.uniform(low=-1, high=1, size=shape).astype("float32") y_data = np.random.uniform(low=-1, high=1, size=newshape).astype("float32") verify_func(func2, [x_data, y_data], x_data)
def test_arange_with_dynamic_shape(): m, n, k = relay.ShapeVar('m'), relay.ShapeVar('n'), relay.ShapeVar('k') x = relay.var('x', shape=(m.var, n.var, k.var), dtype='float32') y0 = relay.shape_of(x) y1 = relay.take(y0, relay.const(0, 'int32')) y2 = relay.op.arange(y1) ex = relay.create_executor() f = relay.Function([x], y2, type_params=[m, n, k])
def verify_full(fill_value, fill_shape, dtype): x = relay.var("x", relay.scalar_type(dtype)) y = relay.var("y", relay.TensorType(fill_shape, "int64")) shape = relay.shape_of(y) shape = relay.strided_slice(shape, [0], relay.shape_of(shape)) z = relay.full(x, shape, dtype) func = relay.Function([x, y], z) func2 = run_opt_pass(run_opt_pass(func, transform.DynamicToStatic()), transform.InferType()) zz = func2.body assert isinstance(zz, relay.Call) assert zz.op == relay.op.get("full") ref_res = np.full(fill_shape, fill_value).astype(dtype) y_data = np.random.uniform(low=-1, high=1, size=fill_shape).astype("int64") verify_func(func2, [fill_value, y_data], ref_res)
def verify_reshape(shape, newshape, oshape): x = relay.var("x", relay.TensorType(shape, "float32")) y = relay.var("y", relay.TensorType(newshape, "float32")) z = relay.reshape(x, relay.shape_of(y)) func = relay.Function([x, y], z) x_data = np.random.uniform(low=-1, high=1, size=shape).astype("float32") y_data = np.random.uniform(low=-1, high=1, size=newshape).astype("float32") ref_res = np.reshape(x_data, oshape) verify_func(func, [x_data, y_data], ref_res)
def test_arange_with_dynamic_shape(): # m, n, k = relay.ShapeVar('m'), relay.ShapeVar('n'), relay.ShapeVar('k') m, n, k = relay.Any(), relay.Any(), relay.Any() x = relay.var("x", shape=(m, n, k), dtype="float32") y0 = relay.shape_of(x) y1 = relay.take(y0, relay.const(0, "int32")) y2 = relay.op.arange(y1, dtype="int32") y3 = y2 + relay.const(1, dtype="int32") data = np.random.rand(10, 5, 3).astype("float32") mod = tvm.IRModule() mod["main"] = relay.Function([x], y3) check_result([data], mod, np.array(range(10)).astype("int32") + 1)
def test_any_shape_of(): x = relay.var('x', shape=any_dims(2), dtype='float32') y = relay.shape_of(x) mod = tvm.IRModule() mod["main"] = relay.Function([x], y) data = np.random.uniform(size=(3, 4)).astype('float32') for kind in ["debug", "vm"]: ex = relay.create_executor(kind, mod=mod, ctx=tvm.cpu(), target="llvm") result = ex.evaluate()(data) tvm.testing.assert_allclose(result.asnumpy(), np.array([3,4]).astype("int64")) x = relay.var('x', shape=any_dims(3), dtype='float32') y0 = relay.shape_of(x) y1 = relay.take(y0, relay.const(1, 'int32')) mod = tvm.IRModule() mod["main"] = relay.Function([x], y1) data = np.random.uniform(size=(2, 3, 4)).astype('float32') for kind in ["debug", "vm"]: ex = relay.create_executor(kind, mod=mod, ctx=tvm.cpu(), target="llvm") result = ex.evaluate()(data) tvm.testing.assert_allclose(result.asnumpy(), np.array(3).astype("int64"))
def verify_reshape(shape, newshape, oshape): x = relay.var("x", relay.TensorType(shape, "float32")) y = relay.var("y", relay.TensorType(newshape, "float32")) z = relay.reshape(x, relay.shape_of(y)) func = relay.Function([x, y], z) x_data = np.random.uniform(low=-1, high=1, size=shape).astype("float32") y_data = np.random.uniform(low=-1, high=1, size=newshape).astype("float32") ref_res = np.reshape(x_data, oshape) check_grad(run_infer_type(func), inputs=[x_data, y_data], eps=1e-3) verify_func(executor_kind, func, [x_data, y_data], ref_res)
def test_arange_with_dynamic_shape(): m, n, k = relay.ShapeVar('m'), relay.ShapeVar('n'), relay.ShapeVar('k') x = relay.var('x', shape=(m.var, n.var, k.var), dtype='float32') y0 = relay.shape_of(x) y1 = relay.take(y0, relay.const(0, 'int32')) y2 = relay.op.arange(y1, dtype="int32") y3 = y2 + relay.const(1, dtype="int32") data = np.random.rand(10, 5, 3).astype('float32') mod = relay.module.Module() mod["main"] = relay.Function([x], y3, type_params=[m, n, k]) for kind in ["debug", "vm"]: ex = relay.create_executor(kind, mod=mod, ctx=tvm.cpu(), target="llvm") result = ex.evaluate()(data) tvm.testing.assert_allclose(result.asnumpy(), np.array(range(10)).astype("int32") + 1)
def verify_ones_zeros(shape, dtype): for op, ref in [(relay.zeros, np.zeros), (relay.ones, np.ones)]: x = relay.var("x", relay.TensorType(shape, dtype)) y = op(relay.shape_of(x), dtype) func = run_infer_type(relay.Function([x], y)) func2 = run_opt_pass(run_opt_pass(func, transform.DynamicToStatic()), transform.InferType()) zz = func2.body assert isinstance(zz, relay.Constant) assert zz.checked_type == relay.ty.TensorType(shape, dtype) x_data = np.random.uniform(low=1, high=1, size=shape) ref_res = ref(x_data.shape) verify_func(func2, [x_data], ref_res)
def verify_full(fill_value, fill_shape, dtype): x = relay.var("x", relay.scalar_type(dtype)) y = relay.var("y", relay.TensorType(fill_shape, 'int64')) z = relay.full(x, relay.shape_of(y), dtype) func = run_infer_type(relay.Function([x, y], z)) func2 = run_opt_pass(run_opt_pass(func, transform.DynamicToStatic()), transform.InferType()) zz = func2.body assert isinstance(zz, relay.Call) assert zz.checked_type == relay.TensorType(fill_shape, dtype) ref_res = np.full(fill_shape, fill_value).astype(dtype) y_data = np.random.uniform(low=-1, high=1, size=fill_shape).astype('int64') verify_func(func2, [fill_value, y_data], ref_res)
def verify_broadcast_to(shape, broadcast_shape): x = relay.var("x", relay.TensorType(shape, "float32")) y = relay.var("y", relay.TensorType(broadcast_shape, "float32")) z = relay.broadcast_to(x, shape=relay.shape_of(y)) func = run_infer_type(relay.Function([x, y], z)) func2 = run_opt_pass(run_opt_pass(func, transform.DynamicToStatic()), transform.InferType()) zz = func2.body assert isinstance(zz, relay.Call) assert zz.op == relay.op.get("broadcast_to") assert zz.checked_type == relay.ty.TensorType(broadcast_shape, "float32") x_data = np.random.uniform(low=-1, high=1, size=shape).astype("float32") y_data = np.random.uniform(low=-1, high=1, size=broadcast_shape).astype("float32") ref_res = np.broadcast_to(x_data, y_data.shape) verify_func(func2, [x_data, y_data], ref_res)
def get_shape_of_func(): data = relay.var("data", shape=(relay.Any(), 28, 28), dtype="float32") out = relay.shape_of(data) return relay.Function([data], out)
def before(dtype): x = relay.var("x", shape=c_shape, dtype="float32") y = relay.var("y", shape=c_shape, dtype="float32") z = relay.shape_of(x + y, dtype) return relay.Function([x, y], z)
def before(dtype): x = relay.var("x", shape=c_shape, dtype="float32") y = relay.var("y", shape=c_shape, dtype="float32") z = relay.shape_of(x + y, dtype) return relay.Function([x, y], z)