From d5e547b3e14beeadca14818864a8834f1615475e Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Mon, 4 May 2026 10:59:28 +0200 Subject: [PATCH 1/2] deps: make ONNX support optional --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 6 +++++- src/lib.rs | 7 +++++-- src/main.rs | 4 ++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc64b9c..a7b7dad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anstream" version = "0.6.21" @@ -215,6 +221,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "generic-array" version = "0.14.7" @@ -253,6 +265,13 @@ name = "hashbrown" version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", + "serde", + "serde_core", +] [[package]] name = "heck" @@ -515,6 +534,17 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "safetensors" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "675656c1eabb620b921efea4f9199f97fc86e36dd6ffd1fbbe48d0f59a4987f5" +dependencies = [ + "hashbrown", + "serde", + "serde_json", +] + [[package]] name = "serde" version = "1.0.228" @@ -665,9 +695,11 @@ dependencies = [ "anyhow", "base64", "clap", + "half", "pest", "pest_derive", "prost", + "safetensors", "serde", "serde_json", "tempfile", diff --git a/Cargo.toml b/Cargo.toml index e2738bf..7b3b9a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,10 @@ path = "src/lib.rs" name = "webnn-graph" path = "src/main.rs" +[features] +default = ["onnx"] +onnx = ["dep:webnn-onnx-utils"] + [dependencies] anyhow = "1.0" serde = { version = "1.0", features = ["derive"] } @@ -25,7 +29,7 @@ pest_derive = "2.7" clap = { version = "4.5", features = ["derive"] } base64 = "0.22" prost = "0.12" -webnn-onnx-utils = { git = "https://github.com/rustnn/webnn-onnx-utils", branch = "main" } +webnn-onnx-utils = { git = "https://github.com/rustnn/webnn-onnx-utils", branch = "main", optional = true } [dev-dependencies] tempfile = "3.8" diff --git a/src/lib.rs b/src/lib.rs index 83ce42c..596623d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,10 +5,13 @@ pub use external_weights::{resolve_external_weights, WeightResolveError}; pub mod emit_html; pub mod emit_js; -pub mod onnx; pub mod parser; -pub mod protos; pub mod serialize; pub mod validate; pub mod weights; pub mod weights_io; + +#[cfg(feature = "onnx")] +pub mod onnx; +#[cfg(feature = "onnx")] +pub mod protos; diff --git a/src/main.rs b/src/main.rs index 2504839..26685ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ use clap::{Parser, Subcommand}; use std::fs; +#[cfg(feature = "onnx")] use std::path::Path; use webnn_graph::ast::GraphJson; use webnn_graph::emit_html::emit_html; use webnn_graph::emit_js::{emit_builder_js, emit_weights_loader_js}; +#[cfg(feature = "onnx")] use webnn_graph::onnx::convert::{convert_onnx, ConvertOptions}; use webnn_graph::parser::parse_wg_text; use webnn_graph::serialize::serialize_graph_to_wg_text; @@ -94,6 +96,7 @@ enum Command { #[arg(long)] output: String, }, + #[cfg(feature = "onnx")] ConvertOnnx { /// Input ONNX model file (.onnx) #[arg(long)] @@ -256,6 +259,7 @@ fn main() -> anyhow::Result<()> { fs::write(&output, json)?; eprintln!("Wrote graph with inline weights to: {}", output); } + #[cfg(feature = "onnx")] Command::ConvertOnnx { input, output, From 4c04daffaccc7fab6f7ef4931206e0bae2a6176e Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Mon, 4 May 2026 11:03:50 +0200 Subject: [PATCH 2/2] ci: test without protoc and ONNX support --- .github/workflows/ci.yml | 23 +++++++++++++---------- Makefile | 34 ++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 65cdba6..8d54518 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,12 +12,15 @@ env: jobs: test: - name: Test on ${{ matrix.os }} + name: Test (${{ matrix.os }}, minimal=${{ matrix.minimal }}) runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] + minimal: [true, false] + env: + CARGO_FLAGS: ${{ matrix.minimal && '--no-default-features' || '' }} steps: - name: Checkout code @@ -29,42 +32,42 @@ jobs: components: rustfmt, clippy - name: Install protoc (Linux) - if: runner.os == 'Linux' + if: ${{ runner.os == 'Linux' && !matrix.minimal }} run: | sudo apt-get update sudo apt-get install -y protobuf-compiler - name: Install protoc (macOS) - if: runner.os == 'macOS' + if: ${{ runner.os == 'macOS' && !matrix.minimal }} run: brew install protobuf - name: Install protoc (Windows) - if: runner.os == 'Windows' + if: ${{ runner.os == 'Windows' && !matrix.minimal }} run: choco install protoc - name: Cache cargo registry uses: actions/cache@v4 with: path: ~/.cargo/registry - key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + key: ${{ runner.os }}-${{ matrix.minimal }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} restore-keys: | - ${{ runner.os }}-cargo-registry- + ${{ runner.os }}-${{ matrix.minimal }}-cargo-registry- - name: Cache cargo index uses: actions/cache@v4 with: path: ~/.cargo/git - key: ${{ runner.os }}-cargo-git-${{ hashFiles('**/Cargo.lock') }} + key: ${{ runner.os }}-${{ matrix.minimal }}-cargo-git-${{ hashFiles('**/Cargo.lock') }} restore-keys: | - ${{ runner.os }}-cargo-git- + ${{ runner.os }}-${{ matrix.minimal }}-cargo-git- - name: Cache target directory uses: actions/cache@v4 with: path: target - key: ${{ runner.os }}-target-${{ hashFiles('**/Cargo.lock') }} + key: ${{ runner.os }}-${{ matrix.minimal }}-target-${{ hashFiles('**/Cargo.lock') }} restore-keys: | - ${{ runner.os }}-target- + ${{ runner.os }}-${{ matrix.minimal }}-target- - name: Check formatting run: make fmt-check diff --git a/Makefile b/Makefile index 800b968..558305b 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,29 @@ -.PHONY: all build test fmt clean run check lint help parse validate emit-js emit-html coverage +.PHONY: all build test fmt clean run check lint help parse validate emit-js emit-html coverage minimal + +CARGO_FLAGS ?= # Default target all: build # Build the project build: - cargo build + cargo build $(CARGO_FLAGS) + +# Build with --no-default-features +minimal: + cargo build --no-default-features # Build with release optimizations release: - cargo build --release + cargo build --release $(CARGO_FLAGS) # Run all tests test: - cargo test + cargo test $(CARGO_FLAGS) # Run tests with output test-verbose: - cargo test -- --nocapture + cargo test $(CARGO_FLAGS) -- --nocapture # Format code fmt: @@ -29,11 +35,11 @@ fmt-check: # Run clippy linter lint: - cargo clippy -- -D warnings + cargo clippy $(CARGO_FLAGS) -- -D warnings # Quick compile check without building check: - cargo check + cargo check $(CARGO_FLAGS) # Clean build artifacts clean: @@ -41,25 +47,25 @@ clean: # Run the CLI tool (parse example) run: - cargo run --bin webnn-graph -- parse examples/resnet_head.webnn + cargo run $(CARGO_FLAGS) --bin webnn-graph -- parse examples/resnet_head.webnn # Parse example graph parse: - cargo run --bin webnn-graph -- parse examples/resnet_head.webnn + cargo run $(CARGO_FLAGS) --bin webnn-graph -- parse examples/resnet_head.webnn # Validate example graph validate: - cargo run --bin webnn-graph -- parse examples/resnet_head.webnn > /tmp/graph.json && \ - cargo run --bin webnn-graph -- validate /tmp/graph.json --weights-manifest examples/weights.manifest.json + cargo run $(CARGO_FLAGS) --bin webnn-graph -- parse examples/resnet_head.webnn > /tmp/graph.json && \ + cargo run $(CARGO_FLAGS) --bin webnn-graph -- validate /tmp/graph.json --weights-manifest examples/weights.manifest.json # Emit JavaScript for example graph emit-js: - cargo run --bin webnn-graph -- parse examples/resnet_head.webnn > /tmp/graph.json && \ - cargo run --bin webnn-graph -- emit-js /tmp/graph.json + cargo run $(CARGO_FLAGS) --bin webnn-graph -- parse examples/resnet_head.webnn > /tmp/graph.json && \ + cargo run $(CARGO_FLAGS) --bin webnn-graph -- emit-js /tmp/graph.json # Generate HTML visualizer for example graph emit-html: - cargo run --bin webnn-graph -- emit-html examples/resnet_head.webnn > /tmp/webnn_viz.html + cargo run $(CARGO_FLAGS) --bin webnn-graph -- emit-html examples/resnet_head.webnn > /tmp/webnn_viz.html @echo "Visualizer generated: /tmp/webnn_viz.html" @echo "Open it with: open /tmp/webnn_viz.html"