deno icon indicating copy to clipboard operation
deno copied to clipboard

Implement WebNN (Web Neural Network) API

Open littledivy opened this issue 1 year ago • 4 comments

Draft Spec: https://www.w3.org/TR/webnn/

From the spec:

At the heart of neural networks is a computational graph of mathematical operations. These operations are the building blocks of modern machine learning technologies in computer vision, natural language processing, and robotics. The WebNN API is a specification for constructing, compiling, and executing computational graphs of neural networks.

The Web Neural Network API defines a web-friendly hardware-agnostic abstraction layer that makes use of Machine Learning capabilities of operating systems and underlying hardware platforms without being tied to platform-specific capabilities. The abstraction layer addresses the requirements of key Machine Learning JavaScript frameworks and also allows web developers familiar with the ML domain to write custom code without the help of libraries.

Explainer: https://github.com/webmachinelearning/webnn/blob/main/explainer.md

Example

/* Computational graph 'C = A + B'. */
const operandType = { type: 'float32', dataType: 'float32', dimensions: [2, 2] };
const context = await navigator.ml.createContext();
const builder = new MLGraphBuilder(context);

const A = builder.input('A', operandType);
const B = builder.input('B', operandType);
const C = builder.add(A, B);

const graph = await builder.build({ 'C': C });

const bufferA = new Float32Array(4).fill(1.0);
const bufferB = new Float32Array(4).fill(1.0);
const bufferC = new Float32Array(4);

const inputs = { 'A': bufferA, 'B': bufferB };
const outputs = { 'C': bufferC };
const results = await context.compute(graph, inputs, outputs);

console.log(results.outputs.C); // [2, 2, 2, 2]

littledivy avatar Dec 17 '23 07:12 littledivy

no browser is pushing for it (nor is anyone from browers working on it), and in my opinion this is something that shouldnt even be in browsers.

crowlKats avatar Dec 17 '23 10:12 crowlKats

Chrome has been actively working on a prototype implementation: https://bugs.chromium.org/p/chromium/issues/detail?id=1273291. Last activitiy from 2 days ago

littledivy avatar Dec 17 '23 11:12 littledivy

Oh interesting, thats news to me. still, i am unsure if we should support such an API, especially due to its size, and that it will most likely be purely be implemented in js (and honestly really feels like something that should be a library and not a built-in in browsers)

crowlKats avatar Dec 17 '23 12:12 crowlKats

I would say lets wait if other browsers will even implement this, or if this becomes a chrome only for a few releases and then no one uses it anyone (like websocketstream).

crowlKats avatar Dec 17 '23 12:12 crowlKats