-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[webgpu] Update INT_DIV #7792
[webgpu] Update INT_DIV #7792
Conversation
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.
LGTM, thanks!
let remainder = round(numerator % denominator); | ||
let quotient = round((numerator - remainder) / denominator); | ||
let resultTemp = | ||
select(quotient, quotient - 1, sign(remainder) == -sign(denominator)); |
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.
Several questions and comments:
- round() in WGSL has a special definition: "Result is the integer k nearest to e, as a floating point value. When e lies halfway between integers k and k + 1, the result is k when k is even, and k + 1 when k is odd." Will this introduce a diff between CPU and WebGPU solution?
- For "let remainder = round(numerator % denominator);" Is round() necessary?
- What's the result of "9 / (-2)" in this code?
- numerator -> dividend, denominator -> divisor?
- Please ensure we have enough unit test cases for them, especially when dividend and/or divisor are halfway between integers, the results from "(numerator - remainder) / denominator" are halfway between integers, etc.
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.
Several questions and comments:
- round() in WGSL has a special definition: "Result is the integer k nearest to e, as a floating point value. When e lies halfway between integers k and k + 1, the result is k when k is even, and k + 1 when k is odd." Will this introduce a diff between CPU and WebGPU solution?
This is not clearly mentioned in the TensorFlow.js API doc. Even if the CPU rounding mode differs from the GPU backend, I cannot tell which one is more correct, or if either is fine.
- For "let remainder = round(numerator % denominator);" Is round() necessary?
No, but I feel that's more aligned with neighboring lines.
- What's the result of "9 / (-2)" in this code?
-5
- numerator -> dividend, denominator -> divisor?
The API doc prefers numerator/denominator.
- Please ensure we have enough unit test cases for them, especially when dividend and/or divisor are halfway between integers, the results from "(numerator - remainder) / denominator" are halfway between integers, etc.
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.
Different languages may have different results.
[python]
(-9) % 2 -> 1
9 % (-2) -> -1
[JavaScript]
(-9) % 2 -> -1
9 % (-2) -> 1
So your result will be at least different from the JS's result.
With round(), we introduce another level of difference between CPU and GPU, at least for the quotient calculation.
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.
Please note INT_DIV
is exclusively used by floorDiv()
, and the doc states the result should be floored. This matches with the current CPU behavior (Math.floor(a / b)
).
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.
JiaJie, if INT_DIV
is only used for floorDiv
, will it be better to rename INT_DIV
to FLOOR_DIV
?
I have concern that the whole implementation of |
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.
Good work, Jiajie. Please also add some test cases for float32
type in tfjs-core.
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.
I defer to Zhaoming's review on this.
@jzm-intel Do you have any concern with the latest change? The test can be found here #7809. |
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.
LGTM, thanks
export const floorDiv = binaryKernelFunc({ | ||
opType: BinaryOpType.FLOOR_DIV, | ||
cpuKernelImpl: floorDivImplCPU, | ||
dtype: 'int32' |
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.
Why does it state as 'int32'? This is the last thing that confuse me....
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.
This is the result dtype.
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.
Thanks, still LGTM.
To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.