11import { EventsManager } from "../../src/system/events-manager.js" ;
2- import {
3- WebNNModule ,
4- OperandDescriptorBuilder } from "../../src/modules/webnn.js" ;
2+ import { WebNNProgram } from "../../src/modules/webnn/webnn-program.js" ;
53
64export default class WebNNView extends HTMLElement {
75 static tag = "webnn-view" ;
86
97 #eventsManager = new EventsManager ( ) ;
10- #graphBuilder
11- #graph;
12- #context;
13- #tensors;
8+ #program = new WebNNProgram ( ) ;
149
1510 constructor ( ) {
1611 super ( ) ;
@@ -22,65 +17,39 @@ export default class WebNNView extends HTMLElement {
2217 url : import . meta. url ,
2318 } ) ;
2419
25- requestAnimationFrame ( ( ) => {
20+ requestAnimationFrame ( async ( ) => {
2621 const button = this . shadowRoot . querySelector ( "button" ) ;
2722 this . #eventsManager. addEvent ( button , "click" , this . #submit. bind ( this ) ) ;
28- } )
29-
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 ) ;
23+
24+ // 1. initialize
25+ await this . #program. init ( ) ;
26+
27+ // 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} ) ;
33+
34+ // 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+ } )
3439 }
3540
3641 async disconnectedCallback ( ) {
3742 this . #eventsManager = this . #eventsManager. dispose ( ) ;
3843 }
3944
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-
6445 async #submit( event ) {
6546 const value1 = this . shadowRoot . querySelector ( "#value1" ) . value ;
6647 const value2 = this . shadowRoot . querySelector ( "#value2" ) . value ;
6748
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- } ;
49+ await this . #program. set ( "A" , [ value1 ] ) ;
50+ await this . #program. set ( "B" , [ value2 ] ) ;
7951
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 ;
52+ this . shadowRoot . querySelector ( "#result" ) . textContent = await this . #program. run ( ) ;
8453 }
8554}
8655
0 commit comments