Programming Model
WebNN (Web Neural Network API) provides a low-level interface for executing machine learning models in web browsers. Its programming model consists of two main steps: model construction and execution.
Overview
WebNN focuses solely on neural network operations and execution infrastructure, delegating higher-level tasks like model loading and parsing to specialized libraries (e.g., ONNX.js, TensorFlow.js) or the web application itself. This design makes WebNN format-agnostic and maintains a clear separation of concerns.
Core Steps
-
Model Construction The
MLGraphBuilder
API constructs the computational graph representing your model. Once defined, this graph is compiled into an executable form. -
Model Execution The compiled graph processes input data to generate predictions or classifications. WebNN provides methods to select appropriate ML backends, either explicitly or by capability requirements.
Hardware Acceleration
WebNN automatically leverages available hardware accelerators (CPU, GPU, NPU, TPU) to optimize model execution. This hardware-agnostic approach ensures:
- Optimal performance across different devices
- Reduced inference latency
- Smooth user experience