-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathbyob_readtensor.https.any.js
178 lines (140 loc) · 5.49 KB
/
byob_readtensor.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
172
173
174
175
176
177
178
// META: title=test WebNN API tensor operations
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils_validation.js
// META: script=../resources/utils.js
// META: timeout=long
'use strict';
// Skip tests if WebNN is unimplemented.
promise_setup(async () => {
assert_implements(navigator.ml, 'missing navigator.ml');
});
// https://www.w3.org/TR/webnn/#api-mltensor
const testContents = Uint32Array.from([0, 1, 2, 3, 4, 5, 6, 7]);
let mlContext;
let mlTensor;
promise_setup(async () => {
try {
mlContext = await navigator.ml.createContext(contextOptions);
} catch (e) {
throw new AssertionError(
`Unable to create context for ${variant} variant. ${e}`);
}
try {
mlTensor = await mlContext.createTensor({
dataType: 'int32',
shape: [2, 4],
readable: true,
writable: true,
});
} catch (e) {
throw new AssertionError(
`Unable to create tensor for ${variant} variant. ${e}`);
}
mlContext.writeTensor(mlTensor, testContents);
});
promise_test(async (t) => {
const arrayBuffer = new ArrayBuffer(testContents.byteLength - 4);
await promise_rejects_js(
t, TypeError, mlContext.readTensor(mlTensor, arrayBuffer));
}, `readTensor() with an ArrayBuffer that is too small should reject`);
promise_test(async (t) => {
const typedArray = new Uint32Array(testContents.length - 1);
await promise_rejects_js(
t, TypeError, mlContext.readTensor(mlTensor, typedArray));
}, `readTensor() with a TypedArray that is too small should reject`);
promise_test(async (t) => {
const arrayBuffer = new ArrayBuffer(testContents.byteLength);
const typedArray = new Uint32Array(arrayBuffer);
arrayBuffer.transfer();
await promise_rejects_js(
t, TypeError, mlContext.readTensor(mlTensor, arrayBuffer));
await promise_rejects_js(
t, TypeError, mlContext.readTensor(mlTensor, typedArray));
}, `readTensor() with a detached ArrayBuffer should reject`);
promise_test(async (t) => {
const arrayBuffer = new ArrayBuffer(testContents.byteLength);
const typedArray = new Uint32Array(arrayBuffer);
const checks = Promise.all([
promise_rejects_js(
t, TypeError, mlContext.readTensor(mlTensor, arrayBuffer)),
promise_rejects_js(
t, TypeError, mlContext.readTensor(mlTensor, typedArray)),
]);
arrayBuffer.transfer();
await checks;
}, `Detaching an ArrayBuffer while readTensor() is in progress should reject`);
promise_test(async () => {
const arrayBuffer = new ArrayBuffer(testContents.byteLength);
await mlContext.readTensor(mlTensor, arrayBuffer);
assert_array_equals(new Uint32Array(arrayBuffer), testContents);
}, `readTensor() with an ArrayBuffer`);
promise_test(async () => {
// Create a slightly larger ArrayBuffer and set up the TypedArray at an
// offset to make sure the MLTensor contents are written to the correct
// offset.
const arrayBuffer = new ArrayBuffer(testContents.byteLength + 4);
const typedArray = new Uint32Array(arrayBuffer, 4);
await mlContext.readTensor(mlTensor, typedArray);
assert_array_equals(typedArray, testContents);
}, `readTensor() with a TypedArray`);
promise_test(async () => {
const arrayBuffer = new ArrayBuffer(testContents.byteLength * 2);
await mlContext.readTensor(mlTensor, arrayBuffer);
assert_array_equals(
new Uint32Array(arrayBuffer).subarray(0, testContents.length),
testContents);
// The rest of the array should remain uninitialized.
assert_array_equals(
new Uint32Array(arrayBuffer)
.subarray(testContents.length, testContents.length * 2),
new Uint32Array(testContents.length));
}, `readTensor() with a larger ArrayBuffer`);
promise_test(async () => {
// Create a slightly larger ArrayBuffer and set up the TypedArray at an
// offset to make sure the MLTensor contents are written to the correct
// offset.
const arrayBuffer = new ArrayBuffer(testContents.byteLength * 2 + 4);
const typedArray = new Uint32Array(arrayBuffer, 4);
await mlContext.readTensor(mlTensor, typedArray);
assert_array_equals(
typedArray.subarray(0, testContents.length), testContents);
// The rest of the array should remain uninitialized.
assert_array_equals(
typedArray.subarray(testContents.length, testContents.length * 2),
new Uint32Array(testContents.length));
}, `readTensor() with a larger TypedArray`);
promise_test(async (t) => {
const tensor = await mlContext.createTensor({
dataType: 'int32',
shape: [2, 2],
readable: true,
});
const arrayBufferView = new Int32Array(2 * 2);
const arrayBuffer = arrayBufferView.buffer;
// Reading a destroyed MLTensor should reject.
tensor.destroy();
await promise_rejects_dom(
t, 'InvalidStateError', mlContext.readTensor(tensor, arrayBuffer));
await promise_rejects_dom(
t, 'InvalidStateError', mlContext.readTensor(tensor, arrayBufferView));
}, `readTensor() rejects on a destroyed MLTensor`);
promise_test(async (t) => {
const tensor = await mlContext.createTensor({
dataType: 'int32',
shape: [2, 2],
readable: true,
});
const arrayBufferView = new Int32Array(2 * 2);
const arrayBuffer = arrayBufferView.buffer;
const checks = Promise.all([
promise_rejects_dom(
t, 'InvalidStateError', mlContext.readTensor(tensor, arrayBuffer)),
promise_rejects_dom(
t, 'InvalidStateError', mlContext.readTensor(tensor, arrayBufferView)),
]);
tensor.destroy();
await checks;
}, `readTensor() rejects when the MLTensor is destroyed`);