mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +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>
223 lines
11 KiB
Python
223 lines
11 KiB
Python
"""Campaign specification and preregistration for numeric geometry preservation (issue #511).
|
|
|
|
The campaign asks one bounded question: does an intervention that moves the
|
|
predictive mean of a next-number distribution to a declared target keep the
|
|
non-target spread (the predictive standard deviation)? Inputs are
|
|
comma-separated integer samples drawn from a normal distribution, rounded half
|
|
to even and clamped to ``[0, 999]``. ``sd`` always denotes the generating
|
|
standard deviation (``sigma_semantics``); variance is never stored.
|
|
|
|
Sequences are generated per partition (``fit``, ``calibration``, ``heldout``)
|
|
and seed block from independent seeds, so whole sequences are split before any
|
|
token activation is collected. The preregistration fixes tolerances, interval
|
|
settings and every arm choice; ``freeze`` adds the calibrated scales and a
|
|
digest before any held-out forward pass. The default model identity pins the
|
|
source checkpoint and its tokenizer to exact 40-hex revisions; a pretrained run
|
|
refuses any campaign whose revisions are missing or not 40-hex.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
import re
|
|
|
|
import torch
|
|
|
|
CAMPAIGN_SCHEMA = "obliteratus.numeric_geometry_campaign"
|
|
PREREG_SCHEMA = "obliteratus.numeric_geometry_preregistration"
|
|
SCHEMA_VERSION = 1
|
|
LO, HI = 0, 999
|
|
SOURCE_SUPPORTED = (300, 500)
|
|
PARTITIONS = ("fit", "calibration", "heldout")
|
|
REVISION = re.compile(r"^[0-9a-f]{40}$")
|
|
|
|
DEFAULT_CAMPAIGN = {
|
|
"schema": CAMPAIGN_SCHEMA, "schema_version": SCHEMA_VERSION,
|
|
# main of meta-llama/Llama-3.2-1B (unchanged since 2024-10-24); the tokenizer
|
|
# files live in the same repository at the same commit.
|
|
"model": {"repository": "meta-llama/Llama-3.2-1B",
|
|
"revision": "4e20de362430cd3b72f300e6b0f18e50e7166e08",
|
|
"tokenizer_revision": "4e20de362430cd3b72f300e6b0f18e50e7166e08", "trust_remote_code": False},
|
|
"runtime": {"device": "cuda", "dtype": "float32", "attention_kernel": "eager"},
|
|
"site": {"layer": 15, "readout": "decoder_layer_output",
|
|
"position": "prediction token (the separator after the last number)"},
|
|
"data": {
|
|
"value_range": [LO, HI], "rounding": "round_half_even_then_clamp",
|
|
"sigma_semantics": "standard_deviation", "separator": ",",
|
|
"context_lengths": [32], "fit_means": [300, 400, 500, 600, 700],
|
|
"base_sd": 100,
|
|
"cells": ([{"source_mean": 300, "target_mean": m, "sd": 100}
|
|
for m in range(350, 701, 50)]
|
|
+ [{"source_mean": 400, "target_mean": 500, "sd": s}
|
|
for s in (20, 50, 150, 200)]),
|
|
"sequences_per_block": {"fit": 20, "calibration": 10, "heldout": 20},
|
|
"seed_blocks": [0, 1, 2, 3, 4], "base_seed": 3179,
|
|
},
|
|
"budget": {"max_forward_passes": 200_000, "max_seconds": 14_400},
|
|
}
|
|
|
|
DEFAULT_PREREGISTRATION = {
|
|
"schema": PREREG_SCHEMA, "schema_version": SCHEMA_VERSION,
|
|
"tolerances": {"mean_abs": 10.0, "sd_abs": 5.0, "min_numeric_mass": 0.5},
|
|
"tolerance_status": ("Editable preregistered choices proposed for this protocol; they are "
|
|
"not established project acceptance defaults or source results."),
|
|
"interval": {"level": 0.95, "n_bootstrap": 1000, "seed": 0,
|
|
"unit": "sequence (positions averaged within a sequence)"},
|
|
"comparisons": ["matched_displacement", "matched_achieved_mean"],
|
|
"arms": {
|
|
"spline": {"kind": "natural_cubic", "knots": "fit_means centroids at base_sd",
|
|
"residual_grid": 512},
|
|
"centroid_chord": {"endpoints": "lowest and highest fit_means centroids"},
|
|
"linear_probe": {"optimizers": ["lbfgs", "adamw"], "primary": "lbfgs",
|
|
"l2": 1e-3, "standardize": True, "lbfgs_max_iter": 200,
|
|
"adamw_steps": 1500, "adamw_lr": 0.05, "tolerance": 1e-9,
|
|
"seed": 0, "target": "generating mean / 1000"},
|
|
"dual_field": {"rank": 2, "ridge": 1e-3, "covariance_l2": 1e-2,
|
|
"bandwidth": 100.0, "normalization": "unit",
|
|
"interpolation": "gaussian kernel ridge over fit means of whitened "
|
|
"centroid differences, projected on the top-rank "
|
|
"eigenbasis, evaluated at the cell midpoint"},
|
|
"random_norm_matched": {"seed": 11},
|
|
"off_manifold": {"tangent_rank": 2, "seed": 13},
|
|
},
|
|
"scales": {"reference_arm": "centroid_chord", "bisection_iterations": 30,
|
|
"max_scale_factor": 4.0},
|
|
"frozen": None,
|
|
}
|
|
|
|
|
|
def canonical_sha256(value):
|
|
text = json.dumps(value, sort_keys=True, allow_nan=False, separators=(",", ":"))
|
|
return hashlib.sha256(text.encode()).hexdigest()
|
|
|
|
|
|
def default_campaign():
|
|
return copy.deepcopy(DEFAULT_CAMPAIGN)
|
|
|
|
|
|
def preregistration_template():
|
|
return copy.deepcopy(DEFAULT_PREREGISTRATION)
|
|
|
|
|
|
def validate_campaign(campaign, *, pretrained=False):
|
|
"""Return ``campaign`` or raise ValueError; ``pretrained`` requires pinned revisions."""
|
|
if campaign.get("schema") != CAMPAIGN_SCHEMA or campaign.get("schema_version") != 1:
|
|
raise ValueError("Campaign schema must be obliteratus.numeric_geometry_campaign v1")
|
|
data = campaign["data"]
|
|
if data.get("sigma_semantics") != "standard_deviation":
|
|
raise ValueError("sigma_semantics must be 'standard_deviation'")
|
|
if list(data.get("value_range", [])) != [LO, HI]:
|
|
raise ValueError("value_range must be [0, 999]")
|
|
if len(data["fit_means"]) < 3 or sorted(data["fit_means"]) != list(data["fit_means"]):
|
|
raise ValueError("fit_means must be at least three increasing values")
|
|
if not data["cells"] or not data["seed_blocks"] or min(data["context_lengths"]) < 2:
|
|
raise ValueError("cells, seed_blocks and context_lengths >= 2 are required")
|
|
for cell in data["cells"]:
|
|
for key in ("source_mean", "target_mean"):
|
|
if not LO <= cell[key] <= HI:
|
|
raise ValueError(f"cell {key} outside [0, 999]: {cell}")
|
|
if cell["sd"] <= 0 or cell["source_mean"] == cell["target_mean"]:
|
|
raise ValueError(f"cell needs sd > 0 and distinct means: {cell}")
|
|
if min(data["sequences_per_block"][p] for p in PARTITIONS) < 2:
|
|
raise ValueError("every partition needs at least two sequences per block")
|
|
if pretrained:
|
|
model = campaign["model"]
|
|
for key in ("revision", "tokenizer_revision"):
|
|
if not isinstance(model.get(key), str) or not REVISION.fullmatch(model[key]):
|
|
raise ValueError(f"Pretrained campaign requires a pinned 40-hex model.{key}")
|
|
if model.get("trust_remote_code"):
|
|
raise ValueError("trust_remote_code must be false")
|
|
return campaign
|
|
|
|
|
|
def sample_values(mean, sd, length, generator):
|
|
"""Rounded (half to even) and clamped integer samples of N(mean, sd)."""
|
|
raw = mean + sd * torch.randn(length, generator=generator, dtype=torch.float64)
|
|
return torch.round(raw).clamp(LO, HI).to(torch.int64).tolist()
|
|
|
|
|
|
def _seed(base, *parts):
|
|
digest = hashlib.sha256(json.dumps([base, *parts]).encode()).hexdigest()
|
|
return int(digest[:15], 16)
|
|
|
|
|
|
def cell_label(cell, fit_means):
|
|
lo, hi = SOURCE_SUPPORTED
|
|
supported = lo <= cell["source_mean"] <= hi and lo <= cell["target_mean"] <= hi
|
|
target = cell["target_mean"]
|
|
support = ("fit_mean" if target in fit_means else
|
|
"interpolation" if fit_means[0] <= target <= fit_means[-1] else "extrapolation")
|
|
return {"regime": "source_supported" if supported else "exploratory",
|
|
"support": support,
|
|
"exploratory": not supported or support == "extrapolation"}
|
|
|
|
|
|
def cell_id(cell):
|
|
return f"m{cell['source_mean']}-to-m{cell['target_mean']}-sd{cell['sd']}"
|
|
|
|
|
|
def build_sequences(campaign):
|
|
"""Whole sequences per partition and seed block, split before any activation exists."""
|
|
data = campaign["data"]
|
|
length = max(data["context_lengths"])
|
|
groups = {"fit": [(m, data["base_sd"]) for m in data["fit_means"]],
|
|
"calibration": sorted({(c["source_mean"], c["sd"]) for c in data["cells"]}),
|
|
"heldout": sorted({(c["source_mean"], c["sd"]) for c in data["cells"]})}
|
|
out = {p: [] for p in PARTITIONS}
|
|
for partition in PARTITIONS:
|
|
for mean, sd in groups[partition]:
|
|
for block in data["seed_blocks"]:
|
|
for i in range(data["sequences_per_block"][partition]):
|
|
seed = _seed(data["base_seed"], partition, mean, sd, block, i)
|
|
values = sample_values(mean, sd, length, torch.Generator().manual_seed(seed))
|
|
out[partition].append({
|
|
"sequence_id": f"{partition}-m{mean}-sd{sd}-b{block}-{i}",
|
|
"partition": partition, "seed_block": block, "seed": seed,
|
|
"generating": {"mean": mean, "sd": sd}, "values": values})
|
|
return out
|
|
|
|
|
|
def prompts(sequences, context_lengths, separator=","):
|
|
"""One prompt per sequence and context length; number index kept beside the text."""
|
|
rows = []
|
|
for seq in sequences:
|
|
for n in context_lengths:
|
|
prefix = torch.tensor(seq["values"][:n], dtype=torch.float64)
|
|
rows.append({"sequence_id": seq["sequence_id"], "seed_block": seq["seed_block"],
|
|
"number_index": n, "values": seq["values"][:n],
|
|
"text": separator.join(map(str, seq["values"][:n])) + separator,
|
|
"generating": seq["generating"],
|
|
"empirical": {"mean": float(prefix.mean()),
|
|
"sd": float(prefix.std(correction=1))}})
|
|
return rows
|
|
|
|
|
|
def freeze(preregistration, campaign, calibration):
|
|
"""Preregistration with calibrated scales and digest, fixed before held-out results."""
|
|
if preregistration.get("frozen") is not None:
|
|
raise ValueError("Preregistration is already frozen")
|
|
body = copy.deepcopy(preregistration)
|
|
body["campaign_sha256"] = canonical_sha256(campaign)
|
|
body["calibrated_scales"] = calibration
|
|
body["frozen"] = {"sha256": canonical_sha256(body), "heldout_forward_passes": 0}
|
|
return body
|
|
|
|
|
|
def verify_frozen(preregistration):
|
|
"""Raise unless the preregistration is frozen and unchanged since freezing."""
|
|
frozen = preregistration.get("frozen")
|
|
if not frozen:
|
|
raise ValueError("Preregistration must be frozen before held-out evaluation")
|
|
body = {**preregistration, "frozen": None}
|
|
if canonical_sha256(body) != frozen["sha256"]:
|
|
raise ValueError("Frozen preregistration changed after freezing")
|
|
return frozen["sha256"]
|
|
|
|
|
|
__all__ = ["CAMPAIGN_SCHEMA", "DEFAULT_CAMPAIGN", "DEFAULT_PREREGISTRATION", "PREREG_SCHEMA",
|
|
"SOURCE_SUPPORTED", "build_sequences", "canonical_sha256", "cell_id", "cell_label",
|
|
"default_campaign", "freeze", "preregistration_template", "prompts",
|
|
"sample_values", "validate_campaign", "verify_frozen"]
|