本文整理汇总了Python中tvm.relay.const函数的典型用法代码示例。如果您正苦于以下问题:Python const函数的具体用法?Python const怎么用?Python const使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了const函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_recursion
def test_recursion():
"""
Program:
def @f(%n: int32, %data: float32) -> float32 {
if (%n == 0) {
%data
} else {
@f(%n - 1, log(%data))
}
}
"""
sb = relay.ScopeBuilder()
f = relay.GlobalVar("f")
ti32 = relay.scalar_type("int32")
tf32 = relay.scalar_type("float32")
n = relay.var("n", ti32)
data = relay.var("data", tf32)
with sb.if_scope(relay.equal(n, relay.const(0, ti32))):
sb.ret(data)
with sb.else_scope():
sb.ret(f(relay.subtract(n, relay.const(1, ti32)), relay.log(data)))
mod = relay.Module()
mod[f] = relay.Function([n, data], sb.get())
assert "@f(%1, %2) /* ty=float32 */" in mod.astext()
assert mod[f].checked_type == relay.FuncType([ti32, tf32], tf32)
开发者ID:bddppq,项目名称:tvm,代码行数:26,代码来源:test_type_infer.py
示例2: gen_intermediate_tuple
def gen_intermediate_tuple(x):
y1 = relay.add(x, relay.const(1, "float32"))
y2 = relay.add(x, relay.const(1, "float32"))
y3 = relay.add(x, relay.const(1, "float32"))
concat = relay.concatenate((y1, y2, y3), axis=1)
out = relay.add(concat, relay.const(1, "float32"))
return out
开发者ID:bddppq,项目名称:tvm,代码行数:7,代码来源:test_pass_fuse_ops.py
示例3: test_let
def test_let():
assert parses_as(
"let %x = 1; ()",
relay.Let(
X,
relay.const(1),
UNIT
)
)
assert parses_as(
"""
let %x = 1;
let %y = 2;
()
""",
relay.Let(
X,
relay.const(1),
relay.Let(
Y,
relay.const(2),
UNIT
)
)
)
开发者ID:bddppq,项目名称:tvm,代码行数:26,代码来源:test_ir_parser.py
示例4: test_filter
def test_filter():
a = relay.TypeVar("a")
expected_type = relay.FuncType([
relay.FuncType([a], relay.scalar_type("bool")), l(a)
], l(a), [a])
assert mod[filter].checked_type == expected_type
x = relay.Var("x", nat())
greater_than_one = relay.Function(
[x],
relay.Match(x, [
relay.Clause(
relay.PatternConstructor(s, [
relay.PatternConstructor(
s, [relay.PatternWildcard()])
]),
relay.const(True)),
relay.Clause(relay.PatternWildcard(), relay.const(False))
]))
res = intrp.evaluate(
filter(greater_than_one,
cons(build_nat(1),
cons(build_nat(1),
cons(build_nat(3),
cons(build_nat(1),
cons(build_nat(5),
cons(build_nat(1),
nil()))))))))
filtered = to_list(res)
assert len(filtered) == 2
assert count(filtered[0]) == 3
assert count(filtered[1]) == 5
开发者ID:bddppq,项目名称:tvm,代码行数:32,代码来源:test_adt.py
示例5: test_list_constructor
def test_list_constructor():
# TODO(wweic): implement pattern match to support this test
def to_list(o):
if isinstance(o, tvm.relay.backend.interpreter.TensorValue):
return [o.data.asnumpy().tolist()]
if isinstance(o, tvm.relay.backend.interpreter.ConstructorValue):
result = []
for f in o.fields:
result.extend(to_list(f))
return result
mod = relay.Module()
p = Prelude(mod)
nil = p.nil
cons = p.cons
l = p.l
one2 = cons(relay.const(1), nil())
one3 = cons(relay.const(2), one2)
one4 = cons(relay.const(3), one3)
f = relay.Function([], one4)
mod[mod.entry_func] = f
result = veval(mod)()
obj = to_list(result)
import pdb; pdb.set_trace()
tvm.testing.assert_allclose(obj, np.array([3,2,1]))
开发者ID:bddppq,项目名称:tvm,代码行数:29,代码来源:test_vm.py
示例6: before
def before(x):
concat = gen_consecutive_tuple(x)
pooled = relay.nn.max_pool2d(concat, pool_size=(2, 2), strides=(2, 2), padding=(0, 0))
out = relay.add(pooled, relay.const(1, "float32"))
out2 = relay.add(out, relay.const(1, "float32"))
out_tup = relay.Tuple((out, out2))
return relay.Function(relay.ir_pass.free_vars(out_tup), out_tup)
开发者ID:bddppq,项目名称:tvm,代码行数:7,代码来源:test_pass_fuse_ops.py
示例7: test_function_type
def test_function_type():
assert parses_as(
"""
let %_: fn () -> int32 = fn () -> int32 { 0 }; ()
""",
relay.Let(
relay.Var("_", relay.FuncType([], int32, [], [])),
relay.Function([], relay.const(0), int32, []),
UNIT
)
)
assert parses_as(
"""
let %_: fn (int32) -> int32 = fn (%x: int32) -> int32 { 0 }; ()
""",
relay.Let(
relay.Var("_", relay.FuncType([int32], int32, [], [])),
relay.Function([relay.Var("x", int32)], relay.const(0), int32, []),
UNIT
)
)
assert parses_as(
"""
let %_: fn (int32, int32) -> int32 = fn (%x: int32, %y: int32) -> int32 { 0 }; ()
""",
relay.Let(
relay.Var("_", relay.FuncType([int32, int32], int32, [], [])),
relay.Function([relay.Var("x", int32), relay.Var("y", int32)], relay.const(0), int32, []),
UNIT
)
)
开发者ID:bddppq,项目名称:tvm,代码行数:33,代码来源:test_ir_parser.py
示例8: test_tuple_type
def test_tuple_type():
assert parses_as(
"""
let %_: () = (); ()
""",
relay.Let(
relay.Var("_", relay.TupleType([])),
UNIT,
UNIT
)
)
assert parses_as(
"""
let %_: (int32,) = (0,); ()
""",
relay.Let(
relay.Var("_", relay.TupleType([int32])),
relay.Tuple([relay.const(0)]),
UNIT
)
)
assert parses_as(
"""
let %_: (int32, int32) = (0, 1); ()
""",
relay.Let(
relay.Var("_", relay.TupleType([int32, int32])),
relay.Tuple([relay.const(0), relay.const(1)]),
UNIT
)
)
开发者ID:bddppq,项目名称:tvm,代码行数:33,代码来源:test_ir_parser.py
示例9: before
def before():
c = relay.const(c_data)
x = relay.var("x")
y = relay.add(c, c)
y = relay.multiply(y, relay.const(2, "float32"))
y = relay.add(x, y)
z = relay.add(y, c)
return relay.Function([x], z)
开发者ID:bddppq,项目名称:tvm,代码行数:8,代码来源:test_pass_fold_constant.py
示例10: expected
def expected():
x = relay.var("x", shape=(1, 16))
y = relay.nn.relu(x)
y1 = relay.add(y, relay.const(1.0, "float32"))
y2 = relay.add(y, relay.const(1.0, "float32"))
y = relay.add(y1, y2)
f = relay.Function([x], y)
return f
开发者ID:bddppq,项目名称:tvm,代码行数:8,代码来源:test_pass_eliminate_common_subexpr.py
示例11: test_equal
def test_equal():
i = relay.var('i', shape=[], dtype='int32')
j = relay.var('i', shape=[], dtype='int32')
z = relay.equal(i, j)
func = relay.Function([i, j], z, ret_type=relay.TensorType([], 'bool'))
i_data = relay.const(0)
j_data = relay.const(0)
check_eval(func, [i_data, j_data], True)
开发者ID:LANHUIYING,项目名称:tvm,代码行数:8,代码来源:test_backend_interpreter.py
示例12: test_closure
def test_closure():
x = relay.var('x', shape=())
y = relay.var('y', shape=())
f = relay.Function([x], x + y)
ff = relay.Function([y], f)
clo = ff(relay.const(1.0))
main = clo(relay.const(2.0))
res = veval(main)
tvm.testing.assert_allclose(res.asnumpy(), 3.0)
开发者ID:bddppq,项目名称:tvm,代码行数:9,代码来源:test_vm.py
示例13: test_graph
def test_graph():
assert parses_as(
"%0 = (); %1 = 1; (%0, %0, %1)",
relay.Tuple([UNIT, UNIT, relay.const(1)])
)
assert not parses_as(
"%0 = (); %1 = 1; (%0, %0, %1)",
relay.Tuple([relay.Tuple([]), relay.Tuple([]), relay.const(1)])
)
开发者ID:bddppq,项目名称:tvm,代码行数:10,代码来源:test_ir_parser.py
示例14: test_let_alpha_equal
def test_let_alpha_equal():
tt1 = relay.TensorType((), "float32")
tt2 = relay.TensorType((), "int8")
v1 = relay.Var("v1")
v1_wtype = relay.Var("v1", tt1)
v2 = relay.Var("v2")
v3 = relay.Var("v3")
let = relay.Let(v1, relay.const(2), v1)
mapped = relay.Let(v2, relay.const(2), v2)
assert alpha_equal(let, mapped)
mismatched_var = relay.Let(v2, relay.const(2), v3)
assert not alpha_equal(let, mismatched_var)
different_value = relay.Let(v2, relay.const(3), v2)
assert not alpha_equal(let, different_value)
different_body = relay.Let(v2, relay.const(3), relay.const(12))
assert not alpha_equal(let, different_body)
# specified types must match
let_with_type = relay.Let(v1_wtype, relay.const(2), v1_wtype)
same_type = relay.Let(v1_wtype, relay.const(2), v1_wtype)
assert alpha_equal(let_with_type, same_type)
assert not alpha_equal(let, let_with_type)
v2 = relay.Var("v1", tt2)
different_type = relay.Let(v2, relay.const(2), v2)
assert not alpha_equal(let_with_type, different_type)
开发者ID:LANHUIYING,项目名称:tvm,代码行数:30,代码来源:test_pass_alpha_equal.py
示例15: test_let_inlining
def test_let_inlining():
tup = relay.Tuple([relay.const(0), relay.const(0)])
x = relay.var("x")
assert relay.Let(x, tup, tup).astext() == SEMVER + \
("%0 = (0, 0)\n"
"let %x = %0\n"
"%0")
assert relay.Let(x, tup, x).astext() == SEMVER + \
("let %x = (0, 0)\n"
"%x")
开发者ID:bddppq,项目名称:tvm,代码行数:11,代码来源:test_ir_text_printer.py
示例16: simple_bn
def simple_bn(x, gamma, beta, moving_mean, moving_var,
axis=1, epsilon=1e-5, shape=None):
# expect = (x - moving_mean) / sqrt(moving_var + eps) * gamma + beta
scale = rly.multiply(rly.const(1, 'float32') /
rly.sqrt(moving_var + rly.const(epsilon, 'float32')), gamma)
shift = rly.add(
rly.multiply(rly.negative(moving_mean), scale), beta)
num_newaxis = len(shape) - (axis + 1)
if num_newaxis:
scale = rly.expand_dims(scale, axis=1, num_newaxis=num_newaxis)
shift = rly.expand_dims(shift, axis=1, num_newaxis=num_newaxis)
return x * scale + shift
开发者ID:LANHUIYING,项目名称:tvm,代码行数:12,代码来源:test_pass_simplify_inference.py
示例17: test_bind_params
def test_bind_params():
x = relay.var("x")
y = relay.var("y")
z = relay.add(x, y)
f = relay.Function([x, y], z)
fbinded = relay.bind(f, {x : relay.const(1, "float32")})
fexpected =relay.Function(
[y],
relay.add(relay.const(1, "float32"), y))
assert relay.ir_pass.alpha_equal(fbinded, fexpected)
zbinded = relay.bind(z, {y: x})
zexpected = relay.add(x, x)
assert relay.ir_pass.alpha_equal(zbinded, zexpected)
开发者ID:bddppq,项目名称:tvm,代码行数:14,代码来源:test_ir_bind.py
示例18: test_func
def test_func():
# 0 args
assert parses_as(
"fn () { 0 }",
relay.Function(
[],
relay.const(0),
None,
[]
)
)
# 1 arg
assert parses_as(
"fn (%x) { %x }",
relay.Function(
[X],
X,
None,
[]
)
)
# 2 args
assert parses_as(
"fn (%x, %y) { %x + %y }",
relay.Function(
[X, Y],
relay.add(X, Y),
None,
[]
)
)
# annotations
assert parses_as(
"fn (%x: int32) -> int32 { %x }",
relay.Function(
[X_ANNO],
X_ANNO,
int32,
[]
)
)
# attributes
assert parses_as(
"fn (n=5) { () }",
relay.Function([], UNIT, None, None, tvm.make.node("DictAttrs", n=relay.const(5)))
)
开发者ID:bddppq,项目名称:tvm,代码行数:50,代码来源:test_ir_parser.py
示例19: test_ref
def test_ref():
mod = relay.Module()
three_with_ref = relay.GlobalVar('three_with_ref')
i = relay.Var('i')
iv = relay.Var('iv')
u = relay.Var('u')
uv = relay.Var('uv')
body = relay.add(iv, uv)
body = relay.Let(uv, relay.RefRead(i), body)
body = relay.Let(u, relay.RefWrite(i, relay.const(2)), body)
body = relay.Let(iv, relay.RefRead(i), body)
body = relay.Let(i, relay.RefCreate(relay.const(1)), body)
mod[three_with_ref] = relay.Function([], body)
check_eval(three_with_ref, [], 3, mod=mod)
开发者ID:bddppq,项目名称:tvm,代码行数:14,代码来源:test_backend_interpreter.py
示例20: test_simple_loop
def test_simple_loop():
mod = relay.module.Module({})
sum_up = relay.GlobalVar('sum_up')
i = relay.var('i', shape=[], dtype='int32')
sb = ScopeBuilder()
with sb.if_scope(relay.equal(i, relay.const(0, dtype='int32'))):
sb.ret(i)
with sb.else_scope():
one_less = relay.subtract(i, relay.const(1, dtype='int32'))
rec_call = relay.Call(sum_up, [one_less])
sb.ret(relay.add(rec_call, i))
func = relay.Function([i], sb.get(), ret_type=relay.TensorType([], 'int32'))
mod[sum_up] = func
i_data = np.array(10, dtype='int32')
check_eval(sum_up, [i_data], sum(range(1, 11)), mod=mod)
开发者ID:LANHUIYING,项目名称:tvm,代码行数:15,代码来源:test_backend_interpreter.py
注:本文中的tvm.relay.const函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论