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
Add broadcasting functionality for Div and Sub ops. #17123
Add broadcasting functionality for Div and Sub ops. #17123
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
I signed it! |
CLAs look good, thanks! |
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 for doing this. Very nice, apart from the complications of quantization.
// that handles broadcasting as the base case. The code generator would then | ||
// generate max(D1, D2) nested for loops. | ||
// TODO(benoitjacob): BroadcastDiv is intentionally duplicated from | ||
// reference_ops.h. Once an optimized version is implemented and NdArrayDesc<T> |
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 make sure we are within 80 columns everywhere.
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 checked this file and maximum line length is <=80characters. Nevertheless there was lines >80 characters in sub_test.cc and div_test.cc which I have fixed.
} | ||
} | ||
|
||
// legacy, for compatibility with old checked-in code |
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.
Since this function didn't exist before you don' t need to keep a legacy version around.
const int32 unclamped_result = | ||
output_offset + | ||
MultiplyByQuantizedMultiplierSmallerThanOne( | ||
input1_val / input2_val, output_multiplier, output_shift); |
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.
There's an accuracy issue here as the integer division will truncate a lot of values.
Leaving that aside for a second, the calculation doesn't seem correct either (quantization is hard!). The quantized multiplier is s1s2/sout so the result here is
s1s2/souti1/i2
but it should be s1/s2i1/i2/sout, right?
Again, quantization is hard, so I'll be OK if you prefer to leave quantization for a subsequent PR.
One solution is to calculate a quantized multiplier that is always greater than 1:
qm = (s1> s2sout) ? qmul(s1/s2/sout) : qmul(s2sout/s1)
and pass a hint as to whether the numerator or the denominator needs scaling
return denominator_needs_scaling ? q1 / (q2 * qm) : (q1 * qm) / q2;
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 can you provide more details or link to docs so I can understand quantized ops better.
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.
The quantization scheme used in TF Lite is similar to gemmlowp's: https://github.com/google/gemmlowp/blob/master/doc/quantization.md
Maybe we can leave the quantization for a separate PR? I'd be happy to approve the other changes, and then we can have someone with more quantization experience review the other PR.
} | ||
} | ||
|
||
// legacy, for compatibility with old checked-in code |
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.
here too: no need for legacy versions since this didn't exist before your changes.
} | ||
} | ||
|
||
// legacy, for compatibility with old checked-in code |
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.
same here for legacy (there are more of those in the other file)
…de. Update code to have 80 characters per line.
PiperOrigin-RevId: 189216312
It has been 14 days with no activity and the |
I fixed conflicts and committed them, so please remove "stalled" label |
Hi, |
Can you look at the test failures?
|
Thanks jhseu for help. |
Hi,
Added broadcasting functionality for Div and Sub ops following the examples of Add and Mul.
Regards,
Hovhannes