Files
OBLITERATUS/obliteratus/models/checkpoint_export.py

569 lines
25 KiB
Python

"""Plan and write native HF checkpoints with only one CPU shard staged at a time.
The shard splitter, serializer and Accelerate alignment context are public APIs.
The sole Transformers compatibility dependency is its tied-weight removal helper;
if missing, planning declines and the pipeline uses its legacy save path. Offline
byte-identity tests pin the helper's behavior without pinning package versions.
Byte identity with legacy export applies to fully resident or fully offloaded models; partial
offload can change shard assignment/order relative to the legacy full gather.
"""
from __future__ import annotations
import ctypes
import gc
import hashlib
import json
import os
import sys
from collections import defaultdict
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
import torch
from accelerate.utils import align_module_device, has_offloaded_params
from huggingface_hub import split_torch_state_dict_into_shards
from safetensors import safe_open
from safetensors.torch import save_file
from transformers import PreTrainedModel
from obliteratus.models import host_resources
try:
# Non-public compatibility boundary, covered by byte-identity tests.
from transformers.modeling_utils import remove_tied_weights_from_state_dict
except ImportError: # Older Transformers: retain the legacy full-gather writer.
remove_tied_weights_from_state_dict = None
WORKSPACE_BYTES = 1024**2
_SAFE_DTYPES = {
torch.float64: "F64", torch.float32: "F32", torch.float16: "F16",
torch.bfloat16: "BF16", torch.int64: "I64", torch.int32: "I32",
torch.int16: "I16", torch.int8: "I8", torch.uint8: "U8", torch.bool: "BOOL",
}
class NativeExportCapacityError(RuntimeError):
"""A shard cannot be staged within the currently available host budget."""
@dataclass(frozen=True)
class TensorSpec:
shape: tuple[int, ...]
dtype: torch.dtype
size_bytes: int
# Module whose public Accelerate context can materialize this tensor.
offload_owner: str | None
@dataclass(frozen=True)
class ExportShard:
filename: str
tensor_names: tuple[str, ...]
size_bytes: int
host_demand_bytes: int
@dataclass(frozen=True)
class NativeExportPlan:
tensors: dict[str, TensorSpec]
shards: tuple[ExportShard, ...]
total_bytes: int
index: dict | None
max_shard_size: int | str
metadata_only: bool = False
@dataclass(frozen=True)
class ExportShardReceipt:
"""Content receipt bound to one candidate and serialized tensor layout."""
candidate_sha256: str
plan_sha256: str
filename: str
size_bytes: int
sha256: str
@dataclass(frozen=True)
class ExportContinuation:
"""Caller-owned storage for verified, previously serialized shards.
The caller must attest that ``candidate_sha256`` identifies the exact live
weights, including their immutable base and edit/runtime provenance. A model
name, recipe, or tensor layout alone is insufficient. The writer verifies
stored bytes against receipts; it cannot verify this live-weight attestation
without materializing the weights again.
``restore_shard`` writes the receipt's bytes to the supplied temporary path.
``record_shard`` durably stores the finalized file and its receipt together,
before returning. Paths belong to atomic staging and must not be retained as
durable references. Callback errors propagate. Receipts must come from the
caller's trusted, immutable storage manifest.
"""
candidate_sha256: str
receipts: tuple[ExportShardReceipt, ...] = ()
restore_shard: Callable[[ExportShardReceipt, Path], None] | None = None
record_shard: Callable[[Path, ExportShardReceipt], None] | None = None
@dataclass(frozen=True)
class ExportProgress:
"""One-based shard progress; hash events report file bytes read so far."""
event: str
filename: str
index: int
total: int
payload_bytes: int
reused: bool
completed_bytes: int = 0
def native_export_plan_sha256(plan: NativeExportPlan) -> str:
"""Identify serialized layout independently of current device placement."""
payload = {
"schema": 1,
"tensors": {
name: {"shape": spec.shape, "dtype": str(spec.dtype), "size_bytes": spec.size_bytes}
for name, spec in plan.tensors.items()
},
"shards": [
{"filename": shard.filename, "tensor_names": shard.tensor_names, "size_bytes": shard.size_bytes}
for shard in plan.shards
],
"total_bytes": plan.total_bytes,
"index": plan.index,
"max_shard_size": plan.max_shard_size,
"metadata_only": plan.metadata_only,
}
return hashlib.sha256(json.dumps(
payload, sort_keys=True, separators=(",", ":"), allow_nan=False,
).encode()).hexdigest()
def _sha256(value: object) -> bool:
return isinstance(value, str) and len(value) == 64 and all(c in "0123456789abcdef" for c in value)
def _continuation_receipts(
continuation: ExportContinuation, plan: NativeExportPlan,
) -> tuple[str, dict[str, ExportShardReceipt]]:
if not isinstance(continuation, ExportContinuation):
raise TypeError("continuation must be ExportContinuation or None")
if not _sha256(continuation.candidate_sha256):
raise ValueError("Export continuation requires a candidate SHA-256")
if not isinstance(continuation.receipts, tuple):
raise TypeError("Export continuation receipts must be an immutable tuple")
for name in ("restore_shard", "record_shard"):
callback = getattr(continuation, name)
if callback is not None and not callable(callback):
raise TypeError(f"Export continuation {name} must be callable or None")
if continuation.receipts and continuation.restore_shard is None:
raise ValueError("Export continuation receipts require restore_shard")
names = {shard.filename for shard in plan.shards}
if len(names) != len(plan.shards) or any(
Path(name).name != name or name.startswith(".") or "\\" in name or not name.endswith(".safetensors")
for name in names
):
raise ValueError("Export continuation requires unique, safe shard filenames")
plan_sha256 = native_export_plan_sha256(plan)
receipts = {}
for receipt in continuation.receipts:
if not isinstance(receipt, ExportShardReceipt):
raise TypeError("Export continuation requires ExportShardReceipt values")
if receipt.candidate_sha256 != continuation.candidate_sha256:
raise ValueError("Export receipt candidate differs")
if receipt.plan_sha256 != plan_sha256:
raise ValueError("Export receipt plan differs")
if receipt.filename not in names or receipt.filename in receipts:
raise ValueError("Export receipt shard is unknown or duplicated")
if type(receipt.size_bytes) is not int or receipt.size_bytes <= 0 or not _sha256(receipt.sha256):
raise ValueError("Export receipt content identity is invalid")
receipts[receipt.filename] = receipt
return plan_sha256, receipts
def _hash_shard(
path: Path, check_cancelled: Callable[[], None], progress: Callable[[int], None],
) -> tuple[int, str]:
digest = hashlib.sha256()
completed = 0
with path.open("rb") as source:
while True:
check_cancelled()
block = source.read(WORKSPACE_BYTES // 2)
if not block:
break
digest.update(block)
completed += len(block)
progress(completed)
return completed, digest.hexdigest()
def native_export_host_demand(plan: NativeExportPlan) -> int:
"""Peak incremental host bytes, including serialization and onload staging.
Onload accounting conservatively counts each offload owner's whole subtree.
Whole-model root-level offload therefore overestimates demand intentionally
so capacity admission remains fail-safe.
"""
return max((shard.host_demand_bytes for shard in plan.shards), default=0)
def native_export_summary(plan: NativeExportPlan | None) -> dict:
"""Return JSON evidence without retaining weights or exposing local paths."""
if plan is None:
return {
"eligible": False, "status": "ineligible", "bounded": False,
"reason": "native serialization or backing layout is unsupported",
}
return {
"eligible": True, "bounded": True,
"status": "capacity_estimate_only" if plan.metadata_only else "eligible",
"runtime_check_required": plan.metadata_only,
"total_bytes": plan.total_bytes,
"tensor_count": len(plan.tensors),
"shard_count": len(plan.shards),
"largest_tensor_bytes": max((spec.size_bytes for spec in plan.tensors.values()), default=0),
"largest_shard_bytes": max((shard.size_bytes for shard in plan.shards), default=0),
"host_peak_bytes": native_export_host_demand(plan),
"max_shard_size": plan.max_shard_size,
"reason": (
"config-only shapes; recheck loaded tensor types, ties and offload owners before editing"
if plan.metadata_only else "one CPU shard and at most one offload owner staged at a time"
),
}
def _offload_owner(model: PreTrainedModel, name: str) -> str | None:
parts = name.split(".")[:-1]
for length in range(len(parts), -1, -1):
path = ".".join(parts[:length])
if has_offloaded_params(model.get_submodule(path)):
return path
return None
def _ordinary_aliases(state: dict[str, torch.Tensor], model: PreTrainedModel, *, allow_meta: bool = False) -> bool:
"""Decline shared views before tie removal can clone real tensor storage.
Offloaded meta aliases are declined: full-gather onloading can break their
identity. Config-only aliases permit provisional resident export estimates.
"""
groups = defaultdict(list)
for name, tensor in state.items():
if tensor.device.type == "meta":
key = ("meta", id(model.get_parameter_or_buffer(name)))
else:
key = (tensor.device, tensor.untyped_storage().data_ptr())
groups[key].append(tensor)
for tensors in groups.values():
if len(tensors) < 2:
continue
first = tensors[0]
if first.device.type == "meta" and not allow_meta:
return False
if not first.is_contiguous() or any(
(tensor.shape, tensor.dtype, tensor.stride(), tensor.storage_offset())
!= (first.shape, first.dtype, first.stride(), first.storage_offset())
for tensor in tensors[1:]
):
return False
return True
def _has_offloaded_tied_weights(state: dict[str, torch.Tensor], model: PreTrainedModel) -> bool:
"""Meta placeholders can hide shared backing storage of declared ties."""
try:
ties = model.get_expanded_tied_weights_keys(all_submodels=True)
tied_keys = set(ties) | set(ties.values())
except Exception: # Older/custom Transformers: use the embedding tie contract.
if not getattr(model.config, "tie_word_embeddings", False):
return False
embeddings = (model.get_input_embeddings(), model.get_output_embeddings())
tied_keys = {
f"{name}.weight" if name else "weight"
for name, module in model.named_modules(remove_duplicate=False)
if any(module is embedding for embedding in embeddings)
}
return any(name in state and state[name].device.type == "meta" for name in tied_keys)
def plan_native_export(
model: object, max_shard_size: int | str = "2GB", *, allow_unbacked_meta: bool = False,
) -> NativeExportPlan | None:
"""Inspect shapes/dtypes and split metadata without reading offloaded weights.
Plans retain no tensor storage. Unsupported serialization customizations
return None so callers can use the existing full-gather save_pretrained path.
``allow_unbacked_meta`` permits config-only capacity estimates. Such plans
cannot be written and never establish eligibility for the eventual model.
"""
if not isinstance(model, PreTrainedModel) or remove_tied_weights_from_state_dict is None:
return None
if (
getattr(model, "hf_quantizer", None) is not None
or getattr(model.config, "quantization_config", None) is not None
or getattr(model, "_hf_peft_config_loaded", False)
or getattr(model, "_tp_size", None) is not None
or getattr(model.config, "distributed_config", None) is not None
or getattr(model, "distributed_config", None) is not None
or getattr(model, "_keys_to_ignore_on_save", None)
or model.is_remote_code()
or type(model).save_pretrained is not PreTrainedModel.save_pretrained
or "save_pretrained" in model.__dict__
):
return None
# Detect both registered hooks and custom state_dict implementations before
# invoking them. nn.Module's hook registries have no public inspection API.
for module in model.modules():
if (
type(module).__name__.startswith("FSDP")
or module._state_dict_hooks or module._state_dict_pre_hooks
or type(module).state_dict is not torch.nn.Module.state_dict
or type(module)._save_to_state_dict is not torch.nn.Module._save_to_state_dict
or type(module).get_extra_state is not torch.nn.Module.get_extra_state
or "state_dict" in module.__dict__
or "_save_to_state_dict" in module.__dict__
):
return None
# Check live values before state_dict() can detach a distributed/custom
# tensor subclass into a seemingly ordinary tensor.
if any(
type(tensor) not in (torch.Tensor, torch.nn.Parameter)
for tensor in (*module.parameters(recurse=False), *module.buffers(recurse=False))
):
return None
state = model.state_dict()
if not state or any(
type(tensor) is not torch.Tensor or tensor.layout != torch.strided
or tensor.dtype not in _SAFE_DTYPES
for tensor in state.values()
):
return None
config_only = allow_unbacked_meta and all(
tensor.device.type == "meta" and _offload_owner(model, name) is None
for name, tensor in state.items()
)
if ((not config_only and _has_offloaded_tied_weights(state, model))
or not _ordinary_aliases(state, model, allow_meta=config_only)):
return None
state = remove_tied_weights_from_state_dict(state, model)
# Partial offload: native keeps model state_dict order; Accelerate's legacy
# full gather emits resident tensors first, so shard bytes/index can differ.
split = split_torch_state_dict_into_shards(
state, filename_pattern="model{suffix}.safetensors", max_shard_size=max_shard_size,
)
specs = {}
onload_bytes = {}
metadata_only = False
for name, tensor in state.items():
owner = None
if tensor.device.type == "meta":
owner = _offload_owner(model, name)
if owner is None:
if not allow_unbacked_meta:
return None
metadata_only = True
if owner is not None and owner not in onload_bytes:
module = model.get_submodule(owner)
# An alignment context may load a subtree as well as the direct
# owner. Account conservatively without inspecting private hooks.
onload_bytes[owner] = sum(
value.numel() * value.element_size()
for value in (*module.parameters(), *module.buffers())
)
specs[name] = TensorSpec(
tuple(tensor.shape), tensor.dtype, tensor.numel() * tensor.element_size(), owner,
)
shards = []
for filename, names in split.filename_to_tensors.items():
size = sum(specs[name].size_bytes for name in names)
onload = max((onload_bytes.get(specs[name].offload_owner, 0) for name in names), default=0)
# CPU shard + serializer/contiguity copy + temporary module onload and
# loader copy + small workspace. No prior shard survives the next one.
shards.append(ExportShard(
filename, tuple(names), size, 2 * size + 2 * onload + WORKSPACE_BYTES,
))
index = None
if split.is_sharded:
index = {
"metadata": {"total_parameters": model.num_parameters(), **split.metadata},
"weight_map": split.tensor_to_filename,
}
return NativeExportPlan(
specs, tuple(shards), split.metadata["total_size"], index, max_shard_size, metadata_only,
)
def _materialize_tensor(model: PreTrainedModel, name: str, spec: TensorSpec) -> torch.Tensor:
if spec.offload_owner is None:
# Copy from device without relocating model parameters (including CUDA).
tensor = model.get_parameter_or_buffer(name).detach().to("cpu").contiguous()
else:
with align_module_device(model.get_submodule(spec.offload_owner), execution_device="cpu"):
# Own the copy independently of Accelerate's temporary module storage.
tensor = model.get_parameter_or_buffer(name).detach().to("cpu", copy=True).contiguous()
if (
tensor.device.type != "cpu" or tuple(tensor.shape) != spec.shape
or tensor.dtype != spec.dtype
):
raise ValueError(f"Native export tensor no longer matches plan: {name}")
return tensor
def _verify_shard(path: Path, shard: ExportShard, plan: NativeExportPlan) -> None:
# Header-only verification avoids reading/mapping tensor payloads into RSS.
with safe_open(path, framework="pt", device="cpu") as saved:
if set(saved.keys()) != set(shard.tensor_names):
raise ValueError(f"Native export shard has incorrect keys: {shard.filename}")
for name in shard.tensor_names:
view = saved.get_slice(name)
spec = plan.tensors[name]
if (
tuple(view.get_shape()) != spec.shape
or view.get_dtype() != _SAFE_DTYPES[spec.dtype]
):
raise ValueError(f"Native export shard tensor metadata mismatch: {name}")
def _trim_allocator() -> None:
"""Best-effort reclaim of released glibc arenas between shards."""
if sys.platform != "linux":
return
try:
trim = ctypes.CDLL(None).malloc_trim
trim.argtypes = [ctypes.c_size_t]
trim.restype = ctypes.c_int
trim(0)
except Exception:
return
def write_native_checkpoint(
model: PreTrainedModel,
checkpoint_dir: Path,
plan: NativeExportPlan,
*,
check_cancelled: Callable[[], None] = lambda: None,
host_budget: Callable[[], int] | None = None,
staged_bytes: Callable[[int], None] | None = None,
on_shard_finalized: Callable[[Path, str], None] | None = None,
continuation: ExportContinuation | None = None,
on_shard_progress: Callable[[ExportProgress], None] | None = None,
) -> None:
"""Write verified shards inside a caller-owned atomic staging directory.
``staged_bytes`` observes live shard payload bytes for deterministic accounting;
the plan additionally accounts for transient loader and serialization copies.
``on_shard_finalized`` receives the finalized staging path and logical filename
after each shard's tensors have been released; callback errors propagate. Its
staging path is invalid after a later rollback or final promotion.
Cancellation/budget checks run before each shard and before final config/index
writes. Atomic destination promotion belongs to atomic_checkpoint_directory.
Optional continuation checks candidate/layout receipts before any write and
verifies restored file content and tensor headers before reuse. Restored
shards do not require a tensor staging budget. Fresh shards retain admission
checks. Progress callbacks receive start, hash, and complete events; callers
may throttle hash events. No callback writes to stdout by default.
"""
if plan.metadata_only:
raise ValueError("config-only native export plan cannot write weights; replan the loaded model")
if on_shard_finalized is not None and not callable(on_shard_finalized):
raise TypeError("on_shard_finalized must be callable or None")
if on_shard_progress is not None and not callable(on_shard_progress):
raise TypeError("on_shard_progress must be callable or None")
plan_sha256, receipts = (
_continuation_receipts(continuation, plan) if continuation is not None else (None, {})
)
budget = host_budget or host_resources.available_host_bytes
checkpoint_dir = Path(checkpoint_dir)
for index, shard in enumerate(plan.shards, 1):
check_cancelled()
receipt = receipts.get(shard.filename)
def progress(event: str, completed_bytes: int = 0) -> None:
if on_shard_progress is not None:
on_shard_progress(ExportProgress(
event, shard.filename, index, len(plan.shards), shard.size_bytes,
receipt is not None, completed_bytes,
))
progress("start")
target = checkpoint_dir / shard.filename
if receipt is not None:
if target.exists() or target.is_symlink():
raise ValueError(f"Export restoration target already exists: {shard.filename}")
temporary = checkpoint_dir / f".{shard.filename}.restore.tmp"
if temporary.exists() or temporary.is_symlink():
raise ValueError(f"Export restoration temporary path already exists: {shard.filename}")
try:
continuation.restore_shard(receipt, temporary)
if temporary.is_symlink() or not temporary.is_file():
raise ValueError(f"Export restoration requires a regular file: {shard.filename}")
if temporary.stat().st_size != receipt.size_bytes:
raise ValueError(f"Export restored shard size differs: {shard.filename}")
size, digest = _hash_shard(temporary, check_cancelled, lambda count: progress("hash", count))
if size != receipt.size_bytes or digest != receipt.sha256:
raise ValueError(f"Export restored shard hash differs: {shard.filename}")
_verify_shard(temporary, shard, plan)
check_cancelled()
os.replace(temporary, target)
finally:
if not temporary.is_dir() or temporary.is_symlink():
temporary.unlink(missing_ok=True)
if continuation.record_shard is not None:
continuation.record_shard(target, receipt)
if on_shard_finalized is not None:
on_shard_finalized(target, shard.filename)
progress("complete", receipt.size_bytes)
continue
available = budget()
if available < shard.host_demand_bytes:
raise NativeExportCapacityError(
f"Native export shard {shard.filename} needs {shard.host_demand_bytes} host bytes; "
f"only {available} available. Reduce max_shard_size or free host memory.",
)
temporary = checkpoint_dir / f".{shard.filename}.tmp"
tensors = {}
try:
live_bytes = 0
for name in shard.tensor_names:
tensors[name] = _materialize_tensor(model, name, plan.tensors[name])
live_bytes += plan.tensors[name].size_bytes
if staged_bytes is not None:
staged_bytes(live_bytes)
save_file(tensors, temporary, metadata={"format": "pt"})
_verify_shard(temporary, shard, plan)
os.replace(temporary, target)
finally:
tensors.clear()
gc.collect()
_trim_allocator()
if staged_bytes is not None:
staged_bytes(0)
temporary.unlink(missing_ok=True)
size = target.stat().st_size if on_shard_progress is not None else 0
if continuation is not None:
size, digest = _hash_shard(target, check_cancelled, lambda count: progress("hash", count))
written_receipt = ExportShardReceipt(
continuation.candidate_sha256, plan_sha256, shard.filename, size, digest,
)
if continuation.record_shard is not None:
continuation.record_shard(target, written_receipt)
if on_shard_finalized is not None:
on_shard_finalized(target, shard.filename)
progress("complete", size)
check_cancelled()
if plan.index is not None:
(checkpoint_dir / "model.safetensors.index.json").write_text(
json.dumps(plan.index, indent=2, sort_keys=True) + "\n", encoding="utf-8",
)
model.config.dtype = str(model.dtype).split(".")[1]
model.config.architectures = [model.__class__.__name__.removeprefix("FSDP")]
model.config.save_pretrained(checkpoint_dir)
if model.can_generate():
model.generation_config.save_pretrained(checkpoint_dir)