Skip to content

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

fix memory issue (#193)

wenhuach21e24b9074af6c
diff --git a/auto_round/autoround.py b/auto_round/autoround.py
index 8622be756..227f0685e 100644
--- a/auto_round/autoround.py
+++ b/auto_round/autoround.py
@@ -832,6 +832,7 @@ class AutoRound(object):
best_iter = last_best_iter
with torch.no_grad():
unwrapper_layer(self.model, wrapper_linear, layer_name, best_v, best_min_scale, best_max_scale)
+ layer = mv_module_from_gpu(layer, self.low_cpu_mem_usage)
dump_info = f"quantized {layer_name}, loss iter 0: {init_loss:.6f} -> iter {best_iter}: {last_loss:.6f}"
logger.info(dump_info)
@@ -966,7 +967,8 @@ class AutoRound(object):
with torch.no_grad():
unwrapper_block(block, best_v, best_min_scale, best_max_scale)
if self.enable_quanted_input:
- block = block.to(device)
+ if self.low_cpu_mem_usage:
+ block = block.to(device)
q_outputs = self.get_block_outputs(
block, input_ids, input_others, self.train_bs * self.infer_bs_coeff, device,
cache_device=self.cache_device
@@ -979,6 +981,7 @@ class AutoRound(object):
return q_outputs, output
else:
+ block = mv_module_from_gpu(block, self.low_cpu_mem_usage)
for i in range(len(input_ids)):
input_ids[i] = None
torch.cuda.empty_cache()
@@ -1039,7 +1042,8 @@ class AutoRound(object):
modules = [get_module(model, n) for n in names]
m = WrapperMultiblock(modules)
- m = m.to(device)
+ if not self.model.device.type == "meta" or self.low_cpu_mem_usage:
+ m = m.to(device)
q_input, input_ids = self.quant_block(
m,
@@ -1048,8 +1052,9 @@ class AutoRound(object):
q_input=q_input,
device=device,
)
- self.model = mv_module_from_gpu(self.model, self.low_cpu_mem_usage)
+
torch.cuda.empty_cache()
+ self.model = mv_module_from_gpu(self.model, self.low_cpu_mem_usage)
del q_input
del input_ids
diff --git a/auto_round/low_cpu_mem/utils.py b/auto_round/low_cpu_mem/utils.py
index 26fdcb855..6b02c2a55 100644
--- a/auto_round/low_cpu_mem/utils.py
+++ b/auto_round/low_cpu_mem/utils.py
@@ -388,10 +388,6 @@ def convert_model(empty_model, saved_path=LWQ_WORKSPACE):
elif len(module._modules) == 0:
# skip method type
if len(module._parameters) == 0 or module.weight.device.type != 'meta':
- if hasattr(module, "scale"):
- module.scale = module.scale.to(device_or_dtype)
- if hasattr(module, "zp"):
- module.zp = module.zp.to(device_or_dtype)
return module.ori_to(device_or_dtype)
else:
for n, _ in module.named_parameters():
diff --git a/test/test_low_cpu_mem.py b/test/test_low_cpu_mem.py
index 6f368dc70..49cdfc5c7 100644
--- a/test/test_low_cpu_mem.py
+++ b/test/test_low_cpu_mem.py
@@ -100,6 +100,7 @@ class TestLowCPUMem(unittest.TestCase):
iters=2,
seqlen=2,
dataset=self.llm_dataloader,
+ low_cpu_mem_usage=True
)
autoround.quantize()