mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Keep allocation failures terminal through quantized experts, adapter selection, Bayesian evaluation and refinement helpers. Route folded-writer errors and warnings promoted to errors through public cleanup before verification or export. Exercise exact exception causes, failed-operation counts, rollback, cleanup and no-export behavior across recipe variants. Fixes #389.
1295 lines
52 KiB
Python
1295 lines
52 KiB
Python
"""LoRA-based ablation with optional exact in-memory restoration.
|
|
|
|
Computes low-rank LoRA adapters for forward-time application or weight merging.
|
|
This provides:
|
|
|
|
1. **Unmerging**: Subtract updates approximately, or restore requested snapshots exactly
|
|
2. **Composability**: Multiple ablation adapters can be stacked/blended
|
|
3. **PEFT compatibility**: Exact-identity exports use standard HuggingFace PEFT
|
|
|
|
Inspired by Heretic (p-e-w, 2025) which pioneered LoRA-mediated ablation.
|
|
OBLITERATUS extends this with:
|
|
- Multi-direction rank-k adapters (not just rank-1)
|
|
- MoE-aware LoRA targeting (router + expert-specific adapters)
|
|
- Integration with EGA per-expert directions
|
|
- CoT-aware adapter strength modulation
|
|
|
|
The mathematical equivalence to in-place projection depends on weight orientation:
|
|
|
|
For W of shape (out, hidden) where d is in the hidden dimension:
|
|
In-place: W' = W - scale * W @ d @ d^T
|
|
LoRA: W' = W + B @ A where B = -scale * (W @ d), A = d^T
|
|
|
|
For W of shape (hidden, out) (e.g., Conv1D layers):
|
|
In-place: W' = W - scale * d @ d^T @ W
|
|
LoRA: W' = W + B @ A where B = -scale * d, A = d^T @ W
|
|
|
|
Both represent the same update up to floating-point rounding. LoRA stores
|
|
{B, A} separately; merging alone does not preserve the original weights exactly.
|
|
|
|
References:
|
|
- Hu et al. (2022): LoRA: Low-Rank Adaptation of Large Language Models
|
|
- Heretic (p-e-w, 2025): LoRA-mediated directional ablation
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import math
|
|
import os
|
|
import re
|
|
from dataclasses import dataclass
|
|
from hashlib import sha256
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING, Any, Callable
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
from safetensors.torch import load_file, save_file
|
|
|
|
from obliteratus.analysis.numerical_contracts import all_finite, validated_weight_update
|
|
from obliteratus.checkpoint_provenance import (
|
|
AdapterIdentity,
|
|
ArtifactIdentity,
|
|
ProvenanceRecord,
|
|
verify_provenance_record,
|
|
)
|
|
from obliteratus.persistence_contracts import atomic_checkpoint_directory
|
|
|
|
if TYPE_CHECKING:
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class UnmergedLoraAdapters:
|
|
"""Forward-time PEFT-oriented (B, A) pairs; base parameters are never written.
|
|
|
|
Unlike merge/subtract, removing these hooks is bit-exact. Both Linear and
|
|
transformers Conv1D use the same (out, rank), (rank, in) adapter geometry.
|
|
Embeddings use (hidden, rank), (rank, vocabulary) pairs.
|
|
"""
|
|
|
|
def __init__(self, model, adapters, *, positions="all_tokens"):
|
|
from transformers.pytorch_utils import Conv1D
|
|
|
|
if positions not in {"all_tokens", "final_token"}:
|
|
raise ValueError("Unsupported adapter positions")
|
|
modules = dict(model.named_modules())
|
|
targets = []
|
|
self.adapters = {}
|
|
self._hooks = []
|
|
self._conv1d = set()
|
|
self._embeddings = set()
|
|
self._embedding_scales = {}
|
|
self.positions = positions
|
|
for name, (b, a) in adapters.items():
|
|
module = modules.get(name)
|
|
if not isinstance(module, (nn.Linear, Conv1D, nn.Embedding)):
|
|
raise ValueError(f"Unsupported LoRA target: {name}")
|
|
weight = module.weight
|
|
if weight.device.type == "meta":
|
|
from obliteratus.models.offload_surgery import resolve_logical_parameter
|
|
|
|
weight = resolve_logical_parameter(module, search_roots=(model,)).tensor
|
|
shape = (
|
|
tuple(reversed(weight.shape))
|
|
if isinstance(module, (Conv1D, nn.Embedding)) else weight.shape
|
|
)
|
|
if (not weight.is_floating_point()
|
|
or b.ndim != 2 or a.ndim != 2 or b.shape[1] != a.shape[0]
|
|
or (b.shape[0], a.shape[1]) != tuple(shape)
|
|
or not all_finite(b) or not all_finite(a)):
|
|
raise ValueError(f"Invalid LoRA target or pair: {name}")
|
|
validated_weight_update(b, weight, context=f"LoRA B factor for {name}")
|
|
validated_weight_update(a, weight, context=f"LoRA A factor for {name}")
|
|
self.adapters[name] = (b.detach().clone(), a.detach().clone())
|
|
if isinstance(module, Conv1D):
|
|
self._conv1d.add(name)
|
|
if isinstance(module, nn.Embedding):
|
|
if module.max_norm is not None:
|
|
raise ValueError("LoRA embedding targets require max_norm=None")
|
|
self._embeddings.add(name)
|
|
# Recent Gemma 2/3 embeddings scale inside their forward pass.
|
|
scale = torch.as_tensor(getattr(module, "embed_scale", 1.0)).detach().clone()
|
|
if scale.numel() != 1 or not all_finite(scale):
|
|
raise ValueError(f"Invalid embedding output scale: {name}")
|
|
self._embedding_scales[name] = scale
|
|
targets.append((name, module))
|
|
try:
|
|
for name, module in targets:
|
|
self._hooks.append(module.register_forward_hook(
|
|
self._make_hook(name, positions), with_kwargs=True,
|
|
))
|
|
except Exception:
|
|
self.remove()
|
|
raise
|
|
|
|
def _make_hook(self, name, positions):
|
|
def hook(module, args, kwargs, output):
|
|
value = args[0] if args else kwargs.get(
|
|
"input", kwargs.get("x", kwargs.get("input_ids")),
|
|
)
|
|
if not isinstance(value, torch.Tensor):
|
|
self.remove()
|
|
raise ValueError(
|
|
f"LoRA target {name} requires a tensor input via args, input, x, or input_ids"
|
|
)
|
|
embedding = name in self._embeddings
|
|
if positions == "final_token" and value.ndim != (2 if embedding else 3):
|
|
self.remove()
|
|
raise ValueError(
|
|
"LoRA final_token positions require (batch, tokens) embedding indices "
|
|
"or a 3D (batch, tokens, hidden) input"
|
|
)
|
|
b, a = (tensor.to(output.device, output.dtype) for tensor in self.adapters[name])
|
|
# Accelerate moves args inside its wrapped forward; PyTorch hooks
|
|
# can still receive the caller's original input on another device.
|
|
value = value.to(output.device)
|
|
if embedding:
|
|
indices = value[:, -1] if positions == "final_token" else value
|
|
update = torch.nn.functional.embedding(indices, a.T) @ b.T
|
|
update = update * self._embedding_scales[name].to(output.device, output.dtype)
|
|
if positions == "final_token":
|
|
result = output.clone()
|
|
result[:, -1, :] += update
|
|
return result
|
|
return output + update
|
|
if positions == "final_token":
|
|
result = output.clone()
|
|
result[:, -1, :] += (value[:, -1, :] @ a.T) @ b.T
|
|
return result
|
|
return output + (value @ a.T) @ b.T
|
|
return hook
|
|
|
|
def remove(self):
|
|
for hook in self._hooks:
|
|
hook.remove()
|
|
self._hooks.clear()
|
|
|
|
def unmerge(self):
|
|
"""Compatibility spelling: detach an adapter that was never merged."""
|
|
self.remove()
|
|
|
|
def save(self, output_dir, *, base_model_name_or_path, base_model_revision=None):
|
|
"""Export PEFT pairs; scaled embeddings require a scale-aware PEFT loader."""
|
|
if self.positions != "all_tokens":
|
|
raise ValueError("PEFT export requires all_tokens adapters")
|
|
if self._conv1d and len(self._conv1d | self._embeddings) != len(self.adapters):
|
|
raise ValueError("Export requires homogeneous Linear or Conv1D targets")
|
|
rank = max((a.shape[0] for _, a in self.adapters.values()), default=0)
|
|
# Per-layer numerical support can differ. Zero padding preserves B @ A
|
|
# while exporting one PEFT rank and unit alpha/rank scaling.
|
|
adapters = {
|
|
name: (torch.nn.functional.pad(b, (0, rank - a.shape[0])),
|
|
torch.nn.functional.pad(a, (0, 0, 0, rank - a.shape[0])))
|
|
if a.shape[0] != rank else (b, a)
|
|
for name, (b, a) in self.adapters.items()
|
|
}
|
|
return save_peft_adapters_unverified_base(
|
|
adapters, output_dir, base_model_name_or_path=base_model_name_or_path,
|
|
base_model_revision=base_model_revision, fan_in_fan_out=bool(self._conv1d),
|
|
embedding_modules=tuple(sorted(self._embeddings)),
|
|
)
|
|
|
|
# Target module name patterns for LoRA adapter placement
|
|
_LORA_TARGETS = [
|
|
"o_proj", "q_proj", "k_proj", "v_proj",
|
|
"down_proj", "up_proj", "gate_proj",
|
|
"gate", "router",
|
|
]
|
|
|
|
_DIGEST = re.compile(r"^sha256:[0-9a-f]{64}$")
|
|
_IMMUTABLE_REVISION = re.compile(r"^[0-9a-f]{40,64}$")
|
|
_MODULE_PATH = re.compile(r"^[A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)*$")
|
|
_PUBLIC_REPO_ID = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._/-]{0,511}$")
|
|
_PUBLIC_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]{0,511}$")
|
|
_SECRET_VALUE = re.compile(r"(?i)(?:hf_[a-z0-9]{12,}|bearer\s+[a-z0-9._~+/-]{12,})")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BaseModelIdentity:
|
|
"""Exact base and tokenizer identity required for a canonical PEFT claim."""
|
|
|
|
repo_id: str
|
|
revision: str
|
|
weights_digest: str
|
|
tokenizer_digest: str
|
|
vocab_size: int
|
|
architecture: str
|
|
tied_embeddings: bool
|
|
|
|
def __post_init__(self) -> None:
|
|
if (
|
|
not isinstance(self.repo_id, str)
|
|
or not self.repo_id
|
|
or len(self.repo_id) > 512
|
|
or not _PUBLIC_REPO_ID.fullmatch(self.repo_id)
|
|
or Path(self.repo_id).is_absolute()
|
|
or ".." in self.repo_id.split("/")
|
|
or _SECRET_VALUE.search(self.repo_id)
|
|
):
|
|
raise ValueError("base repository identity is invalid")
|
|
if not _IMMUTABLE_REVISION.fullmatch(self.revision):
|
|
raise ValueError("base revision must be an immutable commit")
|
|
for value, field in (
|
|
(self.weights_digest, "base weights digest"),
|
|
(self.tokenizer_digest, "tokenizer digest"),
|
|
):
|
|
if not _DIGEST.fullmatch(value):
|
|
raise ValueError(f"{field} is invalid")
|
|
if type(self.vocab_size) is not int or self.vocab_size <= 0:
|
|
raise ValueError("base vocabulary size is invalid")
|
|
if (
|
|
not isinstance(self.architecture, str)
|
|
or not self.architecture
|
|
or len(self.architecture) > 512
|
|
or not _PUBLIC_NAME.fullmatch(self.architecture)
|
|
or Path(self.architecture).is_absolute()
|
|
or _SECRET_VALUE.search(self.architecture)
|
|
):
|
|
raise ValueError("base architecture is invalid")
|
|
if type(self.tied_embeddings) is not bool:
|
|
raise ValueError("base tied-embedding declaration is invalid")
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"repo_id": self.repo_id,
|
|
"revision": self.revision,
|
|
"weights_digest": self.weights_digest,
|
|
"tokenizer_digest": self.tokenizer_digest,
|
|
"vocab_size": self.vocab_size,
|
|
"architecture": self.architecture,
|
|
"tied_embeddings": self.tied_embeddings,
|
|
}
|
|
|
|
def to_artifact_identity(self) -> ArtifactIdentity:
|
|
return ArtifactIdentity("hub", self.repo_id, self.revision, self.weights_digest)
|
|
|
|
def tokenizer_artifact_identity(self) -> ArtifactIdentity:
|
|
return ArtifactIdentity(
|
|
"hub",
|
|
f"{self.repo_id}#tokenizer",
|
|
self.revision,
|
|
self.tokenizer_digest,
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AdapterArtifact:
|
|
artifact_id: str
|
|
weights_path: Path
|
|
config_path: Path
|
|
provenance_path: Path
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LoraProjectionTarget:
|
|
"""One constructible adapter, selected identically for execution/admission."""
|
|
|
|
layer_index: int
|
|
name: str
|
|
module: nn.Module
|
|
rank: int
|
|
writer_kind: str | None = None
|
|
|
|
|
|
def lora_projection_targets(pipeline, rank: int = 1) -> tuple[LoraProjectionTarget, ...]:
|
|
"""Resolve LoRA's actual projections/ranks without allocating tensor storage.
|
|
|
|
An empty selection means the pipeline takes its ordinary-surgery fallback.
|
|
No embeddings, heads, biases or fused expert tensors are adapter targets.
|
|
"""
|
|
from obliteratus.strategies.utils import (
|
|
get_attention_module, get_ffn_module, get_layer_modules,
|
|
)
|
|
from obliteratus.abliterate import (
|
|
_ATTN_OUT_NAMES, _ATTN_IN_NAMES, _FFN_OUT_NAMES, _FFN_IN_NAMES, _ROUTER_NAMES,
|
|
)
|
|
|
|
if not pipeline.handle or not pipeline._strong_layers:
|
|
return ()
|
|
if type(rank) is not int or rank <= 0:
|
|
raise ValueError("LoRA rank must be a positive integer")
|
|
layers = get_layer_modules(pipeline.handle)
|
|
module_names = {id(module): name for name, module in pipeline.handle.model.named_modules()}
|
|
selected = []
|
|
folding = (getattr(pipeline, "fold_post_norms", True)
|
|
and pipeline.handle.architecture in {"gemma2", "gemma3", "gemma3_text", "olmo2"})
|
|
for idx in pipeline._strong_layers:
|
|
subspace = pipeline.refusal_subspaces.get(idx)
|
|
if subspace is None:
|
|
continue
|
|
n_dirs = min(rank, subspace.shape[0])
|
|
if not n_dirs:
|
|
raise ValueError("LoRA refusal subspace has no directions")
|
|
for label, resolve, candidates in (
|
|
("attn", get_attention_module, _ATTN_OUT_NAMES + _ATTN_IN_NAMES),
|
|
("ffn", get_ffn_module, _FFN_OUT_NAMES + _FFN_IN_NAMES + _ROUTER_NAMES),
|
|
):
|
|
try:
|
|
module = resolve(layers[idx], pipeline.handle.architecture)
|
|
except torch.OutOfMemoryError:
|
|
raise
|
|
except (AttributeError, RuntimeError):
|
|
continue
|
|
for name in candidates:
|
|
kind = "attention" if label == "attn" else "ffn"
|
|
output_names = _ATTN_OUT_NAMES if label == "attn" else _FFN_OUT_NAMES
|
|
if folding:
|
|
target = pipeline.projection_target
|
|
if target not in {"all", "output", kind}:
|
|
continue
|
|
if target == "output" and name not in output_names:
|
|
continue
|
|
proj = getattr(module, name, None)
|
|
weight = getattr(proj, "weight", None)
|
|
if weight is None:
|
|
continue
|
|
if weight.ndim != 2 or subspace.shape[1] not in weight.shape:
|
|
continue
|
|
key = module_names.get(id(proj), f"layer.{idx}.{label}.{name}")
|
|
selected.append(LoraProjectionTarget(
|
|
idx, key, proj, n_dirs, kind if name in output_names else None,
|
|
))
|
|
return tuple(selected)
|
|
|
|
|
|
def compute_lora_adapters(
|
|
pipeline: AbliterationPipeline,
|
|
rank: int = 1,
|
|
) -> dict[str, tuple[torch.Tensor, torch.Tensor]]:
|
|
"""Compute FP16 (B, A) adapter pairs for the shared LoRA target selection.
|
|
|
|
Each matrix is converted to FP32 sequentially; all low-rank pairs survive
|
|
until merge/export. Standard (out, hidden) and transposed (hidden, out)
|
|
weights use the same directional projection as ordinary surgery.
|
|
"""
|
|
if not pipeline.handle or not pipeline._strong_layers:
|
|
return {}
|
|
from obliteratus.models.post_norms import folded_writer_weight
|
|
from obliteratus.strategies.utils import get_layer_modules
|
|
|
|
pipeline._prepare_post_norm_folding(get_layer_modules(pipeline.handle))
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]] = {}
|
|
for target in lora_projection_targets(pipeline, rank):
|
|
idx = target.layer_index
|
|
subspace = pipeline.refusal_subspaces[idx]
|
|
reg = pipeline.regularization
|
|
if pipeline.layer_adaptive_strength and idx in pipeline._layer_excise_weights:
|
|
weight = pipeline._layer_excise_weights[idx]
|
|
reg = pipeline.regularization + (1.0 - weight) * (1.0 - pipeline.regularization) * 0.15
|
|
if pipeline.float_layer_interpolation and idx in pipeline._float_layer_weights:
|
|
float_w = pipeline._float_layer_weights[idx]
|
|
reg = reg + (1.0 - float_w) * (1.0 - reg) * 0.3
|
|
scale = 1.0 - reg
|
|
D = subspace[:target.rank].float()
|
|
W = target.module.weight.data.float()
|
|
layer_scales = pipeline._folded_layer_scales.get(idx, {})
|
|
norm_scale = layer_scales.get(target.writer_kind)
|
|
if layer_scales:
|
|
D = D.to(W.device)
|
|
if norm_scale is not None:
|
|
work, s = folded_writer_weight(target.module, W, norm_scale)
|
|
norms = D.norm(dim=-1, keepdim=True)
|
|
zero = norms < 1e-8
|
|
# Retain zero adapters: legacy LoRA counts them as projections.
|
|
D = D.masked_fill(zero, 0) / norms.masked_fill(zero, 1)
|
|
b = -scale * (D / s).T
|
|
a = D @ work.T
|
|
# Keep the exported/unmerged factors in the same coordinates as W.
|
|
if not isinstance(target.module, nn.Linear):
|
|
b, a = a.T, b.T
|
|
b = validated_weight_update(b, target.module.weight, context="folded LoRA factors")
|
|
a = validated_weight_update(a, target.module.weight, context="folded LoRA factors")
|
|
_validate_lora_merge(target.module.weight.data, b, a, target.name)
|
|
adapters[target.name] = (b, a)
|
|
continue
|
|
lora_B_parts, lora_A_parts = [], []
|
|
for di in range(target.rank):
|
|
d = D[di]
|
|
if W.shape[-1] == D.shape[1]:
|
|
coeff = (W @ d.unsqueeze(-1)).squeeze(-1)
|
|
lora_B_parts.append(-scale * coeff.unsqueeze(1))
|
|
lora_A_parts.append(d.unsqueeze(0))
|
|
else:
|
|
coeff = d @ W
|
|
lora_B_parts.append(-scale * d.unsqueeze(1))
|
|
lora_A_parts.append(coeff.unsqueeze(0))
|
|
lora_B = torch.cat(lora_B_parts, dim=1)
|
|
lora_A = torch.cat(lora_A_parts, dim=0)
|
|
adapters[target.name] = (lora_B.half(), lora_A.half())
|
|
pipeline.log(f"Computed {len(adapters)} LoRA adapter pairs (rank={rank})")
|
|
return adapters
|
|
|
|
|
|
def _validate_lora_merge(weight, b, a, name, *, undo=False):
|
|
"""Return the checked storage value for a merge or approximate unmerge."""
|
|
with torch.no_grad():
|
|
delta = validated_weight_update(b @ a, weight, context=f"LoRA delta for {name}")
|
|
delta = delta.to(device=weight.device)
|
|
# The product is private workspace. Reuse it for the checked sum so
|
|
# preflight and commit have the same matrix lifetime as in-place merge.
|
|
if undo:
|
|
torch.sub(weight, delta, out=delta)
|
|
else:
|
|
torch.add(weight, delta, out=delta)
|
|
return validated_weight_update(
|
|
delta, weight,
|
|
context=f"LoRA {'unmerged' if undo else 'merged'} weight for {name}",
|
|
)
|
|
|
|
|
|
def apply_lora_adapters(
|
|
pipeline: AbliterationPipeline | nn.Module,
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
*,
|
|
keep_originals: bool = False,
|
|
) -> dict[str, torch.Tensor]:
|
|
"""Apply pre-computed LoRA adapters by modifying weights in-place.
|
|
|
|
This is equivalent to merging the LoRA into the base model.
|
|
Pass ``keep_originals=True`` to return CPU snapshots of touched weights
|
|
for bit-exact restoration with ``unmerge_lora_adapters``. This is opt-in
|
|
because snapshots cost one full copy of each touched weight; the default
|
|
only retains the low-rank adapters on a pipeline. Without snapshots,
|
|
unmerging is approximate (see its documented rounding bound).
|
|
|
|
Accepts a pipeline or an nn.Module. Meta/offloaded and quantized weights
|
|
are skipped, as are targets with incompatible shapes. Nonfinite deltas or
|
|
merged weights in the target dtype raise before any weight is written.
|
|
"""
|
|
originals: dict[str, torch.Tensor] = {}
|
|
applied = _update_lora_adapters(
|
|
pipeline, adapters, originals=originals if keep_originals else None,
|
|
)
|
|
if not isinstance(pipeline, nn.Module):
|
|
pipeline._lora_adapters = adapters
|
|
pipeline.log(f"Applied {applied} LoRA adapters (merged into weights)")
|
|
return originals
|
|
|
|
|
|
def unmerge_lora_adapters(
|
|
pipeline: AbliterationPipeline | nn.Module,
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
*,
|
|
original_weights: dict[str, torch.Tensor] | None = None,
|
|
) -> None:
|
|
"""Undo one merge on the same model with unchanged adapters and weights.
|
|
|
|
Snapshots returned by ``apply_lora_adapters(keep_originals=True)`` restore
|
|
bit-exactly, provided weight shapes and dtypes have not changed. Otherwise
|
|
recompute ``(B @ A).to(weight.device, weight.dtype)`` exactly as in apply
|
|
and subtract it. Use the same adapter device/dtype and matmul settings.
|
|
Subtraction avoids full-weight snapshots but is not bit-exact: for finite
|
|
values, ignoring underflow, absolute error is bounded approximately by
|
|
``2 * torch.finfo(dtype).eps * (abs(original) + abs(delta))``. Cancellation
|
|
can cause large relative error near zero, particularly in fp16/bf16.
|
|
This reverses only the LoRA merge, not other pipeline weight surgery.
|
|
|
|
Missing targets and shape mismatches are skipped as in apply; unsupported
|
|
meta/offloaded or quantized weights are skipped. Snapshot shape/dtype
|
|
mismatches raise before any update.
|
|
Call once per merge; this is not an idempotent adapter-disable operation.
|
|
"""
|
|
applied = _update_lora_adapters(pipeline, adapters, undo=True, originals=original_weights)
|
|
if not isinstance(pipeline, nn.Module):
|
|
pipeline._lora_adapters = {}
|
|
pipeline.log(f"Unmerged {applied} LoRA adapters")
|
|
|
|
|
|
def _update_lora_adapters(
|
|
pipeline: AbliterationPipeline | nn.Module,
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
*,
|
|
undo: bool = False,
|
|
originals: dict[str, torch.Tensor] | None = None,
|
|
) -> int:
|
|
from obliteratus.strategies.utils import (
|
|
get_attention_module,
|
|
get_ffn_module,
|
|
get_layer_modules,
|
|
)
|
|
|
|
if isinstance(pipeline, nn.Module):
|
|
model = pipeline
|
|
elif pipeline.handle:
|
|
model = pipeline.handle.model
|
|
else:
|
|
return 0
|
|
named_modules = dict(model.named_modules())
|
|
targets = []
|
|
skipped = 0
|
|
|
|
for key, (lora_B, lora_A) in adapters.items():
|
|
proj = named_modules.get(key)
|
|
if proj is None:
|
|
parts = key.split(".")
|
|
if (
|
|
isinstance(pipeline, nn.Module)
|
|
or len(parts) != 4 or parts[0] != "layer" or not parts[1].isdigit()
|
|
):
|
|
continue
|
|
_, idx_str, module_label, weight_name = parts
|
|
idx = int(idx_str)
|
|
try:
|
|
layers = get_layer_modules(pipeline.handle)
|
|
arch = pipeline.handle.architecture
|
|
if module_label == "attn":
|
|
module = get_attention_module(layers[idx], arch)
|
|
else:
|
|
module = get_ffn_module(layers[idx], arch)
|
|
except torch.OutOfMemoryError:
|
|
raise
|
|
except (AttributeError, RuntimeError, IndexError):
|
|
continue
|
|
proj = getattr(module, weight_name, None)
|
|
if proj is None or not hasattr(proj, "weight"):
|
|
continue
|
|
|
|
W = proj.weight.data
|
|
if undo and originals is not None and key in originals:
|
|
original = originals[key]
|
|
if original.shape != W.shape or original.dtype != W.dtype:
|
|
raise ValueError("LoRA original weight shape/dtype changed")
|
|
if (
|
|
W.is_meta or W.is_quantized or not W.is_floating_point()
|
|
or type(proj.weight).__name__ in {"Params4bit", "Int8Params"}
|
|
or W.dtype in {torch.float8_e4m3fn, torch.float8_e5m2}
|
|
or (lora_B.shape[0], lora_A.shape[1]) != W.shape
|
|
):
|
|
skipped += 1
|
|
continue
|
|
if undo and originals is not None and key not in originals:
|
|
continue
|
|
targets.append((key, W, lora_B, lora_A))
|
|
|
|
if skipped:
|
|
message = f"Warning: skipped {skipped} LoRA modules with unsupported weights or shapes"
|
|
if isinstance(pipeline, nn.Module):
|
|
logger.warning(message)
|
|
else:
|
|
pipeline.log(message)
|
|
|
|
with torch.no_grad():
|
|
ordered = list(reversed(targets)) if undo else targets
|
|
# Tied targets must preflight their cumulative update in application order.
|
|
groups = {}
|
|
for entry in ordered:
|
|
W = entry[1]
|
|
groups.setdefault((W.device, W.data_ptr()), []).append(entry)
|
|
for group in groups.values():
|
|
base = group[0][1]
|
|
for key, W, lora_B, lora_A in group:
|
|
if undo and originals is not None:
|
|
base = validated_weight_update(originals[key], W, context=f"LoRA snapshot {key}")
|
|
else:
|
|
base = _validate_lora_merge(base, lora_B, lora_A, key, undo=undo)
|
|
del base
|
|
if not undo and originals is not None:
|
|
for key, W, _, _ in targets:
|
|
originals[key] = W.detach().to(device="cpu", copy=True)
|
|
for key, W, lora_B, lora_A in ordered:
|
|
if undo and originals is not None:
|
|
updated = validated_weight_update(originals[key], W, context=f"LoRA snapshot {key}")
|
|
else:
|
|
updated = _validate_lora_merge(W, lora_B, lora_A, key, undo=undo)
|
|
W.copy_(updated.to(device=W.device))
|
|
del updated
|
|
return len(targets)
|
|
|
|
|
|
def _json_bytes(value: object) -> bytes:
|
|
return (
|
|
json.dumps(value, indent=2, sort_keys=True, ensure_ascii=True, allow_nan=False) + "\n"
|
|
).encode("utf-8")
|
|
|
|
|
|
def _write_new(path: Path, payload: bytes) -> None:
|
|
with path.open("xb") as stream:
|
|
stream.write(payload)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
|
|
|
|
def _digest_bytes(payload: bytes) -> str:
|
|
return f"sha256:{sha256(payload).hexdigest()}"
|
|
|
|
|
|
def _digest_file(path: Path) -> str:
|
|
digest = sha256()
|
|
with path.open("rb") as stream:
|
|
for chunk in iter(lambda: stream.read(1 << 20), b""):
|
|
digest.update(chunk)
|
|
return f"sha256:{digest.hexdigest()}"
|
|
|
|
|
|
def _prepare_adapter_state(
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
*,
|
|
lora_alpha: int | None,
|
|
embedding_modules: tuple[str, ...] = (),
|
|
) -> tuple[dict[str, torch.Tensor], list[dict[str, Any]], int, int, float]:
|
|
if not adapters:
|
|
raise ValueError("canonical PEFT export requires at least one adapter")
|
|
if set(embedding_modules) - adapters.keys():
|
|
raise ValueError("embedding modules must refer to supplied adapters")
|
|
ranks = set()
|
|
for module_name, pair in adapters.items():
|
|
if not _MODULE_PATH.fullmatch(module_name):
|
|
raise ValueError(f"adapter module path is invalid: {module_name!r}")
|
|
if not isinstance(pair, tuple) or len(pair) != 2:
|
|
raise ValueError(f"adapter pair is invalid: {module_name}")
|
|
lora_b, lora_a = pair
|
|
if (
|
|
not isinstance(lora_a, torch.Tensor)
|
|
or not isinstance(lora_b, torch.Tensor)
|
|
or lora_a.ndim != 2
|
|
or lora_b.ndim != 2
|
|
or lora_b.shape[1] != lora_a.shape[0]
|
|
or lora_a.dtype != lora_b.dtype
|
|
):
|
|
raise ValueError(f"adapter tensor geometry is invalid: {module_name}")
|
|
ranks.add(int(lora_a.shape[0]))
|
|
if len(ranks) != 1 or next(iter(ranks)) <= 0:
|
|
raise ValueError("canonical PEFT export requires one positive rank")
|
|
rank = next(iter(ranks))
|
|
alpha = rank if lora_alpha is None else lora_alpha
|
|
if type(alpha) is not int or alpha <= 0:
|
|
raise ValueError("LoRA alpha must be a positive integer")
|
|
scaling = alpha / rank
|
|
state: dict[str, torch.Tensor] = {}
|
|
key_map: list[dict[str, Any]] = []
|
|
for module_name in sorted(adapters):
|
|
lora_b, lora_a = adapters[module_name]
|
|
if module_name in embedding_modules:
|
|
a_key = f"base_model.model.{module_name}.lora_embedding_A"
|
|
b_key = f"base_model.model.{module_name}.lora_embedding_B"
|
|
else:
|
|
a_key = f"base_model.model.{module_name}.lora_A.weight"
|
|
b_key = f"base_model.model.{module_name}.lora_B.weight"
|
|
state[a_key] = lora_a.detach().cpu().contiguous()
|
|
state[b_key] = lora_b.detach().cpu().contiguous() / scaling
|
|
key_map.append(
|
|
{
|
|
"module_name": module_name,
|
|
"target_module": module_name.rsplit(".", 1)[-1],
|
|
"lora_A_key": a_key,
|
|
"lora_B_key": b_key,
|
|
"lora_A_shape": list(lora_a.shape),
|
|
"lora_B_shape": list(lora_b.shape),
|
|
"rank": rank,
|
|
}
|
|
)
|
|
return state, key_map, rank, alpha, scaling
|
|
|
|
|
|
def _peft_adapter_config(
|
|
key_map: list[dict[str, Any]],
|
|
rank: int,
|
|
alpha: int,
|
|
*,
|
|
base_model_name_or_path: str,
|
|
base_model_revision: str | None,
|
|
fan_in_fan_out: bool = False,
|
|
lora_dropout: float = 0.0,
|
|
) -> dict[str, Any]:
|
|
return {
|
|
"base_model_name_or_path": base_model_name_or_path,
|
|
"bias": "none",
|
|
"fan_in_fan_out": fan_in_fan_out,
|
|
"inference_mode": True,
|
|
"init_lora_weights": True,
|
|
"lora_alpha": alpha,
|
|
"lora_dropout": float(lora_dropout),
|
|
"modules_to_save": None,
|
|
"peft_type": "LORA",
|
|
"r": rank,
|
|
"revision": base_model_revision,
|
|
# Full paths prevent PEFT from creating unsupplied adapters on every
|
|
# module that happens to share a leaf name such as ``q_proj``.
|
|
"target_modules": sorted(item["module_name"] for item in key_map),
|
|
"task_type": "CAUSAL_LM",
|
|
}
|
|
|
|
|
|
def save_lora_adapters(
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
output_dir: str | Path,
|
|
*,
|
|
base_model: BaseModelIdentity,
|
|
provenance_factory: Callable[[tuple[str, ...], AdapterIdentity], ProvenanceRecord],
|
|
lora_alpha: int | None = None,
|
|
lora_dropout: float = 0.0,
|
|
modules_to_save: tuple[str, ...] = (),
|
|
adapter_name: str = "default",
|
|
) -> AdapterArtifact:
|
|
"""Atomically write a standard PEFT LoRA artifact with exact provenance."""
|
|
if not isinstance(base_model, BaseModelIdentity):
|
|
raise ValueError("canonical PEFT export requires an exact base model identity")
|
|
if (
|
|
isinstance(lora_dropout, bool)
|
|
or not isinstance(lora_dropout, (int, float))
|
|
or not math.isfinite(lora_dropout)
|
|
or not 0 <= lora_dropout < 1
|
|
):
|
|
raise ValueError("LoRA dropout must be in [0, 1)")
|
|
if modules_to_save:
|
|
raise ValueError("modules_to_save is unsupported unless its tensors are supplied")
|
|
if (
|
|
not isinstance(adapter_name, str)
|
|
or not adapter_name
|
|
or len(adapter_name) > 512
|
|
or not _PUBLIC_NAME.fullmatch(adapter_name)
|
|
or Path(adapter_name).is_absolute()
|
|
or _SECRET_VALUE.search(adapter_name)
|
|
):
|
|
raise ValueError("adapter name is invalid")
|
|
state, key_map, rank, alpha, scaling = _prepare_adapter_state(
|
|
adapters,
|
|
lora_alpha=lora_alpha,
|
|
)
|
|
output_path = Path(output_dir)
|
|
if (
|
|
output_path.is_symlink()
|
|
or output_path.absolute() != output_path.resolve(strict=False)
|
|
):
|
|
raise ValueError("adapter output directory must not be a symlink")
|
|
if output_path.exists() and (not output_path.is_dir() or any(output_path.iterdir())):
|
|
raise FileExistsError("adapter output directory must be absent or empty")
|
|
config = _peft_adapter_config(
|
|
key_map, rank, alpha,
|
|
base_model_name_or_path=base_model.repo_id,
|
|
base_model_revision=base_model.revision,
|
|
lora_dropout=lora_dropout,
|
|
)
|
|
config_payload = _json_bytes(config)
|
|
key_map_digest = _digest_bytes(
|
|
json.dumps(key_map, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
|
)
|
|
adapter_identity = AdapterIdentity(
|
|
adapter_type="lora",
|
|
base_model=base_model.to_artifact_identity(),
|
|
config_digest=_digest_bytes(config_payload),
|
|
key_map_digest=key_map_digest,
|
|
)
|
|
card = (
|
|
"---\n"
|
|
f"base_model: {base_model.repo_id}\n"
|
|
f"base_model_revision: {base_model.revision}\n"
|
|
"library_name: peft\n"
|
|
"tags:\n - peft\n - lora\n - obliteratus\n"
|
|
"---\n\n"
|
|
"# OBLITERATUS LoRA adapter\n\n"
|
|
"This is an unmerged PEFT LoRA artifact representing an OBLITERATUS surgery event.\n\n"
|
|
f"- Exact base: `{base_model.repo_id}@{base_model.revision}`\n"
|
|
f"- Base weights: `{base_model.weights_digest}`\n"
|
|
f"- Tokenizer: `{base_model.tokenizer_digest}`\n"
|
|
"- Scaling: standard PEFT `alpha / rank`; saved B values preserve the exact delta.\n"
|
|
).encode("utf-8")
|
|
manifest = {
|
|
"schema_id": "obliteratus.peft-adapter-manifest",
|
|
"schema_version": "1.0.0",
|
|
"adapter_name": adapter_name,
|
|
"adapter_type": "lora",
|
|
"adapter_format_version": "peft-lora-v1",
|
|
"base_model": base_model.to_dict(),
|
|
"rank": rank,
|
|
"alpha": alpha,
|
|
"scaling": scaling,
|
|
"dropout": float(lora_dropout),
|
|
"bias": "none",
|
|
"modules_to_save": [],
|
|
"target_modules": config["target_modules"],
|
|
"tie_policy": "base_model_declared",
|
|
"merged": False,
|
|
"key_map_digest": key_map_digest,
|
|
"model_card_digest": _digest_bytes(card),
|
|
"key_map": key_map,
|
|
}
|
|
manifest_payload = _json_bytes(manifest)
|
|
artifact_id = ""
|
|
|
|
def validate_staging(staging: Path) -> None:
|
|
expected = {
|
|
"README.md",
|
|
"adapter_config.json",
|
|
"adapter_manifest.json",
|
|
"adapter_model.safetensors",
|
|
"checkpoint-provenance.json",
|
|
}
|
|
paths = tuple(staging.iterdir())
|
|
if {path.name for path in paths} != expected or any(
|
|
path.is_symlink() or not path.is_file() for path in paths
|
|
):
|
|
raise ValueError("adapter artifact set is incomplete")
|
|
loaded = load_file(staging / "adapter_model.safetensors", device="cpu")
|
|
if set(loaded) != set(state):
|
|
raise ValueError("adapter tensor set does not match the manifest")
|
|
validate_adapter_base(staging, base_model)
|
|
|
|
with atomic_checkpoint_directory(output_path, validate=validate_staging) as staging:
|
|
weights_path = staging / "adapter_model.safetensors"
|
|
config_path = staging / "adapter_config.json"
|
|
manifest_path = staging / "adapter_manifest.json"
|
|
provenance_path = staging / "checkpoint-provenance.json"
|
|
card_path = staging / "README.md"
|
|
save_file(dict(sorted(state.items())), weights_path)
|
|
_write_new(config_path, config_payload)
|
|
_write_new(manifest_path, manifest_payload)
|
|
_write_new(card_path, card)
|
|
output_digests = tuple(
|
|
sorted(
|
|
(
|
|
_digest_file(weights_path),
|
|
_digest_file(config_path),
|
|
_digest_file(manifest_path),
|
|
_digest_file(card_path),
|
|
)
|
|
)
|
|
)
|
|
provenance = provenance_factory(output_digests, adapter_identity)
|
|
provenance_record = provenance.to_dict()
|
|
if (
|
|
provenance_record.get("base_model") != base_model.to_artifact_identity().to_dict()
|
|
or provenance_record.get("adapter") != adapter_identity.to_dict()
|
|
or sorted(provenance_record.get("output_digests", [])) != list(output_digests)
|
|
):
|
|
raise ValueError("adapter provenance does not match the exact exported artifact")
|
|
artifact_id = provenance.artifact_id
|
|
_write_new(provenance_path, provenance.to_json().encode("utf-8"))
|
|
return AdapterArtifact(
|
|
artifact_id=artifact_id,
|
|
weights_path=output_path / "adapter_model.safetensors",
|
|
config_path=output_path / "adapter_config.json",
|
|
provenance_path=output_path / "checkpoint-provenance.json",
|
|
)
|
|
|
|
|
|
def save_peft_adapters_unverified_base(
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
output_dir: str | Path,
|
|
*,
|
|
base_model_name_or_path: str,
|
|
base_model_revision: str | None,
|
|
fan_in_fan_out: bool = False,
|
|
lora_alpha: int | None = None,
|
|
lora_dropout: float = 0.0,
|
|
embedding_modules: tuple[str, ...] = (),
|
|
) -> Path:
|
|
"""Atomically export PEFT files without claiming an exact base identity.
|
|
|
|
Load onto the original base, never the already merged pipeline output.
|
|
Pairs use PEFT's (out, rank) B and (rank, in) A orientation; Conv1D
|
|
targets also require ``fan_in_fan_out=True``.
|
|
Embedding pairs use (hidden, rank) B and (rank, vocabulary) A and must
|
|
be listed in ``embedding_modules`` for PEFT's ParameterDict key format.
|
|
"""
|
|
if (
|
|
not isinstance(base_model_name_or_path, str)
|
|
or not base_model_name_or_path
|
|
or _SECRET_VALUE.search(base_model_name_or_path)
|
|
):
|
|
raise ValueError("PEFT export requires a base model name or path")
|
|
if (
|
|
isinstance(lora_dropout, bool)
|
|
or not isinstance(lora_dropout, (int, float))
|
|
or not math.isfinite(lora_dropout)
|
|
or not 0 <= lora_dropout < 1
|
|
):
|
|
raise ValueError("LoRA dropout must be in [0, 1)")
|
|
state, key_map, rank, alpha, scaling = _prepare_adapter_state(
|
|
adapters, lora_alpha=lora_alpha, embedding_modules=embedding_modules,
|
|
)
|
|
output_path = Path(output_dir)
|
|
if (
|
|
output_path.is_symlink()
|
|
or output_path.absolute() != output_path.resolve(strict=False)
|
|
):
|
|
raise ValueError("adapter output directory must not be a symlink")
|
|
if output_path.exists() and (not output_path.is_dir() or any(output_path.iterdir())):
|
|
raise FileExistsError("adapter output directory must be absent or empty")
|
|
config_payload = _json_bytes(_peft_adapter_config(
|
|
key_map, rank, alpha,
|
|
base_model_name_or_path=base_model_name_or_path,
|
|
base_model_revision=base_model_revision,
|
|
fan_in_fan_out=fan_in_fan_out,
|
|
lora_dropout=lora_dropout,
|
|
))
|
|
|
|
def validate_staging(staging: Path) -> None:
|
|
loaded = load_file(staging / "adapter_model.safetensors", device="cpu")
|
|
if set(loaded) != set(state) or any(
|
|
not torch.equal(loaded[key], value) for key, value in state.items()
|
|
):
|
|
raise ValueError("adapter tensor set does not match the export")
|
|
if (staging / "adapter_config.json").read_bytes() != config_payload:
|
|
raise ValueError("adapter configuration does not match the export")
|
|
|
|
with atomic_checkpoint_directory(output_path, validate=validate_staging) as staging:
|
|
save_file(dict(sorted(state.items())), staging / "adapter_model.safetensors")
|
|
_write_new(staging / "adapter_config.json", config_payload)
|
|
_write_new(
|
|
staging / "adapter_manifest.json",
|
|
_json_bytes({
|
|
"schema_id": "obliteratus.peft-adapter-export",
|
|
"schema_version": "1.0.0",
|
|
"base_identity_verified": False,
|
|
"merged": False,
|
|
"scaling": scaling,
|
|
"key_map": key_map,
|
|
}),
|
|
)
|
|
card = (
|
|
b"# OBLITERATUS LoRA adapter\n\n"
|
|
b"Unmerged PEFT adapter. Exact base identity was not retained by this run.\n"
|
|
b"Load onto the original base model, not the merged output checkpoint.\n"
|
|
)
|
|
if embedding_modules:
|
|
card += (
|
|
b"\nScaled embedding modules (including recent Gemma 2/3 implementations) "
|
|
b"require PEFT >=0.18.0 with embedding-scale support "
|
|
b"(Embedding._get_embed_scale). Older PEFT versions may produce incorrect "
|
|
b"outputs for these adapters.\n"
|
|
b"On bases with tied input/output embeddings, keep these adapters unmerged: "
|
|
b"merging may change the shared output head or apply its update twice.\n"
|
|
)
|
|
_write_new(staging / "README.md", card)
|
|
return output_path
|
|
|
|
|
|
def _read_json_object(path: Path, detail: str) -> dict[str, Any]:
|
|
if path.is_symlink() or not path.is_file() or path.stat().st_size > 8 << 20:
|
|
raise ValueError(detail)
|
|
try:
|
|
value = json.loads(
|
|
path.read_text(encoding="utf-8"),
|
|
object_pairs_hook=_reject_duplicate_json_pairs,
|
|
)
|
|
except (UnicodeError, json.JSONDecodeError, ValueError) as error:
|
|
raise ValueError(detail) from error
|
|
if not isinstance(value, dict):
|
|
raise ValueError(detail)
|
|
return value
|
|
|
|
|
|
def _reject_duplicate_json_pairs(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
|
|
result: dict[str, Any] = {}
|
|
for key, value in pairs:
|
|
if key in result:
|
|
raise ValueError(f"duplicate JSON key: {key}")
|
|
result[key] = value
|
|
return result
|
|
|
|
|
|
def _canonical_object_digest(value: object) -> str:
|
|
payload = json.dumps(
|
|
value,
|
|
sort_keys=True,
|
|
separators=(",", ":"),
|
|
ensure_ascii=True,
|
|
allow_nan=False,
|
|
).encode("utf-8")
|
|
return _digest_bytes(payload)
|
|
|
|
|
|
def _validate_adapter_integrity(
|
|
root: Path,
|
|
config: dict[str, Any],
|
|
manifest: dict[str, Any],
|
|
provenance: dict[str, Any],
|
|
) -> None:
|
|
weights = root / "adapter_model.safetensors"
|
|
artifact_paths = (
|
|
root / "README.md",
|
|
root / "adapter_config.json",
|
|
root / "adapter_manifest.json",
|
|
weights,
|
|
root / "checkpoint-provenance.json",
|
|
)
|
|
if any(path.is_symlink() or not path.is_file() for path in artifact_paths):
|
|
raise ValueError("adapter_artifact_file_invalid")
|
|
if weights.stat().st_size == 0:
|
|
raise ValueError("adapter_weights_invalid")
|
|
try:
|
|
verified_provenance = verify_provenance_record(provenance)
|
|
except (TypeError, ValueError) as error:
|
|
if "record digest mismatch" in str(error):
|
|
raise ValueError("adapter_provenance_digest_mismatch") from error
|
|
if "artifact ID mismatch" in str(error):
|
|
raise ValueError("adapter_artifact_identity_mismatch") from error
|
|
raise ValueError("adapter_provenance_invalid") from error
|
|
if verified_provenance != provenance:
|
|
raise ValueError("adapter_provenance_invalid")
|
|
expected_outputs = provenance.get("output_digests")
|
|
if (
|
|
not isinstance(expected_outputs, list)
|
|
or any(
|
|
not isinstance(value, str) or not _DIGEST.fullmatch(value) for value in expected_outputs
|
|
)
|
|
or sorted(expected_outputs)
|
|
!= sorted(
|
|
_digest_file(path)
|
|
for path in (
|
|
root / "README.md",
|
|
root / "adapter_config.json",
|
|
root / "adapter_manifest.json",
|
|
weights,
|
|
)
|
|
)
|
|
):
|
|
raise ValueError("adapter_artifact_digest_mismatch")
|
|
record_digest = provenance.get("record_digest")
|
|
record_core = {key: value for key, value in provenance.items() if key != "record_digest"}
|
|
if record_digest != _canonical_object_digest(record_core):
|
|
raise ValueError("adapter_provenance_digest_mismatch")
|
|
artifact_id = provenance.get("artifact_id")
|
|
identity_core = {key: value for key, value in record_core.items() if key != "artifact_id"}
|
|
expected_artifact_id = _canonical_object_digest(identity_core).replace(
|
|
"sha256:",
|
|
"artifact-sha256:",
|
|
1,
|
|
)
|
|
if artifact_id != expected_artifact_id:
|
|
raise ValueError("adapter_artifact_identity_mismatch")
|
|
key_map = manifest.get("key_map")
|
|
adapter = provenance.get("adapter")
|
|
manifest_base = manifest.get("base_model")
|
|
if (
|
|
not isinstance(key_map, list)
|
|
or not isinstance(adapter, dict)
|
|
or not isinstance(manifest_base, dict)
|
|
):
|
|
raise ValueError("adapter_manifest_integrity_invalid")
|
|
if any(
|
|
not isinstance(item, dict)
|
|
or not isinstance(item.get("module_name"), str)
|
|
or not _MODULE_PATH.fullmatch(item["module_name"])
|
|
for item in key_map
|
|
):
|
|
raise ValueError("adapter_manifest_integrity_invalid")
|
|
key_map_digest = _canonical_object_digest(key_map)
|
|
target_modules = [
|
|
item.get("module_name") for item in key_map if isinstance(item, dict)
|
|
]
|
|
rank = manifest.get("rank")
|
|
alpha = manifest.get("alpha")
|
|
scaling = manifest.get("scaling")
|
|
if (
|
|
manifest.get("key_map_digest") != key_map_digest
|
|
or manifest.get("model_card_digest") != _digest_file(root / "README.md")
|
|
or adapter.get("key_map_digest") != key_map_digest
|
|
or adapter.get("config_digest") != _digest_file(root / "adapter_config.json")
|
|
or config.get("base_model_name_or_path") != manifest_base.get("repo_id")
|
|
or config.get("revision") != manifest_base.get("revision")
|
|
or manifest.get("schema_id") != "obliteratus.peft-adapter-manifest"
|
|
or manifest.get("schema_version") != "1.0.0"
|
|
or config.get("peft_type") != "LORA"
|
|
or config.get("target_modules") != sorted(target_modules)
|
|
or manifest.get("target_modules") != sorted(target_modules)
|
|
or type(rank) is not int
|
|
or rank <= 0
|
|
or config.get("r") != rank
|
|
or type(alpha) is not int
|
|
or alpha <= 0
|
|
or config.get("lora_alpha") != alpha
|
|
or not isinstance(scaling, (int, float))
|
|
or isinstance(scaling, bool)
|
|
or not math.isfinite(scaling)
|
|
or scaling != alpha / rank
|
|
or config.get("lora_dropout") != manifest.get("dropout")
|
|
or config.get("bias") != manifest.get("bias")
|
|
):
|
|
raise ValueError("adapter_manifest_integrity_invalid")
|
|
|
|
|
|
def validate_adapter_base(
|
|
adapter_dir: str | Path,
|
|
base_model: BaseModelIdentity,
|
|
) -> dict[str, Any]:
|
|
"""Fail before loading weights when exact base/tokenizer facts do not match."""
|
|
root = Path(adapter_dir)
|
|
if (
|
|
root.is_symlink()
|
|
or not root.is_dir()
|
|
or root.absolute() != root.resolve(strict=True)
|
|
):
|
|
raise ValueError("adapter_directory_invalid")
|
|
config = _read_json_object(root / "adapter_config.json", "adapter_config_invalid")
|
|
manifest = _read_json_object(root / "adapter_manifest.json", "adapter_manifest_invalid")
|
|
provenance = _read_json_object(
|
|
root / "checkpoint-provenance.json",
|
|
"adapter_provenance_invalid",
|
|
)
|
|
_validate_adapter_integrity(root, config, manifest, provenance)
|
|
checks = (
|
|
(config.get("base_model_name_or_path"), base_model.repo_id, "base_model_identity_mismatch"),
|
|
(config.get("revision"), base_model.revision, "base_model_revision_mismatch"),
|
|
(
|
|
manifest.get("base_model", {}).get("weights_digest"),
|
|
base_model.weights_digest,
|
|
"base_model_digest_mismatch",
|
|
),
|
|
(
|
|
manifest.get("base_model", {}).get("tokenizer_digest"),
|
|
base_model.tokenizer_digest,
|
|
"tokenizer_digest_mismatch",
|
|
),
|
|
(
|
|
manifest.get("base_model", {}).get("vocab_size"),
|
|
base_model.vocab_size,
|
|
"vocab_size_mismatch",
|
|
),
|
|
(
|
|
manifest.get("base_model", {}).get("architecture"),
|
|
base_model.architecture,
|
|
"architecture_mismatch",
|
|
),
|
|
(
|
|
manifest.get("base_model", {}).get("tied_embeddings"),
|
|
base_model.tied_embeddings,
|
|
"tied_embeddings_mismatch",
|
|
),
|
|
)
|
|
for actual, expected, detail in checks:
|
|
if actual != expected:
|
|
raise ValueError(detail)
|
|
if provenance.get("base_model") != base_model.to_artifact_identity().to_dict():
|
|
raise ValueError("adapter_provenance_base_mismatch")
|
|
return manifest
|
|
|
|
|
|
def load_lora_adapters(
|
|
adapter_dir: str | Path,
|
|
*,
|
|
base_model: BaseModelIdentity,
|
|
) -> dict[str, tuple[torch.Tensor, torch.Tensor]]:
|
|
"""Load canonical safetensors only; this path never accepts pickle artifacts."""
|
|
root = Path(adapter_dir)
|
|
manifest = validate_adapter_base(root, base_model)
|
|
state = load_file(root / "adapter_model.safetensors", device="cpu")
|
|
validate_adapter_base(root, base_model)
|
|
scaling = manifest.get("scaling")
|
|
if (
|
|
isinstance(scaling, bool)
|
|
or not isinstance(scaling, (int, float))
|
|
or not math.isfinite(scaling)
|
|
or scaling <= 0
|
|
):
|
|
raise ValueError("adapter_scaling_invalid")
|
|
result: dict[str, tuple[torch.Tensor, torch.Tensor]] = {}
|
|
expected_keys: set[str] = set()
|
|
key_map = manifest.get("key_map")
|
|
if not isinstance(key_map, list):
|
|
raise ValueError("adapter_key_map_invalid")
|
|
for item in key_map:
|
|
if not isinstance(item, dict):
|
|
raise ValueError("adapter_key_map_invalid")
|
|
module_name = item.get("module_name")
|
|
a_key = item.get("lora_A_key")
|
|
b_key = item.get("lora_B_key")
|
|
if (
|
|
not isinstance(module_name, str)
|
|
or not isinstance(a_key, str)
|
|
or not isinstance(b_key, str)
|
|
or not _MODULE_PATH.fullmatch(module_name)
|
|
or module_name in result
|
|
or a_key == b_key
|
|
or a_key in expected_keys
|
|
or b_key in expected_keys
|
|
or a_key not in state
|
|
or b_key not in state
|
|
):
|
|
raise ValueError("adapter_key_map_invalid")
|
|
lora_a = state[a_key]
|
|
lora_b = state[b_key]
|
|
if (
|
|
lora_a.ndim != 2
|
|
or lora_b.ndim != 2
|
|
or lora_b.shape[1] != lora_a.shape[0]
|
|
or lora_a.dtype != lora_b.dtype
|
|
or item.get("lora_A_shape") != list(lora_a.shape)
|
|
or item.get("lora_B_shape") != list(lora_b.shape)
|
|
or item.get("rank") != lora_a.shape[0]
|
|
):
|
|
raise ValueError("adapter_tensor_geometry_mismatch")
|
|
expected_keys.update((a_key, b_key))
|
|
result[module_name] = (lora_b * scaling, lora_a)
|
|
if expected_keys != set(state):
|
|
raise ValueError("adapter_tensor_set_mismatch")
|
|
return result
|
|
|
|
|
|
def save_unsupported_obliteratus_adapters(
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
output_dir: str | Path,
|
|
*,
|
|
reason: str,
|
|
) -> Path:
|
|
"""Persist safe legacy tensors without making a PEFT compatibility claim."""
|
|
if not adapters:
|
|
raise ValueError("legacy adapter export requires at least one adapter")
|
|
if (
|
|
not isinstance(reason, str)
|
|
or not reason
|
|
or len(reason) > 1024
|
|
or _SECRET_VALUE.search(reason)
|
|
or Path(reason).is_absolute()
|
|
):
|
|
raise ValueError("legacy adapter reason is invalid")
|
|
output = Path(output_dir)
|
|
if output.is_symlink() or output.absolute() != output.resolve(strict=False):
|
|
raise ValueError("adapter output directory must not be a symlink")
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
state = {}
|
|
key_map = []
|
|
for module_name in sorted(adapters):
|
|
lora_b, lora_a = adapters[module_name]
|
|
b_key = f"{module_name}.B"
|
|
a_key = f"{module_name}.A"
|
|
state[b_key] = lora_b.detach().cpu().contiguous()
|
|
state[a_key] = lora_a.detach().cpu().contiguous()
|
|
key_map.append({"module_name": module_name, "A": a_key, "B": b_key})
|
|
path = output / "obliteratus_unsupported_adapter.safetensors"
|
|
config_path = output / "obliteratus_unsupported_adapter.json"
|
|
if path.exists() or config_path.exists():
|
|
raise FileExistsError("unsupported adapter artifact already exists")
|
|
save_file(dict(sorted(state.items())), path)
|
|
_write_new(
|
|
config_path,
|
|
_json_bytes(
|
|
{
|
|
"schema_id": "obliteratus.unsupported-adapter",
|
|
"schema_version": "1.0.0",
|
|
"support_status": "unsupported_legacy",
|
|
"safe_serialization": True,
|
|
"peft_compatible": False,
|
|
"reason": reason,
|
|
"key_map": key_map,
|
|
}
|
|
),
|
|
)
|
|
return path
|
|
|
|
|
|
def save_legacy_pickle_adapters_trusted(
|
|
adapters: dict[str, tuple[torch.Tensor, torch.Tensor]],
|
|
output_dir: str | Path,
|
|
*,
|
|
allow_pickle: bool,
|
|
) -> Path:
|
|
"""Write the historical pickle shape only behind an explicit trust gate."""
|
|
if allow_pickle is not True:
|
|
raise PermissionError("allow_pickle=True is required for unsafe legacy export")
|
|
output = Path(output_dir)
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
path = output / "obliteratus_legacy_adapter_unsafe.pt"
|
|
state = {
|
|
f"{module_name}.{suffix}": tensor
|
|
for module_name, (lora_b, lora_a) in sorted(adapters.items())
|
|
for suffix, tensor in (("lora_B", lora_b), ("lora_A", lora_a))
|
|
}
|
|
torch.save(state, path)
|
|
return path
|