Skip to content

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

Honour the padding mask in SignRoundV2's plain loss (#2395)

Shubham Padkonde08832178f3f2
diff --git a/auto_round/algorithms/quantization/sign_roundv2/quantizer.py b/auto_round/algorithms/quantization/sign_roundv2/quantizer.py
index d01778f58..4a7cabc4e 100644
--- a/auto_round/algorithms/quantization/sign_roundv2/quantizer.py
+++ b/auto_round/algorithms/quantization/sign_roundv2/quantizer.py
@@ -98,7 +98,7 @@ class SignRoundV2Quantizer(SignRoundQuantizer):
with autocast_ctx:
return torch.mean((torch.abs(pred_output.to(torch.float32) - ref_output.to(torch.float32)) * mask) ** 2)
- return super()._get_loss(pred_output, ref_output, indices, mse_loss, device)
+ return super()._get_loss(pred_output, ref_output, indices, mse_loss, device, valid_token_mask)
def register_fp_input_forward_hooks(self, block):
"""Register FP-input hooks: imatrix."""
diff --git a/test/unit/common/algorithms/test_signroundv2_loss.py b/test/unit/common/algorithms/test_signroundv2_loss.py
new file mode 100644
index 000000000..d3ca3242d
--- /dev/null
+++ b/test/unit/common/algorithms/test_signroundv2_loss.py
@@ -0,0 +1,56 @@
+# 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.
+
+"""SignRoundV2 must honour the padding mask on its plain-loss path."""
+
+from types import SimpleNamespace
+from unittest.mock import PropertyMock, patch
+
+import torch
+
+from auto_round.algorithms.quantization.sign_roundv2.quantizer import SignRoundV2Quantizer
+
+
+def _loss(use_outlier_suppressed_loss: bool):
+ # `super()` needs a real instance, but the constructor wants a full config.
+ quantizer = SignRoundV2Quantizer.__new__(SignRoundV2Quantizer)
+ quantizer._use_outlier_suppressed_loss = use_outlier_suppressed_loss
+
+ # The second row is padding, so only the first one may reach the loss.
+ pred = torch.tensor([[1.0, 1.0], [5.0, 5.0]])
+ ref = torch.zeros(2, 2)
+ valid_token_mask = [torch.tensor([1.0]), torch.tensor([0.0])]
+
+ with patch.object(SignRoundV2Quantizer, "model_context", new_callable=PropertyMock) as model_context:
+ model_context.return_value = SimpleNamespace(amp=False, amp_dtype=torch.float32)
+
+ return quantizer._get_loss(
+ pred,
+ ref,
+ torch.tensor([0, 1]),
+ torch.nn.functional.mse_loss,
+ "cpu",
+ valid_token_mask,
+ )
+
+
+def test_padding_is_masked_out_of_the_plain_loss():
+ # Masked: mean([1, 1, 0, 0]) == 0.5. Unmasked it would be mean([1, 1, 25, 25]) == 13.
+ assert torch.isclose(_loss(use_outlier_suppressed_loss=False), torch.tensor(0.5))
+
+
+def test_padding_is_masked_out_of_the_outlier_suppressed_loss():
+ loss = _loss(use_outlier_suppressed_loss=True)
+
+ assert torch.isfinite(loss)