Skip to content

Commit

Permalink
webnn: Allow passing SharedArrayBuffers to writeTensor()
Browse files Browse the repository at this point in the history
readTensor() and the other writeTensor() overload both allow shared
buffers and I don't see why this variant of writeTensor() shouldn't

Bug: 365813262
Change-Id: Id0618ec2fc70db0de61d246c8af8fededbd11edd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5976704
Commit-Queue: Austin Sullivan <[email protected]>
Reviewed-by: ningxin hu <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1375674}
  • Loading branch information
a-sully authored and chromium-wpt-export-bot committed Oct 30, 2024
1 parent 45cfb02 commit fb143c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions webnn/conformance_tests/tensor.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,45 @@ const testWriteTensor = (testName) => {
}
});

promise_test(async () => {
const tensorDescriptor = {
dataType: 'int32',
shape: [4],
readable: true,
writable: true,
};
const tensorByteLength = sizeOfDescriptor(tensorDescriptor);

// Required to use SharedArrayBuffer.
assert_true(
self.crossOriginIsolated,
'The page is served with COOP and COEP, it should be cross-origin-isolated.');

let arrayBuffer = new ArrayBuffer(tensorByteLength);
let arrayBufferView = new Int32Array(arrayBuffer);
arrayBufferView.fill(7);

let sharedArrayBuffer = new SharedArrayBuffer(tensorByteLength);
let sharedArrayBufferView = new Int32Array(sharedArrayBuffer);
sharedArrayBufferView.fill(7);

const tensors = await Promise.all([
mlContext.createTensor(tensorDescriptor),
mlContext.createTensor(tensorDescriptor),
mlContext.createTensor(tensorDescriptor),
mlContext.createTensor(tensorDescriptor)
]);

mlContext.writeTensor(tensors[0], arrayBuffer);
mlContext.writeTensor(tensors[2], arrayBufferView);
mlContext.writeTensor(tensors[1], sharedArrayBuffer);
mlContext.writeTensor(tensors[3], sharedArrayBufferView);

await Promise.all(tensors.map(async (tensor) => {
assert_tensor_data_equals(mlContext, tensor, arrayBufferView);
}));
}, `${testName} / write with different kinds of buffers`);

promise_test(async () => {
const tensorDescriptor = {
dataType: 'int32',
Expand Down
2 changes: 2 additions & 0 deletions webnn/conformance_tests/tensor.https.any.js.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

0 comments on commit fb143c3

Please sign in to comment.