-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathema.py
424 lines (349 loc) · 16.4 KB
/
ema.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# copy-paste from: https://github.com/bghira/SimpleTuner/blob/main/helpers/training/ema.py
import contextlib
import copy
import logging
import os
from typing import Any, Dict, Iterable, Optional, Union
import torch
import transformers
from accelerate.logging import get_logger
from diffusers.utils import is_transformers_available
from diffusers.utils.deprecation_utils import deprecate
logger = get_logger(__name__)
def should_update_ema(args, step):
if args.ema_update_interval is None:
# If the EMA update interval is not set, always update the EMA.
return True
else:
should_update = step % args.ema_update_interval == 0
if should_update:
logger.debug("Updating EMA weights...")
return should_update
class EMAModel:
"""
Exponential Moving Average of models weights
"""
def __init__(
self,
args,
accelerator,
parameters: Iterable[torch.nn.Parameter],
decay: float = 0.9999,
min_decay: float = 0.0,
update_after_step: int = 0,
use_ema_warmup: bool = False,
inv_gamma: Union[float, int] = 1.0,
power: Union[float, int] = 2 / 3,
foreach: bool = True,
model_cls: Optional[Any] = None,
model_config: Dict[str, Any] = None,
**kwargs,
):
"""
Args:
parameters (Iterable[torch.nn.Parameter]): The parameters to track.
decay (float): The decay factor for the exponential moving average.
min_decay (float): The minimum decay factor for the exponential moving average.
update_after_step (int): The number of steps to wait before starting to update the EMA weights.
use_ema_warmup (bool): Whether to use EMA warmup.
inv_gamma (float):
Inverse multiplicative factor of EMA warmup. Default: 1. Only used if `use_ema_warmup` is True.
power (float): Exponential factor of EMA warmup. Default: 2/3. Only used if `use_ema_warmup` is True.
foreach (bool): Use torch._foreach functions for updating shadow parameters. Should be faster.
device (Optional[Union[str, torch.device]]): The device to store the EMA weights on. If None, the EMA
weights will be stored on CPU.
@crowsonkb's notes on EMA Warmup:
If gamma=1 and power=1, implements a simple average. gamma=1, power=2/3 are good values for models you plan
to train for a million or more steps (reaches decay factor 0.999 at 31.6K steps, 0.9999 at 1M steps),
gamma=1, power=3/4 for models you plan to train for less (reaches decay factor 0.999 at 10K steps, 0.9999
at 215.4k steps).
"""
if isinstance(parameters, torch.nn.Module):
deprecation_message = (
"Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. "
"Please pass the parameters of the module instead."
)
deprecate(
"passing a `torch.nn.Module` to `ExponentialMovingAverage`",
"1.0.0",
deprecation_message,
standard_warn=False,
)
parameters = parameters.parameters()
# set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility
use_ema_warmup = True
if kwargs.get("max_value", None) is not None:
deprecation_message = "The `max_value` argument is deprecated. Please use `decay` instead."
deprecate("max_value", "1.0.0", deprecation_message, standard_warn=False)
decay = kwargs["max_value"]
if kwargs.get("min_value", None) is not None:
deprecation_message = "The `min_value` argument is deprecated. Please use `min_decay` instead."
deprecate("min_value", "1.0.0", deprecation_message, standard_warn=False)
min_decay = kwargs["min_value"]
parameters = list(parameters)
self.shadow_params = [p.clone().detach() for p in parameters]
if kwargs.get("device", None) is not None:
deprecation_message = "The `device` argument is deprecated. Please use `to` instead."
deprecate("device", "1.0.0", deprecation_message, standard_warn=False)
self.to(device=kwargs["device"])
self.temp_stored_params = None
self.decay = decay
self.min_decay = min_decay
self.update_after_step = update_after_step
self.use_ema_warmup = use_ema_warmup
self.inv_gamma = inv_gamma
self.power = power
self.optimization_step = 0
self.cur_decay_value = None # set in `step()`
self.foreach = foreach
self.model_cls = model_cls
self.model_config = model_config
self.args = args
self.accelerator = accelerator
self.training = True # To emulate nn.Module's training mode
def save_state_dict(self, path: str) -> None:
"""
Save the EMA model's state directly to a file.
Args:
path (str): The file path where the EMA state will be saved.
"""
# if the folder containing the path does not exist, create it
os.makedirs(os.path.dirname(path), exist_ok=True)
# grab state dict
state_dict = self.state_dict()
# save it using torch.save
torch.save(state_dict, path)
logger.info(f"EMA model state saved to {path}")
def load_state_dict(self, path: str) -> None:
"""
Load the EMA model's state from a file and apply it to this instance.
Args:
path (str): The file path from where the EMA state will be loaded.
"""
state_dict = torch.load(path, map_location="cpu", weights_only=True)
# Load metadata
self.decay = state_dict.get("decay", self.decay)
self.min_decay = state_dict.get("min_decay", self.min_decay)
self.optimization_step = state_dict.get("optimization_step", self.optimization_step)
self.update_after_step = state_dict.get("update_after_step", self.update_after_step)
self.use_ema_warmup = state_dict.get("use_ema_warmup", self.use_ema_warmup)
self.inv_gamma = state_dict.get("inv_gamma", self.inv_gamma)
self.power = state_dict.get("power", self.power)
# Load shadow parameters
shadow_params = []
idx = 0
while f"shadow_params.{idx}" in state_dict:
shadow_params.append(state_dict[f"shadow_params.{idx}"])
idx += 1
if len(shadow_params) != len(self.shadow_params):
raise ValueError(
f"Mismatch in number of shadow parameters: expected {len(self.shadow_params)}, "
f"but found {len(shadow_params)} in the state dict."
)
for current_param, loaded_param in zip(self.shadow_params, shadow_params):
current_param.data.copy_(loaded_param.data)
logger.info(f"EMA model state loaded from {path}")
@classmethod
def from_pretrained(cls, path, model_cls) -> "EMAModel":
_, ema_kwargs = model_cls.load_config(path, return_unused_kwargs=True)
model = model_cls.from_pretrained(path)
ema_model = cls(model.parameters(), model_cls=model_cls, model_config=model.config)
ema_model.load_state_dict(ema_kwargs)
return ema_model
def save_pretrained(self, path, max_shard_size: str = "10GB"):
if self.model_cls is None:
raise ValueError("`save_pretrained` can only be used if `model_cls` was defined at __init__.")
if self.model_config is None:
raise ValueError("`save_pretrained` can only be used if `model_config` was defined at __init__.")
model = self.model_cls.from_config(self.model_config)
state_dict = self.state_dict(exclude_params=True)
state_dict.pop("shadow_params", None)
model.register_to_config(**state_dict)
self.copy_to(model.parameters())
model.save_pretrained(path, max_shard_size=max_shard_size)
def get_decay(self, optimization_step: int = None) -> float:
"""
Compute the decay factor for the exponential moving average.
"""
if optimization_step is None:
optimization_step = self.optimization_step
step = max(0, optimization_step - self.update_after_step - 1)
if step <= 0:
return 0.0
if self.use_ema_warmup:
cur_decay_value = 1 - (1 + step / self.inv_gamma) ** -self.power
else:
cur_decay_value = (1 + step) / (10 + step)
cur_decay_value = min(cur_decay_value, self.decay)
# make sure decay is not smaller than min_decay
cur_decay_value = max(cur_decay_value, self.min_decay)
return cur_decay_value
@torch.no_grad()
def step(self, parameters: Iterable[torch.nn.Parameter], global_step: int = None):
if not should_update_ema(self.args, global_step):
return
if self.args.ema_device == "cpu" and not self.args.ema_cpu_only:
# Move EMA to accelerator for faster update.
self.to(device=self.accelerator.device, non_blocking=True)
if isinstance(parameters, torch.nn.Module):
deprecation_message = (
"Passing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. "
"Please pass the parameters of the module instead."
)
deprecate(
"passing a `torch.nn.Module` to `ExponentialMovingAverage.step`",
"1.0.0",
deprecation_message,
standard_warn=False,
)
parameters = parameters.parameters()
parameters = list(parameters)
if global_step is not None:
# When we're updating the EMA periodically, we can't trust the counter.
self.optimization_step = global_step
else:
self.optimization_step += 1
# Compute the decay factor for the exponential moving average.
decay = self.get_decay(self.optimization_step)
self.cur_decay_value = decay
one_minus_decay = 1 - decay
context_manager = contextlib.nullcontext
if is_transformers_available() and transformers.integrations.deepspeed.is_deepspeed_zero3_enabled():
import deepspeed
if self.foreach:
if is_transformers_available() and transformers.integrations.deepspeed.is_deepspeed_zero3_enabled():
context_manager = deepspeed.zero.GatheredParameters(parameters, modifier_rank=None)
with context_manager():
params_grad = [param for param in parameters if param.requires_grad]
s_params_grad = [
s_param for s_param, param in zip(self.shadow_params, parameters) if param.requires_grad
]
if len(params_grad) < len(parameters):
torch._foreach_copy_(
[s_param for s_param, param in zip(self.shadow_params, parameters) if not param.requires_grad],
[param for param in parameters if not param.requires_grad],
non_blocking=True,
)
torch._foreach_sub_(
s_params_grad,
torch._foreach_sub(s_params_grad, params_grad),
alpha=one_minus_decay,
)
else:
for s_param, param in zip(self.shadow_params, parameters):
if is_transformers_available() and transformers.integrations.deepspeed.is_deepspeed_zero3_enabled():
context_manager = deepspeed.zero.GatheredParameters(param, modifier_rank=None)
with context_manager():
if param.requires_grad:
s_param.sub_(one_minus_decay * (s_param - param.to(s_param.device)))
else:
s_param.copy_(param)
if self.args.ema_device == "cpu" and not self.args.ema_cpu_only:
# Move back to CPU for safe-keeping.
self.to(device=self.args.ema_device, non_blocking=True)
def copy_to(self, parameters: Iterable[torch.nn.Parameter]) -> None:
"""
Copy current averaged parameters into given collection of parameters.
Args:
parameters: Iterable of `torch.nn.Parameter`; the parameters to be
updated with the stored moving averages. If `None`, the parameters with which this
`ExponentialMovingAverage` was initialized will be used.
"""
parameters = list(parameters)
if self.foreach:
torch._foreach_copy_(
[param.data for param in parameters],
[s_param.to(param.device).data for s_param, param in zip(self.shadow_params, parameters)],
)
else:
for s_param, param in zip(self.shadow_params, parameters):
param.data.copy_(s_param.to(param.device).data)
def pin_memory(self) -> None:
r"""
Move internal buffers of the ExponentialMovingAverage to pinned memory. Useful for non-blocking transfers for
offloading EMA params to the host.
"""
if torch.backends.mps.is_available():
logger.warning("Apple silicon does not support pinned memory. Skipping.")
return
if self.args.ema_cpu_only:
return
# This probably won't work, but we'll do it anyway.
self.shadow_params = [p.pin_memory() for p in self.shadow_params]
def to(self, *args, **kwargs):
for param in self.shadow_params:
param.data = param.data.to(*args, **kwargs)
return self
def cuda(self, device=None):
return self.to(device="cuda" if device is None else f"cuda:{device}")
def cpu(self):
return self.to(device="cpu")
def state_dict(self, destination=None, prefix="", keep_vars=False, exclude_params: bool = False):
r"""
Returns a dictionary containing a whole state of the EMA model.
"""
state_dict = {
"decay": self.decay,
"min_decay": self.min_decay,
"optimization_step": self.optimization_step,
"update_after_step": self.update_after_step,
"use_ema_warmup": self.use_ema_warmup,
"inv_gamma": self.inv_gamma,
"power": self.power,
}
if exclude_params:
return state_dict
for idx, param in enumerate(self.shadow_params):
state_dict[f"{prefix}shadow_params.{idx}"] = param if keep_vars else param.detach()
return state_dict
def store(self, parameters: Iterable[torch.nn.Parameter]) -> None:
r"""
Save the current parameters for restoring later.
"""
self.temp_stored_params = [param.detach().cpu().clone() for param in parameters]
def restore(self, parameters: Iterable[torch.nn.Parameter]) -> None:
r"""
Restore the parameters stored with the `store` method.
"""
if self.temp_stored_params is None:
raise RuntimeError("This ExponentialMovingAverage has no `store()`ed weights " "to `restore()`")
if self.foreach:
torch._foreach_copy_(
[param.data for param in parameters],
[c_param.data for c_param in self.temp_stored_params],
)
else:
for c_param, param in zip(self.temp_stored_params, parameters):
param.data.copy_(c_param.data)
# Better memory-wise.
self.temp_stored_params = None
def parameter_count(self) -> int:
return sum(p.numel() for p in self.shadow_params)
# Implementing nn.Module methods to emulate its behavior
def named_children(self):
# No child modules
return iter([])
def children(self):
return iter([])
def modules(self):
yield self
def named_modules(self, memo=None, prefix=""):
yield prefix, self
def parameters(self, recurse=True):
return iter(self.shadow_params)
def named_parameters(self, prefix="", recurse=True):
for i, param in enumerate(self.shadow_params):
name = f"{prefix}shadow_params.{i}"
yield name, param
def buffers(self, recurse=True):
return iter([])
def named_buffers(self, prefix="", recurse=True):
return iter([])
def train(self, mode=True):
self.training = mode
return self
def eval(self):
return self.train(False)
def zero_grad(self):
# No gradients to zero in EMA model
pass