-
Notifications
You must be signed in to change notification settings - Fork 2k
[WASM] Add ClipByValue
#2405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WASM] Add ClipByValue
#2405
Conversation
annxingyuan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @annxingyuan, @dsmilkov, @Maratyszcza, and @tafsiri)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 60 at r1 (raw file):
const int flags = 0; OperatorCacheKey cache_key = { static_cast<float>(channels),
Just wondering - what are the implications of not performing the cast?
Maratyszcza
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @dsmilkov, @Maratyszcza, and @tafsiri)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 71 at r1 (raw file):
xnn_status status = xnn_create_clamp_nc_f32(channels, strides, strides, min, max, flags, &clamp_op); if (status != xnn_status_success) {
The function should exit early if xnn_create_clamp_nc_f32 failed. It is not safe to call xnn_setup_clamp_nc_f32 and xnn_run_operator with nullptr for the op
dsmilkov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @annxingyuan, @Maratyszcza, and @tafsiri)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 60 at r1 (raw file):
Previously, annxingyuan (Ann Yuan) wrote…
Just wondering - what are the implications of not performing the cast?
compile error (can't auto-cast int to float)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 71 at r1 (raw file):
Previously, Maratyszcza (Marat Dukhan) wrote…
The function should exit early if
xnn_create_clamp_nc_f32failed. It is not safe to callxnn_setup_clamp_nc_f32andxnn_run_operatorwithnullptrfor the op
Thanks! #2367 will replace all util::warn calls with throw_js_exception which will result in terminating the program.
Maratyszcza
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @annxingyuan, @Maratyszcza, and @tafsiri)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 60 at r1 (raw file):
Previously, dsmilkov (Daniel Smilkov) wrote…
compile error (can't auto-cast int to float)
Why do you use floats as keys? It isn't safe (conversion int->float is lossy, and any NaN would break everything, because (NaN == NaN) == false
dsmilkov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL. I also added unit tests
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @annxingyuan, @Maratyszcza, and @tafsiri)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 60 at r1 (raw file):
Previously, Maratyszcza (Marat Dukhan) wrote…
Why do you use
floats as keys? It isn't safe (conversionint->floatis lossy, and any NaN would break everything, because(NaN == NaN) == false
Because my cache key had a mixture of ints and floats and I didn't know about std::tuple. Changed to std::tuple and removed the casting.
Maratyszcza
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @annxingyuan, @dsmilkov, @Maratyszcza, and @tafsiri)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 58 at r2 (raw file):
xnn_operator_t clamp_op = nullptr; const int channels = x_info.size;
Since you don't use strides, you can set channels = strides = 1 and batch_size = x_info.size. This will get you much better reuse of xnn_operator_t objects, because channels is a part of the key, but batch_size is not.
dsmilkov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @annxingyuan, @Maratyszcza, and @tafsiri)
tfjs-backend-wasm/src/cc/kernels/ClipByValue.cc, line 58 at r2 (raw file):
Previously, Maratyszcza (Marat Dukhan) wrote…
Since you don't use strides, you can set
channels = strides = 1andbatch_size = x_info.size. This will get you much better reuse ofxnn_operator_tobjects, becausechannelsis a part of the key, butbatch_sizeis not.
Nice! I reduced the cache key to 2 floats (min & max) since all the other arguments are hard-coded to constants.
Call into XNNPACK clamp operator for doing clipping.
NOTE: we can't do operator cleanup upon disposal of a tensor, since there are no weights-like tensors for clamp
This change is