WebNN (Web Neural Network API)

The WebNN API provides a web standard for neural network inference, enabling high-performance machine learning directly in the browser. Firefox’s implementation uses ONNX Runtime as the backend for executing neural network operations across different hardware accelerators.

The API is exposed via navigator.ml and allows web applications to:

  • Build computational graphs using common neural network operations

  • Execute inference using hardware acceleration (CPU, GPU, NPU when available)

  • Work with tensors for efficient data transfer

  • Run pre-trained models in various formats

To enable the WebNN API, set the dom.ml.enabled preference to true in about:config.

// Create a context
const context = await navigator.ml.createContext();

// Build a computational graph
const builder = new MLGraphBuilder(context);
const input = builder.input('x', { dataType: 'float32', shape: [3] });
const output = builder.add(input, constant);
const graph = await builder.build({ 'output': output });

// Run inference
await context.compute(graph,
    { 'x': inputData },
    { 'output': outputData }
);

Learn more about the WebNN implementation:

Demo Pages

The dom/webnn/docs/ directory also contains interactive HTML demos:

  • webnn_demo.html - Basic WebNN operations (linear operations, matrix multiplication)

  • mobilenet_complete.html - Full MobileNetV2 image classification with pretrained weights

  • README.md - Demo documentation and usage instructions

See the README.md file in the docs directory for how to run these demos.

Additional Resources