Skip to content

llmcompressor.modifiers.modifier

Classes:

  • Modifier –

    A base class for all modifiers to inherit from.

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_calibration_start if event.type_ == EventType.CALIBRATION_START * on_start if self.start <= event.current_index * on_sequential_epoch_end if event.type_ == EventType.SEQUENTIAL_EPOCH_END * on_end if self.end >= event.current_index * on_calibration_end if event.type_ == EventType.CALIBRATION_END 5. finalize

Subclasses that need calibration data must override requires_calibration_data to return True. This is used by the pipeline registry to select the appropriate calibration pipeline.

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:

  • estimate_sequential_memory –

    Estimate the device memory this modifier needs for a module, for

  • finalize –

    Finalize the modifier for the given model and state.

  • initialize –

    Initialize the modifier for the given model and state.

  • on_calibration_end –

    on_calibration_end is called at the end of a calibration epoch.

  • on_calibration_start –

    on_calibration_start is called at the start of a calibration epoch.

  • 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_sequential_epoch_end –

    on_sequential_epoch_end is called at the end of a sequential layer

  • 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

finalized property

finalized: bool

Returns:

  • bool –

    True if the modifier has been finalized

initialized property

initialized: bool

Returns:

  • bool –

    True if the modifier has been initialized

estimate_sequential_memory

estimate_sequential_memory(
    module: Module, num_tokens: int
) -> tuple[int, int] | None

Estimate the device memory this modifier needs for a module, for sequential_targets_per_subgraph="auto". Called under disable_onloading(). Modifiers which need calibration data report None (unknown) unless they override this

Parameters:

  • module (Module) –

    module belonging to the subgraph being calibrated

  • num_tokens (int) –

    number of calibration tokens, including padding

Returns:

  • tuple[int, int] | None –

    (held, scratch) bytes, or None if unknown. Held memory is kept until the subgraph is compressed, scratch only while the module is compressed

Source code in src/llmcompressor/modifiers/modifier.py
def estimate_sequential_memory(
    self, module: torch.nn.Module, num_tokens: int
) -> tuple[int, int] | None:
    """
    Estimate the device memory this modifier needs for a module, for
    `sequential_targets_per_subgraph="auto"`. Called under `disable_onloading()`.
    Modifiers which need calibration data report None (unknown) unless they
    override this

    :param module: module belonging to the subgraph being calibrated
    :param num_tokens: number of calibration tokens, including padding
    :return: (held, scratch) bytes, or None if unknown. Held memory is kept until
        the subgraph is compressed, scratch only while the module is compressed
    """
    return None if self.requires_calibration_data else (0, 0)

finalize

finalize(state: State, **kwargs)

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 src/llmcompressor/modifiers/modifier.py
def finalize(self, state: State, **kwargs):
    """
    Finalize the modifier for the given model and state.

    :raises RuntimeError: if the modifier has not been initialized
    :param state: The current state of the model
    :param kwargs: Additional arguments for finalizing the modifier
    """
    if self.finalized_:
        raise RuntimeError("cannot finalize a modifier twice")

    if not self.initialized_:
        raise RuntimeError("cannot finalize an uninitialized modifier")

    # TODO: all finalization should succeed
    self.finalized_ = self.on_finalize(state=state, **kwargs)

initialize

initialize(state: State, **kwargs)

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 src/llmcompressor/modifiers/modifier.py
def initialize(self, state: State, **kwargs):
    """
    Initialize the modifier for the given model and state.

    :raises RuntimeError: if the modifier has already been finalized
    :param state: The current state of the model
    :param kwargs: Additional arguments for initializing the modifier
    """
    if self.initialized_:
        raise RuntimeError(
            "Cannot initialize a modifier that has already been initialized"
        )

    if self.finalized_:
        raise RuntimeError(
            "Cannot initialize a modifier that has already been finalized"
        )

    self.initialized_ = self.on_initialize(state=state, **kwargs)

    # trigger starts
    fake_start_event = Event(type_=EventType.BATCH_START, global_step=0)
    if self.should_start(fake_start_event):
        self.on_start(state, fake_start_event, **kwargs)
        self.started_ = True

on_calibration_end

on_calibration_end(state: State, event: Event, **kwargs)

on_calibration_end is called at the end of a calibration epoch.

Parameters:

  • state (State) –

    The current state of the model

  • event (Event) –

    The event that triggered the calibration epoch end

  • kwargs –

    Additional arguments for the calibration epoch end

Source code in src/llmcompressor/modifiers/modifier.py
def on_calibration_end(self, state: State, event: Event, **kwargs):
    """
    on_calibration_end is called at the end of a calibration epoch.

    :param state: The current state of the model
    :param event: The event that triggered the calibration epoch end
    :param kwargs: Additional arguments for the calibration epoch end
    """
    pass

on_calibration_start

on_calibration_start(state: State, event: Event, **kwargs)

on_calibration_start is called at the start of a calibration epoch.

Parameters:

  • state (State) –

    The current state of the model

  • event (Event) –

    The event that triggered the calibration epoch start

  • kwargs –

    Additional arguments for the calibration epoch start

Source code in src/llmcompressor/modifiers/modifier.py
def on_calibration_start(self, state: State, event: Event, **kwargs):
    """
    on_calibration_start is called at the start of a calibration epoch.

    :param state: The current state of the model
    :param event: The event that triggered the calibration epoch start
    :param kwargs: Additional arguments for the calibration epoch start
    """
    pass

on_end

