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
Parallel WebGPU prefill for Bonsai 2 Vision graphs
Claudede9f83af57e4
diff --git a/scripts/models/bonsai/README.md b/scripts/models/bonsai/README.mdindex 6f280eb06..d0c86211b 100644--- a/scripts/models/bonsai/README.md+++ b/scripts/models/bonsai/README.md@@ -48,6 +48,35 @@ and returns embeddings in the original residual basis. The restored visionembeddings therefore need no extra Hadamard rotation. The decoder already acceptsQwen's three-axis position IDs and hybrid attention caches.+## Parallel prefill graph++The danyalmalik language graphs compute GatedDeltaNet prefill with an ONNX+`Scan` over tokens. The WebGPU execution provider has no `Scan` kernel, so ONNX+Runtime runs it as a CPU loop with a GPU round trip per token per layer: about+0.13 s per prompt token for 2B and 0.19 s for 4B on an RTX 5090; a faster GPU+barely helps.+`chunk_gdn_prefill.py` replaces each `Scan` with the chunked gated delta rule+(128-token chunks, stable recursive block inversion inside a chunk, and a+parallel prefix scan across chunks, skipped by `If` when unneeded) and makes+the decoder return logits for the last position only. It uses only ops the+WebGPU EP implements. Weights and the `.onnx_data` file are unchanged, and+decode keeps the original single-token recurrent branch.++`build_vision.py` applies the rewrite. To derive a rewritten bundle from an+already sealed one without rebuilding the vision tower:++```powershell+.build/bonsai/venv/Scripts/python.exe scripts/models/bonsai/make_chunked_bundle.py --source .build/bonsai/exports/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision --output .build/bonsai/exports-chunked/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision --evidence .build/bonsai/chunked-cpu-2B.json --evidence .build/bonsai/chunked-webgpu-2B.json+```++`validate_chunked_gdn.py` compares the two graphs on CPU with real text:+prefills across chunk boundaries, a cached continuation, last-position logits,+every cache output and the following decode step. Browser evidence recorded+against the previous graph is not carried over; rerun the WebGPU checks below+on the new bundle. The worker replaces a stale cached graph for the two+webbrain-one Vision repositories by SHA-256, so existing installs only+download the new 3 MB graph file.+## Runtime contractLoad `AutoProcessor` and `AutoModelForImageTextToText` with:diff --git a/scripts/models/bonsai/build_vision.py b/scripts/models/bonsai/build_vision.pyindex e9124b9d7..83d5d4042 100644--- a/scripts/models/bonsai/build_vision.py+++ b/scripts/models/bonsai/build_vision.py@@ -18,6 +18,8 @@ import numpy as npimport onnximport onnxruntime as ortfrom huggingface_hub import snapshot_download++from chunk_gdn_prefill import MODEL_CARD_SECTION, rewrite_filefrom onnx import numpy_helperfrom safetensors import safe_open@@ -228,10 +230,14 @@ def build(size, workspace, offline=False, skip_inference=False):raise ValueError("Vision graph and original model architectures differ")if config["text_config"]["hidden_size"] != config["vision_config"]["out_hidden_size"]:raise ValueError("Vision and language embedding dimensions differ")- for filename in ("decoder_model_merged_q2f16.onnx", "decoder_model_merged_q2f16.onnx_data",- "embed_tokens_q2f16.onnx", "embed_tokens_q2f16.onnx_data"):+ for filename in ("decoder_model_merged_q2f16.onnx_data", "embed_tokens_q2f16.onnx", "embed_tokens_q2f16.onnx_data"):source = text / "onnx" / filenamelink_or_copy(source, output / "onnx" / filename)+ # Replace the token-sequential GatedDeltaNet Scan with the chunked prefill+ # graph. Only the small graph file changes; the weights stay hardlinked.+ language_graph = rewrite_file(text / "onnx" / "decoder_model_merged_q2f16.onnx",+ output / "onnx" / "decoder_model_merged_q2f16.onnx")+ print(f"{size}: rewrote {language_graph['gdn_layers_rewritten']} GatedDeltaNet prefill scans", flush=True)shutil.copy2(text / "tokenizer.json", output / "tokenizer.json")for filename in ("tokenizer_config.json", "preprocessor_config.json", "processor_config.json","generation_config.json", "chat_template.jinja"):@@ -271,11 +277,19 @@ def build(size, workspace, offline=False, skip_inference=False):"source_vision_pack_sha256": source_pack_hash,"vision_tensor_count": len(records),"onnx_checker": "passed",+ "language_graph": language_graph,"vision_inference": validation,"browser_image_inference": "Run scripts/check-bonsai-webgpu.mjs; this build check does not verify browser answers.","tensor_mapping": records,}write_json(output / "export-validation.json", report)+ write_json(output / "chunked-prefill-validation.json", {+ "rewrite": language_graph,+ "external_data_unchanged": "onnx/decoder_model_merged_q2f16.onnx_data",+ "evidence": [],+ "pending": "Compare with validate_chunked_gdn.py, time WebGPU prefill, and attach both reports "+ "(make_chunked_bundle.py --evidence) before publishing.",+ })ocr_limit = (f"The {size} model omitted or confused characters in four-character OCR fixtures, ""including at a 1,024-visual-token budget. Failed OCR reports are retained "@@ -295,7 +309,7 @@ tags: [onnx, webgpu, ternary, bonsai, vision, qwen3_5]Created using Bonsai by Prism ML. This is a modified ONNX packaging of Prism'sTernary Bonsai 2 {size}, for local text and image inference in WebBrain.-The ternary language graphs and Hadamard transforms come unchanged from+The ternary language weights and Hadamard transforms come unchanged from`danyalmalik/Ternary-Bonsai-2-{size}-ONNX-WebGPU`. The original Prism FP16 visiontower is exported into the compatible `onnx-community/Qwen3.5-{size}-ONNX`topology. All original vision tensors are mapped and checked after serialization;@@ -325,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.-""", encoding="utf-8")+""" + MODEL_CARD_SECTION, 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.pynew file mode 100644index 000000000..2fbb535f6--- /dev/null+++ b/scripts/models/bonsai/chunk_gdn_prefill.py@@ -0,0 +1,361 @@+"""Rewrite Bonsai 2 / Qwen3.5 decoder graphs for parallel WebGPU prefill.++The upstream ONNX export computes each GatedDeltaNet prefill with an ONNX+`Scan` over tokens. ONNX Runtime's WebGPU execution provider has no `Scan`+kernel, so prefill falls back to a CPU loop that round-trips the recurrent+state through the GPU once per token per layer.++This tool replaces every GatedDeltaNet `Scan` with the chunked delta-rule+formulation (the same algebra as `torch_chunk_gated_delta_rule` in+Transformers) built only from ops the WebGPU EP implements:++* inside each chunk the WY transform is computed with matmuls; the unit+ lower-triangular inverse uses stable recursive block inversion;+* chunk-to-chunk state propagation is a parallel (Hillis-Steele) prefix scan+ over the affine maps S -> A_c S + B_c, with steps skipped by `If` when the+ prompt has too few chunks to need them.++It also slices the final hidden state to the last position before the LM+head, so prefill returns logits for one token instead of+[sequence_length, vocab]. Generation in Transformers.js only reads the last+position.++All new weights are small constants stored inline. Existing initializers keep+their external-data references, so the large `.onnx_data` file is unchanged+and only the graph file needs to be replaced.+"""++from __future__ import annotations++import argparse+import hashlib+import json+from pathlib import Path++import numpy as np+import onnx+from onnx import TensorProto, helper, numpy_helper++INT64_MAX = 9223372036854775807++# Model-card paragraph shared by build_vision.py and make_chunked_bundle.py.+MODEL_CARD_SECTION = """+## Parallel prefill++The upstream language graph computes every GatedDeltaNet prefill with an ONNX+`Scan` over tokens. ONNX Runtime's WebGPU execution provider has no `Scan`+kernel, so prompts were processed one token at a time through a CPU loop with+a GPU round trip per token and layer (0.13 s per prompt token for 2B and 0.19 s+for 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+prefix scan across chunks), built only from ops the WebGPU EP implements, and+returns logits for the last position only. Weights, quantization and the+external data file are byte-identical to the previous revision; decode+(one token at a time) uses the unchanged recurrent path.++`chunked-prefill-validation.json` records the rewrite, CPU comparisons with the+original graph on real text (logit differences at fp16 rounding level,+identical top-1 tokens, prefill lengths across chunk boundaries and a cached+continuation) and WebGPU prefill timings against the original graph.+Reproduce with `scripts/models/bonsai/chunk_gdn_prefill.py` in the WebBrain+repository.+"""+++class Builder:+ """Appends nodes to a (sub)graph with layer-unique names."""++ def __init__(self, graph: onnx.GraphProto, prefix: str):+ self.graph = graph+ self.prefix = prefix+ self.nodes: list[onnx.NodeProto] = []+ self.count = 0++ def name(self, hint: str) -> str:+ self.count += 1+ return f"{self.prefix}/{hint}_{self.count}"++ def op(self, op_type: str, inputs, hint: str | None = None, outputs=None, **attrs):+ out = outputs or [self.name(hint or op_type)]+ self.nodes.append(helper.make_node(op_type, list(inputs), out, name=self.name(f"{op_type}_node"), **attrs))+ return out[0] if len(out) == 1 else out+++def add_constants(graph: onnx.GraphProto, chunk: int, head_dim: int, max_steps: int, ns: str) -> dict:+ c = {}++ def init(name, array):+ full = f"{ns}/{name}"+ graph.initializer.append(numpy_helper.from_array(np.ascontiguousarray(array), full))+ c[name] = full++ f32 = np.float32+ idx = np.arange(chunk)+ init("triu1", np.triu(np.ones((chunk, chunk), f32))) # row-vector cumsum+ init("tril0", np.tril(np.ones((chunk, chunk), f32))) # lower incl. diagonal+ init("neg_stril", -np.tril(np.ones((chunk, chunk), f32), -1)) # -strictly lower+ init("eyeC", np.eye(chunk, dtype=f32))+ init("eyeD", np.eye(head_dim, dtype=f32))+ b = 1+ while b < chunk:+ mask = (((idx[:, None] // b) % 2 == 1) & ((idx[None, :] // b) == (idx[:, None] // b) - 1)).astype(f32)+ init(f"blockmask_{b}", mask)+ b *= 2+ init("zero_f", np.array(0.0, f32))+ init("i0", np.array([0], np.int64))+ init("i1", np.array([1], np.int64))+ init("i2", np.array([2], np.int64))+ init("i3", np.array([3], np.int64))+ init("im1", np.array([-1], np.int64))+ init("im2", np.array([-2], np.int64))+ init("imax", np.array([INT64_MAX], np.int64))+ init("chunk", np.array([chunk], np.int64))+ init("pad4_head", np.zeros(6, np.int64)) # [b,h,s,d] begins..(s end)+ init("pad4_tail", np.zeros(1, np.int64))+ init("pad3_head", np.zeros(5, np.int64))+ init("pads5_head", np.zeros(2, np.int64)) # begin pads before axis 2 for rank 5+ init("pads5_tail", np.zeros(7, np.int64)) # begin d3,d4 + all end pads+ init("shape_q5", np.array([0, 0, -1, chunk, head_dim], np.int64))+ init("shape_g4", np.array([0, 0, -1, chunk], np.int64))+ init("shape_out4", np.array([0, 0, -1, head_dim], np.int64))+ for d in range(max_steps):+ init(f"shift_{d}", np.array([1 << d], np.int64))+ return c+++def build_chunked_recurrence(b: Builder, c: dict, state, q, k, v, beta, g, out_state, out_y,+ chunk: int, max_steps: int, always_steps: int):+ """Emit nodes computing the GatedDeltaNet recurrence over a whole prompt."""+ perm5 = [0, 1, 2, 4, 3]++ # --- pad the sequence to a multiple of the chunk size (zeros leave the state unchanged)+ shape = b.op("Shape", [q], "q_shape")+ seq = b.op("Gather", [shape, c["i2"]], "seq_len", axis=0) # [1]+ rem = b.op("Mod", [seq, c["chunk"]], "seq_mod")+ pad = b.op("Mod", [b.op("Sub", [c["chunk"], rem], "pad_raw"), c["chunk"]], "pad_len")+ pads4 = b.op("Concat", [c["pad4_head"], pad, c["pad4_tail"]], "pads4", axis=0)+ pads3 = b.op("Concat", [c["pad3_head"], pad], "pads3", axis=0)+ qp = b.op("Pad", [q, pads4, c["zero_f"]], "q_pad")+ kp = b.op("Pad", [k, pads4, c["zero_f"]], "k_pad")+ vp = b.op("Pad", [v, pads4, c["zero_f"]], "v_pad")+ bp = b.op("Pad", [beta, pads3, c["zero_f"]], "beta_pad")+ gp = b.op("Pad", [g, pads3, c["zero_f"]], "g_pad")++ qc = b.op("Reshape", [qp, c["shape_q5"]], "q_chunks") # [B,H,N,C,D]+ kc = b.op("Reshape", [kp, c["shape_q5"]], "k_chunks")+ vc = b.op("Reshape", [vp, c["shape_q5"]], "v_chunks")+ bc = b.op("Reshape", [bp, c["shape_g4"]], "beta_chunks") # [B,H,N,C]+ gc = b.op("Reshape", [gp, c["shape_g4"]], "g_chunks")++ # --- within-chunk cumulative log decay and pairwise decay matrix+ G = b.op("MatMul", [gc, c["triu1"]], "g_cumsum") # [B,H,N,C]+ Gi = b.op("Unsqueeze", [G, c["im1"]], "g_col") # [B,H,N,C,1]+ Gj = b.op("Unsqueeze", [G, c["im2"]], "g_row")+ diff = b.op("Sub", [Gi, Gj], "g_diff") # [B,H,N,C,C]+ decay = b.op("Mul", [b.op("Exp", [b.op("Mul", [diff, c["tril0"]], "g_diff_low")], "decay_exp"), c["tril0"]], "decay")++ beta_u = b.op("Unsqueeze", [bc, c["im1"]], "beta_col")+ kb = b.op("Mul", [kc, beta_u], "k_beta")+ vb = b.op("Mul", [vc, beta_u], "v_beta")+ kT = b.op("Transpose", [kc], "k_T", perm=perm5)+ A = b.op("Mul", [b.op("Mul", [b.op("MatMul", [kb, kT], "kbk"), decay], "kbk_decay"), c["neg_stril"]], "A_strict")++ # --- T = (I - A)^-1 by recursive block inversion (stable for unit lower-triangular)+ T = b.op("Add", [c["eyeC"], b.op("Mul", [A, c["blockmask_1"]], "A_blk1")], "T2")+ blk = 2+ while blk < chunk:+ Ab = b.op("Mul", [A, c[f"blockmask_{blk}"]], f"A_blk{blk}")+ T = b.op("Add", [T, b.op("MatMul", [b.op("MatMul", [T, Ab], f"TA{blk}"), T], f"TAT{blk}")], f"T{blk * 2}")+ blk *= 2++ expG = b.op("Exp", [G], "exp_g")+ vprime = b.op("MatMul", [T, vb], "v_prime") # [B,H,N,C,Dv]+ kcum = b.op("MatMul", [T, b.op("Mul", [kb, b.op("Unsqueeze", [expG, c["im1"]], "exp_g_col")], "kb_decay")], "k_cumdecay")++ # --- per-chunk affine state maps: S_next = A_c S + B_c+ Glast = b.op("Slice", [G, c["im1"], c["imax"], c["i3"]], "g_last") # [B,H,N,1]+ Kd = b.op("Mul", [kc, b.op("Unsqueeze", [b.op("Exp", [b.op("Sub", [Glast, G], "g_to_end")], "decay_to_end"), c["im1"]], "dte_col")], "k_decayed")+ KdT = b.op("Transpose", [Kd], "k_decayed_T", perm=perm5) # [B,H,N,D,C]+ gamma = b.op("Unsqueeze", [b.op("Exp", [Glast], "chunk_decay"), c["im1"]], "chunk_decay_5d") # [B,H,N,1,1]+ Ac = b.op("Sub", [b.op("Mul", [gamma, c["eyeD"]], "gamma_eye"), b.op("MatMul", [KdT, kcum], "kd_kcum")], "A_chunk")+ Bc = b.op("MatMul", [KdT, vprime], "B_chunk")++ # prepend the incoming state as the map (0, S0)+ s0 = b.op("Unsqueeze", [state, c["i2"]], "s0_5d") # [B,H,1,D,Dv]+ zeros = b.op("Mul", [s0, c["zero_f"]], "zeros_5d")+ A_all = b.op("Concat", [zeros, Ac], "A_all0", axis=2)+ B_all = b.op("Concat", [s0, Bc], "B_all0", axis=2)+ M = b.op("Gather", [b.op("Shape", [B_all], "B_all_shape"), c["i2"]], "num_maps", axis=0) # [1] = N + 1++ # --- Hillis-Steele inclusive scan; positions whose prefix already reaches the+ # (0, S0) map have A == 0, so zero padding is the exact identity element here.+ for d in range(max_steps):+ last = d == max_steps - 1+ sh = c[f"shift_{d}"]++ def emit_step(sb: Builder, A_in, B_in, need_a):+ keep = sb.op("Max", [sb.op("Sub", [M, sh], "keep_raw"), c["i0"]], "keep")+ padlen = sb.op("Min", [sh, M], "pad_maps")+ pads = sb.op("Concat", [c["pads5_head"], padlen, c["pads5_tail"]], "pads5", axis=0)+ B_sh = sb.op("Pad", [sb.op("Slice", [B_in, c["i0"], keep, c["i2"]], "B_head"), pads, c["zero_f"]], "B_shift")+ B_out = sb.op("Add", [sb.op("MatMul", [A_in, B_sh], "AB"), B_in], "B_next")+ if not need_a:+ return None, B_out+ A_sh = sb.op("Pad", [sb.op("Slice", [A_in, c["i0"], keep, c["i2"]], "A_head"), pads, c["zero_f"]], "A_shift")+ A_out = sb.op("MatMul", [A_in, A_sh], "A_next")+ return A_out, B_out++ if d < always_steps:+ A_new, B_new = emit_step(b, A_all, B_all, not last)+ A_all = A_new if A_new is not None else A_all+ B_all = B_new+ continue++ # Skip steps once the shift covers every map.+ cond = b.op("Squeeze", [b.op("Greater", [M, sh], f"need_step{d}")], f"need_step{d}_s")+ then_g = onnx.GraphProto()+ then_g.name = b.name(f"scan_step{d}_then")+ tb = Builder(then_g, f"{b.prefix}/step{d}")+ A_new, B_new = emit_step(tb, A_all, B_all, not last)+ then_outputs = [B_new] + ([A_new] if A_new is not None else [])+ then_g.node.extend(tb.nodes)+ else_g = onnx.GraphProto()+ else_g.name = b.name(f"scan_step{d}_else")+ eb = Builder(else_g, f"{b.prefix}/step{d}_skip")+ else_outputs = [eb.op("Identity", [B_all], "B_same")] + ([eb.op("Identity", [A_all], "A_same")] if A_new is not None else [])+ else_g.node.extend(eb.nodes)+ for graph_, outs in ((then_g, then_outputs), (else_g, else_outputs)):+ for o in outs:+ graph_.output.append(helper.make_tensor_value_info(o, TensorProto.FLOAT, None))+ if_outs = [b.name(f"B_step{d}")] + ([b.name(f"A_step{d}")] if A_new is not None else [])+ b.nodes.append(helper.make_node("If", [cond], if_outs, name=b.name(f"If_step{d}"), then_branch=then_g, else_branch=else_g))+ B_all = if_outs[0]+ if A_new is not None:+ A_all = if_outs[1]++ # --- states before each chunk, final state, and outputs+ N = b.op("Sub", [M, c["i1"]], "num_chunks")+ S_before = b.op("Slice", [B_all, c["i0"], N, c["i2"]], "S_before") # [B,H,N,D,Dv]+ b.op("Squeeze", [b.op("Slice", [B_all, N, M, c["i2"]], "S_last"), c["i2"]], outputs=[out_state])+ qkT = b.op("Mul", [b.op("MatMul", [qc, kT], "qk"), decay], "qk_decay") # lower incl. diag+ vnew = b.op("Sub", [vprime, b.op("MatMul", [kcum, S_before], "kcum_S")], "v_new")+ inter = b.op("MatMul", [b.op("Mul", [qc, b.op("Unsqueeze", [expG, c["im1"]], "exp_g_col2")], "q_decay"), S_before], "o_inter")+ o = b.op("Add", [inter, b.op("MatMul", [qkT, vnew], "o_intra")], "o_chunks") # [B,H,N,C,Dv]+ o4 = b.op("Reshape", [o, c["shape_out4"]], "o_padded")+ b.op("Slice", [o4, c["i0"], seq, c["i2"]], outputs=[out_y])+++def rewrite_gdn(model: onnx.ModelProto, chunk: int, max_steps: int, always_steps: int, head_dim: int) -> int:+ graph = model.graph+ consts = add_constants(graph, chunk, head_dim, max_steps, "/webbrain/chunked_gdn")+ replaced = 0+ for node in graph.node:+ if node.op_type != "If" or "/gdn/" not in node.name:+ continue+ for attr in node.attribute:+ if attr.name != "else_branch":+ continue+ sub = attr.g+ scans = [n for n in sub.node if n.op_type == "Scan"]+ if len(scans) != 1:+ raise RuntimeError(f"{node.name}: expected one Scan, found {len(scans)}")+ scan = scans[0]+ attrs = {a.name: helper.get_attribute_value(a) for a in scan.attribute if a.type != onnx.AttributeProto.GRAPH}+ if attrs.get("num_scan_inputs") != 5 or list(attrs.get("scan_input_axes", [])) != [2] * 5 \+ or list(attrs.get("scan_output_axes", [])) != [2] or len(scan.input) != 6 or len(scan.output) != 2:+ raise RuntimeError(f"{node.name}: unexpected Scan signature {attrs}")+ body = [a.g for a in scan.attribute if a.type == onnx.AttributeProto.GRAPH][0]+ expected_ops = ["Exp", "Unsqueeze", "Unsqueeze", "Mul", "Unsqueeze", "Mul", "ReduceSum", "Sub", "Unsqueeze",+ "Mul", "Unsqueeze", "Mul", "Add", "Unsqueeze", "Mul", "ReduceSum"]+ if [n.op_type for n in body.node] != expected_ops:+ raise RuntimeError(f"{node.name}: Scan body is not the expected gated delta rule")+ prefix = scan.name.rsplit("/", 1)[0] + "/chunked"+ b = Builder(sub, prefix)+ state, q, k, v, beta, g = scan.input+ build_chunked_recurrence(b, consts, state, q, k, v, beta, g, scan.output[0], scan.output[1],+ chunk, max_steps, always_steps)+ position = list(sub.node).index(scan)+ new_nodes = list(sub.node[:position]) + b.nodes + list(sub.node[position + 1:])+ del sub.node[:]+ sub.node.extend(new_nodes)+ replaced += 1+ return replaced+++def slice_last_logits(model: onnx.ModelProto) -> str:+ graph = model.graph+ lm = [n for n in graph.node if "logits" in n.output]+ if len(lm) != 1 or lm[0].op_type != "MatMulNBits":+ raise RuntimeError("expected a single MatMulNBits producing logits")+ lm = lm[0]+ hidden = lm.input[0]+ last = hidden + "/last_position"+ ns = "/webbrain/last_logits"+ graph.initializer.extend([+ numpy_helper.from_array(np.array([-1], np.int64), f"{ns}/start"),+ numpy_helper.from_array(np.array([INT64_MAX], np.int64), f"{ns}/end"),+ numpy_helper.from_array(np.array([1], np.int64), f"{ns}/axis"),+ ])+ slice_node = helper.make_node("Slice", [hidden, f"{ns}/start", f"{ns}/end", f"{ns}/axis"], [last], name=f"{ns}/Slice")+ index = list(graph.node).index(lm)+ graph.node.insert(index, slice_node)+ lm.input[0] = last+ for out in graph.output:+ if out.name == "logits":+ dims = out.type.tensor_type.shape.dim+ dims[1].Clear()+ dims[1].dim_value = 1+ return hidden+++def rewrite_file(source: Path, output: Path, chunk: int = 128, max_steps: int = 12, always_steps: int = 1,+ head_dim: int = 128, last_logits: bool = True) -> dict:+ """Rewrite `source` into `output`; the external data file name is unchanged."""+ if chunk < 2 or chunk & (chunk - 1):+ raise ValueError("chunk must be a power of two")+ source_sha = hashlib.sha256(source.read_bytes()).hexdigest()+ model = onnx.load(source, load_external_data=False)+ replaced = rewrite_gdn(model, chunk, max_steps, always_steps, head_dim)+ if not replaced:+ raise ValueError("no GatedDeltaNet Scan found")+ if last_logits:+ slice_last_logits(model)+ output.parent.mkdir(parents=True, exist_ok=True)+ # Never write through a hardlink into the upstream source graph.+ if output.exists():+ output.unlink()+ onnx.save(model, output)+ # The upstream graph uses ORT contrib ops in the default domain, so the strict+ # ONNX checker rejects it before and after this rewrite; validate with+ # onnxruntime instead (see validate_chunked_gdn.py).+ return {+ "tool": "scripts/models/bonsai/chunk_gdn_prefill.py",+ "source_sha256": source_sha,+ "output_sha256": hashlib.sha256(output.read_bytes()).hexdigest(),+ "output_bytes": output.stat().st_size,+ "gdn_layers_rewritten": replaced,+ "chunk_tokens": chunk,+ "prefix_scan_steps": max_steps,+ "max_prompt_tokens": ((1 << max_steps) - 1) * chunk,+ "logits": "last position only" if last_logits else "every position",+ }+++def main():+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ parser.add_argument("input", type=Path, help="decoder_model_merged_q2f16.onnx (external data stays alongside)")+ parser.add_argument("output", type=Path)+ parser.add_argument("--chunk", type=int, default=128)+ parser.add_argument("--max-steps", type=int, default=12, help="prefix-scan steps; supports (2**steps - 1) chunks")+ parser.add_argument("--always-steps", type=int, default=1, help="leading steps emitted without an If guard")+ parser.add_argument("--head-dim", type=int, default=128)+ parser.add_argument("--keep-all-logits", action="store_true")+ args = parser.parse_args()+ report = rewrite_file(args.input, args.output, args.chunk, args.max_steps, args.always_steps,+ args.head_dim, not args.keep_all_logits)+ print(json.dumps(report, indent=2))+++if __name__ == "__main__":+ main()diff --git a/scripts/models/bonsai/make_chunked_bundle.py b/scripts/models/bonsai/make_chunked_bundle.pynew file mode 100644index 000000000..6d36906d6--- /dev/null+++ b/scripts/models/bonsai/make_chunked_bundle.py@@ -0,0 +1,118 @@+"""Derive a parallel-prefill bundle from a sealed Bonsai 2 vision bundle.++Only the language decoder graph changes (see chunk_gdn_prefill.py). Large+unchanged files are hardlinked. Browser evidence recorded against the previous+graph is not carried over: rerun scripts/check-bonsai-webgpu.mjs on the new+bundle and record it with record_browser_validation.py.++ python scripts/models/bonsai/make_chunked_bundle.py \+ --source .build/bonsai/exports/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision \+ --output .build/bonsai/exports-chunked/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision \+ --evidence .build/bonsai/chunked-cpu-2B.json --evidence .build/bonsai/chunked-webgpu-2B.json+"""++import argparse+import json+import shutil+from pathlib import Path++from build_vision import digest, link_or_copy, read_json, seal_manifest, write_json+from chunk_gdn_prefill import MODEL_CARD_SECTION as CARD_SECTION, rewrite_file++GRAPH = "onnx/decoder_model_merged_q2f16.onnx"+# Upstream danyalmalik language graphs this rewrite was validated against.+SOURCE_GRAPH_SHA256 = {+ "2B": "a4b3e8a9633cfffa35104ccbf2bd748c26f36cd8b1281c8443d47b82ecd0268f",+ "4B": "e7975db212de3464d54f086cc4652e2d56a9f16632ee428601be47e7dc33a542",+}+STALE_EVIDENCE = {"browser-validation.json", "offline-browser-validation.json"}+CARD_LANGUAGE_OLD = "The ternary language graphs and Hadamard transforms come unchanged from"+CARD_LANGUAGE_NEW = "The ternary language weights and Hadamard transforms come unchanged from"+++def main():+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ parser.add_argument("--source", type=Path, required=True, help="sealed bundle with the original graph")+ parser.add_argument("--output", type=Path, required=True)+ parser.add_argument("--graph", type=Path, help="already rewritten graph to use (checked against --source)")+ parser.add_argument("--chunk", type=int, default=128)+ parser.add_argument("--evidence", type=Path, action="append", default=[], help="JSON reports to embed")+ args = parser.parse_args()+ source, output = args.source.resolve(), args.output.resolve()+ size = "2B" if "-2B-" in source.name else "4B"++ manifest = read_json(source / "manifest.json")+ for entry in manifest["files"]:+ if digest(source / entry["path"]) != entry["sha256"]:+ raise ValueError(f"Source bundle changed after sealing: {entry['path']}")+ if digest(source / GRAPH) != SOURCE_GRAPH_SHA256[size]:+ raise ValueError("Source decoder graph is not the validated upstream graph")++ output.mkdir(parents=True, exist_ok=True)+ (output / "onnx").mkdir(exist_ok=True)+ for entry in manifest["files"]:+ path = entry["path"]+ if path == GRAPH or path in STALE_EVIDENCE:+ continue+ target = output / path+ target.parent.mkdir(parents=True, exist_ok=True)+ if entry["size"] > 16 * 1024 * 1024:+ link_or_copy(source / path, target)+ else:+ if target.exists():+ target.unlink()+ shutil.copy2(source / path, target)+ for stale in STALE_EVIDENCE:+ (output / stale).unlink(missing_ok=True)++ scratch = output / "onnx" / ".rewrite.onnx"+ report = rewrite_file(source / GRAPH, scratch, chunk=args.chunk)+ if args.graph:+ if digest(args.graph) != report["output_sha256"]:+ # Protobuf serializers may order bytes differently; require identical graphs.+ import onnx+ a = onnx.load(args.graph, load_external_data=False)+ b = onnx.load(scratch, load_external_data=False)+ a.ClearField("producer_version"); b.ClearField("producer_version")+ if a.SerializeToString(deterministic=True) != b.SerializeToString(deterministic=True):+ raise ValueError("--graph differs from a fresh rewrite of the source graph")+ scratch.unlink()+ (output / GRAPH).unlink(missing_ok=True)+ shutil.copy2(args.graph, output / GRAPH)+ report["output_sha256"] = digest(output / GRAPH)+ report["output_bytes"] = (output / GRAPH).stat().st_size+ else:+ (output / GRAPH).unlink(missing_ok=True)+ scratch.rename(output / GRAPH)++ write_json(output / "chunked-prefill-validation.json", {+ "rewrite": report,+ "external_data_unchanged": "onnx/decoder_model_merged_q2f16.onnx_data",+ "evidence": [read_json(path) for path in args.evidence],+ })+ validation = read_json(output / "export-validation.json")+ validation["language_graph"] = {+ "change": "GatedDeltaNet Scan prefill replaced by chunked delta rule; last-position logits",+ "evidence": "chunked-prefill-validation.json",+ **{key: report[key] for key in ("source_sha256", "output_sha256", "chunk_tokens", "max_prompt_tokens", "logits")},+ }+ validation["browser_image_inference"] = "Run scripts/check-bonsai-webgpu.mjs; this build check does not verify browser answers."+ write_json(output / "export-validation.json", validation)++ card = (output / "README.md").read_text(encoding="utf-8")+ card = card.replace(CARD_LANGUAGE_OLD, CARD_LANGUAGE_NEW)+ head, sep, tail = card.partition("\n## Hardware validation")+ card = head.rstrip() + "\n" + CARD_SECTION+ if sep:+ # Keep OCR limitation notes; hardware evidence is re-recorded for this graph.+ ocr = tail.partition("\n### Observed OCR limitation")+ if ocr[1]:+ card += "\n## Observed OCR limitation" + ocr[2].rstrip() + (+ "\n\nOCR reports in this repository were recorded with the previous (Scan) graph.\n")+ (output / "README.md").write_text(card, encoding="utf-8")+ total = seal_manifest(output)+ print(json.dumps({"output": str(output), "graph_sha256": report["output_sha256"], "total_bytes": total}, indent=2))+++if __name__ == "__main__":+ main()diff --git a/scripts/models/bonsai/validate_chunked_gdn.py b/scripts/models/bonsai/validate_chunked_gdn.pynew file mode 100644index 000000000..d5f8338b6--- /dev/null+++ b/scripts/models/bonsai/validate_chunked_gdn.py@@ -0,0 +1,155 @@+"""Compare an original Bonsai 2 decoder graph with its chunked-prefill rewrite.++Both graphs share the same external weight file. Real text is embedded with the+bundle's embed_tokens graph, then both decoders run on CPU onnxruntime with+identical inputs: a fresh prefill at several lengths (chunk boundaries+included) and a continuation prefill from a non-zero cache. Logits for the+last position, every recurrent/conv state and every KV cache are compared, as+well as one greedy decode step from each resulting cache.++Needs `tokenizers` in addition to requirements.txt:+ python scripts/models/bonsai/validate_chunked_gdn.py \+ --original <source>/onnx/decoder_model_merged_q2f16.onnx \+ --rewritten <bundle>/onnx/decoder_model_merged_q2f16.onnx \+ --embed <bundle>/onnx/embed_tokens_q2f16.onnx --tokenizer <bundle>/tokenizer.json+"""++from __future__ import annotations++import argparse+import json+import time+from pathlib import Path++import numpy as np+import onnxruntime as ort+from tokenizers import Tokenizer++TEXT = (+ "The nested set model is a technique for representing nested set collections in relational databases. "+ "It is based on Nested Intervals, that are immune to hierarchy reorganization problem, and allow answering "+ "ancestor path hierarchical queries algorithmically without accessing the stored hierarchy relation. "+ "The standard relational algebra and relational calculus, and the SQL operations based on them, are unable to "+ "express directly all desirable operations on hierarchies. The nested interval model numbers the nodes "+ "according to a tree traversal, which visits each node twice, assigning numbers in the order of visiting. "+)+++def session(path: Path) -> ort.InferenceSession:+ so = ort.SessionOptions()+ so.log_severity_level = 3+ return ort.InferenceSession(str(path), so, providers=["CPUExecutionProvider"])+++def empty_cache(sess: ort.InferenceSession) -> dict:+ feeds = {}+ for i in sess.get_inputs():+ if i.name.startswith(("past_conv", "past_recurrent", "past_key_values")):+ shape = [1 if d == "batch_size" else 0 if isinstance(d, str) else d for d in i.shape]+ feeds[i.name] = np.zeros(shape, np.float16)+ return feeds+++def run(sess, embeds, cache, past_len):+ S = embeds.shape[1]+ pos = np.arange(past_len, past_len + S, dtype=np.int64)+ feeds = dict(cache)+ feeds.update({+ "inputs_embeds": embeds,+ "attention_mask": np.ones((1, past_len + S), np.int64),+ "position_ids": np.broadcast_to(pos, (3, 1, S)).copy(),+ })+ names = [o.name for o in sess.get_outputs()]+ t0 = time.perf_counter()+ out = dict(zip(names, sess.run(None, feeds)))+ return out, time.perf_counter() - t0+++def to_cache(out: dict) -> dict:+ return {k.replace("present_conv", "past_conv").replace("present_recurrent", "past_recurrent").replace("present.", "past_key_values."): v+ for k, v in out.items() if k.startswith("present")}+++def compare(a: dict, b: dict) -> dict:+ la = a["logits"][:, -1].astype(np.float32)+ lb = b["logits"][:, -1].astype(np.float32)+ worst_state = 0.0+ worst_name = ""+ for k in a:+ if k == "logits":+ continue+ x = a[k].astype(np.float32)+ y = b[k].astype(np.float32)+ scale = max(float(np.abs(x).max()), 1e-6)+ err = float(np.abs(x - y).max()) / scale+ if err > worst_state:+ worst_state, worst_name = err, k+ pa = np.exp(la - la.max()); pa /= pa.sum()+ pb = np.exp(lb - lb.max()); pb /= pb.sum()+ return {+ "logits_shape_original": list(a["logits"].shape),+ "logits_shape_rewritten": list(b["logits"].shape),+ "last_logits_max_abs_diff": float(np.abs(la - lb).max()),+ "last_logits_scale": float(np.abs(la).max()),+ "top1_equal": bool(la.argmax() == lb.argmax()),+ "top5_original": la[0].argsort()[-5:][::-1].tolist(),+ "top5_rewritten": lb[0].argsort()[-5:][::-1].tolist(),+ "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,+ "finite": bool(all(np.isfinite(v.astype(np.float32)).all() for v in b.values())),+ }+++def main():+ p = argparse.ArgumentParser()+ p.add_argument("--original", type=Path, required=True)+ p.add_argument("--rewritten", type=Path, required=True)+ p.add_argument("--embed", type=Path, required=True)+ p.add_argument("--tokenizer", type=Path, required=True)+ p.add_argument("--lengths", default="2,63,64,65,200,700")+ p.add_argument("--output", type=Path)+ args = p.parse_args()++ tok = Tokenizer.from_file(str(args.tokenizer))+ ids = tok.encode(TEXT * 8).ids+ embed = session(args.embed)+ emb_in = embed.get_inputs()[0].name+ all_embeds = embed.run(None, {emb_in: np.array([ids], np.int64)})[0]+ orig = session(args.original)+ new = session(args.rewritten)+ report = {"tokens_available": len(ids), "fresh_prefill": [], "continuation": None}++ for S in [int(x) for x in args.lengths.split(",")]:+ e = all_embeds[:, :S]+ a, ta = run(orig, e, empty_cache(orig), 0)+ b, tb = run(new, e, empty_cache(new), 0)+ r = compare(a, b)+ # one greedy decode step from each cache+ nxt = int(a["logits"][0, -1].argmax())+ e1 = embed.run(None, {emb_in: np.array([[nxt]], np.int64)})[0]+ a2, _ = run(orig, e1, to_cache(a), S)+ b2, _ = run(new, e1, to_cache(b), S)+ r2 = compare(a2, b2)+ r.update({"tokens": S, "cpu_seconds_original": round(ta, 3), "cpu_seconds_rewritten": round(tb, 3),+ "decode_step_logits_max_abs_diff": r2["last_logits_max_abs_diff"], "decode_step_top1_equal": r2["top1_equal"]})+ report["fresh_prefill"].append(r)+ print(json.dumps({k: r[k] for k in ("tokens", "last_logits_max_abs_diff", "last_logits_scale", "top1_equal", "kl_original_to_rewritten",+ "worst_cache_rel_err", "worst_cache_name", "decode_step_top1_equal", "cpu_seconds_original", "cpu_seconds_rewritten")}))++ # continuation: 150 tokens of cache from the original graph, then 300 more tokens through both+ head = all_embeds[:, :150]+ a0, _ = run(orig, head, empty_cache(orig), 0)+ cache = to_cache(a0)+ tail = all_embeds[:, 150:450]+ a, _ = run(orig, tail, cache, 150)+ b, _ = run(new, tail, cache, 150)+ r = compare(a, b)+ report["continuation"] = {"cached_tokens": 150, "new_tokens": 300, **r}+ print(json.dumps({"continuation": {k: r[k] for k in ("last_logits_max_abs_diff", "top1_equal", "kl_original_to_rewritten", "worst_cache_rel_err")}}))+ if args.output:+ args.output.write_text(json.dumps(report, indent=2))+++if __name__ == "__main__":+ main()diff --git a/src/chrome/src/offscreen/inference-worker.js b/src/chrome/src/offscreen/inference-worker.jsindex c53f9a42b..714405aad 100644--- a/src/chrome/src/offscreen/inference-worker.js+++ b/src/chrome/src/offscreen/inference-worker.js@@ -48,6 +48,17 @@ const WEBGPU_BONSAI2_MODEL_IDS = new Set([...WEBGPU_BONSAI2_VISION_MODEL_IDS,'Mike0021/Ternary-Bonsai-2-4B-text-ONNX',]);+// Parallel-prefill decoder graphs (chunked GatedDeltaNet; see+// scripts/models/bonsai/chunk_gdn_prefill.py). Earlier revisions prefilled with+// an ONNX Scan that WebGPU runs one token at a time on the CPU. Installs that+// cached that graph keep it under the same URL, so replace just this small file;+// the unchanged weight files stay cached.+const BONSAI2_DECODER_GRAPH_FILE = 'onnx/decoder_model_merged_q2f16.onnx';+const BONSAI2_DECODER_GRAPH_SHA256 = new Map([+ ['webbrain-one/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision', '2768d880f4fdab46a984d697b901db6e8ac54dc9b896524cb8cb769bbb339df0'],+ ['webbrain-one/Ternary-Bonsai-2-4B-ONNX-WebGPU-Vision', '52424fbb7094ed0a1036d8795e78c3da349fce1838ee6bd077523929b0bb7bb2'],+]);+const checkedBonsai2DecoderGraphs = new Set();// The Qwen3.5 generation_config sidecar omitted by Danyal's split exports.// Per-request decoding still controls do_sample and the output token budget.const BONSAI2_GENERATION_CONFIG = {@@ -208,6 +219,48 @@ async function cacheBonsai2CompatibilityMetadata(modelId) {}}+async function sha256Hex(buffer) {+ const digest = await crypto.subtle.digest('SHA-256', buffer);+ return [...new Uint8Array(digest)].map(byte => byte.toString(16).padStart(2, '0')).join('');+}++/**+ * Make a cached Bonsai 2 Vision decoder graph the parallel-prefill revision.+ * Best effort: offline or unexpected responses keep the cached graph, which+ * still produces the same answers, only with slower prefill.+ */+export async function refreshBonsai2DecoderGraph(modelId, { expected = BONSAI2_DECODER_GRAPH_SHA256.get(modelId) } = {}) {+ if (!expected || checkedBonsai2DecoderGraphs.has(modelId)) return 'skipped';+ if (typeof caches === 'undefined' || !globalThis.crypto?.subtle) return 'unavailable';+ const url = `https://huggingface.co/${modelId}/resolve/main/${BONSAI2_DECODER_GRAPH_FILE}`;+ try {+ const cache = await caches.open(TRANSFORMERS_CACHE_NAME);+ const cached = await cache.match(url);+ if (!cached) {+ // Not downloaded yet: the regular download fetches the current graph.+ checkedBonsai2DecoderGraphs.add(modelId);+ return 'not-cached';+ }+ if (await sha256Hex(await cached.arrayBuffer()) === expected) {+ checkedBonsai2DecoderGraphs.add(modelId);+ return 'current';+ }+ const fetcher = typeof globalThis.fetch === 'function' ? globalThis.fetch.bind(globalThis) : nativeFetch;+ if (!fetcher) return 'unavailable';+ const response = await fetcher(url, { cache: 'no-store' });+ if (!response?.ok) return 'unavailable';+ const bytes = await response.arrayBuffer();+ if (await sha256Hex(bytes) !== expected) return 'unexpected';+ await cache.put(url, new Response(bytes, {+ headers: { 'content-type': 'application/octet-stream', 'content-length': String(bytes.byteLength) },+ }));+ checkedBonsai2DecoderGraphs.add(modelId);+ return 'updated';+ } catch {+ return 'unavailable';+ }+}+export function normalizeBonsai2Config(config) {return {...config,@@ -718,6 +771,7 @@ async function getVisionRuntime(modelId, dtype, device, {error.code = readiness === 'text' ? 'text_model_not_downloaded' : 'vision_model_not_downloaded';throw error;}+ if (WEBGPU_BONSAI2_VISION_MODEL_IDS.has(modelId)) await refreshBonsai2DecoderGraph(modelId);const library = await loadLibrary();const { AutoModelForImageTextToText, AutoProcessor } = library;if (!AutoModelForImageTextToText || !AutoProcessor) {diff --git a/test/run.js b/test/run.jsindex ed43d5fd3..360aa31fa 100644--- a/test/run.js+++ b/test/run.js@@ -67550,6 +67550,61 @@ test('Vision Model removal deletes only its cache entries', async () => {}});+test('Bonsai 2 Vision worker replaces only a stale cached decoder graph', async () => {+ const previousSelf = globalThis.self;+ const previousCaches = globalThis.caches;+ const previousFetch = globalThis.fetch;+ const { createHash } = await import('node:crypto');+ const sha = text => createHash('sha256').update(text).digest('hex');+ const model = 'webbrain-one/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision';+ const graphUrl = `https://huggingface.co/${model}/resolve/main/onnx/decoder_model_merged_q2f16.onnx`;+ const dataUrl = `${graphUrl}_data`;+ const makeCache = entries => ({+ match: async url => entries.has(url) ? new Response(entries.get(url)) : undefined,+ put: async (url, response) => { entries.set(url, Buffer.from(await response.arrayBuffer()).toString()); },+ });+ try {+ globalThis.self = { addEventListener() {}, postMessage() {} };+ const workerSource = fs.readFileSync(path.join(ROOT, 'src/chrome/src/offscreen/inference-worker.js'), 'utf8');+ for (const size of ['2B', '4B']) {+ assert.match(workerSource, new RegExp(`'webbrain-one/Ternary-Bonsai-2-${size}-ONNX-WebGPU-Vision', '[0-9a-f]{64}'`),+ `the ${size} Vision decoder graph must be pinned by SHA-256`);+ }++ const entries = new Map([[graphUrl, 'scan graph'], [dataUrl, 'weights']]);+ const fetched = [];+ globalThis.caches = { open: async () => makeCache(entries) };+ globalThis.fetch = async (url, init) => { fetched.push([url, init?.cache]); return new Response('chunked graph'); };+ const worker = await import(`${pathToFileURL(path.join(ROOT, 'src/chrome/src/offscreen/inference-worker.js')).href}?bonsai2-graph-refresh`);+ assert.equal(await worker.refreshBonsai2DecoderGraph(model, { expected: sha('chunked graph') }), 'updated');+ assert.equal(entries.get(graphUrl), 'chunked graph');+ assert.equal(entries.get(dataUrl), 'weights', 'the unchanged weight file must stay cached');+ assert.deepEqual(fetched, [[graphUrl, 'no-store']]);+ assert.equal(await worker.refreshBonsai2DecoderGraph(model, { expected: sha('chunked graph') }), 'skipped',+ 'a verified graph is not re-hashed on every runtime load');+ assert.equal(await worker.refreshBonsai2DecoderGraph('danyalmalik/Ternary-Bonsai-2-2B-ONNX-WebGPU'), 'skipped');++ const other = 'webbrain-one/Ternary-Bonsai-2-4B-ONNX-WebGPU-Vision';+ const otherUrl = `https://huggingface.co/${other}/resolve/main/onnx/decoder_model_merged_q2f16.onnx`;+ entries.set(otherUrl, 'scan graph');+ globalThis.fetch = async () => new Response('tampered graph');+ assert.equal(await worker.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'unexpected');+ assert.equal(entries.get(otherUrl), 'scan graph', 'an unverified response must not replace the cached graph');+ globalThis.fetch = async () => { throw new TypeError('Failed to fetch'); };+ assert.equal(await worker.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'unavailable');+ assert.equal(entries.get(otherUrl), 'scan graph', 'offline loads keep the working cached graph');+ entries.set(otherUrl, 'chunked graph');+ assert.equal(await worker.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'current');+ } finally {+ if (previousSelf === undefined) delete globalThis.self;+ else globalThis.self = previousSelf;+ if (previousCaches === undefined) delete globalThis.caches;+ else globalThis.caches = previousCaches;+ if (previousFetch === undefined) delete globalThis.fetch;+ else globalThis.fetch = previousFetch;+ }+});+test('Bonsai 2 worker keeps native rotary positions continuous for text and images', async () => {const previousSelf = globalThis.self;const ortSymbol = Symbol.for('onnxruntime');