Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ jobs:
python-version: ["3.10", "3.11", "3.12", "3.13"]

steps:
# Allow checkout of Rust dependency snapshots whose paths exceed Windows' default limit.
- name: Enable Git longpaths (Windows)
if: runner.os == 'Windows'
run: git config --global core.longpaths true

- uses: actions/checkout@v4

- name: Set up Python
Expand Down
16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pywebnn"
version = "0.5.11"
version = "0.5.13"
edition = "2021"
description = "Python bindings for W3C WebNN specification via rustnn"
license = "Apache-2.0"
Expand All @@ -9,28 +9,32 @@ keywords = ["webnn", "python", "machine-learning", "neural-networks", "ai"]
categories = ["api-bindings", "science"]

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]
name = "pywebnn"

[features]
default = ["onnx-runtime", "coreml-runtime", "dynamic-inputs"]
default = ["onnx-runtime", "dynamic-inputs"]
onnx-runtime = ["rustnn/onnx-runtime", "ort", "ndarray"]
coreml-runtime = ["rustnn/coreml-runtime", "objc"]
coreml-runtime = ["rustnn/coreml-runtime"]
trtx-runtime = ["rustnn/trtx-runtime"]
trtx-runtime-mock = ["rustnn/trtx-runtime-mock"]
litert-runtime = ["rustnn/litert-runtime"]
cann-runtime = ["rustnn/cann-runtime"]
cann-runtime-mock = ["rustnn/cann-runtime-mock"]
dynamic-inputs = ["rustnn/dynamic-inputs"]

[dependencies]
pyo3 = { version = "0.28", features = ["extension-module"] }
bytemuck = "1.14"
rustnn = { git = "https://github.com/rustnn/rustnn", branch = "main" }
serde_json = "1.0"
webnn-graph = { git = "https://github.com/rustnn/webnn-graph", branch = "main" }
half = "2.4"
regex = "1.11"
pretty_env_logger = "0.5"
# Optional runtime dependencies
ndarray = { version = "0.16", optional = true }
ort = { version = "=2.0.0-rc.11", optional = true, features = ["ndarray", "half", "load-dynamic"] }
objc = { version = "0.2", optional = true }
ort = { version = "=2.0.0-rc.13", optional = true, features = ["ndarray", "half", "load-dynamic"] }

# Make this crate independent from the parent workspace
[workspace]
30 changes: 18 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
.PHONY: help setup setup-demos build test clean dev install lint fmt check all \
minilm-demo-hub mobilenet-demo-hub smollm-demo-hub run-all-demo run-all-demos

# Python interpreter used by Make targets (override in CI or local env as needed)
PYTHON ?= python3
VENV_DIR = .venv

# Invoke the virtual-environment interpreter directly. This avoids activating
# the environment through shell-specific syntax, so `make test` works with both
# cmd.exe on Windows and POSIX shells on Linux/macOS.
ifeq ($(OS),Windows_NT)
PYTHON ?= python
VENV_PYTHON = $(VENV_DIR)/Scripts/python.exe
VENV_PIP = $(VENV_DIR)/Scripts/pip.exe
VENV_ACTIVATE = $(VENV_DIR)/Scripts/activate
else
PYTHON ?= python3
VENV_PYTHON = $(VENV_DIR)/bin/python
VENV_PIP = $(VENV_DIR)/bin/pip
VENV_ACTIVATE = $(VENV_DIR)/bin/activate
endif

help:
@echo "pywebnn - Python bindings for W3C WebNN"
Expand Down Expand Up @@ -33,13 +45,13 @@ setup:
@echo "Creating virtual environment..."
$(PYTHON) -m venv $(VENV_DIR)
@echo "Installing development dependencies..."
$(VENV_DIR)/bin/pip install --upgrade pip
$(VENV_DIR)/bin/pip install maturin pytest pytest-asyncio numpy onnxruntime
$(VENV_PIP) install --upgrade pip
$(VENV_PIP) install maturin pytest pytest-asyncio numpy onnxruntime
@echo "Virtual environment ready at $(VENV_DIR)"

setup-demos: setup
@echo "Installing demo dependencies..."
$(VENV_DIR)/bin/pip install transformers torch Pillow requests --extra-index-url https://download.pytorch.org/whl/cpu
$(VENV_PIP) install transformers torch Pillow requests --extra-index-url https://download.pytorch.org/whl/cpu
@echo "Demo dependencies installed"

dev: setup
Expand All @@ -62,14 +74,8 @@ install: build
@echo "Installation complete"

test:
@if [ ! -d "$(VENV_DIR)" ]; then \
echo "Virtual environment not found. Run 'make setup' first."; \
exit 1; \
fi
@echo "Running Python tests..."
@ORT_DYLIB_PATH="$$(. $(VENV_ACTIVATE) && python tools/resolve_ort_dylib.py)" && \
echo "Using ORT_DYLIB_PATH=$$ORT_DYLIB_PATH" && \
. $(VENV_ACTIVATE) && ORT_DYLIB_PATH="$$ORT_DYLIB_PATH" pytest tests/ -v
@$(VENV_PYTHON) tools/run_tests.py

lint:
@echo "Running Rust linting..."
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ Python bindings for the W3C WebNN API, powered by `rustnn`.
pip install pywebnn
```

## Development

Install the development dependencies and native extension in editable mode:

```powershell
python -m pip install --upgrade pip
python -m pip install maturin numpy tokenizers
maturin develop
```

Re-run `maturin develop` after changing Rust code. To build with TensorRT RTX
support, use `maturin develop --features trtx-runtime` and ensure the matching
TensorRT runtime DLLs are available on `PATH` before launching Python.

## Docs

Full documentation is published on GitHub Pages:
Expand Down Expand Up @@ -41,6 +55,27 @@ result = context.compute(
print(result["output"])
```

## TensorRT backend

TensorRT is opt-in. Build an editable installation with RustNN's TensorRT feature,
then request it when creating the context:

```bash
maturin develop --features trtx-runtime
```

```python
context = webnn.ML().create_context(backend="trtx")
```

This requires a compatible TensorRT installation and GPU. Use
`context.backend_info()` to inspect the requested backend and compiled features.
On Windows, make the matching TensorRT RTX runtime DLLs available through
`PATH` before starting Python. The DLL version must match the TensorRT headers
used when building the extension.
Other opt-in runtime plugins are `litert-runtime` and `cann-runtime`; select them
with `backend="litert"` and `backend="cann"`, respectively.

## Links

- GitHub: <https://github.com/rustnn/pywebnn>
Expand Down
Loading
Loading