MLGraph
Experimental: This is an experimental technology. Check the browser compatibility table carefully before using it in production.
The MLGraph interface of the Web Neural Network (WebNN) API represents a compiled computational graph. Once built, a graph is immutable: its operators, inputs, outputs, and constant weights cannot be changed.
An MLGraph is obtained from MLGraphBuilder.build() and executed with MLContext.dispatch(). A graph is bound to the MLContext of the builder that created it and can only be dispatched on that context.
This feature is available only in secure contexts (HTTPS or localhost), and is available in both the main thread and Web Workers .
Web IDL
[SecureContext, Exposed=(Window, Worker)]
interface MLGraph {
undefined destroy();
};Constructor
None. Use MLGraphBuilder.build() to create an MLGraph.
Instance properties
None. The names and descriptors of a graph’s inputs and outputs are those that were used when it was built; dispatch() validates the tensors you pass against them.
Instance methods
destroy()
The destroy() method releases the resources associated with the graph. After it is called, the graph can no longer be dispatched: MLContext.dispatch() throws an InvalidStateError DOMException.
Work that was already dispatched with the graph still completes; the implementation frees the graph’s resources once that work has finished. Calling destroy() more than once has no further effect. Graphs are also destroyed automatically when their context is lost or destroyed.
Syntax
destroy()Parameters
None.
Return value
None (undefined).
Examples
Building, running, and destroying a graph
const context = await navigator.ml.createContext();
const builder = new MLGraphBuilder(context);
const desc = { dataType: 'float32', shape: [2, 3] };
const x = builder.input('x', desc);
const graph = await builder.build({ y: builder.relu(x) });
const input = await context.createTensor({ ...desc, writable: true });
const output = await context.createTensor({ ...desc, readable: true });
context.writeTensor(input, new Float32Array([-1, 2, -3, 4, -5, 6]));
context.dispatch(graph, { x: input }, { y: output });
console.log(new Float32Array(await context.readTensor(output))); // [0, 2, 0, 4, 0, 6]
// Free the compiled graph when it is no longer needed.
graph.destroy();Notes
- A graph can be dispatched any number of times, with different input and output tensors each time, as long as their data types and shapes match.
- Compilation in
build()may include weight preprocessing: constant data is transformed and cached in a device-specific format. Reuse a graph rather than rebuilding it for each inference. - Call
destroy()on graphs you no longer need, especially large models, to release device memory promptly instead of waiting for garbage collection.
Specifications
| Specification | Status |
|---|---|
Web Neural Network API — MLGraph interface | Candidate 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.