Skip to content

[1/9] Add independent stochastic TensorQuantizer rounding - #3077

Draft
kaix-nv wants to merge 1 commit into
mainfrom
kaix/tensor-quantizer-rounding
Draft

kaix-nv wants to merge 1 commit into
mainfrom
kaix/tensor-quantizer-rounding

Conversation

@kaix-nv

@kaix-nv kaix-nv commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Merge order: 9 draft PRs

#3077 → #2519 → #3078 → #2657 → #2805 → #2788 → #2878 → #2503 → #2507

#2497 is already on main. Each PR is based on the preceding branch; #3077 is based on main.

What does this PR do?

Type of change: New feature.

Add rounding="nearest" | "stochastic" to TensorQuantizer. Nearest remains the default; integer and FP8 E4M3 fake QDQ and dynamic block16 NVFP4 support elementwise stochastic rounding. NVFP4 block scales retain nearest rounding. Quantizer metadata saves the policy; training checkpoints must also save RNG state.

Calibration and weight folding force nearest rounding. Unsupported real/custom backends and inference export reject stochastic policies. The default integer path preserves its prior arithmetic, including tie cases. The fused-expert fast path on current main only handles nearest rounding, so stochastic quantizers use the per-expert implementation. The biased rounding mode is removed and is not part of this release.

This core TensorQuantizer change is directly based on main and requires review from the TensorQuantizer owners.

Usage

from modelopt.torch.quantization.config import QuantizerAttributeConfig
from modelopt.torch.quantization.nn import TensorQuantizer

q = TensorQuantizer(QuantizerAttributeConfig(
    num_bits=(4, 3), type="dynamic", block_sizes={-1: 128}, rounding="stochastic",
))

Testing

Restacked on main 580ffff1cddb9b20a38b7434e6290654ebf96a8f on 2026-10-27; this PR head is d7b0a3240464b15658ad69fe8834c877c835fe34.

  • Combined stack at 9f913d907c298efeb99daf84a7c32246cf38208a: 226 tests passed in 108.95 seconds, covering TensorQuantizer defaults/calibration/folding/restore, state policies, independent recurrence and gradient references, replay cache lifecycle, CUDA graphs, packed two-precision gradients, GDN operand QDQ and guarded inverse behavior. These are stack-tip results; intermediate heads were not all rerun separately.
  • Changed-file pre-commit checks passed (including Ruff, mypy and Bandit). The recipe checker was run separately with the installed Python environment; all eight changed complete recipes loaded successfully.
  • All nine parent links, signed commits, Python syntax and diff checks verified. Every rebuilt branch is free of the removed biased mode; released changelog entries are preserved.
  • Local environment: RTX A6000/SM86, PyTorch 2.9.1+cu128, Triton 3.5.1. The regular GPU tests use independent reference prefixes where native vLLM is unavailable. The vLLM 0.30 engine, supported Megatron/Bridge environment, FLA 0.5.2 on Hopper/Blackwell, and full-model accuracy were not rerun. These results are not new model-quality or throughput evidence.
  • At this standalone head, all 63 TensorQuantizer CPU tests passed. The extended fused-expert regression requires SM89+ and compatible Transformers and was not run locally.

Before your PR is "Ready for review"

  • Backward compatibility: opt-in; existing default behavior is retained.
  • Necessary tests: existing focused suites extended or retained; validation and environment limits listed above.
  • Dependencies: optional versions and setup are documented with the example; this restack adds no dependency.
  • Changelog: updated under 0.49; released 0.48 entries preserved.
  • Owner review and CI for the new head remain required. Keep draft.

Review history

This draft continues #2829. During the restack, GitHub marked that PR merged when its rewritten head became an ancestor of its former base branch, before its base was retargeted. No main-branch merge occurred; main remains at 580ffff1cddb9b20a38b7434e6290654ebf96a8f. The prior PR retains the earlier discussion.

@copy-pr-bot

copy-pr-bot Bot commented Oct 28, 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 28, 2026 •

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://NVIDIA.github.io/Model-Optimizer/pr-preview/pr-3077/

Built to branch gh-pages at 2026-10-28 06:06 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.91597% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.59%. Comparing base (580ffff) to head (d7b0a32).

Files with missing lines Patch % Lines
modelopt/torch/quantization/tensor_quant.py 91.17% 6 Missing ⚠️
.../torch/quantization/nn/modules/tensor_quantizer.py 89.65% 3 Missing ⚠️
...torch/quantization/backends/fp8_per_tensor_gemm.py 0.00% 2 Missing ⚠️
.../torch/sparsity/attention_sparsity/plugins/vllm.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3077      +/-   ##
==========================================
+ Coverage   73.57%   73.59%   +0.01%     
==========================================
  Files         661      661              
  Lines       75682    75782     +100     
==========================================
+ Hits        55685    55773      +88     
- Misses      19997    20009      +12     
Flag Coverage Δ
unit 61.54% <89.91%> (+0.03%) ⬆️

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.

@kaix-nv
kaix-nv force-pushed the kaix/tensor-quantizer-rounding branch from d7b0a32 to 29c3c17 Compare November 17, 2026 01:08

This branch has not been deployed

No deployments
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.

1 participant