shared_arraybuffer_constant.https.any.js (2243B)
1 // META: title=test WebNN API constant with shared array buffer 2 // META: global=window,dedicatedworker,serviceworker 3 // META: variant=?cpu 4 // META: variant=?gpu 5 // META: variant=?npu 6 // META: script=../resources/utils_validation.js 7 // META: script=../resources/utils.js 8 // META: timeout=long 9 10 'use strict'; 11 12 // Skip tests if WebNN is unimplemented. 13 promise_setup(async () => { 14 assert_implements(navigator.ml, 'missing navigator.ml'); 15 }); 16 17 // https://www.w3.org/TR/webnn/#api-mlgraphbuilder-constant-buffer 18 19 const testContents = Int32Array.from([0, 1, 2, 3, 4, 5, 6, 7]); 20 const sharedArrayBuffer = new SharedArrayBuffer(testContents.byteLength); 21 const typedArray = new Int32Array(sharedArrayBuffer); 22 typedArray.set(testContents); 23 24 let mlContext; 25 let mlGraph; 26 let outputTensor; 27 promise_setup(async () => { 28 try { 29 mlContext = await navigator.ml.createContext(contextOptions); 30 } catch (e) { 31 throw new AssertionError( 32 `Unable to create mlContext for ${variant} variant. ${e}`); 33 } 34 35 try { 36 outputTensor = await mlContext.createTensor({ 37 dataType: 'int32', 38 shape: [8], 39 readable: true, 40 }); 41 } catch (e) { 42 throw new AssertionError( 43 `Unable to create tensor for ${variant} variant. ${e}`); 44 } 45 }); 46 47 promise_test(async () => { 48 const builder = new MLGraphBuilder(mlContext); 49 const constant = 50 builder.constant({dataType: 'int32', shape: [8]}, sharedArrayBuffer); 51 const output = builder.identity(constant); 52 const mlGraph = await builder.build({output}); 53 54 mlContext.dispatch(mlGraph, {}, {output: outputTensor}); 55 const results = new Int32Array(await mlContext.readTensor(outputTensor)); 56 57 assert_array_equals(results, testContents); 58 }, `constant() with a SharedArrayBuffer`); 59 60 promise_test(async () => { 61 const builder = new MLGraphBuilder(mlContext); 62 const constant = 63 builder.constant({dataType: 'int32', shape: [8]}, typedArray); 64 const output = builder.identity(constant); 65 const mlGraph = await builder.build({output}); 66 67 mlContext.dispatch(mlGraph, {}, {output: outputTensor}); 68 const results = new Int32Array(await mlContext.readTensor(outputTensor)); 69 70 assert_array_equals(results, testContents); 71 }, `constant() with a typeArray from a SharedArrayBuffer`);