Files
OBLITERATUS/obliteratus/gsq/coverage.py
Joseph MaglyandClaude Opus 5.5 f50e75aa64 fix(gsq): cast-check retained tensors at load dtype; recheck source around assembly
- Require retained and overwritten floating tensors to stay finite after
  casting to every bound load dtype, checked in bounded chunks, in run
  validation and assembled verification.
- Recompute the GSQ revision and prepared digest immediately before
  spawning save_model.py and after it exits; drift refuses the spawn or
  fails assembly before the output is certified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 14:04:32 -04:00

2963 lines
124 KiB
Python

"""Generic coverage validator for GSQ-quantized checkpoints.
The validator derives the targeted projection list from a Hugging Face
``config.json`` dict, so it covers both hybrid Qwen3.5/Qwen3.8 topologies
(``layer_types`` with linear and full attention) and plain dense stacks.
Module name prefixes match what GSQ writes into its per-layer checkpoint
safetensors: ``model.layers.N...`` for text-only checkpoints and
``model.language_model.layers.N...`` for multimodal ones.
"""
from __future__ import annotations
import json
import re
from dataclasses import dataclass
from math import ceil
from pathlib import Path
from typing import Iterable, Mapping
LINEAR_ATTN_PROJECTIONS = (
"in_proj_qkv",
"in_proj_z",
"in_proj_a",
"in_proj_b",
"out_proj",
)
SELF_ATTN_PROJECTIONS = ("q_proj", "k_proj", "v_proj", "o_proj")
MLP_PROJECTIONS = ("gate_proj", "up_proj", "down_proj")
REQUIRED_PACKED_SUFFIXES = ("weight_packed", "weight_scale", "weight_shape")
# Optional per-projection quantization tensors the symmetric container must
# not carry. GSQ here is symmetric (gptq ``sym: True``; the pinned savers in
# ``src/models/base.py`` / ``qwen35.py`` / ``qwen3.py`` ``save_to_disc`` /
# ``save_attention_to_disc`` write only weight_packed/weight_scale/
# weight_shape): a ``weight_zero_point`` is consumed by compressed-tensors
# during dequantization (NaN/Inf produces non-finite weights) and a
# malformed ``weight_g_idx`` can cause decoding failures. The pinned
# assembler (``save_model.py``) additionally drops ``<prefix>.weight`` from
# the base model whenever ``<prefix>.<suffix>`` is present for any of these
# suffixes, so carrying them silently removes a base weight.
REJECTED_OPTIONAL_QUANT_SUFFIXES = ("weight_zero_point", "weight_g_idx")
# Tensor-key suffixes the pinned GSQ assembler (``save_model.py``) treats as
# replacing a base weight: ``collect_quantized_entries`` gathers every such
# key, and the merge drops ``<prefix>.weight`` from the base model whenever
# ``<prefix>.<suffix>`` is present -- even when no ``weight_packed``
# replacement exists (so ``lm_head.weight_scale`` alone drops
# ``lm_head.weight``). Any key carrying one of these suffixes must therefore
# belong to an expected projection.
QUANT_SUFFIXES = (
"weight_packed",
"weight_scale",
"weight_shape",
"weight_zero_point",
"weight_g_idx",
)
TEXT_ONLY_PREFIX = "model"
MULTIMODAL_PREFIX = "model.language_model"
# The pinned GSQ assembler (``save_model.collect_quantized_entries``) only
# consumes run-dir files ending in ``.safetensors`` whose name matches
# ``layers_(\\d+)``; the layer index comes from the FILENAME and later files
# silently overwrite earlier entries with the same tensor key. The validator
# must consume exactly that shard set: anything else is either invisible to
# assembly (a renamed-away shard) or a guess about what assembly sees.
ASSEMBLER_LAYER_FILENAME_PATTERN = re.compile(r"layers_(\d+)")
CHECKPOINT_LAYER_KEY_PATTERN = re.compile(r"\.layers\.(\d+)\.")
# GSQ stores 2-bit codes in its 4-bit pack container with group size 128.
DEFAULT_PACK_BITS = 4
DEFAULT_GROUP_SIZE = 128
# Safetensors header dtype tags for floating-point tensors, mapped to the
# config dtype name with the same element type (mirrors the workflow's
# render-time mapping; integer/bool tags carry no load dtype).
_SAFETENSORS_TAG_TO_DTYPE_NAME = {
"F16": "float16",
"BF16": "bfloat16",
"F32": "float32",
}
def _torch_dtype_from_name(name: object):
"""Map a load-dtype name to its torch dtype, failing closed.
Accepts canonical names (``bfloat16``/``float16``/``float32``), common
aliases (``bf16``/``fp16``/``fp32``/``half``/``float``), and the
``torch.<name>`` spelling model configs use. A torch dtype passes
through; ``None`` passes through as ``None`` (no bound load dtype).
Anything else raises instead of certifying against an unknown dtype.
"""
import torch
if name is None or isinstance(name, torch.dtype):
return name
if not isinstance(name, str):
raise ValueError(
f"load dtype must be a torch dtype or dtype name, found {name!r}"
)
key = name.strip().lower()
if key.startswith("torch."):
key = key[len("torch."):]
table = {
"bfloat16": torch.bfloat16,
"bf16": torch.bfloat16,
"float16": torch.float16,
"fp16": torch.float16,
"half": torch.float16,
"float32": torch.float32,
"fp32": torch.float32,
"float": torch.float32,
}
try:
return table[key]
except KeyError:
raise ValueError(
f"unusable load dtype {name!r}: expected one of"
" bfloat16/bf16, float16/fp16, float32/fp32"
) from None
def _base_float_load_dtypes(base: Path) -> list:
"""Return the distinct float weight dtypes a base-model dir can load as.
Reads only the JSON headers of the top-level ``*.safetensors`` files
(never tensor bytes) and collects the torch dtypes of floating-point
tensors with at least two dimensions -- the Linear/embedding weights
whose dtype is the model's load dtype. Transformers casts every
floating checkpoint tensor (including ``weight_scale``) to the
requested load dtype on load (see ``core_model_loading`` step 3: a
pre-quantized floating tensor keeps the requested ``dtype``), so
scales must decode finitely in each dtype found here. Empty when the
dir holds no float 2-D+ weights.
"""
import struct
try:
shards = sorted(
path
for path in base.iterdir()
if path.name.endswith(".safetensors") and path.is_file()
)
except OSError as exc:
raise ValueError(
f"cannot list base model dir {base}: {exc}"
) from exc
tags: set[str] = set()
for shard in shards:
try:
with open(shard, "rb") as handle:
prefix = handle.read(8)
if len(prefix) != 8:
raise ValueError(f"safetensors file {shard} is truncated")
header_len = struct.unpack("<Q", prefix)[0]
if header_len > 256 * 1024**2:
raise ValueError(
f"safetensors file {shard} declares an implausible"
f" header length ({header_len} bytes)"
)
raw = handle.read(header_len)
if len(raw) != header_len:
raise ValueError(f"safetensors file {shard} is truncated")
header = json.loads(raw.decode("utf-8"))
except (OSError, UnicodeDecodeError, json.JSONDecodeError,
ValueError) as exc:
raise ValueError(
f"cannot read safetensors header of {shard}: {exc}"
) from exc
if not isinstance(header, dict):
raise ValueError(
f"safetensors file {shard} header must be a JSON object"
)
for key, entry in header.items():
if key == "__metadata__" or not isinstance(entry, dict):
continue
shape = entry.get("shape")
if not isinstance(shape, list) or len(shape) < 2:
continue
tag = entry.get("dtype")
if (
isinstance(tag, str)
and tag in _SAFETENSORS_TAG_TO_DTYPE_NAME
):
tags.add(tag)
return [
_torch_dtype_from_name(_SAFETENSORS_TAG_TO_DTYPE_NAME[tag])
for tag in sorted(tags)
]
def _assembled_config_load_dtypes(config: Mapping) -> list:
"""Return load dtypes named by an assembled ``config.json``.
Transformers resolves an omitted load-dtype request from the config's
``torch_dtype`` (``dtype="auto"``), so either spelling present here
names a dtype the output may load -- and be dequantized -- under, and
scales must decode finitely in each. ``None`` entries are skipped;
any other unusable value fails closed instead of certifying against
an unknown dtype.
"""
found: list = []
for key in ("torch_dtype", "dtype"):
value = config.get(key)
if value is None:
continue
# Non-string, non-null declarations raise here instead of
# certifying output that may load under an unknown dtype.
dtype = _torch_dtype_from_name(value)
if dtype not in found:
found.append(dtype)
return found
QWEN38_NUM_LAYERS = 64
QWEN38_LINEAR_ATTENTION_LAYERS = 48
QWEN38_FULL_ATTENTION_LAYERS = 16
QWEN38_MODULE_COUNT = 496
# Chat/inference metadata the patched assembler copies verbatim
# (``save_model.copy_non_weight_files`` plus the ``chat_templates/``
# subdirectory's ``*.jinja`` files, preserving the subdirectory).
# Every file present in the base dir must be bound by hash and present
# in the assembled output with identical bytes.
_TEMPLATE_NON_WEIGHT_FILENAMES = frozenset(
{
"chat_template.jinja",
"chat_template.json",
"added_tokens.json",
"processor_config.json",
"video_preprocessor_config.json",
}
)
CHAT_TEMPLATES_SUBDIR = "chat_templates"
def detect_module_prefix(config: Mapping) -> str:
"""Return the checkpoint tensor prefix for a HF config dict.
Multimodal Qwen3.5 configs nest the text model under ``text_config`` and
store checkpoint tensors under ``model.language_model.*``; text-only
configs store them under ``model.*``. This is only a heuristic for when
no model weights are available; prefer
:func:`detect_module_prefix_from_model_weights`, which reads the actual
checkpoint tensor names.
"""
if isinstance(config.get("text_config"), dict):
return MULTIMODAL_PREFIX
return TEXT_ONLY_PREFIX
def detect_checkpoint_prefix(checkpoint_names: Iterable[str]) -> str:
"""Return the tensor prefix for actual checkpoint tensor names.
Mirrors the adapter's ``detect_qwen35_model_prefix`` (applied to the
prepared GSQ tree): names under ``model.layers.*`` select ``model``,
which wins when both layouts are present; otherwise names under
``model.language_model.layers.*`` select ``model.language_model``.
A layout with neither prefix cannot be certified and raises.
"""
names = list(checkpoint_names)
if any(name.startswith("model.layers.") for name in names):
return TEXT_ONLY_PREFIX
if any(
name.startswith("model.language_model.layers.") for name in names
):
return MULTIMODAL_PREFIX
raise ValueError(
"unsupported checkpoint layout: expected model.layers.*"
" or model.language_model.layers.* tensors"
)
def detect_module_prefix_from_model_weights(
model_dir: str | Path,
) -> str | None:
"""Detect the tensor prefix from base-model weights, if available.
Reads ``model.safetensors.index.json`` (``weight_map`` keys) or, when
the model is a single safetensors file, that file's tensor keys. Both
live next to the model's ``config.json``. Returns ``None`` when no
weights are available (no index and not exactly one safetensors file),
in which case callers either fail closed or fall back to
:func:`detect_module_prefix`. Unrecognised layouts raise instead of
guessing; when both layouts appear, the text-only prefix wins, as in
:func:`detect_checkpoint_prefix`.
"""
base = Path(model_dir)
index_path = base / "model.safetensors.index.json"
if index_path.is_file():
try:
payload = json.loads(index_path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
raise ValueError(
f"cannot read model weight index {index_path}: {exc}"
) from exc
weight_map = payload.get("weight_map") if isinstance(payload, dict) else None
if not isinstance(weight_map, dict) or not weight_map:
raise ValueError(
f"model weight index {index_path} has no usable weight_map"
)
return detect_checkpoint_prefix(weight_map.keys())
shards = sorted(base.glob("*.safetensors"))
if len(shards) != 1:
return None
from safetensors import safe_open
with safe_open(shards[0], framework="pt", device="cpu") as handle:
names = list(handle.keys())
if not names:
raise ValueError(f"model weights {shards[0]} hold no tensors")
return detect_checkpoint_prefix(names)
def qwen38_layer_types(*, full_interval: int = 4) -> tuple[str, ...]:
"""Return the 64-entry Qwen3.8 hybrid topology (48 linear + 16 full).
Every ``full_interval``-th layer (1-based) is full attention. Callers
validating a real checkpoint should pass that model's own
``layer_types`` from ``config.json`` instead of this helper.
"""
if full_interval <= 0:
raise ValueError("full_interval must be positive")
layer_types: list[str] = []
for index in range(1, QWEN38_NUM_LAYERS + 1):
if index % full_interval == 0:
layer_types.append("full_attention")
else:
layer_types.append("linear_attention")
return tuple(layer_types)
QWEN35_DENSE_MODEL_TYPES = frozenset({"qwen3_5", "qwen3_5_text"})
_MOE_COUNT_FIELDS = ("num_experts", "n_routed_experts", "num_local_experts")
_SUPPORTED_LAYER_TYPES = ("linear_attention", "full_attention")
def _reject_uncertifiable_topology(scope: Mapping, where: str) -> None:
"""Reject MoE/sparse topologies the dense validator cannot certify.
Only dense llama/qwen3-style stacks and Qwen3.5/3.8 hybrid dense
topologies are supported. Anything declaring experts, MLP-only layers,
or sparse-step layer selection gets dense targets wrong (expert
projections instead of gate/up/down, or missing attention), so it
raises instead of silently validating the wrong module list.
"""
for field in _MOE_COUNT_FIELDS:
value = scope.get(field)
if value is None or value is False or value == 0:
continue
raise ValueError(
f"unsupported MoE topology in {where}: {field}={value!r}"
)
mlp_only = scope.get("mlp_only_layers")
if mlp_only is not None and mlp_only != []:
raise ValueError(
f"unsupported topology in {where}:"
f" mlp_only_layers={mlp_only!r} is not empty"
)
sparse_step = scope.get("decoder_sparse_step")
if sparse_step is not None and sparse_step is not False and sparse_step != 0:
raise ValueError(
f"unsupported topology in {where}:"
f" decoder_sparse_step={sparse_step!r} implies sparse layers"
)
def _checked_layer_count(num_layers: object) -> int:
if (
num_layers is None
or isinstance(num_layers, bool)
or not isinstance(num_layers, int)
or num_layers <= 0
):
raise ValueError(
"config must provide layer_types or a positive"
f" num_hidden_layers, found {num_layers!r}"
)
return num_layers
def _derive_qwen35_layer_types(
scope: Mapping, num_layers: int
) -> list[str] | None:
"""Derive a Qwen3.5 hybrid topology when ``layer_types`` is absent.
Mirrors transformers ``Qwen3_5TextConfig``: without ``layer_types``,
every ``full_attention_interval``-th layer (1-based) is full attention
and the rest are linear attention (default interval 4). Applies only
to declared Qwen3.5 dense model types; anything else stays dense.
"""
model_type = scope.get("model_type")
model_type = model_type.lower() if isinstance(model_type, str) else ""
if "moe" in model_type:
raise ValueError(
f"unsupported MoE topology: model_type={model_type!r}"
)
if model_type not in QWEN35_DENSE_MODEL_TYPES:
return None
interval = scope.get("full_attention_interval", 4)
if (
isinstance(interval, bool)
or not isinstance(interval, int)
or interval <= 0
):
raise ValueError(
"Qwen3.5 config needs a positive full_attention_interval"
f" to derive layer_types, found {interval!r}"
)
return [
"full_attention" if (index + 1) % interval == 0 else "linear_attention"
for index in range(num_layers)
]
def _reject_moe_model_type(scope: Mapping, where: str) -> None:
"""Reject MoE model types even when expert counts are omitted.
A config declaring ``layer_types`` without expert-count fields would
otherwise look certifiable, but an MoE ``model_type`` means the dense
projection list below targets the wrong modules.
"""
model_type = scope.get("model_type")
if isinstance(model_type, str) and "moe" in model_type.lower():
raise ValueError(
f"unsupported MoE topology in {where}: model_type={model_type!r}"
)
def _resolve_topology(config: Mapping) -> tuple[list[str] | None, int]:
text = config.get("text_config")
nested = text if isinstance(text, dict) else {}
_reject_uncertifiable_topology(nested, "text_config")
_reject_uncertifiable_topology(config, "config")
_reject_moe_model_type(nested, "text_config")
_reject_moe_model_type(config, "config")
layer_types = nested.get("layer_types", config.get("layer_types"))
num_layers = nested.get("num_hidden_layers", config.get("num_hidden_layers"))
if layer_types is None:
count = _checked_layer_count(num_layers)
merged = dict(config)
merged.update(nested)
return _derive_qwen35_layer_types(merged, count), count
if not isinstance(layer_types, (list, tuple)):
raise ValueError(
"config layer_types must be a non-empty list of supported"
f" layer types, found {layer_types!r}"
)
layer_types = list(layer_types)
if not layer_types:
raise ValueError("config layer_types must not be empty")
if num_layers is None:
num_layers = len(layer_types)
num_layers = _checked_layer_count(num_layers)
if len(layer_types) != num_layers:
raise ValueError(
f"layer_types has {len(layer_types)} entries but num_hidden_layers"
f" is {num_layers}"
)
for entry in layer_types:
if entry not in _SUPPORTED_LAYER_TYPES:
raise ValueError(f"unsupported layer type: {entry!r}")
return layer_types, num_layers
def num_config_layers(config: Mapping) -> int:
"""Return the layer count a HF config dict declares."""
_, num_layers = _resolve_topology(config)
return num_layers
def expected_gsq_modules(
config: Mapping,
*,
quantize_attention: bool = True,
quantize_mlp: bool = True,
module_prefix: str | None = None,
expected_count: int | None = None,
) -> tuple[str, ...]:
"""Derive the targeted GSQ projection list from a HF config dict."""
if not quantize_attention and not quantize_mlp:
raise ValueError("at least one of attention or MLP must be quantized")
layer_types, num_layers = _resolve_topology(config)
prefix = module_prefix or detect_module_prefix(config)
modules: list[str] = []
for index in range(num_layers):
layer_type = layer_types[index] if layer_types is not None else "dense"
base = f"{prefix}.layers.{index}"
if layer_type == "linear_attention":
if quantize_attention:
modules.extend(
f"{base}.linear_attn.{name}"
for name in LINEAR_ATTN_PROJECTIONS
)
elif layer_type in ("full_attention", "dense"):
if quantize_attention:
modules.extend(
f"{base}.self_attn.{name}" for name in SELF_ATTN_PROJECTIONS
)
if quantize_mlp:
modules.extend(f"{base}.mlp.{name}" for name in MLP_PROJECTIONS)
if len(set(modules)) != len(modules):
raise ValueError("derived projection list contains duplicates")
if expected_count is not None and len(modules) != expected_count:
raise ValueError(
f"derived {len(modules)} projections, expected {expected_count}"
)
return tuple(modules)
@dataclass(frozen=True)
class GSQCoverage:
expected_modules: tuple[str, ...]
packed_modules: tuple[str, ...]
missing_modules: tuple[str, ...]
dense_fallback_modules: tuple[str, ...]
@property
def passed(self) -> bool:
return not self.missing_modules and not self.dense_fallback_modules
def _consumed_shard_files(checkpoint_dir: Path) -> list[tuple[Path, int]]:
"""List the ``(path, layer index)`` shards the assembler consumes.
Mirrors ``save_model.collect_quantized_entries``: only ``*.safetensors``
files whose name matches ``layers_(\\d+)``. Any other safetensors file
in the run dir (a stray copy, a renamed-away shard) is invisible to
assembly, so it fails closed instead of being silently ignored.
"""
files = sorted(checkpoint_dir.glob("*.safetensors"))
if not files:
raise ValueError("checkpoint contains no safetensor files")
consumed: list[tuple[Path, int]] = []
for path in files:
match = ASSEMBLER_LAYER_FILENAME_PATTERN.search(path.name)
if match is None:
raise ValueError(
f"unexpected safetensors file {path.name!r}: the GSQ assembler"
" only consumes files matching layers_<N>; a renamed shard"
" would leave its projections unquantized, so rename it back"
" or remove it"
)
consumed.append((path, int(match.group(1))))
return consumed
def _require_key_ownership(
key: str,
filename: str,
layer_idx: int,
module_prefix: str,
expected: frozenset[str],
) -> None:
"""Require a shard tensor key to belong to its file's layer.
Every key in a consumed shard must start with
``<module_prefix>.layers.<N>.`` where ``N`` is the layer index the
assembler reads from the filename: anything else (a renamed shard's
keys, a stray ``lm_head`` tensor) is either invisible to assembly or
lands in the wrong output shard. Any key carrying a quantization
suffix (see :data:`QUANT_SUFFIXES`) must additionally name an expected
projection: the assembler's merge drops ``<prefix>.weight`` from the
base model whenever ``<prefix>.<suffix>`` is present, so unknown
quantization metadata such as ``lm_head.weight_scale`` would silently
remove a base weight with no packed replacement.
"""
if not key.startswith(f"{module_prefix}.layers.{layer_idx}."):
raise ValueError(
f"tensor {key!r} in {filename!r} does not belong to layer"
f" {layer_idx} (expected prefix"
f" {module_prefix!r}.layers.{layer_idx}.); the assembler reads"
" the layer from the filename, so this key would land in the"
" wrong shard or drop an unrelated base weight"
)
for suffix in QUANT_SUFFIXES:
if key.endswith(f".{suffix}"):
module = key[: -(len(suffix) + 1)]
if module not in expected:
raise ValueError(
f"tensor {key!r} in {filename!r} carries quantization"
f" metadata ({suffix}) for {module!r}, which is not an"
" expected projection; the assembler would drop the"
" matching base weight with no packed replacement"
)
break
def _require_no_optional_projection_tensors(
key_files: Mapping[str, str],
expected: Iterable[str],
) -> None:
"""Reject optional quantization tensors for targeted projections.
GSQ here is symmetric (gptq ``sym: True``; the pinned savers write only
``weight_packed``/``weight_scale``/``weight_shape``), so the symmetric
pack-quantized container must not carry ``weight_zero_point``,
``weight_g_idx``, or any other ``weight_*`` tensor besides the three
required suffixes (``<module>.weight`` dense fallbacks are reported via
missing/dense coverage, not here). The pinned assembler drops
``<prefix>.weight`` whenever ``<prefix>.<suffix>`` is present, and
compressed-tensors consumes zero points / group indices during
dequantization, so carrying them produces non-finite weights or
decoding failures.
"""
expected_set = set(expected)
allowed = set(REQUIRED_PACKED_SUFFIXES)
for key in key_files:
suffix = key.rsplit(".", 1)[-1] if "." in key else ""
if not suffix.startswith("weight_"):
continue
if suffix in allowed:
continue
module = key[: -(len(suffix) + 1)]
if module in expected_set:
raise ValueError(
f"tensor {key!r} carries optional quantization metadata"
f" ({suffix}) for targeted projection {module!r}; the"
" symmetric pack-quantized container must hold only"
" weight_packed/weight_scale/weight_shape"
)
def _require_no_optional_assembled_tensors(
key_files: Mapping[str, str],
) -> None:
"""Reject optional quantization tensors anywhere in assembled output.
The assembled checkpoint must contain only dense ``.weight`` tensors
and the symmetric trio (``weight_packed``/``weight_scale``/
``weight_shape``); any other ``weight_*`` tensor -- including
``weight_zero_point`` and ``weight_g_idx`` -- would be merged into the
output by ``save_model.py`` and consumed (or mishandled) by
compressed-tensors at load time.
"""
allowed = set(REQUIRED_PACKED_SUFFIXES)
for key in key_files:
suffix = key.rsplit(".", 1)[-1] if "." in key else ""
if not suffix.startswith("weight_"):
continue
if suffix in allowed:
continue
raise ValueError(
f"assembled tensor {key!r} carries optional quantization"
f" metadata ({suffix}); the symmetric pack-quantized container"
" must hold only weight_packed/weight_scale/weight_shape"
)
def _derive_expected_prefix(expected: tuple[str, ...]) -> str:
"""Return the single ``<module prefix>`` shared by expected modules."""
prefixes: set[str] = set()
for name in expected:
match = CHECKPOINT_LAYER_KEY_PATTERN.search(name)
if match is None:
raise ValueError(
f"expected module {name!r} names no .layers.<N>. segment;"
" cannot certify key ownership without a layer prefix"
)
prefixes.add(name[: match.start()])
if len(prefixes) != 1:
raise ValueError(
f"expected modules span several layer prefixes: {sorted(prefixes)}"
)
return prefixes.pop()
def _checkpoint_tensors(
checkpoint_dir: Path,
*,
module_prefix: str | None = None,
expected_modules: Iterable[str] | None = None,
) -> dict[str, str]:
"""Map tensor key to source filename for exactly the consumed shards.
Rejects duplicate tensor keys across consumed files (assembly would let
later files silently overwrite earlier entries) and rejects a consumed
file whose filename layer index differs from the layer index in any
tensor key it contains (assembly trusts the filename). When
``module_prefix`` and ``expected_modules`` are given, every key must
additionally satisfy :func:`_require_key_ownership`.
"""
from safetensors import safe_open
expected = frozenset(expected_modules) if expected_modules is not None else None
keys: dict[str, str] = {}
for path, layer_idx in _consumed_shard_files(checkpoint_dir):
with safe_open(path, framework="pt", device="cpu") as handle:
file_keys = list(handle.keys())
for key in file_keys:
key_match = CHECKPOINT_LAYER_KEY_PATTERN.search(key)
if key_match is not None and int(key_match.group(1)) != layer_idx:
raise ValueError(
f"tensor {key!r} in {path.name!r} names layer"
f" {key_match.group(1)} but the assembler reads layer"
f" {layer_idx} from the filename"
)
if module_prefix is not None and expected is not None:
_require_key_ownership(
key, path.name, layer_idx, module_prefix, expected
)
if key in keys:
raise ValueError(
f"duplicate checkpoint tensor keys: {sorted([key])[:3]}"
f" (also in {keys[key]!r})"
)
keys[key] = path.name
return keys
def _top_level_safetensor_files(base: Path) -> list[Path]:
"""List top-level ``*.safetensors`` files, as the assembler sees them.
Mirrors pinned ``save_model.collect_base_model_entries``: every
top-level ``*.safetensors`` entry in the base model dir (the index is
ignored). Symlinked snapshot shards count as files.
"""
return sorted(
path for path in base.glob("*.safetensors") if path.is_file()
)
def _require_weight_map_key_locations(
base: Path, index_path: Path, weight_map: Mapping[str, str]
) -> None:
"""Fail closed when an index entry names a tensor its file lacks.
Every ``weight_map`` key must actually live in its mapped top-level
file: GSQ training follows the index, so a key mapped away from the
file holding it trains against different bytes than validation and
assembly certify. Reads headers only (no tensor data).
"""
from safetensors import safe_open
by_file: dict[str, list[str]] = {}
for tensor_key, shard in weight_map.items():
by_file.setdefault(shard, []).append(tensor_key)
for filename in sorted(by_file):
shard = base / filename
try:
with safe_open(shard, framework="pt", device="cpu") as handle:
present = set(handle.keys())
except Exception as exc:
raise ValueError(
f"cannot read base model shard {shard}: {exc}"
) from exc
absent = [key for key in by_file[filename] if key not in present]
if absent:
raise ValueError(
f"model weight index {index_path} maps {absent[0]!r} (and"
f" {len(absent) - 1} more) to {filename!r}, but the file"
" does not hold it; GSQ training follows the index while"
" validation and assembly read the top-level files, so a"
" key mapped away from its weights cannot be certified"
)
def _require_weight_map_covers_top_level_keys(
base: Path, index_path: Path, weight_map: Mapping[str, str]
) -> None:
"""Fail closed when the index key set differs from the shard keys.
GSQ training loads only indexed names, so an index that omits a
tensor (a norm or projection) while referencing every shard leaves
that parameter on ``meta`` -- training then fails when it reaches
that layer, potentially after substantial work -- while validation
and assembly still see the tensor through the shard contents. The
index ``weight_map`` key set must therefore equal exactly the union
of tensor keys in the top-level shard files: an omitted key (or an
extra key naming no shard tensor) refuses here. Reads headers only
(no tensor data).
"""
from safetensors import safe_open
actual: set[str] = set()
for shard in _top_level_safetensor_files(base):
try:
with safe_open(shard, framework="pt", device="cpu") as handle:
actual.update(handle.keys())
except Exception as exc:
raise ValueError(
f"cannot read base model shard {shard}: {exc}"
) from exc
indexed = set(weight_map)
omitted = sorted(actual - indexed)
if omitted:
raise ValueError(
f"model weight index {index_path} omits"
f" {len(omitted)} tensor(s) present in the top-level shards"
f" (e.g. {omitted[:3]}); GSQ training follows the index while"
" validation and assembly read the top-level files, so an"
" omitted tensor (which stays on meta during training)"
" cannot be certified"
)
extra = sorted(indexed - actual)
if extra:
raise ValueError(
f"model weight index {index_path} lists"
f" {len(extra)} tensor(s) absent from the top-level shards"
f" (e.g. {extra[:3]}); the index must cover exactly the"
" top-level shard set"
)
def _require_weight_map_matches_top_level(
base: Path, index_path: Path
) -> None:
"""Fail closed when the weight index leaves the top-level shard set.
The assembler collects only top-level ``*.safetensors`` files, while
GSQ training follows ``model.safetensors.index.json`` (including
nested shard paths): an index whose ``weight_map`` references a
``weights/`` subdir (or any name that is not a top-level file in the
dir) describes tensors the two sides never read together. Any such
reference -- or a malformed entry -- raises, as does any disagreement
between the referenced set and the top-level shard set (an
unreferenced top-level file trains nothing but still assembles), any
key that does not live in its mapped file (headers-only check), and
any tensor key in the top-level shards that the index omits (an
omitted norm or projection stays on ``meta`` during training while
validation and assembly see it through the shards).
"""
try:
payload = json.loads(index_path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
raise ValueError(
f"cannot read model weight index {index_path}: {exc}"
) from exc
weight_map = payload.get("weight_map") if isinstance(payload, dict) else None
if not isinstance(weight_map, dict) or not weight_map:
raise ValueError(
f"model weight index {index_path} has no usable weight_map"
)
top_names = {path.name for path in _top_level_safetensor_files(base)}
for tensor_key, shard in weight_map.items():
if not isinstance(shard, str) or "/" in shard or "\\" in shard:
raise ValueError(
f"model weight index {index_path} maps {tensor_key!r} to"
f" {shard!r}, which is not a top-level file in {base};"
" the assembler only collects top-level *.safetensors"
)
if shard not in top_names:
raise ValueError(
f"model weight index {index_path} maps {tensor_key!r} to"
f" {shard!r}, which is not a top-level safetensors file"
f" in {base}; the assembler only collects top-level"
" *.safetensors"
)
unreferenced = sorted(top_names - set(weight_map.values()))
if unreferenced:
raise ValueError(
f"model weight index {index_path} leaves top-level safetensors"
f" file(s) {unreferenced} not referenced by the index; GSQ"
" training follows the index while assembly collects every"
" top-level *.safetensors file, so the index must cover"
" exactly the top-level shard set"
)
_require_weight_map_key_locations(base, index_path, weight_map)
_require_weight_map_covers_top_level_keys(base, index_path, weight_map)
def require_base_index_agreement(base_model_dir: str | Path) -> None:
"""Require the base weight index (if any) to agree with its shards.
When ``model.safetensors.index.json`` exists beside the base-model
weights, every ``weight_map`` file must be a top-level file in the
dir (no nested paths, no missing files), the referenced set must
equal the top-level ``*.safetensors`` set, each mapped key must
actually live in its mapped file (headers-only check), and the
``weight_map`` key set must equal exactly the union of tensor keys
in the top-level shard files (an omitted norm or projection stays
on ``meta`` during training). Without an index there is nothing
to agree with. ``run`` calls this before
launch and ``validate``/``assemble`` call it again, so training can
never follow a lookup the launch binding did not certify.
"""
base = Path(base_model_dir)
index_path = base / "model.safetensors.index.json"
if not index_path.is_file():
return
_require_weight_map_matches_top_level(base, index_path)
def read_base_weight_shapes(
model_dir: str | Path,
expected_modules: Iterable[str],
) -> dict[str, tuple[int, int]]:
"""Read ``[out, in]`` shapes for projections from base-model weights.
Shapes come from exactly the shard set the pinned assembler reads
(``save_model.collect_base_model_entries``: every top-level
``*.safetensors`` file; the index is ignored for contents). When an
index is present its ``weight_map`` must still reference only
top-level files in the dir -- otherwise validation fails closed
instead of certifying tensors assembly never reads. Any tensor key
repeated across the top-level shards fails closed (a stale copy in
one shard and the corrected copy in another have no single truth),
even when the duplicated key is not a projection weight. Raises
when the directory holds no usable base weights or a projection
weight is absent from the top-level files, so callers fail closed
instead of skipping shape checks.
"""
from safetensors import safe_open
expected = tuple(expected_modules)
wanted = {f"{name}.weight": name for name in expected}
base = Path(model_dir)
shards = _top_level_safetensor_files(base)
if not shards:
raise ValueError(
f"no base-model weights in {base}; cannot verify quantized"
" tensor shapes without base_model_dir or expected_shapes"
)
_require_no_duplicate_base_keys(base)
index_path = base / "model.safetensors.index.json"
if index_path.is_file():
_require_weight_map_matches_top_level(base, index_path)
found: dict[str, Path] = {}
for shard in shards:
with safe_open(shard, framework="pt", device="cpu") as handle:
for key in handle.keys():
if key in wanted:
found[key] = shard
absent = [key for key in wanted if key not in found]
if absent:
raise ValueError(
f"base model weights lack {absent[0]!r} (and"
f" {len(absent) - 1} more); cannot verify quantized shapes"
)
shard_for_key = found
by_shard: dict[Path, list[str]] = {}
for tensor_key, shard in shard_for_key.items():
by_shard.setdefault(shard, []).append(tensor_key)
shapes: dict[str, tuple[int, int]] = {}
for shard, tensor_keys in by_shard.items():
try:
handle = safe_open(shard, framework="pt", device="cpu")
except Exception as exc:
raise ValueError(
f"cannot open base model shard {shard}: {exc}"
) from exc
with handle:
for tensor_key in tensor_keys:
try:
shape = list(handle.get_slice(tensor_key).get_shape())
except Exception as exc:
raise ValueError(
f"cannot read base weight shape for {tensor_key!r}: {exc}"
) from exc
if len(shape) != 2:
raise ValueError(
f"base weight {tensor_key!r} has shape {shape},"
" expected [out, in]"
)
shapes[wanted[tensor_key]] = (shape[0], shape[1])
return shapes
def _checked_shape_table(
expected: tuple[str, ...],
expected_shapes: Mapping[str, tuple[int, int] | list[int]] | None,
base_model_dir: str | Path | None,
) -> dict[str, tuple[int, int]]:
"""Resolve ``{module: (out, in)}`` or fail closed.
Base-model weights are the truth when available: explicit shapes are
cross-checked against them entry by entry and any mismatch refuses,
so an explicit table cannot override actual base-weight dimensions.
Without base weights, explicit shapes are used as-is; without either,
validation raises instead of skipping shape checks.
"""
if expected_shapes is not None:
table: dict[str, tuple[int, int]] = {}
for name in expected:
if name not in expected_shapes:
raise ValueError(
f"explicit expected_shapes has no entry for {name!r}"
)
entry = expected_shapes[name]
try:
out, inn = int(entry[0]), int(entry[1])
except (TypeError, ValueError, IndexError) as exc:
raise ValueError(
f"explicit shape for {name!r} must be [out, in],"
f" found {entry!r}"
) from exc
if out <= 0 or inn <= 0:
raise ValueError(
f"explicit shape for {name!r} must be positive,"
f" found {[out, inn]}"
)
table[name] = (out, inn)
if base_model_dir is not None:
base = Path(base_model_dir)
if base.is_dir() and _top_level_safetensor_files(base):
base_shapes = read_base_weight_shapes(base_model_dir, expected)
for name in expected:
if table[name] != base_shapes[name]:
raise ValueError(
f"explicit shape for {name!r} is"
f" {list(table[name])}, but the base model weight"
f" is {list(base_shapes[name])}; explicit shapes"
" must not override base-weight dimensions"
)
return table
if base_model_dir is None:
raise ValueError(
"cannot verify quantized tensor shapes without base-model weights:"
" pass base_model_dir or expected_shapes"
)
return read_base_weight_shapes(base_model_dir, expected)
# Transient int32 elements materialized per row chunk by the
# decoded-finiteness check (one int32 unpack buffer per chunk, ~4 MiB).
_DECODE_CHECK_CHUNK_ELEMS = 1 << 20
# Elements per chunk for the retained-tensor load-dtype cast check: each
# chunk is cast separately, so large embeddings never hold more than a
# bounded cast buffer (~4 MiB fp32) beside the loaded tensor.
_LOAD_DTYPE_CHECK_CHUNK_ELEMS = 1 << 20
def _require_finite_after_cast_to_load_dtypes(
tensor,
*,
key: str,
what: str,
load_dtypes=(),
) -> None:
"""Require a floating tensor to stay finite in every load dtype.
Transformers casts every floating checkpoint tensor to the requested
load dtype on load, so a retained weight that is finite stored (e.g.
an fp32 ``1e10``) but overflows a bound load dtype (fp16) loads as
non-finite. Each candidate dtype distinct from the stored dtype is
therefore certified in chunks, so large tensors never materialize a
full-size cast copy. Non-floating tensors and empty candidate sets
pass trivially.
"""
import torch
if not tensor.is_floating_point():
return
stored = tensor.dtype
extra: list = []
for candidate in load_dtypes or ():
dtype = _torch_dtype_from_name(candidate)
if dtype is not None and dtype != stored and dtype not in extra:
extra.append(dtype)
if not extra:
return
flat = tensor.view(-1) if tensor.is_contiguous() else tensor.reshape(-1)
for start in range(0, flat.numel(), _LOAD_DTYPE_CHECK_CHUNK_ELEMS):
chunk = flat[start : start + _LOAD_DTYPE_CHECK_CHUNK_ELEMS]
for dtype in extra:
cast = chunk.to(dtype)
if not bool(torch.isfinite(cast).all()):
raise ValueError(
f"{what} {key!r} is not finite after casting to"
f" {dtype}: the loader casts floating weights to the"
" model load dtype, so a weight that overflows the"
" load dtype loads as non-finite"
)
del cast
del chunk
def _require_decoded_weights_finite(
packed,
scale,
*,
in_features: int,
pack_bits: int,
module: str,
load_dtypes=(),
) -> None:
"""Certify packed weights decode to finite values, as the loader computes.
Mirrors the compressed-tensors 0.15.0.1 pack-quantized decompressor
(``compressors/pack_quantized/helpers.unpack_from_int32`` with
``num_bits`` = the container width, then
``quantization/lifecycle/forward.dequantize`` with no zero point,
``g_idx``, or ``global_scale`` and default ``dtype``): every decoded
element is ``round(q * s)`` in the scale dtype, where ``q`` is the
unpacked container int and ``s`` the per-group scale. Finite scales
alone do not suffice: e.g. an fp32 ``weight_scale`` of
``finfo(float32).max`` times an unpacked ``-8`` decodes to ``-inf``.
Per group, decode is finite iff ``max|q| * |s|`` is finite in the
scale dtype (magnitude-monotone rounding; confirmed by differential
trials against the real ``unpack_from_int32`` + ``dequantize`` over
fp32/fp16/bf16 with adversarial scales), so the check evaluates
exactly that bound per group -- using each group's ACTUAL unpacked
maximum, never the container bound -- instead of materializing the
decoded float tensor. Rows are processed in chunks, so large models
never hold more than a bounded slice. Group spans use the
loader-inferred width (``in_features // num_groups``, as
``dequantize`` infers from the scale shape); layouts the loader
cannot reshape, and containers that cannot roundtrip through the
real pack/unpack pair (widths above 8 bits or not dividing the
int32 word), fail closed here instead of certifying undecodable
bytes. Every dtype in ``load_dtypes`` is additionally certified:
Transformers casts floating checkpoint tensors (including
``weight_scale``) to the requested load dtype on load, and the
decompressor then decodes in that dtype (``dequantize`` defaults
``dtype`` to ``scale.dtype``), so each scale must be finite after
casting and ``max|q| * |s|`` must be finite in the load dtype too --
e.g. an fp32 ``1e10`` scale is finite stored but ``+inf`` in fp16.
"""
import torch
decode_dtype = scale.dtype
extra: list = []
for candidate in load_dtypes or ():
dtype = _torch_dtype_from_name(candidate)
if (
dtype is not None
and dtype != decode_dtype
and dtype not in extra
):
extra.append(dtype)
for dtype in extra:
cast = scale.to(dtype)
if not bool(torch.isfinite(cast).all()):
raise ValueError(
f"{module}.weight_scale is not finite after casting to"
f" {dtype}: the loader casts scales to the model load"
" dtype before decoding, so a scale that overflows the"
" load dtype dequantizes to non-finite weights"
)
words_per_row = packed.shape[1]
num_groups = scale.shape[1]
pack_factor = 32 // pack_bits if pack_bits > 0 else 0
if (
pack_bits <= 0
or pack_bits > 8
or 32 % pack_bits != 0
or words_per_row * pack_factor != in_features
):
raise ValueError(
f"{module}: cannot certify decoded finiteness for a"
f" {pack_bits}-bit container holding {in_features} columns"
f" ({words_per_row} int32 words/row); the pack-quantized"
" decompressor supports only word-tiling containers of at"
" most 8 bits"
)
if in_features % num_groups != 0:
raise ValueError(
f"{module}: weight_scale has {num_groups} group(s) for"
f" {in_features} columns, which do not tile; the"
" pack-quantized decompressor cannot reshape that layout"
)
loader_group_size = in_features // num_groups
mask = (1 << pack_bits) - 1
offset = 1 << (pack_bits - 1)
rows_per_chunk = max(1, _DECODE_CHECK_CHUNK_ELEMS // max(1, in_features))
for start in range(0, packed.shape[0], rows_per_chunk):
stop = min(packed.shape[0], start + rows_per_chunk)
words = packed[start:stop]
rows = words.shape[0]
# Same ops, same order as unpack_from_int32 (packed_dim=1):
# strided nibbles truncated to in_features, unsigned -> signed.
unpacked = torch.empty(
(rows, words_per_row * pack_factor), dtype=torch.int32
)
for i in range(pack_factor):
unpacked[:, i::pack_factor] = (words >> (pack_bits * i)) & mask
unpacked = unpacked[:, :in_features]
unpacked.sub_(offset).abs_()
group_max = unpacked.reshape(
rows, num_groups, loader_group_size
).amax(dim=2)
products = group_max.to(decode_dtype) * scale[start:stop].abs()
if not bool(torch.isfinite(products).all()):
raise ValueError(
f"{module} decodes to non-finite weights: max|q| *"
f" |weight_scale| overflows {decode_dtype} (the"
" pack-quantized decompressor multiplies unpacked ints"
" by the per-group scale in the scale dtype)"
)
for dtype in extra:
loaded = (
group_max.to(dtype) * scale[start:stop].to(dtype).abs()
)
if not bool(torch.isfinite(loaded).all()):
raise ValueError(
f"{module} decodes to non-finite weights in {dtype}:"
" max|q| * |weight_scale| overflows the model load"
" dtype (the loader casts scales to the load dtype"
" and the pack-quantized decompressor multiplies"
" unpacked ints by the per-group scale in that dtype)"
)
def _require_tensor_shapes(
checkpoint: Path,
key_files: Mapping[str, str],
table: Mapping[str, tuple[int, int]],
modules: Iterable[str],
*,
group_size: int,
pack_bits: int,
load_dtype=None,
) -> None:
"""Require valid packed dtypes, shapes, and contents per projection.
``weight_packed`` must be int32 with shape ``[out, in*pack_bits/32]``
(GSQ's pack container, ``pack_bits`` wide), ``weight_scale`` floating
with shape ``[out, ceil(in/group_size)]``, and ``weight_shape`` int64
with shape ``(2,)`` holding the base model's ``[out, in]``. Empty
tensors fail: such a checkpoint holds no usable quantized projections.
``in_features`` must be divisible by ``group_size``: group
quantization apportions columns with
``unflatten(-1, (num_groups, group_size))`` and compressed-tensors
0.15.0.1 has no padding path there (a 16-wide projection with
``group_size`` 128 fails loading in ``unflatten``), so a remainder
fails closed instead of certifying unloadable scales. Scales must be
finite (a NaN/Inf scale dequantizes to non-finite weights) and
recorded shapes must hold positive extents. Finite scales are
additionally certified to decode to finite weights as the
pack-quantized decompressor computes them (unpacked container ints
times the per-group scale in the scale dtype, and -- when
``load_dtype`` names the bound model load dtype -- in that dtype
too, since the loader casts scales before decoding; see
:func:`_require_decoded_weights_finite`), in bounded memory.
"""
import torch
from safetensors import safe_open
by_file: dict[str, list[str]] = {}
for name in modules:
for suffix in REQUIRED_PACKED_SUFFIXES:
by_file.setdefault(key_files[f"{name}.{suffix}"], []).append(
f"{name}.{suffix}"
)
loaded: dict[str, torch.Tensor] = {}
for filename, tensor_keys in by_file.items():
with safe_open(checkpoint / filename, framework="pt", device="cpu") as h:
for tensor_key in tensor_keys:
loaded[tensor_key] = h.get_tensor(tensor_key)
for name in modules:
out, inn = table[name]
packed = loaded[f"{name}.weight_packed"]
scale = loaded[f"{name}.weight_scale"]
shape = loaded[f"{name}.weight_shape"]
if packed.dtype != torch.int32:
raise ValueError(
f"{name}.weight_packed has dtype {packed.dtype}, expected int32"
)
if not scale.is_floating_point():
raise ValueError(
f"{name}.weight_scale has dtype {scale.dtype},"
" expected a floating dtype"
)
if not bool(torch.isfinite(scale).all()):
raise ValueError(
f"{name}.weight_scale is not finite; a NaN/Inf scale"
" dequantizes to non-finite weights"
)
if shape.dtype != torch.int64 or tuple(shape.shape) != (2,):
raise ValueError(
f"{name}.weight_shape has dtype {shape.dtype} with shape"
f" {tuple(shape.shape)}, expected int64 with shape (2,)"
)
if bool((shape <= 0).any()):
raise ValueError(
f"{name}.weight_shape holds {shape.tolist()}, expected"
" positive [out, in] extents"
)
for label, tensor in (
("weight_packed", packed),
("weight_scale", scale),
("weight_shape", shape),
):
if tensor.numel() == 0:
raise ValueError(
f"{name}.{label} is empty; the checkpoint holds no usable"
" quantized projection"
)
if shape.tolist() != [out, inn]:
raise ValueError(
f"{name}.weight_shape holds {shape.tolist()}, expected the base"
f" model shape {[out, inn]}"
)
if (inn * pack_bits) % 32 != 0:
raise ValueError(
f"{name}: in_features {inn} is not divisible for a"
f" {pack_bits}-bit pack container"
)
if inn % group_size != 0:
raise ValueError(
f"{name}: in_features {inn} is not divisible by"
f" group_size {group_size}; group quantization has no"
" padding path (the loader apportions columns with"
" unflatten(-1, (num_groups, group_size))), so this"
" projection cannot load"
)
expected_packed = (out, inn * pack_bits // 32)
if tuple(packed.shape) != expected_packed:
raise ValueError(
f"{name}.weight_packed has shape {tuple(packed.shape)},"
f" expected {expected_packed} for [out, in]=[{out}, {inn}]"
f" with pack_bits={pack_bits}"
)
expected_scale = (out, ceil(inn / group_size))
if tuple(scale.shape) != expected_scale:
raise ValueError(
f"{name}.weight_scale has shape {tuple(scale.shape)},"
f" expected {expected_scale} for [out, in]=[{out}, {inn}]"
f" with group_size={group_size}"
)
_require_decoded_weights_finite(
packed,
scale,
in_features=inn,
pack_bits=pack_bits,
module=name,
load_dtypes=(load_dtype,) if load_dtype is not None else (),
)
def validate_gsq_checkpoint(
checkpoint_dir: str | Path,
expected_modules: Iterable[str],
expected_last_layer: int,
*,
base_model_dir: str | Path | None = None,
expected_shapes: Mapping[str, tuple[int, int] | list[int]] | None = None,
group_size: int = DEFAULT_GROUP_SIZE,
pack_bits: int = DEFAULT_PACK_BITS,
module_prefix: str | None = None,
load_dtype=None,
) -> GSQCoverage:
"""Require a completed checkpoint with packed tensors, no dense fallback.
Only the shard set the pinned assembler consumes (``*.safetensors``
files matching ``layers_(\\d+)``) is inspected; any other safetensors
file, filename/key layer mismatch, or duplicate key raises. Every
tensor key must belong to its file's layer
(``<module_prefix>.layers.<N>.``) and any key carrying a quantization
suffix must name an expected projection (unknown quantization metadata
would make the assembler drop a base weight); when ``module_prefix``
is omitted it is derived from the expected modules, which must then
share one layer prefix. For targeted projections only the symmetric
trio (``weight_packed``/``weight_scale``/``weight_shape``) is accepted:
any ``weight_zero_point``, ``weight_g_idx``, or other ``weight_*``
tensor for an expected projection raises, since the symmetric
pack-quantized container (gptq ``sym: True``) must not carry them.
Every expected projection's ``weight_packed``/``weight_scale``/
``weight_shape`` dtypes, shapes, finiteness, and contents are checked
against the base-model ``[out, in]`` (from ``base_model_dir`` weights,
or ``expected_shapes`` explicitly); without shapes validation fails
closed. Every ``in_features`` must additionally be divisible by
``group_size`` (group quantization has no padding path, so a
remainder could never load). Every other (non-projection) tensor in
a consumed shard overwrites the base tensor with the same key during
assembly (``save_model.py`` merges by exact key), so each must exist
in the base model with the same dtype and shape and hold finite,
non-empty contents (streamed per tensor); without ``base_model_dir``
such runs fail closed. When ``load_dtype`` names the bound model load
dtype, every scale must additionally be finite after casting to it
and decode to finite weights in it (the loader casts scales before
decoding), and every floating overwrite must additionally be finite
after casting to it in bounded chunks (the loader casts retained
weights too); an unusable name fails closed.
"""
if isinstance(group_size, bool) or not isinstance(group_size, int):
raise ValueError(f"group_size must be an int, found {group_size!r}")
if group_size <= 0:
raise ValueError(f"group_size must be positive, found {group_size!r}")
if isinstance(pack_bits, bool) or not isinstance(pack_bits, int):
raise ValueError(f"pack_bits must be an int, found {pack_bits!r}")
if pack_bits <= 0:
raise ValueError(f"pack_bits must be positive, found {pack_bits!r}")
load_dtype = _torch_dtype_from_name(load_dtype)
checkpoint = Path(checkpoint_dir)
progress_path = checkpoint / "progress.json"
if not progress_path.is_file():
raise ValueError("checkpoint has no progress.json")
try:
progress = json.loads(progress_path.read_text(encoding="utf-8"))
except json.JSONDecodeError as exc:
raise ValueError(f"checkpoint progress.json is not valid JSON: {exc}") from exc
if not isinstance(progress, dict):
raise ValueError("checkpoint progress.json must be a JSON object")
if progress.get("last_completed_layer") != expected_last_layer:
raise ValueError(
"checkpoint is incomplete: last_completed_layer="
f"{progress.get('last_completed_layer')!r},"
f" expected {expected_last_layer}"
)
expected = tuple(expected_modules)
if module_prefix is not None:
prefix: str | None = module_prefix
elif expected:
prefix = _derive_expected_prefix(expected)
else:
prefix = None
key_files = _checkpoint_tensors(
checkpoint,
module_prefix=prefix,
expected_modules=expected if prefix is not None else None,
)
_require_no_optional_projection_tensors(key_files, expected)
keys = set(key_files)
packed = tuple(name for name in expected if f"{name}.weight_packed" in keys)
missing = tuple(name for name in expected if f"{name}.weight_packed" not in keys)
dense = tuple(name for name in expected if f"{name}.weight" in keys)
incomplete = tuple(
name
for name in expected
if f"{name}.weight_packed" in keys
and any(f"{name}.{suffix}" not in keys for suffix in REQUIRED_PACKED_SUFFIXES)
)
if incomplete:
raise ValueError(
"packed modules have incomplete quantization metadata:"
f" {list(incomplete)[:3]}"
)
table = _checked_shape_table(expected, expected_shapes, base_model_dir)
_require_tensor_shapes(
checkpoint,
key_files,
table,
packed,
group_size=group_size,
pack_bits=pack_bits,
load_dtype=load_dtype,
)
_require_non_projection_tensors(
checkpoint,
key_files,
expected,
base_model_dir=base_model_dir,
load_dtype=load_dtype,
)
return GSQCoverage(expected, packed, missing, dense)
def _require_no_duplicate_base_keys(base: Path) -> None:
"""Refuse a base model whose top-level shards repeat a tensor key.
The pinned assembler (``save_model.collect_base_model_entries``)
iterates the sorted top-level ``*.safetensors`` files and lets later
files silently overwrite earlier entries with the same key, so a
duplicated tensor has no single truth: validation could certify one
copy while assembly merges the other (for example a stale ``[1]``
layernorm in an earlier shard shadowing the correct ``[128]`` copy
a run was checked against). Any repeated key therefore fails closed
instead of emulating precedence. Reads headers only.
"""
from safetensors import safe_open
seen: dict[str, Path] = {}
for shard in _top_level_safetensor_files(base):
with safe_open(shard, framework="pt", device="cpu") as handle:
for key in handle.keys():
if key in seen:
raise ValueError(
f"duplicate base tensor {key!r} in"
f" {shard.name!r} (also in {seen[key].name!r});"
" the assembler lets later shards silently"
" overwrite earlier ones, so refusing to guess"
" which copy validation certifies"
)
seen[key] = shard
def _base_key_files(base: Path) -> dict[str, Path]:
"""Map every base-model tensor key to its top-level shard file.
Reads headers only (no tensor data), mirroring
``save_model.collect_base_model_entries`` (every top-level
``*.safetensors`` file; the index is ignored for contents). A key
repeated across shards fails closed via
:func:`_require_no_duplicate_base_keys` instead of keeping one copy.
"""
from safetensors import safe_open
_require_no_duplicate_base_keys(base)
mapping: dict[str, Path] = {}
for shard in _top_level_safetensor_files(base):
with safe_open(shard, framework="pt", device="cpu") as handle:
for key in handle.keys():
mapping[key] = shard
return mapping
def _base_layer_indices(base_keys: Iterable[str]) -> set[int]:
"""Return layer indices appearing in base-model tensor keys."""
indices: set[int] = set()
for key in base_keys:
match = CHECKPOINT_LAYER_KEY_PATTERN.search(key)
if match is not None:
indices.add(int(match.group(1)))
return indices
def require_base_topology_matches(
expected_modules: Iterable[str],
base_model_dir: str | Path,
num_layers: int,
) -> None:
"""Cross-check the config-derived topology against base weights.
Every expected projection's base ``<module>.weight`` must exist, and
the base model must not contain additional layer indices beyond the
derived ``num_layers`` (layers ``0..num_layers-1``). A shortened
config with fewer layers than the base (e.g. a two-layer base
validated with a one-layer config) refuses here instead of certifying
a subset (the reviewer reproduced 8/15 passing). Reads headers only.
An empty base dir (no top-level weights, as in the explicit-shapes
weightless case) has nothing to compare and is skipped by the caller;
this helper raises when called with no base weights.
"""
expected = tuple(expected_modules)
base = Path(base_model_dir)
base_files = _base_key_files(base)
if not base_files:
raise ValueError(f"no base-model weights in {base}")
base_keys = set(base_files)
missing = [
f"{name}.weight" for name in expected if f"{name}.weight" not in base_keys
]
if missing:
raise ValueError(
f"base model weights lack {missing[0]!r} (and"
f" {len(missing) - 1} more); the config-derived topology does"
" not match the base model"
)
indices = _base_layer_indices(base_keys)
extra = sorted(index for index in indices if index >= num_layers)
if extra:
raise ValueError(
f"base model contains additional layer indices {extra[:5]} beyond"
f" the config-derived {num_layers} layer(s); refusing to certify"
" a shortened config (re-run with the base model's own"
" config.json)"
)
def partial_layer_set(packed_modules: Iterable[str]) -> list[int]:
"""Return sorted layer indices present in packed projection names."""
indices: set[int] = set()
for name in packed_modules:
match = CHECKPOINT_LAYER_KEY_PATTERN.search(name)
if match is not None:
indices.add(int(match.group(1)))
return sorted(indices)
def _non_projection_keys(
key_files: Mapping[str, str],
expected: tuple[str, ...],
) -> list[str]:
"""Return run-shard keys that are not packed-projection metadata.
The pinned assembler merges run shards over base weights by exact
key (``merged = {**base_entries, **quantized_entries}`` in
``save_model.py``), so every such key overwrites the base tensor
with the same key during assembly. Keys of the form
``<expected>.<quant suffix>`` are packed projections checked
elsewhere; everything else (layernorms, ``conv1d``, ``A_log``,
``dt_bias``, ``norm``, ``q_norm``/``k_norm``) is a non-projection
overwrite candidate.
"""
projected = set()
for name in expected:
for suffix in QUANT_SUFFIXES:
projected.add(f"{name}.{suffix}")
# Dense fallbacks (``<expected>.weight``) are reported via
# missing/dense coverage, not the overwrite check below.
projected.add(f"{name}.weight")
return sorted(key for key in key_files if key not in projected)
def _require_non_projection_tensors(
checkpoint: Path,
key_files: Mapping[str, str],
expected: tuple[str, ...],
*,
base_model_dir: str | Path | None,
load_dtype=None,
) -> None:
"""Require honest non-projection tensors that assembly will overwrite.
Every non-projection tensor in a consumed run shard overwrites the
base tensor with the same key during assembly, so each must exist
in the base model with the same dtype and shape, and must be
non-empty and finite when floating -- and, when ``load_dtype`` names
the bound model load dtype, finite after casting to it in bounded
chunks (the loader casts floating weights to the load dtype, so a
stored-finite overwrite that overflows it would load as
non-finite). Tensors are streamed one at a time (never the whole
model at once). Without a base model dir there is nothing to
compare against: runs holding non-projection tensors fail closed
instead of skipping the check.
"""
import torch
from safetensors import safe_open
others = _non_projection_keys(key_files, expected)
if not others:
return
if base_model_dir is None:
raise ValueError(
"cannot verify non-projection tensors without base-model"
" weights: pass base_model_dir"
)
base = Path(base_model_dir)
base_files = _base_key_files(base)
if not base_files:
raise ValueError(f"no base-model weights in {base}")
load_candidates = (load_dtype,) if load_dtype is not None else ()
for key in others:
if key not in base_files:
raise ValueError(
f"run tensor {key!r} has no base-model tensor;"
" assembly would add an undescribed weight"
)
run_file = checkpoint / key_files[key]
with safe_open(run_file, framework="pt", device="cpu") as handle:
run_tensor = handle.get_tensor(key)
run_dtype = run_tensor.dtype
run_shape = tuple(run_tensor.shape)
run_numel = run_tensor.numel()
run_finite = (
bool(torch.isfinite(run_tensor).all())
if run_tensor.is_floating_point()
else True
)
run_floating = run_tensor.is_floating_point()
if run_floating and run_finite:
_require_finite_after_cast_to_load_dtypes(
run_tensor,
key=key,
what="run tensor",
load_dtypes=load_candidates,
)
del run_tensor
if run_numel == 0:
raise ValueError(
f"run tensor {key!r} is empty; the checkpoint holds no"
" usable weight"
)
if run_floating and not run_finite:
raise ValueError(
f"run tensor {key!r} is not finite; assembly would"
" overwrite a valid base weight with NaN/Inf"
)
base_shard = base_files[key]
with safe_open(base_shard, framework="pt", device="cpu") as handle:
base_tensor = handle.get_tensor(key)
base_dtype = base_tensor.dtype
base_shape = tuple(base_tensor.shape)
del base_tensor
if run_dtype != base_dtype:
raise ValueError(
f"run tensor {key!r} has dtype {run_dtype}, expected base"
f" dtype {base_dtype}"
)
if run_shape != base_shape:
raise ValueError(
f"run tensor {key!r} has shape {list(run_shape)}, expected"
f" base shape {list(base_shape)}"
)
def _ignore_entry_matches(entry: str, module: str) -> bool:
"""Mirror compressed-tensors ignore name matching for one entry.
Mirrors ``compressed_tensors.utils.match.match_name`` (verified
against the installed compressed-tensors 0.15.0.1 source): plain
names compare exactly; ``re:``-prefixed entries are regexes applied
with ``re.match`` -- anchored at the start of the module name, not
``re.search`` -- against the module name (the tensor key without
its ``.weight`` / ``.weight_packed`` suffix). An invalid regex
fails closed instead of silently matching nothing. This is only the
name half of the loader's match; see :func:`_ignore_matches` for
the class-name half.
"""
if entry.startswith("re:"):
try:
return re.match(entry[3:], module) is not None
except re.error as exc:
raise ValueError(
f"invalid quantization_config ignore regex {entry!r}: {exc}"
) from exc
return entry == module
# Class names a plain ignore entry can match on a projection module.
# Mirrors compressed-tensors 0.15.0.1 ``utils/match.py`` ``is_match``: a
# module matches an entry when ``match_name`` OR ``_match_class`` hits.
# ``_match_class`` compares plain entries exactly against every
# ``nn.Module`` subclass in the module's MRO (plus vLLM ``LinearBase``
# matching ``"Linear"``); ``re:`` entries never match a class (``":"``
# cannot appear in a class name, so the exact compare always fails --
# verified live: ``is_match("model.layers.0.mlp.gate_proj", Linear(),
# "re:.*Linear")`` is False). A transformers Qwen projection is a
# ``torch.nn.Linear`` whose contributing MRO names are exactly ``Linear``
# and ``Module`` (``object`` is not an ``nn.Module`` subclass), so a
# plain ``"Linear"`` or ``"Module"`` ignore entry makes the loader skip
# every packed projection -- while a name-only check passes it.
_PACKED_LINEAR_CLASS_NAMES = frozenset({"Linear", "Module"})
# The pinned assembler (``save_model.py`` ``make_shard_name``) always
# writes indexed shards as ``model-<NNNNN>-of-<MMMMM>.safetensors``
# (``f"model-{idx+1:05d}-of-{num_shards:05d}.safetensors"``); an index
# referencing any other filename shape is not the assembler's output.
_ASSEMBLER_SHARD_NAME_PATTERN = re.compile(r"^model-\d{5}-of-\d{5}\.safetensors$")
def _require_safe_assembled_filename(
root: Path, filename: str, *, index_path: Path
) -> None:
"""Require a portable top-level shard reference.
The reference must be a plain top-level filename -- no path
separators, no ``..``, not absolute, ending in ``.safetensors`` --
and must resolve inside the output directory, so a traversal
(``../external.safetensors``), an absolute path, a subdirectory
reference, or a symlink escaping the output dir fails closed here
instead of certifying a non-portable export (or reading outside
the output dir at all).
"""
name = Path(filename).name
if (
not filename
or filename != name
or "/" in filename
or "\\" in filename
or Path(filename).is_absolute()
or ".." in Path(filename).parts
):
raise ValueError(
f"assembled weight index {index_path} references"
f" {filename!r}: expected a plain top-level shard filename"
" (no directories, '..', or absolute paths) so the export"
" stays portable"
)
if not filename.endswith(".safetensors"):
raise ValueError(
f"assembled weight index {index_path} references"
f" {filename!r}: expected a .safetensors shard filename"
)
try:
anchored = root.resolve()
resolved = (root / filename).resolve()
except OSError as exc:
raise ValueError(
f"assembled weight index {index_path} references"
f" {filename!r}: cannot resolve the shard path: {exc}"
) from exc
if resolved != anchored and anchored not in resolved.parents:
raise ValueError(
f"assembled weight index {index_path} references"
f" {filename!r}, which resolves outside the output dir"
f" ({resolved}); refusing a non-portable export"
)
def _require_assembler_shard_names(
root: Path, key_files: Mapping[str, str]
) -> None:
"""Require indexed references to match the assembler's shard naming.
Every ``weight_map`` filename in an indexed output must look like
what the pinned ``save_model.py`` ``make_shard_name`` writes
(``model-<NNNNN>-of-<MMMMM>.safetensors``). Runs after the
competing-file and index/file-existence checks, so an index
referencing ``model.safetensors`` is still refused as a competing
standalone file first and a dangling reference still reports the
missing file. Without an index there is no ``weight_map`` to
check: the single ``model.safetensors`` layout's own filename is
the full set.
"""
if not (root / "model.safetensors.index.json").is_file():
return
for filename in sorted(set(key_files.values())):
if not _ASSEMBLER_SHARD_NAME_PATTERN.fullmatch(filename):
raise ValueError(
f"assembled weight index references {filename!r}, which"
" does not match the assembler's shard naming"
" (model-<NNNNN>-of-<MMMMM>.safetensors as"
" save_model.py writes); refusing a non-portable export"
)
def _assembled_key_files(root: Path) -> dict[str, str]:
"""Map tensor key to shard filename for an assembled output dir.
Prefers ``model.safetensors.index.json`` (``weight_map``); without an
index, the dir must hold a single ``model.safetensors`` (the only
index-less layout Transformers loads). Numbered shards without an
index fail closed. Anything else fails closed. Every indexed
reference must be a portable top-level filename resolving inside
the output directory (see :func:`_require_safe_assembled_filename`).
"""
from safetensors import safe_open
index_path = root / "model.safetensors.index.json"
if index_path.is_file():
try:
payload = json.loads(index_path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
raise ValueError(
f"cannot read assembled weight index {index_path}: {exc}"
) from exc
weight_map = payload.get("weight_map") if isinstance(payload, dict) else None
if not isinstance(weight_map, dict) or not weight_map:
raise ValueError(
f"assembled weight index {index_path} has no usable weight_map"
)
for key, filename in weight_map.items():
if not isinstance(key, str) or not isinstance(filename, str):
raise ValueError(
f"assembled weight index {index_path} has a malformed"
" weight_map entry"
)
for filename in set(weight_map.values()):
_require_safe_assembled_filename(
root, filename, index_path=index_path
)
return dict(weight_map)
shards = sorted(root.glob("*.safetensors"))
if not shards:
raise ValueError(f"assembled dir {root} holds no safetensors shards")
if len(shards) != 1 or shards[0].name != "model.safetensors":
names = sorted(path.name for path in shards)[:5]
raise ValueError(
f"assembled dir {root} holds {names} with no"
" model.safetensors.index.json; Transformers loads either a"
" single model.safetensors or indexed shards, so numbered"
" shards without an index cannot be certified"
)
key_files: dict[str, str] = {}
for shard in shards:
with safe_open(shard, framework="pt", device="cpu") as handle:
for key in handle.keys():
if key in key_files:
raise ValueError(
f"duplicate assembled tensor key {key!r}"
f" (also in {key_files[key]!r})"
)
key_files[key] = shard.name
return key_files
def _require_no_competing_weight_files(
root: Path, key_files: Mapping[str, str]
) -> None:
"""Refuse weight files the loader could prefer over the index.
Transformers prefers a top-level ``model.safetensors`` over sharded
``model-*.safetensors`` + index, and any ``*.bin`` / ``*.pt`` /
``pytorch_model*`` file is a competing checkpoint format. When an
index exists, ``model.safetensors`` is always refused -- even when the
index references it -- because loaders prefer the standalone file and
would ignore the indexed projection shards; an indexed output must
consist only of the index plus the shard files it references. Any
other top-level ``*.safetensors`` / ``*.bin`` / ``*.pt`` /
``pytorch_model*`` file not listed in the index ``weight_map`` values
(an extra shard, a ``pytorch_model.bin``) also fails closed. Without
an index, only a single ``model.safetensors`` is loadable (numbered
shards without an index fail in :func:`_assembled_key_files`), but any
``*.bin`` / ``*.pt`` / ``pytorch_model*`` file still fails closed.
"""
index_path = root / "model.safetensors.index.json"
if index_path.is_file():
try:
has_standalone = (root / "model.safetensors").is_file()
except OSError:
has_standalone = False
if has_standalone:
raise ValueError(
f"assembled dir {root} holds model.safetensors alongside"
" model.safetensors.index.json; Transformers prefers the"
" standalone file over indexed shards, so an indexed output"
" must consist only of the index plus the shard files it"
" references -- remove it or assemble into a new or empty"
" directory"
)
referenced = set(key_files.values())
candidates: set[str] = set()
for pattern in ("*.safetensors", "*.bin", "*.pt", "pytorch_model*"):
for path in root.glob(pattern):
try:
if path.is_file():
candidates.add(path.name)
except OSError:
continue
unreferenced = sorted(name for name in candidates if name not in referenced)
if unreferenced:
raise ValueError(
f"assembled dir {root} holds weight file(s) not referenced"
f" by the index {unreferenced[:5]}; a stale model.safetensors"
" beside model.safetensors.index.json would be preferred by"
" loaders over the assembled shards -- remove it or assemble"
" into a new or empty directory"
)
return
competing: set[str] = set()
for pattern in ("*.bin", "*.pt", "pytorch_model*"):
for path in root.glob(pattern):
try:
if path.is_file():
competing.add(path.name)
except OSError:
continue
if competing:
raise ValueError(
f"assembled dir {root} holds competing weight file(s)"
f" {sorted(competing)[:5]} alongside the safetensors shards;"
" remove them or assemble into a new or empty directory"
)
def _require_complete_packed_trios(key_files: Mapping[str, str]) -> None:
"""Require every packed-metadata module to carry the complete trio.
Any module holding any of ``weight_packed`` / ``weight_scale`` /
``weight_shape`` must hold all three. Orphan metadata (a scale and
shape without a packed tensor, or any other partial combination)
fails closed: it would exempt the base weight from retention checks
while providing no quantized replacement, concealing a missing
projection -- including the degenerate case of zero packed modules
with orphan metadata.
"""
present: dict[str, set[str]] = {}
for key in key_files:
for suffix in REQUIRED_PACKED_SUFFIXES:
if key.endswith(f".{suffix}"):
present.setdefault(key[: -(len(suffix) + 1)], set()).add(suffix)
break
orphans = sorted(
module for module, suffixes in present.items() if len(suffixes) != 3
)
if orphans:
raise ValueError(
"assembled modules hold incomplete quantization metadata"
f" (require weight_packed+weight_scale+weight_shape): {orphans[:5]}"
)
def _safetensors_key_ndims(
root: Path, key_files: Mapping[str, str], keys: Iterable[str]
) -> dict[str, int]:
"""Return ``{tensor key: ndim}`` from shard headers without loading data."""
from safetensors import safe_open
wanted = set(keys)
by_shard: dict[str, list[str]] = {}
for key in wanted:
by_shard.setdefault(key_files[key], []).append(key)
ndims: dict[str, int] = {}
for filename, tensor_keys in by_shard.items():
with safe_open(root / filename, framework="pt", device="cpu") as handle:
for key in tensor_keys:
try:
ndims[key] = len(handle.get_slice(key).get_shape())
except Exception as exc:
raise ValueError(
f"cannot read assembled tensor shape for {key!r}: {exc}"
) from exc
return ndims
def _ignore_matches(ignore: list[str], module: str) -> bool:
"""Return True when any ignore entry matches ``module`` by name or class.
Mirrors compressed-tensors 0.15.0.1 ``is_match`` for the modules
involved: packed projections are ``torch.nn.Linear`` modules (the
config targets are ``["Linear"]``), and dense 2-D weights are
assumed Linear fail-closed (a dense Linear the list does not cover
would load unpacked). Either way the loader also matches plain
entries against the module's class names
(:data:`_PACKED_LINEAR_CLASS_NAMES`), so ``ignore: ["Linear"]`` or
``["Module"]`` skips every packed projection; ``re:`` entries never
match a class name (see above), only module names.
"""
for entry in ignore:
if _ignore_entry_matches(entry, module):
return True
if (
not entry.startswith("re:")
and entry in _PACKED_LINEAR_CLASS_NAMES
):
return True
return False
# Exact nested key sets the pinned ``save_model.inject_quantization_config``
# writes (verified against the pinned writer, which builds both dicts as
# unconditional literals with no model-type-dependent keys): the single
# config group holds exactly ``input_activations``/``output_activations``/
# ``targets``/``weights``, and ``weights`` holds exactly ``num_bits``/
# ``strategy``/``group_size``/``symmetric``/``type``. The installed
# compressed-tensors loader ignores unknown nested keys silently (a group
# ``format`` override or a ``block_structure`` entry would decode
# differently than the certified container), so any other nested key --
# including ``dynamic``/``actorder``, which the writer never emits even
# as false/null -- fails closed.
_ASSEMBLED_GROUP_KEYS = frozenset(
{"input_activations", "output_activations", "targets", "weights"}
)
_ASSEMBLED_WEIGHT_KEYS = frozenset(
{"num_bits", "strategy", "group_size", "symmetric", "type"}
)
def _assembled_pack_params(
quantization: Mapping,
*,
config_path: Path,
group_size: int | None = None,
) -> tuple[int, int]:
"""Require the exact pack-quantized metadata and return its extents.
The assembled ``quantization_config`` must be exactly what the pinned
``save_model.inject_quantization_config`` writes: ``quant_method``
``"compressed-tensors"``, ``format`` ``"pack-quantized"``,
``quantization_status`` ``"compressed"``, a single config group
holding exactly ``input_activations``/``output_activations`` (null
as the pinned writer emits -- absent counts as null, anything
present is activation quantization this workflow did not validate),
``targets`` ``["Linear"]``, and ``weights``, and ``weights`` holding
exactly ``num_bits`` equal to the assembler's 4-bit container width,
``type`` ``"int"``, ``symmetric`` true, ``strategy`` ``"group"``,
and a positive ``group_size`` -- with no default fallbacks (a config
carrying only ``{"ignore": [...]}`` cannot describe packed tensors
and fails closed) and with JSON types exact (``num_bits``/
``group_size`` are ints, never bools or floats; ``symmetric`` is
exactly ``True``; ``targets`` is exactly the list ``["Linear"]``).
Any unknown key at any level fails closed: the installed
compressed-tensors ``QuantizationConfig`` sets ``extra="ignore"``
and ignores unknown nested keys silently, so a group ``format``
override or a weights ``block_structure`` entry would load (and
decode) differently than the certified container. The remaining
top-level keys the installed ``QuantizationConfig`` accepts must be
exactly what the pinned writer emits too: ``kv_cache_scheme`` and
``global_compression_ratio`` absent or null (the writer emits
neither, so any value would describe KV-cache quantization or a
compression claim this workflow did not validate) and
``run_compressed`` absent or null (the writer emits no such key;
the installed loader keeps it only as an excluded dummy). Any
other top-level key (``sparsity_config``, ``transform_config``,
``version``, or anything else a future loader might honor) fails
closed. When the caller passes the validated ``group_size``, the
declared width must equal it: packed extents are certified for the
width the run validated. Returns ``(group_size, pack_bits)`` for
the packed-content checks.
"""
where = f"assembled config {config_path}"
allowed_top_level = frozenset(
{
"config_groups",
"format",
"ignore",
"quant_method",
"quantization_status",
"kv_cache_scheme",
"global_compression_ratio",
"run_compressed",
}
)
unknown_top_level = sorted(
key for key in quantization if key not in allowed_top_level
)
if unknown_top_level:
raise ValueError(
f"{where} quantization_config holds unknown top-level keys"
f" {unknown_top_level!r} (save_model.inject_quantization_config"
" writes none of them); refusing to certify metadata a loader"
" might honor that this workflow did not validate"
)
if quantization.get("kv_cache_scheme", None) is not None:
raise ValueError(
f"{where} quantization_config kv_cache_scheme is"
f" {quantization.get('kv_cache_scheme')!r}, expected null (as"
" save_model.inject_quantization_config writes -- it emits no"
" such key); refusing to certify KV-cache quantization this"
" workflow did not validate"
)
if quantization.get("global_compression_ratio", None) is not None:
raise ValueError(
f"{where} quantization_config global_compression_ratio is"
f" {quantization.get('global_compression_ratio')!r}, expected"
" null (as save_model.inject_quantization_config writes -- it"
" emits no such key)"
)
if quantization.get("run_compressed", None) is not None:
raise ValueError(
f"{where} quantization_config run_compressed is"
f" {quantization.get('run_compressed')!r}, expected null (as"
" save_model.inject_quantization_config writes -- it emits no"
" such key)"
)
if quantization.get("quant_method") != "compressed-tensors":
raise ValueError(
f"{where} quantization_config has quant_method"
f" {quantization.get('quant_method')!r}, expected"
" 'compressed-tensors'; refusing to certify an undescribed"
" checkpoint"
)
if quantization.get("format") != "pack-quantized":
raise ValueError(
f"{where} quantization_config has format"
f" {quantization.get('format')!r}, expected 'pack-quantized'"
)
if quantization.get("quantization_status") != "compressed":
raise ValueError(
f"{where} quantization_config has quantization_status"
f" {quantization.get('quantization_status')!r}, expected"
" 'compressed'; refusing to certify a checkpoint whose"
" compression state is undescribed"
)
groups = quantization.get("config_groups")
if not isinstance(groups, Mapping) or len(groups) != 1:
found = (
sorted(groups) if isinstance(groups, Mapping) else groups
)
raise ValueError(
f"{where} quantization_config must hold a single config group"
f" (as save_model.inject_quantization_config writes), found"
f" {found!r}"
)
group = next(iter(groups.values()))
if not isinstance(group, Mapping):
raise ValueError(
f"{where} quantization_config holds an unusable config group:"
f" {group!r}"
)
unknown_group_keys = sorted(
key for key in group if key not in _ASSEMBLED_GROUP_KEYS
)
if unknown_group_keys:
raise ValueError(
f"{where} quantization_config group holds unknown keys"
f" {unknown_group_keys!r}"
" (save_model.inject_quantization_config writes exactly"
" input_activations, output_activations, targets, weights);"
" refusing to certify metadata a loader might honor that"
" this workflow did not validate"
)
for activation_key in ("input_activations", "output_activations"):
if group.get(activation_key, None) is not None:
raise ValueError(
f"{where} quantization_config group {activation_key} is"
f" {group.get(activation_key)!r}, expected null (as"
" save_model.inject_quantization_config writes); refusing"
" to certify activation quantization this workflow did"
" not validate"
)
targets = group.get("targets", None)
if not isinstance(targets, list) or targets != ["Linear"]:
raise ValueError(
f"{where} quantization_config group targets are"
f" {targets!r}, expected ['Linear']"
)
weights = group.get("weights")
if not isinstance(weights, Mapping):
raise ValueError(
f"{where} quantization_config group has no usable weights"
f" block: {weights!r}"
)
unknown_weight_keys = sorted(
key for key in weights if key not in _ASSEMBLED_WEIGHT_KEYS
)
if unknown_weight_keys:
raise ValueError(
f"{where} quantization_config weights hold unknown keys"
f" {unknown_weight_keys!r}"
" (save_model.inject_quantization_config writes exactly"
" num_bits, strategy, group_size, symmetric, type); refusing"
" to certify metadata a loader might honor that this"
" workflow did not validate"
)
raw_bits = weights.get("num_bits")
if (
isinstance(raw_bits, bool)
or not isinstance(raw_bits, int)
or raw_bits != DEFAULT_PACK_BITS
):
raise ValueError(
f"{where} quantization_config weights num_bits is"
f" {raw_bits!r}, expected {DEFAULT_PACK_BITS} (the"
" assembler's pack container width, as a JSON integer)"
)
if weights.get("type") != "int":
raise ValueError(
f"{where} quantization_config weights type is"
f" {weights.get('type')!r}, expected 'int'"
)
if weights.get("symmetric") is not True:
raise ValueError(
f"{where} quantization_config weights symmetric is"
f" {weights.get('symmetric')!r}, expected True"
)
if weights.get("strategy") != "group":
raise ValueError(
f"{where} quantization_config weights strategy is"
f" {weights.get('strategy')!r}, expected 'group'"
)
raw_group = weights.get("group_size")
if (
isinstance(raw_group, bool)
or not isinstance(raw_group, int)
or raw_group <= 0
):
raise ValueError(
f"{where} quantization_config has an unusable group_size:"
f" {raw_group!r}"
)
if group_size is not None and raw_group != group_size:
raise ValueError(
f"{where} quantization_config group_size is {raw_group!r},"
f" but the validated group size is {group_size!r}; refusing"
" packed metadata for a width the run did not validate"
)
return raw_group, DEFAULT_PACK_BITS
def _require_assembled_index_matches_files(
root: Path, key_files: Mapping[str, str]
) -> None:
"""Open every referenced shard and confirm the index matches its keys.
Every reference must additionally resolve inside the output
directory (a symlink escaping it fails closed even when the
filename itself is plain). An index that names a missing file or a
tensor the file does not hold would otherwise pass verification on
key names alone; every reference is therefore opened and checked.
The reverse holds too:
any tensor key inside a referenced shard that is not listed in the
index ``weight_map`` for that file fails closed -- loaders read shard
contents, so an unindexed tensor (such as a NaN ``weight_zero_point``)
would escape certification. Without an index there is nothing to be
unlisted from: every ``*.safetensors`` file's keys are the full set.
"""
from safetensors import safe_open
indexed = (root / "model.safetensors.index.json").is_file()
try:
anchored = root.resolve()
except OSError as exc:
raise ValueError(
f"cannot resolve assembled dir {root}: {exc}"
) from exc
by_file: dict[str, list[str]] = {}
for key, filename in key_files.items():
by_file.setdefault(filename, []).append(key)
for filename, tensor_keys in sorted(by_file.items()):
shard = root / filename
try:
resolved = shard.resolve()
except OSError as exc:
raise ValueError(
f"cannot resolve assembled shard {filename!r}: {exc}"
) from exc
if resolved != anchored and anchored not in resolved.parents:
raise ValueError(
f"assembled shard {filename!r} resolves outside the"
f" output dir ({resolved}); refusing a non-portable"
" export"
)
if not shard.is_file():
raise ValueError(
f"assembled index references missing file {filename!r}"
)
with safe_open(shard, framework="pt", device="cpu") as handle:
present = set(handle.keys())
for key in tensor_keys:
if key not in present:
raise ValueError(
f"assembled index references tensor {key!r} in"
f" {filename!r}, but the file does not hold it"
)
if indexed:
unindexed = sorted(present - set(tensor_keys))
if unindexed:
raise ValueError(
f"assembled shard {filename!r} holds tensor(s) not"
" listed in the index weight_map for that file"
f" {unindexed[:5]}; loaders read shard contents, so"
" unindexed tensors escape certification"
)
def _require_assembled_packed_contents(
root: Path,
key_files: Mapping[str, str],
*,
group_size: int,
pack_bits: int,
load_dtypes=(),
) -> None:
"""Require honest dtypes/shapes for every assembled packed module.
Mirrors :func:`_require_tensor_shapes` without a base table: the
recorded ``weight_shape`` ``[out, in]`` is the truth, and the packed
and scale extents must match it for the assembler's container width.
``in_features`` must be divisible by ``group_size`` (group
quantization has no padding path, so a remainder could never load).
Finite scales are additionally certified to decode to finite
weights as the pack-quantized decompressor computes them (see
:func:`_require_decoded_weights_finite`), in bounded memory -- in
the stored scale dtype and in every dtype in ``load_dtypes`` (each
bound model load dtype: the loader casts scales before decoding).
"""
import torch
from safetensors import safe_open
packed_modules = sorted(
key[: -len(".weight_packed")]
for key in key_files
if key.endswith(".weight_packed")
)
if not packed_modules:
return
by_file: dict[str, list[str]] = {}
for module in packed_modules:
for suffix in REQUIRED_PACKED_SUFFIXES:
key = f"{module}.{suffix}"
if key not in key_files:
continue
by_file.setdefault(key_files[key], []).append(key)
loaded: dict[str, torch.Tensor] = {}
for filename, tensor_keys in by_file.items():
with safe_open(root / filename, framework="pt", device="cpu") as h:
for tensor_key in tensor_keys:
loaded[tensor_key] = h.get_tensor(tensor_key)
for module in packed_modules:
packed = loaded.get(f"{module}.weight_packed")
scale = loaded.get(f"{module}.weight_scale")
shape = loaded.get(f"{module}.weight_shape")
if packed is None or scale is None or shape is None:
continue
if packed.dtype != torch.int32:
raise ValueError(
f"{module}.weight_packed has dtype {packed.dtype},"
" expected int32"
)
if not scale.is_floating_point():
raise ValueError(
f"{module}.weight_scale has dtype {scale.dtype},"
" expected a floating dtype"
)
if not bool(torch.isfinite(scale).all()):
raise ValueError(
f"{module}.weight_scale is not finite; a NaN/Inf scale"
" dequantizes to non-finite weights"
)
if shape.dtype != torch.int64 or tuple(shape.shape) != (2,):
raise ValueError(
f"{module}.weight_shape has dtype {shape.dtype} with shape"
f" {tuple(shape.shape)}, expected int64 with shape (2,)"
)
if bool((shape <= 0).any()):
raise ValueError(
f"{module}.weight_shape holds {shape.tolist()}, expected"
" positive [out, in] extents"
)
for label, tensor in (
("weight_packed", packed),
("weight_scale", scale),
("weight_shape", shape),
):
if tensor.numel() == 0:
raise ValueError(
f"{module}.{label} is empty; the checkpoint holds no"
" usable quantized projection"
)
out, inn = shape.tolist()
if (inn * pack_bits) % 32 != 0:
raise ValueError(
f"{module}: in_features {inn} is not divisible for a"
f" {pack_bits}-bit pack container"
)
if inn % group_size != 0:
raise ValueError(
f"{module}: in_features {inn} is not divisible by"
f" group_size {group_size}; group quantization has no"
" padding path (the loader apportions columns with"
" unflatten(-1, (num_groups, group_size))), so this"
" projection cannot load"
)
expected_packed = (out, inn * pack_bits // 32)
if tuple(packed.shape) != expected_packed:
raise ValueError(
f"{module}.weight_packed has shape {tuple(packed.shape)},"
f" expected {expected_packed} for [out, in]=[{out}, {inn}]"
f" with pack_bits={pack_bits}"
)
expected_scale = (out, ceil(inn / group_size))
if tuple(scale.shape) != expected_scale:
raise ValueError(
f"{module}.weight_scale has shape {tuple(scale.shape)},"
f" expected {expected_scale} for [out, in]=[{out}, {inn}]"
f" with group_size={group_size}"
)
_require_decoded_weights_finite(
packed,
scale,
in_features=inn,
pack_bits=pack_bits,
module=module,
load_dtypes=load_dtypes,
)
def _base_keys_top_level(base: Path) -> set[str]:
"""Return every base-model tensor key from top-level shards.
Mirrors pinned ``save_model.collect_base_model_entries`` (every
top-level ``*.safetensors`` file; the index is ignored). A key
repeated across shards fails closed via
:func:`_require_no_duplicate_base_keys` instead of merging silently.
"""
from safetensors import safe_open
shards = _top_level_safetensor_files(base)
if not shards:
raise ValueError(f"no base-model weights in {base}")
_require_no_duplicate_base_keys(base)
keys: set[str] = set()
for shard in shards:
with safe_open(shard, framework="pt", device="cpu") as handle:
keys.update(handle.keys())
return keys
def _require_packed_shapes_match_base(
root: Path,
key_files: Mapping[str, str],
base: Path,
) -> None:
"""Require each packed module's ``weight_shape`` to match its base weight.
The assembled ``weight_shape`` ``[out, in]`` must equal the base
model's ``<module>.weight`` shape from the top-level shard set the
assembler collects. Trusting ``weight_shape`` alone would certify a
checkpoint whose packed metadata disagrees with the base weights it
was merged with. Missing base weights fail closed.
"""
from safetensors import safe_open
packed_modules = sorted(
key[: -len(".weight_packed")]
for key in key_files
if key.endswith(".weight_packed")
)
if not packed_modules:
return
base_shapes = read_base_weight_shapes(base, packed_modules)
by_file: dict[str, list[str]] = {}
for module in packed_modules:
key = f"{module}.weight_shape"
if key not in key_files:
continue
by_file.setdefault(key_files[key], []).append(key)
recorded: dict[str, list[int]] = {}
for filename, tensor_keys in by_file.items():
with safe_open(root / filename, framework="pt", device="cpu") as handle:
for tensor_key in tensor_keys:
recorded[tensor_key[: -len(".weight_shape")]] = (
handle.get_tensor(tensor_key).tolist()
)
for module in packed_modules:
if module not in recorded:
continue
expected = list(base_shapes[module])
if recorded[module] != expected:
raise ValueError(
f"{module}.weight_shape holds {recorded[module]}, but the"
f" base model weight is {expected}; refusing packed metadata"
" that disagrees with the base weights"
)
def _require_retained_base_tensors(
base: Path,
key_files: Mapping[str, str],
*,
out_dir: str | Path | None = None,
load_dtypes=(),
) -> None:
"""Require every retained base tensor in the assembled output.
Derives the expected retained set the way pinned
``save_model.py`` does, except that only a complete packed trio
(``weight_packed`` + ``weight_scale`` + ``weight_shape``) exempts a
base ``<prefix>.weight``: orphan metadata (a scale/shape without a
packed tensor) must not silently drop its base weight. Missing
retained tensors (embeddings, norms, vision weights, ...) fail
closed. Tied embeddings need no special case: a tied ``lm_head``
absent from the base simply has nothing to require. When
``out_dir`` is given, every retained/overwritten base tensor in the
output must additionally match the base dtype and shape and hold
finite, non-empty contents (streamed one tensor at a time, never
the whole model at once) -- and every floating one must stay finite
after casting to each dtype in ``load_dtypes`` in bounded chunks
(the loader casts floating weights to the load dtype, so a
stored-finite weight that overflows it would load as non-finite):
assembly overwrites retained keys by exact name, so a wrong-shaped
or NaN norm would otherwise ship.
"""
import torch
from safetensors import safe_open
base_files = _base_key_files(base)
if not base_files:
raise ValueError(f"no base-model weights in {base}")
base_keys = set(base_files)
trio_present: dict[str, set[str]] = {}
for key in key_files:
for suffix in REQUIRED_PACKED_SUFFIXES:
if key.endswith(f".{suffix}"):
trio_present.setdefault(
key[: -(len(suffix) + 1)], set()
).add(suffix)
break
quantized_prefixes = {
module for module, suffixes in trio_present.items() if len(suffixes) == 3
}
missing = sorted(
key
for key in base_keys
if not (
key.endswith(".weight")
and key[: -len(".weight")] in quantized_prefixes
)
and key not in key_files
)
if missing:
raise ValueError(
"assembled checkpoint is missing"
f" {len(missing)} retained base tensor(s)"
f" (e.g. {missing[:3]}); the assembler keeps every"
" non-quantized base weight"
)
if out_dir is None:
return
root = Path(out_dir)
retained = sorted(
key
for key in base_keys
if not (
key.endswith(".weight")
and key[: -len(".weight")] in quantized_prefixes
)
)
for key in retained:
out_file = root / key_files[key]
with safe_open(out_file, framework="pt", device="cpu") as handle:
out_tensor = handle.get_tensor(key)
out_dtype = out_tensor.dtype
out_shape = tuple(out_tensor.shape)
out_numel = out_tensor.numel()
out_finite = (
bool(torch.isfinite(out_tensor).all())
if out_tensor.is_floating_point()
else True
)
out_floating = out_tensor.is_floating_point()
if out_floating and out_finite:
_require_finite_after_cast_to_load_dtypes(
out_tensor,
key=key,
what="assembled tensor",
load_dtypes=load_dtypes,
)
del out_tensor
if out_numel == 0:
raise ValueError(
f"assembled tensor {key!r} is empty; the output holds no"
" usable weight"
)
if out_floating and not out_finite:
raise ValueError(
f"assembled tensor {key!r} is not finite; the output"
" would load NaN/Inf weights"
)
base_shard = base_files[key]
with safe_open(base_shard, framework="pt", device="cpu") as handle:
base_tensor = handle.get_tensor(key)
base_dtype = base_tensor.dtype
base_shape = tuple(base_tensor.shape)
del base_tensor
if out_dtype != base_dtype:
raise ValueError(
f"assembled tensor {key!r} has dtype {out_dtype}, expected"
f" base dtype {base_dtype}"
)
if out_shape != base_shape:
raise ValueError(
f"assembled tensor {key!r} has shape {list(out_shape)},"
f" expected base shape {list(base_shape)}"
)
def _template_files_present_in_base(base: Path) -> list[str]:
"""List bound template files present in the base dir, sorted.
Covers the top-level template filenames in
:data:`_TEMPLATE_NON_WEIGHT_FILENAMES` plus
``chat_templates/*.jinja`` (direct children only, as the patched
assembler copies them). Keys use checkout-relative form
(``chat_templates/<name>`` for the subdirectory).
"""
keys: list[str] = []
for name in sorted(_TEMPLATE_NON_WEIGHT_FILENAMES):
try:
if (base / name).is_file():
keys.append(name)
except OSError as exc:
raise ValueError(
f"cannot stat base model file {base / name}: {exc}"
) from exc
chat_dir = base / CHAT_TEMPLATES_SUBDIR
try:
is_dir = chat_dir.is_dir()
except OSError as exc:
raise ValueError(
f"cannot stat base model dir {chat_dir}: {exc}"
) from exc
if is_dir:
try:
entries = sorted(path.name for path in chat_dir.iterdir())
except OSError as exc:
raise ValueError(
f"cannot list base model dir {chat_dir}: {exc}"
) from exc
for name in entries:
if not name.endswith(".jinja"):
continue
candidate = chat_dir / name
try:
if not candidate.is_file():
continue
except OSError as exc:
raise ValueError(
f"cannot stat base model file {candidate}: {exc}"
) from exc
keys.append(f"{CHAT_TEMPLATES_SUBDIR}/{name}")
return keys
def _require_copied_template_files(base: Path, out: Path) -> None:
"""Require every base template file in the output with identical bytes.
Every template file present in the base dir (see
:func:`_template_files_present_in_base`) must be present in the
assembled output with byte-identical contents: the patched
assembler copies them verbatim, so a missing or altered template
means the output lost chat/inference metadata. Anything else fails
closed.
"""
for key in _template_files_present_in_base(base):
src = base / key
dst = out / key
try:
expected = src.read_bytes()
except OSError as exc:
raise ValueError(
f"cannot read base model file {src}: {exc}"
) from exc
try:
actual = dst.read_bytes()
except OSError as exc:
raise ValueError(
f"assembled output is missing base template file {key!r}:"
f" {exc}"
) from exc
if actual != expected:
raise ValueError(
f"assembled output template file {key!r} differs from the"
" base model; refusing output with altered chat/inference"
" metadata"
)
def verify_assembled_checkpoint(
out_dir: str | Path,
*,
base_model_dir: str | Path | None = None,
expected_packed_modules: Iterable[str] | None = None,
group_size: int | None = None,
load_dtype=None,
) -> dict[str, list[str]]:
"""Require an assembled dir's metadata to describe its tensors honestly.
Loads the assembled ``config.json`` ``quantization_config`` and
requires exactly what the pinned
``save_model.inject_quantization_config`` writes -- no default
fallbacks: ``quant_method`` ``"compressed-tensors"``, ``format``
``"pack-quantized"``, ``quantization_status`` ``"compressed"``, a
single config group whose ``input_activations`` and
``output_activations`` are null and whose ``targets`` are
``["Linear"]`` and whose ``weights`` declare ``num_bits`` equal to
the assembler's 4-bit container width, ``type`` ``"int"``,
``symmetric`` true, ``strategy`` ``"group"``, and a positive
``group_size`` (which must equal the validated ``group_size`` when
the caller passes it) -- with exactly those group/weights keys, exact
JSON types, and no other nested key (a group ``format`` override, a
weights ``block_structure`` entry, or any other unknown nested key
fails closed, as do ``dynamic``/``actorder``, which the writer never
emits). The output layout must be loadable by
Transformers: either a single ``model.safetensors`` (no index, and
no other weight files) or ``model.safetensors.index.json`` whose
``weight_map`` covers exactly the shard files present; numbered
shards without an index fail closed. Index ``weight_map`` filenames
must be plain top-level assembler shard names
(``model-<NNNNN>-of-<MMMMM>.safetensors`` -- no directories,
``..``, absolute paths, or other suffixes) resolving inside the
output directory, so a traversal, absolute, subdirectory, or
escaping-symlink reference can never certify a non-portable export.
The ``ignore`` list keeps
compressed-tensors semantics: plain names compared exactly and
``re:`` regexes applied with ``re.match`` -- anchored at the start
-- against module names (tensor keys without the ``.weight`` /
``.weight_packed`` suffix), PLUS plain entries matched against
module class names (as the loader's ``is_match`` does -- a plain
``"Linear"`` or ``"Module"`` entry skips every packed projection;
``re:`` entries never match a class): every module holding a 2-D
dense ``<module>.weight`` tensor must be ignored, and every module
holding ``<module>.weight_packed`` must NOT be ignored and must
still carry ``<module>.weight_scale`` and ``<module>.weight_shape``.
Any module
holding any of ``weight_packed`` / ``weight_scale`` / ``weight_shape``
must hold all three (complete trio): orphan metadata fails closed,
including zero packed modules with orphan scales/shapes. The symmetric
pack-quantized container (gptq ``sym: True``) must not carry optional
tensors: any ``weight_zero_point``, ``weight_g_idx``, or other
``weight_*`` tensor besides the trio raises. Competing checkpoint
files fail closed: with an index, ``model.safetensors`` is always
refused (even when referenced, loaders prefer it over indexed shards)
and every other top-level ``*.safetensors`` / ``*.bin`` / ``*.pt`` /
``pytorch_model*`` file must be listed in its ``weight_map`` values;
without an index, any ``*.bin`` /
``*.pt`` / ``pytorch_model*`` file fails. Every referenced shard
file is opened: each indexed key must exist in its file with honest
packed dtypes/shapes (``in_features`` must be divisible by the
validated ``group_size`` -- group quantization has no padding path,
so a remainder could never load -- and scales must decode to finite
weights in the stored dtype and in every bound load dtype: the
explicit ``load_dtype``, the bound base dtype, and the assembled
config's own ``torch_dtype``/``dtype`` declarations, since the loader
casts scales to the load dtype before decoding), and a config+index
referencing nonexistent
tensors or missing files fails closed -- as does any tensor key
inside a referenced shard that is not listed in the index
``weight_map`` for that file (loaders read shard contents, so
unindexed tensors would escape certification; without an index the
single ``model.safetensors`` file's own keys are the full set). When
``base_model_dir`` is given, each packed module's ``weight_shape``
must equal its base
model's ``<module>.weight`` shape (the packed metadata is never
trusted alone), and every retained base-model tensor (the top-level
shard set the assembler collects, minus dropped
quantized-projection weights for complete trios only) must be
present in the output with the base dtype and shape and finite,
non-empty contents (streamed per tensor) -- and every floating one
finite after casting to each bound load dtype in bounded chunks
(the loader casts retained weights too, so a stored-finite weight
that overflows the load dtype would load as non-finite) -- so
orphan metadata cannot silently drop a base weight. Every chat/inference template file
present in the base dir (``chat_template.jinja``,
``chat_template.json``, ``added_tokens.json``,
``processor_config.json``, ``video_preprocessor_config.json``, and
``chat_templates/*.jinja``) must be present in the output with
identical bytes, so a dropped or altered template fails closed.
Base-model reads refuse duplicated
tensor keys across the top-level shards instead of guessing which
copy assembly merged. When ``expected_packed_modules`` is given, the
output packed set must equal it exactly (missing or extra projections
fail); without it, at least complete trios are required.
Anything else -- a missing ``quantization_config``, an undescribed
container (bare ``{"ignore": [...]}``, a wrong method, format,
status, group count, targets, width, type, symmetry, strategy,
group size, activations, KV-cache scheme, compression ratio,
``run_compressed`` flag, a group ``format`` override, a weights
``block_structure`` entry, or any other unknown key at any level),
a non-portable
index reference (traversal, absolute path, subdirectory, foreign
suffix, or a symlink escaping the output dir), an unignored dense
projection (such as a vision Linear the ignore list does not
cover), an ignored packed module, packed metadata without
scale/shape, orphan metadata, competing weight files, a missing
retained base weight (such as ``embed_tokens``), or a missing or
altered base template file -- fails closed.
Returns the verified ``{"packed_modules", "ignored_dense_modules"}``.
"""
root = Path(out_dir)
if not root.is_dir():
raise ValueError(f"assembled dir not found: {root}")
config_path = root / "config.json"
try:
config = json.loads(config_path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
raise ValueError(
f"cannot load assembled config {config_path}: {exc}"
) from exc
if not isinstance(config, dict):
raise ValueError(f"assembled config {config_path} must be a JSON object")
quantization = config.get("quantization_config")
if not isinstance(quantization, dict):
raise ValueError(
f"assembled config {config_path} has no quantization_config;"
" refusing to certify an undescribed checkpoint"
)
ignore = quantization.get("ignore")
if not isinstance(ignore, list) or not all(
isinstance(entry, str) for entry in ignore
):
raise ValueError(
f"assembled config {config_path} has no usable"
" quantization_config.ignore list"
)
key_files = _assembled_key_files(root)
_require_no_competing_weight_files(root, key_files)
_require_no_optional_assembled_tensors(key_files)
_require_complete_packed_trios(key_files)
_require_assembled_index_matches_files(root, key_files)
# Provenance after existence: the traversal/absolute/subdirectory
# and escaping-symlink references already failed closed above
# (before any shard was opened); a plain contained reference with
# a foreign filename shape is not the assembler's output either.
_require_assembler_shard_names(root, key_files)
validated_group_size, pack_bits = _assembled_pack_params(
quantization, config_path=config_path, group_size=group_size
)
# Certify scales in every dtype the output may load (and be
# dequantized) under: the explicitly bound load dtype, the bound
# base dtype when base weights are given, and the assembled config's
# own torch_dtype/dtype declarations (an omitted load request
# resolves from torch_dtype). The loader casts scales before
# decoding, so each must be finite after casting and decode to
# finite weights in each candidate dtype.
load_candidates: list = []
bound = _torch_dtype_from_name(load_dtype)
if bound is not None:
load_candidates.append(bound)
if base_model_dir is not None:
for dtype in _base_float_load_dtypes(Path(base_model_dir)):
if dtype not in load_candidates:
load_candidates.append(dtype)
for dtype in _assembled_config_load_dtypes(config):
if dtype not in load_candidates:
load_candidates.append(dtype)
_require_assembled_packed_contents(
root,
key_files,
group_size=validated_group_size,
pack_bits=pack_bits,
load_dtypes=load_candidates,
)
if base_model_dir is not None:
_require_packed_shapes_match_base(
root, key_files, Path(base_model_dir)
)
_require_retained_base_tensors(
Path(base_model_dir),
key_files,
out_dir=root,
load_dtypes=load_candidates,
)
_require_copied_template_files(Path(base_model_dir), root)
if expected_packed_modules is not None:
expected_set = set(expected_packed_modules)
actual_set = {
key[: -len(".weight_packed")]
for key in key_files
if key.endswith(".weight_packed")
}
if actual_set != expected_set:
missing = sorted(expected_set - actual_set)[:5]
extra = sorted(actual_set - expected_set)[:5]
raise ValueError(
"assembled packed modules do not match the validated"
f" projection set (missing={missing}, extra={extra})"
)
modules: dict[str, dict[str, str]] = {}
for key in key_files:
if key.endswith(".weight"):
modules.setdefault(key[: -len(".weight")], {})["dense"] = key
elif key.endswith(".weight_packed"):
modules.setdefault(key[: -len(".weight_packed")], {})["packed"] = key
dense_keys = [
entry["dense"] for entry in modules.values() if "dense" in entry
]
ndims = _safetensors_key_ndims(root, key_files, dense_keys)
ignored = [module for module in modules if _ignore_matches(ignore, module)]
unignored_dense = sorted(
module
for module, entry in modules.items()
if "dense" in entry
and ndims.get(entry["dense"]) == 2
and module not in set(ignored)
)
if unignored_dense:
raise ValueError(
"assembled checkpoint holds dense 2-D weights the"
" quantization_config ignore list does not cover"
f" (they would load as unpacked Linears): {unignored_dense[:5]}"
)
ignored_packed = sorted(
module
for module, entry in modules.items()
if "packed" in entry and module in set(ignored)
)
if ignored_packed:
raise ValueError(
"assembled checkpoint marks packed modules ignored in"
f" quantization_config (loaders would skip them): {ignored_packed[:5]}"
)
missing_meta = sorted(
module
for module, entry in modules.items()
if "packed" in entry
and (
f"{module}.weight_scale" not in key_files
or f"{module}.weight_shape" not in key_files
)
)
if missing_meta:
raise ValueError(
"assembled packed modules lack weight_scale/weight_shape:"
f" {missing_meta[:5]}"
)
return {
"packed_modules": sorted(
module for module, entry in modules.items() if "packed" in entry
),
"ignored_dense_modules": sorted(
module
for module, entry in modules.items()
if "dense" in entry
and ndims.get(entry["dense"]) == 2
and module in set(ignored)
),
}
def require_complete_coverage(coverage: GSQCoverage) -> GSQCoverage:
"""Fail closed unless every targeted projection is packed with no fallback."""
if coverage.passed:
return coverage
details: list[str] = []
if coverage.missing_modules:
preview = list(coverage.missing_modules)[:5]
details.append(
f"missing {len(coverage.missing_modules)}"
f" packed modules, e.g. {preview}"
)
if coverage.dense_fallback_modules:
preview = list(coverage.dense_fallback_modules)[:5]
details.append(
f"dense fallback for {len(coverage.dense_fallback_modules)}"
f" modules, e.g. {preview}"
)
raise ValueError("incomplete GSQ coverage: " + "; ".join(details))