Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

bug fix: prune static kv/attention granularity keys when dtype not set (#2415)

Yi Liua20f5297c87d
diff --git a/auto_round/export/utils.py b/auto_round/export/utils.py
index 12cc289ee..9ef56f57d 100644
--- a/auto_round/export/utils.py
+++ b/auto_round/export/utils.py
@@ -507,6 +507,16 @@ def filter_quantization_config(quantization_config):
if quantization_config[k] is None:
quantization_config.pop(k)
+ # static_*_granularity only carries a value ("tensor") when the matching static
+ # dtype is set; without it the key is inert but misleads readers into thinking
+ # KV/attention quantization is configured.
+ for dtype_key, gran_key in (
+ ("static_kv_dtype", "static_kv_granularity"),
+ ("static_attention_dtype", "static_attention_granularity"),
+ ):
+ if quantization_config.get(dtype_key) is None:
+ quantization_config.pop(gran_key, None)
+
if quantization_config.get("act_bits", 16) >= 16:
quantization_config.pop("act_bits", None)
quantization_config.pop("act_data_type", None)
diff --git a/test/unit/common/export/test_export_utils.py b/test/unit/common/export/test_export_utils.py
index 7df0a6c9b..9099f0fe4 100644
--- a/test/unit/common/export/test_export_utils.py
+++ b/test/unit/common/export/test_export_utils.py
@@ -447,6 +447,40 @@ class TestFilterQuantizationConfig:
filter_quantization_config(cfg)
assert "amp" not in cfg
+ def test_granularity_without_dtype_removed(self):
+ cfg = {"static_kv_dtype": None, "static_kv_granularity": "tensor", "custom": "value"}
+ filter_quantization_config(cfg)
+ assert "static_kv_granularity" not in cfg
+
+ def test_granularity_missing_dtype_key_removed(self):
+ cfg = {"static_attention_granularity": "tensor", "custom": "value"}
+ filter_quantization_config(cfg)
+ assert "static_attention_granularity" not in cfg
+
+ def test_granularity_kept_with_dtype(self):
+ cfg = {
+ "static_kv_dtype": "fp8",
+ "static_kv_granularity": "head",
+ "static_attention_dtype": "fp8",
+ "static_attention_granularity": "tensor",
+ "custom": "value",
+ }
+ result = filter_quantization_config(cfg)
+ assert result["static_kv_granularity"] == "head"
+ assert result["static_attention_granularity"] == "tensor"
+ assert "custom" in result
+
+ def test_granularity_keys_are_independent(self):
+ cfg = {
+ "static_kv_dtype": "fp8",
+ "static_kv_granularity": "head",
+ "static_attention_granularity": "tensor",
+ "custom": "value",
+ }
+ result = filter_quantization_config(cfg)
+ assert result["static_kv_granularity"] == "head"
+ assert "static_attention_granularity" not in cfg
+
def test_act_bits_handling(self):
cfg = {"act_bits": 16, "act_data_type": "fp8", "custom": "value"}
result = filter_quantization_config(cfg)