-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix]: Enable SYMMETRIC_NO_CLIPPING_ERR Mapping type and tests
Signed-off-by: Nikhil Gupta <[email protected]>
- Loading branch information
Showing
2 changed files
with
86 additions
and
2 deletions.
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
84 changes: 84 additions & 0 deletions
84
...mental/tests/test_packed_linear_int8_dynamic_activation_intx_weight_layout_target_aten.py
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import copy | ||
import unittest | ||
|
||
import torch | ||
|
||
from torchao.dtypes import PlainLayout | ||
from torchao.experimental.packed_linear_int8_dynamic_activation_intx_weight_layout import ( | ||
PackedLinearInt8DynamicActivationIntxWeightLayout, | ||
) | ||
from torchao.experimental.quant_api import ( | ||
int8_dynamic_activation_intx_weight, | ||
) | ||
from torchao.quantization.granularity import ( | ||
PerGroup, | ||
PerRow, | ||
) | ||
from torchao.quantization.quant_api import quantize_ | ||
from torchao.utils import unwrap_tensor_subclass | ||
from torchao.quantization.quant_primitives import MappingType | ||
|
||
|
||
class TestPackedLinearInt8DynamicActivationIntxWeightLayoutAten(unittest.TestCase): | ||
def test_accuracy(self): | ||
""" | ||
Checks the accuracy of PackedLinearInt8DynamicActivationIntxWeightLayout() by comparing | ||
its results to the results of a reference model that uses PlainLayout() | ||
""" | ||
granularities = [PerRow()] | ||
m = 32 | ||
n = 128 | ||
k = 256 | ||
activations = torch.randn(m, k) | ||
weight_mapping_type = MappingType.SYMMETRIC_NO_CLIPPING_ERR | ||
model = torch.nn.Sequential(*[torch.nn.Linear(k, n, bias=False)]) | ||
|
||
for weight_dtype in [ | ||
torch.int4, | ||
]: | ||
for has_weight_zeros in [True]: | ||
for granularity in granularities: | ||
print( | ||
f"Testing weight_dtype={weight_dtype}, has_weight_zeros={ | ||
has_weight_zeros}, granularity={granularity}" | ||
) | ||
quantized_model = copy.deepcopy(model) | ||
quantize_( | ||
quantized_model, | ||
int8_dynamic_activation_intx_weight( | ||
weight_dtype=weight_dtype, | ||
granularity=granularity, | ||
has_weight_zeros=has_weight_zeros, | ||
weight_mapping_type=weight_mapping_type, | ||
layout=PackedLinearInt8DynamicActivationIntxWeightLayout( | ||
target="aten"), # default | ||
), | ||
) | ||
|
||
quantized_model_reference = copy.deepcopy(model) | ||
quantize_( | ||
quantized_model_reference, | ||
int8_dynamic_activation_intx_weight( | ||
weight_dtype=weight_dtype, | ||
granularity=granularity, | ||
has_weight_zeros=has_weight_zeros, | ||
layout=PlainLayout(), | ||
), | ||
) | ||
|
||
with torch.no_grad(): | ||
res = quantized_model(activations) | ||
ref = quantized_model_reference(activations) | ||
|
||
mean_err = ((res - ref).abs() / ref).mean() | ||
self.assertTrue(mean_err < 0.04) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |