Skip to content

test: direct unit suite for the quantization conversion layer (63 tests)#1903

Closed
arham766 wants to merge 2 commits into
NVIDIA:mainfrom
arham766:tests/test-conversion
Closed

test: direct unit suite for the quantization conversion layer (63 tests)#1903
arham766 wants to merge 2 commits into
NVIDIA:mainfrom
arham766:tests/test-conversion

Conversation

@arham766

@arham766 arham766 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: new tests

Part of the unit-coverage initiative in #1902 (one PR per module; this one is self-contained). Covers: quantize/restore mode layer: replace_quant_module insertion rules, register/unregister lifecycle, set_quantizer_by_cfg full-replacement vs partial-merge semantics across all three accepted formats (legacy formats pinned under pytest.warns(DeprecationWarning)), SequentialQuantizer creation/downgrade (guarded non-empty collection helpers), restore error directions (Unmatched vs Extra keys), cfg context-manager revert paths. Documents two real defects from the fused-experts name normalization (see issue).

Usage

N/A — tests only.

Testing

Suite is hermetic (CPU-only, no network, deterministic, <3s) and passes locally; the full collectable tests/unit tree passes with it (1999 passed). The suite was adversarially reviewed and verified to kill seeded mutations of the target module (details in #1902).

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?: ✅
  • Did you update Changelog?: N/A
  • Did you get Claude approval on this PR?: N/A (external contributor)

Additional Information

Issue: #1902

Summary by CodeRabbit

  • Tests
    • Added extensive unit-test coverage for quantization model conversion and restoration workflows.
    • Validated quantizer module replacement bookkeeping, including enable/disable defaults, in-place root conversion, and safeguards against already-quantized models.
    • Verified quantizer configuration application across multiple config formats (including wildcard matching and sequential quantizers), attribute update APIs (full/partial), and deprecated attribute handling with deprecation warnings.
    • Covered quantizer state extraction, metadata tracking, and strict round-trip restoration error cases.

Hermetic CPU-only unit tests; adversarially reviewed and verified to
kill seeded mutations of the target module. Part of the coverage
initiative in NVIDIA#1902.

Signed-off-by: arham766 <arhamislam766@yahoo.com>
@arham766 arham766 requested a review from a team as a code owner July 5, 2026 20:10
@copy-pr-bot

copy-pr-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: f00890b9-c654-4c87-babe-cfa11478fb22

📥 Commits

Reviewing files that changed from the base of the PR and between 7321acb and 2ebd0ff.

📒 Files selected for processing (1)
  • tests/unit/torch/quantization/test_conversion.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/unit/torch/quantization/test_conversion.py

📝 Walkthrough

Walkthrough

Adds a new unit test suite for quantization conversion, covering module replacement, registration, cfg-driven quantizer updates, attribute APIs, state restoration, conversion entrypoints, context-managed overrides, and a deprecated helper.

Changes

Conversion module test suite

Layer / File(s) Summary
Test setup, helpers, and fixtures
tests/unit/torch/quantization/test_conversion.py
Adds imports, shared constants, helper utilities, and dummy module classes used across the suite.
replace_quant_module and register/unregister lifecycle
tests/unit/torch/quantization/test_conversion.py
Tests quantizer insertion behavior, parameter preservation, default states, rejection of already-quantized models, and register/unregister lifecycle with error handling.
set_quantizer_by_cfg behavior
tests/unit/torch/quantization/test_conversion.py
Tests cfg application across new and legacy formats, deny-all/selective-enable patterns, parent-class scoping, sequential quantizer upgrade and downgrade, and enable-state broadcasting.
set_quantizer_attributes full and partial semantics
tests/unit/torch/quantization/test_conversion.py
Tests full replacement and partial-merge semantics, callable and wildcard filters, sequential broadcast behavior, and validation of unsupported inputs.
Fused-experts quantizer name normalization
tests/unit/torch/quantization/test_conversion.py
Tests normalization of fused-experts quantizer names for wildcard matching.
Quantizer state, metadata, and restoration
tests/unit/torch/quantization/test_conversion.py
Tests quantizer_state coverage and equality, metadata recording, restore_quantizer_state roundtrips, buffer placeholder reconstruction, and NVFP4 promotion.
Convert/restore entrypoints
tests/unit/torch/quantization/test_conversion.py
Tests convert_to_quantized_model/restore_quantized_model roundtrips, including in-place conversion and calibrated buffer presence.
Context overrides and deprecated API
tests/unit/torch/quantization/test_conversion.py
Tests set_quantizer_by_cfg_context restoration behavior and deprecated set_quantizer_attribute warning and merge semantics.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related issues

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a direct unit test suite for the quantization conversion layer.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 Only a new test file was added, and it contains no torch.load/np.load/trust_remote_code/eval/exec/nosec patterns or new deps.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/unit/torch/quantization/test_conversion.py (1)

189-201: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Weak assertion for the "forward routes through quantizer" claim.

The comment on Line 199 states the forward call must route through the quantizer, but the test only checks that the call doesn't raise. By default TensorQuantizer has if_quant=True, if_calib=False and computes a dynamic amax per call, so a broken routing (quantizer silently bypassed) wouldn't necessarily raise here — the assertion doesn't actually pin the claimed behavior.

Consider a stronger check, e.g. patching/spying on input_quantizer.forward or comparing output against a manually-unquantized forward pass, to make this a real regression guard.

♻️ Example approach
-            model(torch.randn(2, 4))  # forward must route through the quantizer
+            called = []
+            orig_forward = model[1].input_quantizer.forward
+            model[1].input_quantizer.forward = lambda x: (called.append(True), orig_forward(x))[1]
+            model(torch.randn(2, 4))
+            assert called, "forward did not route through the quantizer"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/torch/quantization/test_conversion.py` around lines 189 - 201, The
forward-call assertion in test_register_replace_unregister_cycle is too weak to
prove routing through TensorQuantizer. Strengthen the check around
model(torch.randn(2, 4)) by spying on or patching
_QuantPlainAct.input_quantizer.forward, or by asserting the output differs from
a manually computed unquantized pass, so the test in
test_register_replace_unregister_cycle and the
replace_quant_module/register/unregister path actually verify quantizer usage
rather than just lack of exceptions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/unit/torch/quantization/test_conversion.py`:
- Around line 189-201: The forward-call assertion in
test_register_replace_unregister_cycle is too weak to prove routing through
TensorQuantizer. Strengthen the check around model(torch.randn(2, 4)) by spying
on or patching _QuantPlainAct.input_quantizer.forward, or by asserting the
output differs from a manually computed unquantized pass, so the test in
test_register_replace_unregister_cycle and the
replace_quant_module/register/unregister path actually verify quantizer usage
rather than just lack of exceptions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 9ce486ed-389a-4f8c-8ac4-04ca3170e3f5

📥 Commits

Reviewing files that changed from the base of the PR and between b96a785 and 7321acb.

📒 Files selected for processing (1)
  • tests/unit/torch/quantization/test_conversion.py

…lamp assertion; update header year

Signed-off-by: arham766 <arhamislam766@yahoo.com>
@arham766

arham766 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated into #1927 per maintainer feedback in #1902 — the suite was trimmed to only the lines codecov reports uncovered, with parametrization clusters deduplicated. Closing in favor of that PR.

@arham766 arham766 closed this Jul 6, 2026
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