PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
Fix compatibility with latest compressed_tensors compressor refactor (#1576)
Yi Liu49d2dbb0de14
diff --git a/auto_round/export/export_to_llmcompressor/export.py b/auto_round/export/export_to_llmcompressor/export.pyindex e5fe46abf..61685377c 100644--- a/auto_round/export/export_to_llmcompressor/export.py+++ b/auto_round/export/export_to_llmcompressor/export.py@@ -97,11 +97,42 @@ def _get_quant_format(model):return None+def _compress_and_set_format(layer, scheme, device=None):+ """Compress a layer and set its quantization format, with backward compatibility.++ Supports both old (<0.9.0) and new (>=0.9.0) compressed_tensors APIs:+ - New API: compress_module() handles compression, state dict replacement, status, and format.+ - Old API: NaiveQuantizationCompressor instance + manual param management + set_per_module_format.+ """+ try:+ from compressed_tensors.compressors import compress_module # pylint: disable=E0401++ compress_module(layer)+ except (ImportError, TypeError):+ from compressed_tensors.compressors import NaiveQuantizationCompressor # pylint: disable=E0401+ from compressed_tensors.config.format import set_per_module_format # pylint: disable=E0401+ from compressed_tensors.quantization import QuantizationStatus # pylint: disable=E0401+ from compressed_tensors.utils import ( # pylint: disable=E0401+ delete_offload_parameter,+ register_offload_parameter,+ )++ compressor = NaiveQuantizationCompressor()+ q_state_dict = compressor.compress(layer.state_dict(), names_to_scheme={"": scheme}, show_progress=False)++ for param_name, _ in list(layer.named_parameters(recurse=False)):+ delete_offload_parameter(layer, param_name)++ for param_name, value in q_state_dict.items():+ param = torch.nn.Parameter(value, requires_grad=False)+ register_offload_parameter(layer, param_name, param, device)++ layer.quantization_status = QuantizationStatus.COMPRESSED+ set_per_module_format(layer)++def pack_layer(name, model, device=None):- from compressed_tensors.compressors import NaiveQuantizationCompressor # pylint: disable=E0401- from compressed_tensors.config.format import set_per_module_format # pylint: disable=E0401from compressed_tensors.quantization import QuantizationStatus # pylint: disable=E0401- from compressed_tensors.utils import delete_offload_parameter, register_offload_parameter # pylint: disable=E0401layer = get_module(model, name)if type(layer) not in SUPPORTED_LAYER_TYPES and not isinstance(layer, WrapperWALayer): ##already packed@@ -135,20 +166,7 @@ def pack_layer(name, model, device=None):setattr(layer, "weight_zero_point", torch.nn.Parameter(zp.to(weight_device), requires_grad=False))delattr(layer, "scale")- compressor = NaiveQuantizationCompressor()- q_state_dict = compressor.compress(layer.state_dict(), names_to_scheme={"": scheme}, show_progress=False)-- # remove any existing parameters- for name, _ in list(layer.named_parameters(recurse=False)):- delete_offload_parameter(layer, name)-- # replace with compressed parameters- for name, value in q_state_dict.items():- param = torch.nn.Parameter(value, requires_grad=False)- register_offload_parameter(layer, name, param, device)-- layer.quantization_status = QuantizationStatus.COMPRESSED- set_per_module_format(layer)+ _compress_and_set_format(layer, scheme, device)@torch.no_grad()diff --git a/test/test_cpu/advanced/test_low_precision_input_model.py b/test/test_cpu/advanced/test_low_precision_input_model.pyindex 48aa59b24..ccdf0634f 100644--- a/test/test_cpu/advanced/test_low_precision_input_model.py+++ b/test/test_cpu/advanced/test_low_precision_input_model.py@@ -1,3 +1,4 @@+import pytestimport torchfrom auto_round.utils.weight_handler import (@@ -14,6 +15,7 @@ class TestCompressedTensor:mxfp4_model_path = "QuixiAI/Llama-3.2-1B-MXFP4"fp8_block_model_path = "RedHatAI/Qwen3-0.6B-FP8-BLOCK"+ @pytest.mark.skip(reason="CompressedLinear removed in compressed_tensors PR #610, see #1578")def test_fp8_block(self):model = get_tiny_model(get_model_path(self.fp8_block_model_path))assert (@@ -26,6 +28,7 @@ class TestCompressedTensor:type(model.model.layers[0].mlp.up_proj) is torch.nn.Linear), "CompressedLinear layer was not converted to Linear"+ @pytest.mark.skip(reason="CompressedLinear removed in compressed_tensors PR #610, see #1578")def test_nvfp4(self):model = get_tiny_model(get_model_path(self.nvfp4_model_path))assert (@@ -38,6 +41,7 @@ class TestCompressedTensor:type(model.model.layers[0].mlp.up_proj) is torch.nn.Linear), "CompressedLinear layer was not converted to Linear"+ @pytest.mark.skip(reason="CompressedLinear removed in compressed_tensors PR #610, see #1578")def test_mxfp4(self):model = get_tiny_model(get_model_path(self.mxfp4_model_path))assert (diff --git a/test/test_cpu/requirements.txt b/test/test_cpu/requirements.txtindex 80f981849..a6ce699a7 100644--- a/test/test_cpu/requirements.txt+++ b/test/test_cpu/requirements.txt@@ -5,8 +5,7 @@ sentencepiecetorchvisionpillownumba-llmcompressor @ git+https://github.com/vllm-project/llm-compressor.git@370c04c34191c0e5fb88cf889e16578bf8c7fe20-compressed-tensors==0.14.1a20260313 # temporary pin for llmcompressor+llmcompressor @ git+https://github.com/vllm-project/llm-compressor.git@mainlm_eval >= 0.4.10 # for transformers >= 5.0.0diffusersprotobufdiff --git a/test/test_cuda/requirements_llmc.txt b/test/test_cuda/requirements_llmc.txtindex d03556a6b..b11e53b7a 100644--- a/test/test_cuda/requirements_llmc.txt+++ b/test/test_cuda/requirements_llmc.txt@@ -1,3 +1,2 @@-llmcompressor @ git+https://github.com/vllm-project/llm-compressor.git@370c04c34191c0e5fb88cf889e16578bf8c7fe20-compressed-tensors==0.14.1a20260313 # temporary pin for llmcompressor+llmcompressor @ git+https://github.com/vllm-project/llm-compressor.git@mainparameterized