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

Function test_lsq

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

Source from the content-addressed store, hash-verified

190
191
192def test_lsq():
193 g = []
194
195 def cb(grad):
196 g.append(grad)
197
198 # FIXME: use random number when LSQ is fixed
199 # x = np.random.randint(-128, 128, size=(1, 2, 3, 4)).astype("float32")
200 # s = np.random.rand(1)
201 x = np.array(
202 [
203 [
204 [
205 [4.0, 38.0, -121.0, 38.0],
206 [15.0, -115.0, -112.0, 24.0],
207 [23.0, -65.0, 109.0, -115.0],
208 ],
209 [
210 [-66.0, -90.0, -45.0, -101.0],
211 [68.0, -98.0, 108.0, -79.0],
212 [54.0, 63.0, -10.0, -50.0],
213 ],
214 ]
215 ],
216 dtype="float32",
217 )
218 s = np.array([0.02918224], dtype="float32")
219 eps = np.array([1e-5], dtype="float32")
220 s = np.abs(s) if np.abs(s) > eps else eps
221 zero_point = np.array([1.0], dtype="float32")
222 grad_s = np.array([2.0], dtype="float32")
223
224 g_y = np.ones(shape=(1, 2, 3, 4), dtype="float32")
225
226 n = LSQ_numpy(-127, 127)
227 y_np = n.forward(x, s, zero_point, grad_s)
228 g_x_np, g_s_np = n.backward(g_y)
229
230 x = mge.tensor(x, dtype="float32")
231 s = mge.tensor(s, dtype="float32")
232 zero_point = mge.tensor(zero_point, dtype="float32")
233 grad_s = mge.tensor(grad_s, dtype="float32")
234
235 g_y = mge.tensor(g_y, dtype="float32")
236 with Grad() as grad:
237 grad.wrt(x, s, callback=cb)
238 y = lsq_forward(-127, 127, x, s, zero_point, grad_s)
239 grad(y, g_y)
240 g_x, g_s = g
241
242 np.testing.assert_allclose(y.numpy(), y_np, rtol=1e-7, atol=1e-7)
243 np.testing.assert_allclose(g_x.numpy(), g_x_np, rtol=1e-7, atol=1e-7)
244 np.testing.assert_allclose(g_s.numpy(), g_s_np, rtol=5e-7, atol=5e-7)

Callers

nothing calls this directly

Calls 11

forwardMethod · 0.95
backwardMethod · 0.95
GradClass · 0.90
lsq_forwardFunction · 0.90
LSQ_numpyClass · 0.85
gradFunction · 0.85
arrayMethod · 0.80
onesMethod · 0.80
tensorMethod · 0.80
wrtMethod · 0.80
numpyMethod · 0.45

Tested by

no test coverage detected