Skip to content

test: direct unit suite for TensorQuantizer with exact-value fake-quant checks (90 tests)#1905

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

test: direct unit suite for TensorQuantizer with exact-value fake-quant checks (90 tests)#1905
arham766 wants to merge 2 commits into
NVIDIA:mainfrom
arham766:tests/test-tensor-quantizer

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: the TensorQuantizer class contract directly: 10 hand-computed INT8 exact-value forwards (narrow/unsigned/per-channel/int4/zero-amax), 4 exact FP8 E4M3 cases incl. RNE ties, block quant, fp16/bf16 round-trips, amax/pre_quant_scale get-set semantics, calibration choreography, modelopt_state+state_dict round-trip, STE backward variants, backend registry error paths. All rounding operands verified tie-safe for platform determinism.

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 CPU-focused unit coverage for tensor quantization, including default configuration, enable/disable behavior, scaling interactions, and robust handling of empty tensors.
    • Verified amax and pre-quant scale semantics (including state persistence and shape-change safeguards).
    • Added exact correctness checks for INT8/FP8 fake quantization and fixed-shape block quantization.
    • Covered dtype handling, calibration contract, state serialization/restoration, backward/STE behavior, backend registry validation, sequential quantizer delegation, and warning hygiene.

…-quant checks

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: 0b2f784f-eb8f-44ec-8a5b-a94c928547b2

📥 Commits

Reviewing files that changed from the base of the PR and between ad0ffd5 and 3703629.

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

📝 Walkthrough

Walkthrough

This PR adds a new CPU-only unit test module for TensorQuantizer, covering construction, enable/disable behavior, amax and pre_quant_scale semantics, exact fake-quant math, block quantization, dtype handling, calibration, state serialization, repr/validation, backward gradients, hard-disabled behavior, backend registry behavior, SequentialQuantizer contracts, constant amax, and warning hygiene.

Changes

TensorQuantizer test suite

Layer / File(s) Summary
Module setup and construction/enable defaults
tests/unit/torch/quantization/test_tensor_quantizer.py
Adds docstring, imports, _tq helper, and tests for default construction, config propagation, and enable/disable forward behavior.
Amax and pre_quant_scale contracts
tests/unit/torch/quantization/test_tensor_quantizer.py
Tests amax get/set, buffer persistence, reset, step-size behavior, and pre_quant_scale setter/context-manager behavior.
Exact fake-quant math and block quantization
tests/unit/torch/quantization/test_tensor_quantizer.py
Hand-computed INT8/FP8 fake-quant forward tests, static block quantization, and dtype preservation tests.
Calibration and state serialization
tests/unit/torch/quantization/test_tensor_quantizer.py
Tests calibration collection/load behavior and modelopt state serialization/restoration.
Repr, validation, and backward gradient tests
tests/unit/torch/quantization/test_tensor_quantizer.py
Tests extra_repr stability, validate_attr/export_amax behavior, and STE gradient behavior.
Hard-disabled quantizer, backend registry, sequential and constant amax
tests/unit/torch/quantization/test_tensor_quantizer.py
Tests HardDisabledTensorQuantizer errors, backend registry behavior, SequentialQuantizer delegation, constant amax override, and warning hygiene.

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 new TensorQuantizer test suite and its exact-value fake-quant coverage.
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 test file changed, and it contains none of the banned load/eval/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_tensor_quantizer.py (1)

766-810: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider a fixture for backend registry register/unregister cleanup.

test_register_forward_unregister and test_reregister_warns each hand-roll try/finally registration cleanup for register_quant_backend/unregister_quant_backend. A small pytest fixture yielding a registered-name helper (with teardown always calling unregister_quant_backend) would remove the duplicated try/finally boilerplate across these tests.

🤖 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_tensor_quantizer.py` around lines 766 -
810, The backend registry tests duplicate register/unregister cleanup logic in
test_register_forward_unregister and test_reregister_warns. Introduce a small
pytest fixture in TestQuantBackendRegistry that registers a backend name for
each test and always unregisters it in teardown, then update those tests to use
the fixture instead of hand-written try/finally blocks around
register_quant_backend and unregister_quant_backend.
🤖 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_tensor_quantizer.py`:
- Around line 766-810: The backend registry tests duplicate register/unregister
cleanup logic in test_register_forward_unregister and test_reregister_warns.
Introduce a small pytest fixture in TestQuantBackendRegistry that registers a
backend name for each test and always unregisters it in teardown, then update
those tests to use the fixture instead of hand-written try/finally blocks around
register_quant_backend and unregister_quant_backend.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 9bc62e74-ebe0-4ae7-a491-b8bfc7bf77f8

📥 Commits

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

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

…date 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