From d773ea06b4e8cfa739a66b4088f78024b97689f7 Mon Sep 17 00:00:00 2001 From: Shengliang Xu Date: Wed, 21 Oct 2026 07:44:17 +0000 Subject: [PATCH] Seed the quantizer tests that compare against the reference at exact rounding ties test_per_channel_scale and test_per_tensor_scale of TensorQuantizerTester, and test_per_tensor_scale of TensorQuantCommon, compare a fake-quantized random input with the test's own reference quantizer. The two round an element that sits exactly on a tie (x / step = N.5) in opposite directions, so the outputs are one quantization step apart, which neither rtol=1e-2 nor the default tolerances absorb. Over 20,000 RNG states the unseeded tests fail 8 times for the per-channel test (0.040%, e.g. seeds 1786, 5343, 8185, 11150) and once for each per-tensor test (0.005%, seed 8276); each failure is exactly one element one step off. A serial run draws the same input every time, but under pytest-xdist the RNG state depends on the scheduling, and one such failure hit a unit job of PR #2852. The other methods of TensorQuantCommon pass all 20,000 states. Seed the three tests with 123, as test_per_channel_scale of TensorQuantCommon already does. Under outer RNG states 0..11999, the seeds above included, all three now pass every time. Signed-off-by: Shengliang Xu --- tests/_test_utils/torch/quantization/tensor_quant_common.py | 1 + tests/_test_utils/torch/quantization/tensor_quantizer_common.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tests/_test_utils/torch/quantization/tensor_quant_common.py b/tests/_test_utils/torch/quantization/tensor_quant_common.py index 5119519ed02..7205e0c79c5 100644 --- a/tests/_test_utils/torch/quantization/tensor_quant_common.py +++ b/tests/_test_utils/torch/quantization/tensor_quant_common.py @@ -36,6 +36,7 @@ def test_simple_run(self): def test_per_tensor_scale(self): """Tensor_quant matches quantization""" + torch.manual_seed(123) # an element on a rounding tie rounds differently in the reference x = torch.randn(31).to(self.device) quant_x_ref = quant( x, diff --git a/tests/_test_utils/torch/quantization/tensor_quantizer_common.py b/tests/_test_utils/torch/quantization/tensor_quantizer_common.py index 77d3acb7df3..c7ec0c2e53d 100644 --- a/tests/_test_utils/torch/quantization/tensor_quantizer_common.py +++ b/tests/_test_utils/torch/quantization/tensor_quantizer_common.py @@ -45,6 +45,7 @@ def test_simple_run(self): def test_per_tensor_scale(self): """Quantizer performs expected quantization""" + torch.manual_seed(123) # an element on a rounding tie rounds differently in the reference x = torch.randn(31).to(self.device) quant_x = quant(x, torch.max(torch.abs(x)), fake=True) @@ -54,6 +55,7 @@ def test_per_tensor_scale(self): def test_per_channel_scale(self): """Quantizer performs per channel scaling""" + torch.manual_seed(123) # an element on a rounding tie rounds differently in the reference x = torch.randn(5, 5, 4, 8).to(self.device) # Pytorch filter layout seems to be KCRS, reduce max to shape [K, 1, 1, 1] to test per channel scale