Skip to content

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

format add check for scheme args (#1332)

Heng Guo7f1dee6ee1c1
diff --git a/auto_round/formats.py b/auto_round/formats.py
index b8e384c65..a0689fb82 100644
--- a/auto_round/formats.py
+++ b/auto_round/formats.py
@@ -218,9 +218,13 @@ class OutputFormat(ABC):
if isinstance(scheme, str) and scheme.upper() in cls.support_schemes:
return True
if isinstance(scheme, QuantizationScheme):
- return True
+ return cls.check_scheme_args(scheme)
return False
+ @classmethod
+ def check_scheme_args(cls: OutputFormat, scheme: QuantizationScheme) -> bool:
+ return True
+
def check_and_reset_format(self, ar: BaseCompressor) -> str:
if self.backend is not None:
new_format = self.backend.check_and_reset_format(ar)
@@ -355,6 +359,26 @@ class LLMCompressorFormat(OutputFormat):
self.output_format = f"llm_compressor:{format}"
self.backend = None
+ @classmethod
+ def check_scheme_args(cls: OutputFormat, scheme: QuantizationScheme) -> bool:
+ error_logs = []
+ if scheme.bits not in [4, 8, 16]:
+ error_logs.append(f"bits={scheme.bits}")
+ if not re.search("mxfp|fp|nvfp", scheme.data_type):
+ error_logs.append(f"data_type={scheme.data_type}")
+ if scheme.data_type == "fp" and scheme.bits != 8:
+ error_logs.append(f"data_type={scheme.data_type}, bits={scheme.bits}")
+ if scheme.super_bits:
+ error_logs.append(f"super_bits={scheme.super_bits}")
+ if scheme.super_group_size:
+ error_logs.append(f"super_group_size={scheme.super_group_size}")
+ if error_logs:
+ raise ValueError(
+ f"LLMCompressor format support quantization scheme with {','.join(cls.support_schemes)} "
+ f"but got {', '.join(error_logs)}, please have a check."
+ )
+ return True
+
def check_and_reset_format(self, ar: BaseCompressor) -> str | None:
if self.backend is not None:
new_format = self.backend.check_and_reset_format(ar)
@@ -450,6 +474,24 @@ class AutoGPTQFormat(OutputFormat):
_check_divisible_by_32(ar)
return super().check_and_reset_format(ar)
+ @classmethod
+ def check_scheme_args(cls: OutputFormat, scheme: QuantizationScheme) -> bool:
+ error_logs = []
+ if scheme.bits not in [2, 3, 4, 8, 16]:
+ error_logs.append(f"bits={scheme.bits}")
+ if not re.search("int", scheme.data_type):
+ error_logs.append(f"data_type={scheme.data_type}")
+ if scheme.super_bits:
+ error_logs.append(f"super_bits={scheme.super_bits}")
+ if scheme.super_group_size:
+ error_logs.append(f"super_group_size={scheme.super_group_size}")
+ if error_logs:
+ raise ValueError(
+ f"{cls.format_name} format support quantization scheme with {','.join(cls.support_schemes)} "
+ f"but got {', '.join(error_logs)}, please have a check."
+ )
+ return True
+
def pack_layer(self, layer_name, model, device=None, **kwargs):
if self.output_format.startswith("auto_round"):
from auto_round.export.export_to_autoround.export import pack_layer
@@ -498,6 +540,24 @@ class AutoAWQFormat(OutputFormat):
support_schemes = ["W4A16"]
format_name = "auto_awq"
+ @classmethod
+ def check_scheme_args(cls: OutputFormat, scheme: QuantizationScheme) -> bool:
+ error_logs = []
+ if scheme.bits != 4:
+ error_logs.append(f"bits={scheme.bits}")
+ if not re.search("int", scheme.data_type):
+ error_logs.append(f"data_type={scheme.data_type}")
+ if scheme.super_bits:
+ error_logs.append(f"super_bits={scheme.super_bits}")
+ if scheme.super_group_size:
+ error_logs.append(f"super_group_size={scheme.super_group_size}")
+ if error_logs:
+ raise ValueError(
+ f"{cls.format_name} format support quantization scheme with {','.join(cls.support_schemes)} "
+ f"but got {', '.join(error_logs)}, please have a check."
+ )
+ return True
+
@staticmethod
def check_awq_gemm_compatibility(model, bits, group_size, sym, layer_configs=None):
"""Checks if a model is compatible with the AutoAWQ GEMM kernel.
@@ -638,6 +698,18 @@ class GGUFFormat(OutputFormat):
self.backend = None
self.mllm = ar.mllm
+ @classmethod
+ def check_scheme_args(cls: OutputFormat, scheme: QuantizationScheme) -> bool:
+ error_logs = []
+ if not re.search("int", scheme.data_type):
+ error_logs.append(f"data_type={scheme.data_type}")
+ if error_logs:
+ raise ValueError(
+ f"{cls.format_name} format support quantization scheme with {','.join(cls.support_schemes)} "
+ f"but got {', '.join(error_logs)}, please have a check."
+ )
+ return True
+
def check_and_reset_format(self, ar):
if ar.iters != 0 and ar.bits != 3 and not ar.enable_alg_ext:
logger.warning_once(
diff --git a/test/test_cpu/export/test_export.py b/test/test_cpu/export/test_export.py
index 511fd6287..cb6033de9 100644
--- a/test/test_cpu/export/test_export.py
+++ b/test/test_cpu/export/test_export.py
@@ -407,3 +407,38 @@ class TestAutoRound:
assert format_list[0].get_backend_name() == "auto_round:auto_awq"
assert format_list[1].output_format == "auto_gptq"
assert format_list[1].get_backend_name() == "auto_gptq"
+
+ def test_export_format_with_scheme(self, tiny_qwen_model_path):
+ from auto_round.formats import get_formats
+
+ ar = AutoRound(
+ model=tiny_qwen_model_path,
+ scheme="W4A16",
+ bits=2,
+ group_size=32,
+ sym=True,
+ )
+ with pytest.raises(ValueError, match="auto_awq format support quantization scheme with W4A16 but got bits=2"):
+ get_formats("auto_round:auto_awq", ar)
+
+ with pytest.raises(ValueError, match="but got bits=2, data_type=int"):
+ get_formats("auto_round:llm_compressor", ar)
+
+ ar = AutoRound(
+ model=tiny_qwen_model_path,
+ scheme="FP8_STATIC",
+ bits=4,
+ group_size=32,
+ sym=True,
+ )
+ with pytest.raises(ValueError, match="but got data_type=fp, bits=4"):
+ get_formats("auto_round:llm_compressor", ar)
+
+ ar = AutoRound(
+ model=tiny_qwen_model_path,
+ scheme="w2a16",
+ bits=4,
+ group_size=256,
+ sym=True,
+ )
+ get_formats("auto_round:auto_awq", ar)
diff --git a/test/test_cpu/integrations/test_llmcompressor.py b/test/test_cpu/integrations/test_llmcompressor.py
index c11f12456..c2966cedd 100644
--- a/test/test_cpu/integrations/test_llmcompressor.py
+++ b/test/test_cpu/integrations/test_llmcompressor.py
@@ -22,22 +22,23 @@ class TestLLMC:
shutil.rmtree("./saved", ignore_errors=True)
shutil.rmtree("runs", ignore_errors=True)
- def test_llmcompressor_w8a8(self):
- bits, group_size, sym, act_bits = 8, -1, True, 8
- ## quantize the model
- autoround = AutoRound(
- self.model,
- self.tokenizer,
- bits=bits,
- group_size=group_size,
- sym=sym,
- act_bits=act_bits,
- seqlen=8,
- nsamples=2,
- iters=0,
- )
- autoround.quantize()
- autoround.save_quantized("./saved", format="llm_compressor", inplace=True)
+ # remove since w8a8 not in llmcompressor format supported schemes
+ # def test_llmcompressor_w8a8(self):
+ # bits, group_size, sym, act_bits = 8, -1, True, 8
+ # ## quantize the model
+ # autoround = AutoRound(
+ # self.model,
+ # self.tokenizer,
+ # bits=bits,
+ # group_size=group_size,
+ # sym=sym,
+ # act_bits=act_bits,
+ # seqlen=8,
+ # nsamples=2,
+ # iters=0,
+ # )
+ # autoround.quantize()
+ # autoround.save_quantized("./saved", format="llm_compressor", inplace=True)
def test_llmcompressor_fp8(self):
## quantize the model