Skip to content
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

[optim] bugfix when all parameters have no grad #52944

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/test_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,26 @@ def test_duplicate_params_in_param_group(self):
self.assertEqual(len(w), 1)
self.assertIn('a parameter group with duplicate parameters', str(w[0].message))

def test_no_grad_for_all_params(self):
param = torch.randn(5, 5, requires_grad=False)

optimizer_list = [
optim.Adadelta,
optim.AdamW,
optim.Adam,
optim.Adagrad,
optim.Adamax,
optim.RMSprop,
optim.SGD,
optim.SparseAdam,
optim.ASGD,
]
for optim_ctr in optimizer_list:
opt = optim_ctr([param, param], lr=0.1)
# make sure step can still run even if
# all params have no grad
opt.step()


class SchedulerTestNet(torch.nn.Module):
def __init__(self):
Expand Down
3 changes: 1 addition & 2 deletions torch/optim/adadelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def step(self, closure=None):
grads = []
square_avgs = []
acc_deltas = []
lr, rho, eps, weight_decay = group['lr'], group['rho'], group['eps'], group['weight_decay']

for p in group['params']:
if p.grad is None:
Expand All @@ -74,8 +75,6 @@ def step(self, closure=None):
square_avgs.append(state['square_avg'])
acc_deltas.append(state['acc_delta'])

lr, rho, eps, weight_decay = group['lr'], group['rho'], group['eps'], group['weight_decay']

state['step'] += 1

F.adadelta(params_with_grad,
Expand Down
2 changes: 1 addition & 1 deletion torch/optim/adam.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def step(self, closure=None):
state_sums = []
max_exp_avg_sqs = []
state_steps = []
beta1, beta2 = group['betas']

for p in group['params']:
if p.grad is not None:
Expand Down Expand Up @@ -104,7 +105,6 @@ def step(self, closure=None):
# record the step after step update
state_steps.append(state['step'])

beta1, beta2 = group['betas']
F.adam(params_with_grad,
grads,
exp_avgs,
Expand Down
2 changes: 1 addition & 1 deletion torch/optim/adamw.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def step(self, closure=None):
max_exp_avg_sqs = []
state_steps = []
amsgrad = group['amsgrad']
beta1, beta2 = group['betas']

for p in group['params']:
if p.grad is None:
Expand Down Expand Up @@ -101,7 +102,6 @@ def step(self, closure=None):
if amsgrad:
max_exp_avg_sqs.append(state['max_exp_avg_sq'])

beta1, beta2 = group['betas']
# update the steps for each param group update
state['step'] += 1
# record the step after step update
Expand Down