llmcompressor.modifiers
Modifier
Bases: ModifierInterface
, HooksMixin
A base class for all modifiers to inherit from. Modifiers are used to modify the training process for a model. Defines base attributes and methods available to all modifiers
Lifecycle: 1. initialize 2. on_event -> * on_start if self.start <= event.current_index * on_end if self.end >= event.current_index 5. finalize
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index | The index of the modifier in the list of modifiers for the model | required | |
group | The group name for the modifier | required | |
start | The start step for the modifier | required | |
end | The end step for the modifier | required | |
update | The update step for the modifier | required |
Source code in src/llmcompressor/modifiers/modifier.py
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 |
|
finalized
property
Returns:
Type | Description |
---|---|
bool | True if the modifier has been finalized |
initialized
property
Returns:
Type | Description |
---|---|
bool | True if the modifier has been initialized |
finalize(state, **kwargs)
Finalize the modifier for the given model and state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
kwargs | Additional arguments for finalizing the modifier | {} |
Raises:
Type | Description |
---|---|
RuntimeError | if the modifier has not been initialized |
Source code in src/llmcompressor/modifiers/modifier.py
initialize(state, **kwargs)
Initialize the modifier for the given model and state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
kwargs | Additional arguments for initializing the modifier | {} |
Raises:
Type | Description |
---|---|
RuntimeError | if the modifier has already been finalized |
Source code in src/llmcompressor/modifiers/modifier.py
on_end(state, event, **kwargs)
on_end is called when the modifier ends and must be implemented by the inheriting modifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
event | Event | The event that triggered the end | required |
kwargs | Additional arguments for ending the modifier | {} |
Source code in src/llmcompressor/modifiers/modifier.py
on_event(state, event, **kwargs)
on_event is called whenever an event is triggered
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
event | Event | The event that triggered the update | required |
kwargs | Additional arguments for updating the model | {} |
Source code in src/llmcompressor/modifiers/modifier.py
on_finalize(state, **kwargs)
on_finalize is called on modifier finalization and must be implemented by the inheriting modifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
kwargs | Additional arguments for finalizing the modifier | {} |
Returns:
Type | Description |
---|---|
bool | True if the modifier was finalized successfully, False otherwise |
Source code in src/llmcompressor/modifiers/modifier.py
on_initialize(state, **kwargs)
abstractmethod
on_initialize is called on modifier initialization and must be implemented by the inheriting modifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
kwargs | Additional arguments for initializing the modifier | {} |
Returns:
Type | Description |
---|---|
bool | True if the modifier was initialized successfully, False otherwise |
Source code in src/llmcompressor/modifiers/modifier.py
on_start(state, event, **kwargs)
on_start is called when the modifier starts and must be implemented by the inheriting modifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
event | Event | The event that triggered the start | required |
kwargs | Additional arguments for starting the modifier | {} |
Source code in src/llmcompressor/modifiers/modifier.py
on_update(state, event, **kwargs)
on_update is called when the model in question must be updated based on passed in event. Must be implemented by the inheriting modifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
event | Event | The event that triggered the update | required |
kwargs | Additional arguments for updating the model | {} |
Source code in src/llmcompressor/modifiers/modifier.py
should_end(event)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event | Event | The event to check if the modifier should end | required |
Returns:
Type | Description |
---|---|
True if the modifier should end based on the given event |
Source code in src/llmcompressor/modifiers/modifier.py
should_start(event)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event | Event | The event to check if the modifier should start | required |
Returns:
Type | Description |
---|---|
bool | True if the modifier should start based on the given event |
Source code in src/llmcompressor/modifiers/modifier.py
update_event(state, event, **kwargs)
Update modifier based on the given event. In turn calls on_start, on_update, and on_end based on the event and modifier settings. Returns immediately if the modifier is not initialized
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of sparsification | required |
event | Event | The event to update the modifier with | required |
kwargs | Additional arguments for updating the modifier | {} |
Raises:
Type | Description |
---|---|
RuntimeError | if the modifier has been finalized |
Source code in src/llmcompressor/modifiers/modifier.py
ModifierFactory
A factory for loading and registering modifiers
Source code in src/llmcompressor/modifiers/factory.py
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 |
|
create(type_, allow_registered, allow_experimental, **kwargs)
staticmethod
Instantiate a modifier of the given type from registered modifiers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ | str | The type of modifier to create | required |
framework | The framework the modifier is for | required | |
allow_registered | bool | Whether or not to allow registered modifiers | required |
allow_experimental | bool | Whether or not to allow experimental modifiers | required |
kwargs | Additional keyword arguments to pass to the modifier during instantiation | {} |
Returns:
Type | Description |
---|---|
Modifier | The instantiated modifier |
Raises:
Type | Description |
---|---|
ValueError | If no modifier of the given type is found |
Source code in src/llmcompressor/modifiers/factory.py
load_from_package(package_path)
staticmethod
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package_path | str | The path to the package to load modifiers from | required |
Returns:
Type | Description |
---|---|
Dict[str, Type[Modifier]] | The loaded modifiers, as a mapping of name to class |
Source code in src/llmcompressor/modifiers/factory.py
refresh()
staticmethod
A method to refresh the factory by reloading the modifiers Note: this will clear any previously registered modifiers
Source code in src/llmcompressor/modifiers/factory.py
register(type_, modifier_class)
staticmethod
Register a modifier class to be used by the factory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type_ | str | The type of modifier to register | required |
modifier_class | Type[Modifier] | The class of the modifier to register, must subclass the Modifier base class | required |
Raises:
Type | Description |
---|---|
ValueError | If the provided class does not subclass the Modifier base class or is not a type |
Source code in src/llmcompressor/modifiers/factory.py
ModifierInterface
Bases: ABC
Defines the contract that all modifiers must implement
Source code in src/llmcompressor/modifiers/interface.py
finalized
abstractmethod
property
Returns:
Type | Description |
---|---|
bool | True if the modifier has been finalized |
initialized
abstractmethod
property
Returns:
Type | Description |
---|---|
bool | True if the modifier has been initialized |
finalize(state, **kwargs)
abstractmethod
Finalize the modifier
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
kwargs | Additional keyword arguments for modifier finalization | {} |
Source code in src/llmcompressor/modifiers/interface.py
initialize(state, **kwargs)
abstractmethod
Initialize the modifier
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
kwargs | Additional keyword arguments for modifier initialization | {} |
Source code in src/llmcompressor/modifiers/interface.py
update_event(state, event, **kwargs)
abstractmethod
Update the modifier based on the event
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The current state of the model | required |
event | Event | The event to update the modifier with | required |
kwargs | Additional keyword arguments for modifier update | {} |
Source code in src/llmcompressor/modifiers/interface.py
StageModifiers
Bases: ModifierInterface
, BaseModel
Represents a collection of modifiers that are applied together as a stage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
modifiers | The modifiers to apply as a stage | required | |
index | The index of the stage, if applicable | required | |
group | The group name of the stage, if applicable | required | |
applied | Flag for indicating if this stage has has already been applied to the model through finalization | required |
Source code in src/llmcompressor/modifiers/stage.py
finalized
property
Returns:
Type | Description |
---|---|
bool | True if all of the stage modifiers have been finalized, False otherwise |
initialized
property
Returns:
Type | Description |
---|---|
bool | True if all of the stage modifiers have been initialized, False otherwise |
unique_id
property
Returns:
Type | Description |
---|---|
str | ID for stage containing the name and index |
finalize(state, **kwargs)
Finalize all the stage modifiers and mark the stage as applied
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The state of current session | required |
kwargs | Additional kwargs to pass to the modifier(s) finalize method | {} |
Source code in src/llmcompressor/modifiers/stage.py
initialize(state, **kwargs)
Initialize all the stage modifiers
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The state of current session | required |
kwargs | Additional kwargs to pass to the modifier(s) initialize method | {} |
Source code in src/llmcompressor/modifiers/stage.py
update_event(state, event, **kwargs)
Propagate the event to all the stage modifiers
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | State | The state of current session | required |
event | Event | The event to propagate | required |
kwargs | Additional kwargs to pass to the modifier(s) update_event method | {} |