Repository navigation
Conversation
Add a WebNN (NPU) backend option and a Float32/Float16 data type toggle to style_transfer, semantic_segmentation, face_recognition, facial_landmark_detection and lenet. The audio samples (nsnet2, rnnoise) are unchanged. Float16 converts the fp32 weights at load time and casts the graph input to float16 and the output(s) back to float32, so JS pre/post-processing is unchanged. Data type selection per device: - image_classification and object_detection used to force Float16 and disable Float32 whenever NPU was selected. NPU stacks differ in what they expect, so both buttons are now enabled from the device's opSupportLimits(), via a new utils.selectDataTypeForDevice() used by all eight vision samples (including selfie_segmentation). - Switching devices selects the preferred data type: float16 on NPU, float32 on CPU/GPU (selfie_segmentation keeps float16 everywhere). - The float16 check covers graph inputs/outputs, constants, cast and conv2d, so a model can still fail to build on a device lacking some other op in float16. selfie_segmentation's float16 models use float16 graph inputs/outputs directly and are checked for that. - opSupportLimits() is queried once per device and cached. If the query fails, the samples fall back and the real error is reported by the sample's normal alert. Float16 precision in style_transfer: - Instance normalization runs in float32 (cast in and out, float32 scale/bias): its variance overflows float16 on some GPU backends, which produced flat or striped output. - The raw 0-255 input is scaled by 1/16 before the first conv, whose outputs reach about 68,500 for The Starry Night, above float16's max of 65,504. That conv has no bias and feeds instance normalization, which is scale invariant, so the output is unchanged (within 0.0034/255 on CPU). This applies to float32 graphs too, since NPUs may compute them in float16. Other changes: - common/component/component.js: the NPU availability probe targeted a stale #webnn_npu id and never disabled anything; it now targets #npu. - Rename the vision samples' webnn_cpu/webnn_gpu button ids to cpu/gpu/npu and drop the backend/lastBackend bookkeeping. - Document the data type toggle in the lenet and style_transfer READMEs. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
92daea1d1f6554efda2f22480a45e2d5e60216ebβ Automated harness on two Intel Core Ultra devices (Lunar Lake, Panther Lake): all 8 vision samples pass on CPU, GPU and NPU in both data types, in both WebNN architectures, with no CPU/GPU regressions vs
master.β Style transfer float16 is within 0.1/255 of float32 on every backend tested.
β Not validated on AMD or Qualcomm NPUs.
β The float16 capability check covers conv2d, cast and I/O only, not every op.
β Upstream CI (lint) not yet run; overlaps #340 in
image_classification/main.js.Summary
Adds a WebNN (NPU) backend option and a Float32/Float16 toggle to the vision samples that didn't have them: style transfer, semantic segmentation, face recognition, facial landmark detection and LeNet. It also replaces the "NPU β Float16 only" rule in image classification and object detection with per-device selection based on
opSupportLimits(). The audio samples (NSNet2, RNNoise) are unchanged.Changes
Float16 models. fp32 weights are converted to fp16 at load time, and the graph casts its input to float16 and its output(s) back to float32. JS pre- and post-processing is unchanged. The shared SSD MobileNet V2 Face model (used by both face samples) is updated once.
Data type selection per device (
utils.selectDataTypeForDevice(), used by all 8 vision samples):opSupportLimits(), instead of disabling Float32 whenever NPU is selected. NPU stacks differ: some run float16 natively, others take float32.opSupportLimits()is queried once per device and cached. If the query fails, the sample falls back and reports the real error through its normal alert.Float16 precision in style transfer:
Other:
common/component/component.js: the NPU availability probe targeted a stale#webnn_npuid, so it never disabled the NPU button. It now targets#npu, and only runs on pages that have one.webnn_cpu/webnn_gpubutton ids are renamed tocpu/gpu/npu, and thebackend/lastBackendbookkeeping is gone.Testing
npm run lint: clean.--webnn-ort-disable-cpu-fallback. Logs confirm all nodes ran on the NPU.master: same pass/fail results, and timings within noise.Notes for reviewers
image_classification/main.js. Whichever lands first, I'm happy to rebase. TheopSupportLimits()-based selection here should also cover Enable NPU + FLoat32 for Image Classification and ResNet50 FP32 modelΒ #340's NPU + float32 case.--webnn-ort-logging-level=INFO|VERBOSEflag, an OpenVINO NPU compiler issue can terminate the GPU process when the last NPU context is destroyed. The samples' capability probe does that on page load. The issue has been reported to Intel, and the default configuration isn't affected.