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

[Fix] device mismatch problems #267

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/changelogs/v3.1.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

### Bug

* Fix to handle the optimizers that only take the `model` instead of the parameters in `create_optimizer()`. (#263)
* Handle the optimizers that only take the `model` instead of the parameters in `create_optimizer()`. (#263)
* Move the variable to the same device with the parameter. (#266, #267)
2 changes: 1 addition & 1 deletion pytorch_optimizer/optimizer/adamg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
lr: float = 1e-3,
betas: BETAS = (0.95, 0.999, 0.95),
p: float = 0.5,
q: float = 0.25,
q: float = 0.24,
weight_decay: float = 0.0,
weight_decouple: bool = False,
fixed_decay: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion pytorch_optimizer/optimizer/adan.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def get_global_gradient_norm(self) -> Union[torch.Tensor, float]:
if self.defaults['max_grad_norm'] == 0.0:
return 1.0

global_grad_norm = get_global_gradient_norm(self.param_groups, self.param_groups[0]['params'][0].device)
global_grad_norm = get_global_gradient_norm(self.param_groups)
global_grad_norm.sqrt_().add_(self.defaults['eps'])

return torch.clamp(self.defaults['max_grad_norm'] / global_grad_norm, max=1.0)
Expand Down
2 changes: 1 addition & 1 deletion pytorch_optimizer/optimizer/alig.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def reset(self):
@torch.no_grad()
def compute_step_size(self, loss: float) -> float:
r"""Compute step_size."""
global_grad_norm = get_global_gradient_norm(self.param_groups, torch.device('cpu'))
global_grad_norm = get_global_gradient_norm(self.param_groups)
global_grad_norm.add_(1e-6)

return loss / global_grad_norm.item()
Expand Down
2 changes: 1 addition & 1 deletion pytorch_optimizer/optimizer/dadapt.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def step(self, closure: CLOSURE = None) -> LOSS:
numerator_weighted = group['numerator_weighted']

if group['step'] == 0:
group['g0_norm'] = get_global_gradient_norm(self.param_groups, device).sqrt_().item()
group['g0_norm'] = get_global_gradient_norm(self.param_groups).sqrt_().item()
g0_norm = group['g0_norm']

if g0_norm == 0:
Expand Down
2 changes: 1 addition & 1 deletion pytorch_optimizer/optimizer/lamb.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_global_gradient_norm(self) -> Union[torch.Tensor, float]:
if self.defaults['max_grad_norm'] == 0.0:
return 1.0

global_grad_norm = get_global_gradient_norm(self.param_groups, self.param_groups[0]['params'][0].device)
global_grad_norm = get_global_gradient_norm(self.param_groups)
global_grad_norm.sqrt_().add_(self.defaults['eps'])

return torch.clamp(self.defaults['max_grad_norm'] / global_grad_norm, max=1.0)
Expand Down
2 changes: 2 additions & 0 deletions pytorch_optimizer/optimizer/prodigy.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def step(self, closure: CLOSURE = None) -> LOSS:

if 'd_numerator' not in group:
group['d_numerator'] = torch.tensor([0.0], device=device)
elif group['d_numerator'].device != device:
group['d_numerator'] = group['d_numerator'].to(device)

d_numerator = group['d_numerator']
d_numerator.mul_(beta3)
Expand Down
4 changes: 2 additions & 2 deletions pytorch_optimizer/optimizer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ def l2_projection(parameters: PARAMETERS, max_norm: float = 1e2):


@torch.no_grad()
def get_global_gradient_norm(param_groups: List[Dict], device: torch.device) -> torch.Tensor:
def get_global_gradient_norm(param_groups: List[Dict]) -> torch.Tensor:
r"""Get global gradient norm."""
global_grad_norm = torch.zeros(1, dtype=torch.float32, device=device)
global_grad_norm = torch.zeros(1, dtype=torch.float32, device=param_groups[0]['params'][0].device)

for group in param_groups:
for p in group['params']:
Expand Down
4 changes: 2 additions & 2 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@
(Prodigy, {'lr': 1e0, 'beta3': 0.999, 'weight_decay': 1e-3, 'safeguard_warmup': True}, 15),
(PAdam, {'lr': 1e0, 'weight_decay': 1e-3}, 5),
(Tiger, {'lr': 1e0, 'weight_decay': 1e-3}, 5),
(CAME, {'lr': 7.5e-1, 'weight_decay': 1e-3}, 75),
(CAME, {'lr': 7.5e-1, 'weight_decay': 1e-3, 'ams_bound': True}, 75),
(CAME, {'lr': 7.5e-1, 'weight_decay': 1e-3}, 70),
(CAME, {'lr': 7.5e-1, 'weight_decay': 1e-3, 'ams_bound': True}, 70),
(Aida, {'lr': 1e0, 'weight_decay': 1e-3, 'ams_bound': True}, 5),
(
GaLore,
Expand Down