Skip to content

Commit

Permalink
WebNN: Respect ArrayBufferView's byte offset and length for MLGraphMojo
Browse files Browse the repository at this point in the history
An `ArrayBufferView` can be created from an `ArrayBuffer` with bigger
size and with a proper byte offset, i.e. `new Float32Array(arrayBuffer,
byteOffset, length)`. `MLGraphMojo::ComputeImpl()` should use
`ArrayBufferView`'s byte offset and length when copying the input or
output data into or from the mojo `BigBuffer`.

Bug: 332151809
Change-Id: Ib8873a100de0e5565ae39b483f6fa06b024cb6f6
Cq-Include-Trybots: luci.chromium.try:win11-blink-rel
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5409911
Reviewed-by: Austin Sullivan <[email protected]>
Commit-Queue: ningxin hu <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1284226}
  • Loading branch information
huningxin authored and chromium-wpt-export-bot committed Apr 9, 2024
1 parent fc96c50 commit 822ba04
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// META: title=test WebNN MLContext.compute() for ArrayBufferView created from bigger ArrayBuffer
// META: global=window,dedicatedworker
// META: script=../../resources/utils.js

'use strict';

// These tests are used to reproduce the Chromium issue:
// https://issues.chromium.org/issues/332151809
promise_test(async t => {
const context = await navigator.ml.createContext({deviceType: 'gpu'});
const builder = new MLGraphBuilder(context);
const a = builder.input('a', {dataType: 'float32', dimensions: [2]});
const b = builder.relu(a);
const graph = await builder.build({b});
const arraybuffer = new ArrayBuffer(100);
const aBuffer = new Float32Array(arraybuffer, /*byteOffset*/ 4, /*length*/ 2)
aBuffer.set([1, -1]);
const bBuffer = new Float32Array(2);
const results = await context.compute(graph, {'a': aBuffer}, {'b': bBuffer});
assert_array_approx_equals_ulp(
results.outputs.b, [1, 0], /*nulp*/ 0, 'float32');
}, 'Test compute() working for input ArrayBufferView created from bigger ArrayBuffer');

promise_test(async t => {
const context = await navigator.ml.createContext({deviceType: 'gpu'});
const builder = new MLGraphBuilder(context);
const a = builder.input('a', {dataType: 'float32', dimensions: [2]});
const b = builder.relu(a);
const graph = await builder.build({b});
const aBuffer = new Float32Array(2);
aBuffer.set([1, -1]);
const arraybuffer = new ArrayBuffer(100);
const bBuffer = new Float32Array(arraybuffer, /*byteOffset*/ 8, /*length*/ 2);
const results = await context.compute(graph, {'a': aBuffer}, {'b': bBuffer});
assert_array_approx_equals_ulp(
results.outputs.b, [1, 0], /*nulp*/ 0, 'float32');
}, 'Test compute() working for output ArrayBufferView created from bigger ArrayBuffer');

0 comments on commit 822ba04

Please sign in to comment.