-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Fix docstring for clip_grads_with_norm_ to reflect clamping behavior #158200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/158200
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 7e384dc with merge base fac0be7 ( UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "release notes: python_frontend" |
|
Hello @spzala, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix lint
|
lint is still failing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RajeshvShiyal thanks for the PR and addressing review comments quickly. Seems like your new changes failing lint. Make sure to run it successfully locally.
|
Hello @mikaylagawarecki, @spzala, Following error seems infra specific. Collecting uv==0.1.45 File "/home/ec2-user/actions-runner/_work/pytorch/pytorch/test-infra/.github/scripts/run_with_env_secrets.py", line 98, in main Error: Process completed with exit code 1. Note: I have also run lintrunner locally, but no lint errors found on my local machine. |
|
@RajeshvShiyal thanks much for verifying locally. I agree the error seems related to build process. Hopefully, rerunning test shouldn't have it. |
|
@pytorchbot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
This PR updates the docstring for torch.nn.utils.clip_grads_with_norm_ to accurately reflect the implementation behavior. The current documentation suggests that gradients are always scaled by: grad = grad * (max_norm / (total_norm + eps)) However, the actual implementation clamps the scale coefficient to a maximum of 1.0, ensuring gradients are only scaled down, not up. This PR corrects the formula and adds a clarifying note to avoid confusion for users. Updated the formula in the docstring to: grad = grad * min(max_norm / (total_norm + eps), 1.0) Added a note explaining the rationale for clamping (to prevent gradient amplification). Ensured consistency with the behavior of clip_grad_norm_.
|
Successfully rebased |
|
The lint failure is real https://github.com/pytorch/pytorch/actions/runs/16324470514/job/46120316635?pr=158200 |
@mikaylagawarecki yes, it's real this time :) Thanks! The previous failure only had |
I have run the lintrunner locally. It was simulated. Now it would be resolved.
|
Hello @mikaylagawarecki, @spzala, I have run lintrunner locally and resolved the errors. Note: Lint also suggested me below changes, so, those changes also done. used below format instead this |
|
Hello @mikaylagawarecki, @spzala, Still framework/infra specific lint error.
|
|
@RajeshvShiyal the error seems real if you see here, https://github.com/pytorch/pytorch/actions/runs/16336731501/job/46188865026?pr=158200#step:15:163 |
Changes which are not related to this PR reverted back. Thank you |
|
Hello @mikaylagawarecki, @spzala Now following one failure. It seems specific to "sudo: setrlimit(RLIMIT_STACK): Operation not permitted"
|
Removed tuple of ints from supported dtype for parameter dim
Update _torch_docs.py
|
Hello @mikaylagawarecki, @spzala Still, one failure which is same as previous one.
|
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…158200) Fix docstring for clip_grads_with_norm_ to reflect clamping behavior This PR updates the docstring for torch.nn.utils.clip_grads_with_norm_ to accurately reflect the implementation behavior. The current documentation suggests that gradients are always scaled by: grad = grad * (max_norm / (total_norm + eps)) However, the actual implementation clamps the scale coefficient to a maximum of 1.0, ensuring gradients are only scaled down, not up. This PR corrects the formula and adds a clarifying note to avoid confusion for users. Updated the formula in the docstring to: grad = grad * min(max_norm / (total_norm + eps), 1.0) Added a note explaining the rationale for clamping (to prevent gradient amplification). Ensured consistency with the behavior of clip_grad_norm_. Fixes #151554 Pull Request resolved: #158200 Approved by: https://github.com/mikaylagawarecki







Fix docstring for clip_grads_with_norm_ to reflect clamping behavior
This PR updates the docstring for torch.nn.utils.clip_grads_with_norm_ to accurately reflect the implementation behavior. The current documentation suggests that gradients are always scaled by:
grad = grad * (max_norm / (total_norm + eps))
However, the actual implementation clamps the scale coefficient to a maximum of 1.0, ensuring gradients are only scaled down, not up. This PR corrects the formula and adds a clarifying note to avoid confusion for users.
Updated the formula in the docstring to:
grad = grad * min(max_norm / (total_norm + eps), 1.0)
Added a note explaining the rationale for clamping (to prevent gradient amplification).
Ensured consistency with the behavior of clip_grad_norm_.
Fixes #151554