Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Tighten prefill evidence checks and make bundle replacement crash-safe
Claude4dfef545634c
diff --git a/scripts/models/bonsai/build_vision.py b/scripts/models/bonsai/build_vision.pyindex 05f0b9ecb..4d40df926 100644--- a/scripts/models/bonsai/build_vision.py+++ b/scripts/models/bonsai/build_vision.py@@ -339,7 +339,7 @@ optional check. {ocr_limit}Apache 2.0; see LICENSE and NOTICE.txt. Original context capacity is 262,144 tokens;WebBrain applies a smaller browser context budget. Model weights and processorfiles are downloaded once; inference runs locally.-""" + model_card_section([], language_graph["output_sha256"]), encoding="utf-8")+""" + model_card_section([], language_graph["output_sha256"], language_graph["chunk_tokens"]), encoding="utf-8")total = seal_manifest(output)print(json.dumps({"output": str(output.resolve()), "vision_tensors": len(records),"total_bytes": total, "vision_inference": validation}), flush=True)diff --git a/scripts/models/bonsai/chunk_gdn_prefill.py b/scripts/models/bonsai/chunk_gdn_prefill.pyindex 6af975b8e..6c2ec23a2 100644--- a/scripts/models/bonsai/chunk_gdn_prefill.py+++ b/scripts/models/bonsai/chunk_gdn_prefill.py@@ -50,7 +50,7 @@ kernel, so prompts were processed one token at a time through a CPU loop witha GPU round trip per token and layer (0.13 s per prompt token for 2B and 0.19 sfor 4B on an RTX 5090; a faster GPU barely helps).This graph replaces each `Scan` with the chunked gated delta rule-(128-token chunks; stable recursive block inversion inside a chunk; a parallel+({chunk}-token chunks; stable recursive block inversion inside a chunk; a parallelprefix scan across chunks), built only from ops the WebGPU EP implements, andreturns logits for the last position only. Weights, quantization and theexternal data file are byte-identical to the previous revision; decode@@ -63,6 +63,11 @@ MAX_RELATIVE_LOGIT_DIFF = 5e-3MAX_ABSOLUTE_LOGIT_DIFF = 0.05MAX_KL = 1e-3MAX_WEBGPU_KL = 1e-2+# Cache outputs (recurrent/conv states, KV) carry FP16 rounding through the+# stack: observed whole-tensor error stays below 1% and the worst single+# element below 3% of the tensor maximum. A corrupted cache is O(1).+MAX_CACHE_REL_FRO = 2e-2+MAX_CACHE_REL_MAX = 1e-1def _require(condition, message):@@ -88,6 +93,12 @@ def verify_prefill_evidence(reports, graph_sha256):"CPU report must cover a chunk boundary and a multi-chunk prompt")for row in rows + [continuation]:_require(row.get("top1_equal") is True, f"top-1 differs ({row})")+ _require(row.get("finite") is True, f"non-finite outputs ({row})")+ _require("worst_cache_rel_fro" in row and "worst_cache_rel_err" in row,+ "CPU report must record cache-output errors (rerun validate_chunked_gdn.py)")+ _require(float(row["worst_cache_rel_fro"]) <= MAX_CACHE_REL_FRO+ and float(row["worst_cache_rel_err"]) <= MAX_CACHE_REL_MAX,+ f"cache outputs diverge ({row})")# Without a recorded scale, allow a few FP16 ulps at typical |logit| of 16-32.tolerance = (MAX_RELATIVE_LOGIT_DIFF * max(float(row["last_logits_scale"]), 1.0)if row.get("last_logits_scale") else MAX_ABSOLUTE_LOGIT_DIFF)@@ -97,8 +108,7 @@ def verify_prefill_evidence(reports, graph_sha256):for row in rows:_require(row.get("decode_step_top1_equal") is True, f"decode step differs ({row})")elif kind == WEBGPU_EVIDENCE_KIND:- if "rewritten_graph_sha256" in report:- _require(report["rewritten_graph_sha256"] == graph_sha256, "WebGPU report is for a different graph")+ _require(report.get("rewritten_graph_sha256") == graph_sha256, "WebGPU report is for a different graph")rewritten = [row for row in report.get("prefill") or [] if row.get("variant") == "rewritten"]original = [row for row in report.get("prefill") or [] if row.get("variant") == "original"]comparisons = list(report.get("webgpu_logit_comparison") or [])@@ -112,9 +122,9 @@ def verify_prefill_evidence(reports, graph_sha256):return verified-def model_card_section(reports, graph_sha256):+def model_card_section(reports, graph_sha256, chunk_tokens=128):verified = verify_prefill_evidence(reports, graph_sha256)- text = MODEL_CARD_INTRO + "\n"+ text = MODEL_CARD_INTRO.format(chunk=chunk_tokens) + "\n"if CPU_EVIDENCE_KIND in verified:text += ("`chunked-prefill-validation.json` records CPU comparisons with the original\n""graph on real text (prefill lengths across chunk boundaries and a cached\n"diff --git a/scripts/models/bonsai/make_chunked_bundle.py b/scripts/models/bonsai/make_chunked_bundle.pyindex eb502d2c7..6330adef0 100644--- a/scripts/models/bonsai/make_chunked_bundle.py+++ b/scripts/models/bonsai/make_chunked_bundle.py@@ -21,6 +21,7 @@ import argparseimport jsonimport shutilimport tempfile+import uuidfrom pathlib import Pathfrom build_vision import digest, link_or_copy, read_json, seal_manifest, write_json@@ -56,11 +57,25 @@ def make_staging(output):def replace_output(staging, output):- """Swap the sealed staging bundle into place; only a previous derived bundle is removed."""+ """Swap the sealed staging bundle into place; only a previous derived bundle is removed.++ The previous bundle is renamed aside first and deleted only after the new+ one is in place, so a locked file (for example a browser still serving the+ old bundle) cannot leave a half-deleted --output.+ """+ backup = Noneif output.exists():check_output(staging, output)- shutil.rmtree(output)- staging.rename(output)+ backup = output.with_name(f".{output.name}.{uuid.uuid4().hex}.old")+ output.rename(backup)+ try:+ staging.rename(output)+ except BaseException:+ if backup is not None:+ backup.rename(output)+ raise+ if backup is not None:+ shutil.rmtree(backup, ignore_errors=True)def build_bundle(source, output, manifest, args):@@ -94,7 +109,7 @@ def build_bundle(source, output, manifest, args):scratch.rename(output / GRAPH)evidence = [read_json(path) for path in args.evidence]- card_section = model_card_section(evidence, report["output_sha256"]) # rejects failed evidence+ card_section = model_card_section(evidence, report["output_sha256"], report["chunk_tokens"]) # rejects failed evidencewrite_json(output / DERIVED_MARKER, {"rewrite": report,"external_data_unchanged": "onnx/decoder_model_merged_q2f16.onnx_data",diff --git a/scripts/models/bonsai/validate_chunked_gdn.py b/scripts/models/bonsai/validate_chunked_gdn.pyindex 3fbd37532..94b5fc3b6 100644--- a/scripts/models/bonsai/validate_chunked_gdn.py+++ b/scripts/models/bonsai/validate_chunked_gdn.py@@ -79,6 +79,8 @@ def compare(a: dict, b: dict) -> dict:lb = b["logits"][:, -1].astype(np.float32)worst_state = 0.0worst_name = ""+ worst_fro = 0.0+ worst_fro_name = ""for k in a:if k == "logits":continue@@ -88,6 +90,10 @@ def compare(a: dict, b: dict) -> dict:err = float(np.abs(x - y).max()) / scaleif err > worst_state:worst_state, worst_name = err, k+ # Whole-tensor error: robust to a single FP16 rounding flip on an outlier.+ fro = float(np.linalg.norm(x - y)) / max(float(np.linalg.norm(x)), 1e-6)+ if fro > worst_fro:+ worst_fro, worst_fro_name = fro, kpa = np.exp(la - la.max()); pa /= pa.sum()pb = np.exp(lb - lb.max()); pb /= pb.sum()return {@@ -101,6 +107,8 @@ def compare(a: dict, b: dict) -> dict:"kl_original_to_rewritten": float((pa * (np.log(pa + 1e-30) - np.log(pb + 1e-30))).sum()),"worst_cache_rel_err": worst_state,"worst_cache_name": worst_name,+ "worst_cache_rel_fro": worst_fro,+ "worst_cache_rel_fro_name": worst_fro_name,"finite": bool(all(np.isfinite(v.astype(np.float32)).all() for v in b.values())),}