Skip to content

fix(autoquant): score grouped QKV at attention output#1993

Draft
realAsma wants to merge 2 commits into
mainfrom
asma/autoquantize-qkv-self-attn-scoring
Draft

fix(autoquant): score grouped QKV at attention output#1993
realAsma wants to merge 2 commits into
mainfrom
asma/autoquantize-qkv-self-attn-scoring

Conversation

@realAsma

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: Bug fix.

Changes gradient-based AutoQuantize sensitivity scoring for grouped q_proj, k_proj, and v_proj candidates to measure their joint perturbation at the enclosing self-attention module output. QKV projections still share one format decision, and parameter/cost accounting still aggregates all three projections.

This follows the output-side sensitivity formulation described in #1979 while avoiding the previous sum of three independently measured projection-output scores.

Usage

No API changes.

Testing

  • pre-commit run --files modelopt/torch/quantization/algorithms.py tests/unit/torch/quantization/test_autoquant.py
  • pytest_pwd -q -x tests/unit/torch/quantization/test_autoquant.py (71 passed)
  • Focused regression coverage verifies the attention-output score, distinguishes it from the old summed-projection score, and preserves shared grouping plus aggregate cost.

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 — internal scoring correction with no API change
  • Did you get Claude approval on this PR?: N/A — draft PR; owner review pending

Additional Information

Related technical background: #1979.

Signed-off-by: realAsma <akuriparambi@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Jul 19, 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 Jul 19, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 866dd36f-f306-4c5b-9c0d-732b20e69560

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch asma/autoquantize-qkv-self-attn-scoring

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

@github-actions

github-actions Bot commented Jul 19, 2026

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-1993/

Built to branch gh-pages at 2026-07-19 12:17 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Signed-off-by: realAsma <akuriparambi@nvidia.com>
@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.80%. Comparing base (9392dfe) to head (73ad76f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1993   +/-   ##
=======================================
  Coverage   77.79%   77.80%           
=======================================
  Files         518      518           
  Lines       58204    58205    +1     
=======================================
+ Hits        45279    45285    +6     
+ Misses      12925    12920    -5     
Flag Coverage Δ
unit 54.81% <100.00%> (+0.01%) ⬆️

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.

@realAsma

Copy link
Copy Markdown
Contributor Author

/claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude review passed — no blocking issues found. LGTM

Summary

Findings — CRITICAL: 0, IMPORTANT: 0, SUGGESTION: 1

This is a tightly-scoped, well-tested correctness fix (2 files, +87/-4).

What was reviewed

  • modelopt/torch/quantization/algorithms.py — the substantive change.
  • tests/unit/torch/quantization/test_autoquant.py — new focused regression coverage.

Algorithm correctness (OK)
The change adds _ATTN_QKV_RULE to AutoQuantizeGradientSearcher.score_module_rules, so grouped q_proj/k_proj/v_proj candidates are scored via joint perturbation at the enclosing self-attention output instead of the previous sum of three independently-measured projection-output scores. QKV already share one QuantRecipeHparam via grouping; the three identical score_modules dedupe to the single attn module (list(set(...))), and the full-backward-hook on attn accumulates one importance entry keyed by grad_output w.r.t. the attention output. Toggling hparam.active swaps quantizers on all three projections together, so output_diff correctly captures the joint QKV perturbation. Parameter/cost accounting still aggregates all three projections (verified by the test's cost assertion).

Backward compatibility (OK)
The forward-wrapper signature change from (module, input, *args, **kwargs) to (module, *args, **kwargs) is strictly more general: identical for positional calls, and it additionally supports keyword-bound inputs (x=x) which the new attention score module needs and which the parametrized test exercises. No public API, config schema, or modelopt_state change.

Most impactful note
One non-blocking SUGGESTION (inline): scoring now re-invokes the full attention forward multiple times per step rather than a pure Linear. This is safe under the usual use_cache=False calibration path, but that idempotency assumption is now load-bearing — worth documenting or guarding for score modules that mutate state (KV cache).

Risk assessment
Low. Internal scoring correction, no API change, fully covered by new tests, coverage green.

Comment thread modelopt/torch/quantization/algorithms.py
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