Skip to content

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

fix gradient_accumulate_steps cli issue (#2041)

Weiweid357f0d1c1b6
diff --git a/auto_round/algorithms/quantization/base.py b/auto_round/algorithms/quantization/base.py
index 7fba0ca2c..14096213e 100644
--- a/auto_round/algorithms/quantization/base.py
+++ b/auto_round/algorithms/quantization/base.py
@@ -271,6 +271,14 @@ class BaseQuantizer(BasePipelineMember):
def batch_size(self, value: int) -> None:
self._calibration_state.batch_size = value
+ @property
+ def gradient_accumulate_steps(self) -> int:
+ return self._calibration_state.gradient_accumulate_steps
+
+ @gradient_accumulate_steps.setter
+ def gradient_accumulate_steps(self, value: int) -> None:
+ self._calibration_state.gradient_accumulate_steps = value
+
@property
def nsamples(self) -> int:
return self._calibration_state.nsamples
diff --git a/auto_round/cli/main.py b/auto_round/cli/main.py
index 82144efa9..aa066dce0 100644
--- a/auto_round/cli/main.py
+++ b/auto_round/cli/main.py
@@ -56,6 +56,7 @@ def _build_entry_base_kwargs(args, *, low_cpu_mem_usage, enable_torch_compile, l
"seqlen": args.seqlen,
"nsamples": args.nsamples,
"batch_size": args.batch_size,
+ "gradient_accumulate_steps": getattr(args, "gradient_accumulate_steps", 1),
"low_gpu_mem_usage": args.low_gpu_mem_usage,
"low_cpu_mem_usage": low_cpu_mem_usage,
"device_map": args.device_map,
diff --git a/test/test_cpu/core/test_init.py b/test/test_cpu/core/test_init.py
index 01785d679..e20f63b0d 100644
--- a/test/test_cpu/core/test_init.py
+++ b/test/test_cpu/core/test_init.py
@@ -1,8 +1,53 @@
+import argparse
+
from auto_round import AutoRound
+from auto_round.compressors.entry import AutoRound as NewAutoRound
-def test_torch_compile(tiny_opt_model_path):
+def test_argparse_check(tiny_opt_model_path):
ar = AutoRound(model=tiny_opt_model_path, scheme="NVFP4", enable_torch_compile=True)
assert not ar.enable_torch_compile, "NVFP4 cannot work with torch.compile."
ar = AutoRound(model=tiny_opt_model_path, scheme="FP8_STATIC", enable_torch_compile=True)
assert not ar.enable_torch_compile, "FP8_STATIC cannot work with torch.compile."
+
+ # Regression for issue #2034: gradient_accumulate_steps must flow from the CLI
+ # args all the way to the quantizer. Previously the CLI path dropped the flag,
+ # so CalibrationState defaulted to 1 regardless of the user's value.
+ steps = 8
+ from auto_round.cli.main import _build_entry_base_kwargs
+
+ args = argparse.Namespace(
+ platform="hf",
+ format="auto_round",
+ dataset="NeelNanda/pile-10k",
+ seqlen=2048,
+ nsamples=128,
+ batch_size=8,
+ gradient_accumulate_steps=steps,
+ low_gpu_mem_usage=False,
+ device_map="0",
+ seed=42,
+ layer_config=None,
+ model_dtype=None,
+ disable_trust_remote_code=False,
+ )
+ cli_kwargs = _build_entry_base_kwargs(args, low_cpu_mem_usage=True, enable_torch_compile=False, layer_config=None)
+ assert cli_kwargs.get("gradient_accumulate_steps") == steps, (
+ "_build_entry_base_kwargs must forward gradient_accumulate_steps so that "
+ "CalibrationState is seeded with the CLI value, not the default 1."
+ )
+
+ ar = NewAutoRound(
+ tiny_opt_model_path,
+ scheme="W4A16",
+ gradient_accumulate_steps=steps,
+ iters=1,
+ nsamples=1,
+ seqlen=8,
+ low_cpu_mem_usage=False,
+ )
+ ar.post_init() # triggers _build_quantizer() → bind()
+ assert ar.gradient_accumulate_steps == steps
+ assert ar.quantizer.gradient_accumulate_steps == steps
+ # Compressor and quantizer must share exactly the same CalibrationState instance.
+ assert ar.quantizer._calibration_state is ar._calibration_state