Skip to content

Commit

Permalink
WebNN: Implement MLBuffer creation/destroy.
Browse files Browse the repository at this point in the history
Introduces MLBuffer to the WebNN service. Since MLBuffer exists in the
 domain of the context, instead of a graph, a dedicated command
 recorder was needed to ensure future context operations
 (ie. readBuffer) get applied prior to graph operations.

* Defines handles to identify objects in the ML service.
* Defines MLBuffer interfaces.
* Implements buffer creation and destruction.

webmachinelearning/webnn#482

Bug: 1472888
Change-Id: I852eff452346a968812e9f248fbb0a4cfc917dbc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5173676
Reviewed-by: ningxin hu <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Reviewed-by: Alex Gough <[email protected]>
Reviewed-by: Rafael Cintron <[email protected]>
Commit-Queue: Bryan Bernhart <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1270252}
  • Loading branch information
bbernhar authored and BruceDai committed Mar 25, 2024
1 parent 6d700ed commit 888ed78
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
12 changes: 12 additions & 0 deletions webnn/conformance_tests/buffer.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// META: title=test WebNN API buffer operations
// META: global=window,dedicatedworker
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#api-mlbuffer

testCreateWebNNBuffer("create", 4);

testDestroyWebNNBuffer("destroyTwice");
12 changes: 12 additions & 0 deletions webnn/conformance_tests/gpu/buffer.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// META: title=test WebNN API buffer operations
// META: global=window,dedicatedworker
// META: script=../../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#api-mlbuffer

testCreateWebNNBuffer("create", 4, 'gpu');

testDestroyWebNNBuffer("destroyTwice", 'gpu');
56 changes: 56 additions & 0 deletions webnn/resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,4 +898,60 @@ const toHalf = (value) => {
* the exponent, which is OK. */
bits += m & 1;
return bits;
};


/**
* WebNN buffer creation.
* @param {MLContext} context - the context used to create the buffer.
* @param {Number} bufferSize - Size of the buffer to create, in bytes.
*/
const createBuffer = (context, bufferSize) => {
let buffer;
try {
buffer = context.createBuffer({size: bufferSize});
assert_equals(buffer.size, bufferSize);
} catch (e) {
assert_true(e instanceof DOMException);
assert_equals(e.name, "NotSupportedError");
}
return buffer;
};

/**
* WebNN destroy buffer twice test.
* @param {String} testName - The name of the test operation.
* @param {String} deviceType - The execution device type for this test.
*/
const testDestroyWebNNBuffer = (testName, deviceType = 'cpu') => {
let context;
let buffer;
promise_setup(async () => {
context = await navigator.ml.createContext({deviceType});
buffer = createBuffer(context, 4);
});
promise_test(async () => {
// MLBuffer is not supported for this deviceType.
if (buffer === undefined) {
return;
}
buffer.destroy();
buffer.destroy();
}, `${testName}`);
};

/**
* WebNN create buffer test.
* @param {String} testName - The name of the test operation.
* @param {Number} bufferSize - Size of the buffer to create, in bytes.
* @param {String} deviceType - The execution device type for this test.
*/
const testCreateWebNNBuffer = (testName, bufferSize, deviceType = 'cpu') => {
let context;
promise_setup(async () => {
context = await navigator.ml.createContext({deviceType});
});
promise_test(async () => {
createBuffer(context, bufferSize);
}, `${testName} / ${bufferSize}`);
};

0 comments on commit 888ed78

Please sign in to comment.