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

Function test_single_input

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

Source from the content-addressed store, hash-verified

12
13
14def test_single_input():
15 data_shape = (9, 2, 6)
16 av = np.random.random(data_shape).astype(np.float32)
17
18 class MulFunc(Function):
19 def forward(self, a):
20 self.a = a
21 return a * 10
22
23 def backward(self, grad_o):
24 return grad_o * 10
25
26 class Simple(Module):
27 def __init__(self, a):
28 super().__init__()
29 self.a = Parameter(a, dtype=np.float32)
30 self.layer1 = MulFunc()
31
32 def forward(self):
33 x = self.layer1(self.a)
34 return x
35
36 net = Simple(av)
37 gm = ad.GradManager().attach(net.parameters())
38 opt = optimizer.SGD(net.parameters(), lr=1.0)
39
40 opt.clear_grad()
41 with gm:
42 loss = net()
43 gm.backward(loss.sum())
44 opt.step()
45
46 np.testing.assert_almost_equal(loss.numpy(), (av * 10))
47 np.testing.assert_almost_equal(net.a.numpy(), (av - 10))
48
49
50def test_multi_input():

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