Skip to Content
💡The WebNN Origin Trial is coming! Developers can sign up for trial keys to explore →
ReferenceWebNN APInavigator.ml

navigator.ml

Experimental: This is an experimental technology. Check the browser compatibility table carefully before using it in production.

The read-only ml property of the Navigator and WorkerNavigator interfaces returns an ML object, which is the entry point to the Web Neural Network (WebNN) API.

This feature is available only in secure contexts  (HTTPS or localhost), and is available in both the main thread and Web Workers .

Web IDL

web-idl
interface mixin NavigatorML { [SecureContext, SameObject] readonly attribute ML ml; }; Navigator includes NavigatorML; WorkerNavigator includes NavigatorML;

Value

An ML object. Because the attribute is marked [SameObject], the same ML object is returned every time the property is accessed within a given global object.

Description

navigator.ml gives access to the ML interface, whose createContext() method creates an MLContext. From the context you can build computational graphs with MLGraphBuilder, allocate MLTensor objects, and run inference on the CPU, GPU, or NPU of the device.

  • Window: available as window.navigator.ml.
  • Workers: available as self.navigator.ml through WorkerNavigator. Running inference in a worker keeps graph compilation and data transfers off the main thread.
  • Secure context only: in an insecure context (for example, a page served over plain HTTP), the ml property is not exposed, so 'ml' in navigator is false.

Permissions Policy

Use of WebNN is controlled by the Permissions Policy  feature identified by the string webnn. Its default allowlist is 'self', so WebNN is available to same-origin documents by default. Cross-origin iframes must be granted access explicitly:

iframe.html
<iframe src="https://example.com/inference.html" allow="webnn"></iframe>

When a document is not allowed to use the feature, the ml property is still present, but ML.createContext() returns a promise rejected with a SecurityError DOMException, and the MLGraphBuilder() constructor throws a SecurityError.

Examples

Feature detection

feature-detection.js
if ('ml' in navigator) { const context = await navigator.ml.createContext(); console.log('WebNN is available.'); } else { console.log('WebNN is not supported in this browser.'); }

Using WebNN in a worker

main.js
const worker = new Worker('worker.js', { type: 'module' }); worker.postMessage({ a: [1, 2, 3, 4], b: [5, 6, 7, 8] }); worker.onmessage = (e) => console.log('Result:', e.data); // Float32Array [6, 8, 10, 12]
worker.js
self.onmessage = async (e) => { const context = await navigator.ml.createContext(); const builder = new MLGraphBuilder(context); const desc = { dataType: 'float32', shape: [4] }; const graph = await builder.build({ c: builder.add(builder.input('a', desc), builder.input('b', desc)), }); const [a, b, c] = await Promise.all([ context.createTensor({ ...desc, writable: true }), context.createTensor({ ...desc, writable: true }), context.createTensor({ ...desc, readable: true }), ]); context.writeTensor(a, new Float32Array(e.data.a)); context.writeTensor(b, new Float32Array(e.data.b)); context.dispatch(graph, { a, b }, { c }); self.postMessage(new Float32Array(await context.readTensor(c))); };

Specifications

SpecificationStatus
Web Neural Network API — navigator.mlCandidate Recommendation Draft

The latest Editor’s Draft is available at webmachinelearning.github.io/webnn .

Browser compatibility

WebNN is under active development and support varies across browsers, operating systems, and hardware.

See Browser Compatibility: WebNN APIs.

See also

Last updated on