Skip to content

llmcompressor.pipelines.sequential.error_logging

Functions:

  • compute_sqnr –

    Compute the Signal-to-Quantization-Noise Ratio (SQNR), in decibels, from

  • compute_subgraph_sqnr –

    Aggregate per-batch signal/noise power sums and compute the subgraph's

  • process_batch_error –

    Process SQNR tracking for a single batch during the propagation pass. When

compute_sqnr

compute_sqnr(
    signal_power_sum: float, noise_power_sum: float
) -> float

Compute the Signal-to-Quantization-Noise Ratio (SQNR), in decibels, from accumulated signal and noise power sums; higher values indicate less distortion. Callers should sum power across every batch in a subgraph (see :func:_accumulate_batch_power) and call this once on the totals, since SQNR is a logarithmic ratio and cannot be meaningfully averaged batch-by-batch.

Parameters:

  • signal_power_sum (float) –

    sum of squared pre-compression activation values

  • noise_power_sum (float) –

    sum of squared compression noise values

Returns:

  • float –

    SQNR in dB, or inf if there is no noise

Source code in src/llmcompressor/pipelines/sequential/error_logging.py
def compute_sqnr(signal_power_sum: float, noise_power_sum: float) -> float:
    """
    Compute the Signal-to-Quantization-Noise Ratio (SQNR), in decibels, from
    accumulated signal and noise power sums; higher values indicate less
    distortion. Callers should sum power across every batch in a subgraph
    (see :func:`_accumulate_batch_power`) and call this once on the totals,
    since SQNR is a logarithmic ratio and cannot be meaningfully averaged
    batch-by-batch.

    :param signal_power_sum: sum of squared pre-compression activation values
    :param noise_power_sum: sum of squared compression noise values
    :return: SQNR in dB, or ``inf`` if there is no noise
    """
    if noise_power_sum == 0:
        return float("inf")
    return 10 * math.log10(signal_power_sum / noise_power_sum)

compute_subgraph_sqnr

compute_subgraph_sqnr(
    batch_powers: list[tuple[float, float]],
) -> float

Aggregate per-batch signal/noise power sums and compute the subgraph's overall SQNR in one call.

Parameters:

  • batch_powers (list[tuple[float, float]]) –

    list of (signal power sum, noise power sum) tuples, one per batch, as returned by :func:process_batch_error

Returns:

  • float –

    SQNR in dB, or inf if there is no noise

Source code in src/llmcompressor/pipelines/sequential/error_logging.py
def compute_subgraph_sqnr(batch_powers: list[tuple[float, float]]) -> float:
    """
    Aggregate per-batch signal/noise power sums and compute the subgraph's
    overall SQNR in one call.

    :param batch_powers: list of (signal power sum, noise power sum) tuples,
        one per batch, as returned by :func:`process_batch_error`
    :return: SQNR in dB, or ``inf`` if there is no noise
    """
    signal_power_sum = sum(signal for signal, _ in batch_powers)
    noise_power_sum = sum(noise for _, noise in batch_powers)
    return compute_sqnr(signal_power_sum, noise_power_sum)

process_batch_error

process_batch_error(
    cache: IntermediatesCache,
    activations: IntermediatesCache,
    batch_idx: int,
    output: dict[str, Any],
    propagate_error: bool,
    consumed_names: set[str],
) -> tuple[float, float] | None

Process SQNR tracking for a single batch during the propagation pass. When propagate_error is disabled, first transfer this subgraph's unquantized outputs into activations for the next subgraph, since pass 2 doesn't do so itself in that case. Then compare pre/post-compression outputs to accumulate this batch's signal/noise power (see :func:_accumulate_batch_power).

Parameters:

  • cache (IntermediatesCache) –

    intermediates cache dedicated to log_sequential_error

  • activations (IntermediatesCache) –

    intermediates cache shared across the pipeline

  • batch_idx (int) –

    index of the current propagation batch

  • output (dict[str, Any]) –

    subgraph output dict from the propagation (post-compression) pass

  • propagate_error (bool) –

    whether quantized outputs are fed to the next subgraph

  • consumed_names (set[str]) –

    names no longer needed by any subsequent subgraph

Returns:

  • tuple[float, float] | None –

    (signal power sum, noise power sum) for this batch, or None if either tensor is unavailable

Source code in src/llmcompressor/pipelines/sequential/error_logging.py
def process_batch_error(
    cache: IntermediatesCache,
    activations: IntermediatesCache,
    batch_idx: int,
    output: dict[str, Any],
    propagate_error: bool,
    consumed_names: set[str],
) -> tuple[float, float] | None:
    """
    Process SQNR tracking for a single batch during the propagation pass. When
    propagate_error is disabled, first transfer this subgraph's unquantized
    outputs into activations for the next subgraph, since pass 2 doesn't do
    so itself in that case. Then compare pre/post-compression outputs to
    accumulate this batch's signal/noise power (see
    :func:`_accumulate_batch_power`).

    :param cache: intermediates cache dedicated to log_sequential_error
    :param activations: intermediates cache shared across the pipeline
    :param batch_idx: index of the current propagation batch
    :param output: subgraph output dict from the propagation (post-compression) pass
    :param propagate_error: whether quantized outputs are fed to the next subgraph
    :param consumed_names: names no longer needed by any subsequent subgraph
    :return: (signal power sum, noise power sum) for this batch, or None if
        either tensor is unavailable
    """
    if not propagate_error:
        activations.transfer(cache, batch_idx)
        activations.delete(batch_idx, consumed_names)

    return _accumulate_batch_power(cache, batch_idx, output)