跳到内容

llmcompressor.metrics.utils

模块

函数

  • log_ready

    根据给定参数检查是否准备好再次记录

FrequencyManager

FrequencyManager(
    log_frequency: LogStepType = None,
    mode: LoggingModeType = DEFAULT_LOGGING_MODE,
    frequency_type: FrequencyType = DEFAULT_FREQUENCY_TYPE,
)

用于管理日志记录和模型更新频率的类

参数

  • 日志频率

    (LogStepType, default: None ) –

    记录的频率

  • 模式

    (LoggingModeType, default: DEFAULT_LOGGING_MODE ) –

    要使用的日志记录模式,可以是 "on_change" 或 "exact","on_change" 将在上次记录以来模型已更新时记录,"exact" 将无论模型更新如何都在给定频率记录

  • 频率类型

    (FrequencyType, default: DEFAULT_FREQUENCY_TYPE ) –

    要使用的频率类型,可以是 "epoch"、"step" 或 "batch" 来控制频率管理器跟踪的内容,例如,如果频率类型是 "epoch",则频率管理器将跟踪自上次日志记录以来已过的 epoch 数,如果频率类型是 "step",则频率管理器将跟踪优化器步骤数

方法

  • log_ready

    检查频率管理器是否准备好记录

  • log_written

    将上次记录的步数设置为给定步数

  • model_updated

    将上次模型更新设置为给定步数

属性

源文件位于 llmcompressor/metrics/utils/frequency_manager.py
def __init__(
    self,
    log_frequency: LogStepType = None,
    mode: LoggingModeType = DEFAULT_LOGGING_MODE,
    frequency_type: FrequencyType = DEFAULT_FREQUENCY_TYPE,
):
    # sets self._logging_mode and self._check_model_update
    self._logging_mode = self._set_logging_mode(mode=mode)

    # sets self._frequency_type and self._valid_python_types
    self.frequency_type = self._set_frequency_type(frequency_type=frequency_type)

    self._validate_log_frequency(log_frequency=log_frequency)
    self._log_frequency = log_frequency

    self.last_log_step: LogStepType = None
    self.last_model_update_step: LogStepType = None

is_epoch_frequency_manager property

is_epoch_frequency_manager: bool

返回

  • bool

    如果频率管理器正在跟踪 epoch,则为 True,否则为 False

is_optim_frequency_manager property

is_optim_frequency_manager: bool

返回

  • bool

    如果频率管理器正在跟踪优化器步数,则为 True,否则为 False

log_frequency property writable

log_frequency: LogStepType

返回

  • LogStepType

    记录频率

log_ready

log_ready(
    current_log_step: LogStepType,
    check_model_update: bool = False,
)

检查频率管理器是否准备好记录 就绪条件: - 记录频率不为 None - 当前记录步数不为 None - 当前记录步数大于或等于上次记录步数加上记录频率 - 如果 check_model_update 为 True,或 self._check_model_update 为 True,则上次模型更新步数必须大于或等于上次记录步数,并且当前记录步数必须大于或等于上次模型更新步数加上记录频率

参数

  • 当前日志步骤

    (LogStepType) –

    当前记录步数

  • 检查模型更新

    (bool, 默认值: False ) –

    如果为 True,将检查自上次记录步数以来模型是否已更新,以及自上次模型更新以来是否已过 _log_frequency 步数;默认为 False。

返回

  • 如果已再次达到记录频率,则为 True,否则为 False

源文件位于 llmcompressor/metrics/utils/frequency_manager.py
def log_ready(
    self,
    current_log_step: LogStepType,
    check_model_update: bool = False,
):
    """
    Check if the frequency manager is ready to log
    Conditions for readiness:
        - log frequency is not None
        - current log step is None
        - current log step greater than or equal to the last log step
            plus the log frequency
        - if check_model_update is True, or self._check_model_update is True,
            then the last model update step must be greater than or equal
            to the last log step, and the current log step must be greater
            than or equal to the last model update step plus the log frequency

    :param current_log_step: The current log step
    :param check_model_update: If True, will check if the model has been updated
        since the last log step and if _log_frequency steps have passed since the
        last model update; Defaults to False.
    :return: True if the frequency manager is ready to log,
        False otherwise
    """
    # check_model_update is used to override self._check_model_update
    # e.g. if check_model_update is True, then the model update check
    # will be performed even if self._check_model_update is False

    check_model_update = check_model_update or self._check_model_update

    return log_ready(
        current_log_step=current_log_step,
        last_log_step=self.last_log_step,
        log_frequency=self.log_frequency,
        last_model_update_step=self.last_model_update_step,
        check_model_update=check_model_update,
    )

