Skip to content

Commit fbe8427

Browse files
committed
Enable HQQ quantization algorithm by default
Add configurable qparams_algorithm parameter to quantize_model_() with "hqq_scale_only" as default. HQQ (Half-Quadratic Quantization) provides better accuracy by minimizing reconstruction error during quantization. Applied to IntxWeightOnlyConfig and Int8DynamicActivationIntxWeightConfig. Int4WeightOnlyConfig (4w with packing) keeps hardcoded "hqq" due to different API. UIntxWeightOnlyConfig (fpa4w) unchanged as it lacks this parameter. Users can override globally via: from optimum.exporters.executorch import quantization quantization.DEFAULT_QPARAMS_ALGORITHM = "affine"
1 parent 5bf1aeb commit fbe8427

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

optimum/exporters/executorch/quantization.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,24 @@
1818
import torch
1919

2020

21+
# Applied to IntxWeightOnlyConfig and Int8DynamicActivationIntxWeightConfig.
22+
# Not applied to Int4WeightOnlyConfig (different API) or UIntxWeightOnlyConfig (no such param).
23+
DEFAULT_QPARAMS_ALGORITHM = "hqq_scale_only"
24+
25+
2126
def quantize_model_(
2227
eager_model: torch.nn.Module,
2328
qlinear_config: Optional[str] = None,
2429
qlinear_group_size: Optional[int] = 32,
2530
qlinear_packing_format: Optional[str] = None,
2631
qembedding_config: Optional[str] = None,
2732
qembedding_group_size: Optional[int] = 0,
33+
qparams_algorithm: Optional[str] = None,
2834
) -> torch.nn.Module:
35+
# qparams_algorithm is applied to IntxWeightOnlyConfig and Int8DynamicActivationIntxWeightConfig.
36+
# Not applied to Int4WeightOnlyConfig (different API) or UIntxWeightOnlyConfig (no such param).
37+
if qparams_algorithm is None:
38+
qparams_algorithm = DEFAULT_QPARAMS_ALGORITHM
2939
if not (qlinear_config or qembedding_config):
3040
return
3141

@@ -54,10 +64,12 @@ def quantize_model_(
5464
"4w": IntxWeightOnlyConfig(
5565
weight_dtype=torch.int4,
5666
granularity=embedding_weight_granularity,
67+
intx_choose_qparams_algorithm=qparams_algorithm,
5768
),
5869
"8w": IntxWeightOnlyConfig(
5970
weight_dtype=torch.int8,
6071
granularity=embedding_weight_granularity,
72+
intx_choose_qparams_algorithm=qparams_algorithm,
6173
),
6274
}[qembedding_config]
6375

@@ -75,6 +87,7 @@ def build_linear_config(quant_config_key: str, granularity: str, packing_format:
7587
return Int8DynamicActivationIntxWeightConfig(
7688
weight_dtype=torch.int4,
7789
weight_granularity=granularity,
90+
intx_choose_qparams_algorithm=qparams_algorithm,
7891
)
7992
if quant_config_key == "4w":
8093
# Determine if we need to use Int4WeightOnlyConfig with int4_packing_format
@@ -88,16 +101,19 @@ def build_linear_config(quant_config_key: str, granularity: str, packing_format:
88101
return IntxWeightOnlyConfig(
89102
weight_dtype=torch.int4,
90103
granularity=granularity,
104+
intx_choose_qparams_algorithm=qparams_algorithm,
91105
)
92106
if quant_config_key == "8w":
93107
return IntxWeightOnlyConfig(
94108
weight_dtype=torch.int8,
95109
granularity=granularity,
110+
intx_choose_qparams_algorithm=qparams_algorithm,
96111
)
97112
if quant_config_key == "8da8w":
98113
return Int8DynamicActivationIntxWeightConfig(
99114
weight_dtype=torch.int8,
100115
weight_granularity=PerAxis(0),
116+
intx_choose_qparams_algorithm=qparams_algorithm,
101117
)
102118
if quant_config_key == "fpa4w":
103119
# Need to import to load the ops

0 commit comments

Comments
 (0)