1+ import { EventsManager } from "../../src/system/events-manager.js" ;
12import {
23 WebNNModule ,
34 OperandDescriptorBuilder } from "../../src/modules/webnn.js" ;
45
56export 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
0 commit comments