Skip to content
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

[TFLite] Fix potential overflow in 64-bit MultiplyByQuantizedMultiplier function #42134

Conversation

Tessil
Copy link
Contributor

@Tessil Tessil commented Aug 7, 2020

Hi,

This PR fixes potential overflows than can happen in the 64-bit version of MultiplyByQuantizedMultiplier.

Overflow example:

int32_t quantized_multiplier;
int shift;
double scale = 0.00097656;
QuantizeMultiplier(scale, &quantized_multiplier, &shift);

const int64_t x = 10000;
std::cout << x*scale << std::endl;
std::cout << MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift) << std::endl;

Output:

9.7656
-10 // Should be 10

Thanks,
Thibaut

@google-ml-butler google-ml-butler bot added the size:XS CL Change Size: Extra Small label Aug 7, 2020
@gbaned gbaned self-assigned this Aug 7, 2020
@gbaned gbaned added the comp:lite TF Lite related issues label Aug 7, 2020
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation Aug 7, 2020
@MeghnaNatraj MeghnaNatraj requested review from rthadur and removed request for MeghnaNatraj August 7, 2020 21:20
@gbaned gbaned added the awaiting review Pull request awaiting review label Aug 11, 2020
@Tessil
Copy link
Contributor Author

Tessil commented Sep 21, 2020

Hi @suharshs, could you take a look to the PR? Thanks.

@advaitjain
Copy link
Member

Apologies for the delay. If @suharshs can take a look, that would be great.

I would usually ask for a test that currently fails but passes with the change but I don't see any existing tests for MultiplyByQuantizedMultipler so I'll defer to @suharshs on this as well.

@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Sep 23, 2020
@gbaned gbaned added the awaiting review Pull request awaiting review label Sep 23, 2020
@suharshs suharshs requested review from jianlijianli and daverim and removed request for suharshs September 29, 2020 15:23
@gbaned
Copy link
Contributor

gbaned commented Oct 6, 2020

@jianlijianli, @daverim Can you please take a look on this PR ? Thanks!

@advaitjain advaitjain removed their request for review October 23, 2020 20:06
@talumbau talumbau self-requested a review November 3, 2020 15:50
@talumbau talumbau self-assigned this Nov 3, 2020
Copy link
Member

@talumbau talumbau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this change! Looks good. I looked around for the right spot for a test. It looks like it the test should go in tensorflow/lite/kernels/internal/quantization_util_test.cc (it already includes this common.h). We should have more tests for this function, so it would be good to start with at least one. Thanks!

PR Queue automation moved this from Assigned Reviewer to Reviewer Requested Changes Nov 3, 2020
@Tessil
Copy link
Contributor Author

Tessil commented Nov 3, 2020

Thank you, I hesitated as there was no existing tests for any of the MultiplyByQuantizedMultiplier versions. I will check to add some tests as these are quite central functions.

@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Nov 5, 2020
@gbaned gbaned added the stat:awaiting response Status - Awaiting response from author label Nov 5, 2020
@Tessil
Copy link
Contributor Author

Tessil commented Nov 6, 2020

@talumbau I added some tests for the int32 and int64 versions of the MultiplyByQuantizedMultiplier function.

I had to modify a bit the BUILD file to link libm as I had some undefined reference to symbol 'round@@GLIBC_2.2.5 error when compiling the quantization_util_test.

@google-ml-butler google-ml-butler bot added the ready to pull PR ready for merge process label Dec 7, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Dec 7, 2020
@talumbau
Copy link
Member

talumbau commented Dec 8, 2020

Hi Thibaut,

I looked at the Windows Bazel GPU CI build failure, as well as the Intel oneDNN build failure. They look spurious to me and not related to this change. Can you attempt to rebase and invoke the submit process again? I believe that change in 0cead48 should be good.

@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label Dec 8, 2020
@Tessil
Copy link
Contributor Author

Tessil commented Dec 8, 2020

I merged the latest changes, thanks.

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Dec 8, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Dec 8, 2020
@talumbau
Copy link
Member

talumbau commented Dec 9, 2020

Looks like there are some builds with additional warnings

third_party/tensorflow/lite/kernels/internal/quantization_util_test.cc:453:49: error: operator '<<' has lower precedence than '-'; '-' will be evaluated first [-Werror,-Wshift-op-parentheses]
                  static_cast<double>(1ll << 31 - (-3)));
                                          ~~ ~~~^~~~~~
third_party/tensorflow/lite/kernels/internal/quantization_util_test.cc:453:49: note: place parentheses around the '-' expression to silence this warning
                  static_cast<double>(1ll << 31 - (-3)));
                                                ^

Can you fix please?

@gbaned gbaned removed the ready to pull PR ready for merge process label Dec 9, 2020
@Tessil
Copy link
Contributor Author

Tessil commented Dec 9, 2020

Thank you, I fixed the warning.

Is there a reason to not enable these warnings and -Werror in the GitHub CI build? I have to see if we can modify a bit our internal CI to more closely match the warnings you use.

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Dec 9, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Dec 9, 2020
@talumbau
Copy link
Member

talumbau commented Dec 9, 2020

That's a good idea. I have filed this issue internally. Thanks for your patience.

@talumbau
Copy link
Member

talumbau commented Dec 9, 2020

Looks like the Github CI needs significant improvement. I see a failure with this command:

bazel --blazerc=/dev/null test --compilation_mode=opt --copt=-UNDEBUG tensorflow/lite/kernels/internal:quantization_util_test

it fails on the assert here:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/kernels/internal/common.h#L179

Can you reproduce?

@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label Dec 10, 2020
@Tessil
Copy link
Contributor Author

Tessil commented Dec 10, 2020

Thank you, the 64-bit MultiplyByQuantizedMultiplier effectively doesn't support negative multipliers. From what I gather it's an intentional behaviour and some HW implementations rely on the fact that the multiplier is >= 0. The TOSA spec also has an assert(multiplier >= 0); in apply_scale_32.

I removed the tests with negative multipliers for the 64-bit version but left them for the 32-bit version as some kernels use negative multipliers (notably the SUB operator to reuse the ADD kernel).

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Dec 10, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Dec 10, 2020
@copybara-service copybara-service bot merged commit d79e6fa into tensorflow:master Dec 10, 2020
PR Queue automation moved this from Approved by Reviewer to Merged Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes comp:lite TF Lite related issues ready to pull PR ready for merge process size:XS CL Change Size: Extra Small
Projects
PR Queue
  
Merged
Development

Successfully merging this pull request may close these issues.

None yet

8 participants