跳到内容

llmcompressor.core.events.event

用于在 LLM Compressor 中定义和管理事件的模块。

此模块提供了一个枚举类型用于不同的事件类型,以及一个类用于创建和管理事件,包括用于计算事件属性和根据指定间隔触发更新的方法。

  • Event

    一个用于定义在稀疏化过程中可以触发的事件的类。

  • EventType

    一个枚举,用于定义可以触发的不同类型的事件

Event dataclass

Event(
    type_: Optional[EventType] = None,
    steps_per_epoch: Optional[int] = None,
    batches_per_step: Optional[int] = None,
    invocations_per_step: int = 1,
    global_step: int = 0,
    global_batch: int = 0,
)

一个用于定义在稀疏化过程中可以触发的事件的类。

参数

  • 类型_

    (Optional[EventType], 默认值: None ) –

    事件类型。

  • steps_per_epoch

    (Optional[int], 默认值: None ) –

    每个 epoch 的步数。

  • batches_per_step

    (Optional[int], 默认值: None ) –

    每个步骤的批次数量,其中步骤是优化器步骤调用。对于大多数路径,它们是相同的。当它们不同时,请参阅 invocations_per_step 参数以获取更多详细信息。

  • invocations_per_step

    (int, 默认值: 1 ) –

    调用步骤包装器之前 optimizer.step 被调用的次数。通常可以保留为 1(默认值)。对于较旧的 amp 路径,这是在调用包装的优化器步骤函数以处理 fp16 中的累积之前调用缩放器包装器的次数。

  • global_step

    (int, 默认值: 0 ) –

    当前全局步骤。

  • global_batch

    (int, 默认值: 0 ) –

    当前全局批次。

方法

  • new_instance

    使用提供的关键字参数创建事件的新实例。

  • should_update

    确定事件是否应触发更新。

属性

  • current_index (float) –

    计算事件的当前索引。

  • epoch (int) –

    计算当前 epoch。

  • epoch_based (bool) –

    确定事件是否基于 epoch。

  • epoch_batch (int) –

    计算当前 epoch 中的当前批次。

  • epoch_full (float) –

    计算当前 epoch 和当前步骤的比例。

  • epoch_step (int) –

    计算当前 epoch 中的当前步骤。

current_index property writable

current_index: float

计算事件的当前索引。

返回

  • float

    事件的当前索引,可以是全局步数,也可以是当前步数占 epoch 的比例。

引发

  • ValueError

    如果事件不是基于 epoch 的,或者每个 epoch 的步数太多。

epoch property

epoch: int

计算当前 epoch。

返回

  • int

    当前 epoch。

引发

  • ValueError

    如果事件不是基于 epoch 的。

epoch_based property

epoch_based: bool

确定事件是否基于 epoch。

返回

  • bool

    如果事件基于 epoch,则为 True,否则为 False。

epoch_batch property

epoch_batch: int

计算当前 epoch 中的当前批次。

返回

  • int

    当前 epoch 中的当前批次。

引发

  • ValueError

    如果事件不是基于 epoch 的。

epoch_full property

epoch_full: float

计算当前 epoch 和当前步骤的比例。

返回

  • float

    当前 epoch 和当前步骤的比例。

引发

  • ValueError

    如果事件不是基于 epoch 的。

epoch_step property

epoch_step: int

计算当前 epoch 中的当前步骤。

返回

  • int

    当前 epoch 中的当前步骤。

引发

  • ValueError

    如果事件不是基于 epoch 的。

new_instance

new_instance(**kwargs) -> Event

使用提供的关键字参数创建事件的新实例。

参数

  • kwargs

    要在新实例中设置的关键字参数。

返回

  • Event

    具有提供的 kwargs 的事件新实例。

源代码位于 llmcompressor/core/events/event.py
def new_instance(self, **kwargs) -> "Event":
    """
    Creates a new instance of the event with the provided keyword arguments.

    :param kwargs: Keyword arguments to set in the new instance.
    :return: A new instance of the event with the provided kwargs.
    :rtype: Event
    """
    logger.debug("Creating new instance of event with kwargs: {}", kwargs)
    instance = deepcopy(self)
    for key, value in kwargs.items():
        setattr(instance, key, value)
    return instance

should_update

should_update(
    start: Optional[float],
    end: Optional[float],
    update: Optional[float],
) -> bool

确定事件是否应触发更新。

参数

  • start

    (Optional[float]) –

    要检查的起始索引,设置为 None 则忽略起始。

  • 结束

    (Optional[float]) –

    要检查的结束索引,设置为 None 则忽略结束。

  • 更新

    (Optional[float]) –

    更新间隔,设置为 None 或 0.0 则始终更新,否则必须大于 0.0,默认为 None。

返回

  • bool

    如果事件应触发更新,则为 True,否则为 False。

源代码位于 llmcompressor/core/events/event.py
def should_update(
    self, start: Optional[float], end: Optional[float], update: Optional[float]
) -> bool:
    """
    Determines if the event should trigger an update.

    :param start: The start index to check against, set to None to ignore start.
    :type start: Optional[float]
    :param end: The end index to check against, set to None to ignore end.
    :type end: Optional[float]
    :param update: The update interval, set to None or 0.0 to always update,
        otherwise must be greater than 0.0, defaults to None.
    :type update: Optional[float]
    :return: True if the event should trigger an update, False otherwise.
    :rtype: bool
    """
    current = self.current_index
    logger.debug(
        "Checking if event should update: "
        "current_index={}, start={}, end={}, update={}",
        current,
        start,
        end,
        update,
    )
    if start is not None and current < start:
        return False
    if end is not None and current > end:
        return False
    return update is None or update <= 0.0 or current % update < 1e-10

EventType

基类:Enum

一个枚举,用于定义在模型压缩生命周期中可以触发的不同类型的事件。每个 EventType 的目的是在训练或训练后管道中触发相应的修饰符回调。

参数

  • 初始化

    初始化事件类型。

  • 完成

    完成事件类型。

  • BATCH_START

    批次开始事件类型。

  • 损失计算

    损失计算事件类型。

  • BATCH_END

    批次结束事件类型。

  • CALIBRATION_EPOCH_START

    校准 epoch 开始事件类型。

  • SEQUENTIAL_EPOCH_END

    层校准 epoch 结束事件类型,专门用于 src/llmcompressor/pipelines/sequential/pipeline.py

  • CALIBRATION_EPOCH_END

    校准 epoch 结束事件类型。

  • OPTIM_PRE_STEP

    优化前步骤事件类型。

  • OPTIM_POST_STEP

    优化后步骤事件类型。