File size: 13,333 Bytes
70ac094 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | """Shared statistics and atlas lookup for the registered zero-GPU analyses.
Used by `scripts/audit_verdicts.py` (AUDIT_REGISTRATION_2026-07-15 §4-5) and
`scripts/certification_tables.py` (ATLAS_MINING_REGISTRATION_2026-07-15 §5).
Statistical model
-----------------
Both analyses rest on the **paired-flip model**. For a paired evaluation of a
baseline and a compressed model on the same n items, the per-item accuracy
difference is d_i in {-1, 0, +1}: -1 when the compression turns a correct answer
wrong (harmful flip), +1 for the reverse (beneficial flip), 0 when both agree.
Writing p_d for the discordance rate (harmful + beneficial flip rate, which the
atlas records as `accuracy_state_churn`), under the null of no true difference
the flips split evenly and
Var(d) = p_d => sd_paired = sqrt(p_d)
The naive alternative treats the two evaluations as independent binomials at
baseline accuracy p, giving Var(p_hat_1 - p_hat_2) * n = 2p(1-p), i.e.
sd_independent = sqrt(2 p (1 - p))
Because real compressed models agree with their baselines on the large majority
of items, p_d is typically far below 2p(1-p) and the paired design needs far
fewer items for the same resolution. Quantifying that gap is the point of the
certification tables, so both columns are always reported side by side.
Reuse and cross-checking
------------------------
`flipeval.core.minimum_detectable_difference` and `required_n_for_effect` are the
project's tested implementations, but they take a per-item delta *array*. The
audit has no per-item data -- only (n, baseline accuracy, imputed discordance) --
so the sd is derived analytically above and fed to the same z-formulas here.
`tests/test_audit_stats.py` pins these against the flipeval functions on
synthetic delta vectors: any divergence is a test failure, not a silent fork.
"""
from __future__ import annotations
import csv
import math
import re
from dataclasses import dataclass
from pathlib import Path
from scipy import stats
ALPHA = 0.05
POWER = 0.80
# Two-sided detection (V1/MDD) vs one-sided-per-bound TOST (V2/required-n).
# TOST rejects both one-sided nulls at alpha each; its sample size therefore uses
# z_{1-alpha}, not z_{1-alpha/2}. See the methods block in the output docs.
Z_TWO_SIDED = float(stats.norm.ppf(1 - ALPHA / 2))
Z_ONE_SIDED = float(stats.norm.ppf(1 - ALPHA))
Z_POWER = float(stats.norm.ppf(POWER))
def paired_flip_sd(discordance: float) -> float:
"""Per-item sd of the paired accuracy delta at discordance rate `discordance`."""
if not 0.0 <= discordance <= 1.0:
raise ValueError(f"discordance must be in [0, 1], got {discordance}")
return math.sqrt(discordance)
def independent_binomial_sd(baseline_accuracy: float) -> float:
"""Per-item sd of the delta if the two runs were independent binomials."""
p = baseline_accuracy
if not 0.0 <= p <= 1.0:
raise ValueError(f"baseline accuracy must be in [0, 1], got {p}")
return math.sqrt(2.0 * p * (1.0 - p))
def minimum_detectable_delta(sd: float, n: int, alpha: float = ALPHA, power: float = POWER) -> float:
"""Smallest true delta detectable at `power`, two-sided `alpha`, given n items."""
if n <= 0:
raise ValueError(f"n must be positive, got {n}")
if sd == 0.0:
return 0.0
z = float(stats.norm.ppf(1 - alpha / 2)) + float(stats.norm.ppf(power))
return z * sd / math.sqrt(n)
def required_n_for_tost(sd: float, margin: float, alpha: float = ALPHA, power: float = POWER) -> int | None:
"""Items needed to conclude equivalence within +/-`margin` via TOST.
Standard TOST sample size with assumed true difference zero:
n = ceil( ((z_{1-alpha} + z_{1-beta}) * sd / margin)^2 )
"""
if margin <= 0:
raise ValueError(f"margin must be positive, got {margin}")
if sd == 0.0:
return 1
z = float(stats.norm.ppf(1 - alpha)) + float(stats.norm.ppf(power))
return int(math.ceil((z * sd / margin) ** 2))
def synthetic_deltas(n: int, discordance: float):
"""A per-item delta vector realising `discordance` with zero net delta.
Lets the analytic sd be cross-checked against flipeval's array-based
functions, which is how `tests/test_audit_stats.py` pins the two together.
"""
import numpy as np
flips_each_way = int(round(n * discordance / 2.0))
deltas = np.zeros(n, dtype=float)
deltas[:flips_each_way] = 1.0
deltas[flips_each_way : 2 * flips_each_way] = -1.0
return deltas
# ---------------------------------------------------------------------------
# Atlas cell loading and the nearest-cell matcher
# ---------------------------------------------------------------------------
# Atlas task name -> benchmark family. The atlas logs MMLU as 57 per-subject
# hendrycksTest cells and BBH/MATH/MuSR/GPQA as per-subtask cells; the audit
# matches at family granularity, so these collapse.
_TASK_FAMILY_PREFIXES = (
("harness_hendrycksTest_", "mmlu"),
("bbh_", "bbh"),
("gpqa_", "gpqa"),
("math_", "math"),
("musr_", "musr"),
)
_TASK_FAMILY_EXACT = {
"mmlu_pro": "mmlu_pro",
"ifeval": "ifeval",
"harness_arc_challenge_25": "arc_challenge",
"harness_hellaswag_10": "hellaswag",
"harness_winogrande_5": "winogrande",
"harness_gsm8k_5": "gsm8k",
"harness_drop_3": "drop",
"harness_truthfulqa_mc_0": "truthfulqa",
}
# Atlas `method` -> (family, bit width). Bit width is the weight precision.
_METHOD_PROFILE = {
"GPTQ": ("gptq", 4),
"AWQ": ("awq", 4),
"W4A16": ("w4a16", 4),
"W8A8-INT8": ("w8a8_int8", 8),
"W8A8-FP8": ("w8a8_fp8", 8),
"bnb-4bit(LoRA)": ("bnb_4bit", 4),
"bnb-8bit(LoRA)": ("bnb_8bit", 8),
}
def benchmark_family(task: str) -> str:
"""Map an atlas task name to its benchmark family."""
if task in _TASK_FAMILY_EXACT:
return _TASK_FAMILY_EXACT[task]
for prefix, family in _TASK_FAMILY_PREFIXES:
if task.startswith(prefix):
return family
return task
def method_profile(method: str) -> tuple[str, int | None]:
"""Map an atlas method label to (method family, bit width)."""
return _METHOD_PROFILE.get(method, (method.lower(), None))
@dataclass(frozen=True)
class AtlasCell:
method_family: str
bits: int | None
benchmark: str
n: int
discordance: float
baseline_accuracy: float
is_probe: bool
def load_atlas_cells(path: str | Path, include_probe: bool = False) -> list[AtlasCell]:
"""Load analysable atlas cells.
Drops rows the atlas already excluded, and -- per
ATLAS_MINING_REGISTRATION §6 -- drops every cell belonging to a disclosed
probe pair unless `include_probe`. Probe pairs are tiny hand-built
sanity pairs (n as low as 10) and would distort an empirical
discordance distribution badly.
"""
cells: list[AtlasCell] = []
with Path(path).open("r", encoding="utf-8", newline="") as f:
for row in csv.DictReader(f):
if row["excluded_or_skipped"].strip().lower() != "false":
continue
is_probe = row["contains_disclosed_probe_cell"].strip().lower() == "true"
if is_probe and not include_probe:
continue
try:
n = int(row["n"])
discordance = float(row["accuracy_state_churn"])
baseline = float(row["baseline_accuracy"])
except (TypeError, ValueError):
continue
family, bits = method_profile(row["method"])
cells.append(AtlasCell(
method_family=family,
bits=bits,
benchmark=benchmark_family(row["task"]),
n=n,
discordance=discordance,
baseline_accuracy=baseline,
is_probe=is_probe,
))
return cells
# Match tiers, most specific first. Each is (label, key function over an
# AtlasCell, key function over the requested target). A tier matches when the
# two keys are equal; `None` in a target field makes that field a wildcard, so a
# claim with no bit width (e.g. a pruning method) simply falls through the
# bit-width tiers instead of matching them spuriously.
MATCH_TIERS = ("family+bits+benchmark", "family+bits", "bits+benchmark", "bits", "benchmark", "global")
def _tier_matches(tier: str, cell: AtlasCell, family: str | None, bits: int | None, benchmark: str | None) -> bool:
if tier == "family+bits+benchmark":
return cell.method_family == family and cell.bits == bits and cell.benchmark == benchmark
if tier == "family+bits":
return cell.method_family == family and cell.bits == bits
if tier == "bits+benchmark":
return cell.bits == bits and cell.benchmark == benchmark
if tier == "bits":
return cell.bits == bits
if tier == "benchmark":
return cell.benchmark == benchmark
if tier == "global":
return True
raise ValueError(f"unknown match tier: {tier}")
@dataclass(frozen=True)
class DiscordanceMatch:
discordance: float
tier: str
n_cells: int
# Every matching cell's discordance, sorted. The point imputation above is
# this sequence's median; AUDIT_REGISTRATION Amendment 2 requires the
# surviving power result to be reported with its reversal point, which
# cannot be computed from the median alone.
values: tuple = ()
def reversal_discordance(n: int, margin: float, alpha: float = ALPHA, power: float = POWER) -> float:
"""The discordance at which a claim of size `n` stops being underpowered.
`required_n_for_tost` is ceil((z*sqrt(d)/margin)**2), which is monotone
increasing in d, so the verdict flips where that expression equals n:
d* = n * margin**2 / z**2
Below d* the claim is adequately powered at `margin`; above it, underpowered.
Integer ceilings make the realised boundary rough by at most one cell of d,
so callers report this as approximate.
"""
if n <= 0:
raise ValueError(f"n must be positive, got {n}")
if margin <= 0:
raise ValueError(f"margin must be positive, got {margin}")
z = float(stats.norm.ppf(1 - alpha)) + float(stats.norm.ppf(power))
return n * margin ** 2 / z ** 2
def nearest_cell_discordance(
cells: list[AtlasCell],
family: str | None,
bits: int | None,
benchmark: str | None,
) -> DiscordanceMatch:
"""Impute a discordance rate from the nearest atlas cells.
Tiers are tried most-specific first and the first non-empty tier wins:
1. family + bits + benchmark exact cell
2. family + bits same method at same precision, any benchmark
3. bits + benchmark same precision on the same benchmark
4. bits same precision anywhere
5. benchmark same benchmark at any precision
6. global all analysable cells
A tier whose target field is None descends rather than being forced into a
wrong cell -- but NOT always, and this docstring used to claim otherwise.
It said `cell.bits == None` is "false for every real cell". That is false
for 183 of them: `bnb-4bit`, `QLoRA-4bit` and `bnb-4bit(DPO-FT)` are absent
from `_METHOD_PROFILE`, so they parse with `bits=None` despite plainly being
4-bit. A claim with no bit width therefore MATCHES them at the `bits` tier,
on the two sides sharing a missing field rather than a real one. R06 and R07
are exactly this case. The behaviour is registered and is not changed here;
it is disclosed in the audit appendix instead. Do not restore the old
sentence. The **median** over matching
cells is used, not the mean: per-cell discordance is right-skewed and a few
high-churn generative cells would otherwise dominate.
"""
for tier in MATCH_TIERS:
matching = [c for c in cells if _tier_matches(tier, c, family, bits, benchmark)]
if matching:
values = sorted(c.discordance for c in matching)
return DiscordanceMatch(_median(values), tier, len(matching), tuple(values))
raise ValueError("no atlas cells available for imputation")
def _median(sorted_values: list[float]) -> float:
if not sorted_values:
raise ValueError("median of empty sequence")
mid = len(sorted_values) // 2
if len(sorted_values) % 2:
return sorted_values[mid]
return (sorted_values[mid - 1] + sorted_values[mid]) / 2.0
def quantiles(values: list[float]) -> dict[str, float]:
"""25th / 50th / 75th percentiles, linear interpolation (numpy default)."""
import numpy as np
array = np.asarray(values, dtype=float)
return {
"p25": float(np.quantile(array, 0.25)),
"median": float(np.quantile(array, 0.50)),
"p75": float(np.quantile(array, 0.75)),
}
def sha256_of(path: str | Path) -> str:
import hashlib
digest = hashlib.sha256()
with Path(path).open("rb") as f:
for chunk in iter(lambda: f.read(1 << 20), b""):
digest.update(chunk)
return digest.hexdigest()
def parse_leading_int(text: str) -> int | None:
"""First standalone integer in `text`, or None. Used for stated-n parsing."""
match = re.search(r"\b(\d{2,7})\b", text.replace(",", ""))
return int(match.group(1)) if match else None
|