-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathsession.py
292 lines (252 loc) · 10.4 KB
/
session.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
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Union
from loguru import logger
from llmcompressor.core.events import EventType
from llmcompressor.core.helpers import log_model_info, should_log_model_info
from llmcompressor.core.lifecycle import CompressionLifecycle
from llmcompressor.core.state import ModifiedState, State
from llmcompressor.metrics import BaseLogger, LoggerManager
from llmcompressor.recipe import Recipe
__all__ = [
"CompressionSession",
]
@dataclass
class _CallbackContainer:
"""
A container for a callback and its deregister function
:param id_: the id of the callback
:param callback: the callback to invoke
:param deregister: the function to call to deregister the callback
:param event_type: the event type the callback is registered for
:param kwargs: the kwargs the callback was registered with
"""
id_: int
callback: Callable
deregister: Callable
event_type: EventType
kwargs: dict
class CompressionSession:
"""
A session for compression that holds the lifecycle
and state for the current compression session
"""
def __init__(self):
self._lifecycle = CompressionLifecycle()
@property
def lifecycle(self) -> CompressionLifecycle:
"""
Lifecycle is used to keep track of where we are in the compression
process and what modifiers are active. It also provides the ability
to invoke events on the lifecycle.
:return: the lifecycle for the session
"""
return self._lifecycle
@property
def state(self) -> State:
"""
State of the current compression session. State instance
is used to store all information such as the recipe, model
optimizer, data, etc. that is needed for compression.
:return: the current state of the session
"""
return self._lifecycle.state
def initialize(
self,
recipe: Union[str, List[str], "Recipe", List["Recipe"], None] = None,
recipe_stage: Union[str, List[str], None] = None,
recipe_args: Union[Dict[str, Any], None] = None,
model: Optional[Any] = None,
teacher_model: Optional[Any] = None,
optimizer: Optional[Any] = None,
attach_optim_callbacks: bool = True,
train_data: Optional[Any] = None,
val_data: Optional[Any] = None,
test_data: Optional[Any] = None,
calib_data: Optional[Any] = None,
copy_data: bool = True,
start: Optional[float] = None,
steps_per_epoch: Optional[int] = None,
batches_per_step: Optional[int] = None,
loggers: Union[None, LoggerManager, List[BaseLogger]] = None,
**kwargs,
) -> ModifiedState:
"""
Initialize the session for compression. This will run the initialize method
for each modifier in the session's lifecycle. This will also set the session's
state to the initialized state.
:param recipe: the recipe to use for the compression, can be a path to a
recipe file, a raw recipe string, a recipe object, or a list
of recipe objects.
:param recipe_stage: the stage to target for the compression
:param recipe_args: the args to use for overriding the recipe defaults
:param model: the model to compress
:param teacher_model: the teacher model to use for knowledge distillation
:param optimizer: the optimizer to use for the compression
:param attach_optim_callbacks: True to attach the optimizer callbacks to the
compression lifecycle, False otherwise
:param train_data: the training data to use for the compression
:param val_data: the validation data to use for the compression
:param test_data: the testing data to use for the compression
:param calib_data: the calibration data to use for the compression
:param copy_data: True to copy the data, False otherwise
:param start: the start epoch to use for the compression
:param steps_per_epoch: the number of steps per epoch to use for the
compression
:param batches_per_step: the number of batches per step to use for
compression
:param loggers: the metrics manager to setup logging important info
and milestones to, also accepts a list of BaseLogger(s)
:param kwargs: additional kwargs to pass to the lifecycle's initialize method
:return: the modified state of the session after initializing
"""
mod_data = self._lifecycle.initialize(
recipe=recipe,
recipe_stage=recipe_stage,
recipe_args=recipe_args,
model=model,
teacher_model=teacher_model,
optimizer=optimizer,
attach_optim_callbacks=attach_optim_callbacks,
train_data=train_data,
val_data=val_data,
test_data=test_data,
calib_data=calib_data,
copy_data=copy_data,
start=start,
steps_per_epoch=steps_per_epoch,
batches_per_step=batches_per_step,
loggers=loggers,
**kwargs,
)
return ModifiedState(
model=self.state.model,
optimizer=self.state.optimizer,
loss=self.state.loss,
modifier_data=mod_data,
)
def finalize(self, **kwargs) -> ModifiedState:
"""
Finalize the session for compression. This will run the finalize method
for each modifier in the session's lifecycle. This will also set the session's
state to the finalized state.
:param kwargs: additional kwargs to pass to the lifecycle's finalize method
:return: the modified state of the session after finalizing
"""
mod_data = self._lifecycle.finalize(**kwargs)
return ModifiedState(
model=self.state.model,
optimizer=self.state.optimizer,
loss=self.state.loss,
modifier_data=mod_data,
)
def apply(self, **kwargs):
"""
Apply the recipe in one-shot manner. This will invoke the initialize
and then finalize methods for each modifier in the session's lifecycle.
This will also set the session's state to the finalized state.
:param kwargs: additional kwargs to pass to the lifecycle's initialize and
finalize methods
"""
self.initialize(**kwargs)
return self.finalize(**kwargs)
def event(
self,
event_type: EventType,
batch_data: Optional[Any] = None,
loss: Optional[Any] = None,
**kwargs,
) -> ModifiedState:
"""
Invoke an event for current CompressionSession.
:param event_type: the event type to invoke
:param batch_data: the batch data to use for the event
:param loss: the loss to use for the event if any
:param kwargs: additional kwargs to pass to the lifecycle's event method
:return: the modified state of the session after invoking the event
"""
mod_data = self._lifecycle.event(
event_type=event_type, batch_data=batch_data, loss=loss, **kwargs
)
return ModifiedState(
model=self.state.model,
optimizer=self.state.optimizer,
loss=self.state.loss, # TODO: is this supposed to be a different type?
modifier_data=mod_data,
)
def log(self, event_type: EventType, loss: Optional[Any] = None):
"""
Log model and loss information for the current event type
:param event_type: the event type to log for
:param loss: the loss to log if any
"""
self._log_model_info()
self._log_loss(event_type=event_type, loss=loss)
def reset(self):
"""
Reset the session to its initial state
"""
self._lifecycle.reset()
def reset_stage(self):
"""
Reset the session for starting a new stage, recipe and model stays intact
"""
self.lifecycle.initialized_ = False
self.lifecycle.finalized = False
def get_serialized_recipe(self) -> Optional[str]:
"""
:return: serialized string of the current compiled recipe
"""
recipe = self.lifecycle.recipe_container.compiled_recipe
if recipe is not None and hasattr(recipe, "yaml"):
return recipe.yaml()
logger.warning("Recipe not found in session - it may have been reset")
def _log_model_info(self):
# Log model level logs if cadence reached
event_lifecycle = self._lifecycle.event_lifecycle
if event_lifecycle is None:
# event lifecycle not available
# when recipe is not provided
return
epoch = event_lifecycle.current_index
if (
should_log_model_info(
model=self.state.model,
loggers=self.state.loggers,
current_log_step=epoch,
last_log_step=self.state._last_log_step,
)
and self.state.loggers.frequency_manager.is_epoch_frequency_manager
):
log_model_info(
state=self.state,
current_log_step=epoch,
)
# update last log epoch
self.state.loggers.log_written(epoch)
def _log_loss(self, event_type: EventType, loss: Any):
if event_type != EventType.LOSS_CALCULATED:
# only log loss when loss is calculated
return
event_lifecycle = self._lifecycle.event_lifecycle
if event_lifecycle is None:
# event lifecycle not available
# when recipe is not provided
return
epoch = event_lifecycle.current_index
if self.state.loggers.frequency_manager.is_optim_frequency_manager:
# log integer step for optimizer frequency manager
current_step = int(
self.state.loggers.epoch_to_step(
epoch=epoch,
steps_per_epoch=len(self.state.data.train),
)
)
else:
# log float epoch for epoch frequency manager
current_step = epoch
# always log loss if available
if loss is not None:
loss = loss if isinstance(loss, dict) else {"loss": loss}
self.state.loggers.metric.log_scalars(
tag="Loss", values=loss, step=current_step
)