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

Fix overflow/crash in tf.range when limits is large #51359

Merged
merged 2 commits into from Aug 18, 2021

Conversation

yongtang
Copy link
Member

@yongtang yongtang commented Aug 7, 2021

This PR tries to address the issue raised in #46913 where
tf.range (and implicitly tf.keras.layers.RepeatVector)
will overflow/crash when limits is large.

The reason of the overflow is that while calculating
the size within the kernel, the conditional statements
comes with int64 = cond ? int64 : double will implicitly
convert to double first and then cast back to int64, causing
the overflow and crash.

This PR fixes the issue by casting to int64 in both selections
within the conditional statements first.

This PR fixes #46913.

Signed-off-by: Yong Tang yong.tang.github@outlook.com

This PR tries to address the issue raised in 46913 where
tf.range (and implicitly tf.keras.layers.RepeatVector)
will overflow/crash when limits is large.

The reason of the overflow is that while calculating
the size within the kernel, the conditional statements
comes with `int64 = cond ? int64 : double` will implicitly
convert to double first and then cast back to int64, causing
the overflow and crash.

This PR fixes the issue by casting to int64 in both selections
within the conditional statements first.

This PR fixes 46913.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
@google-ml-butler google-ml-butler bot added the size:S CL Change Size: Small label Aug 7, 2021
@google-cla google-cla bot added the cla: yes label Aug 7, 2021
@google-ml-butler google-ml-butler bot added the awaiting review Pull request awaiting review label Aug 7, 2021
@yongtang yongtang added the kokoro:force-run Tests on submitted change label Aug 8, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Aug 8, 2021
@gbaned gbaned self-assigned this Aug 9, 2021
@gbaned gbaned added the comp:core issues related to core part of tensorflow label Aug 9, 2021
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation Aug 9, 2021
@gbaned gbaned added the prtype:bugfix PR to fix a bug label Aug 9, 2021
Comment on lines 75 to 79
? static_cast<int64>(
(std::abs(limit - start) + std::abs(delta) - 1) /
std::abs(delta))
: static_cast<int64>(
std::ceil(std::abs((limit - start) / delta))));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's separate each branch to a separate expression to ease in readability.

PR Queue automation moved this from Assigned Reviewer to Reviewer Requested Changes Aug 9, 2021
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
@yongtang
Copy link
Member Author

yongtang commented Aug 9, 2021

Thanks @mihaimaruseac . The PR has been updated.

@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Aug 11, 2021
@google-ml-butler google-ml-butler bot added the awaiting review Pull request awaiting review label Aug 17, 2021
PR Queue automation moved this from Reviewer Requested Changes to Approved by Reviewer Aug 17, 2021
@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Aug 17, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Aug 17, 2021
@gbaned gbaned added ready to pull PR ready for merge process and removed awaiting review Pull request awaiting review ready to pull PR ready for merge process labels Aug 18, 2021
@copybara-service copybara-service bot merged commit 6d94002 into tensorflow:master Aug 18, 2021
PR Queue automation moved this from Approved by Reviewer to Merged Aug 18, 2021
@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label Aug 18, 2021
@yongtang yongtang deleted the 46913-range-overflow branch August 18, 2021 16:35
mihaimaruseac pushed a commit that referenced this pull request Oct 25, 2021
PiperOrigin-RevId: 391529518
Change-Id: Ie3db4ae6d3c0f3dc88404e1dbdc22f7d03cbeb3b
mihaimaruseac pushed a commit that referenced this pull request Oct 25, 2021
PiperOrigin-RevId: 391529518
Change-Id: Ie3db4ae6d3c0f3dc88404e1dbdc22f7d03cbeb3b
mihaimaruseac pushed a commit that referenced this pull request Oct 25, 2021
PiperOrigin-RevId: 391529518
Change-Id: Ie3db4ae6d3c0f3dc88404e1dbdc22f7d03cbeb3b
mihaimaruseac added a commit that referenced this pull request Oct 25, 2021
…1d297dbba90390d5482b76113899-on-r2.6

Merge pull request #51359 from yongtang:46913-range-overflow
mihaimaruseac added a commit that referenced this pull request Oct 25, 2021
…1d297dbba90390d5482b76113899-on-r2.5

Merge pull request #51359 from yongtang:46913-range-overflow
mihaimaruseac added a commit that referenced this pull request Oct 25, 2021
…1d297dbba90390d5482b76113899-on-r2.4

Merge pull request #51359 from yongtang:46913-range-overflow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes comp:core issues related to core part of tensorflow prtype:bugfix PR to fix a bug size:S CL Change Size: Small
Projects
PR Queue
  
Merged
Development

Successfully merging this pull request may close these issues.

tf.keras.layers.RepeatVector crashes(aborts) when n is large
5 participants