Skip to content

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

fix gguf packing bug and moe regression (#614)

Wenhua Cheng7d18ddbdf87d
diff --git a/auto_round/autoround.py b/auto_round/autoround.py
index b78be16ea..70eb63393 100644
--- a/auto_round/autoround.py
+++ b/auto_round/autoround.py
@@ -804,7 +804,7 @@ class AutoRound(object):
for format_ in self.formats:
if "gguf" in format_ and "k" in format_:
has_gguf_k = True
- if has_gguf_k:
+ if has_gguf_k and not self.disable_opt_rtn:
self.get_imatrix()
self.quantize_embedding_layer()
pbar = tqdm(all_to_quantized_module_names)
@@ -906,6 +906,8 @@ class AutoRound(object):
clear_memory(self.inputs)
all_q_inputs = self.try_cache_inter_data_gpucpu(all_first_block_names, self.nsamples,
layer_names=layer_names)
+ self.model = mv_module_from_gpu(self.model, self.low_cpu_mem_usage)
+ clear_memory()
if hasattr(self.model, "hf_device_map") and len(self.model.hf_device_map) > 1:
accelerate.hooks.remove_hook_from_submodules(self.model) ##self.model.hf_device_map has not been changed
self.model = mv_module_from_gpu(self.model, self.low_cpu_mem_usage)
@@ -996,7 +998,7 @@ class AutoRound(object):
from auto_round.data_type import QUANT_FUNC_WITH_DTYPE
layer = get_module(self.model, layer_name)
- if "rtn_" + layer.data_type in QUANT_FUNC_WITH_DTYPE:
+ if not self.disable_opt_rtn and "rtn_" + layer.data_type in QUANT_FUNC_WITH_DTYPE:
layer.data_type = "rtn_" + layer.data_type
logger.info("using optimized rtn method for quantizing %s", layer_name)
self.layer_config[layer_name]["data_type"] = layer.data_type
@@ -1018,7 +1020,7 @@ class AutoRound(object):
if enable_quanted_input:
logger.info(
- "starting to cache layer inputs for %s as `enable_quanted_input` is enable, this may be quite slow ",
+ "starting to cache layer inputs for %s, this may be quite slow ",
layer_names)
q_layer_inputs = self.try_cache_inter_data_gpucpu([], self.nsamples, layer_names=layer_names)
if hasattr(self.model, "hf_device_map") and len(self.model.hf_device_map) > 1:
@@ -1246,7 +1248,7 @@ class AutoRound(object):
data_new[key] = data[key].to(self.model.device)
input_ids = data_new["input_ids"]
elif isinstance(data, tuple) or isinstance(data, list):
- data_new = data
+ data_new = to_device(data)
input_ids = data_new[0]
else:
data_new = {}
@@ -1261,6 +1263,7 @@ class AutoRound(object):
if isinstance(data_new, torch.Tensor):
self.model(data_new)
elif isinstance(data_new, tuple) or isinstance(data_new, list):
+
self.model(*data_new)
else:
self.model(**data_new)
@@ -1322,16 +1325,16 @@ class AutoRound(object):
all_inputs = self.cache_inter_data(
block_names, nsamples, layer_names=layer_names, last_cache_name=last_cache_name
)
- self.model = mv_module_from_gpu(self.model, self.low_cpu_mem_usage)
- clear_memory()
except RuntimeError as e:
if "CUDA out of memory" in str(e) or "MODULE:PT_DEVMEM" in str(e):
- self.no_enough_vram_for_w_model = True
logger.info("switch to cpu to cache block inputs")
if (self.has_qlayer_outside_block or
self.__class__.__name__ == "AutoRoundMLLM"):
logger.warning("We strongly recommend using more GPUs in calibration."
" Otherwise, some layers may fall back to `rtn` mode, which can affect accuracy.")
+ if hasattr(self.model, "hf_device_map") and len(self.model.hf_device_map) > 1:
+ accelerate.hooks.remove_hook_from_submodules(
+ self.model) ##self.model.hf_device_map has not been changed
self.model = mv_module_from_gpu(self.model, self.low_cpu_mem_usage)
clear_memory()
## Important change after v0.51, on cpu, we use rtn mode for layers in layer_names
diff --git a/auto_round/export/export_to_gguf/packing.py b/auto_round/export/export_to_gguf/packing.py
index a2b25e2d4..e5ec0b54a 100644
--- a/auto_round/export/export_to_gguf/packing.py
+++ b/auto_round/export/export_to_gguf/packing.py
@@ -428,10 +428,12 @@ def q2_k_quant_block(blocks, scale=None, zp=None, wmin_m=None, d_scale=None, d_w
replace_ids = (max_scales > 0).squeeze()
output_scale = torch.zeros_like(scales).to(torch.uint8)
- output_scale[replace_ids] = torch.round(inv_scales * scales).clip(0, 15).to(torch.uint8)
+ output_scale[replace_ids] = torch.round(
+ inv_scales[replace_ids] * scales[replace_ids]).clip(0, 15).to(torch.uint8)
replace_ids = (max_mins > 0).squeeze()
- output_scale[replace_ids] |= torch.round(inv_mins * mins).clip(0, 15).to(torch.uint8) << 4
+ output_scale[replace_ids] |= torch.round(
+ inv_mins[replace_ids] * mins[replace_ids]).clip(0, 15).to(torch.uint8) << 4
if d_scale is None:
output_d = torch.where(max_scales > 0, max_scales / 15, 0)
if d_wmin_m is None:
@@ -568,7 +570,6 @@ def q4_k_quant_block(blocks, scale=None, zp=None, wmin_m=None, d_scale=None, d_w
return np.concatenate([output_d, output_dmin, output_scale, output_qs], axis=-1)
-
@register_qtype("q5_k")
def q5_k_quant_block(blocks, scale=None, zp=None, wmin_m=None, d_scale=None, d_wmin_m=None, **kwargs):
nb = blocks.shape[0]
diff --git a/auto_round/utils.py b/auto_round/utils.py
index a9b085307..40d13e19b 100644
--- a/auto_round/utils.py
+++ b/auto_round/utils.py
@@ -912,7 +912,10 @@ def _clear_memory_for_cpu_and_cuda(tensor=None):
if tensor is not None:
del tensor
gc.collect()
- torch.cuda.empty_cache()
+ if torch.cuda.is_available():
+ torch.cuda.empty_cache()
+ if torch.xpu.is_available():
+ torch.xpu.empty_cache()
def clear_memory(tensor=None):
@@ -1639,13 +1642,17 @@ def get_layer_config_by_gguf_format(layer_config, gguf_format, model):
if lm_head_name is not None and layer_name == lm_head_name and not tie_word_embeddings:
if gguf.MODEL_ARCH.FALCON == model_class.model_arch or input_features % block_size != 0:
new_type = "gguf:q8_0"
+ elif "lm_head" in GGUF_CONFIG[target_gguf_format]:
+ new_type = GGUF_CONFIG[target_gguf_format]["lm_head"]
elif new_type != "gguf:q8_0":
new_type = "gguf:q6_k"
elif lm_head_name is not None and layer_name == lm_head_name and tie_word_embeddings:
pass
elif isinstance(layer, torch.nn.Embedding):
+ if "embedding" in GGUF_CONFIG[target_gguf_format]:
+ new_type = GGUF_CONFIG[target_gguf_format]["embedding"]
+ elif gguf_name is None:
pass
-
# attn_v
elif "attn_v" in gguf_name:
if target_gguf_format == "gguf:q2_k":
@@ -1679,7 +1686,7 @@ def get_layer_config_by_gguf_format(layer_config, gguf_format, model):
if n_expert == 8:
new_type = "gguf:q8_0"
# ffn_down
- if "ffn_down" in gguf_name:
+ elif "ffn_down" in gguf_name:
if target_gguf_format == "gguf:q2_k":
new_type = "gguf:q3_k"
elif target_gguf_format == "gguf:q2_k_s":