mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Resolves conflicts in the Pain Axis scripts between the capacity planning entry points and the split-model placement controls from #357; the planner now honours device maps, max_memory and offload arguments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
269 lines
14 KiB
Python
269 lines
14 KiB
Python
"""S2 cut -> pristine self-distillation -> LoRA -> collateral/Appendix D pilot.
|
|
|
|
Run on an explicitly authorized GPU runner, never in CI. Models are
|
|
resident one at a time. Inputs are a portable S2 direction, pinned Pain-axis
|
|
checkout, and licensed held-out harmful/benign prompt JSONL.
|
|
"""
|
|
|
|
import argparse
|
|
from copy import deepcopy
|
|
from dataclasses import asdict
|
|
import gc
|
|
from pathlib import Path
|
|
|
|
import torch
|
|
|
|
from obliteratus.capacity_planning import CapacityPlanningError, add_capacity_arguments, maybe_plan
|
|
from obliteratus.concepts.direction import ConceptDirection
|
|
from obliteratus.concepts.evaluation import EvaluationBattery
|
|
from obliteratus.concepts.evaluation_model import ModelEvaluator
|
|
from obliteratus.concepts.intervention import apply_concept_intervention
|
|
from obliteratus.concepts.runs import save_concept_run
|
|
from obliteratus.model_placement import add_placement_arguments, placement_from_args, placement_metadata
|
|
from obliteratus.persistence_contracts import atomic_checkpoint_directory
|
|
from obliteratus.recover import RecoveryConfig, _files, _load_model, _model_directory, recover
|
|
from obliteratus.recovery_assessment import assess_recovery
|
|
from obliteratus.recovery_dataset import (
|
|
build_recovery_dataset, checked_output, file_digest, json_bytes, read_json,
|
|
)
|
|
from obliteratus.recovery_distillation import build_self_distillation, check_disjointness, prompt_mix
|
|
|
|
if __package__:
|
|
from . import pain_axis_appendix_d as appendix
|
|
else:
|
|
import pain_axis_appendix_d as appendix
|
|
|
|
REASON = "Pain Axis refusal recovery pilot (upstream OBLITERATUS#346)"
|
|
|
|
|
|
def select_s2(source, layer):
|
|
if layer is None:
|
|
if len(source.directions) != 1:
|
|
raise ValueError("Use --extraction-layer for a multi-layer S2 artifact")
|
|
layer = next(iter(source.directions))
|
|
if layer not in source.directions:
|
|
raise ValueError("S2 artifact has no direction at the selected layer")
|
|
result = deepcopy(source)
|
|
result.directions = {layer: source.directions[layer].clone()}
|
|
result.extraction_layers = result.intervention_layers = [layer]
|
|
# This pilot removes the selected S2 vector, not an artifact's rank-k basis.
|
|
for name in ("subspaces", "projector_left", "projector_right", "reference_means", "cv_scores"):
|
|
values = getattr(result, name)
|
|
setattr(result, name, {layer: values[layer]} if layer in values else {})
|
|
return result
|
|
|
|
|
|
def load_local(directory, args):
|
|
from transformers import AutoTokenizer
|
|
|
|
tok = AutoTokenizer.from_pretrained(str(directory), local_files_only=True,
|
|
trust_remote_code=args.trust_remote_code)
|
|
model = _load_model(directory, recovery_config(args), args.trust_remote_code)
|
|
model.eval()
|
|
return tok, model
|
|
|
|
|
|
def recovery_config(args):
|
|
placement = placement_from_args(args)
|
|
device_map = placement.get("device_map")
|
|
devices = list(device_map.values()) if isinstance(device_map, dict) else [device_map]
|
|
device = "cpu" if devices and all(str(value) == "cpu" for value in devices) else "cuda"
|
|
return RecoveryConfig(device=device, dtype="float32" if device == "cpu" else "bfloat16",
|
|
max_length=args.max_length, rank=args.rank, alpha=2 * args.rank,
|
|
epochs=args.epochs, gradient_accumulation=args.gradient_accumulation,
|
|
learning_rate=args.learning_rate, seed=args.seed,
|
|
target_modules=tuple(args.target_modules), **placement)
|
|
|
|
|
|
def release():
|
|
gc.collect()
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
def humor(model, tokenizer, scenarios, args, out, name):
|
|
rows = appendix.generate(model, tokenizer, scenarios, appendix.replication.MODELS[args.name][0],
|
|
1, args.humor_tokens)
|
|
(out / f"{name}_humor.json").write_bytes(json_bytes(rows))
|
|
return {"humor_proxy": sum(bool(appendix.replication.HUMOR.search(r["generation"])) for r in rows),
|
|
"n": len(rows), "max_new_tokens": args.humor_tokens, "do_sample": False,
|
|
"batch_size": 1, "generation_file": f"{name}_humor.json"}
|
|
|
|
|
|
def run(args):
|
|
if getattr(args, "plan_only", False):
|
|
if args.plan_output is None:
|
|
output = Path(args.out)
|
|
args.plan_output = output.with_name(output.name + ".capacity-plan.json")
|
|
placement = placement_from_args(args)
|
|
device_map = placement.get("device_map")
|
|
devices = list(device_map.values()) if isinstance(device_map, dict) else [device_map]
|
|
dtype = "float32" if devices and all(str(value) == "cpu" for value in devices) else "bfloat16"
|
|
return maybe_plan(
|
|
args, args.model, dtype=dtype, placement=placement,
|
|
batch_size=1, sequence_length=args.max_length, concept_edit=True,
|
|
generation_tokens=max(args.humor_tokens, args.refusal_tokens, args.distill_tokens),
|
|
unsupported_reason="Recovery training memory is not modelled; "
|
|
"this is an inference capacity estimate only",
|
|
)
|
|
config = recovery_config(args)
|
|
try:
|
|
import peft # noqa: F401 (needed only at the recovery stage; fail before GPU work)
|
|
except ImportError as error:
|
|
raise SystemExit("the pilot needs the recovery extra (peft); install obliteratus[recovery]") from error
|
|
direction = select_s2(ConceptDirection.load(args.direction), args.extraction_layer)
|
|
prompt_rows = [read_json(line) for line in Path(args.prompts).read_bytes().splitlines()]
|
|
mix = prompt_mix(prompt_rows)
|
|
if not all(mix.values()):
|
|
raise ValueError("pilot requires both harmful and benign held-out prompts")
|
|
check_disjointness([r["prompt"] for r in prompt_rows])
|
|
scenarios_path = Path(args.pin) / "datasets/4.1_self_other_420_scenarios.json"
|
|
scenarios = [row for row in read_json(scenarios_path.read_bytes())
|
|
if row["category"] in appendix.replication.KEEP_CATEGORIES]
|
|
if len(scenarios) != 100:
|
|
raise ValueError("Expected the pinned 100 hostile Appendix D scenarios")
|
|
neighbours = {}
|
|
for item in args.neighbour:
|
|
name, path = item.split("=", 1)
|
|
if not name or name in neighbours:
|
|
raise ValueError("neighbour names must be nonempty and distinct")
|
|
neighbours[name] = ConceptDirection.load(path)
|
|
inputs = [args.prompts, args.license, args.direction, args.pin]
|
|
if Path(args.model).exists():
|
|
inputs.append(args.model)
|
|
out = checked_output(args.out, inputs)
|
|
if out.exists():
|
|
raise ValueError("pilot output must be a new directory")
|
|
out.mkdir(parents=True)
|
|
config_metadata = asdict(config)
|
|
config_metadata.update(placement_metadata(None, **placement_from_args(args))["requested"])
|
|
summary = {"status": "running", "stage": "pristine", "model": args.model,
|
|
"revision": args.revision, "direction_sha256": file_digest(args.direction),
|
|
"scenario_sha256": file_digest(scenarios_path), "config": config_metadata,
|
|
"projection_threshold": args.projection_threshold, "collateral_margin": args.collateral_margin,
|
|
"seed": args.seed, "humor": {}, "neighbours": args.neighbour}
|
|
model = evaluator = battery = intervention = None
|
|
try:
|
|
directory, identity = _model_directory(args.model, args.revision, args.allow_network, args.trust_remote_code)
|
|
checked_output(out, [directory])
|
|
pristine_files = _files(directory)
|
|
summary["pristine"] = {**identity, "files": pristine_files}
|
|
tok, model = load_local(directory, args)
|
|
summary["placement"] = {"pristine": placement_metadata(model, **placement_from_args(args))}
|
|
evaluator = ModelEvaluator(model, tok, direction, neighbours=neighbours)
|
|
battery = EvaluationBattery(
|
|
evaluator.generate, measure=evaluator.measure, model_id=args.model,
|
|
model_revision=args.revision, refusal_size=args.refusal_size,
|
|
collateral_margin=args.collateral_margin, seed=args.seed,
|
|
max_new_tokens=args.refusal_tokens, decoding_metadata=evaluator.decoding_metadata,
|
|
).capture()
|
|
summary["humor"]["pristine"] = humor(model, tok, scenarios, args, out, "pristine")
|
|
summary["stage"] = "cut"
|
|
intervention = apply_concept_intervention(
|
|
model, direction, "orthogonalize", layers="all", project_embeddings=True,
|
|
tied_embeddings="edit_both", override_gate=args.override_gate,
|
|
)
|
|
cut = battery.finish(out / "cut")
|
|
summary["cut_assessment"] = assess_recovery(cut, projection_threshold=args.projection_threshold)
|
|
summary["humor"]["cut"] = humor(model, tok, scenarios, args, out, "cut")
|
|
with atomic_checkpoint_directory(out / "edited") as staging:
|
|
save_concept_run(staging, direction, intervention, model=model, tokenizer=tok)
|
|
battery.generate = battery.measure = None
|
|
model = evaluator = intervention = None
|
|
release()
|
|
summary["stage"] = "distillation"
|
|
build_self_distillation(
|
|
directory, args.prompts, out / "distillation", license=read_json(Path(args.license).read_bytes()),
|
|
response_license=read_json(Path(args.response_license).read_bytes()) if args.response_license else None,
|
|
refusal_policy="keep", max_new_tokens=args.distill_tokens, device=config.device, dtype=config.dtype,
|
|
**placement_from_args(args),
|
|
trust_remote_code=args.trust_remote_code,
|
|
)
|
|
if _files(directory) != pristine_files:
|
|
raise ValueError("pristine checkpoint changed between baseline and distillation")
|
|
build_recovery_dataset(out / "distillation/manifest.json", out / "corpus", seed=args.seed)
|
|
release()
|
|
summary["stage"] = "recovery"
|
|
recover(out / "edited", out / "corpus", out / "recovered", config=config,
|
|
trust_remote_code=args.trust_remote_code)
|
|
release()
|
|
summary["stage"] = "assessment"
|
|
from peft import PeftModel
|
|
tok, model = load_local(out / "edited", args)
|
|
summary["placement"]["assessment"] = placement_metadata(model, **placement_from_args(args))
|
|
model = PeftModel.from_pretrained(model, str(out / "recovered/adapter"), local_files_only=True)
|
|
model.eval()
|
|
evaluator = ModelEvaluator(model.get_base_model(), tok, direction, neighbours=neighbours)
|
|
battery.generate, battery.measure = evaluator.generate, evaluator.measure
|
|
report = battery.finish(out / "post_recovery")
|
|
combined = assess_recovery(report, projection_threshold=args.projection_threshold)
|
|
(out / "assessment.json").write_bytes(json_bytes(combined))
|
|
summary["humor"]["recovered"] = humor(model, tok, scenarios, args, out, "recovered")
|
|
summary.update(status="complete", assessment=combined["status"])
|
|
except Exception as error:
|
|
summary.update(status="failed", error=str(error))
|
|
raise
|
|
finally:
|
|
if battery is not None:
|
|
battery.generate = battery.measure = None
|
|
model = evaluator = intervention = None
|
|
release()
|
|
(out / "summary.json").write_bytes(json_bytes(summary))
|
|
return summary
|
|
|
|
|
|
def parser():
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument("--model", default="google/gemma-2-2b-it")
|
|
p.add_argument("--revision", help="Immutable Hub commit; unnecessary for a local checkpoint")
|
|
p.add_argument("--name", choices=list(appendix.replication.MODELS), default="Gemma_2_2B_instruct")
|
|
p.add_argument("--direction", required=True, help="Portable S2 .safetensors direction artifact")
|
|
p.add_argument("--extraction-layer", type=int)
|
|
p.add_argument("--pin", required=True, help="Pain-axis checkout pinned at 4d75cd90")
|
|
p.add_argument("--prompts", required=True, help="Held-out harmful + benign JSONL")
|
|
p.add_argument("--license", required=True, help="Prompt licence declaration JSON")
|
|
p.add_argument("--response-license", help="Response licence declaration JSON")
|
|
p.add_argument("--out", required=True)
|
|
p.add_argument("--projection-threshold", required=True, type=float)
|
|
p.add_argument("--collateral-margin", type=float, default=0.03)
|
|
p.add_argument("--refusal-size", type=int, default=320)
|
|
p.add_argument("--refusal-tokens", type=int, default=48)
|
|
p.add_argument("--humor-tokens", type=int, default=60)
|
|
p.add_argument("--distill-tokens", type=int, default=96)
|
|
p.add_argument("--max-length", type=int, default=256)
|
|
p.add_argument("--rank", type=int, default=8)
|
|
p.add_argument("--epochs", type=int, default=1)
|
|
p.add_argument("--gradient-accumulation", type=int, default=8)
|
|
p.add_argument("--learning-rate", type=float, default=2e-4)
|
|
p.add_argument("--target-modules", nargs="+", default=["q_proj", "v_proj"])
|
|
p.add_argument("--seed", type=int, default=42)
|
|
p.add_argument("--neighbour", action="append", default=[], help="Optional NAME=portable-direction path")
|
|
p.add_argument("--allow-network", action="store_true")
|
|
p.add_argument("--trust-remote-code", action="store_true")
|
|
p.add_argument("--override-gate", default=REASON)
|
|
add_capacity_arguments(p)
|
|
add_placement_arguments(p)
|
|
return p
|
|
|
|
|
|
def main(argv=None):
|
|
p = parser()
|
|
args = p.parse_args(argv)
|
|
# Validate declared endpoints before any model acquisition.
|
|
try:
|
|
assess_recovery({}, projection_threshold=args.projection_threshold,
|
|
collateral_margin=args.collateral_margin)
|
|
if min(args.refusal_size, args.refusal_tokens, args.humor_tokens, args.distill_tokens) < 1:
|
|
raise ValueError("prompt counts and generation lengths must be positive")
|
|
except ValueError as error:
|
|
p.error(str(error))
|
|
if not args.plan_only:
|
|
torch.manual_seed(args.seed)
|
|
try:
|
|
run(args)
|
|
except CapacityPlanningError as exc:
|
|
p.error(str(exc))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|