Guard Spark reference provenance against recording input changes

This commit is contained in:
Constantin committed 2026-11-03 13:53:26 -05:00
1 parent 6295ba1d92
commit 4a60c64ec5
3 files changed
+85 -14

No files matched your search

+4
View File
@@ -111,6 +111,10 @@ a colon-separated home search path. They replay the same corpus against BF16 onc
candidate with fixed chunking/cache/TP flags. A reference `provenance.json` binds the BF16 model bytes,
corpus, runtime/plugins, settings and recorded data files. `--reuse-reference` requires this binding
to match; legacy unbound references must be recorded afresh, not relabelled with the supplied model.
The harness snapshots inputs before recording, checks them immediately afterward before publishing
provenance, and checks them again before publishing the run receipt. These checks detect persistent
changes, not a concurrent change that is reverted between checks; keep inputs immutable throughout.
A retry removes any previous `run.json` before measurement, so retain old receipts separately.
Reports and per-position score files are hashed into the run receipt as well:
```sh
+28 -14
View File
@@ -83,14 +83,26 @@ def main():
str(ref_dir), "--kld-out", str(out / f"{name}-kld.json")]))
return 0
plugins = plugin_hashes(home)
reference_inputs = {
"reference_model": str(reference), "reference_sha256": sha256(reference),
"corpus": str(corpus), "corpus_sha256": sha256(corpus),
"corpus_fnv1a64": fnv1a64(corpus), "radiance": str(radiance),
"radiance_sha256": sha256(radiance), "plugins": plugins, "arguments": common,
}
out.mkdir(parents=True, exist_ok=True)
# A failed retry must not leave a previous run's receipt looking like its result.
(out / "run.json").unlink(missing_ok=True)
def snapshot_inputs():
return {
"reference_model": str(reference), "reference_sha256": sha256(reference),
"corpus": str(corpus), "corpus_sha256": sha256(corpus),
"corpus_fnv1a64": fnv1a64(corpus), "radiance": str(radiance),
"radiance_sha256": sha256(radiance), "plugins": plugin_hashes(home),
"arguments": list(common),
}
reference_inputs = snapshot_inputs()
plugins = reference_inputs["plugins"]
def verify_inputs():
if snapshot_inputs() != reference_inputs:
raise ValueError("reference inputs changed during recording or scoring")
ref_meta_path = ref_dir / "kld.json"
if args.reuse_reference:
if not ref_meta_path.is_file():
@@ -102,17 +114,21 @@ def main():
ref_dir.mkdir(parents=True)
env = os.environ.copy()
subprocess.run(record, env=env, check=True)
# Check before trusting or binding any output from the recorder.
verify_inputs()
ref_meta = json.loads(ref_meta_path.read_text())
validate_reference(ref_meta, reference, corpus)
# Never publish a binding if the recorder may have observed different input bytes.
verify_inputs()
bound_reference = reference_provenance(ref_dir, reference_inputs, args.reuse_reference)
run = {
"reference_model": str(reference), "reference_sha256": sha256(reference),
"reference_model": str(reference), "reference_sha256": reference_inputs["reference_sha256"],
"reference_dir": str(ref_dir), "reference_form": "top-128 + tail",
"corpus": str(corpus), "corpus_sha256": sha256(corpus),
"corpus_fnv1a64": fnv1a64(corpus), "evaluation_manifest": None,
"radiance": str(radiance), "radiance_sha256": sha256(radiance),
"corpus": str(corpus), "corpus_sha256": reference_inputs["corpus_sha256"],
"corpus_fnv1a64": reference_inputs["corpus_fnv1a64"], "evaluation_manifest": None,
"radiance": str(radiance), "radiance_sha256": reference_inputs["radiance_sha256"],
"radiance_home": str(home), "kernel_selector": args.kernels,
"tp": args.tp, "kv_cache_dtype": "bf16", "max_num_seqs": 1,
"max_model_len": args.max_model_len,
@@ -169,9 +185,7 @@ def main():
f"p99.9={values['p99_9']:.6g}, max={values['max']:.6g}, "
f">1 nat={run['candidates'][name]['positions_over_1_nat']}")
if (plugin_hashes(home) != plugins or sha256(radiance) != run["radiance_sha256"] or
sha256(reference) != run["reference_sha256"]):
p.error("runtime, reference model or plugin inventory changed during the run")
verify_inputs()
reference_provenance(ref_dir, reference_inputs, reuse=True)
(out / "run.json").write_text(json.dumps(run, indent=2) + "\n")
print(f"Run receipt: {out / 'run.json'}")
+53
View File
@@ -97,6 +97,59 @@ class MeasurementTests(unittest.TestCase):
execute = self.record_quality("--reuse-reference")
self.assertEqual(execute.call_count, 1) # score only, no new reference recording
def test_recording_input_mutations_do_not_publish_provenance_or_receipt(self):
paths = (self.reference, self.runtime, self.corpus, self.home / "architectures/fixture.so")
for index, path in enumerate(paths):
with self.subTest(path=path):
self.out = self.root / f"record-mutation-{index}"
original = path.read_bytes()
def mutate_during_record(command, **kwargs):
self.fake_runtime(command, **kwargs)
if "--kld-record" in command:
path.write_bytes(original + b"changed")
try:
with patch.object(sys, "argv", self.quality_args()), \
patch.object(quality.subprocess, "run", side_effect=mutate_during_record) as execute:
with self.assertRaisesRegex(ValueError, "reference inputs changed"):
quality.main()
self.assertEqual(execute.call_count, 1)
self.assertFalse((self.out / "bf16-top128-ref/provenance.json").exists())
self.assertFalse((self.out / "run.json").exists())
finally:
path.write_bytes(original)
def test_scoring_input_mutations_prevent_success_receipt(self):
paths = (self.reference, self.runtime, self.corpus, self.home / "architectures/fixture.so")
for index, path in enumerate(paths):
with self.subTest(path=path):
self.out = self.root / f"score-mutation-{index}"
original = path.read_bytes()
def mutate_during_score(command, **kwargs):
self.fake_runtime(command, **kwargs)
if "--kld-ref" in command:
path.write_bytes(original + b"changed")
try:
with patch.object(sys, "argv", self.quality_args()), \
patch.object(quality.subprocess, "run", side_effect=mutate_during_score), \
contextlib.redirect_stdout(io.StringIO()):
with self.assertRaisesRegex(ValueError, "reference inputs changed"):
quality.main()
self.assertFalse((self.out / "run.json").exists())
finally:
path.write_bytes(original)
def test_failed_retry_removes_previous_success_receipt(self):
self.record_quality()
self.reference.write_bytes(b"changed")
with patch.object(sys, "argv", self.quality_args("--reuse-reference")):
with self.assertRaises(ValueError):
quality.main()
self.assertFalse((self.out / "run.json").exists())
def test_signed_kl_roundoff_is_preserved(self):
self.score_kl = -1e-10
self.record_quality()