tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Customize setTimeout

Open gyagp opened this issue 3 years ago • 8 comments

If the setTimeout nesting level is greater than 5 and timeout is less than 4ms, timeout will be clamped to 4ms, which hurts the perf. A custom setTimeout is provided to mitigate the perf impact.

BUG: #6687

To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.


This change is Reviewable

gyagp avatar Jul 29 '22 14:07 gyagp

This PR is still WIP. @qjia7 @xhcao PTAL to see if you have any high-level comments. Do you have a good idea to write some test cases for this?

gyagp avatar Jul 29 '22 15:07 gyagp

This PR is still WIP. @qjia7 @xhcao PTAL to see if you have any high-level comments. Do you have a good idea to write some test cases for this?

The implementation looks good for me. How about enabling setTimeoutWPM behind a flag for a while? For the test case, maybe add a new file util_base_test.js. Test that repeat call setTimeoutWPM 20(or a larger number) times. And expect the avg time of the last 15 is less than 4 ms or less?

qjia7 avatar Aug 01 '22 05:08 qjia7

The function could be called by tf.profile, if you want to write unit cases. You could also view the total of tfjs-models' demos, the time could be reduced.

xhcao avatar Aug 01 '22 06:08 xhcao

With above changes, I get below from unit test: LOG: 'averageTime of setTimeoutWPM is 0.06666664282480876 ms' LOG: 'averageTime of setTimeout is 4 ms'

gyagp avatar Aug 03 '22 21:08 gyagp

Thank you all for the comments! I think I have addressed all of them. Could you please take another look? I also tested bodypix-MobileNetV1-image-0.25 and got below data (best time, average time): [setTimeout] 16.5, 22.5 15.3, 20.7 15.8, 21.4 17.6, 23.2 17.4, 22.5

[setTimeoutCustom] 13.1, 16.0 13.2, 18.3 13.6, 15.8 12.7, 17.0 11.7, 15.2

The data shows we can get 2-4ms back.

It seems the bots are still not satisfied, but I couldn't figure out the reason after checking the detailed log. Could you please point me to the reason? Thanks!

gyagp avatar Aug 05 '22 15:08 gyagp

@pyu10055 Can you review again the latest changes? Thanks!

gyagp avatar Aug 08 '22 04:08 gyagp

I love this improvement as it results in quite significant performance improvements

but at the moment it causes conflict with @tensorflow/tfjs-backend-wasm as soon as that module is loaded
regardless if its used or not (IMO its due to bad behavior by EMScripten, but its a conflict non-the-less),

see https://github.com/tensorflow/tfjs/issues/6687#issuecomment-1209286270 for details

vladmandic avatar Aug 09 '22 12:08 vladmandic

Thanks @vladmandic , I will investigate this.

gyagp avatar Aug 09 '22 14:08 gyagp

@pyu10055 @mattsoulanille, given related Emscripten issues were fixed (Thanks @vladmandic !) and @mattsoulanille upgraded Emscripten to include the fixes, can we merge this change now?

gyagp avatar Sep 27 '22 15:09 gyagp