跳到内容

llmcompressor.metrics.utils.frequency_manager

用于日志记录的频率管理实用程序。

提供类和函数,用于管理日志记录频率,并在训练和压缩工作流中确定何时记录指标。支持基于 epoch 和基于 step 的日志记录,具有可配置的模式和间隔。

函数

  • log_ready

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

FrequencyManager

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

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

参数

  • 日志频率

    (LogStepType, 默认: None ) –

    记录的频率

  • 模式

    (LoggingModeType, 默认: DEFAULT_LOGGING_MODE ) –

    使用的日志模式,可以是 "on_change" 或 "exact","on_change" 将在自上次记录以来模型更新时进行记录,"exact" 将根据给定频率进行记录,无论模型是否更新。

  • 频率类型

    (FrequencyType, 默认: DEFAULT_FREQUENCY_TYPE ) –

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

方法

  • log_ready

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

  • log_written

    将上次记录步骤设置为给定步骤

  • model_updated

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

属性

Source code in 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

Source code in 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, 默认: None ) –

    要将上次日志步数设置为的步数

Source code in 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, 默认: None ) –

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

Source code in 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, 默认: None ) –

    上次更新模型的步数

  • 检查模型更新

    (bool, 默认值: False ) –

    如果为 True,则检查自上次日志步数以来模型是否已更新,并且自上次模型更新以来是否已过 log_frequency 步数;默认为 False。

返回

  • 如果再次达到日志节奏,则为 True,否则为 False

Source code in 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