()
| 185 | |
| 186 | |
| 187 | def 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"]) |
nothing calls this directly
no test coverage detected