1+ import DeviceType from "./webnn/device-type.js" ;
2+ import PowerOptions from "./webnn/power-options.js" ;
3+ import DataType from "./webnn/data-type.js" ;
4+ import OperandDescriptorBuilder from "./webnn/builders/operand-descriptor-builder.js" ;
5+
6+ class WebNNModule {
7+ static async available ( ) {
8+ return navigator . ml !== undefined ;
9+ }
10+
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+
26+ static async createContext ( args = { } ) {
27+ const options = await this . createContextOption ( args . options ) ;
28+ return await navigator . ml . createContext ( options ) ;
29+ }
30+
31+ static async createGraph ( args = { } ) {
32+ // https://www.w3.org/TR/webnn/#mlgraphbuilder
33+ const { context = await this . createContext ( ) } = args ;
34+ return new MLGraphBuilder ( context ) ;
35+ }
36+ }
37+
38+ export {
39+ WebNNModule ,
40+ DeviceType ,
41+ PowerOptions ,
42+ DataType ,
43+ OperandDescriptorBuilder
44+ } ;
0 commit comments