-
Notifications
You must be signed in to change notification settings - Fork 501
Add reduce_range to avoid overflow in int8 tensor #4266
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
dd390af
Add reduce_range to avoid overflow in int8 tensor
2adbdd9
Refine UT
e087a60
Only test reduce_range=True on CPUs without VNNI support
3c0c12b
Hardcode recude range
9fd46cd
Move vnni check func into utils
4e825f1
Automatically set reduce_range
716cca8
Re-add user-configurable reduce_range
8a5840d
Add UT notes
741115a
Fix CI
6e8eb08
Set reduce_range default to False
6eaf74b
Use should_reduce_range in UT
6cdd804
Merge upstream/main into reduce_range
232882c
Move should_reduce_range to utils
ea96be6
Refine codes
c4d095c
Remove Optional
4877828
Modify assert
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -931,6 +931,10 @@ class Int8DynamicActivationInt8WeightConfig(AOBaseConfig): | |
| SYMMETRIC and ASYMMETRIC are supported. | ||
| set_inductor_config: bool = True - If True, adjusts `torchinductor` settings to recommended values | ||
| for better performance with this quantization scheme. | ||
| version (int): the version of the config | ||
| reduce_range (Optional[bool] = False): If True, both activation and weight int8 quantization use reduced range | ||
| [_REDUCED_QUANT_MIN, _REDUCED_QUANT_MAX] instead of full range | ||
| [_FULL_QUANT_MIN, _FULL_QUANT_MAX] to reduce overflow risk on platforms without VNNI instructions. | ||
|
|
||
| Example: | ||
|
|
||
|
|
@@ -945,6 +949,7 @@ class Int8DynamicActivationInt8WeightConfig(AOBaseConfig): | |
| ] = PerRow() | ||
| set_inductor_config: bool = True | ||
| version: int = 2 | ||
| reduce_range: Optional[bool] = False | ||
|
|
||
| def __post_init__(self): | ||
| torch._C._log_api_usage_once( | ||
|
|
@@ -969,6 +974,9 @@ def __post_init__(self): | |
| "Please set it to MappingType.SYMMETRIC or " | ||
| "MappingType.ASYMMETRIC." | ||
| ) | ||
| assert self.reduce_range in (True, False), ( | ||
| "`reduce_range` must be True or False" | ||
| ) | ||
|
|
||
|
|
||
| def _int8_dynamic_activation_int8_weight_quantize_tensor(weight, config): | ||
|
|
@@ -983,7 +991,9 @@ def _int8_dynamic_activation_int8_weight_quantize_tensor(weight, config): | |
| act_quant_kwargs=QuantizeTensorToInt8Kwargs( | ||
| granularity=act_granularity, | ||
| mapping_type=config.act_mapping_type, | ||
| reduce_range=config.reduce_range, | ||
| ), | ||
| reduce_range=config.reduce_range, | ||
| ) | ||
|
|
||
| return quantized_weight | ||
|
|
@@ -1037,6 +1047,9 @@ class Int8StaticActivationInt8WeightConfig(AOBaseConfig): | |
| act_mapping_type (MappingType): The mapping type for activation quantization. SYMMETRIC and ASYMMETRIC are supported. | ||
| set_inductor_config (bool): if True, adjusts `torchinductor` settings to recommended values. | ||
| version (int): the version of the config | ||
| reduce_range (Optional[bool] = False): If True, both activation and weight int8 quantization use reduced range | ||
| [_REDUCED_QUANT_MIN, _REDUCED_QUANT_MAX] instead of full range | ||
| [_FULL_QUANT_MIN, _FULL_QUANT_MAX] to reduce overflow risk on platforms without VNNI instructions. | ||
| """ | ||
|
|
||
| act_quant_scale: Optional[torch.Tensor] = None | ||
|
|
@@ -1047,6 +1060,7 @@ class Int8StaticActivationInt8WeightConfig(AOBaseConfig): | |
| act_mapping_type: Optional[MappingType] = MappingType.SYMMETRIC | ||
| set_inductor_config: bool = True | ||
| version: int = 1 | ||
| reduce_range: Optional[bool] = False | ||
|
|
||
| def __post_init__(self): | ||
| torch._C._log_api_usage_once( | ||
|
|
@@ -1066,6 +1080,9 @@ def __post_init__(self): | |
| "Please set it to MappingType.SYMMETRIC or " | ||
| "MappingType.ASYMMETRIC." | ||
| ) | ||
| assert self.reduce_range in (True, False), ( | ||
|
Contributor
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. same here |
||
| "`reduce_range` must be True or False" | ||
| ) | ||
|
|
||
| def get_act_quant_kwargs(self) -> QuantizeTensorToInt8Kwargs: | ||
| """Get the activation quantization kwargs for static quantization. | ||
|
|
@@ -1080,6 +1097,7 @@ def get_act_quant_kwargs(self) -> QuantizeTensorToInt8Kwargs: | |
| return QuantizeTensorToInt8Kwargs( | ||
| granularity=act_granularity, | ||
| mapping_type=self.act_mapping_type, | ||
| reduce_range=self.reduce_range, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -1110,9 +1128,11 @@ def _int8_static_activation_int8_weight_transform( | |
| act_quant_kwargs=QuantizeTensorToInt8Kwargs( | ||
| granularity=activation_granularity, | ||
| mapping_type=config.act_mapping_type, | ||
| reduce_range=config.reduce_range, | ||
| ), | ||
| act_quant_scale=config.act_quant_scale.detach(), | ||
| act_quant_zero_point=act_quant_zero_point, | ||
| reduce_range=config.reduce_range, | ||
| ) | ||
|
|
||
| setattr( | ||
|
|
||
This file contains hidden or 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.
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.
nit: maybe create a new
test_int8_tensor_cpu.pyit will be easier to separate tests by device for CI in the future if we want.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.
Thanks for comments, updated.