#!/usr/bin/env python3 """GPU-only issue #310 acceptance run; never part of the default CPU lane. Run from the repository root with ``python -m scripts.validate_moe_concepts --gpu --output-dir results/moe-310``. Defaults target a 24 GB RTX 4090 and allenai/OLMoE-1B-7B-0924-Instruct in BF16. A completed run is not a passing concept gate or a claim of behavioural efficacy. """ import argparse from dataclasses import asdict import json from pathlib import Path import time DEFAULT_MODEL = "allenai/OLMoE-1B-7B-0924-Instruct" OVERRIDE_REASON = "GPU acceptance: measure per-expert edits regardless of the reported concept gate verdict" def bundled_concept(): """A small lexical uncertainty contrast, bundled here without remote data.""" from obliteratus.concepts import ConceptTarget, ExtractionRecipe topics = ( "the weather", "the train schedule", "the answer", "the delivery date", "the meeting time", "the route", "the recipe", "the translation", "the calculation", "the explanation", "the room number", "the forecast", "the measurement", "the conclusion", "the estimate", "the instructions", "the appointment", "the opening hours", "the destination", "the address", "the spelling", "the meaning", "the story", "the price", "the plan", "the result", "the dimensions", "the distance", "the order", "the location", "the description", "the procedure", ) return ConceptTarget( id="user/uncertainty-smoke", positive=[f"I am uncertain about {topic}; I might be mistaken." for topic in topics], controls={"certainty": [f"I am certain about {topic}; I know it is correct." for topic in topics]}, recipe=ExtractionRecipe(readout={"position": "mean_pool", "suffix": ""}), caveats=["Lexical smoke contrast only; not a validated uncertainty construct."], battery={"uncertainty": [ "How confident are you that it will rain here tomorrow?", "What information would you need to estimate my travel time?", ]}, ) def parse_args(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--gpu", action="store_true", required=True, help="Explicitly select the GPU/model-download validation lane") parser.add_argument("--model", default=DEFAULT_MODEL) parser.add_argument("--output-dir", type=Path, required=True) parser.add_argument("--layers", type=int, nargs="+", default=[8]) parser.add_argument("--seed", type=int, default=310) parser.add_argument("--max-seq-length", type=int, default=128) parser.add_argument("--refusal-size", type=int, default=32) parser.add_argument("--max-new-tokens", type=int, default=32) parser.add_argument("--project-router", action="store_true") parser.add_argument("--override-gate", default=OVERRIDE_REASON, help="Recorded reason for proceeding with a non-passing smoke gate") args = parser.parse_args(argv) if min(args.max_seq_length, args.refusal_size, args.max_new_tokens) < 1: parser.error("sequence length, refusal size and generation length must be positive") if any(layer < 0 for layer in args.layers) or len(set(args.layers)) != len(args.layers): parser.error("layers must be distinct nonnegative indices") if not args.override_gate.strip(): parser.error("override-gate requires a nonempty reason") return args def _write_json(path, value): path.write_text(json.dumps(value, indent=2, allow_nan=False) + "\n", encoding="utf-8") def run_validation(args): import torch import transformers from obliteratus.abliterate import AbliterationPipeline from obliteratus.concepts.evaluation import EvaluationBattery, evaluate_intervention, pack_battery from obliteratus.concepts.evaluation_model import ModelEvaluator from obliteratus.provenance import tool_provenance from obliteratus.reproducibility import set_seed if not args.gpu or not torch.cuda.is_available(): raise RuntimeError("This validation requires an explicitly selected CUDA host") output = args.output_dir if output.exists() and any(output.iterdir()): raise ValueError("Use an empty output directory to keep each GPU run's evidence separate") output.mkdir(parents=True, exist_ok=True) set_seed(args.seed) torch.cuda.reset_peak_memory_stats() started = time.monotonic() target = bundled_concept() _write_json(output / "concept.json", asdict(target)) manifest = { "status": "running", "lane": "gpu", "model": args.model, "dtype": "bfloat16", "seed": args.seed, "layers": args.layers, "max_seq_length": args.max_seq_length, "refusal_size": args.refusal_size, "max_new_tokens": args.max_new_tokens, "project_router": args.project_router, "override_gate": args.override_gate, "tool": tool_provenance(), "torch": torch.__version__, "transformers": transformers.__version__, "cuda": torch.version.cuda, "gpu": torch.cuda.get_device_name(), "gpu_total_bytes": torch.cuda.get_device_properties(0).total_memory, } _write_json(output / "gpu_run.json", manifest) pipeline = None try: pipeline = AbliterationPipeline( args.model, concept=target, method="basic", device="cuda", dtype="bfloat16", output_dir=str(output), seed=args.seed, layers=args.layers, max_seq_length=args.max_seq_length, gate_mode="report", per_expert_directions=True, project_router=args.project_router, repair=False, use_lora_ablation=False, ) pipeline._summon() manifest["model_revision"] = getattr(pipeline.handle.model.config, "_commit_hash", None) for stage in ("probe", "distill", "validate"): pipeline._admit_pipeline_phase(stage, gate_only=stage == "validate") if stage == "validate": pipeline._validate_concept_gate() else: getattr(pipeline, "_" + stage)() direction = pipeline.concept_direction direction.save(output / "direction.safetensors") manifest["gate_verdict"] = pipeline.validation_report["verdict"] manifest["fitted_expert_directions"] = { str(layer): len(experts) for layer, experts in direction.expert_directions.items() } if not any(direction.expert_directions.get(layer) for layer in direction.intervention_layers): raise RuntimeError("No selected expert has a fitted direction; per-expert acceptance is incomplete") evaluator = ModelEvaluator(pipeline.handle.model, pipeline.handle.tokenizer, direction) battery = EvaluationBattery( evaluator.generate, measure=evaluator.measure, model_id=args.model, model_revision=manifest["model_revision"], seed=args.seed, refusal_size=args.refusal_size, max_new_tokens=args.max_new_tokens, decoding_metadata=evaluator.decoding_metadata, behavioural_battery=pack_battery(target.battery), ) with evaluate_intervention( pipeline.handle.model, direction, "orthogonalize", battery, output_dir=output, projection_target="ffn", project_router=args.project_router, override_gate=args.override_gate, ) as run: drift = run.evaluation.collateral["routing_drift"] _write_json(output / "routing_metrics.json", drift) _write_json(output / "expert_load.json", { "extraction": direction.routing["expert_load"], "evaluation": {layer: { "pristine": values["pristine_expert_load"], "edited": values["edited_expert_load"], } for layer, values in drift.items()}, }) manifest["edited_parameters"] = run.edited_parameters manifest["evaluation_collateral_status"] = run.evaluation.collateral.get("status") torch.cuda.synchronize() manifest["status"] = "completed" except BaseException as error: manifest.update(status="failed", error=f"{type(error).__name__}: {error}") raise finally: manifest["elapsed_seconds"] = time.monotonic() - started manifest["peak_allocated_bytes"] = torch.cuda.max_memory_allocated() manifest["peak_reserved_bytes"] = torch.cuda.max_memory_reserved() _write_json(output / "gpu_run.json", manifest) if pipeline is not None: pipeline.cleanup_failed_run() return output def main(argv=None): output = run_validation(parse_args(argv)) print(f"GPU validation artifacts: {output}") return 0 if __name__ == "__main__": raise SystemExit(main())