def test_qadd_relu_different_qparams(self): add_relu = torch.ops.quantized.add_relu add = torch.ops.quantized.add A = torch.arange(-25, 25, dtype=torch.float) B = torch.arange(-25, 25, dtype=torch.float) scale_A = 3.0 zero_point_A = 7 scale_B = 5.0 zero_point_B = 127 scale_C = 0.5 zero_point_C = 5 qA = torch.quantize_linear(A, scale=scale_A, zero_point=zero_point_A, dtype=torch.quint8) qB = torch.quantize_linear(B, scale=scale_B, zero_point=zero_point_B, dtype=torch.quint8) # Add ground truth C = (qA.dequantize() + qB.dequantize()).numpy() qC = _quantize(C, scale_C, zero_point_C) qC_hat = add(qA, qB, scale=scale_C, zero_point=zero_point_C) np.testing.assert_equal(qC, qC_hat.int_repr(), "Quantized addition failed.") # Add + ReLU ground truth Crelu = C.copy() Crelu[C < 0] = 0 qCrelu = _quantize(Crelu, scale_C, zero_point_C) qCrelu_hat = add_relu(qA, qB, scale=scale_C, zero_point=zero_point_C) np.testing.assert_equal(qCrelu, qCrelu_hat.int_repr(), "Quantized addition with ReLU failed.")
def test_qmul_relu_different_qparams(self): mul_relu = torch.ops.quantized.mul_relu mul = torch.ops.quantized.mul mul_out = torch.ops.quantized.mul_out mul_relu_out = torch.ops.quantized.mul_relu_out A = torch.arange(-25, 25, dtype=torch.float) B = torch.arange(-25, 25, dtype=torch.float) scale_A = 3.0 zero_point_A = 7 scale_B = 5.0 zero_point_B = 127 scale_C = 0.5 zero_point_C = 5 qA = torch.quantize_linear(A, scale=scale_A, zero_point=zero_point_A, dtype=torch.quint8) qB = torch.quantize_linear(B, scale=scale_B, zero_point=zero_point_B, dtype=torch.quint8) # mul ground truth C = (qA.dequantize() * qB.dequantize()).numpy() qC = _quantize(C, scale_C, zero_point_C) qC_hat = mul(qA, qB, scale=scale_C, zero_point=zero_point_C) np.testing.assert_equal(qC, qC_hat.int_repr(), "Quantized multiplication failed.") qC_out_hat = torch._empty_affine_quantized(qC.shape, scale=scale_C, zero_point=zero_point_C, dtype=torch.quint8) mul_out(qA, qB, out=qC_out_hat) self.assertEqual(qC_hat, qC_out_hat, message="mul.out failed") # mul + ReLU ground truth Crelu = C.copy() Crelu[C < 0] = 0 qCrelu = _quantize(Crelu, scale_C, zero_point_C) qCrelu_hat = mul_relu(qA, qB, scale=scale_C, zero_point=zero_point_C) np.testing.assert_equal(qCrelu, qCrelu_hat.int_repr(), "Quantized multiplication with ReLU failed.") qCrelu_out_hat = torch._empty_affine_quantized(qCrelu.shape, scale=scale_C, zero_point=zero_point_C, dtype=torch.quint8) mul_relu_out(qA, qB, out=qCrelu_out_hat) self.assertEqual(qCrelu_hat, qCrelu_out_hat, message="mulReLU.out failed")
def test_qmul_relu_same_qparams(self): mul_relu = torch.ops.quantized.mul_relu mul = torch.ops.quantized.mul mul_out = torch.ops.quantized.mul_out mul_relu_out = torch.ops.quantized.mul_relu_out A = torch.arange(-25, 25, dtype=torch.float) B = torch.arange(-25, 25, dtype=torch.float) scale = 2.0 zero_point = 127 qA = torch.quantize_linear(A, scale=scale, zero_point=zero_point, dtype=torch.quint8) qB = torch.quantize_linear(B, scale=scale, zero_point=zero_point, dtype=torch.quint8) # mul ReLU ground truth C = (qA.dequantize() * qB.dequantize()).numpy() qC = _quantize(C, scale, zero_point) qC_hat = mul(qA, qB, scale=scale, zero_point=zero_point) np.testing.assert_equal(qC, qC_hat.int_repr(), "Quantized mulition failed.") qC_out_hat = torch._empty_affine_quantized(qC.shape, scale=scale, zero_point=zero_point, dtype=torch.quint8) mul_out(qA, qB, out=qC_out_hat) self.assertEqual(qC_hat, qC_out_hat, message="mul.out failed") # mul + ReLU ground truth Crelu = C.copy() Crelu[C < 0] = 0 qCrelu = _quantize(Crelu, scale, zero_point) qCrelu_hat = mul_relu(qA, qB, scale=scale, zero_point=zero_point) np.testing.assert_equal(qCrelu, qCrelu_hat.int_repr(), "Quantized mulition with ReLU failed.") qCrelu_out_hat = torch._empty_affine_quantized(qCrelu.shape, scale=scale, zero_point=zero_point, dtype=torch.quint8) mul_relu_out(qA, qB, out=qCrelu_out_hat) self.assertEqual(qCrelu_hat, qCrelu_out_hat, message="mulReLU.out failed") # Scalar addition mul = torch.ops.quantized.mul_scalar for b in B: C_ref = qA.dequantize().numpy() * b.item() qC = _quantize(C_ref, scale, zero_point) dqC = _dequantize(qC, scale, zero_point) qC_hat = mul(qA, b.item(), scale, zero_point) dqC_hat = qC_hat.dequantize() self.assertEqual(dqC, dqC_hat)
def test_qadd_relu_same_qparams(self): add_relu = torch.ops.quantized.add_relu add = torch.ops.quantized.add add_out = torch.ops.quantized.add_out add_relu_out = torch.ops.quantized.add_relu_out A = torch.arange(-25, 25, dtype=torch.float) B = torch.arange(-25, 25, dtype=torch.float) scale = 2.0 zero_point = 127 qA = torch.quantize_linear(A, scale=scale, zero_point=zero_point, dtype=torch.quint8) qB = torch.quantize_linear(B, scale=scale, zero_point=zero_point, dtype=torch.quint8) # Add ReLU ground truth C = (qA.dequantize() + qB.dequantize()).numpy() qC = _quantize(C, scale, zero_point) qC_hat = add(qA, qB, scale=scale, zero_point=zero_point) np.testing.assert_equal(qC, qC_hat.int_repr(), "Quantized addition failed.") qC_out_hat = torch._empty_affine_quantized(qC.shape, scale=scale, zero_point=zero_point, dtype=torch.quint8) add_out(qA, qB, out=qC_out_hat) self.assertEqual(qC_hat, qC_out_hat, message="Add.out failed") # Add + ReLU ground truth Crelu = C.copy() Crelu[C < 0] = 0 qCrelu = _quantize(Crelu, scale, zero_point) qCrelu_hat = add_relu(qA, qB, scale=scale, zero_point=zero_point) np.testing.assert_equal(qCrelu, qCrelu_hat.int_repr(), "Quantized addition with ReLU failed.") qCrelu_out_hat = torch._empty_affine_quantized(qCrelu.shape, scale=scale, zero_point=zero_point, dtype=torch.quint8) add_relu_out(qA, qB, out=qCrelu_out_hat) self.assertEqual(qCrelu_hat, qCrelu_out_hat, message="AddReLU.out failed")
def qlinear_ref(X_q, X_scale, X_zp, W_q, W_scale, W_zp, b_q, Y_scale, Y_zp): X_q = np.reshape(X_q, (-1, X_q.shape[X_q.ndim - 1])) row_offsets_ref = X_q.sum(axis=1).astype(np.int32).reshape((-1, 1)) col_offsets_ref = W_q.sum(axis=1).astype(np.int32).reshape((1, -1)) assert X_q.ndim == 2 batch_size, input_channels = X_q.shape Prod_XqWq_ref = (np.matmul(X_q.astype(np.int32), W_q.astype(np.int32).T) - W_zp * row_offsets_ref - X_zp * col_offsets_ref + input_channels * X_zp * W_zp) Y_q_ref = _quantize(Prod_XqWq_ref + b_q, Y_scale / (X_scale * W_scale), Y_zp) return Y_q_ref
def test_qnnpack_relu(self, Q): X, (scale, zero_point), (qmin, qmax), (torch_type, np_type) = Q relu = torch.ops.quantized.qnnpack_relu Y = X.copy() X = torch.from_numpy(X) qX = torch.quantize_linear(X, scale=scale, zero_point=zero_point, dtype=torch_type) qY_hat = relu(qX) Y[Y < 0] = 0 qY = _quantize(Y, scale, zero_point, dtype=np_type) np.testing.assert_equal(qY, qY_hat.int_repr())