/
protocol.py
574 lines (479 loc) · 17.4 KB
/
protocol.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import math
import torch
import numpy as np
import torch.nn as nn
from abc import abstractmethod
from abc import ABC
from abc import ABCMeta
from copy import deepcopy
from tqdm import tqdm
from typing import Any
from typing import Dict
from typing import List
from typing import Type
from typing import Union
from typing import Optional
from typing import NamedTuple
from onnxruntime import InferenceSession
from cftool.misc import shallow_copy_dict
from cftool.misc import context_error_handler
from .types import losses_type
from .types import np_dict_type
from .types import tensor_dict_type
from .constants import LOSS_KEY
from .constants import LABEL_KEY
from .misc.toolkit import to_numpy
from .misc.toolkit import to_device
from .misc.toolkit import to_standard
from .misc.toolkit import eval_context
from .misc.toolkit import WithRegister
data_dict: Dict[str, Type["DataProtocol"]] = {}
loader_dict: Dict[str, Type["DataLoaderProtocol"]] = {}
model_dict: Dict[str, Type["ModelProtocol"]] = {}
monitor_dict: Dict[str, Type["TrainerMonitor"]] = {}
loss_dict: Dict[str, Type["LossProtocol"]] = {}
metric_dict: Dict[str, Type["MetricProtocol"]] = {}
# data
class DataProtocol(ABC, WithRegister):
d: Dict[str, Type["DataProtocol"]] = data_dict
def __init__(self, *args: Any, **kwargs: Any):
pass
@abstractmethod
def __len__(self) -> int:
pass
class DataLoaderProtocol(ABC, WithRegister):
d: Dict[str, Type["DataLoaderProtocol"]] = loader_dict
data: DataProtocol
batch_size: int
def __init__(self, *, sample_weights: Optional[np.ndarray] = None):
self.sample_weights = sample_weights
@abstractmethod
def __iter__(self) -> "DataLoaderProtocol":
pass
@abstractmethod
def __next__(self) -> tensor_dict_type:
pass
def __len__(self) -> int:
return math.ceil(len(self.data) / self.batch_size)
def copy(self) -> "DataLoaderProtocol":
return deepcopy(self)
# model
class ModelProtocol(nn.Module, WithRegister, metaclass=ABCMeta):
d: Dict[str, Type["ModelProtocol"]] = model_dict
def __init__(self, *args: Any, **kwargs: Any):
super().__init__()
@property
def device(self) -> torch.device:
return list(self.parameters())[0].device
def _init_with_trainer(self, trainer: Any) -> None:
pass
@abstractmethod
def forward(
self,
batch_idx: int,
batch: tensor_dict_type,
state: Optional["TrainerState"] = None,
**kwargs: Any,
) -> tensor_dict_type:
pass
def summary_forward(self, batch_idx: int, batch: tensor_dict_type) -> None:
self.forward(batch_idx, batch)
class StepOutputs(NamedTuple):
forward_results: tensor_dict_type
loss_dict: tensor_dict_type
class MetricsOutputs(NamedTuple):
final_score: float
metric_values: Dict[str, float]
class InferenceOutputs(NamedTuple):
forward_results: np_dict_type
labels: Optional[np.ndarray]
metric_outputs: Optional[MetricsOutputs]
loss_items: Optional[Dict[str, float]]
class ModelWithCustomSteps(ModelProtocol, metaclass=ABCMeta):
custom_train_step: bool = True
custom_evaluate_step: bool = True
@abstractmethod
def train_step(
self,
batch_idx: int,
batch: tensor_dict_type,
trainer: Any,
forward_kwargs: Dict[str, Any],
loss_kwargs: Dict[str, Any],
) -> StepOutputs:
pass
@abstractmethod
def evaluate_step(
self,
loader: DataLoaderProtocol,
portion: float,
trainer: Any,
) -> MetricsOutputs:
pass
# trainer
class TrainerState:
def __init__(
self,
loader: DataLoaderProtocol,
*,
num_epoch: int,
max_epoch: int,
extension: int = 5,
enable_logging: bool = True,
min_num_sample: int = 3000,
snapshot_start_step: Optional[int] = None,
max_snapshot_file: int = 5,
num_snapshot_per_epoch: int = 2,
num_step_per_log: int = 350,
num_step_per_snapshot: Optional[int] = None,
max_step_per_snapshot: int = 2000,
):
self.step = self.epoch = 0
self.batch_size = loader.batch_size
self.num_step_per_epoch = len(loader)
self.num_epoch = num_epoch
self.max_epoch = max_epoch
self.extension = extension
self.enable_logging = enable_logging
if snapshot_start_step is None:
snapshot_start_step = math.ceil(min_num_sample / self.batch_size)
self.snapshot_start_step = snapshot_start_step
self.max_snapshot_file = max_snapshot_file
self.num_step_per_log = num_step_per_log
if num_step_per_snapshot is None:
num_step_per_snapshot = max(1, int(len(loader) / num_snapshot_per_epoch))
num_step_per_snapshot = min(max_step_per_snapshot, num_step_per_snapshot)
self.num_step_per_snapshot = num_step_per_snapshot
def set_terminate(self) -> None:
self.step = self.epoch = -1
@property
def is_terminate(self) -> bool:
return self.epoch == -1
@property
def should_train(self) -> bool:
return self.epoch < self.num_epoch
@property
def should_monitor(self) -> bool:
return self.step % self.num_step_per_snapshot == 0
@property
def should_log_lr(self) -> bool:
if not self.enable_logging:
return False
denominator = min(self.num_step_per_epoch, 10)
return self.step % denominator == 0
@property
def should_log_losses(self) -> bool:
if not self.enable_logging:
return False
patience = max(4, int(round(self.num_step_per_epoch / 50.0)))
denominator = min(self.num_step_per_epoch, patience)
return self.step % denominator == 0
@property
def should_log_artifacts(self) -> bool:
return self.should_log_metrics_msg
@property
def should_log_metrics_msg(self) -> bool:
if not self.enable_logging:
return False
if self.is_terminate:
return True
min_period = math.ceil(self.num_step_per_log / self.num_step_per_snapshot)
period = max(1, int(min_period)) * self.num_step_per_snapshot
return self.step % period == 0
@property
def should_start_snapshot(self) -> bool:
return self.step >= self.snapshot_start_step
@property
def should_extend_epoch(self) -> bool:
return self.epoch == self.num_epoch and self.epoch < self.max_epoch
@property
def reached_max_epoch(self) -> bool:
return self.epoch > self.max_epoch
@property
def disable_logging(self) -> context_error_handler:
class _(context_error_handler):
def __init__(self, state: TrainerState):
self.state = state
self.enabled = state.enable_logging
def __enter__(self) -> None:
self.state.enable_logging = False
def _normal_exit(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
self.state.enable_logging = self.enabled
return _(self)
class TrainerMonitor(ABC, WithRegister):
d: Dict[str, Type["TrainerMonitor"]] = monitor_dict
def __init__(self, *args: Any, **kwargs: Any):
pass
@abstractmethod
def snapshot(self, new_score: float) -> bool:
pass
@abstractmethod
def check_terminate(self, new_score: float) -> bool:
pass
@abstractmethod
def punish_extension(self) -> None:
pass
def handle_extension(self, state: TrainerState) -> None:
if state.should_extend_epoch:
self.punish_extension()
new_epoch = state.num_epoch + state.extension
state.num_epoch = min(new_epoch, state.max_epoch)
class MonitorResults(NamedTuple):
terminate: bool
save_checkpoint: bool
metric_outputs: Optional[MetricsOutputs]
# loss
class LossProtocol(nn.Module, WithRegister, metaclass=ABCMeta):
d: Dict[str, Type["LossProtocol"]] = loss_dict
def __init__(
self,
config: Optional[Dict[str, Any]] = None,
reduction: str = "mean",
):
super().__init__()
self.config = config or {}
self._init_config()
self._reduction = reduction
def _init_config(self) -> None:
pass
def _reduce(self, losses: torch.Tensor) -> torch.Tensor:
if self._reduction == "none":
return losses
if self._reduction == "mean":
return losses.mean()
if self._reduction == "sum":
return losses.sum()
raise NotImplementedError(f"reduction '{self._reduction}' is not implemented")
@abstractmethod
def _core(
self,
forward_results: tensor_dict_type,
batch: tensor_dict_type,
state: Optional[TrainerState] = None,
**kwargs: Any,
) -> losses_type:
# return losses without reduction
pass
def forward(
self,
forward_results: tensor_dict_type,
batch: tensor_dict_type,
state: Optional[TrainerState] = None,
) -> tensor_dict_type:
losses = self._core(forward_results, batch, state)
if isinstance(losses, torch.Tensor):
return {LOSS_KEY: self._reduce(losses)}
# requires returns a value with LOSS_KEY as its key
return {k: self._reduce(v) for k, v in losses.items()}
# inference
class ONNX:
def __init__(
self,
*,
onnx_path: str,
output_names: List[str],
):
self.ort_session = InferenceSession(onnx_path)
self.output_names = output_names
def inference(self, new_inputs: np_dict_type) -> np_dict_type:
if self.ort_session is None:
raise ValueError("`onnx_path` is not provided")
ort_inputs = {
node.name: to_standard(new_inputs[node.name])
for node in self.ort_session.get_inputs()
}
return dict(zip(self.output_names, self.ort_session.run(None, ort_inputs)))
class InferenceProtocol:
def __init__(
self,
*,
onnx: Optional[ONNX] = None,
model: Optional[ModelProtocol] = None,
use_grad_in_predict: bool = False,
):
self.onnx = onnx
self.model = model
if onnx is None and model is None:
raise ValueError("either `onnx` or `model` should be provided")
if onnx is not None and model is not None:
raise ValueError("only one of `onnx` & `model` should be provided")
self.use_grad_in_predict = use_grad_in_predict
def get_outputs(
self,
loader: DataLoaderProtocol,
*,
portion: float = 1.0,
state: Optional[TrainerState] = None,
metrics: Optional["MetricProtocol"] = None,
loss: Optional[LossProtocol] = None,
return_outputs: bool = True,
use_tqdm: bool = False,
**kwargs: Any,
) -> InferenceOutputs:
def _core() -> InferenceOutputs:
results: Dict[str, Optional[List[np.ndarray]]] = {}
metric_outputs_list: List[MetricsOutputs] = []
loss_items: Dict[str, List[float]] = {}
labels = []
iterator = enumerate(loader)
if use_tqdm:
iterator = tqdm(list(iterator), desc="inference")
requires_all_outputs = return_outputs
if metrics is not None and metrics.requires_all:
requires_all_outputs = True
for i, batch in iterator:
if i / len(loader) >= portion:
break
np_batch = {
batch_key: None if batch_tensor is None else to_numpy(batch_tensor)
for batch_key, batch_tensor in batch.items()
}
if self.model is not None:
batch = to_device(batch, self.model.device)
local_labels = batch[LABEL_KEY]
if local_labels is not None:
if not isinstance(local_labels, np.ndarray):
local_labels = to_numpy(local_labels)
labels.append(local_labels)
if self.onnx is not None:
local_outputs = self.onnx.inference(np_batch)
else:
assert self.model is not None
with eval_context(self.model, use_grad=use_grad):
assert not self.model.training
local_outputs = self.model(
i,
batch,
state,
**shallow_copy_dict(kwargs),
)
# gather outputs
requires_metrics = metrics is not None and not metrics.requires_all
requires_np = requires_metrics or requires_all_outputs
np_outputs: np_dict_type = {}
for k, v in local_outputs.items():
if not requires_np:
results[k] = None
continue
if v is None:
continue
if isinstance(v, np.ndarray):
v_np = v
elif isinstance(v, torch.Tensor):
v_np = to_numpy(v)
elif isinstance(v, list):
if isinstance(v[0], np.ndarray):
v_np = v
else:
v_np = list(map(to_numpy, v))
else:
raise ValueError(f"unrecognized value ({k}={type(v)}) occurred")
np_outputs[k] = v_np
if not requires_all_outputs:
results[k] = None
else:
results.setdefault(k, []).append(v_np) # type: ignore
# metrics
if requires_metrics:
sub_outputs = metrics.evaluate(np_batch, np_outputs) # type: ignore
metric_outputs_list.append(sub_outputs)
# loss
if loss is not None:
with eval_context(loss, use_grad=use_grad):
local_losses = loss(local_outputs, batch)
for k, v in local_losses.items():
loss_items.setdefault(k, []).append(v.item())
# gather outputs
final_results: Dict[str, Union[np.ndarray, Any]]
if not requires_all_outputs:
final_results = {k: None for k in results}
else:
final_results = {
batch_key: np.vstack(batch_results)
if isinstance(batch_results[0], np.ndarray)
else [
np.vstack([batch[i] for batch in batch_results])
for i in range(len(batch_results[0]))
]
for batch_key, batch_results in results.items()
if batch_results is not None
}
# gather metric outputs
if metrics is None:
metric_outputs = None
elif metrics.requires_all:
metric_outputs = metrics.evaluate(
{LABEL_KEY: np.vstack(labels)},
final_results,
)
else:
scores = []
metric_values: Dict[str, List[float]] = {}
for sub_outputs in metric_outputs_list:
scores.append(sub_outputs.final_score)
for k, v in sub_outputs.metric_values.items():
metric_values.setdefault(k, []).append(v)
metric_outputs = MetricsOutputs(
sum(scores) / len(scores),
{k: sum(vl) / len(vl) for k, vl in metric_values.items()},
)
return InferenceOutputs(
final_results,
None if not labels else np.vstack(labels),
metric_outputs,
None
if not loss_items
else {k: sum(v) / len(v) for k, v in loss_items.items()},
)
use_grad = kwargs.pop("use_grad", self.use_grad_in_predict)
try:
return _core()
except:
use_grad = self.use_grad_in_predict = True
return _core()
# metrics
class MetricProtocol(ABC, WithRegister):
d: Dict[str, Type["MetricProtocol"]] = metric_dict
trainer: Any
def __init__(self, *args: Any, **kwargs: Any):
pass
@property
@abstractmethod
def is_positive(self) -> bool:
pass
@abstractmethod
def _core(
self,
np_batch: np_dict_type,
np_outputs: np_dict_type,
loader: Optional[DataLoaderProtocol],
) -> float:
pass
@property
def requires_all(self) -> bool:
return False
def evaluate(
self,
np_batch: np_dict_type,
np_outputs: np_dict_type,
loader: Optional[DataLoaderProtocol] = None,
) -> MetricsOutputs:
metric = self._core(np_batch, np_outputs, loader)
score = metric * (1.0 if self.is_positive else -1.0)
return MetricsOutputs(score, {self.__identifier__: metric})
__all__ = [
"data_dict",
"loader_dict",
"loss_dict",
"DataProtocol",
"DataLoaderProtocol",
"ModelProtocol",
"TrainerState",
"TrainerMonitor",
"MonitorResults",
"LossProtocol",
"InferenceOutputs",
"InferenceProtocol",
"MetricsOutputs",
"MetricProtocol",
]