destroyGraph.https.any.js (3102B)
1 // META: timeout=long 2 // META: title=validation tests for WebNN API MLContext::destroy() 3 // META: global=window,worker 4 // META: variant=?cpu 5 // META: variant=?gpu 6 // META: variant=?npu 7 8 'use strict'; 9 10 let context; 11 12 promise_setup(async () => { 13 assert_implements(navigator.ml, 'WebNN is not supported'); 14 const contextOptions = {deviceType: location.search.substring(1)}; 15 try { 16 context = await navigator.ml.createContext(contextOptions); 17 } catch (e) { 18 throw new AssertionError( 19 `Unable to create context for ${variant} variant. ${e}`); 20 } 21 }, {explicit_timeout: true}); 22 23 promise_test(async t => { 24 const builder = new MLGraphBuilder(context); 25 const operandType = {dataType: 'float32', shape: [1]}; 26 const input_operand = builder.input('input', operandType); 27 const const_operand = builder.constant(operandType, Float32Array.from([2])); 28 const output_operand = builder.mul(input_operand, const_operand); 29 const graph = await builder.build({'output': output_operand}); 30 31 graph.destroy(); 32 graph.destroy(); 33 }, 'Graph can be destroyed twice.'); 34 35 promise_test(async t => { 36 const builder = new MLGraphBuilder(context); 37 const operandType = {dataType: 'float32', shape: [1]}; 38 const lhsOperand = builder.input('lhs', operandType); 39 const rhsOperand = builder.input('rhs', operandType); 40 const graph = 41 await builder.build({'output': builder.mul(lhsOperand, rhsOperand)}); 42 43 const lhsTensor = await context.createTensor(operandType); 44 const rhsTensor = await context.createTensor(operandType); 45 const dispatchOutputs = {'output': await context.createTensor(operandType)}; 46 47 graph.destroy(); 48 assert_throws_dom('InvalidStateError', () => { 49 context.dispatch( 50 graph, { 51 'lhs': lhsTensor, 52 'rhs': rhsTensor, 53 }, 54 dispatchOutputs); 55 }); 56 }, 'Destroyed graph can not dispatch.'); 57 58 promise_test(async t => { 59 const builder = new MLGraphBuilder(context); 60 const operandType = {dataType: 'float32', shape: [1]}; 61 const lhsOperand = builder.input('lhs', operandType); 62 const rhsOperand = builder.input('rhs', operandType); 63 const graph = 64 await builder.build({'output': builder.mul(lhsOperand, rhsOperand)}); 65 66 const lhsTensor = await context.createTensor({ 67 dataType: 'float32', 68 shape: [1], 69 writable: true, 70 }); 71 const rhsTensor = await context.createTensor({ 72 dataType: 'float32', 73 shape: [1], 74 writable: true, 75 }); 76 const outputTensor = await context.createTensor({ 77 dataType: 'float32', 78 shape: [1], 79 readable: true, 80 }); 81 // Initialize inputs 82 const inputData = new Float32Array(1).fill(2.0); 83 context.writeTensor(lhsTensor, inputData); 84 context.writeTensor(rhsTensor, inputData); 85 context.dispatch( 86 graph, { 87 'lhs': lhsTensor, 88 'rhs': rhsTensor, 89 }, 90 {'output': outputTensor}); 91 92 graph.destroy(); 93 const outputData = await context.readTensor(outputTensor); 94 assert_array_equals( 95 new Float32Array(outputData), [4], 96 'Read tensor data equals expected data.'); 97 }, 'Destroying graph after dispatch() and before readTensor() is OK.');