denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.43k stars 5.24k forks source link

Implement WebNN (Web Neural Network) API #21610

Open littledivy opened 9 months ago

littledivy commented 9 months ago

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]
crowlKats commented 9 months ago

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.

littledivy commented 9 months ago

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

crowlKats commented 9 months ago

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 commented 9 months ago

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).