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

Function test_linear

imperative/python/test/unit/quantization/test_module.py:187–227  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

185
186
187def test_linear():
188 normal_net = Float.Linear(3, 3, bias=True)
189 normal_net.eval()
190
191 qat_net = QAT.Linear(3, 3, bias=True)
192 qat_net.eval()
193 disable_observer(qat_net)
194
195 propagate_qconfig(qat_net, min_max_fakequant_qconfig)
196 init_qat_net(qat_net)
197
198 x = mge.tensor(np.random.normal(size=(3, 3)).astype("float32"))
199 inp_scale = gen_inp_scale()
200 x = fake_quant_act(x, inp_scale)
201 x.qparams.update(create_qparams(QuantMode.SYMMERTIC, "qint8", inp_scale))
202
203 x_int8 = quant(x, inp_scale)
204
205 weight = np.random.normal(size=(3, 3)).astype("float32")
206 bias = np.random.normal(size=(3,)).astype("float32")
207 normal_net.weight[...] = fake_quant_weight(weight, weight_scale)
208 normal_net.bias[...] = fake_quant_bias(bias, inp_scale * weight_scale)
209 qat_net.weight[...] = Parameter(weight)
210 qat_net.bias[...] = Parameter(bias)
211
212 qat_from_float = QAT.Linear.from_float_module(normal_net)
213 qat_from_float.eval()
214 disable_fake_quant(qat_from_float)
215 disable_observer(qat_from_float)
216
217 q_net = Q.Linear.from_qat_module(qat_net)
218 q_net.eval()
219
220 normal = normal_net(x)
221 qat_without_fakequant = qat_from_float(x)
222 fake_quant_normal = fake_quant_act(normal_net(x), act_scale)
223 qat = qat_net(x)
224 q = q_net(x_int8).numpy() * act_scale
225 np.testing.assert_allclose(qat_without_fakequant, normal)
226 np.testing.assert_allclose(qat, fake_quant_normal.numpy())
227 np.testing.assert_allclose(q, fake_quant_normal.numpy())
228
229
230@pytest.mark.parametrize("module", ["Conv2d", "ConvBn2d", "ConvBnRelu2d"])

Callers

nothing calls this directly

Calls 15

disable_observerFunction · 0.90
propagate_qconfigFunction · 0.90
create_qparamsFunction · 0.90
ParameterClass · 0.90
disable_fake_quantFunction · 0.90
gen_inp_scaleFunction · 0.85
fake_quant_biasFunction · 0.85
tensorMethod · 0.80
normalMethod · 0.80
init_qat_netFunction · 0.70
quantFunction · 0.70
evalMethod · 0.45

Tested by

no test coverage detected