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

Function tester

imperative/python/test/unit/xla/functional/test_xla_nn.py:22–77  ·  view source on GitHub ↗
(x_shape, w_shape, b_shape, stride, padding, groups, dtype=None)

Source from the content-addressed store, hash-verified

20 mge.random.seed(123)
21
22 def tester(x_shape, w_shape, b_shape, stride, padding, groups, dtype=None):
23 dtype = dtype or np.float32
24 x = tensor(0.1 * np.random.rand(*x_shape), dtype=dtype)
25 w = tensor(0.1 * np.random.rand(*w_shape), dtype=dtype)
26 b = tensor(0.1 * np.random.rand(*b_shape), dtype=dtype) if b_shape else None
27 y = F.conv2d(x, w, b, stride=stride, padding=padding, groups=groups)
28 dy = tensor(0.1 * np.random.rand(*y.shape), dtype=dtype)
29 cm = "float32" if dtype == np.float16 else "default"
30 rtol = 5e-3 if dtype == np.float16 else 1e-07
31
32 gm = GradManager()
33
34 if b is not None:
35
36 @jit.xla_trace(without_host=True)
37 def func(x, w, b, dy):
38 gm.attach([x, w, b])
39 with gm:
40 y = F.conv2d(
41 x,
42 w,
43 b,
44 stride=stride,
45 padding=padding,
46 groups=groups,
47 compute_mode=cm,
48 )
49 gm.backward(y, dy)
50 return [y, x.grad, w.grad, b.grad]
51
52 mge_rsts = func(x, w, b, dy)
53 xla_rsts = func(x, w, b, dy)
54 else:
55
56 @jit.xla_trace(without_host=True)
57 def func(x, w, dy):
58 gm.attach([x, w])
59 with gm:
60 y = F.conv2d(
61 x,
62 w,
63 stride=stride,
64 padding=padding,
65 groups=groups,
66 compute_mode=cm,
67 )
68 gm.backward(y, dy)
69 return [y, x.grad, w.grad]
70
71 mge_rsts = func(x, w, dy)
72 xla_rsts = func(x, w, dy)
73
74 for mge_rst, xla_rst in zip(mge_rsts, xla_rsts):
75 np.testing.assert_allclose(
76 mge_rst.numpy(), xla_rst.numpy(), atol=1e-5, rtol=rtol
77 )
78
79 for dtype in [np.float16, np.float32]:

Callers 6

test_conv2dFunction · 0.70
test_conv_transpose2dFunction · 0.70
test_adaptive_poolingFunction · 0.70
test_poolingFunction · 0.70
test_softmaxFunction · 0.70
test_lossFunction · 0.70

Calls 5

GradManagerClass · 0.90
funcFunction · 0.70
numpyMethod · 0.45
GradManagerMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected