Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webnn: Immediately copy constant data #46883

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions webnn/validation_tests/constant-changed-buffer.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// META: title=ensure MLGraphBuilder.constant() handles buffers which change
// META: global=window,dedicatedworker
// META: script=../resources/utils_validation.js

promise_test(async t => {
let backingBuffer = new ArrayBuffer(8);
let aBuffer = new Float32Array(backingBuffer, 0, 2);
aBuffer[0] = 2;
aBuffer[1] = 3;
const a = builder.constant({dataType: 'float32', dimensions: [2]}, aBuffer);

// Detach `aBuffer`. Constant data should already be copied, so changes to
// the buffer afterwards should not be reflected in the graph.
const unusedBuffer = backingBuffer.transfer();

const b = builder.input('b', {dataType: 'float32', dimensions: [2]});
const c = builder.add(a, b);
const graph = await builder.build({c});

const bBuffer = new Float32Array([5, 7]);
const cBuffer = new Float32Array(2);
const result = await context.compute(graph, {'b': bBuffer}, {'c': cBuffer});

const expectedResult = new Float32Array([7, 10]);
assert_array_equals(result.outputs.c, expectedResult);
}, 'Constant data is unaffected by detaching the buffer');

promise_test(async t => {
let aBuffer = new Float32Array([2, 3]);
const a = builder.constant({dataType: 'float32', dimensions: [2]}, aBuffer);

// Rewrite `aBuffer` contents. Constant data should already be copied, so
// changes to the buffer afterwards should not be reflected in the graph.
aBuffer[0] = 10;
aBuffer[1] = 20;

const b = builder.input('b', {dataType: 'float32', dimensions: [2]});
const c = builder.add(a, b);
const graph = await builder.build({c});

const bBuffer = new Float32Array([5, 7]);
const cBuffer = new Float32Array(2);
const result = await context.compute(graph, {'b': bBuffer}, {'c': cBuffer});

const expectedResult = new Float32Array([7, 10]);
assert_array_equals(result.outputs.c, expectedResult);
}, 'Constant data is unaffected by changes to the buffer contents');

promise_test(async t => {
let backingBuffer = new ArrayBuffer(8);
const aBuffer = new Float32Array(backingBuffer, 0, 2);
// Detach `aBuffer` _before_ calling `constant()`. This should throw, since
// detached buffers have a length of zero, which does not match the length of
// the descriptor. See
// https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy
const unusedBuffer = backingBuffer.transfer();

assert_throws_js(
TypeError,
() => builder.constant({dataType: 'float32', dimensions: [2]}, aBuffer));
}, 'Constant data cannot use a detached buffer, which is empty');