on_end(state: State, event: Event, **kwargs)

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 src/llmcompressor/modifiers/modifier.py
def on_end(self, state: State, event: Event, **kwargs):
    """
    on_end is called when the modifier ends and must be implemented
    by the inheriting modifier.

    :param state: The current state of the model
    :param event: The event that triggered the end
    :param kwargs: Additional arguments for ending the modifier
    """
    pass

on_event

on_event(state: State, event: Event, **kwargs)

on_event is called whenever an event is triggered

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 src/llmcompressor/modifiers/modifier.py
def on_event(self, state: State, event: Event, **kwargs):
    """
    on_event is called whenever an event is triggered

    :param state: The current state of the model
    :param event: The event that triggered the update
    :param kwargs: Additional arguments for updating the model
    """
    pass

on_finalize

on_finalize(state: State, **kwargs) -> bool

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 src/llmcompressor/modifiers/modifier.py
def on_finalize(self, state: State, **kwargs) -> bool:
    """
    on_finalize is called on modifier finalization and
    must be implemented by the inheriting modifier.

    :param state: The current state of the model
    :param kwargs: Additional arguments for finalizing the modifier
    :return: True if the modifier was finalized successfully,
        False otherwise
    """
    return True

on_initialize abstractmethod

on_initialize(state: State, **kwargs) -> bool

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 src/llmcompressor/modifiers/modifier.py
@abstractmethod
def on_initialize(self, state: State, **kwargs) -> bool:
    """
    on_initialize is called on modifier initialization and
    must be implemented by the inheriting modifier.

    :param state: The current state of the model
    :param kwargs: Additional arguments for initializing the modifier
    :return: True if the modifier was initialized successfully,
        False otherwise
    """
    raise NotImplementedError()

on_sequential_epoch_end

on_sequential_epoch_end(
    state: State,
    event: Event,
    modules: list[Module],
    **kwargs,
)

on_sequential_epoch_end is called at the end of a sequential layer calibration/training epoch.

Parameters:

  • state (State) –

    The current state of the model

  • event (Event) –

    The event that triggered the sequential epoch end

  • modules (list[Module]) –

    List of de-duplicated modules which were included in the sequential subgraph

  • kwargs –

    Additional arguments for the sequential epoch end

Source code in src/llmcompressor/modifiers/modifier.py
def on_sequential_epoch_end(
    self, state: State, event: Event, modules: list[torch.nn.Module], **kwargs
):
    """
    on_sequential_epoch_end is called at the end of a sequential layer
    calibration/training epoch.

    :param state: The current state of the model
    :param event: The event that triggered the sequential epoch end
    :param modules: List of de-duplicated modules which were included in the
        sequential subgraph
    :param kwargs: Additional arguments for the sequential epoch end
    """
    pass

on_start

on_start(state: State, event: Event, **kwargs)

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 src/llmcompressor/modifiers/modifier.py
def on_start(self, state: State, event: Event, **kwargs):
    """
    on_start is called when the modifier starts and
    must be implemented by the inheriting modifier.

    :param state: The current state of the model
    :param event: The event that triggered the start
    :param kwargs: Additional arguments for starting the modifier
    """
    pass

on_update

on_update(state: State, event: 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:

  • 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 src/llmcompressor/modifiers/modifier.py
def on_update(self, state: State, event: 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.

    :param state: The current state of the model
    :param event: The event that triggered the update
    :param kwargs: Additional arguments for updating the model
    """
    pass

should_end

should_end(event: Event)

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 src/llmcompressor/modifiers/modifier.py
def should_end(self, event: Event):
    """
    :param event: The event to check if the modifier should end
    :return: True if the modifier should end based on the given event
    """
    current = event.current_index

    return self.end is not None and current >= self.end

should_start

should_start(event: Event) -> bool

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 src/llmcompressor/modifiers/modifier.py
def should_start(self, event: Event) -> bool:
    """
    :param event: The event to check if the modifier should start
    :return: True if the modifier should start based on the given event
    """
    if self.start is None:
        return False

    current = event.current_index

    return self.start <= current and (self.end is None or current < self.end)

update_event

update_event(state: State, event: 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:

  • state (State) –

    The current state of compression

  • 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 src/llmcompressor/modifiers/modifier.py
def update_event(self, state: State, event: 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

    :raises RuntimeError: if the modifier has been finalized
    :param state: The current state of compression
    :param event: The event to update the modifier with
    :param kwargs: Additional arguments for updating the modifier
    """
    if not self.initialized_:
        raise RuntimeError("Cannot update an uninitialized modifier")

    if self.finalized_:
        raise RuntimeError("Cannot update a finalized modifier")

    ## catch-all hook ##

    self.on_event(state, event, **kwargs)

    ## calibration lifecycle ##

    if event.type_ == EventType.CALIBRATION_START:
        self.on_calibration_start(state, event, **kwargs)
        self.started_ = True
        return

    if event.type_ == EventType.SEQUENTIAL_EPOCH_END:
        self.on_sequential_epoch_end(state, event, **kwargs)
        return

    if event.type_ == EventType.CALIBRATION_END:
        self.on_calibration_end(state, event, **kwargs)
        self.ended_ = True
        return

    ## training lifecycle ##

    if (
        event.type_ == EventType.BATCH_START
        and not self.started_
        and self.should_start(event)
    ):
        self.on_start(state, event, **kwargs)
        self.started_ = True
        self.on_update(state, event, **kwargs)
        return

    if (
        event.type_ == EventType.BATCH_END
        and not self.ended_
        and self.should_end(event)
    ):
        self.on_end(state, event, **kwargs)
        self.ended_ = True
        self.on_update(state, event, **kwargs)
        return

    if self.started_ and not self.ended_:
        self.on_update(state, event, **kwargs)