-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathdestroyGraph.https.any.js
141 lines (124 loc) · 4.97 KB
/
destroyGraph.https.any.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// META: timeout=long
// META: title=validation tests for WebNN API MLContext::destroy()
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
'use strict';
let context;
promise_setup(async () => {
assert_implements(navigator.ml, 'WebNN is not supported');
const contextOptions = {deviceType: location.search.substring(1)};
try {
context = await navigator.ml.createContext(contextOptions);
} catch (e) {
throw new AssertionError(
`Unable to create context for ${variant} variant. ${e}`);
}
}, {explicit_timeout: true});
promise_test(async t => {
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', shape: [1]};
const input_operand = builder.input('input', operandType);
const const_operand = builder.constant(operandType, Float32Array.from([2]));
const output_operand = builder.mul(input_operand, const_operand);
const graph = await builder.build({'output': output_operand});
graph.destroy();
graph.destroy();
}, 'Graph can be destroyed twice.');
promise_test(async t => {
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', shape: [1]};
const input_operand = builder.input('input', operandType);
const const_operand = builder.constant(operandType, Float32Array.from([2]));
const output_operand = builder.mul(input_operand, const_operand);
const graph = await builder.build({'output': output_operand});
graph.destroy();
let inputs = {'input': Float32Array.from([1])};
let outputs = {'output': new Float32Array(1)};
promise_rejects_dom(
t, 'InvalidStateError', context.compute(graph, inputs, outputs));
}, 'Destroyed graph can not compute.');
promise_test(async t => {
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', shape: [1]};
const input_operand = builder.input('input', operandType);
const const_operand = builder.constant(operandType, Float32Array.from([2]));
const output_operand = builder.mul(input_operand, const_operand);
const graph = await builder.build({'output': output_operand});
let inputs = {'input': Float32Array.from([1])};
let outputs = {'output': new Float32Array(1)};
await context.compute(graph, inputs, outputs);
graph.destroy();
}, 'Destroying graph after compute() with await is OK.');
promise_test(async t => {
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', shape: [1]};
const input_operand = builder.input('input', operandType);
const const_operand = builder.constant(operandType, Float32Array.from([2]));
const output_operand = builder.mul(input_operand, const_operand);
const graph = await builder.build({'output': output_operand});
let inputs = {'input': Float32Array.from([1])};
let outputs = {'output': new Float32Array(1)};
const promise = context.compute(graph, inputs, outputs);
graph.destroy();
promise_rejects_dom(t, 'InvalidStateError', promise);
}, 'compute() rejects when graph is destroyed.');
promise_test(async t => {
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', shape: [1]};
const lhsOperand = builder.input('lhs', operandType);
const rhsOperand = builder.input('rhs', operandType);
const graph =
await builder.build({'output': builder.mul(lhsOperand, rhsOperand)});
const lhsTensor = await context.createTensor(operandType);
const rhsTensor = await context.createTensor(operandType);
const dispatchOutputs = {'output': await context.createTensor(operandType)};
graph.destroy();
assert_throws_dom('InvalidStateError', () => {
context.dispatch(
graph, {
'lhs': lhsTensor,
'rhs': rhsTensor,
},
dispatchOutputs);
});
}, 'Destroyed graph can not dispatch.');
promise_test(async t => {
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', shape: [1]};
const lhsOperand = builder.input('lhs', operandType);
const rhsOperand = builder.input('rhs', operandType);
const graph =
await builder.build({'output': builder.mul(lhsOperand, rhsOperand)});
const lhsTensor = await context.createTensor({
dataType: 'float32',
shape: [1],
writable: true,
});
const rhsTensor = await context.createTensor({
dataType: 'float32',
shape: [1],
writable: true,
});
const outputTensor = await context.createTensor({
dataType: 'float32',
shape: [1],
readable: true,
});
// Initialize inputs
const inputData = new Float32Array(1).fill(2.0);
context.writeTensor(lhsTensor, inputData);
context.writeTensor(rhsTensor, inputData);
context.dispatch(
graph, {
'lhs': lhsTensor,
'rhs': rhsTensor,
},
{'output': outputTensor});
graph.destroy();
const outputData = await context.readTensor(outputTensor);
assert_array_equals(
new Float32Array(outputData), [4],
'Read tensor data equals expected data.');
}, 'Destroying graph after dispatch() and before readTensor() is OK.');