-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Changes from 4 commits
ae74b42
0026828
ae3b62d
bbc0701
f027bc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
"""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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
|
||
def _dequantize(qx, scale, zero_point): | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?