Skip to content

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

fix: preserve calibration dataset on memory errors (#2433)

Shubham Padkondefde1f580b5e1
diff --git a/auto_round/auto_scheme/delta_loss.py b/auto_round/auto_scheme/delta_loss.py
index c5fbfe232..dc500b63f 100644
--- a/auto_round/auto_scheme/delta_loss.py
+++ b/auto_round/auto_scheme/delta_loss.py
@@ -1446,7 +1446,12 @@ def get_score_for_scheme(
batch_checkpoint=batch_checkpoint,
)
except Exception as exc: # noqa: BLE001
- if not is_vlm:
+ # Memory exhaustion does not indicate incompatible text inputs.
+ if (
+ not is_vlm
+ or isinstance(exc, (torch.OutOfMemoryError, MemoryError))
+ or (isinstance(exc, RuntimeError) and "out of memory" in str(exc).lower())
+ ):
raise
logger.warning(
f"Text-only calibration failed on VLM ({exc}); "
@@ -1546,7 +1551,12 @@ def get_score_for_scheme(
try:
_run_forward_loop(_build_calib_dataloader())
except Exception as exc: # noqa: BLE001
- if not is_vlm:
+ # Memory exhaustion does not indicate incompatible text inputs.
+ if (
+ not is_vlm
+ or isinstance(exc, (torch.OutOfMemoryError, MemoryError))
+ or (isinstance(exc, RuntimeError) and "out of memory" in str(exc).lower())
+ ):
raise
logger.warning(
f"Text-only calibration failed on VLM ({exc}); "
diff --git a/test/unit/common/schemes/test_calibration_oom.py b/test/unit/common/schemes/test_calibration_oom.py
new file mode 100644
index 000000000..1cf3e599c
--- /dev/null
+++ b/test/unit/common/schemes/test_calibration_oom.py
@@ -0,0 +1,79 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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.
+"""Calibration memory failures must not change the requested dataset."""
+
+import pytest
+import torch
+
+from auto_round.auto_scheme import delta_loss
+from auto_round.compressors.mllm import dataset as mllm_dataset
+
+
[email protected]("low_gpu_mem_usage", [False, True])
+ "error_type, message, should_fallback",
+ [
+ (torch.OutOfMemoryError, "calibration failed", False),
+ (MemoryError, "calibration failed", False),
+ (RuntimeError, "CUDA out of memory", False),
+ (RuntimeError, "Backend OUT OF MEMORY", False),
+ (RuntimeError, "unsupported text inputs", True),
+ (ValueError, "unsupported text inputs", True),
+ ],
+)
+def test_calibration_memory_errors_do_not_change_dataset(
+ monkeypatch, low_gpu_mem_usage, error_type, message, should_fallback
+):
+ model = torch.nn.Module()
+ text_loader = [torch.ones(1, 2, dtype=torch.long)]
+ fallback_calls = []
+ error = error_type(message)
+
+ monkeypatch.setattr(delta_loss, "get_block_names", lambda *args, **kwargs: [[]])
+ monkeypatch.setattr(delta_loss, "get_dataloader", lambda *args, **kwargs: text_loader)
+
+ def fail_forward(*args, **kwargs):
+ raise error
+
+ def multimodal_loader(**kwargs):
+ fallback_calls.append(kwargs["dataset"])
+ return [], None, None, None
+
+ # Exercise both real calibration branches without allocating GPU memory.
+ def low_gpu_forward(model, loader, **kwargs):
+ if loader is text_loader:
+ raise error
+
+ monkeypatch.setattr(delta_loss, "model_forward", fail_forward)
+ monkeypatch.setattr(delta_loss, "model_forward_low_gpu", low_gpu_forward)
+ monkeypatch.setattr(mllm_dataset, "get_mllm_dataloader", multimodal_loader)
+ kwargs = dict(
+ model=model,
+ tokenizer=None,
+ quant_layer_names=[],
+ fixed_layer_scheme={},
+ dataset="NeelNanda/pile-10k",
+ processor=object(),
+ is_vlm=True,
+ low_gpu_mem_usage=low_gpu_mem_usage,
+ )
+ if should_fallback:
+ # Preserve the fallback for models that reject text-only inputs.
+ assert delta_loss.get_score_for_scheme(**kwargs) == {}
+ assert fallback_calls == ["liuhaotian/llava_conv_58k"]
+ else:
+ with pytest.raises(error_type) as caught:
+ delta_loss.get_score_for_scheme(**kwargs)
+ assert caught.value is error
+ assert fallback_calls == []