|
| 1 | +# Copyright 2026 TIER IV, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Self-describing quantized checkpoints. |
| 16 | +
|
| 17 | +A quantized checkpoint carries, next to its ``state_dict``, a ``quantization`` entry |
| 18 | +holding the :class:`~autoware_ml.quantization.config.QuantizationConfig` that built |
| 19 | +its tree and the :class:`~autoware_ml.quantization.plan.PlacementRecord` the |
| 20 | +build recorded. That is everything a later ``build_model`` needs to rebuild the |
| 21 | +identical quantized tree and verify it — so ``deploy`` and ``test`` never read a |
| 22 | +``quantization`` config section, and PTQ and QAT checkpoints (a Lightning |
| 23 | +checkpoint with the same entry) load through one path. |
| 24 | +
|
| 25 | +There are no sidecar files: the calibrated ``_amax`` buffers live in the |
| 26 | +``state_dict`` like any other buffer. |
| 27 | +""" |
| 28 | + |
| 29 | +from __future__ import annotations |
| 30 | + |
| 31 | +import logging |
| 32 | +from collections.abc import Mapping, Sequence |
| 33 | +from dataclasses import dataclass |
| 34 | +from pathlib import Path |
| 35 | +from typing import Any |
| 36 | + |
| 37 | +import torch |
| 38 | + |
| 39 | +from autoware_ml.quantization.config import QuantizationConfig |
| 40 | +from autoware_ml.quantization.plan import PlacementRecord |
| 41 | + |
| 42 | +logger = logging.getLogger(__name__) |
| 43 | + |
| 44 | +#: Top-level checkpoint key holding the quantization description. |
| 45 | +QUANTIZATION_KEY = "quantization" |
| 46 | + |
| 47 | + |
| 48 | +@dataclass(frozen=True) |
| 49 | +class QuantizationDescription: |
| 50 | + """What a quantized checkpoint says about itself. |
| 51 | +
|
| 52 | + The embedded format carries no version field on purpose: checkpoints are |
| 53 | + reproducible artifacts (re-run ``autoware-ml quantize``), and a format drift |
| 54 | + fails loudly anyway — ``QuantizationConfig.from_dict`` rejects unknown keys and |
| 55 | + a missing key raises here. |
| 56 | + """ |
| 57 | + |
| 58 | + config: QuantizationConfig |
| 59 | + placement_record: PlacementRecord |
| 60 | + |
| 61 | + def to_payload(self) -> dict[str, Any]: |
| 62 | + """Serialize for embedding under :data:`QUANTIZATION_KEY`.""" |
| 63 | + return { |
| 64 | + "config": self.config.to_dict(), |
| 65 | + "placement_record": self.placement_record.to_json_dict(), |
| 66 | + } |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def from_payload(cls, payload: Mapping[str, Any]) -> QuantizationDescription: |
| 70 | + """Deserialize an embedded payload. |
| 71 | +
|
| 72 | + Raises: |
| 73 | + KeyError: When the payload does not have this build's layout — the |
| 74 | + checkpoint predates a format change; re-produce it with |
| 75 | + ``autoware-ml quantize``. |
| 76 | + """ |
| 77 | + return cls( |
| 78 | + config=QuantizationConfig.from_dict(payload["config"]), |
| 79 | + placement_record=PlacementRecord.from_json_dict(payload["placement_record"]), |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def attach_quantization(checkpoint: dict[str, Any], description: QuantizationDescription) -> None: |
| 84 | + """Embed ``description`` into a checkpoint dict in place (used by ``on_save_checkpoint``).""" |
| 85 | + checkpoint[QUANTIZATION_KEY] = description.to_payload() |
| 86 | + |
| 87 | + |
| 88 | +def save_quantized_checkpoint( |
| 89 | + model: torch.nn.Module, path: str | Path, description: QuantizationDescription |
| 90 | +) -> Path: |
| 91 | + """Write ``{"state_dict", "quantization"}`` — the PTQ producer's output. |
| 92 | +
|
| 93 | + The layout is a subset of a Lightning checkpoint, so PTQ and QAT checkpoints read |
| 94 | + identically. |
| 95 | + """ |
| 96 | + path = Path(path) |
| 97 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 98 | + checkpoint: dict[str, Any] = {"state_dict": model.state_dict()} |
| 99 | + attach_quantization(checkpoint, description) |
| 100 | + torch.save(checkpoint, path) |
| 101 | + logger.info( |
| 102 | + "Saved quantized checkpoint: %s (%d decisions in the embedded placement record)", |
| 103 | + path, |
| 104 | + len(description.placement_record), |
| 105 | + ) |
| 106 | + return path |
| 107 | + |
| 108 | + |
| 109 | +def read_quantization(checkpoint: Mapping[str, Any]) -> QuantizationDescription | None: |
| 110 | + """Return the embedded description of a loaded checkpoint dict, or ``None`` for an FP one.""" |
| 111 | + payload = checkpoint.get(QUANTIZATION_KEY) |
| 112 | + if payload is None: |
| 113 | + return None |
| 114 | + return QuantizationDescription.from_payload(payload) |
| 115 | + |
| 116 | + |
| 117 | +def read_quantization_from_file(path: str | Path) -> QuantizationDescription | None: |
| 118 | + """Return the embedded description of a checkpoint file, or ``None`` for an FP one. |
| 119 | +
|
| 120 | + Only the payload is inspected; tensors are memory-mapped, not materialized. |
| 121 | + """ |
| 122 | + checkpoint = torch.load(str(path), map_location="cpu", weights_only=False, mmap=True) |
| 123 | + return read_quantization(checkpoint) |
| 124 | + |
| 125 | + |
| 126 | +def find_quantization( |
| 127 | + weight_paths: Sequence[str | Path], |
| 128 | +) -> tuple[Path, QuantizationDescription] | None: |
| 129 | + """Find the one quantized checkpoint among ``weight_paths``. |
| 130 | +
|
| 131 | + Returns: |
| 132 | + ``(path, description)`` of the quantized checkpoint, or ``None`` when every |
| 133 | + checkpoint is a plain FP one. |
| 134 | +
|
| 135 | + Raises: |
| 136 | + ValueError: When more than one checkpoint is quantized — a quantized tree is a |
| 137 | + whole-model construction; merging two of them is not defined. |
| 138 | + """ |
| 139 | + found = [ |
| 140 | + (Path(path), description) |
| 141 | + for path in weight_paths |
| 142 | + if (description := read_quantization_from_file(path)) is not None |
| 143 | + ] |
| 144 | + if not found: |
| 145 | + return None |
| 146 | + if len(found) > 1: |
| 147 | + raise ValueError( |
| 148 | + "More than one --weights checkpoint is quantized " |
| 149 | + f"({[str(p) for p, _ in found]}); a quantized model loads from exactly one." |
| 150 | + ) |
| 151 | + return found[0] |
0 commit comments