def test_qadd_relu_same_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 = 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.") # 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.")
def qlinear_ref(X_q, X_scale, X_zp, W_q, W_scale, W_zp, b_q, Y_scale, Y_zp): 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())