log_written

log_written(step: LogStepType = None) -> None

将上次记录的步数设置为给定步数

:post-cond: 上次记录步数设置为给定步数

参数

  • 步骤

    (LogStepType, default: None ) –

    要设置为上次记录步数的步数

源文件位于 llmcompressor/metrics/utils/frequency_manager.py
def log_written(self, step: LogStepType = None) -> None:
    """
    Sets the last log step to the given step

    :param step: The step to set the last log step to
    :post-cond: The last log step is set to the given step
    """
    self._validate_log_step(log_step=step)
    self.last_log_step = step

model_updated

model_updated(step: LogStepType = None) -> None

将上次模型更新设置为给定步数

:post-cond: 上次模型更新步数设置为给定步数

参数

  • 步骤

    (LogStepType, default: None ) –

    要设置为上次模型更新的步数

源文件位于 llmcompressor/metrics/utils/frequency_manager.py
def model_updated(self, step: LogStepType = None) -> None:
    """
    Sets the last model update to the given step

    :param step: The step to set the last model update to
    :post-cond: The last model update step is set to the given step
    """
    self._validate_log_step(log_step=step)
    self.last_model_update_step = step

log_ready

log_ready(
    current_log_step: LogStepType | None,
    last_log_step: LogStepType | None,
    log_frequency: LogStepType | None,
    last_model_update_step: LogStepType = None,
    check_model_update: bool = False,
)

根据给定参数检查是否准备好再次记录 (FrequencyManager().log_ready 的无状态版本)

就绪条件: - 记录频率不为 None - 当前记录步数不为 None - 当前记录步数大于或等于上次记录步数加上记录频率 - 如果 check_model_update 为 True,则上次模型更新步数必须大于或等于上次记录步数,并且当前记录步数必须大于或等于上次模型更新步数加上记录频率

参数

  • 当前日志步骤

    (LogStepType | None) –

    当前记录步数

  • 上次日志步骤

    (LogStepType | None) –

    上次记录发生的步数

  • 日志频率

    (LogStepType | None) –

    记录的频率

  • last_model_update_step

    (LogStepType, default: None ) –

    上次模型更新发生的步数

  • 检查模型更新

    (bool, 默认值: False ) –

    如果为 True,将检查自上次记录步数以来模型是否已更新,以及自上次模型更新以来是否已过 log_frequency 步数;默认为 False。

返回

  • 如果再次达到记录频率,则为 True,否则为 False

源文件位于 llmcompressor/metrics/utils/frequency_manager.py
def log_ready(
    current_log_step: LogStepType | None,
    last_log_step: LogStepType | None,
    log_frequency: LogStepType | None,
    last_model_update_step: LogStepType = None,
    check_model_update: bool = False,
):
    """
    Check if we are ready to log again based on the given parameters
    (Stateless version of FrequencyManager().log_ready)

    Conditions for readiness:
        - log frequency is not None
        - current log step is None
        - current log step greater than or equal to the last log step
            plus the log frequency
        - if check_model_update is True, then the last model update step
            must be greater than or equal to the last log step, and the
            current log step must be greater than or equal to the
            last model update step plus the log frequency

    :param current_log_step: The current log step
    :param last_log_step: The last step at which logging occurred
    :param log_frequency: The frequency to log at
    :param last_model_update_step: The last step at which the model was updated
    :param check_model_update: If True, will check if the model has been updated
        since the last log step and if log_frequency steps have passed since the
        last model update; Defaults to False.
    :return: True if logging cadence has been reached again False otherwise
    """
    # format is used to avoid floating point errors
    # e.g. 0.1 + 0.2 != 0.3
    # format(0.1 + 0.2, ".4f") == format(0.3, ".4f")

    cadence_reached: bool = log_frequency is not None and (
        current_log_step is None
        or last_log_step is None
        or current_log_step >= float(format(last_log_step + log_frequency, ".4f"))
    )

    if not cadence_reached or not check_model_update:
        # early return if cadence not reached or,
        # model update check not requested
        return cadence_reached

    model_updated_since_last_log: bool = (
        last_model_update_step is None
        or last_log_step is None
        or current_log_step is None
        or (
            last_model_update_step >= last_log_step
            and current_log_step
            >= float(format(log_frequency + last_model_update_step, ".4f"))
        )
    )

    return cadence_reached and model_updated_since_last_log