const context await navigator ml createContext default deviceType is cpu console log context context deviceType cpu gpu npu default value is cpu Create WebNN MLContext const context await navigator ml createContext deviceType cpu Create a WebNN MLTensor const inputMLTensor await context createTensor dataType float32 shape 1 3 writable true readable true console log inputMLTensor inputMLTensor typeof inputMLTensor typeof inputMLTensor Write data to the MLTensor const inputArrayBuffer new Float32Array 1 3 fill 1 2 await context writeTensor inputMLTensor inputArrayBuffer Read and log the results const outputArrayBuffer new Float32Array 1 3 await context readTensor inputMLTensor outputArrayBuffer console log outputArrayBuffer Output should be 1 2 1 2 console log ready Define the context and graph builder const context await navigator ml createContext const builder new MLGraphBuilder context console log builder builder typeof builder typeof builder for let b in builder console log b
new Float32Array 1 3 fill 1 2 await context writeTensor inputMLTensor inputArrayBuffer Read and log the results const outputArrayBuffer new Float32Array 1 3 await context readTensor inputMLTensor outputArrayBuffer console log outputArrayBuffer Output should be 1 2 1 2 console log ready Define the context and graph builder const context await navigator ml createContext const builder new MLGraphBuilder context console log builder builder typeof builder typeof builder for let b in builder console log b