Skip to content

Enable NPU and Float16 for the vision samples - #342

Open
adrastogi wants to merge 1 commit into
webmachinelearning:masterfrom
adrastogi:adrastogi/enable-npu
Open

adrastogi wants to merge 1 commit into
webmachinelearning:masterfrom
adrastogi:adrastogi/enable-npu

Conversation

@adrastogi

Copy link
Copy Markdown

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 92daea1d1f6554efda2f22480a45e2d5e60216eb

Dimension Assessment Rationale
Change confidence 8/10 ↑ ESLint clean; every changed file stored with LF.
↑ 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):

  • Both data types are enabled based on the device's opSupportLimits(), instead of disabling Float32 whenever NPU is selected. NPU stacks differ: some run float16 natively, others take float32.
  • Switching devices selects the preferred data type: float16 on NPU, float32 on CPU and GPU. Selfie segmentation keeps its float16 default everywhere.
  • The float16 check covers graph inputs/outputs, constants, cast and conv2d, so it's a heuristic: a model can still fail to build if a device lacks some other op in float16. Selfie segmentation's float16 models use float16 graph inputs and outputs directly, and are checked for that.
  • 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:

  • Instance normalization runs in float32 (cast in and out, with float32 scale and bias). In float16, its variance overflowed on some GPU backends and produced flat or striped output.
  • The raw 0–255 input is scaled by 1/16 before the first conv. That conv's outputs reach about 68,500 for The Starry Night, above float16's max of 65,504, which left a dark streak on some GPUs. The conv has no bias and feeds instance normalization, which is scale invariant, so the output is unchanged (within 0.0034/255 on CPU). The scaling also applies to float32 graphs, since NPUs may compute those in float16.

Other:

  • common/component/component.js: the NPU availability probe targeted a stale #webnn_npu id, so it never disabled the NPU button. It now targets #npu, and only runs on pages that have one.
  • The vision samples' webnn_cpu/webnn_gpu button ids are renamed to cpu/gpu/npu, and the backend/lastBackend bookkeeping is gone.
  • The LeNet and style transfer READMEs describe the data type toggle.

Testing

  • npm run lint: clean.
  • An automated harness ran on two Intel Core Ultra laptops (Lunar Lake and Panther Lake, Intel NPU) in Chrome Canary 159/160. It covered both WebNN architectures (ONNX Runtime in the GPU process, and the WebNN compiler process), with these backends: OpenVINO EP on NPU and CPU, DirectML on GPU, and LiteRT for the compiler process's GPU/CPU fallback.
    • All 8 vision samples pass on NPU in float32 and float16, also with --webnn-ort-disable-cpu-fallback. Logs confirm all nodes ran on the NPU.
    • CPU/GPU vs master: same pass/fail results, and timings within noise.
    • Style transfer, all 9 styles: float16 is within 0.1/255 of same-device float32 on DirectML, LiteRT GPU, CPU and NPU. On NPU, float32 builds and runs within about 10% of float16, with identical output.
  • Data type gating was tested with mocked NPUs that report float16 only, report float32 only, or fail context creation. In each case the right button was disabled, a supported type was selected, and there were no unhandled errors.
  • Not tested: AMD and Qualcomm NPUs.

Notes for reviewers

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant