Skip to content

Fix clip backward with broadcasted constant bounds - #2089

Open
Robertboy18 wants to merge 3 commits into
NVIDIA:mainfrom
Robertboy18:fix/clip-broadcast-backward
Open

Fix clip backward with broadcasted constant bounds#2089
Robertboy18 wants to merge 3 commits into
NVIDIA:mainfrom
Robertboy18:fix/clip-broadcast-backward

Conversation

@Robertboy18

@Robertboy18 Robertboy18 commented Aug 6, 2026

Copy link
Copy Markdown

What does this PR do?

Type of change: Bug fix

Hey! I was experimenting with formalizing the clipping backward rule in Lean using TorchLean, and noticed a small discrepancy here.

ClipFunction.forward supports broadcastable tensor bounds, but backward raises the scalar-only error even when those bounds are constants and do not need gradients.

This keeps the scalar restriction for learnable bounds, while allowing constant tensor bounds to broadcast normally. I also added a CPU regression test that compares both the output and input gradient against the equivalent PyTorch min/max operations.

I modeled the coordinatewise broadcasted input VJP in Lean as well and kernel-checked the result. The formalization is not included in this PR, but I'm happy to share it if useful :)

I used a bit of AI assistance during the investigation and debugging, then manually checked the code, tests, and Lean statement/results.

Usage

inputs = torch.randn(2, 3, requires_grad=True)
clip_min = torch.tensor([-1.0, 0.0, 1.0])
clip_max = torch.tensor([0.0, 1.0, 3.0])

outputs = clip(inputs, clip_min, clip_max)
outputs.sum().backward()

Testing

  • Added a CPU regression test for vector bounds broadcast over a matrix.
  • Compared forward output and input gradients with PyTorch.
  • Ran focused pytest and pre-commit checks.

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

Summary by CodeRabbit

  • Bug Fixes

    • Fixed clipping operations with broadcastable, non-learnable minimum and maximum bounds.
    • Backward propagation now works correctly for tensor bounds without requiring scalar shapes.
  • Tests

    • Added coverage verifying clipping outputs and input gradients against equivalent native PyTorch operations.

Signed-off-by: Robert joseph <robujsph2001@gmail.com>
@Robertboy18
Robertboy18 requested review from a team as code owners August 6, 2026 14:03
@Robertboy18
Robertboy18 requested review from sugunav14 and a lite review from Copilot August 6, 2026 14:03
@copy-pr-bot

copy-pr-bot Bot commented Aug 6, 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.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 6, 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: d6d51867-01d3-42f3-b060-6703ac9b002c

📥 Commits

Reviewing files that changed from the base of the PR and between 089109d and d8fb74f.

📒 Files selected for processing (2)
  • modelopt/torch/quantization/nn/functional.py
  • tests/unit/torch/quantization/test_functional.py

📝 Walkthrough

Walkthrough

The clip backward validation now permits broadcastable non-learnable bounds. A CPU test verifies forward outputs and input gradients against native PyTorch operations.

Changes

Broadcastable Clip Bounds

Layer / File(s) Summary
Clip validation and broadcast coverage
modelopt/torch/quantization/nn/functional.py, tests/unit/torch/quantization/test_functional.py
Validation rejects non-scalar bounds only when they require gradients. The CPU test covers per-column broadcast bounds and compares outputs and input gradients with torch.minimum and torch.maximum.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: sugunav14

🚥 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 clearly and concisely describes the fix for clip backward propagation with broadcasted constant bounds.
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 The production change only adds requires_grad shape checks; no unsafe loads, remote-code flags, eval/exec, nosec comments, or dependency additions appear in the PR diff.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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