Freeze GINO two-keyword patch, CPU acceptance and runtime prerequisite

This commit is contained in:
NewMeltAssist committed 2026-11-19 14:50:32 +08:00
1 parent 1a9ebf278f
commit fa95ea8251
12 files changed
+2002

No files matched your search

@@ -0,0 +1,194 @@
"""CPU accept the pinned paper environment, then bind the measurement prerequisite."""
import argparse
from copy import deepcopy
import hashlib
from importlib import metadata
from pathlib import Path
import os
import sys
from tempfile import TemporaryDirectory
from unittest.mock import patch
from zipfile import ZipFile
import track_b_gino_import_checked_entry as entry
import track_b_gino_paper_keyword_patch as compatibility
import track_b_gino_shapenet_entry as legacy
from track_b_gino_shapenet_worker import paper_model, test_environment
BASE_CONTRACT = legacy.MEASUREMENT_ROOT / "contract.json"
ACCEPTANCE = entry.ROOT / "cpu-environment-acceptance.json"
def accept():
if os.environ.get("CUDA_VISIBLE_DEVICES") != "":
raise PermissionError("The acceptance interpreter must have an empty CUDA mask")
c = legacy.contract(legacy.checked(BASE_CONTRACT, entry.ORIGINAL_CONTRACT))
c["paper_compatibility"] = compatibility.patch_binding()
patch = compatibility.patch_identity(c)
python = Path(sys.executable).absolute()
prefix = python.parent.parent
if str(python) != c["environment"]["python"] or Path(sys.prefix).absolute() != prefix:
raise PermissionError("Use the exact isolated interpreter in the measurement contract")
cfg = prefix / "pyvenv.cfg"
if "include-system-site-packages = false" not in cfg.read_text():
raise PermissionError("System Python packages must be excluded")
test_environment(c)
import numpy as np
import torch
torch.set_num_threads(1)
torch.set_num_interop_threads(1)
torch.manual_seed(0)
learning_case = c["split"]["train"][0]
if learning_case in c["split"]["test"]:
raise PermissionError("Acceptance must only open a learning case")
audit = legacy.read(c["mapping_audit"]["path"])
row = next(r for r in audit["rows"] if r["case"] == learning_case)
if row["split"] != "train":
raise PermissionError("Source audit must identify the same learning role")
with TemporaryDirectory(prefix="track-b-paper-import-") as temporary:
root = Path(temporary)
compatibility.extract_patched_paper_code(c, root)
from neuralop.utils import UnitGaussianNormalizer
from src.data.cfd_datamodule import CFDSDFDataModule
from src.losses import LpLoss
# Exercise the original loader on exactly one audited learning case.
local = root / "data"
local.mkdir()
with ZipFile(c["paper_data"]["path"]) as archive:
for field, name in (("mesh", f"mesh_{learning_case:03d}.ply"),
("pressure", f"press_{learning_case:03d}.npy")):
payload = archive.read(f"car-pressure-data/data/{name}")
if hashlib.sha256(payload).hexdigest() != row[field + "_sha256"]:
raise ValueError("Learning file differs from the paper mapping audit")
(local / name).write_bytes(payload)
loader = object.__new__(CFDSDFDataModule)
pressure = loader.load_pressure(root, learning_case)
mesh_path = local / f"mesh_{learning_case:03d}.ply"
axis = [np.linspace(a, b, 2) for a, b in zip(*c["split"]["raw_global_bounds"])]
grid = np.stack(np.meshgrid(*axis, indexing="ij"), axis=-1).astype(np.float32)
sdf, vertices, _ = loader.sdf_vertices_closest_from_mesh(mesh_path, grid, False)
if (pressure.shape != (3586,) or vertices.shape != (3586, 3) or sdf.shape != (2, 2, 2)
or not np.isfinite(sdf).all()
or hashlib.sha256(np.asarray(pressure, dtype="<f4").tobytes()).hexdigest() != row["retained_pressure_f32_sha256"]):
raise ValueError("Original loader support mismatch on the learning case")
exact = paper_model(c)
if any(parameter.device.type != "cpu" for parameter in exact.parameters()):
raise ValueError("Full protocol decoder must instantiate on CPU")
parameters = sum(parameter.numel() for parameter in exact.parameters())
del exact
# A reduced, synthetic sample exercises the same decoder implementation
# and native radius/segment extensions without timing or fitting it.
smoke = deepcopy(c)
smoke["protocol"].update(grid=[8, 8, 8], modes=[4, 4, 4])
model = paper_model(smoke).eval()
coordinates = torch.linspace(-1, 1, 8)
positions = torch.stack(torch.meshgrid(coordinates, coordinates, coordinates, indexing="ij"))
inputs = torch.cat((positions, torch.zeros(1, 8, 8, 8))).unsqueeze(0)
points = torch.tensor([[[-1., -1., -1.], [1., 1., 1.], [-1., 1., -1.]]])
with torch.no_grad():
output = model(inputs, points)
if tuple(output.shape) != (1, 1, 3) or output.device.type != "cpu" or not bool(torch.isfinite(output).all()):
raise ValueError(f"Original CPU decoder forward shape or values: {tuple(output.shape)}")
reference = torch.tensor([[3., 4.]])
estimate = torch.tensor([[0., 4.]])
encoder = UnitGaussianNormalizer(torch.tensor([[1., 3.], [2., 4.]]),
eps=c["protocol"]["pressure_epsilon"], reduce_dim=[0, 1], verbose=False)
true_decoded = encoder.decode(encoder.encode(reference.clone()))
pred_decoded = encoder.decode(encoder.encode(estimate.clone()))
original_metric = float(LpLoss(size_average=True)(pred_decoded, true_decoded))
entry_metric = float(torch.linalg.vector_norm(pred_decoded - true_decoded) / torch.linalg.vector_norm(true_decoded))
if abs(original_metric - .6) > 1e-6 or abs(entry_metric - original_metric) > 1e-6:
raise ValueError("Decoded relative L2 differs from the original paper metric")
entry.ROOT.mkdir(exist_ok=True)
if ACCEPTANCE.exists():
raise FileExistsError("The acceptance receipt is immutable")
receipt = dict(
status="PASS", environment_ruling="h-0d21f3bd",
original_contract_sha256=entry.ORIGINAL_CONTRACT,
comparison_contract_sha256=c["comparison_contract_sha256"],
interpreter_path=str(python), interpreter_resolved_path=str(python.resolve(strict=True)),
interpreter_sha256=legacy.sha(python), pyvenv_cfg_sha256=legacy.sha(cfg),
package_versions=c["environment"]["package_versions"], neuralop_commit=c["environment"]["neuralop_commit"],
installed_distributions=sorted((d.metadata["Name"], d.version) for d in metadata.distributions()),
torch_cuda_build=torch.version.cuda, cuda_visible_devices="", scientific_training=False,
paper_compatibility=c["paper_compatibility"], patched_decoder_file_sha256=patch["patched_file_sha256"],
paper_decoder_cpu_forward=True, paper_loader_learning_only=True, paper_metric_cpu=True,
audited_learning_cases=[learning_case], heldout_target_reads=0,
full_decoder_cpu_parameters=parameters, synthetic_decoder_output_shape=list(output.shape),
cpu_metric=entry_metric)
legacy.save(ACCEPTANCE, receipt)
print(f"PASS {legacy.sha(ACCEPTANCE)}")
def gate_replay(path):
c = entry.contract(path)
receipt = entry.accepted_environment(c)
outcomes = {}
for case in ("missing", "bad_receipt_sha", "bad_interpreter_path", "bad_interpreter_sha",
"accepted_receipt_before_review"):
with TemporaryDirectory(prefix="track-b-runtime-gate-") as directory:
temp = Path(directory)
probe = deepcopy(c)
if case == "missing":
probe["environment_acceptance"]["path"] = str(temp / "absent.json")
elif case == "bad_receipt_sha":
probe["environment_acceptance"]["sha256"] = "0" * 64
elif case in ("bad_interpreter_path", "bad_interpreter_sha"):
fake = temp / "wrong-interpreter.json"
field, value = (("interpreter_path", str(temp / "unaccepted-python"))
if case == "bad_interpreter_path" else ("interpreter_sha256", "0" * 64))
legacy.save(fake, dict(receipt, **{field: value}))
probe["environment_acceptance"] = dict(path=str(fake), sha256=legacy.sha(fake))
legacy.save(temp / "contract.json", probe)
with (patch.object(entry, "ROOT", temp),
patch.object(legacy, "validate_review", side_effect=PermissionError("Independent review absent")) as review,
patch.object(entry, "dispatch_guard", side_effect=AssertionError("Can't dispatch without prerequisites")) as dispatch):
try:
entry.run(temp)
except (FileNotFoundError, PermissionError, ValueError) as exc:
outcomes[case] = dict(error=str(exc), passed_to_review=review.call_count)
else:
raise AssertionError(f"Expected the entry to stop: {case}")
if dispatch.call_count or (temp / "started.json").exists():
raise AssertionError("Unverified environment consumed the sole attempt")
if review.call_count != (1 if case == "accepted_receipt_before_review" else 0):
raise AssertionError("CPU acceptance must precede runtime review and start")
return dict(status="PASS", actual_contract_sha256=legacy.sha(path),
cpu_receipt_sha256=c["environment_acceptance"]["sha256"], cases=outcomes,
real_gpu_queries=0, real_workers=0, started_records_written=0)
def bind():
if not ACCEPTANCE.is_file():
raise FileNotFoundError("Actual isolated CPU acceptance must finish first")
old = legacy.contract(legacy.checked(BASE_CONTRACT, entry.ORIGINAL_CONTRACT))
new = deepcopy(old)
new["environment_ruling"] = "h-0d21f3bd"
new["environment_acceptance"] = dict(path=str(ACCEPTANCE), sha256=legacy.sha(ACCEPTANCE))
new["paper_compatibility"] = compatibility.patch_binding()
for p in (entry.__file__, entry.WORKER, __file__, compatibility.__file__):
new["code_sha256"][str(Path(p).resolve())] = legacy.sha(p)
entry.accepted_environment(new)
path = entry.ROOT / "contract.json"
if path.exists():
raise FileExistsError("Contract is already frozen")
legacy.save(path, new)
proof = gate_replay(path)
legacy.save(entry.ROOT / "environment-prerequisite-cpu-replay.json", proof)
print(proof)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("mode", choices=("accept", "bind"))
args = parser.parse_args()
{"accept": accept, "bind": bind}[args.mode]()
+113
View File
@@ -0,0 +1,113 @@
"""Add the reviewed CPU acceptance prerequisite to the fixed measurement entry."""
import argparse
import fcntl
from pathlib import Path
import os
import signal
from types import FunctionType
import track_b_gino_shapenet_entry as legacy
from track_b_gino_paper_keyword_patch import patch_identity
from track_b_gino_shapenet_entry import checked, read, save, sha, process_identity
ROOT = legacy.MEASUREMENT_ROOT.with_name("track-b-gino-shapenet611-import-checked-2026-11-19")
ORIGINAL_CONTRACT = "139fec49b7e4627b55ad12f3a129bec75ecc2446811c23a24f376dae207094aa"
WORKER = Path(__file__).with_name("track_b_gino_import_checked_worker.py").resolve()
def contract(path):
c = legacy.contract(path)
if c["mode"] != "measurement" or c["environment_ruling"] != "h-0d21f3bd":
raise PermissionError("Only the reviewed measurement environment delta is supported")
return c
def accepted_environment(c):
patch = patch_identity(c)
pin = c["environment_acceptance"]
receipt = read(checked(pin["path"], pin["sha256"]))
python = Path(c["environment"]["python"])
cfg = python.parent.parent / "pyvenv.cfg"
if (receipt["status"] != "PASS" or receipt["environment_ruling"] != c["environment_ruling"]
or receipt["original_contract_sha256"] != ORIGINAL_CONTRACT
or receipt["comparison_contract_sha256"] != c["comparison_contract_sha256"]
or receipt["paper_compatibility"] != c["paper_compatibility"]
or receipt["patched_decoder_file_sha256"] != patch["patched_file_sha256"]
or receipt["cuda_visible_devices"] != "" or receipt["scientific_training"] is not False
or receipt["heldout_target_reads"] != 0 or receipt["paper_decoder_cpu_forward"] is not True
or receipt["paper_loader_learning_only"] is not True or receipt["paper_metric_cpu"] is not True
or receipt["audited_learning_cases"] != [c["split"]["train"][0]]
or receipt["package_versions"] != c["environment"]["package_versions"]
or receipt["neuralop_commit"] != c["environment"]["neuralop_commit"]
or receipt["torch_cuda_build"] != c["environment"]["torch_cuda"]
or receipt["interpreter_path"] != str(python)
or receipt["interpreter_resolved_path"] != str(python.resolve(strict=True))
or receipt["interpreter_sha256"] != sha(python)
or receipt["pyvenv_cfg_sha256"] != sha(cfg)
or not os.access(python, os.X_OK)):
raise PermissionError("Matching CPU acceptance and isolated interpreter are required")
return receipt
def dispatch_guard(root, path):
def supervise(command, *args, **kwargs):
if Path(command[1]).name != "track_b_gino_shapenet_worker.py":
raise RuntimeError("Unexpected source worker route")
# The original guard's monitor closes over this exact command list.
# Select the checked wrapper before the shared supervisor can spawn.
command[1] = str(WORKER)
return legacy.supervise(command, *args, **kwargs)
namespace = dict(legacy.dispatch_guard.__globals__, contract=contract, supervise=supervise)
return FunctionType(legacy.dispatch_guard.__code__, namespace, legacy.dispatch_guard.__name__)(root, path)
def authorized_child(root):
root = Path(root).resolve()
if root != ROOT:
raise PermissionError("Only the new fixed measurement root")
c = contract(root / "contract.json")
accepted_environment(c)
namespace = dict(legacy.authorized_child.__globals__, MEASUREMENT_ROOT=ROOT, contract=contract)
return FunctionType(legacy.authorized_child.__code__, namespace, legacy.authorized_child.__name__)(root)
def run(root):
root = Path(root).resolve()
if root != ROOT:
raise PermissionError("Only the new fixed measurement root")
path = root / "contract.json"
c = contract(path)
accepted_environment(c)
legacy.validate_review(root)
authorization = read(root / "stage-authorization.json")
if (authorization["contract_sha256"] != sha(path) or authorization["decision"] != "APPROVED"
or authorization["mode"] != "measurement"):
raise PermissionError("Exact stage authorization is required")
with (root / "entry.lock").open("a") as lock:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
if (root / "started.json").exists() or (legacy.MEASUREMENT_ROOT / "started.json").exists():
raise FileExistsError("The measurement attempt already has a start record")
environment = accepted_environment(c)
save(root / "started.json", dict(
coordinator=process_identity(os.getpid()), contract_sha256=sha(path),
cpu_acceptance_sha256=c["environment_acceptance"]["sha256"],
interpreter_path=environment["interpreter_path"], interpreter_sha256=environment["interpreter_sha256"]))
def request_stop(signum, _frame):
raise KeyboardInterrupt(signal.Signals(signum).name)
handler = signal.signal(signal.SIGTERM, request_stop)
try:
return dispatch_guard(root, path)
finally:
signal.signal(signal.SIGTERM, handler)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("root", type=Path)
args = parser.parse_args()
raise SystemExit(run(args.root))
@@ -0,0 +1,21 @@
"""Route the verified measurement to the original scientific worker."""
import argparse
from pathlib import Path
import sys
from types import FunctionType
from track_b_gino_import_checked_entry import authorized_child
from track_b_gino_paper_keyword_patch import extract_patched_paper_code
from track_b_gino_shapenet_worker import work
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("root", type=Path)
root = parser.parse_args().root.resolve()
c = authorized_child(root)
if str(Path(sys.executable).absolute()) != c["environment"]["python"]:
raise PermissionError("The worker did not start with the accepted interpreter")
namespace = dict(work.__globals__, extract_paper_code=extract_patched_paper_code)
FunctionType(work.__code__, namespace, work.__name__)(root, c)
+49
View File
@@ -0,0 +1,49 @@
"""Apply the two reviewed keyword names only to a verified temporary unpack."""
from pathlib import Path
from track_b_gino_shapenet_entry import BASE, checked, read, sha
from track_b_gino_shapenet_worker import extract_paper_code
EVIDENCE = BASE / "artifacts/track-b-paper-keyword-compatibility-2026-11-19"
PATCH_SHA = "7307ba49f83bfc46c5decf803a993a41ed602d987de65714958e738d2f52f077"
SEARCH_SHA = "77a897bf534a9a0e9e6a7c072b4f914168648eadc2af3493b70d63d0b715b3d0"
def patch_binding():
return dict(handoff="h-b8508140", identity="supplement code + 2-keyword patch",
patch_path=str(EVIDENCE / "two-keyword-patch.json"), patch_sha256=PATCH_SHA,
search_path=str(EVIDENCE / "keyword-search.json"), search_sha256=SEARCH_SHA)
def patch_identity(c):
pin = c["paper_compatibility"]
if pin != patch_binding():
raise PermissionError("The exact keyword patch and archive search are required")
patch = read(checked(pin["patch_path"], pin["patch_sha256"]))
search = read(checked(pin["search_path"], pin["search_sha256"]))
if (patch["nested_archive_sha256"] != c["paper_code_zip_sha256"]
or search["nested_archive_sha256"] != c["paper_code_zip_sha256"]
or search["neuraloperator_commit"] != c["environment"]["neuralop_commit"]
or search["status"] != "NO_ALTERNATE_INTERFACE"
or search["accepting_function_signatures"] or search["neuraloperator_matches"]):
raise ValueError("Alternative interface audit or source archive identity differs")
return patch
def extract_patched_paper_code(c, directory):
patch = patch_identity(c)
extract_paper_code(c, directory)
target = Path(directory) / patch["target"]
original = checked(target, patch["original_file_sha256"])
checked(target.with_name("neighbor_ops.py"), patch["neighbor_ops_file_sha256"])
source = original.read_bytes()
for before, after in patch["changes"]:
old, new = before.encode("ascii"), after.encode("ascii")
if source.count(old) != 1:
raise ValueError("The original keyword must occur exactly once")
source = source.replace(old, new)
original.write_bytes(source)
if sha(original) != patch["patched_file_sha256"]:
raise ValueError("Supplement decoder differs from the reviewed two-keyword patch")