Skip to content

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

fixed several issues

wenhuach21c75668b35a6e
diff --git a/auto_round/autoround.py b/auto_round/autoround.py
index b36200855..24e9b0c0e 100644
--- a/auto_round/autoround.py
+++ b/auto_round/autoround.py
@@ -16,11 +16,11 @@
import logging
import torch
-
logger = logging.getLogger("autoround")
logger.setLevel(logging.INFO)
fh = logging.StreamHandler()
-fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(filename)s L%(lineno)d : %(message)s', "%Y-%m-%d %H:%M:%S")
+fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(filename)s L%(lineno)d : %(message)s',
+ "%Y-%m-%d %H:%M:%S")
fh.setFormatter(fh_formatter)
logger.addHandler(fh)
import copy
@@ -45,7 +45,7 @@ def quant_weight_asym(weight, num_bits=4, v=0, min_scale=0, max_scale=0):
Returns:
Quantized and dequantized weight, scale, zero-point
"""
- maxq = torch.tensor(2**num_bits - 1)
+ maxq = torch.tensor(2 ** num_bits - 1)
zeros = torch.zeros(weight.shape[0], device=weight.device)
if isinstance(min_scale, torch.Tensor):
wmin_tmp = torch.minimum(weight.min(1)[0], zeros)
@@ -818,7 +818,7 @@ def get_block_names(model):
for n, m in model.named_modules():
if hasattr(type(m), "__name__") and "ModuleList" in type(m).__name__:
target_m = (n, m)
- break ## only find the first modulelist, may be not robust
+ break ## only find the first modulelist, may be not robust
for n, m in target_m[1].named_children():
block_names.append(target_m[0] + "." + n)
return block_names
@@ -939,37 +939,37 @@ class AutoRound(object):
"""
def __init__(
- self,
- model,
- tokenizer=None,
- bits: int = 4,
- group_size: int = 128,
- scheme: str = "asym",
- weight_config: dict = {},
- enable_full_range: bool = False, ##for symmetric, TODO support later
- bs: int = 8,
- amp: bool = True,
- device="cuda:0",
- lr_scheduler=None,
- dataloader=None, ## to support later
- dataset_name: str = "NeelNanda/pile-10k",
- dataset_split: str = "train",
- use_quant_input: bool = True,
- enable_minmax_tuning: bool = True,
- lr: float = 0.005,
- minmax_lr: float = None,
- low_gpu_mem_usage: bool = True,
- iters: int = 200,
- seqlen: int = 2048,
- n_samples: int = 512,
- sampler: str = "rand",
- seed: int = 42,
- n_blocks: int = 1,
- gradient_accumulate_steps: int = 1,
- not_use_mse: bool = False,
- dynamic_max_gap: int = -1,
- data_type: str = "int", ##only support data_type
- **kwargs,
+ self,
+ model,
+ tokenizer=None,
+ bits: int = 4,
+ group_size: int = 128,
+ scheme: str = "asym",
+ weight_config: dict = {},
+ enable_full_range: bool = False, ##for symmetric, TODO support later
+ bs: int = 8,
+ amp: bool = True,
+ device="cuda:0",
+ lr_scheduler=None,
+ dataloader=None, ## to support later
+ dataset_name: str = "NeelNanda/pile-10k",
+ dataset_split: str = "train",
+ use_quant_input: bool = True,
+ enable_minmax_tuning: bool = True,
+ lr: float = 0.005,
+ minmax_lr: float = None,
+ low_gpu_mem_usage: bool = True,
+ iters: int = 200,
+ seqlen: int = 2048,
+ n_samples: int = 512,
+ sampler: str = "rand",
+ seed: int = 42,
+ n_blocks: int = 1,
+ gradient_accumulate_steps: int = 1,
+ not_use_mse: bool = False,
+ dynamic_max_gap: int = -1,
+ data_type: str = "int", ##only support data_type
+ **kwargs,
):
self.model = model
self.model = self.model.to("cpu")
@@ -1165,7 +1165,8 @@ class AutoRound(object):
output = []
for i in range(0, self.n_samples, bs):
- indices = torch.arange(i, i + bs).to(torch.long)
+ end_index = min(self.n_samples, i + bs)
+ indices = torch.arange(i, end_index).to(torch.long)
tmp_input_ids, tmp_input_others = sampling_inputs(input_ids, input_others, indices, self.seqlen)
tmp_output = block_forward(block, tmp_input_ids, tmp_input_others, self.amp, self.amp_dtype, device).to(
cache_device
@@ -1297,12 +1298,12 @@ class AutoRound(object):
return None, output
def q_dq_weight_round(
- self,
- model: torch.nn.Module,
- inputs,
- block_names,
- n_blocks=1,
- device=torch.device("cpu"),
+ self,
+ model: torch.nn.Module,
+ inputs,
+ block_names,
+ n_blocks=1,
+ device=torch.device("cpu"),
):
"""Quantize and dequantize the weights of the specified blocks in the model.
@@ -1330,7 +1331,7 @@ class AutoRound(object):
logger.info(n)
m = get_module(model, n)
else:
- names = block_names[i : i + n_blocks]
+ names = block_names[i: i + n_blocks]
logger.info(names)
modules = [get_module(model, n) for n in names]
m = WrapperMultiblock(modules)
@@ -1451,38 +1452,38 @@ class AutoOPTRound(AutoRound):
"""
def __init__(
- self,
- model,
- tokenizer=None,
- bits: int = 4,
- group_size: int = 128,
- scheme: str = "asym",
- weight_config: dict = {},
- enable_full_range: bool = False,
- bs: int = 8,
- amp: bool = True,
- device="cuda:0",
- lr_scheduler=None,
- dataloader=None,
- dataset_name: str = "NeelNanda/pile-10k",
- dataset_split: str = "train",
- use_quant_input: bool = True,
- enable_minmax_tuning: bool = True,
- lr: float = 0.005,
- minmax_lr: float = None,
- low_gpu_mem_usage: bool = True,
- iters: int = 200,
- seqlen: int = 2048,
- n_samples: int = 512,
- sampler: str = "rand",
- seed: int = 42,
- n_blocks: int = 1,
- gradient_accumulate_steps: int = 1,
- not_use_mse: bool = False,
- dynamic_max_gap: int = -1,
- data_type: str = "int",
- optimizer="AdamW",
- **kwargs,
+ self,
+ model,
+ tokenizer=None,
+ bits: int = 4,
+ group_size: int = 128,
+ scheme: str = "asym",
+ weight_config: dict = {},
+ enable_full_range: bool = False,
+ bs: int = 8,
+ amp: bool = True,
+ device="cuda:0",
+ lr_scheduler=None,
+ dataloader=None,
+ dataset_name: str = "NeelNanda/pile-10k",
+ dataset_split: str = "train",
+ use_quant_input: bool = True,
+ enable_minmax_tuning: bool = True,
+ lr: float = 0.005,
+ minmax_lr: float = None,
+ low_gpu_mem_usage: bool = True,
+ iters: int = 200,
+ seqlen: int = 2048,
+ n_samples: int = 512,
+ sampler: str = "rand",
+ seed: int = 42,
+ n_blocks: int = 1,
+ gradient_accumulate_steps: int = 1,
+ not_use_mse: bool = False,
+ dynamic_max_gap: int = -1,
+ data_type: str = "int",
+ optimizer="AdamW",
+ **kwargs,
):
super(AutoOPTRound, self).__init__(
model,
@@ -1599,38 +1600,38 @@ class AutoAdamRound(AutoOPTRound):
"""
def __init__(
- self,
- model,
- tokenizer=None,
- bits: int = 4,
- group_size: int = 128,
- scheme: str = "asym",
- weight_config: dict = {},
- enable_full_range: bool = False,
- bs: int = 8,
- amp: bool = True,
- device="cuda:0",
- lr_scheduler=None,
- dataloader=None,
- dataset_name: str = "NeelNanda/pile-10k",
- dataset_split: str = "train",
- use_quant_input: bool = True,
- enable_minmax_tuning: bool = True,
- lr: float = 0.01,
- minmax_lr: float = None,
- low_gpu_mem_usage: bool = True,
- iters: int = 200,
- seqlen: int = 2048,
- n_samples: int = 512,
- sampler: str = "rand",
- seed: int = 42,
- n_blocks: int = 1,
- gradient_accumulate_steps: int = 1,
- not_use_mse: bool = False,
- dynamic_max_gap: int = -1,
- data_type: str = "int",
- optimizer="AdamW",
- **kwargs,
+ self,
+ model,
+ tokenizer=None,
+ bits: int = 4,
+ group_size: int = 128,
+ scheme: str = "asym",
+ weight_config: dict = {},
+ enable_full_range: bool = False,
+ bs: int = 8,
+ amp: bool = True,
+ device="cuda:0",
+ lr_scheduler=None,
+ dataloader=None,
+ dataset_name: str = "NeelNanda/pile-10k",
+ dataset_split: str = "train",
+ use_quant_input: bool = True,
+ enable_minmax_tuning: bool = True,
+ lr: float = 0.01,
+ minmax_lr: float = None,
+ low_gpu_mem_usage: bool = True,
+ iters: int = 200,
+ seqlen: int = 2048,
+ n_samples: int = 512,
+ sampler: str = "rand",
+ seed: int = 42,
+ n_blocks: int = 1,
+ gradient_accumulate_steps: int = 1,
+ not_use_mse: bool = False,
+ dynamic_max_gap: int = -1,
+ data_type: str = "int",
+ optimizer="AdamW",
+ **kwargs,
):
super(AutoAdamRound, self).__init__(
model,
diff --git a/examples/eval/eval.py b/examples/eval/evaluation.py
similarity index 98%
rename from examples/eval/eval.py
rename to examples/eval/evaluation.py
index f68cabc3f..a9e46ae55 100644
--- a/examples/eval/eval.py
+++ b/examples/eval/evaluation.py
@@ -3,7 +3,7 @@ import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import os.path
import torch
-from .parse_results import result_parser
+from eval.parse_results import result_parser
import pprint
import re
import shutil
@@ -283,7 +283,7 @@ def eval_model(output_dir=None, model=None, tokenizer=None,
# model.seqlen = seqlen
model.seqlen = 2048
- from utils import get_loaders, eval_ppl_same_with_gptq
+ from eval.utils import get_loaders, eval_ppl_same_with_gptq
for dataset in external_tasks:
try:
dataloader, testloader = get_loaders(
@@ -355,8 +355,6 @@ if __name__ == "__main__":
test_tasks = ['wikitext2', 'ptb-new', 'c4-new', 'lambada_openai', 'hellaswag', 'winogrande', 'piqa',
"hendrycksTest-*", "wikitext", "truthfulqa_mc", "openbookqa", "boolq", "rte", "arc_easy",
"arc_challenge"]
- test_tasks = ['wikitext2', 'ptb-new', 'c4-new', 'lambada_openai', 'hellaswag', 'winogrande', 'piqa',
- ]
excel_name = (args.model_name).split('/')[-1] + ".xlsx"
eval_model(output_dir=args.model_name,
tasks=test_tasks,
diff --git a/examples/main.py b/examples/main.py
index 905dac285..f5dcff857 100644
--- a/examples/main.py
+++ b/examples/main.py
@@ -13,7 +13,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel
from transformers import set_seed
-from eval.eval import eval_model
+from eval.evaluation import eval_model
import re
@@ -88,18 +88,18 @@ if __name__ == '__main__':
# parser.add_argument("--tasks", default=["lambada_openai", "hellaswag", "winogrande", "piqa"],
# help="lm-eval tasks")
- # parser.add_argument("--tasks", default=["lambada_openai"],
- # help="lm-eval tasks")
+ parser.add_argument("--tasks", default=["lambada_openai","wikitext2",'hendrycksTest-*'],
+ help="lm-eval tasks")
# parser.add_argument("--tasks",
# default=['wikitext2', 'ptb-new', 'c4-new', 'lambada_openai', 'hellaswag', 'winogrande', 'piqa',
# 'coqa', 'truthfulqa_mc', 'openbookqa', 'boolq', 'rte', 'arc_easy', 'arc_challenge',
# 'hendrycksTest-*', 'wikitext', 'drop', 'gsm8k'],##all
- parser.add_argument("--tasks",
- default=['wikitext2', 'ptb-new', 'c4-new', 'lambada_openai', 'hellaswag', 'winogrande', 'piqa',
- "hendrycksTest-*", "wikitext", "truthfulqa_mc", "openbookqa", "boolq", "rte",
- "arc_easy", "arc_challenge"],
- help="lm-eval tasks") # "truthfulqa_gen"
+ # parser.add_argument("--tasks",
+ # default=['wikitext2', 'ptb-new', 'c4-new', 'lambada_openai', 'hellaswag', 'winogrande', 'piqa',
+ # "hendrycksTest-*", "wikitext", "truthfulqa_mc", "openbookqa", "boolq", "rte",
+ # "arc_easy", "arc_challenge"],
+ # help="lm-eval tasks") # "truthfulqa_gen"
# parser.add_argument("--tasks", default=["lambada_openai"],
# help="lm-eval tasks")