EXPERIMENTAL: Python bindings for W3C WebNN API - Neural network graph validation, ONNX/CoreML conversion, and cross-platform execution with CPU/GPU/NPU support
Project description
rustnn / PyWebNN
A Rust implementation of WebNN graph handling with Python bindings that implement the W3C WebNN API specification.
[WARNING] EXPERIMENTAL - DO NOT USE IN PRODUCTION
This is an early-stage experimental implementation for research and exploration. Many features are incomplete, untested, or may change significantly.
What is rustnn?
rustnn provides:
- Rust Library: Validates WebNN graphs and converts to ONNX/CoreML formats
- Python API: Complete W3C WebNN API implementation via PyO3 bindings
- Runtime Backends: Execute on CPU, GPU, or Neural Engine with backend selection at context creation
- Real Examples: Complete MobileNetV2 (99.60% accuracy) and Transformer text generation
Installation
Python Package (PyWebNN)
PyPI Package (v0.4.0+):
# Install with bundled ONNX Runtime - no additional dependencies needed
pip install pywebnn
# Works immediately with actual execution (no zeros)
Build from Source (For Development):
git clone https://github.com/tarekziade/rustnn.git
cd rustnn
make python-dev # Sets up venv and builds with ONNX Runtime + CoreML
source .venv-webnn/bin/activate
Requirements: Python 3.11+, NumPy 1.20+
Note: Version 0.4.0+ includes bundled ONNX Runtime. Earlier versions (0.3.0 and below) had no backends and returned zeros.
Rust Library
[dependencies]
rustnn = "0.1"
Quick Start
import webnn
import numpy as np
# Create ML context with device hints
ml = webnn.ML()
context = ml.create_context(accelerated=False) # CPU execution
builder = context.create_graph_builder()
# Build a simple graph: output = relu(x + y)
x = builder.input("x", [2, 3], "float32")
y = builder.input("y", [2, 3], "float32")
z = builder.add(x, y)
output = builder.relu(z)
# Compile the graph
graph = builder.build({"output": output})
# Execute with real data
x_data = np.array([[1, -2, 3], [4, -5, 6]], dtype=np.float32)
y_data = np.array([[-1, 2, -3], [-4, 5, -6]], dtype=np.float32)
results = context.compute(graph, {"x": x_data, "y": y_data})
print(results["output"]) # [[0. 0. 0.] [0. 0. 0.]]
# Optional: Export to ONNX
context.convert_to_onnx(graph, "model.onnx")
Backend Selection
Following the W3C WebNN Device Selection spec, backends are selected via hints:
# CPU-only execution
context = ml.create_context(accelerated=False)
# Request GPU/NPU (platform selects best available)
context = ml.create_context(accelerated=True)
# Request high-performance (prefers GPU)
context = ml.create_context(accelerated=True, power_preference="high-performance")
# Request low-power (prefers NPU/Neural Engine)
context = ml.create_context(accelerated=True, power_preference="low-power")
Platform-Specific Backends:
- NPU: CoreML Neural Engine (Apple Silicon macOS only)
- GPU: ONNX Runtime GPU (cross-platform) or CoreML GPU (macOS)
- CPU: ONNX Runtime CPU (cross-platform)
Examples
Complete MobileNetV2 Image Classification
# Download pretrained weights (first time only)
bash scripts/download_mobilenet_weights.sh
# Run on different backends
python examples/mobilenetv2_complete.py examples/images/test.jpg --backend cpu
python examples/mobilenetv2_complete.py examples/images/test.jpg --backend gpu
python examples/mobilenetv2_complete.py examples/images/test.jpg --backend coreml
Output:
Top 5 Predictions (Real ImageNet Labels):
1. lesser panda 99.60%
2. polecat 0.20%
3. weasel 0.09%
Performance: 74.41ms (CPU) / 77.14ms (GPU) / 51.93ms (CoreML)
Text Generation with Transformer Attention
# Run generation with attention
make text-gen-demo
# Train on custom text
make text-gen-train
# Generate with trained weights
make text-gen-trained
See examples/ for more samples.
Documentation
- Getting Started - Installation and first steps
- API Reference - Complete Python API documentation
- Examples - Code examples and tutorials
- Architecture - Design principles and structure
- Development Guide - Building and contributing
Implementation Status
- 85 of ~95 WebNN operations (89% spec coverage)
- Shape inference: 85/85 (100%)
- Python API: 85/85 (100%)
- ONNX Backend: 85/85 (100%)
- CoreML MLProgram: 85/85 (100%)
- 1350+ WPT conformance tests passing
See docs/development/implementation-status.md for complete details.
Rust CLI Usage
# Validate a graph
cargo run -- examples/sample_graph.json
# Visualize a graph (requires graphviz)
cargo run -- examples/sample_graph.json --export-dot graph.dot
dot -Tpng graph.dot -o graph.png
# Convert to ONNX
cargo run -- examples/sample_graph.json --convert onnx --convert-output model.onnx
# Execute with ONNX Runtime
cargo run --features onnx-runtime -- examples/sample_graph.json --convert onnx --run-onnx
See make help for all available targets.
Contributing
Contributions welcome! Please see:
- AGENTS.md - Project architecture and conventions
- docs/development/contributing.md - How to add features
- TODO.txt - Feature requests and known issues
Quick Contribution Guide:
- Fork and create feature branch:
git checkout -b feature/my-feature - Install hooks (optional):
./scripts/install-git-hooks.sh - Make changes and test:
make test && make python-test - Format code:
make fmt - Commit and push
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Links
- GitHub: https://github.com/tarekziade/rustnn
- PyPI: https://pypi.org/project/pywebnn/
- Documentation: https://tarekziade.github.io/rustnn/
- W3C WebNN Spec: https://www.w3.org/TR/webnn/
Acknowledgments
- W3C WebNN Community Group for the specification
- Chromium WebNN implementation for reference
- PyO3 and Maturin projects for excellent Python-Rust integration
Made with Rust by Tarek Ziade
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pywebnn-0.5.11.tar.gz.
File metadata
- Download URL: pywebnn-0.5.11.tar.gz
- Upload date:
- Size: 304.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d3e0b0858bbd4670f5290f58cdeb0a7bb8496ee100d6533cb25a5aceb295baa
|
|
| MD5 |
5aea4c02ef461be0dbe19371eae6468d
|
|
| BLAKE2b-256 |
4f3cee105907ae846cc552c104fc622c2582fd19b4e9b1ce571c9949ae5b531d
|
Provenance
The following attestation bundles were made for pywebnn-0.5.11.tar.gz:
Publisher:
publish-pypi.yml on tarekziade/rustnn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pywebnn-0.5.11.tar.gz -
Subject digest:
5d3e0b0858bbd4670f5290f58cdeb0a7bb8496ee100d6533cb25a5aceb295baa - Sigstore transparency entry: 781560031
- Sigstore integration time:
-
Permalink:
tarekziade/rustnn@28788cce0582d030c455688b4789e5f72fb62342 -
Branch / Tag:
refs/tags/v0.5.11 - Owner: https://github.com/tarekziade
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@28788cce0582d030c455688b4789e5f72fb62342 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pywebnn-0.5.11-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: pywebnn-0.5.11-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab97a22936fc48dc3a2274b6cc7be65b944543b26e4f12ddefb8e9038289d60d
|
|
| MD5 |
8bef74987a2d003bdbde2553399e9f99
|
|
| BLAKE2b-256 |
74fb2d1208165088ee9f39a7838a03ff9dd60edf29eacf4f47d06f193e635070
|
Provenance
The following attestation bundles were made for pywebnn-0.5.11-cp312-cp312-win_amd64.whl:
Publisher:
publish-pypi.yml on tarekziade/rustnn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pywebnn-0.5.11-cp312-cp312-win_amd64.whl -
Subject digest:
ab97a22936fc48dc3a2274b6cc7be65b944543b26e4f12ddefb8e9038289d60d - Sigstore transparency entry: 781560079
- Sigstore integration time:
-
Permalink:
tarekziade/rustnn@28788cce0582d030c455688b4789e5f72fb62342 -
Branch / Tag:
refs/tags/v0.5.11 - Owner: https://github.com/tarekziade
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@28788cce0582d030c455688b4789e5f72fb62342 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pywebnn-0.5.11-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pywebnn-0.5.11-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
268e24e218487e91601913c81b810130a25b782b601f5842da8a3a6acacdc3dd
|
|
| MD5 |
cca7a0955e1d661663ce115150f8f2d2
|
|
| BLAKE2b-256 |
a26e0016c873b16283c41052dd42069385180c1f173e1b71f97a1b40480f646c
|
Provenance
The following attestation bundles were made for pywebnn-0.5.11-cp312-cp312-manylinux_2_28_x86_64.whl:
Publisher:
publish-pypi.yml on tarekziade/rustnn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pywebnn-0.5.11-cp312-cp312-manylinux_2_28_x86_64.whl -
Subject digest:
268e24e218487e91601913c81b810130a25b782b601f5842da8a3a6acacdc3dd - Sigstore transparency entry: 781560061
- Sigstore integration time:
-
Permalink:
tarekziade/rustnn@28788cce0582d030c455688b4789e5f72fb62342 -
Branch / Tag:
refs/tags/v0.5.11 - Owner: https://github.com/tarekziade
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@28788cce0582d030c455688b4789e5f72fb62342 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pywebnn-0.5.11-cp312-cp312-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: pywebnn-0.5.11-cp312-cp312-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 1.6 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
337cc67c9d43e06ceaa5726b92d178944dda5b822ec29331bc784317d71a83be
|
|
| MD5 |
36240ccdf8a83629e923af949a6ea470
|
|
| BLAKE2b-256 |
a688618f51e0f3b77a32690d9a7a0e8ece8d9d35e3e63e3f8b28f609e5e02dbe
|
Provenance
The following attestation bundles were made for pywebnn-0.5.11-cp312-cp312-manylinux_2_28_aarch64.whl:
Publisher:
publish-pypi.yml on tarekziade/rustnn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pywebnn-0.5.11-cp312-cp312-manylinux_2_28_aarch64.whl -
Subject digest:
337cc67c9d43e06ceaa5726b92d178944dda5b822ec29331bc784317d71a83be - Sigstore transparency entry: 781560057
- Sigstore integration time:
-
Permalink:
tarekziade/rustnn@28788cce0582d030c455688b4789e5f72fb62342 -
Branch / Tag:
refs/tags/v0.5.11 - Owner: https://github.com/tarekziade
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@28788cce0582d030c455688b4789e5f72fb62342 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pywebnn-0.5.11-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: pywebnn-0.5.11-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46c52eb62f74cbcc385a1f0c2d2c78549b19319e71d1a13d02a7ec1041aba767
|
|
| MD5 |
bd2a97cab022144c5ccf280f4a20a2d2
|
|
| BLAKE2b-256 |
57d0a5f6394fa9a520e0108f0a8b5c26ecf00619a0238f92b52cf77271c62e1b
|
Provenance
The following attestation bundles were made for pywebnn-0.5.11-cp312-cp312-macosx_11_0_arm64.whl:
Publisher:
publish-pypi.yml on tarekziade/rustnn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pywebnn-0.5.11-cp312-cp312-macosx_11_0_arm64.whl -
Subject digest:
46c52eb62f74cbcc385a1f0c2d2c78549b19319e71d1a13d02a7ec1041aba767 - Sigstore transparency entry: 781560048
- Sigstore integration time:
-
Permalink:
tarekziade/rustnn@28788cce0582d030c455688b4789e5f72fb62342 -
Branch / Tag:
refs/tags/v0.5.11 - Owner: https://github.com/tarekziade
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@28788cce0582d030c455688b4789e5f72fb62342 -
Trigger Event:
release
-
Statement type: