llmcompressor.core
CompressionLifecycle
dataclass
A class for managing the lifecycle of compression events in the LLM Compressor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the compression process | State() |
recipe_container | RecipeContainer | The container for the compression recipe | RecipeContainer() |
modifiers | List[StageModifiers] | The list of stage modifiers | list() |
Source code in src/llmcompressor/core/lifecycle.py
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 |
|
event(event_type, global_step=0, **kwargs)
Handle a compression event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_type | EventType | The type of event to handle | required |
kwargs | Additional arguments to pass to the event handlers | {} |
Returns:
Type | Description |
---|---|
List[Any] | List of data returned from handling the event by modifiers |
Raises:
Type | Description |
---|---|
ValueError | If called before initialization, after finalization, or for an invalid event type |
Source code in src/llmcompressor/core/lifecycle.py
finalize(**kwargs)
Finalize the compression lifecycle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs | Additional arguments to update the state with | {} |
Returns:
Type | Description |
---|---|
List[Any] | List of data returned from finalizing modifiers |
Raises:
Type | Description |
---|---|
ValueError | If called before initialization or more than once |
Source code in src/llmcompressor/core/lifecycle.py
initialize(recipe=None, recipe_stage=None, recipe_args=None, **kwargs)
Initialize the compression lifecycle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs | Additional arguments to update the state with | {} |
Returns:
Type | Description |
---|---|
List[Any] | List of data returned from initialization of modifiers |
Source code in src/llmcompressor/core/lifecycle.py
reset()
Reset the compression lifecycle, finalizing any active modifiers and resetting all attributes.
Source code in src/llmcompressor/core/lifecycle.py
CompressionSession
A session for compression that holds the lifecycle and state for the current compression session
Source code in src/llmcompressor/core/session.py
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 |
|
lifecycle
property
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.
Returns:
Type | Description |
---|---|
CompressionLifecycle | the lifecycle for the session |
state
property
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.
Returns:
Type | Description |
---|---|
State | the current state of the session |
event(event_type, batch_data=None, loss=None, **kwargs)
Invoke an event for current CompressionSession.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_type | EventType | the event type to invoke | required |
batch_data | Optional[Any] | the batch data to use for the event | None |
loss | Optional[Any] | the loss to use for the event if any | None |
kwargs | additional kwargs to pass to the lifecycle's event method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the session after invoking the event |
Source code in src/llmcompressor/core/session.py
finalize(**kwargs)
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs | additional kwargs to pass to the lifecycle's finalize method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the session after finalizing |
Source code in src/llmcompressor/core/session.py
get_modifiers()
Get all modifiers across all stages
Source code in src/llmcompressor/core/session.py
get_serialized_recipe()
Returns:
Type | Description |
---|---|
Optional[str] | serialized string of the current compiled recipe |
Source code in src/llmcompressor/core/session.py
initialize(recipe=None, recipe_stage=None, recipe_args=None, model=None, teacher_model=None, optimizer=None, attach_optim_callbacks=True, train_data=None, val_data=None, test_data=None, calib_data=None, copy_data=True, start=None, steps_per_epoch=None, batches_per_step=None, loggers=None, **kwargs)
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recipe | Union[str, List[str], Recipe, List[Recipe], None] | 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. | None |
recipe_stage | Union[str, List[str], None] | the stage to target for the compression | None |
recipe_args | Union[Dict[str, Any], None] | the args to use for overriding the recipe defaults | None |
model | Optional[Any] | the model to compress | None |
teacher_model | Optional[Any] | the teacher model to use for knowledge distillation | None |
optimizer | Optional[Any] | the optimizer to use for the compression | None |
attach_optim_callbacks | bool | True to attach the optimizer callbacks to the compression lifecycle, False otherwise | True |
train_data | Optional[Any] | the training data to use for the compression | None |
val_data | Optional[Any] | the validation data to use for the compression | None |
test_data | Optional[Any] | the testing data to use for the compression | None |
calib_data | Optional[Any] | the calibration data to use for the compression | None |
copy_data | bool | True to copy the data, False otherwise | True |
start | Optional[float] | the start epoch to use for the compression | None |
steps_per_epoch | Optional[int] | the number of steps per epoch to use for the compression | None |
batches_per_step | Optional[int] | the number of batches per step to use for compression | None |
loggers | Union[None, LoggerManager, List[BaseLogger]] | the metrics manager to setup logging important info and milestones to, also accepts a list of BaseLogger(s) | None |
kwargs | additional kwargs to pass to the lifecycle's initialize method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the session after initializing |
Source code in src/llmcompressor/core/session.py
log(event_type, loss=None)
Log model and loss information for the current event type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_type | EventType | the event type to log for | required |
loss | Optional[Any] | the loss to log if any | None |
Source code in src/llmcompressor/core/session.py
reset()
reset_stage()
Reset the session for starting a new stage, recipe and model stays intact
Data
dataclass
A dataclass to hold different data sets for training, validation, testing, and/or calibration. Each data set is a ModifiableData instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train | Optional[Any] | The training data set | None |
val | Optional[Any] | The validation data set | None |
test | Optional[Any] | The testing data set | None |
calib | Optional[Any] | The calibration data set | None |
Source code in src/llmcompressor/core/state.py
Event
dataclass
A class for defining an event that can be triggered during sparsification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ | Optional[EventType] | The type of event. | None |
steps_per_epoch | Optional[int] | The number of steps per epoch. | None |
batches_per_step | Optional[int] | The number of batches per step where step is an optimizer step invocation. For most pathways, these are the same. See the invocations_per_step parameter for more details when they are not. | None |
invocations_per_step | int | The number of invocations of the step wrapper before optimizer.step was called. Generally can be left as 1 (default). For older amp pathways, this is the number of times the scaler wrapper was invoked before the wrapped optimizer step function was called to handle accumulation in fp16. | 1 |
global_step | int | The current global step. | 0 |
global_batch | int | The current global batch. | 0 |
Source code in src/llmcompressor/core/events/event.py
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 |
|
current_index
property
writable
Calculates the current index of the event.
Returns:
Type | Description |
---|---|
float | The current index of the event, which is either the global step or the epoch with the fraction of the current step. |
Raises:
Type | Description |
---|---|
ValueError | if the event is not epoch based or if the steps per epoch are too many. |
epoch
property
Calculates the current epoch.
Returns:
Type | Description |
---|---|
int | The current epoch. |
Raises:
Type | Description |
---|---|
ValueError | if the event is not epoch based. |
epoch_based
property
Determines if the event is based on epochs.
Returns:
Type | Description |
---|---|
bool | True if the event is based on epochs, False otherwise. |
epoch_batch
property
Calculates the current batch within the current epoch.
Returns:
Type | Description |
---|---|
int | The current batch within the current epoch. |
Raises:
Type | Description |
---|---|
ValueError | if the event is not epoch based. |
epoch_full
property
Calculates the current epoch with the fraction of the current step.
Returns:
Type | Description |
---|---|
float | The current epoch with the fraction of the current step. |
Raises:
Type | Description |
---|---|
ValueError | if the event is not epoch based. |
epoch_step
property
Calculates the current step within the current epoch.
Returns:
Type | Description |
---|---|
int | The current step within the current epoch. |
Raises:
Type | Description |
---|---|
ValueError | if the event is not epoch based. |
new_instance(**kwargs)
Creates a new instance of the event with the provided keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs | Keyword arguments to set in the new instance. | {} |
Returns:
Type | Description |
---|---|
Event | A new instance of the event with the provided kwargs. |
Source code in src/llmcompressor/core/events/event.py
should_update(start, end, update)
Determines if the event should trigger an update.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start | Optional[float] | The start index to check against, set to None to ignore start. | required |
end | Optional[float] | The end index to check against, set to None to ignore end. | required |
update | Optional[float] | The update interval, set to None or 0.0 to always update, otherwise must be greater than 0.0, defaults to None. | required |
Returns:
Type | Description |
---|---|
bool | True if the event should trigger an update, False otherwise. |
Source code in src/llmcompressor/core/events/event.py
EventType
Bases: Enum
An Enum for defining the different types of events that can be triggered during model compression lifecycles. The purpose of each EventType is to trigger the corresponding modifier callback during training or post training pipelines.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
INITIALIZE | Event type for initialization. | required | |
FINALIZE | Event type for finalization. | required | |
BATCH_START | Event type for the start of a batch. | required | |
LOSS_CALCULATED | Event type for when loss is calculated. | required | |
BATCH_END | Event type for the end of a batch. | required | |
CALIBRATION_EPOCH_START | Event type for the start of a calibration epoch. | required | |
SEQUENTIAL_EPOCH_END | Event type for the end of a layer calibration epoch, specifically used by | required | |
CALIBRATION_EPOCH_END | Event type for the end of a calibration epoch. | required | |
OPTIM_PRE_STEP | Event type for pre-optimization step. | required | |
OPTIM_POST_STEP | Event type for post-optimization step. | required |
Source code in src/llmcompressor/core/events/event.py
Hardware
dataclass
A dataclass to hold information about the hardware being used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device | Optional[str] | The current device being used for training | None |
devices | Optional[List[str]] | List of all devices to be used for training | None |
rank | Optional[int] | The rank of the current device | None |
world_size | Optional[int] | The total number of devices being used | None |
local_rank | Optional[int] | The local rank of the current device | None |
local_world_size | Optional[int] | The total number of devices being used on the local machine | None |
distributed | Optional[bool] | Whether or not distributed training is being used | None |
distributed_strategy | Optional[str] | The distributed strategy being used | None |
Source code in src/llmcompressor/core/state.py
LifecycleCallbacks
A class for invoking lifecycle events for the active session
Source code in src/llmcompressor/core/session_functions.py
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 |
|
batch_end(**kwargs)
classmethod
Invoke a batch end event for the active session
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs | additional kwargs to pass to the current session's event method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the active session after invoking the event |
Source code in src/llmcompressor/core/session_functions.py
batch_start(batch_data=None, **kwargs)
classmethod
Invoke a batch start event for the active session
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_data | Optional[Any] | the batch data to use for the event | None |
kwargs | additional kwargs to pass to the current session's event method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the active session after invoking the event |
Source code in src/llmcompressor/core/session_functions.py
calibration_epoch_end(**kwargs)
classmethod
Invoke a epoch end event for the active session during calibration. This event should be called after the model has been calibrated for one epoch
see src/llmcompressor/pipelines/basic/pipeline.py
for usage example
Source code in src/llmcompressor/core/session_functions.py
calibration_epoch_start(**kwargs)
classmethod
Invoke a epoch start event for the active session during calibration. This event should be called before calibration starts for one epoch
see src/llmcompressor/pipelines/basic/pipeline.py
for usage example
Source code in src/llmcompressor/core/session_functions.py
event(event_type, **kwargs)
classmethod
Invoke an event for the active session
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_type | EventType | the event type to invoke | required |
kwargs | additional kwargs to pass to the current session's event method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the active session after invoking the event |
Source code in src/llmcompressor/core/session_functions.py
loss_calculated(loss=None, **kwargs)
classmethod
Invoke a loss calculated event for the active session
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss | Optional[Any] | the loss to use for the event | None |
kwargs | additional kwargs to pass to the current session's event method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the active session after invoking the event |
Source code in src/llmcompressor/core/session_functions.py
optim_post_step(**kwargs)
classmethod
Invoke an optimizer post-step event for the active session
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs | additional kwargs to pass to the current session's event method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the active session after invoking the event |
Source code in src/llmcompressor/core/session_functions.py
optim_pre_step(**kwargs)
classmethod
Invoke an optimizer pre-step event for the active session
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs | additional kwargs to pass to the current session's event method | {} |
Returns:
Type | Description |
---|---|
ModifiedState | the modified state of the active session after invoking the event |
Source code in src/llmcompressor/core/session_functions.py
sequential_epoch_end(**kwargs)
classmethod
Invoke a sequential epoch end event for the active session. This event should be called after one sequential layer has been calibrated/trained for one epoch
This is called after a sequential layer has been calibrated with one batch, see src/llmcompressor/pipelines/sequential/pipeline.py
for usage example
Source code in src/llmcompressor/core/session_functions.py
ModelParameterizedLayer
dataclass
A dataclass for holding a parameter and its layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer_name | str | the name of the layer | required |
layer | Any | the layer object | required |
param_name | str | the name of the parameter | required |
param | Any | the parameter object | required |
Source code in src/llmcompressor/core/model_layer.py
ModifiedState
dataclass
A dataclass to represent a modified model, optimizer, and loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model | Optional[Any] | The modified model | required |
optimizer | Optional[Any] | The modified optimizer | required |
loss | Optional[Any] | The modified loss | required |
modifier_data | Optional[List[Dict[str, Any]]] | The modifier data used to modify the model, optimizer, and loss | required |
Source code in src/llmcompressor/core/state.py
__init__(model, optimizer, loss, modifier_data)
Initialize the ModifiedState with the given parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model | Any | The modified model | required |
optimizer | Any | The modified optimizer | required |
loss | Any | The modified loss | required |
modifier_data | List[Dict[str, Any]] | The modifier data used to modify the model, optimizer, and loss | required |
Source code in src/llmcompressor/core/state.py
State
dataclass
State class holds information about the current compression state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model | Any | The model being used for compression | None |
teacher_model | Any | The teacher model being used for compression | None |
optimizer | Any | The optimizer being used for training | None |
optim_wrapped | bool | Whether or not the optimizer has been wrapped | None |
loss | Any | The loss function being used for training | None |
batch_data | Any | The current batch of data being used for compression | None |
data | Data | The data sets being used for training, validation, testing, and/or calibration, wrapped in a Data instance | Data() |
hardware | Hardware | Hardware instance holding info about the target hardware being used | Hardware() |
loggers | Optional[LoggerManager] | LoggerManager instance holding all the loggers to log | None |
model_log_cadence | Optional[float] | The cadence to log model information w.r.t epochs. If 1, logs every epoch. If 2, logs every other epoch, etc. Default is 1. | None |
Source code in src/llmcompressor/core/state.py
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 |
|
compression_ready
property
Check if the model and optimizer are set for compression.
Returns:
Type | Description |
---|---|
bool | True if model and optimizer are set, False otherwise |
update(model=None, teacher_model=None, optimizer=None, attach_optim_callbacks=True, train_data=None, val_data=None, test_data=None, calib_data=None, copy_data=True, start=None, steps_per_epoch=None, batches_per_step=None, loggers=None, model_log_cadence=None, **kwargs)
Update the state with the given parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model | Any | The model to update the state with | None |
teacher_model | Any | The teacher model to update the state with | None |
optimizer | Any | The optimizer to update the state with | None |
attach_optim_callbacks | bool | Whether or not to attach optimizer callbacks | True |
train_data | Any | The training data to update the state with | None |
val_data | Any | The validation data to update the state with | None |
test_data | Any | The testing data to update the state with | None |
calib_data | Any | The calibration data to update the state with | None |
copy_data | bool | Whether or not to copy the data | True |
start | float | The start index to update the state with | None |
steps_per_epoch | int | The steps per epoch to update the state with | None |
batches_per_step | int | The batches per step to update the state with | None |
loggers | Union[None, LoggerManager, List[BaseLogger]] | The metrics manager to setup logging important info and milestones to, also accepts a list of BaseLogger(s) | None |
model_log_cadence | Optional[float] | The cadence to log model information w.r.t epochs. If 1, logs every epoch. If 2, logs every other epoch, etc. Default is 1. | None |
kwargs | Additional keyword arguments to update the state with | {} |
Returns:
Type | Description |
---|---|
Dict | The updated state as a dictionary |
Source code in src/llmcompressor/core/state.py
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 |
|
active_session()
Returns:
Type | Description |
---|---|
CompressionSession | the active session for sparsification |
create_session()
Context manager to create and yield a new session for sparsification. This will set the active session to the new session for the duration of the context.
Returns:
Type | Description |
---|---|
Generator[CompressionSession, None, None] | the new session |