Skip to content

[OMNIML-6228] Seed the quantizer tests that compare against the reference at exact rounding ties - #2914

Merged
shengliangxu merged 1 commit into
mainfrom
shengliangx/ci-flaky-quantizer-seed
Oct 21, 2026
Merged

shengliangxu merged 1 commit into
mainfrom
shengliangx/ci-flaky-quantizer-seed

Conversation

@shengliangxu

@shengliangxu shengliangxu commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Type of change: Bug fix (tests)

TensorQuantizerTester.test_per_channel_scale failed once in a unit job of PR #2852 (unit-3.12(torch_214, tf_latest), xdist worker gw3). It fake-quantizes an unseeded random tensor and compares the result with the test's own reference quantizer (quant in _test_utils/torch/quantization/quant_utils.py). The two round an element that sits exactly on a tie (x / step = N.5) in opposite directions, so that element ends up one quantization step apart (0.02-0.03 here), which rtol=1e-2 cannot absorb. Reported by the CI speed-up work; I reproduced it.

Over 20,000 RNG states on CPU the unseeded tests fail like this:

test failures
TensorQuantizerTester.test_per_channel_scale 8 (0.040%), e.g. seeds 1786, 5343, 8185, 11150
TensorQuantizerTester.test_per_tensor_scale 1 (0.005%), seed 8276
TensorQuantCommon.test_per_tensor_scale 1 (0.005%), seed 8276

Every failure is exactly one element one step off (seed 1786: x / step = -74.500000, reference -2.1077, quantizer -2.0796). The other methods of TensorQuantCommon pass all 20,000 states, and its test_per_channel_scale already calls torch.manual_seed(123).

A serial run draws the same input every time, so this stayed hidden until pytest-xdist (#2845) made the RNG state depend on the scheduling. About 14 unit jobs of a PR run execute these tests, so a 0.05% failure rate per execution is roughly a 0.7% chance that a PR run needs a re-run.

The three tests now start with torch.manual_seed(123), like the sibling test does.

Usage

N/A

Testing

  • With the seeds, the three tests pass under every outer RNG state 0..11999 (the failing seeds above are in that range): the unseeded version failed 8 + 1 + 1 times in the first 20,000.
  • tests/unit/torch/quantization/test_tensor_quantizer_cpu.py and test_tensor_quant_cpu.py: 83 passed.
  • The same testers also back the GPU classes in tests/gpu/torch/quantization/test_tensor_quantizer_cuda.py and test_tensor_quant_cuda.py. I did not run those on a GPU; a fixed input could in principle tie on the CUDA kernel, which would show up as a deterministic failure in this PR's GPU CI rather than a random one.

Before your PR is "Ready for review"

  • Is this change backward compatible?: ✅
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: N/A
  • Did you write any new necessary tests?: N/A (the change makes existing tests deterministic)
  • Did you update Changelog?: N/A
  • Did you get Claude approval on this PR?: ❌ (to be run)

Summary by CodeRabbit

  • Tests
    • Improved quantization test consistency by using deterministic random seeds.
    • Stabilized coverage of rounding-tie behavior across per-tensor and per-channel quantization tests.

…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 <shengliangx@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 21, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/Model-Optimizer/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 1a3b90c8-c550-4d7d-8f9a-c77e4b54938e
📥 Commits

Reviewing files that changed from the base of the PR and between af6d7d9 and d773ea0.

📒 Files selected for processing: 2
  • tests/_test_utils/torch/quantization/tensor_quant_common.py
  • tests/_test_utils/torch/quantization/tensor_quantizer_common.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

📝 Walkthrough

Three quantization tests now seed PyTorch’s random number generator with 123 before generating input data.

Changes: Quantization test determinism

Layer / File(s) Summary
Seed quantization test inputs
tests/_test_utils/torch/quantization/tensor_quant_common.py, tests/_test_utils/torch/quantization/tensor_quantizer_common.py
The per-tensor and per-channel scale tests set PyTorch’s random seed to 123 before generating inputs.

Priority: ⬇️ Low

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

Suggested reviewers: kevalmorabia97

Merge Risk: ⚪ Minimal · up to d773e

The deterministic test changes are ready to merge; CUDA coverage remains a follow-up validation item.

Pre-merge checks: Passed 6
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly identifies the quantizer tests and the deterministic seeding needed to handle exact rounding ties.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns Passed PASS. The pull request changes only two Python test utility files under tests/_test_utils/torch/quantization/. It does not change the modelopt package, examples, pyproject.toml, or `requirements…
Full details: Security Anti-Patterns

Explanation

PASS. The pull request changes only two Python test utility files under tests/_test_utils/torch/quantization/. It does not change the modelopt package, examples, pyproject.toml, or requirements.txt, so none of the specified security anti-pattern conditions apply.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.82%. Comparing base (576c66f) to head (d773ea0).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2914      +/-   ##
==========================================
+ Coverage   70.08%   78.82%   +8.74%     
==========================================
  Files         659      659              
  Lines       74081    74081              
==========================================
+ Hits        51917    58392    +6475     
+ Misses      22164    15689    -6475     
Flag Coverage Δ
examples-diffusers 20.04% <ø> (-0.01%) ⬇️
examples-gpt-oss 13.51% <ø> (-0.01%) ⬇️
examples-hf_ptq 23.19% <ø> (-0.04%) ⬇️
examples-llm_distill 13.59% <ø> (-0.01%) ⬇️
examples-llm_eval 17.24% <ø> (+<0.01%) ⬆️
examples-llm_qat 17.48% <ø> (-0.01%) ⬇️
examples-llm_sparsity 15.76% <ø> (-0.01%) ⬇️
examples-megatron_bridge 26.43% <ø> (-0.13%) ⬇️
examples-specdec_bench 13.29% <ø> (-0.01%) ⬇️
examples-speculative_decoding 17.47% <ø> (-0.01%) ⬇️
examples-torch_onnx 21.39% <ø> (-0.01%) ⬇️
examples-torch_trt 15.24% <ø> (-0.01%) ⬇️
examples-vllm_serve 15.08% <ø> (-0.01%) ⬇️
gpu 59.22% <ø> (+37.10%) ⬆️
regression 15.10% <ø> (-0.01%) ⬇️
unit 60.59% <ø> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shengliangxu
shengliangxu marked this pull request as ready for review October 21, 2026 17:02
@shengliangxu
shengliangxu requested a review from a team as a code owner October 21, 2026 17:02
@shengliangxu
shengliangxu merged commit 704093c into main Oct 21, 2026
69 of 71 checks passed
@shengliangxu
shengliangxu deleted the shengliangx/ci-flaky-quantizer-seed branch October 21, 2026 20:24
@github-actions

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-10-21 20:25 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants