forked from IEIT-AGI/MIX-Kalman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_engine.py
55 lines (43 loc) · 1.42 KB
/
base_engine.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
import weakref
from abc import ABCMeta, abstractmethod
from mixk.engine.hooks import HookBase
import logging
class EngineBase(metaclass=ABCMeta):
"""Base class for mixk engine."""
def __init__(self):
self._hooks: list[HookBase] = []
self.logger = logging.getLogger(__name__)
def register_hooks(self, engine_hooks):
for hk in engine_hooks:
if hk is not None:
assert isinstance(hk, HookBase), 'the current hook object must be a HookBase subclass!'
hk.trainer = weakref.proxy(self)
self._hooks.append(hk)
def before_train(self):
for hk in self._hooks:
hk.before_train()
def after_train(self):
for hk in self._hooks:
hk.after_train()
def before_train_iter(self):
for hk in self._hooks:
hk.before_train_iter()
def after_train_iter(self):
for hk in self._hooks:
hk.after_train_iter()
self.log_buffer.step()
if self.by_epoch:
self.log_buffer.epoch_iter(self.inner_iter)
def before_train_epoch(self):
for hk in self._hooks:
hk.before_train_epoch()
def after_train_epoch(self):
for hk in self._hooks:
hk.after_train_epoch()
self.log_buffer.epoch_step()
@abstractmethod
def run_train_epoch(self):
pass
@abstractmethod
def run_train_iter(self):
pass