mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 02:53:54 +01:00
Add an opt-in, in-memory reuse cache (O04) for work repeated across analyses: per-layer and per-fold extraction fits, candidate-discovery nulls and labeled row selections, plus incremental pooled moments. - Artifacts are keyed by kind, algorithm version, site, partition/role, fold, normalization, tokenizer, model, adapter, weight fingerprint, dataset revision, parameters and a data digest. Missing or unknown fields never match. - A statistic fitted on all rows, another fold or held-out rows is never served inside a train or held-out fold (contamination refused); other mismatches are refused with per-field reasons. - Exact results only; incremental moments carry a declared error budget and a distinct algorithm name. Reports cover hits, misses, avoided work and setup cost amortized over hits. - extract_directions and discover_candidates take reuse=; outputs are bit-identical to fresh computation and defaults are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
8.0 KiB
Python
166 lines
8.0 KiB
Python
"""O04 opt-in reuse in extraction and candidate discovery: bit-identical outcomes."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis.candidate_discovery import discover_candidates
|
|
from obliteratus.analysis.labeled_selection import LabeledSelection
|
|
from obliteratus.analysis.reuse_cache import ReuseCache
|
|
from obliteratus.analysis.reuse_consumers import cache_fetch, labeled_matrix
|
|
from obliteratus.analysis.reuse_identity import ReuseRefused, ReuseScope
|
|
from obliteratus.concepts.extraction import extract_directions
|
|
from tests.test_concept_extraction import cv_recipe, planted_cache
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
OPTIONS = {"n_permutations": 6, "n_bootstrap": 5, "n_random": 8, "folds": 3}
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def single_thread():
|
|
previous = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
yield
|
|
torch.set_num_threads(previous)
|
|
|
|
|
|
def make_scope(cache=None, **changes):
|
|
fields = {"tokenizer": "tok", "model": "model", "adapter": "none",
|
|
"weights": "sampled:" + "a" * 64, "dataset_revision": "rev1", "capture": None}
|
|
fields.update(changes)
|
|
return ReuseScope(cache=ReuseCache() if cache is None else cache, **fields)
|
|
|
|
|
|
def assert_same_direction(left, right):
|
|
assert left.extraction_layers == right.extraction_layers
|
|
assert left.intervention_layers == right.intervention_layers
|
|
assert left.cv_scores == right.cv_scores
|
|
for name in ("directions", "subspaces", "reference_means", "projector_left",
|
|
"projector_right"):
|
|
a, b = getattr(left, name), getattr(right, name)
|
|
assert a.keys() == b.keys()
|
|
assert all(torch.equal(a[k], b[k]) for k in a)
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["diff_means", "fisher_lda", "leace_exact"])
|
|
def test_extraction_reuse_is_bit_identical_and_avoids_fold_refits(method):
|
|
positive, controls = planted_cache()
|
|
recipe = cv_recipe(method=method)
|
|
fresh = extract_directions(positive, controls, recipe, seed=227)
|
|
scope = make_scope()
|
|
cold = extract_directions(positive, controls, recipe, seed=227, reuse=scope)
|
|
assert_same_direction(fresh, cold)
|
|
after_cold = scope.cache.report()
|
|
layers, folds = len(positive), recipe.layer_select["folds"]
|
|
assert after_cold["misses"] == layers * (folds + 1) and after_cold["hits"] == 0
|
|
other_rule = replace(recipe, intervention_layer={"rule": "norm_ratio", "target": 0.5})
|
|
warm = extract_directions(positive, controls, other_rule, seed=227, reuse=scope)
|
|
assert_same_direction(extract_directions(positive, controls, other_rule, seed=227), warm)
|
|
report = scope.cache.report()
|
|
assert report["hits"] == layers * (folds + 1)
|
|
assert report["avoided"]["estimated_ops"] > 0 and report["avoided"]["input_bytes"] > 0
|
|
assert {e["fold"] for e in report["events"]} == {"all"} | {
|
|
f"train:{k}/{folds}" for k in range(folds)}
|
|
assert all(e["validity"]["matched_fields"] for e in report["events"]
|
|
if e["outcome"] == "hit")
|
|
|
|
|
|
def test_extraction_reuse_misses_when_fit_inputs_change():
|
|
positive, controls = planted_cache()
|
|
scope = make_scope()
|
|
extract_directions(positive, controls, cv_recipe(), seed=227, reuse=scope)
|
|
before = scope.cache.report()["hits"]
|
|
extract_directions(positive, controls, cv_recipe(), seed=228, reuse=scope)
|
|
# Whole-set fits are shared; a different seed changes every fold split.
|
|
assert scope.cache.report()["hits"] - before == len(positive)
|
|
changed = {layer: value + 1e-3 for layer, value in positive.items()}
|
|
hits = scope.cache.report()["hits"]
|
|
extract_directions(changed, controls, cv_recipe(), seed=227, reuse=scope)
|
|
assert scope.cache.report()["hits"] == hits
|
|
extract_directions(positive, controls, cv_recipe(method="fisher_lda"), seed=227,
|
|
reuse=scope)
|
|
assert scope.cache.report()["hits"] == hits
|
|
other_model = make_scope(scope.cache, weights="sampled:" + "b" * 64)
|
|
extract_directions(positive, controls, cv_recipe(), seed=227, reuse=other_model)
|
|
report = scope.cache.report()
|
|
assert report["hits"] == hits and report["refused"] > 0
|
|
assert any(r["field"] == "weights" for e in report["events"] if e["outcome"] == "refused"
|
|
for r in e["reasons"])
|
|
|
|
|
|
def test_extraction_preset_path_with_reuse_matches_default():
|
|
positive, controls = planted_cache()
|
|
recipe = replace(cv_recipe(), layer_select={"strategy": "preset", "preset": "all"})
|
|
scope = make_scope()
|
|
for _ in range(2):
|
|
assert_same_direction(extract_directions(positive, controls, recipe),
|
|
extract_directions(positive, controls, recipe, reuse=scope))
|
|
assert scope.cache.report()["hits"] == len(positive)
|
|
|
|
|
|
def labeled_fixture(seed=5, revision="rev1", capture="c" * 64):
|
|
generator = torch.Generator().manual_seed(seed)
|
|
sets = ["positive"] * 24 + ["control:neutral"] * 24
|
|
values = torch.randn(48, 6, generator=generator)
|
|
values[:24, 2] += 2.5
|
|
roles = tuple("select" if i % 4 == 3 else "fit" for i in range(48))
|
|
groups = tuple(f"g{i // 2}" for i in range(48))
|
|
manifest = {"capture_identity_sha256": capture, "labels_sha256": "l" * 64,
|
|
"dataset_revision": revision, "pack": {"id": "p"}, "partitioned": True,
|
|
"locked_test_acknowledged": False}
|
|
return LabeledSelection(
|
|
manifest=manifest, values={"layer.0": values}, index=tuple(range(48)),
|
|
record_ids=tuple(f"r{i}" for i in range(48)), paths=tuple("p" for _ in range(48)),
|
|
sets=tuple(sets), kinds=tuple("positive" if s == "positive" else "control"
|
|
for s in sets),
|
|
roles=roles, groups=groups, families=tuple(None for _ in range(48)))
|
|
|
|
|
|
def test_discovery_null_reuse_is_bit_identical():
|
|
labeled = labeled_fixture()
|
|
fresh = discover_candidates(labeled, "layer.0", seed=3, **OPTIONS)
|
|
scope = make_scope(capture="c" * 64)
|
|
cold = discover_candidates(labeled, "layer.0", seed=3, reuse=scope, **OPTIONS)
|
|
warm = discover_candidates(labeled, "layer.0", seed=3, reuse=scope,
|
|
**{**OPTIONS, "top_n": 3, "n_bootstrap": 7})
|
|
assert json.dumps(fresh, sort_keys=True) == json.dumps(cold, sort_keys=True)
|
|
reference = discover_candidates(labeled, "layer.0", seed=3,
|
|
**{**OPTIONS, "top_n": 3, "n_bootstrap": 7})
|
|
assert json.dumps(reference, sort_keys=True) == json.dumps(warm, sort_keys=True)
|
|
report = scope.cache.report()
|
|
assert (report["misses"], report["hits"]) == (1, 1)
|
|
assert report["avoided"]["estimated_ops"] > 0
|
|
discover_candidates(labeled, "layer.0", seed=4, reuse=scope, **OPTIONS)
|
|
assert scope.cache.report()["refused"] == 1
|
|
|
|
|
|
def test_discovery_refuses_scope_bound_to_another_capture_or_revision():
|
|
labeled = labeled_fixture()
|
|
with pytest.raises(ReuseRefused, match="different capture"):
|
|
discover_candidates(labeled, "layer.0", reuse=make_scope(capture="d" * 64), **OPTIONS)
|
|
with pytest.raises(ReuseRefused, match="revision"):
|
|
discover_candidates(labeled, "layer.0", reuse=make_scope(dataset_revision="rev2"),
|
|
**OPTIONS)
|
|
|
|
|
|
def test_labeled_matrix_reuse_by_lineage():
|
|
labeled = labeled_fixture()
|
|
scope = make_scope(capture="c" * 64)
|
|
first = labeled_matrix(scope, labeled, "layer.0", roles="fit")
|
|
again = labeled_matrix(scope, labeled.take(range(48)), "layer.0", roles="fit")
|
|
assert first.dtype == torch.float64 and torch.equal(first, again)
|
|
assert torch.equal(first, labeled.values["layer.0"][labeled.rows(roles="fit")].double())
|
|
assert scope.cache.report()["hits"] == 1
|
|
with pytest.raises(ValueError):
|
|
labeled_matrix(scope, labeled, "layer.0", roles="test")
|
|
held = labeled_matrix(scope, labeled, "layer.0", roles="fit", fold="heldout:0/3")
|
|
assert torch.equal(held, first) and scope.cache.report()["refused"] == 1
|
|
value, outcome = cache_fetch(scope, scope.identity(
|
|
kind="custom", algorithm="a/1", site="s", partition="p", fold="all",
|
|
normalization="none", parameters={}, data="d"), lambda: 1)
|
|
assert (value, outcome) == (1, "miss")
|