-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
base.py
278 lines (209 loc) · 8.3 KB
/
base.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
import logging
from collections import defaultdict
from inspect import isclass, signature
from itertools import count
from queue import Queue
from typing import (
Any,
Callable,
Dict,
Iterator,
List,
NewType,
Optional,
Sequence,
Set,
Type,
Union,
cast,
)
log = logging.getLogger("flair")
PluginArgument = Union["BasePlugin", Type["BasePlugin"]]
HookHandleId = NewType("HookHandleId", int)
EventIdenifier = str
class TrainingInterrupt(Exception):
"""Allows plugins to interrupt the training loop."""
class Pluggable:
"""Dispatches events which attached plugins can react to."""
valid_events: Optional[Set[EventIdenifier]] = None
def __init__(self, *, plugins: Sequence[PluginArgument] = []) -> None:
"""Initialize a `Pluggable`.
Args:
plugins: Plugins which should be attached to this `Pluggable`.
"""
self._hook_handles: Dict[EventIdenifier, Dict[HookHandleId, HookHandle]] = defaultdict(dict)
self._hook_handle_id_counter = count()
self._plugins: List[BasePlugin] = []
# This flag tracks, whether an event is currently being processed (otherwise it is added to the queue)
self._processing_events = False
self._event_queue: Queue = Queue()
for plugin in plugins:
if isclass(plugin):
# instantiate plugin
plugin = plugin()
plugin = cast("BasePlugin", plugin)
plugin.attach_to(self)
@property
def plugins(self):
return self._plugins
def append_plugin(self, plugin):
self._plugins.append(plugin)
def validate_event(self, *events: EventIdenifier):
for event in events:
assert isinstance(event, EventIdenifier)
if self.valid_events is not None and event not in self.valid_events:
raise RuntimeError(f"Event '{event}' not recognized. Available: {', '.join(self.valid_events)}")
return event
return None
def register_hook(self, func: Callable, *events: EventIdenifier):
"""Register a hook.
Args:
func: Function to be called when the event is emitted.
*events: List of events to call this function on.
"""
self.validate_event(*events)
handle: HookHandle = HookHandle(
HookHandleId(next(self._hook_handle_id_counter)), events=events, func=func, pluggable=self
)
for event in events:
self._hook_handles[event][handle.id] = handle
return handle
def dispatch(self, event: EventIdenifier, *args, **kwargs) -> None:
"""Call all functions hooked to a certain event."""
self.validate_event(event)
self._event_queue.put((event, args, kwargs))
if not self._processing_events:
try:
self._processing_events = True
while not self._event_queue.empty():
event, args, kwargs = self._event_queue.get()
for hook in self._hook_handles[event].values():
hook(*args, **kwargs)
finally:
# Reset the flag, since an exception event might be dispatched
self._processing_events = False
def remove_hook(self, handle: "HookHandle"):
"""Remove a hook handle from this instance."""
for event in handle.events:
del self._hook_handles[event][handle.id]
class HookHandle:
"""Represents the registration information of a hook callback."""
def __init__(
self, _id: HookHandleId, *, events: Sequence[EventIdenifier], func: Callable, pluggable: Pluggable
) -> None:
"""Intitialize `HookHandle`.
Args:
_id: Id, the callback is stored as in the `Pluggable`.
events: List of events, the callback is registered for.
func: The callback function.
pluggable: The `Pluggable` where the callback is registered.
"""
pluggable.validate_event(*events)
self._id = _id
self._events = events
self._func = func
self._pluggable = pluggable
@property
def id(self) -> HookHandleId:
"""Return the id of this `HookHandle`."""
return self._id
@property
def func_name(self):
return self._func.__qualname__
@property
def events(self) -> Iterator[EventIdenifier]:
"""Return iterator of events whis `HookHandle` is registered for."""
yield from self._events
def remove(self):
"""Remove a hook from the `Pluggable` it is attached to."""
self._pluggable.remove_hook(self)
def __call__(self, *args, **kw):
"""Call the hook this `HookHandle` is associated with."""
try:
return self._func(*args, **kw)
except TypeError as err:
sig = signature(self._func)
if not any(p.kind == p.VAR_KEYWORD for p in sig.parameters.values()):
# If there is no **kw argument in the callback, check if any of the passed kw args is not accepted by
# the callback
for name in kw:
if name not in sig.parameters:
raise TypeError(
f"Hook callback {self.func_name}() does not accept keyword argument '{name}'"
) from err
raise err
class BasePlugin:
"""Base class for all plugins."""
def __init__(self) -> None:
"""Initialize the base plugin."""
self._hook_handles: List[HookHandle] = []
self._pluggable: Optional[Pluggable] = None
def attach_to(self, pluggable: Pluggable):
"""Attach this plugin to a `Pluggable`."""
assert self._pluggable is None
assert len(self._hook_handles) == 0
self._pluggable = pluggable
pluggable.append_plugin(self)
# go through all attributes
for name in dir(self):
try:
func = getattr(self, name)
# get attribute hook events (may raise an AttributeError)
events = func._plugin_hook_events
# register function as a hook
handle = pluggable.register_hook(func, *events)
self._hook_handles.append(handle)
except AttributeError:
continue
def detach(self):
"""Detach a plugin from the `Pluggable` it is attached to."""
assert self._pluggable is not None
for handle in self._hook_handles:
handle.remove()
self._pluggable = None
self._hook_handles = []
@classmethod
def mark_func_as_hook(cls, func: Callable, *events: EventIdenifier) -> Callable:
"""Mark method as a hook triggered by the `Pluggable`."""
if len(events) == 0:
events = (func.__name__,)
func._plugin_hook_events = events # type: ignore[attr-defined]
return func
@classmethod
def hook(
cls,
first_arg: Optional[Union[Callable, EventIdenifier]] = None,
*other_args: EventIdenifier,
) -> Callable:
"""Convience function for `BasePlugin.mark_func_as_hook`).
Enables using the `@BasePlugin.hook` syntax.
Can also be used as:
`@BasePlugin.hook("some_event", "another_event")`
"""
if first_arg is None:
# Decorator was used with parentheses, but no args
return cls.mark_func_as_hook
if isinstance(first_arg, EventIdenifier):
# Decorator was used with args (strings specifiying the events)
def decorator_func(func: Callable):
return cls.mark_func_as_hook(func, cast(EventIdenifier, first_arg), *other_args)
return decorator_func
# Decorator was used without args
return cls.mark_func_as_hook(first_arg, *other_args)
@property
def pluggable(self) -> Optional[Pluggable]:
return self._pluggable
def __str__(self) -> str:
return self.__class__.__name__
def get_state(self) -> Dict[str, Any]:
return {"__cls__": f"{self.__module__}.{self.__class__.__name__}"}
class TrainerPlugin(BasePlugin):
@property
def trainer(self):
return self.pluggable
@property
def model(self):
return self.trainer.model
@property
def corpus(self):
return self.trainer.corpus