-
Notifications
You must be signed in to change notification settings - Fork 383
Int8Tensor migration cleanup #3407
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
+194
−214
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
48cdb61
Int8Tensor migration
jcaip 0b73aed
ruff fixes
jcaip 1e49945
add init
jcaip 669b6ee
fix ruff again
jcaip 9071526
update
jcaip 1539e0f
wip
jcaip d9a2b1b
Merge branch 'main' into jcaip/int8-tensor
jcaip 673f228
undo update tests
jcaip 739fd64
fix ruff
jcaip 750db1a
fix varname
jcaip 9410488
fix typing
jcaip 45a3a76
add tests
jcaip 4e2f09c
fix dtype
jcaip dd80cca
fix ci
jcaip 7f73062
address granularity cr
jcaip ac6a2b6
update _choose_quant_func_and_quantize_tensor
jcaip f28df4a
make block size required attribute
jcaip 328585e
made dtype required as well
jcaip ce4d568
address nits
jcaip a665d45
skip per tensor weight only test for now
jcaip 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 |
|---|---|---|
|
|
@@ -1341,6 +1341,10 @@ class Int8WeightOnlyConfig(AOBaseConfig): | |
|
|
||
| def __post_init__(self): | ||
| torch._C._log_api_usage_once("torchao.quantization.Int8WeightOnlyConfig") | ||
| if self.version == 2: | ||
| assert self.group_size is None, ( | ||
| f"Only support version 2 with group_size=None, got {self.group_size}" | ||
| ) | ||
|
|
||
|
|
||
| # for BC | ||
|
|
@@ -1522,9 +1526,7 @@ class Int8DynamicActivationInt8WeightConfig(AOBaseConfig): | |
| layout: Optional[Layout] = PlainLayout() | ||
| act_mapping_type: Optional[MappingType] = MappingType.SYMMETRIC | ||
| weight_only_decode: bool = False | ||
| # TODO: Revisit for supported granularitys | ||
| # https://github.com/pytorch/ao/pull/3241#discussion_r2551497849 | ||
| granularity: Optional[Granularity] = PerRow() | ||
| granularity: Granularity = PerRow() | ||
| set_inductor_config: bool = True | ||
| version: int = 1 | ||
|
|
||
|
|
@@ -1541,37 +1543,30 @@ def __post_init__(self): | |
|
|
||
|
|
||
| def _int8_dynamic_activation_int8_weight_quantize_tensor(weight, config): | ||
| layout = config.layout | ||
| act_mapping_type = config.act_mapping_type | ||
| weight_only_decode = config.weight_only_decode | ||
|
|
||
| in_features = weight.shape[-1] | ||
| # int8 dynamic quantization only has benefit when in_feature > 16 | ||
| if in_features <= 16: | ||
| logger.info( | ||
| f"Skipping applying Int8DynamicActivationInt8WeightConfig to weight of shape {weight.shape}" | ||
| f" because `in_feature` is <= 16: {in_features}" | ||
| ) | ||
| return weight | ||
| if config.version == 1: | ||
| layout = config.layout | ||
| act_mapping_type = config.act_mapping_type | ||
| weight_only_decode = config.weight_only_decode | ||
|
|
||
| in_features = weight.shape[-1] | ||
| # int8 dynamic quantization only has benefit when in_feature > 16 | ||
| if in_features <= 16: | ||
| logger.info( | ||
| f"Skipping applying Int8DynamicActivationInt8WeightConfig to weight of shape {weight.shape}" | ||
| f" because `in_feature` is <= 16: {in_features}" | ||
| ) | ||
| return weight | ||
|
|
||
| # weight settings | ||
| mapping_type = MappingType.SYMMETRIC | ||
| weight_zero_point_domain = ZeroPointDomain.NONE | ||
| # weight settings | ||
| mapping_type = MappingType.SYMMETRIC | ||
| weight_zero_point_domain = ZeroPointDomain.NONE | ||
|
|
||
| target_dtype = torch.int8 | ||
| eps = torch.finfo(torch.float32).eps | ||
| zero_point_dtype = torch.int64 | ||
| def get_weight_block_size(x): | ||
| return tuple([1 for _ in range(x.dim() - 1)] + [x.shape[-1]]) | ||
|
|
||
| if config.version == 1: | ||
| warnings.warn( | ||
| "Config Deprecation: version 1 of Int8DynamicActivationInt8WeightConfig is deprecated and will no longer be supported in a future release, please use version 2, see https://github.com/pytorch/ao/issues/2752 for more details" | ||
| ) | ||
| if isinstance(config.granularity, PerTensor): | ||
| block_size = weight.shape | ||
| else: | ||
| block_size = tuple( | ||
| [1 for _ in range(weight.dim() - 1)] + [weight.shape[-1]] | ||
| ) | ||
| target_dtype = torch.int8 | ||
| eps = torch.finfo(torch.float32).eps | ||
| zero_point_dtype = torch.int64 | ||
|
|
||
| if weight_only_decode: | ||
| input_quant_func = _int8_symm_per_token_reduced_range_quant_noop_decode | ||
|
|
@@ -1582,7 +1577,8 @@ def _int8_dynamic_activation_int8_weight_quantize_tensor(weight, config): | |
| else: | ||
| input_quant_func = _int8_asymm_per_token_quant | ||
|
|
||
| quantized_weight = to_affine_quantized_intx( | ||
| block_size = get_weight_block_size(weight) | ||
| new_weight = to_affine_quantized_intx( | ||
| weight, | ||
| mapping_type, | ||
| block_size, | ||
|
|
@@ -1592,24 +1588,32 @@ def _int8_dynamic_activation_int8_weight_quantize_tensor(weight, config): | |
| _layout=layout, | ||
| zero_point_domain=weight_zero_point_domain, | ||
| ) | ||
| quantized_weight = to_linear_activation_quantized( | ||
| quantized_weight, input_quant_func | ||
| ) | ||
| quantized_weight = to_linear_activation_quantized(new_weight, input_quant_func) | ||
| else: | ||
| from torchao.quantization.quantize_.workflows.int8.int8_tensor import ( | ||
| QuantizeTensorToInt8Kwargs, | ||
| ) | ||
|
|
||
| assert config.granularity in {PerRow(), PerTensor()}, ( | ||
| "Only PerRow and PerTensor are supported" | ||
| ) | ||
| weight_granularity = config.granularity | ||
| act_granularity = config.granularity | ||
|
|
||
| assert config.act_mapping_type == MappingType.SYMMETRIC, ( | ||
| "asymmetric dynamic quant not supported currently" | ||
| ) | ||
| assert config.version == 2, f"Unexpected version: {config.version}" | ||
|
|
||
| # TODO: Symmentric/Asymmetric choice for weight quantization | ||
| # https://github.com/pytorch/ao/pull/3241#discussion_r2551515539 | ||
| # TODO: Add block_size args to return in from_hp | ||
| # https://github.com/pytorch/ao/pull/3241#discussion_r2552016429 | ||
| quantized_weight = Int8Tensor.from_hp( | ||
| weight, | ||
| granularity=config.granularity, | ||
| act_quant_kwargs=QuantizeTensorToInt8Kwargs(granularity=config.granularity), | ||
| granularity=weight_granularity, | ||
|
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. mapping_type for weight is not passed?
Contributor
Author
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. the config doesn't have an option for the weight_mapping_type, so we just use the default (symmetric) |
||
| act_quant_kwargs=QuantizeTensorToInt8Kwargs( | ||
| granularity=act_granularity, | ||
| mapping_type=config.act_mapping_type, | ||
| ), | ||
| ) | ||
|
|
||
| return quantized_weight | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.