def test_tuple_passing(): x = relay.var('x', type_annotation=relay.ty.TupleType([ relay.ty.TensorType((), 'int64'), relay.ty.TensorType((), 'int64') ])) fn = relay.Function([x], relay.expr.TupleGetItem(x, 0)) mod = relay.Module({}) gv = relay.GlobalVar('main') mod[gv] = fn mod = relay.transform.InferType()(mod) ctx = tvm.cpu() target = tvm.target.create('llvm') exec = relay.create_executor(mod=mod, ctx=ctx, target=target) f = exec.evaluate(gv) # First use a Python tuple. out = f((10, 8)) tvm.testing.assert_allclose(out.asnumpy(), np.array(10)) # Second use a tuple value. value_tuple = container.tuple_object( [nd.array(np.array(11)), nd.array(np.array(12))]) out = f(value_tuple) tvm.testing.assert_allclose(out.asnumpy(), np.array(11))
def test_tuple_value(): tv = container.tuple_object( [relay.const(1), relay.const(2), relay.const(3)]) np.testing.assert_allclose(tv[0].data.asnumpy(), 1) np.testing.assert_allclose(tv[1].data.asnumpy(), 2) np.testing.assert_allclose(tv[2].data.asnumpy(), 3)
def _convert(arg, cargs): if isinstance(arg, _expr.Constant): cargs.append(arg.data) elif isinstance(arg, Object): cargs.append(arg) elif isinstance(arg, np.ndarray): nd_arr = tvm.nd.array(arg, ctx=tvm.cpu(0)) cargs.append(nd_arr) elif isinstance(arg, tvm.nd.NDArray): cargs.append(arg) elif isinstance(arg, (tuple, list)): field_args = [] for field in arg: _convert(field, field_args) cargs.append(container.tuple_object(field_args)) elif isinstance(arg, (_base.numeric_types, bool)): dtype = "int32" if isinstance(arg, (int, bool)) else "float32" value = tvm.nd.array(np.array(arg, dtype=dtype), ctx=tvm.cpu(0)) cargs.append(value) else: raise TypeError("Unsupported type: %s" % (type(arg)))
def test_tuple_object(): x = relay.var('x', type_annotation=relay.ty.TupleType([ relay.ty.TensorType((), 'int32'), relay.ty.TensorType((), 'int32') ])) fn = relay.Function([x], relay.expr.TupleGetItem(x, 0)) mod = relay.Module.from_expr(fn) exe = relay.create_executor(kind="vm", mod=mod, ctx=nd.cpu(), target="llvm") f = exe.evaluate() value_tuple = _container.tuple_object( [nd.array(np.array(11)), nd.array(np.array(12))]) # pass an ADT object to evaluate out = f(value_tuple) tvm.testing.assert_allclose(out.asnumpy(), np.array(11))
def test_function_taking_adt_ref_tuple(): mod = relay.Module() prelude = relay.prelude.Prelude(mod) intrp = create_executor("debug", mod) nil_value = ConstructorValue(prelude.nil.tag, [], prelude.nil) cons_value = ConstructorValue( prelude.cons.tag, [nd.array(np.random.rand(1, 10).astype('float32')), nil_value], prelude.cons) ref_value = RefValue(nd.array(np.random.rand(1, 10).astype('float32'))) tuple_value = container.tuple_object( [nd.array(np.random.rand(1, 10).astype('float32')) for _ in range(10)]) id_func = intrp.evaluate(prelude.id) res_nil = id_func(nil_value) assert res_nil.tag == nil_value.tag assert len(res_nil.fields) == 0 res_cons = id_func(cons_value) assert res_cons.tag == cons_value.tag assert len(res_cons.fields) == len(cons_value.fields) tvm.testing.assert_allclose(res_cons.fields[0].asnumpy(), cons_value.fields[0].asnumpy()) assert isinstance(res_cons.fields[1], ConstructorValue) assert res_cons.fields[1].tag == prelude.nil.tag assert len(res_cons.fields[1].fields) == 0 res_ref = id_func(ref_value) tvm.testing.assert_allclose(res_ref.value.asnumpy(), ref_value.value.asnumpy()) res_tuple = id_func(tuple_value) for i in range(10): tvm.testing.assert_allclose(res_tuple[i].asnumpy(), tuple_value[i].asnumpy())