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

Function test_multi_input

imperative/python/test/unit/core/test_function.py:50–87  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

48
49
50def test_multi_input():
51 data_shape = (9, 2, 6)
52 av = np.random.random(data_shape).astype(np.float32)
53 bv = np.random.random(data_shape).astype(np.float32)
54
55 class MulFunc(Function):
56 def forward(self, a, b):
57 self.a = a
58 self.b = b
59 return a * b
60
61 def backward(self, grad_o):
62 return grad_o * self.b * 2, grad_o * self.a * 3
63
64 class Simple(Module):
65 def __init__(self, a, b):
66 super().__init__()
67 self.a = Parameter(a, dtype=np.float32)
68 self.b = Parameter(b, dtype=np.float32)
69 self.layer1 = MulFunc()
70
71 def forward(self):
72 x = self.layer1(self.a, self.b)
73 return x
74
75 net = Simple(av, bv)
76 gm = ad.GradManager().attach(net.parameters())
77 opt = optimizer.SGD(net.parameters(), lr=1.0)
78
79 opt.clear_grad()
80 with gm:
81 loss = net()
82 gm.backward(loss.sum())
83 opt.step()
84
85 np.testing.assert_almost_equal(loss.numpy(), (av * bv))
86 np.testing.assert_almost_equal(net.a.numpy(), (av - 2 * bv))
87 np.testing.assert_almost_equal(net.b.numpy(), (bv - 3 * av))
88
89
90def test_multi_output():

Callers

nothing calls this directly

Calls 11

parametersMethod · 0.80
SimpleClass · 0.70
astypeMethod · 0.45
attachMethod · 0.45
GradManagerMethod · 0.45
SGDMethod · 0.45
clear_gradMethod · 0.45
backwardMethod · 0.45
sumMethod · 0.45
stepMethod · 0.45
numpyMethod · 0.45

Tested by

no test coverage detected