Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

5.3 KB
Raw Blame
# Copyright (c) 2026 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""``CalibrationContext`` — single source of truth for calibration-time state.

This dataclass owns every per-run calibration field shared between
:class:`~auto_round.compressors.base.BaseCompressor` and
:class:`~auto_round.algorithms.quantization.base.BaseQuantizer`:

- Per-batch shape state ``(attention_mask, batch_dim)``
- Calibration parameters ``(batch_size, gradient_accumulate_steps, nsamples, seqlen, dataset, dataloader)``

Both the compressor and the quantizer hold a reference to the same
``CalibrationContext`` instance (wired in ``BaseCompressor._resolve_scheme``).

The dataclass also provides a behavioural helper:

- :meth:`clamp_seqlen` — clamps ``self.seqlen`` to model / tokenizer limits.
"""

from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from auto_round.compressors.base import BaseOrchestrator

from auto_round.logger import logger

__all__ = ["CalibrationContext"]


@dataclass
class CalibrationContext:
    """Authoritative runtime store for calibration state.

    See module docstring for field semantics and design rationale.
    """

    # # ── Capture buffers ────────────────────────────────────────────────────
    # inputs: dict = field(default_factory=dict)
    # to_cached_layers: list = field(default_factory=list)
    # last_cache_name: Optional[str] = None
    # blocks_requiring_input_ids: list = field(default_factory=list)
    #

    batch_dim: int | None = None

    # ── Calibration parameters ─────────────────────────────────────────────
    batch_size: int = 8
    nsamples: int = 128
    seqlen: int = 2048
    dataset: Any = None
    is_only_supported_bs1: bool = False
    orig_batch_size: int = 8  # some models only support batch_size 1,we need keep this info to set grad_accumulate_step

    # ── Compressor / quantizer round-tripping ──────────────────────────────

    @classmethod
    def from_orchestrator(cls, orchestrator: "BaseOrchestrator") -> "CalibrationContext":
        """Return the live shared instance held by ``orchestrator``.

        The orchestrator always owns a ``_calibration_state`` after
        ``BaseOrchestrator.__init__``, so the legacy "snapshot" fallback is
        no longer required.  We still allow it for safety in case a custom
        subclass forgets to call ``super().__init__``.
        """
        live = getattr(orchestrator, "calibration_context", None)
        if isinstance(live, cls):
            return live
        # Legacy fallback (no shared instance wired yet).
        return cls(
            # inputs=getattr(orchestrator, "inputs", {}) or {},
            # to_cached_layers=getattr(orchestrator, "to_cached_layers", []) or [],
            # last_cache_name=getattr(orchestrator, "last_cache_name", None),
            # blocks_requiring_input_ids=getattr(orchestrator, "blocks_requiring_input_ids", []) or [],
            batch_size=getattr(orchestrator, "batch_size", 8) or 8,
            nsamples=getattr(orchestrator, "nsamples", 128) or 128,
            seqlen=getattr(orchestrator, "seqlen", 2048) or 2048,
            dataset=getattr(orchestrator, "dataset", None),
            orig_batch_size=getattr(orchestrator, "batch_size", 8) or 8,
        )

    # ── Behavioural helpers ────────────────────────────────────────────────

    def clamp_seqlen(self, model_context: Any) -> None:
        """Clamp :attr:`seqlen` to model / tokenizer maximum lengths.

        Migrated verbatim from ``Compressor._check_compatibility``.
        Safe to call multiple times; warns on each clamp.
        """
        if self.seqlen is None:
            return
        model = getattr(model_context, "model", None)
        max_pos = getattr(getattr(model, "config", None), "max_position_embeddings", None)
        if max_pos is not None and max_pos < self.seqlen:
            logger.warning(f"Change sequence length to {max_pos} due to the limitation of max_position_embeddings")
            self.seqlen = min(self.seqlen, max_pos)

        tokenizer = getattr(model_context, "tokenizer", None)
        tok_max = getattr(tokenizer, "model_max_length", None)
        if tok_max is not None and tok_max < self.seqlen:
            logger.warning(
                f"Change sequence length to {tok_max} due to the limitation of model_max_length. "
                "You can also try to increase the model_max_length to avoid this issue."
            )
            self.seqlen = min(self.seqlen, tok_max)