-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebnn-add.html
228 lines (202 loc) · 6.87 KB
/
webnn-add.html
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<html>
<head>
<title>WebNN Simple Example</title>
<script src="half-floats.js"></script>
</head>
<body>
<p>
<input id="a" type="number" value="1"> +
<input id="b" type="number" value="1"> =
<span id="c">?</span>
</p>
<p>
Device preference:
<select id="device">
<option selected value="cpu">CPU</option>
<option value="gpu">GPU</option>
</select>
</p>
<p>
Power preference:
<select id="power">
<option selected value="default">Default</option>
<option value="low-power">Low power</option>
<option value="high-performance">High performance</option>
</select>
</p>
<p>
Data type:
<select id="dataType">
<option value="float16">float16</option>
<option selected value="float32">float32</option>
<option value="int8">int8</option>
<option value="uint8">uint8</option>
<option value="int32">int32</option>
<option value="uint32">uint32</option>
<option value="int64">int64</option>
<option value="uint64">uint64</option>
</select>
</p>
<p>
<label for="dispatch">Use <code>MLBuffer</code>:</label>
<input id="dispatch" type="checkbox">
</p>
<p>
<button id="build">Build</button> <button disabled id="compute">Compute</button>
</p>
<p>
<span id="output"></span>
</p>
<script>
const aInput = document.getElementById('a');
const bInput = document.getElementById('b');
const cOutput = document.getElementById('c');
const deviceOption = document.getElementById('device');
const powerOption = document.getElementById('power');
const dataTypeOption = document.getElementById('dataType');
const dispatchCheckbox = document.getElementById('dispatch');
const buildButton = document.getElementById('build');
const computeButton = document.getElementById('compute');
const outputSpan = document.getElementById('output');
let inputs = {'a': null, 'b': null};
let outputs = {'c': null};
const inputMLBuffers = {'a': null, 'b': null};
const outputMLBuffers = {'c': null};
let context;
let graph;
function getOperandDescriptor() {
const dataType = dataTypeOption.value;
return {dataType, dimensions: [1]};
}
function getTypedArrayConstructor() {
switch (dataTypeOption.value) {
case 'float16':
return Uint16Array;
case 'float32':
return Float32Array;
case 'int8':
return Int8Array;
case 'uint8':
return Uint8Array;
case 'int32':
return Int32Array;
case 'uint32':
return Uint32Array;
case 'int64':
return BigInt64Array;
case 'uint64':
return BigUint64Array;
}
}
function maybeEncodeInput(input) {
if (dataTypeOption.value == 'float16') {
return toHalf(input);
}
return input;
}
function maybeDecodeOutput(output) {
if (dataTypeOption.value == 'float16') {
return fromHalf(output);
}
return output;
}
function createArrayBuffers() {
const typedArray = getTypedArrayConstructor();
inputs.a = new typedArray(1);
inputs.b = new typedArray(1);
outputs.c = new typedArray(1);
}
function createMLBuffers() {
const operandDescriptor = getOperandDescriptor();
inputMLBuffers.a = context.createBuffer(operandDescriptor);
inputMLBuffers.b = context.createBuffer(operandDescriptor);
outputMLBuffers.c = context.createBuffer(operandDescriptor);
}
function destroyMLBuffers() {
if (inputMLBuffers.a) {
inputMLBuffers.a.destroy();
inputMLBuffers.a = null;
}
if (inputMLBuffers.b) {
inputMLBuffers.b.destroy();
inputMLBuffers.b = null;
}
if (outputMLBuffers.c) {
outputMLBuffers.c.destroy();
outputMLBuffers.c = null;
}
}
function contextOptionsChanged() {
context = null;
graphOptionsChanged();
}
function graphOptionsChanged() {
createArrayBuffers();
destroyMLBuffers();
buildButton.disabled = false;
computeButton.disabled = true;
graph = null;
}
deviceOption.addEventListener('change', contextOptionsChanged);
powerOption.addEventListener('change', contextOptionsChanged);
dataTypeOption.addEventListener('change', graphOptionsChanged);
createArrayBuffers();
dispatchCheckbox.addEventListener('change', () => {
if (dispatchCheckbox.checked) {
if (context) {
createMLBuffers();
}
} else {
destroyMLBuffers();
}
});
buildButton.addEventListener('click', async () => {
try {
computeButton.disabled = true;
const options = {
deviceType: deviceOption.value,
powerPreference: powerOption.value,
};
console.log(options);
context = await navigator.ml.createContext(options);
if (dispatchCheckbox.checked) {
createMLBuffers();
}
const builder = new MLGraphBuilder(context);
const operandDescriptor = getOperandDescriptor();
const a = builder.input('a', operandDescriptor);
const b = builder.input('b', operandDescriptor);
const c = builder.add(a, b);
graph = await builder.build({'c': c});
outputSpan.textContent = 'Graph ready!'
computeButton.disabled = false;
buildButton.disabled = true;
} catch (e) {
outputSpan.textContent = `${e.name}: ${e.message}`;
}
});
computeButton.addEventListener('click', async () => {
try {
outputSpan.textContent = 'Compute running...';
cOutput.textContent = '?';
inputs.a[0] = maybeEncodeInput(aInput.value);
inputs.b[0] = maybeEncodeInput(bInput.value);
if (dispatchCheckbox.checked) {
context.writeBuffer(inputMLBuffers.a, inputs.a);
context.writeBuffer(inputMLBuffers.b, inputs.b);
context.dispatch(graph, inputMLBuffers, outputMLBuffers);
const buffer = await context.readBuffer(outputMLBuffers.c);
const typedArray = getTypedArrayConstructor();
outputs.c = new typedArray(buffer);
} else {
({inputs, outputs} = await context.compute(graph, inputs, outputs));
}
cOutput.textContent = maybeDecodeOutput(outputs.c[0]);
outputSpan.textContent = 'Compute finished!';
} catch (e) {
outputSpan.textContent = `${e.name}: ${e.message}`;
}
});
</script>
</body>
</html>