mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Pin the default campaign's model and tokenizer revisions to 4e20de362430cd3b72f300e6b0f18e50e7166e08, the main commit of meta-llama/Llama-3.2-1B (unchanged since 2024-10-24; the tokenizer files share it), so the opt-in pretrained run (#511) is ready once gated-model access and the opt-in flag are present. The pretrained run stays opt-in behind OBLITERATUS_NUMERIC_GEOMETRY_E2E=1, and campaigns whose revisions are missing or not 40-hex are still refused; tests now clear the pins explicitly to exercise that refusal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
336 lines
17 KiB
Python
336 lines
17 KiB
Python
"""Numeric geometry preservation building blocks: spec, splits, fitting, metrics, hooks (#511)."""
|
|
|
|
import math
|
|
|
|
import pytest
|
|
import torch
|
|
from transformers import LlamaConfig, LlamaForCausalLM
|
|
|
|
from obliteratus.analysis import numeric_geometry_adapter as adapter
|
|
from obliteratus.analysis import numeric_geometry_fit as fit
|
|
from obliteratus.analysis import numeric_geometry_metrics as metrics
|
|
from obliteratus.analysis import numeric_geometry_spec as spec
|
|
from obliteratus.analysis.numeric_geometry_synthetic import (
|
|
ToyNumericModel, rounded_clamped_normal, synthetic_model,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
PIN = "0123456789abcdef0123456789abcdef01234567"
|
|
|
|
|
|
# Campaign specification and splits -----------------------------------------------
|
|
|
|
def test_default_campaign_pins_source_and_refuses_unpinned_pretrained():
|
|
campaign = spec.default_campaign()
|
|
assert campaign["model"]["repository"] == "meta-llama/Llama-3.2-1B"
|
|
assert campaign["data"]["sigma_semantics"] == "standard_deviation"
|
|
assert campaign["runtime"] == {"device": "cuda", "dtype": "float32",
|
|
"attention_kernel": "eager"}
|
|
assert spec.validate_campaign(campaign) is campaign
|
|
assert campaign["model"]["revision"] == "4e20de362430cd3b72f300e6b0f18e50e7166e08"
|
|
assert campaign["model"]["tokenizer_revision"] == "4e20de362430cd3b72f300e6b0f18e50e7166e08"
|
|
assert spec.validate_campaign(campaign, pretrained=True) is campaign
|
|
campaign["model"].update(revision=None)
|
|
with pytest.raises(ValueError, match="pinned 40-hex model.revision"):
|
|
spec.validate_campaign(campaign, pretrained=True)
|
|
campaign["model"].update(revision=PIN, tokenizer_revision="main")
|
|
with pytest.raises(ValueError, match="pinned 40-hex model.tokenizer_revision"):
|
|
spec.validate_campaign(campaign, pretrained=True)
|
|
campaign["model"].update(revision=PIN, tokenizer_revision=PIN)
|
|
assert spec.validate_campaign(campaign, pretrained=True)
|
|
campaign["model"]["trust_remote_code"] = True
|
|
with pytest.raises(ValueError, match="trust_remote_code"):
|
|
spec.validate_campaign(campaign, pretrained=True)
|
|
|
|
|
|
@pytest.mark.parametrize("edit, message", [
|
|
(lambda c: c["data"].update(sigma_semantics="variance"), "sigma_semantics"),
|
|
(lambda c: c["data"].update(value_range=[0, 99]), "value_range"),
|
|
(lambda c: c["data"].update(fit_means=[500, 300, 700]), "fit_means"),
|
|
(lambda c: c["data"].update(cells=[]), "cells"),
|
|
(lambda c: c["data"]["cells"].append({"source_mean": 300, "target_mean": 1200, "sd": 9}),
|
|
"outside"),
|
|
(lambda c: c["data"]["cells"].append({"source_mean": 300, "target_mean": 300, "sd": 9}),
|
|
"distinct"),
|
|
(lambda c: c["data"]["sequences_per_block"].update(heldout=1), "two sequences"),
|
|
(lambda c: c.update(schema="other"), "schema"),
|
|
])
|
|
def test_campaign_validation_refuses_ambiguous_protocols(edit, message):
|
|
campaign = spec.default_campaign()
|
|
edit(campaign)
|
|
with pytest.raises(ValueError, match=message):
|
|
spec.validate_campaign(campaign)
|
|
|
|
|
|
def test_samples_are_rounded_half_even_and_clamped():
|
|
values = spec.sample_values(990, 100, 400, torch.Generator().manual_seed(0))
|
|
assert all(isinstance(v, int) and 0 <= v <= 999 for v in values)
|
|
assert values.count(999) > 100
|
|
assert torch.round(torch.tensor([0.5, 1.5, 2.5])).tolist() == [0.0, 2.0, 2.0]
|
|
low = spec.sample_values(3, 50, 400, torch.Generator().manual_seed(1))
|
|
assert low.count(0) > 100
|
|
|
|
|
|
def test_whole_sequences_are_split_before_any_activation():
|
|
campaign = spec.default_campaign()
|
|
campaign["data"]["sequences_per_block"] = {"fit": 2, "calibration": 2, "heldout": 2}
|
|
campaign["data"]["seed_blocks"] = [0, 1]
|
|
seqs = spec.build_sequences(campaign)
|
|
ids = [s["sequence_id"] for part in seqs.values() for s in part]
|
|
seeds = [s["seed"] for part in seqs.values() for s in part]
|
|
assert len(ids) == len(set(ids)) and len(seeds) == len(set(seeds))
|
|
values = {p: {tuple(s["values"]) for s in part} for p, part in seqs.items()}
|
|
assert not values["calibration"] & values["heldout"]
|
|
assert {s["partition"] for s in seqs["heldout"]} == {"heldout"}
|
|
assert spec.build_sequences(campaign) == seqs
|
|
rows = spec.prompts(seqs["heldout"][:1], [4, 8])
|
|
assert [r["number_index"] for r in rows] == [4, 8]
|
|
assert rows[0]["text"].endswith(",") and rows[0]["text"].count(",") == 4
|
|
assert {r["sequence_id"] for r in rows} == {seqs["heldout"][0]["sequence_id"]}
|
|
assert rows[0]["generating"] == seqs["heldout"][0]["generating"]
|
|
assert rows[0]["empirical"]["mean"] == pytest.approx(sum(rows[0]["values"]) / 4)
|
|
|
|
|
|
def test_cell_labels_mark_source_regime_and_extrapolation():
|
|
fit_means = [300, 400, 500, 600, 700]
|
|
assert spec.cell_label({"source_mean": 300, "target_mean": 450}, fit_means) == {
|
|
"regime": "source_supported", "support": "interpolation", "exploratory": False}
|
|
assert spec.cell_label({"source_mean": 300, "target_mean": 650}, fit_means)["exploratory"]
|
|
assert spec.cell_label({"source_mean": 300, "target_mean": 800},
|
|
fit_means)["support"] == "extrapolation"
|
|
assert spec.cell_label({"source_mean": 300, "target_mean": 500},
|
|
fit_means)["support"] == "fit_mean"
|
|
|
|
|
|
def test_freeze_fixes_the_preregistration_and_detects_edits():
|
|
campaign, prereg = spec.default_campaign(), spec.preregistration_template()
|
|
with pytest.raises(ValueError, match="must be frozen"):
|
|
spec.verify_frozen(prereg)
|
|
frozen = spec.freeze(prereg, campaign, {"cell": {"spline": {"scale": 1.0}}})
|
|
assert frozen["frozen"]["heldout_forward_passes"] == 0
|
|
assert spec.verify_frozen(frozen) == frozen["frozen"]["sha256"]
|
|
assert frozen["campaign_sha256"] == spec.canonical_sha256(campaign)
|
|
with pytest.raises(ValueError, match="already frozen"):
|
|
spec.freeze(frozen, campaign, {})
|
|
frozen["tolerances"]["sd_abs"] = 50.0
|
|
with pytest.raises(ValueError, match="changed after freezing"):
|
|
spec.verify_frozen(frozen)
|
|
assert "not established project acceptance defaults" in prereg["tolerance_status"]
|
|
|
|
|
|
# Fitted geometry ---------------------------------------------------------------
|
|
|
|
def test_spline_interpolates_knots_and_extrapolates_linearly():
|
|
knots = [0.0, 1.0, 2.0, 3.0]
|
|
values = torch.tensor([[math.cos(t), math.sin(t)] for t in knots], dtype=torch.float64)
|
|
spline = fit.NaturalCubicSpline(knots, values)
|
|
for t, v in zip(knots, values):
|
|
assert torch.allclose(spline(t), v)
|
|
assert torch.allclose(spline(4.0), spline(3.0) + spline.derivative(3.0))
|
|
assert torch.allclose(spline(-1.0), spline(0.0) - spline.derivative(0.0))
|
|
line = fit.NaturalCubicSpline([0, 1, 2], torch.tensor([[0.], [2.], [4.]],
|
|
dtype=torch.float64))
|
|
assert float(line(1.5)) == pytest.approx(3.0) and float(line.derivative(0.5)) == \
|
|
pytest.approx(2.0)
|
|
variation = fit.tangent_variation(spline)
|
|
assert len(variation["knot_angles_degrees"]) == 3 and variation["max_degrees"] > 0
|
|
assert "not a curvature estimate" in variation["note"]
|
|
assert spline.sample(5).shape == (5, 2)
|
|
|
|
|
|
def test_probe_optimizers_fit_the_same_ridge_objective():
|
|
gen = torch.Generator().manual_seed(0)
|
|
x = torch.randn(200, 4, generator=gen, dtype=torch.float64)
|
|
y = x @ torch.tensor([1.0, -2.0, 0.0, 0.5], dtype=torch.float64) + 3.0
|
|
cfg = {"l2": 1e-6, "standardize": False, "lbfgs_max_iter": 200, "adamw_steps": 3000,
|
|
"adamw_lr": 0.05, "tolerance": 1e-12, "seed": 0}
|
|
lbfgs, adamw = fit.fit_probe(x, y, "lbfgs", **cfg), fit.fit_probe(x, y, "adamw", **cfg)
|
|
for probe in (lbfgs, adamw):
|
|
assert torch.allclose(probe["weights"], torch.tensor([1.0, -2.0, 0.0, 0.5],
|
|
dtype=torch.float64), atol=1e-3)
|
|
assert probe["bias"] == pytest.approx(3.0, abs=1e-3) and probe["seconds"] >= 0
|
|
assert lbfgs["converged"] and adamw["iterations"] == 3000
|
|
scaled = fit.fit_probe(x * 10, y, "lbfgs", **{**cfg, "standardize": True})
|
|
assert "standardized" in scaled["objective"]
|
|
assert torch.allclose(scaled["weights"] * 10, lbfgs["weights"], atol=1e-3)
|
|
with pytest.raises(ValueError, match="optimizer"):
|
|
fit.fit_probe(x, y, "sgd", **cfg)
|
|
|
|
|
|
def test_dual_field_distances_and_off_manifold_direction():
|
|
gen = torch.Generator().manual_seed(0)
|
|
keys = [0, 1, 2, 3]
|
|
cents = torch.tensor([[k, 0.0, 0.0] for k in keys], dtype=torch.float64)
|
|
acts = torch.cat([c + 0.1 * torch.randn(20, 3, generator=gen, dtype=torch.float64)
|
|
for c in cents])
|
|
means = [k for k in keys for _ in range(20)]
|
|
dual = fit.DualField(acts, means, keys, cents, rank=1, ridge=1e-3, covariance_l2=1e-2,
|
|
bandwidth=1.0, normalization="unit")
|
|
assert abs(float(dual.direction(1.5)[0])) == pytest.approx(1.0, abs=5e-3)
|
|
assert dual.record["rank"] == 1 and len(dual.record["eigenbasis_singular_values"]) == 3
|
|
with pytest.raises(ValueError, match="normalization"):
|
|
fit.DualField(acts, means, keys, cents, rank=1, ridge=1e-3, covariance_l2=1e-2,
|
|
bandwidth=1.0, normalization="none")
|
|
spline = fit.NaturalCubicSpline(keys, cents)
|
|
point = torch.tensor([[1.5, 2.0, 0.0]], dtype=torch.float64)
|
|
assert float(fit.manifold_distance(point, spline.sample(301))[0]) == pytest.approx(2.0)
|
|
plane = torch.tensor([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]], dtype=torch.float64)
|
|
assert float(fit.pca_plane_distance(torch.tensor([[5., 5., 3.]]), plane)[0]) == \
|
|
pytest.approx(3.0)
|
|
direction, span = fit.off_manifold_direction(spline, 1.0, acts[:20], 2, seed=3)
|
|
assert span == 2 and abs(float(direction @ spline.derivative(1.0))) < 1e-9
|
|
assert float(direction.norm()) == pytest.approx(1.0)
|
|
|
|
|
|
def test_centroids_follow_label_order():
|
|
acts = torch.tensor([[1.0], [3.0], [10.0]], dtype=torch.float64)
|
|
keys, cents = fit.centroids(acts, [5, 5, 1])
|
|
assert keys == [1, 5] and cents.flatten().tolist() == [10.0, 2.0]
|
|
|
|
|
|
# Measurements ------------------------------------------------------------------
|
|
|
|
def test_moments_divergences_and_explicit_zero_handling():
|
|
target = rounded_clamped_normal(500.0, 100.0)
|
|
assert float(target.sum()) == pytest.approx(1.0)
|
|
mean, sd = metrics.moments(target[None])
|
|
assert float(mean) == pytest.approx(500.0, abs=0.01)
|
|
assert float(sd) == pytest.approx(math.sqrt(100 ** 2 + 1 / 12), abs=0.05)
|
|
clamped = rounded_clamped_normal(990.0, 100.0)
|
|
assert float(clamped[-1]) > 0.4 and float(metrics.moments(clamped[None])[0]) < 990
|
|
point = torch.zeros(1, 1000, dtype=torch.float64)
|
|
point[0, 500] = 1.0
|
|
js, kl = metrics.divergences(torch.cat([target[None], point]), target)
|
|
assert float(js[0]) == pytest.approx(0.0, abs=1e-12) and kl[0] == pytest.approx(0.0)
|
|
assert 0 < float(js[1]) <= math.log(2) and kl[1] is None
|
|
|
|
|
|
def test_paired_interval_seed_blocks_and_verdicts():
|
|
values, groups = [1.0, 3.0, 2.0, 2.0, 0.0, 4.0], ["a", "a", "b", "b", "c", "c"]
|
|
interval = metrics.paired_interval(values, groups, level=0.9, n_bootstrap=200, seed=0,
|
|
unit="sequence")
|
|
assert interval["estimate"] == 2.0 and interval["n_sequences"] == 3
|
|
assert interval["low"] == interval["high"] == 2.0
|
|
blocks = metrics.seed_block_sensitivity([1.0, 3.0, 5.0], [0, 0, 1])
|
|
assert blocks == {"per_block": {"0": 2.0, "1": 5.0}, "min": 2.0, "max": 5.0}
|
|
tol = {"mean_abs": 10.0, "sd_abs": 5.0, "min_numeric_mass": 0.5}
|
|
tight = {"low": -1.0, "high": 1.0}
|
|
assert metrics.verdict(tight, tight, 0.9, tol) == ("preserved", [])
|
|
assert metrics.verdict(tight, {"low": 6.0, "high": 9.0}, 0.9, tol)[0] == "not_preserved"
|
|
assert metrics.verdict({"low": -30.0, "high": -20.0}, tight, 0.9, tol)[1] == [
|
|
"target mean missed: interval outside the mean tolerance"]
|
|
assert "numeric mass" in metrics.verdict(tight, tight, 0.2, tol)[1][0]
|
|
assert metrics.verdict(tight, {"low": 3.0, "high": 7.0}, 0.9, tol)[0] == "inconclusive"
|
|
|
|
|
|
# Hooks and readout -------------------------------------------------------------
|
|
|
|
def test_hooks_are_temporary_and_restoration_is_checked():
|
|
model = synthetic_model("preserving")
|
|
prompts = ["300,310,290,", "500,480,"]
|
|
_, _, before = model.readout(prompts, hooked=False)
|
|
acts = model.activations(prompts)
|
|
assert acts.shape == (2, 8) and model.last_positions == [2, 1]
|
|
dist, mass, logits = model.readout(prompts, torch.zeros(8, dtype=torch.float64))
|
|
assert torch.equal(logits, before) and float(mass.min()) > 0.99
|
|
shifted, _, _ = model.readout(prompts, torch.tensor([0, 0, 50.0, 0, 0, 0, 0, 0]))
|
|
assert float(metrics.moments(shifted)[1][0]) > float(metrics.moments(dist)[1][0])
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
with model._hook(torch.tensor([0]), None):
|
|
raise RuntimeError("boom")
|
|
assert model.restoration_check(prompts, before) == {
|
|
"hooks_restored": True, "hook_free_logits_identical": True}
|
|
model.forward_passes = 0
|
|
handle = model.layer.register_forward_hook(lambda m, i, o: o + 1.0)
|
|
with pytest.raises(adapter.HookRestorationError):
|
|
model.restoration_check(prompts, before)
|
|
handle.remove()
|
|
with pytest.raises(adapter.HookRestorationError, match="not restored"):
|
|
with model._hook(torch.tensor([0]), None):
|
|
model.layer.register_forward_hook(lambda m, i, o: None)
|
|
with pytest.raises(ValueError, match="one distinct id"):
|
|
adapter.HookedNumericModel(model.model, model.layer, None, [0] * 1000)
|
|
with pytest.raises(ValueError, match="variant"):
|
|
ToyNumericModel("other")
|
|
|
|
|
|
class DigitTokenizer:
|
|
"""Value v -> token id v + 3 (single token); separators and digits otherwise."""
|
|
|
|
def __call__(self, text, add_special_tokens=True):
|
|
ids = [1] if add_special_tokens else []
|
|
for part in text.split(","):
|
|
if part:
|
|
ids += [int(part) + 3, 2]
|
|
return {"input_ids": ids if text.endswith(",") else ids[:-1]}
|
|
|
|
|
|
def tiny_llama():
|
|
torch.manual_seed(0)
|
|
return LlamaForCausalLM(LlamaConfig(vocab_size=1010, hidden_size=16, intermediate_size=32,
|
|
num_hidden_layers=2, num_attention_heads=2,
|
|
num_key_value_heads=2)).eval()
|
|
|
|
|
|
def test_tokenizer_readout_on_a_tiny_local_llama():
|
|
tok = DigitTokenizer()
|
|
ids = adapter.numeric_token_ids(tok)
|
|
assert ids[:2] == [3, 4] and ids[-1] == 1002
|
|
llama = tiny_llama()
|
|
model = adapter.HookedNumericModel(llama, llama.model.layers[1],
|
|
adapter.tokenizer_encoder(tok), ids)
|
|
prompts = ["300,310,", "5,"]
|
|
dist, mass, before = model.readout(prompts, hooked=False)
|
|
assert model.last_positions == [4, 2]
|
|
assert torch.allclose(dist.sum(1), torch.ones(2, dtype=torch.float64))
|
|
assert 0 < float(mass.max()) < 1
|
|
acts = model.activations(prompts)
|
|
_, _, moved = model.readout(prompts, 5 * acts[0] / acts[0].norm())
|
|
assert not torch.equal(moved, before)
|
|
assert model.restoration_check(prompts, before)["hook_free_logits_identical"]
|
|
|
|
class Split(DigitTokenizer):
|
|
def __call__(self, text, add_special_tokens=True):
|
|
return {"input_ids": [1, 2] if text == "17" else [int(text) % 5]}
|
|
with pytest.raises(ValueError, match="not a single token"):
|
|
adapter.numeric_token_ids(Split())
|
|
|
|
class Shared(DigitTokenizer):
|
|
def __call__(self, text, add_special_tokens=True):
|
|
return {"input_ids": [int(text) % 5]}
|
|
with pytest.raises(ValueError, match="share token ids"):
|
|
adapter.numeric_token_ids(Shared())
|
|
|
|
|
|
def test_pretrained_loader_wires_pinned_revisions_without_network(monkeypatch):
|
|
import transformers
|
|
calls = {}
|
|
llama = tiny_llama()
|
|
|
|
class FakeModel:
|
|
@staticmethod
|
|
def from_pretrained(repo, **kwargs):
|
|
calls["model"] = (repo, kwargs)
|
|
return llama
|
|
|
|
class FakeTokenizer:
|
|
@staticmethod
|
|
def from_pretrained(repo, **kwargs):
|
|
calls["tokenizer"] = (repo, kwargs)
|
|
return DigitTokenizer()
|
|
monkeypatch.setattr(transformers, "AutoModelForCausalLM", FakeModel)
|
|
monkeypatch.setattr(transformers, "AutoTokenizer", FakeTokenizer)
|
|
campaign = spec.default_campaign()
|
|
campaign["model"].update(revision=None)
|
|
with pytest.raises(ValueError, match="pinned"):
|
|
adapter.load_pretrained(campaign)
|
|
campaign["model"].update(revision=PIN, tokenizer_revision="f" * 40)
|
|
campaign["runtime"]["device"] = "cpu"
|
|
campaign["site"]["layer"] = 1
|
|
model = adapter.load_pretrained(campaign)
|
|
assert calls["model"][1]["revision"] == PIN and calls["model"][1]["trust_remote_code"] is False
|
|
assert calls["model"][1]["attn_implementation"] == "eager"
|
|
assert calls["tokenizer"][1]["revision"] == "f" * 40
|
|
assert model.layer is llama.model.layers[1]
|
|
assert model.identity["kind"] == "pretrained" and model.identity["layer"] == 1
|