PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
fix log bug and device "auto" bug (#409)
Heng Guoba5975c8e6a6
diff --git a/auto_round/autoround.py b/auto_round/autoround.pyindex 945cb0163..2df9befd1 100644--- a/auto_round/autoround.py+++ b/auto_round/autoround.py@@ -268,11 +268,16 @@ class AutoRound(object):logger.warning("mx_fp should only support group_size of 32 in real deployment")if self.nsamples < self.gradient_accumulate_steps * self.batch_size:- self.batch_size = min(self.batch_size, self.nsamples)- self.gradient_accumulate_steps = min(self.nsamples // self.batch_size, self.gradient_accumulate_steps)- logger.warning(- f"reset gradient_accumulate_steps to {self.gradient_accumulate_steps} as nsamples must equal or greater"- " than gradient_accumulate_steps * batch_szie")+ if self.batch_size > self.nsamples:+ logger.warning(f"reset batch_size to {self.nsamples} as n_sample({self.nsamples})"+ f" is smaller than batch_size({self.batch_size})")+ self.batch_size = self.nsamples+ if self.gradient_accumulate_steps > self.nsamples // self.batch_size:+ self.gradient_accumulate_steps = self.nsamples // self.batch_size+ logger.warning(+ f"reset gradient_accumulate_steps to {self.gradient_accumulate_steps}"+ f" as nsamples must equal or greater"+ f" than gradient_accumulate_steps * batch_szie")def quantize(self):"""Quantize the model and return the quantized model along with layer configurations.diff --git a/auto_round/script/llm.py b/auto_round/script/llm.pyindex eee6a1e2f..ba7267a10 100644--- a/auto_round/script/llm.py+++ b/auto_round/script/llm.py@@ -25,6 +25,8 @@# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.+import os+import reimport argparsefrom auto_round.utils import detect_device, get_fp_layer_names@@ -252,9 +254,47 @@ def setup_eval_parser():args = parser.parse_args()return args+def _gguf_args_check(args):+ from auto_round.utils import logger++ _GGUF_CONFIG = {+ "gguf:q4_0": {+ "bits": 4,+ "act_bits": 16,+ "group_size": 32,+ "asym": False,+ },+ "gguf:q4_1": {+ "bits": 4,+ "act_bits": 16,+ "group_size": 32,+ "asym": True,+ }+ }++ formats = args.format.lower().replace(' ', '').split(",")+ for format in _GGUF_CONFIG:+ if format in formats:+ unsupport_list, reset_list = [],[]+ gguf_config = _GGUF_CONFIG[format]+ for k, v in gguf_config.items():+ if getattr(args, k) != v:+ unsupport_list.append(f"{k}={getattr(args, k)}")+ reset_list.append(f"{k}={v}")+ setattr(args, k, v)+ if len(unsupport_list) > 0:+ if len(formats) > 1:+ logger.error(f"format {format} not support for {', '.join(unsupport_list)},"+ f" please reset to {', '.join(reset_list)}, and retry")+ exit(-1)+ else:+ logger.error(f"format {format} not support for {', '.join(unsupport_list)},"+ f" reset to {', '.join(reset_list)}.")+ logger.info(f"export format {format}, sym = {not args.asym}, group_size = {args.group_size}")++ return args+def tune(args):- import re- import torchimport transformersfrom transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel, AutoConfig, AutoProcessor@@ -274,21 +314,8 @@ def tune(args):for format in formats:if format not in supported_formats:raise ValueError(f"{format} is not supported, we only support {supported_formats}")- if format in ["gguf:q4_0", "gguf:q4_1"]:- args.bits = 4- if args.act_bits <= 8:- logger.warning(f"{args.format} not support for activation quantization.")- if args.group_size != 32:- logger.warning(f"{args.format} not support for group_size: {args.group_size}. "- "Reset group_size to 32.")- args.group_size = 32- if args.format.endswith("_0") and args.asym:- logger.warning(f"{args.format} not support for asymmetric quantization, will reset to sym.")- args.asym = False- if args.format.endswith("_1") and not args.asym:- logger.warning(f"{args.format} not support for symmetric quantization, will reset to asym.")- args.asym = True- logger.info(f"export format {format}, sym = {not args.asym}, group_size = {args.group_size}")++ args = _gguf_args_check(args)if "auto_gptq" in args.format and args.asym is True:print(@@ -299,7 +326,6 @@ def tune(args):assert False, "marlin backend only supports sym quantization, please remove --asym"##must set this before import torch- import osdevices = args.device.replace(" ", "").split(',')use_auto_mapping = Falseif all(s.isdigit() for s in devices):@@ -323,9 +349,11 @@ def tune(args):if len(devices) > 1: ##for 70B model on single card, use auto will cause some layer offload to cpuuse_auto_mapping = Trueelif args.device == "auto":- use_auto_mapping == True+ use_auto_mapping = Trueos.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"++ import torchtorch.use_deterministic_algorithms(True, warn_only=True)model_name = args.model@@ -529,7 +557,6 @@ def tune(args):print(make_table(res))def _eval_init(args):- import osdevices = args.device.replace(" ", "").split(',')parallelism = Falsediff --git a/test_cuda/test_support_vlms.py b/test_cuda/test_support_vlms.pyindex 187e85f0a..a2627df3c 100644--- a/test_cuda/test_support_vlms.py+++ b/test_cuda/test_support_vlms.py@@ -145,7 +145,6 @@ class TestSupportVLMS(unittest.TestCase):## test inferfrom transformers import AutoModelForCausalLM, AutoProcessor- from auto_round.export.export_to_awq import WQLinear_GEMMquantized_model_path = os.path.join(self.save_dir, "Phi-3.5-vision-instruct-w4g128-auto_awq")res = os.system(f"cp /models/Phi-3.5-vision-instruct/*.py {quantized_model_path}")model = AutoModelForCausalLM.from_pretrained(