Skip to content

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

fix: prevent quantized layers from being reinitialized by Transformers (#2147)

Heng Guoa5e7393ec411
diff --git a/auto_round/calibration/diffusion.py b/auto_round/calibration/diffusion.py
index 7d22d59c2..688be5936 100644
--- a/auto_round/calibration/diffusion.py
+++ b/auto_round/calibration/diffusion.py
@@ -120,8 +120,7 @@ class DiffusionCalibrator(LLMCalibrator):
exit(-1)
target_device = device_manager.device
- if pipe.device != torch.device(target_device):
- pipe.to(target_device)
+ pipe.to(torch.device(target_device))
pipeline_fn = getattr(pipe, "_autoround_pipeline_fn", None)
# Check if this is an I2V pipeline (needs calibration image)
requires_image = False
diff --git a/auto_round/inference/convert_model.py b/auto_round/inference/convert_model.py
index b04c1bbaa..2a832d125 100644
--- a/auto_round/inference/convert_model.py
+++ b/auto_round/inference/convert_model.py
@@ -476,6 +476,13 @@ def _replace_by_quant_layers(
new_layer = _create_quant_layer(layer, layer_backend, config, in_features, out_features, packing_format)
set_module(module, layer_name, new_layer)
+ # Transformers may run model-specific initialization after loading. Mark both the
+ # replacement and its direct parent so parent initializers do not access attributes
+ # such as ``c_proj.weight`` that packed quantized layers intentionally do not expose.
+ new_layer._is_hf_initialized = True
+ parent_name, _, _ = layer_name.rpartition(".")
+ parent_module = get_module(module, parent_name) if parent_name else module
+ parent_module._is_hf_initialized = True
return used_backends
diff --git a/test/unit/test_cpu/algorithms/test_awq.py b/test/unit/test_cpu/algorithms/test_awq.py
index 4b5569d0f..854451fbd 100644
--- a/test/unit/test_cpu/algorithms/test_awq.py
+++ b/test/unit/test_cpu/algorithms/test_awq.py
@@ -279,7 +279,8 @@ class TestAWQMoE:
for name in fp_layers:
assert name.endswith("gate"), f"Unexpected FP layer: {name}"
- @pytest.mark.timeout(300)
+ # TODO: Investigate and fix the excessive test runtime instead of relying on an increased timeout.
+ @pytest.mark.timeout(400)
def test_awq_moe_save_quant_config(self, tiny_qwen_moe_model_path):
"""AWQ MoE: saved quantization_config should be consistent and loadable."""
ar = AutoRound(
diff --git a/test/unit/test_cpu/calibration/test_diffusion.py b/test/unit/test_cpu/calibration/test_diffusion.py
index b2fc8fd8e..0ea5c25c4 100644
--- a/test/unit/test_cpu/calibration/test_diffusion.py
+++ b/test/unit/test_cpu/calibration/test_diffusion.py
@@ -221,7 +221,7 @@ class TestDiffusionCalibrator:
):
calibrator.calib(nsamples=2, bs=1)
- assert seen == ["cuda:0"]
+ assert seen == [torch.device("cuda:0")]
def test_calib_uses_autoround_pipeline_fn_when_available(self, calibrator):
calls = []
diff --git a/test/unit/test_cpu/core/test_autoround.py b/test/unit/test_cpu/core/test_autoround.py
index 9eb4be7df..4603d94c6 100644
--- a/test/unit/test_cpu/core/test_autoround.py
+++ b/test/unit/test_cpu/core/test_autoround.py
@@ -108,7 +108,8 @@ class TestAutoRound:
)
autoround.quantize()
- @pytest.mark.timeout(120)
+ # TODO: Investigate and fix the excessive test runtime instead of relying on an increased timeout.
+ @pytest.mark.timeout(220)
def test_mx_fp4(self, dataloader):
model_name = opt_name_or_path
bits, group_size, sym = 4, 32, False