-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Check SparseAdam params are dense on init (#41966) #43668
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
Check SparseAdam params are dense on init (#41966) #43668
Conversation
💊 CI failures summary and remediationsAs of commit 46c4279 (more details on the Dr. CI page): 💚 💚 Looks good so far! There are no failures yet. 💚 💚 This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group. This comment has been revised 5 times. |
test/test_optim.py
Outdated
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.
"params" should be a list, so the correct usage is:
optim.SparseAdam([{"params": [torch.zeros(3, layout=torch.sparse_coo)]}])
The documentation for that is here: https://pytorch.org/docs/master/optim.html#per-parameter-options
torch/optim/sparse_adam.py
Outdated
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.
If we get a parameter dictionary, then param should be a List of Tensors. So instead of checking if param is sparse, we want to check if any of the tensors in param is sparse.
zou3519
left a comment
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.
The case for if the user passes in per-parameter options seems to be wrong (see https://pytorch.org/docs/master/optim.html#per-parameter-options) but the general approach here looks good to me
fd5b315 to
97fc86c
Compare
97fc86c to
46c4279
Compare
|
Can you think of a better way than nested iterating over all the params if it's a dictionary? sparse_params = []
for index, param in enumerate(params):
if isinstance(param, dict):
for d_index, d_param in enumerate(param.get("params", [])):
if d_param.is_sparse:
sparse_params.append([index, d_index])
elif param.is_sparse:
sparse_params.append(index)
if sparse_params:
raise ValueError(
f"Sparse params at indices {sparse_params}: SparseAdam requires dense parameter tensors"
)Would it make sense to let the optimizer parse the params and then check param groups after the super call? would still involve a nested check of is_sparse on all the parameters. e.g. for group in self.param_groups:
for param in group['params']:
if param.is_sparse:
# do somethingI think I prefer the first approach since it lets a user know where in their input the error is coming from. |
|
There are some pros and cons to each approach. For the first approach:
For the second approach:
I don't have a real preference, so let's go for the first approach. It might look weird but at least we're giving the user a nice error message :) |
Codecov Report
@@ Coverage Diff @@
## master #43668 +/- ##
=======================================
Coverage 69.34% 69.35%
=======================================
Files 378 378
Lines 46680 46690 +10
=======================================
+ Hits 32369 32380 +11
+ Misses 14311 14310 -1
Continue to review full report at Codecov.
|
facebook-github-bot
left a comment
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.
@ranman has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Fixes #41966
Raises a value error if user attempts to create SparseAdam optimizer with sparse parameter tensors.