-
Notifications
You must be signed in to change notification settings - Fork 14
feat(autojac): Make jac_to_grad return optional weights #586
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
Open
ValerianRey
wants to merge
8
commits into
main
Choose a base branch
from
make-jac-to-grad-return-weights-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−14
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f09a485
feat(autojac): Make `jac_to_grad` return optional weights.
PierreQuinton 13523cc
Add test with `Weighting`
PierreQuinton cad8971
Make jac_to_grad only take aggregator
ValerianRey f9b728d
Make the aggregator parameter positional & keyword
ValerianRey 56ad695
Merge branch 'main' into make-jac-to-grad-return-weights-2
ValerianRey b701c59
Stop mentioning WeightedAggregator in jac_to_grad docstring
ValerianRey 5801380
Remove :returns:
ValerianRey d24c56a
fix typo
ValerianRey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,20 +3,23 @@ | |
| the obtained `.grad` field. | ||
| """ | ||
|
|
||
| from torch.testing import assert_close | ||
| from utils.asserts import assert_grad_close | ||
|
|
||
| from torchjd.aggregation import UPGrad | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I accidentally made this move out of the function. |
||
|
|
||
|
|
||
| def test_jac_to_grad() -> None: | ||
| import torch | ||
|
|
||
| from torchjd.aggregation import UPGrad | ||
| from torchjd.autojac import backward, jac_to_grad | ||
|
|
||
| param = torch.tensor([1.0, 2.0], requires_grad=True) | ||
| # Compute arbitrary quantities that are function of param | ||
| y1 = torch.tensor([-1.0, 1.0]) @ param | ||
| y2 = (param**2).sum() | ||
| backward([y1, y2]) # param now has a .jac field | ||
| jac_to_grad([param], aggregator=UPGrad()) # param now has a .grad field | ||
| weights = jac_to_grad([param], UPGrad()) # param now has a .grad field | ||
|
|
||
| assert_grad_close(param, torch.tensor([0.5000, 2.5000]), rtol=0.0, atol=1e-04) | ||
| assert_close(weights, torch.tensor([0.5, 0.5]), rtol=0.0, atol=0.0) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should return an empty tensor of weights when the aggregator is weighted.
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.
Or maybe not. Empty weights would be with tensors with 0 rows, but here we have 0 tensors.
Uh oh!
There was an error while loading. Please reload this page.
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.
We could also raise an error in that case. Worst case would be that some users would have to do
So that they avoid gettting this error in the niche cases where the number of elements of tensors varies between iterations and may be 0.