MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / test_replace_oprs

Function test_replace_oprs

imperative/python/test/unit/utils/test_cgtools.py:44–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

42
43
44def test_replace_oprs():
45 g = mgb_graph.Graph()
46 g.options.async_exec_level = 0b100
47 device = "xpux"
48 dtype = np.float32
49 a = mgb_graph.InputNode(device=device, dtype=dtype, graph=g)
50 const = g.make_const(1.25, device=device)
51 add_op = Elemwise(Elemwise.Mode.ADD)
52 mul_op = Elemwise(Elemwise.Mode.MUL)
53 a_plus_a = apply_normal_varnode(add_op, a.outputs[0], a.outputs[0])[0]
54 old_opr = a_plus_a.op
55 a_plus_a_mul_const = apply_normal_varnode(mul_op, a_plus_a, const)[0]
56 a_mul_a = apply_normal_varnode(mul_op, a.outputs[0], a.outputs[0])[0]
57 new_opr = a_mul_a.op
58 (new,) = cgtools.replace_oprs(
59 [a_plus_a_mul_const._node], {old_opr._node: new_opr._node}
60 )
61 out = mgb_graph.OutputNode(mgb_graph.VarNode(new))
62 func = g.compile(out.outputs[0])
63 func.execute()
64 x = make_dev_tensor(5.0, device=device)
65 a.set_value(x)
66 res = out.get_value().numpy()
67 np.testing.assert_equal(res, np.array([5.0 * 5.0 * 1.25]))
68
69
70def test_graph_traversal():

Callers

nothing calls this directly

Calls 13

make_constMethod · 0.95
compileMethod · 0.95
set_valueMethod · 0.95
get_valueMethod · 0.95
apply_normal_varnodeFunction · 0.90
make_dev_tensorFunction · 0.85
VarNodeMethod · 0.80
assert_equalMethod · 0.80
arrayMethod · 0.80
ElemwiseClass · 0.50
replace_oprsMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected