Skip to content

Commit 82c47ed

Browse files
committed
feat: introduce DeviceType, PowerOptions, and DataType constants; refactor WebNN program structure and update input form fields
1 parent 3c907e9 commit 82c47ed

File tree

10 files changed

+166
-205
lines changed

10 files changed

+166
-205
lines changed

app/web-nn/web-nn.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ <h2>WebNN</h2>
22

33
<form>
44
<label>
5-
<div>Value1</div>
6-
<input id="value1" type="number" value="10">
5+
<div>cloudiness</div>
6+
<input id="cloudiness" type="number" value="50">
77
</label>
88

99
<label>
10-
<div>Value2</div>
11-
<input id="value2" type="number" value="20">
10+
<div>humidity</div>
11+
<input id="humidity" type="number" value="10">
1212
</label>
1313

1414
<button type="button">Submit</button>

app/web-nn/web-nn.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { EventsManager } from "../../src/system/events-manager.js";
2-
import { WebNNProgram } from "../../src/modules/webnn/webnn-program.js";
2+
import { Program } from "../../src/webnn/program.js";
33

44
export default class WebNNView extends HTMLElement {
55
static tag = "webnn-view";
66

77
#eventsManager = new EventsManager();
8-
#program = new WebNNProgram();
8+
#program = new Program();
99

1010
constructor() {
1111
super();
@@ -25,30 +25,35 @@ export default class WebNNView extends HTMLElement {
2525
await this.#program.init();
2626

2727
// 2. build graph
28-
const descriptor = {dataType: 'float32', shape: [1]};
29-
const A = this.#program.addToGraph("input", "A", descriptor);
30-
const B = this.#program.addToGraph("input", "B", descriptor);
31-
const C = this.#program.addToGraph("add", A, B);
32-
this.#program.build({C});
28+
const features = this.#program.addToGraph("input", "features", {dataType: 'float32', shape: [1, 2]});
29+
const weights = this.#program.addConstant({dataType: 'float32', shape: [2,1]}, [0.7, 0.3]);
30+
const bias = this.#program.addConstant({dataType: 'float32', shape: [1]}, [-0.5]);
31+
32+
const weightedSum = this.#program.addToGraph("matmul", features, weights);
33+
const output = this.#program.addToGraph("add", weightedSum, bias);
34+
35+
await this.#program.build({output});
3336

3437
// 3. add input and output tensors
35-
await this.#program.addInputTensor("A", A);
36-
await this.#program.addInputTensor("B", B);
37-
await this.#program.addOutputTensor("C", C);
38+
await this.#program.addInputTensor("features", features);
39+
await this.#program.addOutputTensor("output", output);
3840
})
3941
}
4042

4143
async disconnectedCallback() {
4244
this.#eventsManager = this.#eventsManager.dispose();
45+
this.#program = this.#program.dispose();
4346
}
4447

45-
async #submit(event) {
46-
const value1 = this.shadowRoot.querySelector("#value1").value;
47-
const value2 = this.shadowRoot.querySelector("#value2").value;
48+
async disconnectedCallback() {
49+
this.#eventsManager = this.#eventsManager.dispose();
50+
}
4851

49-
await this.#program.set("A", [value1]);
50-
await this.#program.set("B", [value2]);
52+
async #submit(event) {
53+
const cloudiness = this.shadowRoot.querySelector("#cloudiness").value;
54+
const humidity = this.shadowRoot.querySelector("#humidity").value;
5155

56+
await this.#program.set("features", [cloudiness, humidity]);
5257
this.shadowRoot.querySelector("#result").textContent = await this.#program.run();
5358
}
5459
}

src/modules/webnn.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/modules/webnn/builders/context-options-builder.js

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +0,0 @@
1-
/**
2-
* OperandDescriptorBuilder class to build MLOperandDescriptor objects.
3-
* @see https://www.w3.org/TR/webnn/#dictdef-mloperanddescriptor
4-
*/
5-
import DataType from "./../data-type.js";
6-
7-
export default class OperandDescriptorBuilder {
8-
#dataType;
9-
#shape;
10-
11-
/**
12-
* Creates an instance of OperandDescriptorBuilder.
13-
* @param {string} [dataType=DataType.FLOAT32] - The data type of the operand.
14-
* @param {number[]} [shape=[]] - The shape of the operand.
15-
*/
16-
constructor(dataType = DataType.FLOAT32, shape = [1]) {
17-
this.#dataType = dataType;
18-
this.#shape = shape;
19-
}
20-
21-
/**
22-
* Sets the data type of the operand.
23-
* @param {string} dataType - The data type of the operand.
24-
* @returns {OperandDescriptorBuilder} The builder instance.
25-
* @example
26-
* const builder = new OperandDescriptorBuilder().setDataType(DataType.INT16);
27-
*/
28-
setDataType(dataType) {
29-
this.#dataType = dataType;
30-
return this;
31-
}
32-
33-
/**
34-
* Sets the shape of the operand.
35-
* @param {number[]} shape - The shape of the operand.
36-
* @returns {OperandDescriptorBuilder} The builder instance.
37-
* @example
38-
* const builder = new OperandDescriptorBuilder().setShape([1, 2, 3]);
39-
*/
40-
setShape(shape) {
41-
this.#shape = shape;
42-
return this;
43-
}
44-
45-
/**
46-
* Builds the MLOperandDescriptor object.
47-
* @returns {{dataType: string, shape: number[]}} The operand descriptor.
48-
* @example
49-
* const descriptor = new OperandDescriptorBuilder()
50-
* .setDataType(DataType.FLOAT32)
51-
* .setShape([1, 2, 3])
52-
* .build();
53-
*/
54-
build() {
55-
return {
56-
dataType: this.#dataType,
57-
shape: this.#shape
58-
};
59-
}
60-
}

src/modules/webnn/webnn-program.js

Lines changed: 0 additions & 66 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)