Skip to content

Commit 4be01af

Browse files
committed
refactor: enhance WebNN module with ContextOptionsBuilder and improve OperandDescriptorBuilder documentation
1 parent 1288443 commit 4be01af

File tree

4 files changed

+69
-21
lines changed

4 files changed

+69
-21
lines changed

app/web-nn/web-nn.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { WebNNModule, OperandDescriptorBuilder } from "../../src/modules/webnn.js";
1+
import {
2+
WebNNModule,
3+
OperandDescriptorBuilder } from "../../src/modules/webnn.js";
24

35
export default class WebNNView extends HTMLElement {
46
static tag = "webnn-view";
@@ -15,9 +17,7 @@ export default class WebNNView extends HTMLElement {
1517
url: import.meta.url,
1618
});
1719

18-
const operandType = await WebNNModule.createOperandType({ dimensions: [2, 2]});
1920
const graphBuilder = await WebNNModule.createGraph();
20-
2121
const constant2 = graphBuilder.constant(new OperandDescriptorBuilder().build(), new Float32Array([0.2]));
2222
console.log(constant2);
2323
}

src/modules/webnn.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,15 @@ import DeviceType from "./webnn/device-type.js";
22
import PowerOptions from "./webnn/power-options.js";
33
import DataType from "./webnn/data-type.js";
44
import OperandDescriptorBuilder from "./webnn/builders/operand-descriptor-builder.js";
5+
import ContextOptionsBuilder from "./webnn/builders/context-options-builder.js";
56

67
class WebNNModule {
78
static async available() {
89
return navigator.ml !== undefined;
910
}
1011

11-
static createOperandDescriptor(args = {}) {
12-
const { dataType = DataType.FLOAT32, shape = [] } = args;
13-
return { dataType, shape };
14-
}
15-
16-
static createOperandType(args= {}) {
17-
const { dataType = DataType.FLOAT32, dimensions = [] } = args;
18-
return { dataType, dimensions };
19-
}
20-
21-
static async createContextOption(args = {}) {
22-
const { deviceType = DeviceType.GPU, powerPreference = PowerOptions.DEFAULt } = args;
23-
return { deviceType, powerPreference };
24-
}
25-
2612
static async createContext(args = {}) {
27-
const options = await this.createContextOption(args.options);
13+
const options = new ContextOptionsBuilder(args.deviceType, args.powerPreference).build();
2814
return await navigator.ml.createContext(options);
2915
}
3016

@@ -40,5 +26,6 @@ export {
4026
DeviceType,
4127
PowerOptions,
4228
DataType,
43-
OperandDescriptorBuilder
29+
OperandDescriptorBuilder,
30+
ContextOptionsBuilder
4431
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import PowerOptions from "./../power-options.js";
2+
import DeviceType from "../device-type.js";
3+
4+
export default class ContextOptionsBuilder {
5+
#contextType;
6+
#powerPreference;
7+
8+
constructor(contextType = DeviceType.GPU, powerPreference = PowerOptions.DEFAULt) {
9+
this.#contextType = contextType;
10+
this.#powerPreference = powerPreference;
11+
}
12+
13+
setContextType(contextType) {
14+
this.#contextType = contextType;
15+
return this;
16+
}
17+
18+
setPowerPreference(powerPreference) {
19+
this.#powerPreference = powerPreference;
20+
return this;
21+
}
22+
23+
build() {
24+
return {
25+
deviceType: this.#contextType,
26+
powerPreference: this.#powerPreference
27+
};
28+
}
29+
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
1-
import DataType from "./../data-type.js";
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";
26

37
export default class OperandDescriptorBuilder {
48
#dataType;
59
#shape;
610

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+
*/
716
constructor(dataType = DataType.FLOAT32, shape = []) {
817
this.#dataType = dataType;
918
this.#shape = shape;
1019
}
1120

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+
*/
1228
setDataType(dataType) {
1329
this.#dataType = dataType;
1430
return this;
1531
}
1632

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+
*/
1740
setShape(shape) {
1841
this.#shape = shape;
1942
return this;
2043
}
2144

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+
*/
2254
build() {
2355
return {
2456
dataType: this.#dataType,

0 commit comments

Comments
 (0)