Skip to content

Commit 0ab38b9

Browse files
committed
feat: add form for input values and display result in WebNN component
1 parent 4be01af commit 0ab38b9

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

app/web-nn/web-nn.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:host {
2+
padding: var(--padding);
3+
display: block;
4+
}

app/web-nn/web-nn.html

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
<h2>WebNN</h2>
1+
<h2>WebNN</h2>
2+
3+
<form>
4+
<label>
5+
<div>Value1</div>
6+
<input id="value1" type="number" value="10">
7+
</label>
8+
9+
<label>
10+
<div>Value2</div>
11+
<input id="value2" type="number" value="20">
12+
</label>
13+
14+
<button type="button">Submit</button>
15+
</form>
16+
17+
<div>
18+
<span>Result: </span>
19+
<span id="result"></span>
20+
</div>

app/web-nn/web-nn.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import { EventsManager } from "../../src/system/events-manager.js";
12
import {
23
WebNNModule,
34
OperandDescriptorBuilder } from "../../src/modules/webnn.js";
45

56
export default class WebNNView extends HTMLElement {
67
static tag = "webnn-view";
78

9+
#eventsManager = new EventsManager();
10+
#graphBuilder
11+
#graph;
812
#context;
13+
#tensors;
914

1015
constructor() {
1116
super();
@@ -16,10 +21,66 @@ export default class WebNNView extends HTMLElement {
1621
this.shadowRoot.innerHTML = await api.call("component", "load_html", {
1722
url: import.meta.url,
1823
});
24+
25+
requestAnimationFrame(() => {
26+
const button = this.shadowRoot.querySelector("button");
27+
this.#eventsManager.addEvent(button, "click", this.#submit.bind(this));
28+
})
1929

20-
const graphBuilder = await WebNNModule.createGraph();
21-
const constant2 = graphBuilder.constant(new OperandDescriptorBuilder().build(), new Float32Array([0.2]));
22-
console.log(constant2);
30+
this.#context = await WebNNModule.createContext();
31+
this.#graphBuilder = await WebNNModule.createGraph( { context: this.#context } );
32+
const operands = await this.#createGraph();
33+
await this.#createTensors(operands.A, operands.B, operands.C);
34+
}
35+
36+
async disconnectedCallback() {
37+
this.#eventsManager = this.#eventsManager.dispose();
38+
}
39+
40+
async #createGraph() {
41+
const descriptor = new OperandDescriptorBuilder().build();
42+
const A = this.#graphBuilder.input("A", descriptor);
43+
const B = this.#graphBuilder.input("B", descriptor);
44+
const C = this.#graphBuilder.add(A, B);
45+
46+
this.#graph = await this.#graphBuilder.build({C});
47+
return { A, B, C };
48+
}
49+
50+
async #createTensors(inputA, inputB, outputC) {
51+
this.#tensors = {
52+
inputA: await this.#createTensor(this.#context, inputA, true, true),
53+
inputB: await this.#createTensor(this.#context, inputB, true, true),
54+
outputC: await this.#createTensor(this.#context, outputC, false, true)
55+
};
56+
}
57+
58+
async #createTensor(context, operand, writable, readable) {
59+
return context.createTensor({
60+
dataType: operand.dataType, shape: operand.shape, writable, readable
61+
})
62+
}
63+
64+
async #submit(event) {
65+
const value1 = this.shadowRoot.querySelector("#value1").value;
66+
const value2 = this.shadowRoot.querySelector("#value2").value;
67+
68+
this.#context.writeTensor(this.#tensors.inputA, new Float32Array([value1]));
69+
this.#context.writeTensor(this.#tensors.inputB, new Float32Array([value2]));
70+
71+
const inputs = {
72+
'A': this.#tensors.inputA,
73+
'B': this.#tensors.inputB
74+
};
75+
76+
const outputs = {
77+
'C': this.#tensors.outputC
78+
};
79+
80+
this.#context.dispatch(this.#graph, inputs, outputs);
81+
const output = await this.#context.readTensor(this.#tensors.outputC);
82+
const result = new Float32Array(output)[0];
83+
this.shadowRoot.querySelector("#result").textContent = result;
2384
}
2485
}
2586

src/modules/webnn/builders/operand-descriptor-builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class OperandDescriptorBuilder {
1313
* @param {string} [dataType=DataType.FLOAT32] - The data type of the operand.
1414
* @param {number[]} [shape=[]] - The shape of the operand.
1515
*/
16-
constructor(dataType = DataType.FLOAT32, shape = []) {
16+
constructor(dataType = DataType.FLOAT32, shape = [1]) {
1717
this.#dataType = dataType;
1818
this.#shape = shape;
1919
}

0 commit comments

Comments
 (0)