Files
OBLITERATUS/obliteratus/analysis/adaptive_capture_study.py
Joseph MaglyandClaude Opus 5.5 780c450e0b feat(analysis): adaptive coarse-to-fine capture study
Add optimization study O03: can a coarse layer pass followed by
targeted fine captures find the same targets as the full sweep at lower
end-to-end cost?

- Three arms over the same rows and target rule: the reference sweep
  (every candidate layer), a fixed layer grid, and adaptive capture
  (coarse grid plus random exploration, then fine captures ranked by
  neighbouring AUC, uncertainty, direction disagreement and gap
  position, plus coverage probes).
- Forwards are counted by a hook; inference savings are claimed only
  when every seed needs fewer complete forwards. Bytes and
  extraction/CV work are counted; host timings stay outside the report
  digest.
- Unobserved regions are listed with "no evidence either way", and each
  missed target names its region.
- The recommendation is "no improvement" unless the missed-target rate
  stays within the pre-declared tolerance over at least 3 seeds with
  lower counted cost, and adoption also needs measured end-to-end time
  below the reference in every seed. Confirmation-exposed rows are
  refused.
- `obliteratus experiments adaptive-capture` and a Python API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:21:55 -05:00

117 lines
5.5 KiB
Python

"""O03 study: fixed layer grid, exhaustive reference sweep and adaptive coarse-to-fine capture.
All three arms run real captures of the same rows on the same model. The
reference sweep (the current exhaustive candidate-layer workflow) captures
every candidate layer on every row in one pass; the fixed grid captures every
``grid_stride``-th layer (plus the last) on every row in one pass; the
adaptive arm is described in ``adaptive_capture_plan``. The reference and
grid captures do not depend on the seed and are captured once; every seed
re-scores them with that seed's folds, and the adaptive arm captures anew.
The reference targets of each seed define recall for that seed.
Deterministic counters (complete forwards counted by a model pre-hook,
captured bytes, layer fits, captured row-layers) are sealed into the report;
measured forward, transfer and extraction/CV seconds go to
``host_measurements`` with the end-to-end sum and are excluded from
``report_sha256``.
"""
from __future__ import annotations
from collections import Counter
import torch
from obliteratus.analysis.adaptive_capture import (
UNOBSERVED, capture_layers, grid_indices, resolve_config,
)
from obliteratus.analysis.adaptive_capture_plan import adaptive_arm, score_capture
from obliteratus.analysis.adaptive_capture_report import (
adopt, compare_arm, inference_claim, recommend, seal,
)
SCHEMA = "obliteratus.adaptive-capture/1"
ARMS = ("reference_sweep", "fixed_grid", "adaptive")
NOTES = [
"Results on synthetic fixtures are not evidence for real models.",
"Capturing fewer layers does not skip the model's forward computation; inference savings "
"are claimed only through fewer complete forwards.",
"Unobserved layers are listed per arm and seed: " + UNOBSERVED + ".",
]
def _rows(rows):
if not rows:
raise ValueError("Adaptive capture needs at least one row")
positive = torch.tensor([bool(r["positive"]) for r in rows])
if not bool(positive.any()) or bool(positive.all()):
raise ValueError("Adaptive capture needs positive and negative rows")
return [r["batch"] for r in rows], positive, [str(r["group"]) for r in rows]
def _end_to_end(host):
if host["extraction_seconds"] is None:
return None
return host["forward_seconds"] + host["transfer_seconds"] + host["extraction_seconds"]
def run_adaptive_study(model, layers, rows, *, config=None, device="cpu", timings=True,
inputs=None):
"""Run every arm for every seed and return the sealed, versioned report.
``layers`` maps candidate site names, in depth order, to decoder modules;
``rows`` are ``{"batch", "positive", "group"}`` with one unpadded row per
batch. ``inputs`` is a JSON-ready description of the data source.
"""
if len(layers) < 2:
raise ValueError("Adaptive capture compares at least two candidate layers")
cfg = resolve_config(config, len(layers))
order = list(layers)
batches, positive, groups = _rows(rows)
everything = list(range(len(rows)))
reference_capture = capture_layers(model, layers, order, batches, everything, device=device)
grid_names = [order[i] for i in grid_indices(len(order), cfg["grid_stride"])]
grid_capture = capture_layers(model, layers, grid_names, batches, everything, device=device)
runs = {arm: [] for arm in ARMS}
host = {arm: [] for arm in ARMS}
for seed in range(cfg["seeds"]):
for arm, captured in (("reference_sweep", reference_capture),
("fixed_grid", grid_capture)):
result, measured = score_capture(order, captured, positive, groups, cfg, seed,
timings)
runs[arm].append(result)
host[arm].append(measured)
result, measured = adaptive_arm(model, layers, batches, positive, groups, cfg, seed,
device=device, timings=timings)
runs["adaptive"].append(result)
host["adaptive"].append(measured)
reference = runs["reference_sweep"]
comparisons = {arm: compare_arm(runs[arm], reference) for arm in ARMS}
for arm in ARMS:
comparisons[arm]["inference"] = inference_claim(comparisons[arm]["counts"],
comparisons["reference_sweep"]["counts"])
recommendation = recommend(comparisons, cfg)
report = {
"schema": SCHEMA, "study": "O03 adaptive capture and coarse-to-fine localization",
"config": cfg, "notes": NOTES, "inputs": inputs or {},
"candidate_layers": order, "grid_layers": grid_names,
"rows": {"total": len(rows), "positive": int(positive.sum()),
"negative": int((~positive).sum()), "groups": len(set(groups)),
"rows_per_group_max": max(Counter(groups).values())},
"arms": {arm: {"runs": runs[arm], "comparison": comparisons[arm]} for arm in ARMS},
"recommendation": recommendation,
}
if timings:
report["host_measurements"] = {
"recorded": True, "label": "measured on this host; not in report_sha256",
"arms": {arm: [{**m, "end_to_end_seconds": _end_to_end(m)} for m in host[arm]]
for arm in ARMS},
"note": "reference and grid capture seconds are one capture reused by every seed"}
else:
report["host_measurements"] = {"recorded": False}
report["adoption"] = adopt(recommendation, report["host_measurements"], cfg["seeds"])
return seal(report)
__all__ = ["ARMS", "SCHEMA", "run_adaptive_study"]