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
Audit local function bodies; take long-prompt path and dtype from the presets
Claudef61514320de3
diff --git a/scripts/check-webgpu-long-prompts.mjs b/scripts/check-webgpu-long-prompts.mjsindex 657a729b8..e2a245dba 100644--- a/scripts/check-webgpu-long-prompts.mjs+++ b/scripts/check-webgpu-long-prompts.mjs@@ -20,8 +20,12 @@// node scripts/check-webgpu-long-prompts.mjs --model webbrain-one/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision \// --vision --profile .build/bonsai/chunked-2b-profile --offline --lengths 1000,4000,8000 --slices 0,512//-// A repository whose name ends in -Vision always uses the multimodal path, as-// in production; --vision forces it for other repositories.+// Shipped presets use their production path and dtype from+// src/chrome/src/providers/webgpu.js: `onnx-vl` presets run multimodal chat,+// and other ONNX presets run text chat with their preset dtype (q4f16 by+// default, as for custom repositories). --vision forces multimodal chat and+// --dtype (a name or a JSON object) overrides the dtype. The `bitgpu` runtime+// does not use this worker and is rejected.//// Options mirror check-bonsai-webgpu.mjs: --local-repo owner/repo=dir serves// unpublished files, --offline requires a cached model and blocks Hugging Face,@@ -34,17 +38,21 @@ import http from 'node:http';import path from 'node:path';import { fileURLToPath } from 'node:url';import { chromium } from 'playwright';+import {+ WEBGPU_RUNTIME_BITGPU, WEBGPU_RUNTIME_ONNX_VL, webgpuModelDtype, webgpuModelRuntime,+} from '../src/chrome/src/providers/webgpu.js';const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');const args = process.argv.slice(2);const option = (name, fallback) => { const i = args.indexOf(name); return i < 0 ? fallback : args[i + 1]; };const model = option('--model', 'webbrain-one/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision');-// The Vision presets always run through the multimodal path in production.-const vision = args.includes('--vision') || /-Vision$/i.test(model);+const runtime = webgpuModelRuntime(model);+assert(runtime !== WEBGPU_RUNTIME_BITGPU, `${model} uses the ${runtime} runtime, which this ONNX worker check does not cover`);+// As in production, onnx-vl presets always use the multimodal path.+const vision = args.includes('--vision') || runtime === WEBGPU_RUNTIME_ONNX_VL;const offline = args.includes('--offline');-const dtype = option('--dtype', null)- ? JSON.parse(option('--dtype'))- : vision ? { embed_tokens: 'q2f16', vision_encoder: 'fp16', decoder_model_merged: 'q2f16' } : 'q2f16';+const parseDtype = value => { try { return JSON.parse(value); } catch { return value; } };+const dtype = option('--dtype', null) ? parseDtype(option('--dtype')) : webgpuModelDtype(model);const lengths = option('--lengths', '1000,4000,8000').split(',').map(Number);const slices = option('--slices', '0,512').split(',').map(Number);const maxTokens = Number(option('--max-tokens', 48));diff --git a/scripts/models/audit_webgpu_onnx.py b/scripts/models/audit_webgpu_onnx.pyindex 229672c3f..e434aaa4f 100644--- a/scripts/models/audit_webgpu_onnx.py+++ b/scripts/models/audit_webgpu_onnx.py@@ -4,9 +4,10 @@ Catches the problems that made Ternary Bonsai 2 prefill take minutes in thebrowser (see docs/webgpu-models.md):* control-flow ops that ONNX Runtime Web's WebGPU execution provider does not- implement (`Scan`, `Loop`), including inside `If` branches. ONNX Runtime- places them on the CPU and their bodies then run once per iteration with a- GPU round trip, so a per-token recurrence becomes a per-token CPU loop;+ implement (`Scan`, `Loop`), including inside `If` branches and model-local+ functions. ONNX Runtime places them on the CPU and their bodies then run+ once per iteration with a GPU round trip, so a per-token recurrence becomes+ a per-token CPU loop;* a `logits` output that keeps the sequence dimension, so prefill computes anddownloads `[sequence_length, vocab]` scores (0.5 MB per prompt token for a248k vocabulary in FP16) and can exceed a 2 GB WebGPU buffer limit.@@ -62,7 +63,11 @@ def audit(path, text_generate_only=False):control_flow = collections.defaultdict(list)op_counts = collections.Counter()- for node, location in walk(graph):+ # Model-local functions are inlined by ONNX Runtime, so audit their bodies too.+ bodies = [(graph, "")] + [(function, f"function:{function.domain}::{function.name}")+ for function in model.functions]+ nodes = [entry for body, location in bodies for entry in walk(body, location)]+ for node, location in nodes:domain = node.domain or "ai.onnx"op_counts[f"{domain}::{node.op_type}"] += 1if node.op_type in CPU_ONLY_CONTROL_FLOW and domain == "ai.onnx":@@ -104,11 +109,13 @@ def audit(path, text_generate_only=False):"the only caller.")blockers.append(entry)- contrib = sorted(k for k in op_counts if not k.startswith("ai.onnx::"))+ local_functions = {f"{function.domain}::{function.name}" for function in model.functions}+ contrib = sorted(k for k in op_counts if not k.startswith("ai.onnx::") and k not in local_functions)info.update({"opsets": {o.domain or "ai.onnx": o.version for o in model.opset_import},"nodes": sum(op_counts.values()),"if_nodes": op_counts.get("ai.onnx::If", 0),+ "local_functions": sorted(local_functions),"contrib_ops": {k: op_counts[k] for k in contrib},"external_data_files": sorted({entry.value for tensor in graph.initializer if tensor.data_location == onnx.TensorProto.EXTERNAL