Model2WebNN
Model2WebNN is a browser-based tool that parses .onnx and .tflite model files and generates self-contained JavaScript code using the WebNN MLGraphBuilder API. All processing is client-side — models never leave the browser.
Privacy and Security
🔒 Client-Side Processing Only
All model conversion and code generation operations run exclusively in your browser. No model data, weights, or intellectual property is transmitted to or stored on any external servers. This makes the tool safe for:
- Proprietary models
- Sensitive intellectual property
- Enterprise environments with strict data governance
- Any scenario requiring complete data privacy
Model2WebNN , along with WebNN Netron , ensures your models never leave your local environment during the conversion process.
Overview
Model2WebNN provides a web UI, CLI, and library API for generating WebNN-compatible JavaScript and TypeScript code. It supports ONNX (.onnx) and TFLite (.tflite) model formats, with operator implementations that follow the ORT WebNN Execution Provider 1:1.
Output files:
| File | Description |
|---|---|
model.js | async buildGraph(context, weights) — pure MLGraphBuilder calls, no framework |
model.weights | WGWT v1 binary — raw tensor data with 8-byte header |
model.manifest.json | Tensor index: names, shapes, data types, byte offsets |
model.html | Runnable test harness with device selector and result viewer |
Step-by-Step Guide
Web UI
- Open Model2WebNN
- Upload a model file (
.onnxor.tflite) by dropping it, browsing files, or fetching from a URL (HuggingFace links supported) - Code generates instantly in the Monaco editor
- Set free dimension overrides if your model has symbolic dimensions to regenerate with fixed shapes
- Download individual files or the full
.zipbundle from the header
You can also auto-load a model by appending
?url=to the URL, e.g.https://ibelem.github.io/model2webnn/?url=https://huggingface.co/webnn/mobilenet-v2/resolve/main/onnx/model_fp16.onnx
CLI
npx tsx src/cli.ts model.onnx -o dist/
npx tsx src/cli.ts model.onnx -o dist/ -f ts
npx tsx src/cli.ts model.onnx -o dist/ -d batch_size=1 -d seq_len=128
npx tsx src/cli.ts --list-ops| Option | Description |
|---|---|
-o, --output <dir> | Output directory (default: .) |
-f, --format <fmt> | js, ts, or html (default: js) |
-d, --free-dim <n=v> | Override a symbolic dimension; repeatable |
--list-ops | Print all supported operations and exit |
Library API
import { convert } from 'model2webnn';
const buffer = new Uint8Array(await file.arrayBuffer());
const result = await convert(buffer, {
format: 'javascript',
freeDimensionOverrides: { batch_size: 1 },
});
result.code; // .js source — buildGraph(context, weights)
result.weights; // Uint8Array — WGWT binary
result.manifest; // tensor name → { dataType, shape, byteOffset, byteLength }
result.html; // self-contained test page
result.coverage; // { totalOps, supportedOps, unsupportedOpTypes, … }
result.unresolvedFreeDims; // symbolic dimension names not yet overriddenFree Dimension Overrides
Models with symbolic dimensions (e.g. batch_size, sequence_length) are converted immediately with those names preserved. Override them to generate code with concrete shapes:
# CLI
npx tsx src/cli.ts model.onnx -d batch_size=1 -d sequence_length=128In the web UI, input fields appear for each symbolic dimension. Entering a value auto-regenerates the code. Leaving a field empty keeps the symbolic name.
See symbolic dimensions documentation for details.
Testing Your Generated Code
Security Requirements
WebNN requires a secure context to function. Valid environments include:
https://URLshttp://localhostorhttp://127.0.0.1- Local development servers
Local Testing
Start a local HTTP server to test your generated code:
# Install http-server if needed
npm install -g http-server
# Start server in your project directory
http-server
# Navigate to http://localhost:8080 in your browserOpen the generated .html file in your browser to validate the conversion.
Operator Coverage
Operator implementations follow the ORT WebNN Execution Provider 1:1 — same attribute defaults and edge-case handling.
- ONNX — 114 ops (simple ops with direct WebNN mapping + composite ops decomposed into WebNN primitives)
- TFLite — 96 ops
When a model contains ops with no WebNN equivalent (e.g. TopK, Range, Mod), the codegen marks those op outputs as dead, propagates the dead state through all downstream ops automatically, and exports the last live frontier tensors before the dead zone instead of the original outputs. The generated graph always builds successfully.
Use Cases
Model2WebNN is ideal for:
- Rapid Prototyping: Quickly convert models for web-based testing
- Educational Purposes: Understanding WebNN API structure and usage
- IP-Sensitive Projects: Converting proprietary models without cloud exposure
- Cross-Platform Development: Generating code that runs across WebNN-supported browsers
Best Practices
- Model Preparation: Ensure your model is optimized and has fixed input dimensions when possible
- Dimension Overrides: Carefully set symbolic dimensions based on your expected input data
- Testing: Always validate the generated code with sample inputs before production use
- Performance: Consider model quantization or optimization before conversion for better inference speed
Related Resources
- Model2WebNN GitHub - Source code and documentation
- GitHub Issues - Model2WebNN - Report bugs and request features
- WebNN Netron - Model visualization tool with enhanced features for WebNN API
- WebNN API Specification - Official API documentation