-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathdestroyContext.https.any.js
171 lines (150 loc) · 5.97 KB
/
destroyContext.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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 contextOptions;
promise_setup(async () => {
if (navigator.ml === undefined) {
return;
}
contextOptions = {deviceType: location.search.substring(1)};
}, {explicit_timeout: true});
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
context.destroy();
await context.lost;
}, 'Context will be lost by destroyed.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
context.destroy();
context.destroy();
await context.lost;
}, 'Context can be destroyed twice.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
const builder = new MLGraphBuilder(context);
context.destroy();
assert_throws_dom('InvalidStateError', () => {
const operandType = {dataType: 'float32', dimensions: [1]};
builder.input('input', operandType);
});
}, 'Destroyed context can not build operator.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
context.destroy();
assert_throws_dom('InvalidStateError', () => {
new MLGraphBuilder(context);
});
}, 'Destroyed context can not create graph builder.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', dimensions: [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);
context.destroy();
promise_rejects_dom(
t, 'InvalidStateError', builder.build({'output': output_operand}));
}, 'Destroyed context can not build graph.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', dimensions: [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});
context.destroy();
let inputs = {'input': Float32Array.from([1])};
let outputs = {'output': new Float32Array(1)};
promise_rejects_dom(
t, 'InvalidStateError', context.compute(graph, inputs, outputs));
}, 'Destroyed context can not compute.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', dimensions: [1]};
const lhsOperand = builder.input('lhs', operandType);
const rhsOperand = builder.input('rhs', operandType);
const graph =
await builder.build({'output': builder.sub(lhsOperand, rhsOperand)});
const lhsTensor = await context.createTensor(operandType);
const rhsTensor = await context.createTensor(operandType);
const dispatchOutputs = {'output': await context.createTensor(operandType)};
context.destroy();
assert_throws_dom('InvalidStateError', () => {
context.dispatch(
graph, {
'lhs': lhsTensor,
'rhs': rhsTensor,
},
dispatchOutputs);
});
}, 'Destroyed context can not dispatch.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', dimensions: [1]};
const lhsOperand = builder.input('lhs', operandType);
const rhsOperand = builder.input('rhs', operandType);
const graph =
await builder.build({'output': builder.sub(lhsOperand, rhsOperand)});
const lhsTensor = await context.createTensor(operandType);
const rhsTensor = await context.createTensor(operandType);
const dispatchOutputs = {'output': await context.createTensor(operandType)};
context.dispatch(
graph, {
'lhs': lhsTensor,
'rhs': rhsTensor,
},
dispatchOutputs);
context.destroy();
}, 'Executing dispatch() before context destroyed is OK.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
context.destroy();
promise_rejects_dom(
t, 'InvalidStateError',
context.createTensor({dataType: 'float32', dimensions: [1]}));
}, 'Destroyed context can not create tensor.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
const tensor = await context.createTensor({
dataType: 'float32',
dimensions: [1],
usage: MLTensorUsage.READ,
});
context.destroy();
promise_rejects_dom(t, 'InvalidStateError', context.readTensor(tensor));
}, 'Destroyed context can not read tensor.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
const tensor = await context.createTensor({
dataType: 'float32',
dimensions: [1],
usage: MLTensorUsage.READ,
});
let promise = context.readTensor(tensor);
context.destroy();
promise_rejects_dom(t, 'InvalidStateError', promise);
}, 'Pending promise of readtensor() will be rejected immediately when context is destroyed.');
promise_test(async t => {
const context = await navigator.ml.createContext(contextOptions);
// Destroying another context doesn't impact the first context.
const another_context = await navigator.ml.createContext(contextOptions);
another_context.destroy();
const tensor = await context.createTensor({
dataType: 'float32',
dimensions: [1],
usage: MLTensorUsage.WRITE,
});
let arrayBuffer = new ArrayBuffer(4);
context.destroy();
assert_throws_dom('InvalidStateError', () => {
context.writeTensor(tensor, new Uint8Array(arrayBuffer));
});
}, 'Destroyed context can not write tensor.');