def test_tuple(): ib = IRBuilder() dup = ib.global_var('dup') x = ib.param('x') with ib.decl(dup, x): ib.ret(relay.Tuple([x, x])) # todo: why is this not generalized? fn_ty = func_type([tensor_type()], relay.TupleType([tensor_type(), tensor_type()])) assert_decl_has_type(ib.env, dup, fn_ty)
def test_decl(): """Program: def f(x : Tensor[f32, (10, 10)]) { let lx = log(x); return lx; } """ b = IRBuilder() x = b.param('x') with b.decl('f', x): lx = b.let('lx', log(x)) b.ret(lx) _, env = b.get() assert_decl_has_type(env, 'f', func_type(['float32'], 'float32'))
def test_concat(): """ Program: def try_concat2(x: Float(3, 2), y: Float(2, 2)) -> Float(5, 2) { return concat(x, y); } """ ib = IRBuilder() try_concat2 = ib.global_var('try_concat2') x = ib.param('x', ty=tensor_type(3, 2)) y = ib.param('y', ty=tensor_type(2, 2)) with ib.decl(try_concat2, x, y): ib.ret(concat(x, y)) fn_ty = func_type([tensor_type(3, 2), tensor_type(2, 2)], tensor_type(5, 2)) assert_decl_has_type(ib.env, try_concat2, fn_ty)
def test_recursion(): """ Program: def f(n: i32, data: f32) -> f32 { if (n == 0) { return f(n - 1, log(data)); } else { return data; } } f(2, 10000); """ b = IRBuilder() f = b.global_var('f') n = b.param('n', ty='int32') data = b.param('data', ty='float32') with b.decl(f, n, data): with b.if_scope(equal(n, convert(0))): b.ret(f(subtract(n, convert(1)), log(data))) with b.else_scope(): b.ret(data) b.ret(f(convert(2.0), convert(10000.0))) assert_decl_has_type(b.env, 'f', func_type( ['int32', 'float32'], 'float32'))