mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Muse BT6 audit of 3c7a6ca for #671: the experiments runner cleans up the
handle even when observers.close() fails after a successful run; the neuron
lab notes a storage cleanup failure on the model-cleanup error and lets
interrupts win; candidate and smaller_change wrap directory removal so handle
cleanup always runs and the primary error stays primary (shutil imported at
module top). A shared run_during_error helper carries the logic, and the
replaced-restore-point warning names the path to remove.
132 lines
6.1 KiB
Python
132 lines
6.1 KiB
Python
"""Step artifacts of an edit sequence: a checkpoint/adapter DAG.
|
|
|
|
A step's artifact is a stack: a base checkpoint (the selected reference, or the
|
|
edited copy written by an earlier weight-edit step) and an ordered list of
|
|
activation adapters. ``stack.json`` names them by path relative to the sequence
|
|
directory. Loading reads the base from its files and wraps each adapter's site
|
|
module in order, so a later adapter acts on an earlier one's output, the same
|
|
order as the interaction experiment's ``X>Y`` hooks.
|
|
|
|
* a candidate's activation adapter is copied byte for byte and checked against
|
|
the candidate manifest's artifact digests;
|
|
* a candidate weight edit on the reference is the candidate's edited copy, also
|
|
copied; on an edited parent the edit is recomputed from the re-derived target
|
|
on the parent's weights and saved as a new copy;
|
|
* a registration component writes an activation adapter from the re-derived
|
|
target at the registered decision coefficient.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
|
|
from obliteratus.concepts.dataset import identity
|
|
from obliteratus.concepts.restoration import state_digest
|
|
from obliteratus.experiments import candidate as cand
|
|
from obliteratus.experiments import candidate_edits as edits
|
|
from obliteratus.experiments import candidate_io as cio
|
|
from obliteratus.experiments.sequence import SequenceRefused
|
|
from obliteratus.handle_cleanup import cleanup_during_error, cleanup_on_exit
|
|
|
|
STACK = "stack.json"
|
|
ARTIFACT = "artifact"
|
|
REFERENCE = {"kind": "reference"}
|
|
|
|
|
|
def reference_identity(plan):
|
|
return identity({"reference_files_sha256": plan["frozen"]["reference"]["files_sha256"]})
|
|
|
|
|
|
def step_name(position):
|
|
return f"{position:02d}"
|
|
|
|
|
|
def _copy_candidate(component, out):
|
|
manifest, path = cand.read_candidate(component["run"], component["source"]["candidate"])
|
|
shutil.copytree(path / manifest["artifact"]["path"], out)
|
|
if cio.tree_digests(out) != manifest["artifact"]["files"]:
|
|
raise SequenceRefused(f"Copied artifact of {manifest['id']} differs from its manifest")
|
|
|
|
|
|
def write_step(seq_dir, plan, position, parent_stack, component, target, model_io):
|
|
"""Write step ``position``'s artifact under ``steps/NN/artifact``; returns the stack."""
|
|
relative = f"steps/{step_name(position)}/{ARTIFACT}"
|
|
out = Path(seq_dir) / relative
|
|
out.mkdir(parents=True)
|
|
source, representation = component["source"], component["source"]["representation"]
|
|
op = component["signature"]["operation"]
|
|
coefficient = op["coefficient"]
|
|
if representation == "activation_adapter":
|
|
if source["kind"] == "candidate":
|
|
_copy_candidate(component, out / "adapter")
|
|
else:
|
|
edits.save_activation_adapter(out / "adapter", target["vector"].detach().cpu(),
|
|
edits.adapter_config(target["module"], op, coefficient))
|
|
stack = {"base": parent_stack["base"],
|
|
"adapters": [*parent_stack["adapters"], f"{relative}/adapter"]}
|
|
else:
|
|
if parent_stack["adapters"]: # pragma: no cover - refused when the plan is written
|
|
raise SequenceRefused("A weight edit cannot follow an activation adapter")
|
|
if parent_stack["base"] == REFERENCE:
|
|
_copy_candidate(component, out / "model")
|
|
else:
|
|
handle = load_stack(seq_dir, plan, parent_stack, model_io)
|
|
# Cleans up only a callable ``cleanup``; never replaces an in-flight error.
|
|
with cleanup_on_exit(handle):
|
|
module = dict(handle.model.named_modules())[target["module"]]
|
|
changed = edits.apply_weight_edit(handle.model, target["module"],
|
|
edits.weight_edit(module, op, target["vector"],
|
|
coefficient))
|
|
model_io.save(handle.model, handle.tokenizer, out / "model")
|
|
if not changed:
|
|
raise SequenceRefused(f"Step {position} changes no parameter of its parent")
|
|
stack = {"base": {"kind": "step", "path": f"{relative}/model"}, "adapters": []}
|
|
(out / STACK).write_text(json.dumps(stack, indent=2, sort_keys=True) + "\n",
|
|
encoding="utf-8")
|
|
return stack
|
|
|
|
|
|
def load_stack(seq_dir, plan, stack, model_io):
|
|
"""Fresh load of a stack from disk (``model``, ``tokenizer``, optional ``cleanup``)."""
|
|
from types import SimpleNamespace
|
|
base = (plan["frozen"]["reference"]["source"] if stack["base"] == REFERENCE
|
|
else Path(seq_dir) / stack["base"]["path"])
|
|
handle = model_io.load(base, plan["frozen"]["model"])
|
|
try:
|
|
for adapter in stack["adapters"]:
|
|
edits.attach_activation_adapter(handle.model, Path(seq_dir) / adapter)
|
|
return SimpleNamespace(model=handle.model, tokenizer=handle.tokenizer,
|
|
cleanup=getattr(handle, "cleanup", None))
|
|
except BaseException as error:
|
|
# A cleanup failure is noted on the in-flight error, never replacing it.
|
|
cleanup_during_error(handle, error)
|
|
raise
|
|
|
|
|
|
def stack_files(seq_dir, plan, stack):
|
|
"""``{path: {file: sha256}}`` of every file the stack loads (base and adapters)."""
|
|
base = (plan["frozen"]["reference"]["source"] if stack["base"] == REFERENCE
|
|
else Path(seq_dir) / stack["base"]["path"])
|
|
found = {"base": cio.tree_digests(base)}
|
|
for adapter in stack["adapters"]:
|
|
found[adapter] = cio.tree_digests(Path(seq_dir) / adapter)
|
|
return found
|
|
|
|
|
|
def output_identity(parent, position, component, artifact_sha256, loaded_state):
|
|
"""Step output identity: parent identity, order, component and the bytes it loads."""
|
|
return identity({"parent": parent, "position": position,
|
|
"component": {"signature": component["signature"],
|
|
"source": component["source"]},
|
|
"artifact_sha256": artifact_sha256, "loaded_state_sha256": loaded_state})
|
|
|
|
|
|
def loaded_state(model):
|
|
return state_digest(model)
|
|
|
|
|
|
__all__ = ["ARTIFACT", "REFERENCE", "STACK", "load_stack", "loaded_state", "output_identity",
|
|
"reference_identity", "stack_files", "step_name", "write_step"]
|