-
Notifications
You must be signed in to change notification settings - Fork 58
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
Fix KL error method per-tensor #1041
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,8 +89,8 @@ def _lp_error_histogram(q_bins: np.ndarray, | |
|
||
|
||
def _kl_error_function(x: np.ndarray, | ||
range_min: float, | ||
range_max: float, | ||
range_min: np.ndarray, | ||
range_max: np.ndarray, | ||
n_bins: int = 2048, | ||
n_bits: int = 8) -> np.float32: | ||
""" | ||
|
@@ -148,7 +148,8 @@ def _kl_error_function_wrapper(x: np.ndarray, | |
range_min: np.ndarray, | ||
range_max: np.ndarray, | ||
n_bins: int = 2048, | ||
n_bits: int = 8) -> np.ndarray: | ||
n_bits: int = 8, | ||
per_channel: int = False) -> np.ndarray: | ||
""" | ||
Computes the error function between a tensor and its quantized version for each channel. | ||
The error is based on the KL-divergence between the distributions. | ||
|
@@ -161,24 +162,28 @@ def _kl_error_function_wrapper(x: np.ndarray, | |
range_max: Array specifying the maximum bound of the quantization range for each channel. | ||
n_bins: Number of bins for the float histogram. | ||
n_bits: Number of bits used for quantization. | ||
per_channel: Whether quantization is done per-channel. | ||
|
||
Returns: | ||
An array containing the KL-divergence between the float and quantized histograms of the tensor for each channel. | ||
|
||
""" | ||
|
||
error_list = [] | ||
for j in range(x.shape[0]): # iterate all channels of the tensor. | ||
error_list.append(_kl_error_function(x[j], range_min[j], range_max[j], n_bins=n_bins, n_bits=n_bits)) | ||
if per_channel: | ||
for j in range(x.shape[0]): # iterate all channels of the tensor. | ||
error_list.append(_kl_error_function(x[j], range_min[j], range_max[j], n_bins=n_bins, n_bits=n_bits)) | ||
else: | ||
error_list.append(_kl_error_function(x, range_min, range_max, n_bins=n_bins, n_bits=n_bits)) | ||
return np.asarray(error_list) | ||
|
||
|
||
def _kl_error_histogram(q_bins: np.ndarray, | ||
q_count: np.ndarray, | ||
bins: np.ndarray, | ||
counts: np.ndarray, | ||
range_min: float, | ||
range_max: float) -> np.float32: | ||
range_min: np.ndarray, | ||
range_max: np.ndarray) -> np.float32: | ||
""" | ||
Compute the error function between a histogram to its quantized version. | ||
The error is computed based on the KL-divergence the distributions have. | ||
|
@@ -241,8 +246,8 @@ def _kl_error_histogram(q_bins: np.ndarray, | |
|
||
|
||
def _get_bins_indices_from_range(bins: np.ndarray, | ||
range_min: float, | ||
range_max: float) -> Tuple[int, int]: | ||
range_min: np.ndarray, | ||
range_max: np.ndarray) -> Tuple[int, int]: | ||
""" | ||
For bins and a threshold, compute the first and last bins in between the threshold | ||
ranges. | ||
|
@@ -262,7 +267,7 @@ def _get_bins_indices_from_range(bins: np.ndarray, | |
return first_bin_idx, last_bin_idx | ||
|
||
|
||
def _is_range_valid(bins: np.ndarray, range_min: float, range_max: float) -> bool: | ||
def _is_range_valid(bins: np.ndarray, range_min: np.ndarray, range_max: np.ndarray) -> bool: | ||
""" | ||
Check whether there are some bins from a numpy array of bins that are in between | ||
a threshold range or not. | ||
|
@@ -387,15 +392,36 @@ def get_threshold_selection_tensor_error_function(quantization_method: Quantizat | |
|
||
Returns: a Callable method that calculates the error between a tensor and a quantized tensor. | ||
""" | ||
if quant_error_method == qc.QuantizationErrorMethod.KL: | ||
if axis is None: | ||
# per-tensor | ||
if quantization_method == QuantizationMethod.UNIFORM: | ||
return lambda x, y, threshold: _kl_error_function_wrapper(x, range_min=threshold[0], | ||
range_max=threshold[1], | ||
n_bits=n_bits, | ||
per_channel=False) | ||
else: | ||
return lambda x, y, threshold: _kl_error_function_wrapper(x, range_min=0 if not signed else -threshold, | ||
range_max=threshold, | ||
n_bits=n_bits, | ||
per_channel=False) | ||
else: | ||
# per-channel | ||
if quantization_method == QuantizationMethod.UNIFORM: | ||
return lambda x, y, threshold: _kl_error_function_wrapper(x, range_min=threshold[:, 0], | ||
range_max=threshold[:, 1], | ||
n_bits=n_bits, | ||
per_channel=True) | ||
else: | ||
return lambda x, y, threshold: _kl_error_function_wrapper(x, range_min=0 if not signed else -threshold, | ||
range_max=threshold, | ||
n_bits=n_bits, | ||
per_channel=True) | ||
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. Maybe use variables to pass to _kl_error_function_wrapper to avoid unnecessary duplications? |
||
|
||
quant_method_error_function_mapping = { | ||
qc.QuantizationErrorMethod.MSE: lambda x, y, threshold: compute_mse(x, y, norm=norm, axis=axis), | ||
qc.QuantizationErrorMethod.MAE: lambda x, y, threshold: compute_mae(x, y, norm=norm, axis=axis), | ||
qc.QuantizationErrorMethod.LP: lambda x, y, threshold: compute_lp_norm(x, y, p=p, norm=norm, axis=axis), | ||
qc.QuantizationErrorMethod.KL: | ||
lambda x, y, threshold: _kl_error_function_wrapper(x, range_min=threshold[:,0], range_max=threshold[:,1], | ||
n_bits=n_bits) if quantization_method == QuantizationMethod.UNIFORM | ||
else _kl_error_function_wrapper(x, range_min=0 if not signed else -threshold, range_max=threshold, n_bits=n_bits) | ||
} | ||
|
||
return quant_method_error_function_mapping[quant_error_method] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe this can be done in a vectorial way?