Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[quant] Compute scale and zero point automatically in testing::_quantize #46232

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 18 additions & 11 deletions test/quantization/test_quantized_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def qlinear_ref(X_q, X_scale, X_zp, W_q, W_scale, W_zp, b_q, Y_scale, Y_zp):
)
if b_q is not None:
Prod_XqWq_ref += b_q
Y_q_ref = _quantize(Prod_XqWq_ref, Y_scale / (X_scale * W_scale), Y_zp)
Y_q_ref, _, _ = _quantize(Prod_XqWq_ref,
Y_scale / (X_scale * W_scale),
Y_zp)
return Y_q_ref

"""Computes the output shape given pooling parameters."""
Expand Down Expand Up @@ -752,7 +754,7 @@ def test_qadd_relu_same_qparams(self):

# Add ReLU ground truth
C = (qA.dequantize() + qB.dequantize()).numpy()
qC = _quantize(C, scale, zero_point, dtype=np_dtype[dtype])
qC, _, _ = _quantize(C, scale, zero_point, dtype=np_dtype[dtype])
qC_hat = add(qA, qB, scale=scale, zero_point=zero_point)
np.testing.assert_equal(qC, qC_hat.int_repr(),
"Quantized addition failed.")
Expand All @@ -766,7 +768,8 @@ def test_qadd_relu_same_qparams(self):
# Add + ReLU ground truth
Crelu = C.copy()
Crelu[C < 0] = 0
qCrelu = _quantize(Crelu, scale, zero_point, dtype=np_dtype[dtype])
qCrelu, _, _ = _quantize(Crelu, scale, zero_point,
dtype=np_dtype[dtype])
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.")
Expand Down Expand Up @@ -807,7 +810,8 @@ def test_qadd_relu_different_qparams(self):

# Add ground truth
C = (qA.dequantize() + qB.dequantize()).numpy()
qC = _quantize(C, scale_C, zero_point_C, dtype=np_dtype[dtype])
qC, _, _ = _quantize(C, scale_C, zero_point_C,
dtype=np_dtype[dtype])
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.")
Expand All @@ -821,7 +825,8 @@ def test_qadd_relu_different_qparams(self):
# Add + ReLU ground truth
Crelu = C.copy()
Crelu[C < 0] = 0
qCrelu = _quantize(Crelu, scale_C, zero_point_C, dtype=np_dtype[dtype])
qCrelu, _, _ = _quantize(Crelu, scale_C, zero_point_C,
dtype=np_dtype[dtype])
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.")
Expand Down Expand Up @@ -852,7 +857,7 @@ def test_qmul_relu_same_qparams(self):

# mul ReLU ground truth
C = (qA.dequantize() * qB.dequantize()).numpy()
qC = _quantize(C, scale, zero_point, dtype=np_dtype[dtype])
qC, _, _ = _quantize(C, scale, zero_point, dtype=np_dtype[dtype])
qC_hat = mul(qA, qB, scale=scale, zero_point=zero_point)
np.testing.assert_equal(qC, qC_hat.int_repr(),
"Quantized mulition failed.")
Expand All @@ -866,7 +871,8 @@ def test_qmul_relu_same_qparams(self):
# mul + ReLU ground truth
Crelu = C.copy()
Crelu[C < 0] = 0
qCrelu = _quantize(Crelu, scale, zero_point, dtype=np_dtype[dtype])
qCrelu, _, _ = _quantize(Crelu, scale, zero_point,
dtype=np_dtype[dtype])
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.")
Expand Down Expand Up @@ -918,7 +924,7 @@ def test_qmul_relu_different_qparams(self):

# mul ground truth
C = (qA.dequantize() * qB.dequantize()).numpy()
qC = _quantize(C, scale_C, zero_point_C, dtype=np_dtype[dtype])
qC, _, _ = _quantize(C, scale_C, zero_point_C, dtype=np_dtype[dtype])
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.")
Expand All @@ -932,7 +938,8 @@ def test_qmul_relu_different_qparams(self):
# mul + ReLU ground truth
Crelu = C.copy()
Crelu[C < 0] = 0
qCrelu = _quantize(Crelu, scale_C, zero_point_C, dtype=np_dtype[dtype])
qCrelu, _, _ = _quantize(Crelu, scale_C, zero_point_C,
dtype=np_dtype[dtype])
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.")
Expand Down Expand Up @@ -970,7 +977,7 @@ def test_qmul_broadcast(self):

# mul ground truth
C = (qA.dequantize() * qB.dequantize()).numpy()
qC = _quantize(C, scale_C, zero_point_C)
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.")
Expand Down Expand Up @@ -4073,7 +4080,7 @@ def test_qnnpack_add(self, A, zero_point, scale_A, scale_B, scale_C):
# Add ground truth
C = (qA.dequantize() + qB.dequantize()).numpy()

qC = _quantize(C, scale_C, zero_point_C)
qC, _, _ = _quantize(C, scale_C, zero_point_C)

qC_qnnp = torch.ops.quantized.add(qA, qB, scale_C, zero_point_C)

Expand Down
15 changes: 13 additions & 2 deletions torch/testing/_internal/common_quantized.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,27 @@ def _conv_output_shape(input_size, kernel_size, padding, stride, dilation,
* (dilation - 1)) / stride) + 2 * output_padding + 1

# Quantization references
def _quantize(x, scale, zero_point, qmin=None, qmax=None, dtype=np.uint8):
def _quantize(x, scale=None, zero_point=None, qmin=None, qmax=None,
dtype=np.uint8):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good idea to update the docstring for this function to reflect the new behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the comment here? This is just an internal function for testing -- do we have a docstring?

"""Quantizes a numpy array."""
if qmin is None:
qmin = np.iinfo(dtype).min
if qmax is None:
qmax = np.iinfo(dtype).max
if scale is None:
fmin = min(x.min().item(), 0)
fmax = max(x.max().item(), 0)
if fmin == fmax:
scale = 1.0
else:
scale = (fmax - fmin) / (qmax - qmin)
if zero_point is None:
zero_point = int(round(qmin - fmin / scale))
zero_point = np.clip(zero_point, qmin, qmax)
qx = np.round(x / scale + zero_point).astype(np.int64)
qx = np.clip(qx, qmin, qmax)
qx = qx.astype(dtype)
return qx
return qx, scale, zero_point
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we actually using the returned scale and zero_point anywhere in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in one case I needed it -- this is optional, but because the current function returns the int_repr of the x, it losses information about the scale and zero_point, especially if it is inferred internally in this function



def _dequantize(qx, scale, zero_point):
Expand Down