(zero_point, scale)
| 114 | test_dtype = QuantDtypeMeta("test_qint8", None, "int8", qmin, qmax) |
| 115 | |
| 116 | def run(zero_point, scale): |
| 117 | qparams = create_qparams(QuantMode.ASYMMERTIC, test_dtype, scale, zero_point) |
| 118 | inp_data = np.random.uniform(low=-512.0, high=512.0, size=(1, 32, 32, 32)) |
| 119 | inp = tensor(inp_data, dtype=np.float32) |
| 120 | # test forward |
| 121 | oup = fake_quant_tensor(inp, qparams).numpy() |
| 122 | oup_gt = fake_quant_tensor_gt(inp, scale, zero_point, qmin, qmax).numpy() |
| 123 | assert np.allclose(oup, oup_gt) |
| 124 | assert oup.shape == oup_gt.shape |
| 125 | |
| 126 | # test backward |
| 127 | x = tensor(inp_data, dtype=np.float32) |
| 128 | with Grad() as grad: |
| 129 | grad.wrt(x, callback=_save_to(x)) |
| 130 | y = fake_quant_tensor(x, qparams) |
| 131 | grad(y, tensor(F.ones_like(x))) |
| 132 | |
| 133 | x1 = tensor(inp_data, dtype=np.float32) |
| 134 | with Grad() as grad: |
| 135 | grad.wrt(x1, callback=_save_to(x1)) |
| 136 | y1 = fake_quant_tensor_gt(x1, scale, zero_point, qmin, qmax) |
| 137 | grad(y1, tensor(F.ones_like(x1))) |
| 138 | |
| 139 | assert np.allclose(x.grad.numpy(), x1.grad.numpy()) |
| 140 | assert make_shape_tuple(x.grad.shape) == make_shape_tuple(x1.grad.shape) |
| 141 | |
| 142 | # test nan |
| 143 | x = F.full((1, 32, 3, 3), np.nan) |
| 144 | y = fake_quant_tensor(x, qparams).numpy() |
| 145 | assert np.isnan(y).all() |
| 146 | |
| 147 | zero_point = tensor([1.0], dtype=np.float32) |
| 148 | scale = tensor([4.0], dtype=np.float32) |
no test coverage detected