PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
fix bug of format (#1287)
Heng Guo5ba31b6c4df4
diff --git a/auto_round/compressors/base.py b/auto_round/compressors/base.pyindex accbc946b..4bba244ed 100644--- a/auto_round/compressors/base.py+++ b/auto_round/compressors/base.py@@ -877,7 +877,9 @@ class BaseCompressor(object):else:model, _ = self.quantize()# Save the quantized model in the specified format_list- model, folders = self.save_quantized(output_dir, format=format, inplace=inplace, return_folders=True, **kwargs)+ model, folders = self.save_quantized(+ output_dir, format=self.formats, inplace=inplace, return_folders=True, **kwargs+ )memory_monitor.log_summary()return model, folders@@ -2968,7 +2970,7 @@ class BaseCompressor(object):object: The compressed model object."""self.orig_output_dir = output_dir- if isinstance(format, str):+ if isinstance(format, str) and getattr(self, "formats", None) is None:formats = get_formats(format, self)if not hasattr(self, "formats"):self.formats = formats@@ -2977,7 +2979,7 @@ class BaseCompressor(object):logger.warning("please run autoround.quantize first")returnfolders = []- for format in formats:+ for format in self.formats:save_folder = self._get_save_folder_name(format)if self.act_bits <= 8 and format.is_fake():logger.warning(diff --git a/test/test_cpu/core/test_autoround.py b/test/test_cpu/core/test_autoround.pyindex 4df10efe6..3842cd14a 100644--- a/test/test_cpu/core/test_autoround.py+++ b/test/test_cpu/core/test_autoround.py@@ -47,7 +47,7 @@ class TestAutoRound:amp=False,)autoround.quantize_and_save(self.save_folder, inplace=False, format="fake")- shutil.rmtree(self.save_folder)+ shutil.rmtree(self.save_folder, ignore_errors=True)def test_remove_whole_block(self, tiny_opt_model_path, dataloader):model_name = tiny_opt_model_path@@ -386,7 +386,7 @@ class TestAutoRound:tokenizer = AutoTokenizer.from_pretrained(self.save_folder)model_infer(model, tokenizer)- shutil.rmtree(self.save_folder)+ shutil.rmtree(self.save_folder, ignore_errors=True)def test_embed_quant(self, tiny_opt_model_path, dataloader):bits, group_size, sym = 4, 128, Truediff --git a/test/test_cpu/export/test_gguf_format.py b/test/test_cpu/export/test_gguf_format.pyindex 4ba59629e..6f736eb98 100644--- a/test/test_cpu/export/test_gguf_format.py+++ b/test/test_cpu/export/test_gguf_format.py@@ -29,7 +29,7 @@ class TestGGUF:def test_basic_usage(self, tiny_gemma_model_path, tiny_qwen_model_path):python_path = sys.executableres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model {tiny_gemma_model_path} "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model {tiny_gemma_model_path} "f" --bs 16 --iters 0 --nsamples 1 --format gguf:q4_k_m")if res > 0 or res == -1:@@ -37,7 +37,7 @@ class TestGGUF:shutil.rmtree("./saved", ignore_errors=True)res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model {tiny_qwen_model_path}"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model {tiny_qwen_model_path}"f" --bs 16 --iters 1 --nsamples 1 --format fake,gguf:q4_0")if res > 0 or res == -1:@@ -165,7 +165,7 @@ class TestGGUF:# for gguf_format in ["gguf:q4_0", "gguf:q4_1", "gguf:q4_k_m", "gguf:q6_k"]:for gguf_format in ["gguf:q4_k_m"]:res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model {model_name} "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model {model_name} "f" --bs 16 --iters 1 --nsamples 1 --seqlen 16 --format {gguf_format}")if res > 0 or res == -1:@@ -173,7 +173,7 @@ class TestGGUF:shutil.rmtree("../../tmp_autoround", ignore_errors=True)res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model {model_name}"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model {model_name}"f" --bs 16 --iters 0 --nsamples 1 --seqlen 16 --format fake,{gguf_format}")if res > 0 or res == -1:@@ -182,7 +182,7 @@ class TestGGUF:# test mixed q2_k_sres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model {model_name}"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model {model_name}"f" --bs 16 --iters 0 --nsamples 1 --seqlen 16 --scheme GGUF:Q2_K_MIXED")if res > 0 or res == -1:diff --git a/test/test_cpu/quantization/test_asym.py b/test/test_cpu/quantization/test_asym.pyindex 587de1b9c..525daa09e 100644--- a/test/test_cpu/quantization/test_asym.py+++ b/test/test_cpu/quantization/test_asym.py@@ -55,7 +55,7 @@ class TestAutoRoundAsym(unittest.TestCase):# tokenizer = AutoTokenizer.from_pretrained(self.save_folder)# model_infer(model, tokenizer)- shutil.rmtree(self.save_folder)+ shutil.rmtree(self.save_folder, ignore_errors=True)def test_asym_bits(self):model_name = self.model_name@@ -76,7 +76,7 @@ class TestAutoRoundAsym(unittest.TestCase):# tokenizer = AutoTokenizer.from_pretrained(self.save_folder)# model_infer(model, tokenizer)- shutil.rmtree(self.save_folder)+ shutil.rmtree(self.save_folder, ignore_errors=True)# use parameters laterdef test_asym_format(self):@@ -97,4 +97,4 @@ class TestAutoRoundAsym(unittest.TestCase):# tokenizer = AutoTokenizer.from_pretrained(self.save_folder)# model_infer(model, tokenizer)- shutil.rmtree(self.save_folder)+ shutil.rmtree(self.save_folder, ignore_errors=True)diff --git a/test/test_cpu/utils/test_cli_usage.py b/test/test_cpu/utils/test_cli_usage.pyindex 6ba676936..f57e9c102 100644--- a/test/test_cpu/utils/test_cli_usage.py+++ b/test/test_cpu/utils/test_cli_usage.py@@ -25,24 +25,24 @@ class TestAutoRoundCmd:python_path = sys.executable# Test llm script- res = os.system(f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round -h")+ res = os.system(f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round -h")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model {tiny_opt_model_path} --seqlen 32 --iter 2 --nsamples 1 --format auto_gptq,auto_round --output_dir ./saved --tasks piqa"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model {tiny_opt_model_path} --seqlen 32 --iter 2 --nsamples 1 --format auto_gptq,auto_round --output_dir ./saved --tasks piqa")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model {tiny_opt_model_path} --seqlen 8 --iter 1 --nsamples 1 --eval_task_by_task --tasks openbookqa --bs 32"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model {tiny_opt_model_path} --seqlen 8 --iter 1 --nsamples 1 --eval_task_by_task --tasks openbookqa --bs 32")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -c 'from auto_round.__main__ import run_light; run_light()' --seqlen 8 --iter 2 --nsamples 8 --output_dir ./saved --tasks lambada_openai"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -c 'from auto_round.__main__ import run_light; run_light()' --seqlen 8 --iter 2 --nsamples 8 --output_dir ./saved --tasks lambada_openai")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"@@ -50,23 +50,23 @@ class TestAutoRoundCmd:# test mllm script# test auto_round_mllm --eval help- res = os.system(f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --eval -h")+ res = os.system(f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --eval -h")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"# test auto_round_mllm --lmms help- res = os.system(f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --eval --lmms -h")+ res = os.system(f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --eval --lmms -h")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --mllm --model {tiny_qwen_vl_model_path} --iter 2 --nsamples 2 --seqlen 32 --format auto_round --output_dir ./saved"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --mllm --model {tiny_qwen_vl_model_path} --iter 2 --nsamples 2 --seqlen 32 --format auto_round --output_dir ./saved")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --mllm --iter 2 --nsamples 2 --model {tiny_qwen_vl_model_path} --seqlen 32 --format auto_round"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --mllm --iter 2 --nsamples 2 --model {tiny_qwen_vl_model_path} --seqlen 32 --format auto_round"" --quant_nontext_module --output_dir ./saved ")if res > 0 or res == -1:diff --git a/test/test_cuda/advanced/test_multiple_card_calib.py b/test/test_cuda/advanced/test_multiple_card_calib.pyindex 06c869a86..8eef8b981 100644--- a/test/test_cuda/advanced/test_multiple_card_calib.py+++ b/test/test_cuda/advanced/test_multiple_card_calib.py@@ -44,7 +44,7 @@ class TestAutoRound:##test llm scriptres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model /models/Meta-Llama-3.1-8B-Instruct --devices '0,1' --quant_lm_head --iters 1 --nsamples 1 --output_dir None"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model /models/Meta-Llama-3.1-8B-Instruct --devices '0,1' --quant_lm_head --iters 1 --nsamples 1 --output_dir None")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"@@ -55,7 +55,7 @@ class TestAutoRound:##test llm scriptres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {python_path} -m auto_round --model facebook/opt-125m --scheme NVFP4 --devices '0,1' --iters 1 --nsamples 1 --enable_torch_compile --low_gpu_mem_usage"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {python_path} -m auto_round --model facebook/opt-125m --scheme NVFP4 --devices '0,1' --iters 1 --nsamples 1 --enable_torch_compile --low_gpu_mem_usage")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"diff --git a/test/test_cuda/export/test_gguf.py b/test/test_cuda/export/test_gguf.pyindex a6089a086..454a2c6ce 100644--- a/test/test_cuda/export/test_gguf.py+++ b/test/test_cuda/export/test_gguf.py@@ -59,7 +59,7 @@ class TestAutoRound:save_dir = os.path.join(os.path.dirname(__file__), "saved")res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {sys.executable} -m auto_round --model {tiny_qwen_model_path} --iter 2 "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {sys.executable} -m auto_round --model {tiny_qwen_model_path} --iter 2 "f"--output_dir {save_dir} --nsample 2 --format gguf:q4_0 --device 0")print(save_dir)diff --git a/test/test_cuda/models/test_support_vlms.py b/test/test_cuda/models/test_support_vlms.pyindex 4f9373ca5..a339ba69a 100644--- a/test/test_cuda/models/test_support_vlms.py+++ b/test/test_cuda/models/test_support_vlms.py@@ -30,7 +30,7 @@ class TestSupportVLMS:model_path = "/models/Qwen2-VL-2B-Instruct/"# test tuneres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {self.python_path} -m auto_round --mllm "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {self.python_path} -m auto_round --mllm "f"--model {model_path} --iter 2 --output_dir {self.save_dir} --device {self.device}")assert not (res > 0 or res == -1), "qwen2 tuning fail"@@ -85,7 +85,7 @@ class TestSupportVLMS:model_path = "/models/Phi-3.5-vision-instruct/"## test tuneres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {self.python_path} -m auto_round --mllm "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {self.python_path} -m auto_round --mllm "f"--model {model_path} --iter 2 --output_dir {self.save_dir} --device {self.device}")assert not (res > 0 or res == -1), "Phi-3.5 tuning fail"@@ -133,7 +133,7 @@ class TestSupportVLMS:model_path = "/models/Phi-3.5-vision-instruct/"## test tuneres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {self.python_path} -m auto_round --mllm "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {self.python_path} -m auto_round --mllm "f"--model {model_path} --iter 2 --quant_nontext_module "f"--nsample 64 --seqlen 32 "f"--format auto_awq --output_dir {self.save_dir} --device {self.device}"@@ -181,7 +181,7 @@ class TestSupportVLMS:model_path = "/models/glm-4v-9b/"## test tuneres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {self.python_path} -m auto_round "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {self.python_path} -m auto_round "f"--model {model_path} --iter 1 --output_dir {self.save_dir} --device {self.device}")assert not (res > 0 or res == -1), "glm-4v-9b tuning fail"@@ -190,7 +190,7 @@ class TestSupportVLMS:model_path = "/models/granite-vision-3.2-2b"## test tuneres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' {self.python_path} -m auto_round "+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' {self.python_path} -m auto_round "f"--model {model_path} --iter 1 --output_dir {self.save_dir} --device {self.device}")assert not (res > 0 or res == -1), "granite-vision-3.2-2b tuning fail"diff --git a/test/test_cuda/quantization/test_asym.py b/test/test_cuda/quantization/test_asym.pyindex 488ef8a73..d55934898 100644--- a/test/test_cuda/quantization/test_asym.py+++ b/test/test_cuda/quantization/test_asym.py@@ -57,7 +57,7 @@ class TestAutoRoundAsym:# tokenizer = AutoTokenizer.from_pretrained(self.save_dir)# model_infer(model, tokenizer)- shutil.rmtree(self.save_dir)+ shutil.rmtree(self.save_dir, ignore_errors=True)def test_asym_bits(self, tiny_opt_model_path):for bits in [2, 3, 8]:@@ -77,7 +77,7 @@ class TestAutoRoundAsym:# tokenizer = AutoTokenizer.from_pretrained(self.save_dir)# model_infer(model, tokenizer)- shutil.rmtree(self.save_dir)+ shutil.rmtree(self.save_dir, ignore_errors=True)# use parameters laterdef test_asym_format(self, tiny_opt_model_path):@@ -97,7 +97,7 @@ class TestAutoRoundAsym:# tokenizer = AutoTokenizer.from_pretrained(self.save_dir)# model_infer(model, tokenizer)- shutil.rmtree(self.save_dir)+ shutil.rmtree(self.save_dir, ignore_errors=True)def test_asym_group_size_with_tuning(self, tiny_opt_model_path):for group_size in [32, 64, 128]:@@ -117,7 +117,7 @@ class TestAutoRoundAsym:# tokenizer = AutoTokenizer.from_pretrained(self.save_dir)# model_infer(model, tokenizer)- shutil.rmtree(self.save_dir)+ shutil.rmtree(self.save_dir, ignore_errors=True)def test_asym_bits_with_tuning(self, tiny_opt_model_path):for bits in [2, 3, 8]:@@ -137,7 +137,7 @@ class TestAutoRoundAsym:# tokenizer = AutoTokenizer.from_pretrained(self.save_dir)# model_infer(model, tokenizer)- shutil.rmtree(self.save_dir)+ shutil.rmtree(self.save_dir, ignore_errors=True)# use parameters laterdef test_asym_format_with_tuning(self, tiny_opt_model_path):@@ -157,4 +157,4 @@ class TestAutoRoundAsym:# tokenizer = AutoTokenizer.from_pretrained(self.save_dir)# model_infer(model, tokenizer)- shutil.rmtree(self.save_dir)+ shutil.rmtree(self.save_dir, ignore_errors=True)diff --git a/test/test_cuda/utils/test_alg_ext.py b/test/test_cuda/utils/test_alg_ext.pyindex d2e8e4bfb..16f42bda5 100644--- a/test/test_cuda/utils/test_alg_ext.py+++ b/test/test_cuda/utils/test_alg_ext.py@@ -52,13 +52,13 @@ class TestAlgExt:python_path = sys.executableres = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' CUDA_VISIBLE_DEVICES=0 {python_path} -m auto_round --model {tiny_opt_model_path} --iters 1 --device auto --enable_alg_ext --avg_bits 2 --options=W2A16,W4A16 --ignore_scale_zp_bits --nsamples 1 --seqlen 32"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' CUDA_VISIBLE_DEVICES=0 {python_path} -m auto_round --model {tiny_opt_model_path} --iters 1 --device auto --enable_alg_ext --avg_bits 2 --options=W2A16,W4A16 --ignore_scale_zp_bits --nsamples 1 --seqlen 32")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"res = os.system(- f"PYTHONPATH='AUTO_ROUND_PATH:$PYTHONPATH' CUDA_VISIBLE_DEVICES=0 {python_path} -m auto_round --model {tiny_opt_model_path} --iters 1 --device auto --enable_alg_ext --avg_bits 5.5 --options=mxfp4,mxfp8 --ignore_scale_zp_bits --enable_torch_compile --nsamples 1 --seqlen 32"+ f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' CUDA_VISIBLE_DEVICES=0 {python_path} -m auto_round --model {tiny_opt_model_path} --iters 1 --device auto --enable_alg_ext --avg_bits 5.5 --options=mxfp4,mxfp8 --ignore_scale_zp_bits --enable_torch_compile --nsamples 1 --seqlen 32")if res > 0 or res == -1:assert False, "cmd line test fail, please have a check"