tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.48k stars 1.93k forks source link

[wasm]xnnpack not work when use wasm backend #7739

Open vacing opened 1 year ago

vacing commented 1 year ago

System information

        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm/dist/tf-backend-wasm.js"></script>
            tf.env().set('WASM_HAS_SIMD_SUPPORT', true)
            tf.wasm.setThreadsCount(1);
            tf.setBackend('wasm').then(() => main());

{ "tfjs-core": "4.6.0", "tfjs-backend-cpu": "4.6.0", "tfjs-backend-webgl": "4.6.0", "tfjs-data": "4.6.0", "tfjs-layers": "4.6.0", "tfjs-converter": "4.6.0", "tfjs": "4.6.0" }

model: https://tfhub.dev/mediapipe/tfjs-model/selfie_segmentation/general/1

Describe the current behavior use wasm kernel only, 10 times slower than webgl/webgpu(webgl and webgpu almost same speed, also weired)

Describe the expected behavior use some xnnpack kernel

gaikwadrahul8 commented 1 year ago

Hi, @vacing

Thank you for reporting this issue, Could you please help us with steps and error log to reproduce the same issue from our end and May I know are facing this issue in browser or node.js environment please ? Thank you!

vacing commented 1 year ago

Hi, @vacing

Thank you for reporting this issue, Could you please help us with steps and error log to reproduce the same issue from our end and May I know are facing this issue in browser or node.js environment please ? Thank you!

I retest the case today and fall back tfjs version to 4.6.0(default release version changed to 4.7.0), it's much fast than before, maybe I made a mistake I enabled debug and want to confirm the real backend(raw cpu or xnnpack) being used, but I can see the following only, it has no kernel details

FusedConv2D 0.5400000214576721ms 4D 1,72,128,16 147456 x: 4D 1,72,128,16 filter: 4D 1,1,16,16 bias: 1D 16

could you tell me how to get the kernel details, such as used xnnpack or raw cpu? I can do that using ort web, but failed in tfjs

gaikwadrahul8 commented 1 year ago

Hi, @vacing

May I know have you tried to use tf.getBackend and tf.engine to get details about backend please ? If not could you please try to use and see whether are your able to get the kernel details and also check this source code which may help you to solve your problem ?

If you're unable to get the kernel details please let us know ? Thank you!

vacing commented 1 year ago

Hi, @vacing

May I know have you tried to use tf.getBackend and tf.engine to get details about backend please ? If not could you please try to use and see whether are your able to get the kernel details and also check this source code which may help you to solve your problem ?

If you're unable to get the kernel details please let us know ? Thank you!

after search the tf.engine() struct and search in the source code, I guess xnnpack is not expose to user but as a multithread implementation of wasm, so I can't know if wasm used xnnpack or not like onnxruntime(which I can get the follow info when profile)

{"cat" : "Node","pid" :42,"tid" :890620,"dur" :0,"ts" :355585,"ph" : "X","name" :"XnnpackExecutionProvider_Conv_0_fence_before","args" : {"op_name" : **"Conv"}},**