-
Notifications
You must be signed in to change notification settings - Fork 1
/
pfedmac.py
170 lines (142 loc) · 5.08 KB
/
pfedmac.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
"""
from typing import Any, Iterable, Optional, Union
import torch # noqa: F401
from torch import Tensor
from torch.nn import Parameter
from torch.optim.optimizer import Optimizer
from . import functional as F
from ._register import register_optimizer
__all__ = [
"pFedMacOptimizer",
]
@register_optimizer()
class pFedMacOptimizer(Optimizer):
"""Local optimizer for ``pFedMac`` via maximizing correlation (Mac)
using ``SGD`` (with variance reduction).
Mathematical definition:
.. math::
\\DeclareMathOperator*{\\argmin}{arg\\,min}
\\argmin_x \\{ f(x) - \\lambda \\langle x, x_0 \\rangle \\}
Parameters
----------
params : Iterable[dict] or Iterable[torch.nn.parameter.Parameter]
The parameters to optimize or dicts defining parameter groups.
lr : float, default: 1e-3
Learning rate.
momentum : float, default 1e-3
Momentum factor.
dampening : float, default 0
Dampening for momentum.
weight_decay : float, default 0
Weight decay factor (L2 penalty).
nesterov : bool, default False
If True, enables Nesterov momentum.
lam : float, default 0.1
The (penalty) coeff. of the maximizing correlation term,
i.e. the term :math:`\\lambda` in
.. math::
\\lambda \\langle x, x_0 \\rangle
"""
__name__ = "pFedMacOptimizer"
def __init__(
self,
params: Iterable[Union[dict, Parameter]],
lr: float = 1e-3,
momentum: float = 1e-3,
dampening: float = 0,
weight_decay: float = 0,
nesterov: bool = False,
lam: float = 0.1,
) -> None:
if lr < 0.0:
raise ValueError(f"Invalid learning rate: {lr}")
if momentum < 0.0:
raise ValueError(f"Invalid momentum value: {momentum}")
if weight_decay < 0.0:
raise ValueError(f"Invalid weight_decay value: {weight_decay}")
if nesterov and (momentum <= 0 or dampening != 0):
raise ValueError("Nesterov momentum requires a momentum and zero dampening")
if lam < 0.0:
raise ValueError(f"Invalid lam value: {lam}")
defaults = dict(
lr=lr,
momentum=momentum,
dampening=dampening,
weight_decay=weight_decay,
nesterov=nesterov,
lam=lam,
)
super().__init__(params, defaults)
def __setstate__(self, state: dict) -> None:
super().__setstate__(state)
for group in self.param_groups:
group.setdefault("nesterov", False)
@torch.no_grad()
def step(
self,
local_weights: Iterable[Parameter],
variance_buffer: Optional[Iterable[Tensor]] = None,
closure: Optional[callable] = None,
**kwargs: Any,
) -> Optional[Tensor]:
"""Performs a single optimization step.
Parameters
----------
local_weights : Iterable[torch.nn.parameter.Parameter]
The local weights updated by the local optimizer,
or of the previous iteration,
i.e. the term :math:`x_0` in
.. math::
\\lambda \\langle x, x_0 \\rangle
variance_buffer : Iterable[torch.Tensor], optional
The variance buffer of the local weights,
for the variance-reduced algorithms.
closure : callable, optional
A closure that reevaluates the model and returns the loss.
Returns
-------
loss : torch.Tensor, optional
The loss value evaluated by the closure.
"""
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
params_with_grad = []
d_p_list = []
momentum_buffer_list = []
weight_decay = group["weight_decay"]
momentum = group["momentum"]
dampening = group["dampening"]
nesterov = group["nesterov"]
lam = group["lam"]
lr = group["lr"]
for p in group["params"]:
if p.grad is not None:
params_with_grad.append(p)
d_p_list.append(p.grad)
state = self.state[p]
if "momentum_buffer" not in state:
momentum_buffer_list.append(None)
else:
momentum_buffer_list.append(state["momentum_buffer"])
F.mac_sgd(
params_with_grad,
local_weights,
variance_buffer,
d_p_list,
momentum_buffer_list,
weight_decay=weight_decay,
momentum=momentum,
lr=lr,
dampening=dampening,
nesterov=nesterov,
lam=lam,
)
# update momentum_buffers in state
for p, momentum_buffer in zip(params_with_grad, momentum_buffer_list):
state = self.state[p]
state["momentum_buffer"] = momentum_buffer
return loss