PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
rm duplicate args of the quantization extra config (#1334)
Weiwei7e4ba88ae0cb
diff --git a/auto_round/export/export_to_autoround/export.py b/auto_round/export/export_to_autoround/export.pyindex 52b35f9a4..8d78d0bee 100644--- a/auto_round/export/export_to_autoround/export.py+++ b/auto_round/export/export_to_autoround/export.py@@ -294,9 +294,14 @@ def save_quantized_as_autoround(regex_config = quantization_config.pop("regex_config")if regex_config is not None:- for name in regex_config.keys():+ for name, cfg in regex_config.items():regex_name = to_standard_regex(name)- extra_config[regex_name] = {**{k: regex_config[name][k] for k in scheme_keys}}+ neq_keys = check_neq_config(cfg, **{k: quantization_config[k] for k in scheme_keys})+ if len(neq_keys) > 0:+ extra_config[regex_name] = {}+ for key in neq_keys:+ if cfg.get(key) is not None:+ extra_config[regex_name][key] = cfg[key]if len(extra_config) > 0:quantization_config["extra_config"] = extra_configdiff --git a/auto_round/export/export_to_autoround/export_to_fp8.py b/auto_round/export/export_to_autoround/export_to_fp8.pyindex b97a5db5a..38bea79f3 100644--- a/auto_round/export/export_to_autoround/export_to_fp8.py+++ b/auto_round/export/export_to_autoround/export_to_fp8.py@@ -191,7 +191,7 @@ def save_quantized_as_autoround(neq_keys = check_neq_config(cfg, **{k: quantization_config[k] for k in scheme_keys})if len(neq_keys) > 0:extra_config[layer_name] = {}- for key in scheme_keys:+ for key in neq_keys:if cfg[key] is not None:extra_config[layer_name][key] = cfg[key]diff --git a/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py b/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.pyindex 7265941d3..1320846dc 100644--- a/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py+++ b/auto_round/export/export_to_autoround/export_to_nvfp_mxfp.py@@ -210,15 +210,20 @@ def save_quantized_as_fp(neq_keys = check_neq_config(cfg, **{k: quantization_config[k] for k in scheme_keys})if len(neq_keys) > 0:extra_config[layer_name] = {}- for key in scheme_keys:+ for key in neq_keys:if cfg.get(key, None) is not None:extra_config[layer_name][key] = cfg.get(key, None)regex_config = quantization_config.pop("regex_config")if regex_config is not None:- for name in regex_config.keys():+ for name, cfg in regex_config.items():regex_name = to_standard_regex(name)- extra_config[regex_name] = {**{k: regex_config[name][k] for k in scheme_keys}}+ neq_keys = check_neq_config(cfg, **{k: quantization_config[k] for k in scheme_keys})+ if len(neq_keys) > 0:+ extra_config[regex_name] = {}+ for key in neq_keys:+ if cfg.get(key) is not None:+ extra_config[regex_name][key] = cfg[key]if len(extra_config) > 0:quantization_config["extra_config"] = extra_configdiff --git a/test/test_cpu/quantization/test_act_quantization.py b/test/test_cpu/quantization/test_act_quantization.pyindex 47cda3599..014d80f6f 100644--- a/test/test_cpu/quantization/test_act_quantization.py+++ b/test/test_cpu/quantization/test_act_quantization.py@@ -119,14 +119,8 @@ class TestAutoRoundAct:# check inblock layer config valueskproj_config = model.config.quantization_config.extra_config["model.decoder.layers.1.self_attn.k_proj"]- assert "act_data_type" in kproj_config.keys() and kproj_config["act_data_type"] == "mx_fp"assert "act_bits" in kproj_config.keys() and kproj_config["act_bits"] == 8- assert "act_group_size" in kproj_config.keys() and kproj_config["act_group_size"] == 32- assert "act_sym" in kproj_config.keys() and kproj_config["act_sym"]- assert "data_type" in kproj_config.keys() and kproj_config["data_type"] == "mx_fp"assert "bits" in kproj_config.keys() and kproj_config["bits"] == 8- assert "group_size" in kproj_config.keys() and kproj_config["group_size"] == 32- assert "sym" in kproj_config.keys() and kproj_config["sym"]shutil.rmtree(quantized_model_path, ignore_errors=True)def test_act_config_NVFP4_saving(self, tiny_opt_model_path, dataloader):@@ -144,14 +138,8 @@ class TestAutoRoundAct:autoround.quantize_and_save(output_dir=quantized_model_path, format="auto_round")model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cpu")kproj_config = model.config.quantization_config.extra_config["model.decoder.layers.1.self_attn.k_proj"]- assert "act_data_type" in kproj_config.keys() and kproj_config["act_data_type"] == "nv_fp4_with_static_gs"assert "act_bits" in kproj_config.keys() and kproj_config["act_bits"] == 16- assert "act_group_size" in kproj_config.keys() and kproj_config["act_group_size"] == 16- assert "act_sym" in kproj_config.keys() and kproj_config["act_sym"]- assert "data_type" in kproj_config.keys() and kproj_config["data_type"] == "nv_fp"assert "bits" in kproj_config.keys() and kproj_config["bits"] == 16- assert "group_size" in kproj_config.keys() and kproj_config["group_size"] == 16- assert "sym" in kproj_config.keys() and kproj_config["sym"]shutil.rmtree(quantized_model_path, ignore_errors=True)def test_WOQ_config_INT_saving(self, tiny_opt_model_path, dataloader):diff --git a/test/test_cpu/quantization/test_mix_bits.py b/test/test_cpu/quantization/test_mix_bits.pyindex 5db3053cb..e5a8a7c42 100644--- a/test/test_cpu/quantization/test_mix_bits.py+++ b/test/test_cpu/quantization/test_mix_bits.py@@ -112,7 +112,17 @@ class TestAutoRound:layer_config=layer_config,)quantized_model_path = "./saved"- compressed_model = autoround.quantize_and_save(output_dir=quantized_model_path, format="auto_round")+ autoround.quantize_and_save(output_dir=quantized_model_path, format="auto_round")+ config_file = Path(quantized_model_path) / "config.json"+ with open(config_file, "r", encoding="utf-8") as f:+ config = json.load(f)+ quant_config = config.get("quantization_config", {})+ extra_config = quant_config.get("extra_config", {})+ # check extra_config only saved attributes differing from Scheme values+ assert "act_bits" not in extra_config[".*fc1.*"].keys() ## TODO refine this assert+ assert "group_size" not in extra_config[".*fc1.*"].keys()+ assert "act_bits" not in extra_config["model.decoder.layers.0.self_attn.k_proj"].keys()+ assert "group_size" not in extra_config["model.decoder.layers.0.self_attn.k_proj"].keys()model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cpu")assert model.model.decoder.layers[0].self_attn.k_proj.bits == 8assert model.model.decoder.layers[0].self_attn.q_proj.bits == 3@@ -167,26 +177,10 @@ class TestAutoRound:# remove old extra_config(which contains full name layer configs), only test regex config loadingnew_extra_config = {".*fc1.*": { # standard regex- "act_bits": 16,- "act_data_type": "float",- "act_dynamic": True,- "act_group_size": 128,- "act_sym": True,"bits": 16,- "data_type": "int",- "group_size": 128,- "sym": True,},"k_proj": { # part name- "act_bits": 16,- "act_data_type": "float",- "act_dynamic": True,- "act_group_size": 128,- "act_sym": True,"bits": 8,- "data_type": "int",- "group_size": 128,- "sym": True,},}config_file = Path(quantized_model_path) / "config.json"@@ -194,6 +188,9 @@ class TestAutoRound:config = json.load(f)quant_config = config.get("quantization_config", {})old_extra_config = quant_config.get("extra_config", {})+ # check extra_config only saved attributes differing from Scheme values+ assert "act_bits" not in old_extra_config[".*fc1.*"].keys()+ assert "group_size" not in old_extra_config[".*fc1.*"].keys()quant_config["extra_config"] = new_extra_configconfig["quantization_config"] = quant_configwith open(config_file, "w", encoding="utf-8") as f:diff --git a/test/test_cuda/integrations/test_sglang.py b/test/test_cuda/integrations/test_sglang.pyindex ac96bed74..ddbb3f0f4 100644--- a/test/test_cuda/integrations/test_sglang.py+++ b/test/test_cuda/integrations/test_sglang.py@@ -1,3 +1,4 @@+import jsonimport shutilimport sysfrom pathlib import Path@@ -59,8 +60,8 @@ class TestAutoRound:def test_mixed_ar_format_sglang(self, dataloader):layer_config = {- "self_attn": {"bits": 16, "act_bits": 16},- "lm_head": {"bits": 16, "act_bits": 16},+ "self_attn": {"bits": 8},+ "lm_head": {"bits": 16},"fc1": {"bits": 16, "act_bits": 16},}@@ -78,7 +79,16 @@ class TestAutoRound:inplace=True,format="auto_round",)-+ config_file = Path(self.save_dir) / "config.json"+ with open(config_file, "r", encoding="utf-8") as f:+ config = json.load(f)+ quant_config = config.get("quantization_config", {})+ extra_config = quant_config.get("extra_config", {})+ # check extra_config only saved attributes differing from Scheme values+ assert "act_bits" not in extra_config[".*fc1.*"].keys()+ assert "group_size" not in extra_config[".*fc1.*"].keys()+ assert "bits" in extra_config[".*fc1.*"].keys() and extra_config[".*fc1.*"]["bits"] == 16+ assert "bits" in extra_config[".*self_attn.*"].keys() and extra_config[".*self_attn.*"]["bits"] == 8generated_text = self._run_sglang_inference(self.save_dir)print(generated_text)diff --git a/test/test_cuda/quantization/test_mix_bits.py b/test/test_cuda/quantization/test_mix_bits.pyindex 9daa9b727..eadaf8f53 100644--- a/test/test_cuda/quantization/test_mix_bits.py+++ b/test/test_cuda/quantization/test_mix_bits.py@@ -108,6 +108,18 @@ class TestAutoRound:)quantized_model_path = "self.save_dir"autoround.quantize_and_save(output_dir=quantized_model_path, format="auto_round")+ config_file = Path(quantized_model_path) / "config.json"+ with open(config_file, "r", encoding="utf-8") as f:+ config = json.load(f)+ quant_config = config.get("quantization_config", {})+ extra_config = quant_config.get("extra_config", {})+ # check extra_config only saved attributes differing from Scheme values+ assert "act_bits" not in extra_config[".*fc1.*"].keys() ## TODO refine this assert+ assert "group_size" not in extra_config[".*fc1.*"].keys()+ assert "act_bits" not in extra_config["model.decoder.layers.0.self_attn.k_proj"].keys()+ assert "group_size" not in extra_config["model.decoder.layers.0.self_attn.k_proj"].keys()+ assert "group_size" not in extra_config["model.decoder.layers.1.self_attn.q_proj"].keys()+ assert "bits" in extra_config["model.decoder.layers.1.self_attn.q_proj"].keys()model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="auto")assert model.model.decoder.layers[0].self_attn.k_proj.bits == 8assert model.model.decoder.layers[0].self_attn.q_proj.bits == 3@@ -164,26 +176,10 @@ class TestAutoRound:# remove old extra_config(which contains full name layer configs), only test regex config loadingnew_extra_config = {".*fc1.*": { # standard regex- "act_bits": 16,- "act_data_type": "float",- "act_dynamic": True,- "act_group_size": 128,- "act_sym": True,"bits": 16,- "data_type": "int",- "group_size": 128,- "sym": True,},"k_proj": { # part name- "act_bits": 16,- "act_data_type": "float",- "act_dynamic": True,- "act_group_size": 128,- "act_sym": True,"bits": 8,- "data_type": "int",- "group_size": 128,- "sym": True,},}config_file = Path(quantized_model_path) / "config.json"@@ -191,6 +187,10 @@ class TestAutoRound:config = json.load(f)quant_config = config.get("quantization_config", {})old_extra_config = quant_config.get("extra_config", {})+ # check extra_config only saved attributes differing from Scheme values+ assert "sym" not in old_extra_config[".*fc1.*"].keys()+ assert "act_dynamic" not in old_extra_config[".*fc1.*"].keys()+ assert "group_size" not in old_extra_config[".*fc1.*"].keys()quant_config["extra_config"] = new_extra_configconfig["quantization_config"] = quant_configwith open(config_file, "w", encoding="utf-8") as f: