llmcompressor.modifiers
Compression modifiers for applying various optimization techniques.
Provides the core modifier system for applying compression techniques like quantization, pruning, distillation, and other optimization methods to neural networks. Includes base classes, factory patterns, and interfaces for extensible compression workflows.
Modules:
-
awq
– -
distillation
–Provides model distillation functionality, specifically importing output-based
-
factory
– -
interface
– -
logarithmic_equalization
– -
modifier
– -
obcq
– -
pruning
– -
quantization
– -
smoothquant
– -
transform
– -
utils
–
Classes:
-
Modifier
–A base class for all modifiers to inherit from.
-
ModifierFactory
–A factory for loading and registering modifiers
-
ModifierInterface
–Defines the contract that all modifiers must implement
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:
-
index
The index of the modifier in the list of modifiers for the model
-
group
The group name for the modifier
-
start
The start step for the modifier
-
end
The end step for the modifier
-
update
The update step for the modifier
Methods:
-
finalize
–Finalize the modifier for the given model and state.
-
initialize
–Initialize the modifier for the given model and state.
-
on_end
–on_end is called when the modifier ends and must be implemented
-
on_event
–on_event is called whenever an event is triggered
-
on_finalize
–on_finalize is called on modifier finalization and
-
on_initialize
–on_initialize is called on modifier initialization and
-
on_start
–on_start is called when the modifier starts and
-
on_update
–on_update is called when the model in question must be
-
should_end
–:param event: The event to check if the modifier should end
-
should_start
–:param event: The event to check if the modifier should start
-
update_event
–Update modifier based on the given event. In turn calls
Attributes:
-
finalized
(bool
) –:return: True if the modifier has been finalized
-
initialized
(bool
) –:return: True if the modifier has been initialized
finalize
Finalize the modifier for the given model and state.
Parameters:
-
state
State
) –The current state of the model
-
kwargs
Additional arguments for finalizing the modifier
Raises:
-
RuntimeError
–if the modifier has not been initialized
Source code in llmcompressor/modifiers/modifier.py
initialize
Initialize the modifier for the given model and state.
Parameters:
-
state
State
) –The current state of the model
-
kwargs
Additional arguments for initializing the modifier
Raises:
-
RuntimeError
–if the modifier has already been finalized
Source code in llmcompressor/modifiers/modifier.py
on_end
on_end is called when the modifier ends and must be implemented by the inheriting modifier.
Parameters:
-
state
State
) –The current state of the model
-
event
Event
) –The event that triggered the end
-
kwargs
Additional arguments for ending the modifier
Source code in llmcompressor/modifiers/modifier.py
on_event
on_finalize
on_finalize is called on modifier finalization and must be implemented by the inheriting modifier.
Parameters:
-
state
State
) –The current state of the model
-
kwargs
Additional arguments for finalizing the modifier
Returns:
-
bool
–True if the modifier was finalized successfully, False otherwise
Source code in llmcompressor/modifiers/modifier.py
on_initialize abstractmethod
on_initialize is called on modifier initialization and must be implemented by the inheriting modifier.
Parameters:
-
state
State
) –The current state of the model
-
kwargs
Additional arguments for initializing the modifier
Returns:
-
bool
–True if the modifier was initialized successfully, False otherwise
Source code in llmcompressor/modifiers/modifier.py
on_start
on_start is called when the modifier starts and must be implemented by the inheriting modifier.
Parameters:
-
state
State
) –The current state of the model
-
event
Event
) –The event that triggered the start
-
kwargs
Additional arguments for starting the modifier
Source code in llmcompressor/modifiers/modifier.py
on_update
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:
-
state
State
) –The current state of the model
-
event
Event
) –The event that triggered the update
-
kwargs
Additional arguments for updating the model
Source code in llmcompressor/modifiers/modifier.py
should_end
Parameters:
-
event
Event
) –The event to check if the modifier should end
Returns:
- –
True if the modifier should end based on the given event
Source code in llmcompressor/modifiers/modifier.py
should_start
Parameters:
-
event
Event
) –The event to check if the modifier should start
Returns:
-
bool
–True if the modifier should start based on the given event
Source code in llmcompressor/modifiers/modifier.py
update_event
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:
-
state
State
) –The current state of sparsification
-
event
Event
) –The event to update the modifier with
-
kwargs
Additional arguments for updating the modifier
Raises:
-
RuntimeError
–if the modifier has been finalized
Source code in llmcompressor/modifiers/modifier.py
ModifierFactory
A factory for loading and registering modifiers
Methods:
-
create
–Instantiate a modifier of the given type from registered modifiers.
-
load_from_package
–:param package_path: The path to the package to load modifiers from
-
refresh
–A method to refresh the factory by reloading the modifiers
-
register
–Register a modifier class to be used by the factory.
create staticmethod
Instantiate a modifier of the given type from registered modifiers.
Parameters:
-
type_
str
) –The type of modifier to create
-
framework
The framework the modifier is for
-
allow_registered
bool
) –Whether or not to allow registered modifiers
-
allow_experimental
bool
) –Whether or not to allow experimental modifiers
-
kwargs
Additional keyword arguments to pass to the modifier during instantiation
Returns:
-
Modifier
–The instantiated modifier
Raises:
-
ValueError
–If no modifier of the given type is found
Source code in llmcompressor/modifiers/factory.py
load_from_package staticmethod
Parameters:
-
package_path
str
) –The path to the package to load modifiers from
Returns:
-
Dict[str, Type[Modifier]]
–The loaded modifiers, as a mapping of name to class
Source code in 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 llmcompressor/modifiers/factory.py
register staticmethod
Register a modifier class to be used by the factory.
Parameters:
-
type_
str
) –The type of modifier to register
-
modifier_class
Type[Modifier]
) –The class of the modifier to register, must subclass the Modifier base class
Raises:
-
ValueError
–If the provided class does not subclass the Modifier base class or is not a type
Source code in llmcompressor/modifiers/factory.py
ModifierInterface
Bases: ABC
Defines the contract that all modifiers must implement
Methods:
-
finalize
–Finalize the modifier
-
initialize
–Initialize the modifier
-
update_event
–Update the modifier based on the event
Attributes:
-
finalized
(bool
) –:return: True if the modifier has been finalized
-
initialized
(bool
) –:return: True if the modifier has been initialized
finalized abstractmethod
property
Returns:
-
bool
–True if the modifier has been finalized
initialized abstractmethod
property
Returns:
-
bool
–True if the modifier has been initialized
finalize abstractmethod
Finalize the modifier
Parameters:
-
state
State
) –The current state of the model
-
kwargs
Additional keyword arguments for modifier finalization
Source code in llmcompressor/modifiers/interface.py
initialize abstractmethod
Initialize the modifier
Parameters:
-
state
State
) –The current state of the model
-
kwargs
Additional keyword arguments for modifier initialization
Source code in llmcompressor/modifiers/interface.py
update_event abstractmethod
Update the modifier based on the event
Parameters:
-
state
State
) –The current state of the model
-
event
Event
) –The event to update the modifier with
-
kwargs
Additional keyword arguments for modifier update