intel / webml-polyfill

Deprecated, the Web Neural Network Polyfill project has been moved to https://github.com/webmachinelearning/webnn-polyfill
Apache License 2.0
161 stars 42 forks source link

[Example] FPS drops when integrate refactored BaseRunner.js #1197

Closed ibelem closed 4 years ago

ibelem commented 4 years ago

When trying to integrate @BruceDai's latest refactored BaseRunner.js, the FPS drops 2 (9->7) in WebNN meeting (Deeplab 257 Atrous) on i7-6700 + Ubuntu desktop.

This issue also occurs on WebML SS Examples (Linux Laptop). Good news is the inference time is faster than before.

Full Screen (WebNN SS Examples) Before Refactor Before Refactor After Refactor After Refactor
Inference Time(ms) FPS Inference Time(ms) FPS
DeepLab 224 52.72 16 48.18 12 (25% ↓)
DeepLab 257 Atrous 45.92 17 41.75 12 (30% ↓)
BruceDai commented 4 years ago

The root cause was that converting inputTensor of none typedArray to inputTensor of specified typedArray consumed some timing.

    tensorArray = getTensorArray(src, options);

    const typedArray = this._getInputTensorTypedArray();
    this._inputTensor = [new typedArray(tensorArray)]; # This converting will consume some timing
Full Screen (WebNN SS Examples) Before fixing After fixing
  FPS FPS
DeepLab 224 12 16
DeepLab 257 Atrous 12 17

The inference time after fixing were similar to ones before refactoring.

huningxin commented 4 years ago

Thanks for documenting the root cause. So the learning is to avoid creating typed array in hot loop, is that correct?

BruceDai commented 4 years ago

I did type converting by

typedArray(tensorArray)

Here tensorArray is just an array from

new Array([size])

it's not ArrayBuffer type, while 'typedArray ' could be Float32Array or Uint8Array on condition.

We need avoid to type converting for those array of large size in hot loop on Camera scene , thanks @huningxin

huningxin commented 4 years ago

Creation of typed array from array is expensive. Thanks for fixing that.