-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidecar.py
358 lines (324 loc) · 11 KB
/
sidecar.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
import contextlib
import io
import os
import pickle
import statistics
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable, Dict, Generator, Iterable, Optional, Tuple
import numpy as np
import optax
import yaml
from chex import ArrayTree, PRNGKey
from jax import Array
from wandb.sdk.wandb_run import Run as WandbRun
import wandb
from .common import Config, get_logger
from .training import EndOfTraining, Event, Save, TrainStep
logger = get_logger()
def accumulate_gac_steps(
*,
events: Iterable[Event],
) -> Iterable[Event]:
"""Accumulate gradient accumulation steps. This way, only the last
step of the accumulation is propagated, with the mean loss over all
of its sub-steps."""
losses = []
gradients = None
params = None
gradients_finite = True
for event in events:
if isinstance(event, EndOfTraining):
yield event
return
if not isinstance(event, TrainStep):
yield event
continue
losses.append(event.loss) # Take the mean of the losses.
gradients_finite = gradients_finite and event.gradients_finite # All finite.
gradients = event.gradients or gradients # Only keep the last gradients.
params = event.params or params # Only keep the last params.
if not event.has_updated:
continue
yield TrainStep(
step=event.step,
has_updated=True,
loss=statistics.mean(losses),
gradients_finite=gradients_finite,
loss_scale_log2=event.loss_scale_log2,
gradients=gradients,
params=params,
timestamp=event.timestamp,
)
losses = []
gradients = None
params = None
gradients_finite = True
def log_losses(
*,
events: Iterable[Event],
frequency: int,
log_fn: Callable[[str], None] = logger.info,
) -> Iterable[Event]:
"""Log the mean loss and standard deviation over the last `frequency`
steps."""
losses = []
for event in events:
if isinstance(event, EndOfTraining):
yield event
return
if not isinstance(event, TrainStep):
yield event
continue
losses.append(event.loss)
if event.step % frequency == 0 and losses:
mean, std = statistics.mean(losses), statistics.pstdev(losses)
items = (
f"Step: {event.step:>6}",
f"Loss: {mean:0.6f} ± {std:0.6f}",
)
log_fn(" | ".join(items))
losses = []
if not event.gradients_finite:
logger.info(
f"Step: {event.step:>6} | Non-finite gradients, "
f"loss scale (log2): {event.loss_scale_log2}"
)
yield event
def log_time_per_step(
*,
events: Iterable[Event],
frequency: int,
percentiles: Iterable[int],
log_fn: Callable[[str], None] = logger.info,
) -> Iterable[Event]:
"""Log the time per step over the last `frequency` steps. Concretely, log
the given percentiles of the time between each step."""
if frequency < 100:
raise ValueError(f"Expected frequency to be at least 100, got {frequency}")
percentiles = tuple(percentiles)
timestamps = []
for event in events:
if isinstance(event, EndOfTraining):
yield event
return
if not isinstance(event, TrainStep):
yield event
continue
timestamps.append(event.timestamp)
if (
event.has_updated
and event.step % frequency == 0
and len(timestamps) >= max(percentiles)
):
deltas = [b - a for a, b in zip(timestamps[:-1], timestamps[1:])]
deltas_seconds = [delta.total_seconds() for delta in deltas]
points = statistics.quantiles(deltas_seconds, n=101, method="inclusive")
points = [points[p] for p in percentiles]
points_str = ", ".join(
f"{p}%: {t:0.4f}s" for p, t in zip(percentiles, points)
)
items = (
f"Step: {event.step:>6}",
"s/step: " + points_str,
)
log_fn(" | ".join(items))
yield event
def detect_anomalies(
events: Iterable[Event],
) -> Iterable[Event]:
"""Detect anomalies in the gradients and loss such as NaNs and infs."""
for event in events:
if isinstance(event, EndOfTraining):
yield event
return
if not isinstance(event, TrainStep):
yield event
continue
if np.isnan(event.loss):
logger.error(f"Loss is NaN at step {event.step}")
if np.isinf(event.loss):
logger.error(f"Loss is infinite at step {event.step}")
if event.gradients is not None:
non_finites = _find_anomalies(event.gradients)
for key, reason in non_finites:
logger.error(f"Gradient {key} {reason} at step {event.step}")
yield event
def _find_anomalies(
x: ArrayTree,
prefix: str = "",
) -> Iterable[Tuple[str, str]]:
if isinstance(x, (np.ndarray, Array)):
if np.isnan(x).any():
yield prefix, "contains NaNs"
if np.isinf(x).any():
yield prefix, "contains infinities"
if np.std(x) == 0:
yield prefix, "has zero std"
elif isinstance(x, dict):
for key, value in x.items():
yield from _find_anomalies(value, prefix + f"{key}.")
elif isinstance(x, Iterable):
for i, value in enumerate(x):
yield from _find_anomalies(value, prefix + f"{i}.")
else:
raise TypeError(f"Unexpected type {type(x)}")
@contextlib.contextmanager
def atomic_open(path: Path, mode: str = "w") -> Generator[io.IOBase, None, None]:
f = tempfile.NamedTemporaryFile(mode=mode, dir=path.parent, delete=False)
try:
yield f # type: ignore
f.close()
os.replace(f.name, path)
finally:
if os.path.exists(f.name):
os.remove(f.name)
def save_to_directory(
*,
events: Iterable[Event],
) -> Iterable[Event]:
"""Save the parameters, optimizer state, etc. to the directory specified
in the `Save` event."""
for event in events:
if isinstance(event, EndOfTraining):
yield event
return
if not isinstance(event, Save):
yield event
continue
path = Path(event.path)
path.mkdir(parents=True, exist_ok=True)
event.config.to_yaml(path / "config.yaml")
with atomic_open(path / "params.pkl", "wb") as f:
pickle.dump(event.params, f)
with atomic_open(path / "opt_state.pkl", "wb") as f:
pickle.dump(event.opt_state, f)
with atomic_open(path / "rng_key.pkl", "wb") as f:
pickle.dump(event.rng_key, f)
with atomic_open(path / "step.txt", "w") as f:
f.write(str(event.step))
with atomic_open(path / "seed.txt", "w") as f:
f.write(str(event.seed))
path.parent.mkdir(parents=True, exist_ok=True)
logger.info(f"Step: {event.step:>6} | Saved model to {path}")
yield event
@dataclass
class LoadResultForInference:
config: Config
params: ArrayTree
@dataclass
class LoadResult(LoadResultForInference):
step: int
seed: int
rng_key: PRNGKey
opt_state: optax.MultiStepsState
def load_from_directory_for_inference(
*,
path: Path,
) -> LoadResultForInference:
"""Load the parameters and config from the directory specified in the `Load` event."""
config = Config.from_yaml(path / "config.yaml")
with open(path / "params.pkl", "rb") as f:
params = pickle.load(f)
assert isinstance(params, dict)
return LoadResultForInference(
config=config,
params=params,
)
def load_from_directory(
*,
path: Path,
) -> LoadResult:
"""Load the parameters, optimiser state, config etc. from the directory
specified."""
config = Config.from_yaml(path / "config.yaml")
with open(path / "params.pkl", "rb") as f:
params = pickle.load(f)
assert isinstance(params, dict)
with open(path / "opt_state.pkl", "rb") as f:
opt_state = pickle.load(f)
assert isinstance(opt_state, optax.MultiStepsState)
with open(path / "rng_key.pkl", "rb") as f:
rng_key = pickle.load(f)
with open(path / "step.txt") as f:
step = int(f.read().strip())
with open(path / "seed.txt") as f:
seed = int(f.read().strip())
return LoadResult(
config=config,
step=step,
seed=seed,
rng_key=rng_key,
params=params,
opt_state=opt_state,
)
def log_to_wandb(
*,
events: Iterable[Event],
run: WandbRun,
) -> Iterable[Event]:
"""Log the parameters, gradients, loss, etc. to wandb."""
for event in events:
if isinstance(event, EndOfTraining):
yield event
return
if isinstance(event, Save):
event.path.mkdir(parents=True, exist_ok=True)
with atomic_open(event.path / "wandb-run.yaml", "w") as f:
yaml.dump(dict(id=run.id, project=run.project, group=run.group), f)
elif isinstance(event, TrainStep):
data: Dict[str, Any] = dict(loss=event.loss)
if event.gradients is not None and event.gradients_finite:
tuples = _to_histograms(event.gradients, "/")
data["gradients"] = dict(tuples)
if event.params is not None:
tuples = _to_histograms(event.params, "/")
data["params"] = dict(tuples)
run.log(data, step=event.step)
yield event
def load_wandb_run(
*,
path: Path,
) -> WandbRun:
with open(path / "wandb-run.yaml") as f:
run_data = dict(yaml.safe_load(f))
run = wandb.init(**run_data, resume="must")
assert isinstance(run, WandbRun)
return run
def new_wandb_run(
*,
project: Optional[str] = None,
tags: Iterable[str] = [],
group: Optional[str] = None,
name: Optional[str] = None,
notes: Optional[str] = None,
) -> WandbRun:
run = wandb.init(
project=project,
group=group,
tags=tuple(tags),
name=name,
notes=notes,
)
assert isinstance(run, WandbRun)
return run
def _to_histograms(
x: ArrayTree,
prefix: str = "",
bins: int = 64,
) -> Iterable[Tuple[str, wandb.Histogram]]:
if isinstance(x, (np.ndarray, Array)):
x = np.asarray(x)
x = x.flatten()
hist = np.histogram(x, bins=bins)
yield prefix, wandb.Histogram(np_histogram=hist)
elif isinstance(x, dict):
for key, value in x.items():
key = str(key).replace("/", ".").replace(" ", "_")
yield from _to_histograms(value, prefix=f"{prefix}.{key}", bins=bins)
elif isinstance(x, Iterable):
for i, value in enumerate(x):
yield from _to_histograms(value, prefix=f"{prefix}.{i}", bins=bins)
else:
raise TypeError(f"Expected x to be an array dict or iterable, got {type(x)}")