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

Function test_tqt

imperative/python/test/unit/quantization/test_fake_quant.py:60–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

58
59
60def test_tqt():
61
62 g = []
63
64 def cb(grad):
65 g.append(grad)
66
67 x = np.random.randint(-128, 128, size=(1, 2, 3, 4)).astype("float32")
68 s = np.random.rand(1) - 1
69 g_y = np.ones(shape=(1, 2, 3, 4), dtype="float32")
70
71 n = TQT_numpy(-127, 127)
72 y_np = n.forward(x, s)
73 g_x_np, g_s_np = n.backward(g_y)
74
75 x = mge.tensor(x, dtype="float32")
76 s = mge.tensor(s, dtype="float32")
77 g_y = mge.tensor(g_y, dtype="float32")
78 with Grad() as grad:
79 grad.wrt(x, s, callback=cb)
80 y = tqt_forward(-127, 127, x, s)
81 grad(y, g_y)
82 g_x, g_s = g
83
84 np.testing.assert_allclose(y.numpy(), y_np, rtol=1e-5, atol=1e-5)
85 np.testing.assert_allclose(g_x.numpy(), g_x_np, rtol=1e-5, atol=1e-5)
86 np.testing.assert_allclose(g_s.numpy(), g_s_np, rtol=5e-5, atol=5e-5)
87
88
89def _save_to(self, name="grad"):

Callers

nothing calls this directly

Calls 11

forwardMethod · 0.95
backwardMethod · 0.95
GradClass · 0.90
tqt_forwardFunction · 0.90
TQT_numpyClass · 0.85
gradFunction · 0.85
onesMethod · 0.80
tensorMethod · 0.80
wrtMethod · 0.80
astypeMethod · 0.45
numpyMethod · 0.45

Tested by

no test coverage detected