Repository navigation
Fix integer divisor handling in portable remainder - #24348
Open
JakeStevens wants to merge 1 commit into
Open
JakeStevens wants to merge 1 commit into
JakeStevens wants to merge 1 commit into
Conversation
remainder.Scalar_out checked the scalar for zero as a double and then cast it to the compute dtype, so a scalar such as 256 for an int8 tensor reached a % 0. The check now runs on the cast value, matching ATen, where 256 raises ZeroDivisionError and 255 and 257 wrap to -1 and 1. The integer remainder_override also computed INT_MIN % -1, which overflows; like ATen, a divisor of -1 now returns 0. This change was authored with AI assistance (Claude Code).
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24348
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
remainder.Scalar_outchecked the scalar for zero as adoubleand then cast it to the compute dtype, so a scalar such as 256 for an int8 tensor became 0 and reacheda % 0(SIGFPE on x86). The check now runs on the cast value, so like ATen, 256 raises a division-by-zero error while 255 and 257 wrap to -1 and 1. Empty inputs skip the check and return an empty tensor, also matching ATen.The integer
remainder_overridecomputedINT_MIN % -1, which overflows. Like ATen'sremainder_kernel, a divisor of -1 now returns 0.These are the same issues #24170 fixes for
fmodanddiv, found while reviewing that PR.Test plan
Added
MostNegativeByMinusOneIsZero,IntegerDivisionByZeroFails,ScalarIsCastToIntegerDtype, andEmptyInputWithZeroScalarDivisortoop_remainder_test.cpp; expected values match eager PyTorch 2.14. Builtportable_kernels_testwith CMake (Release,EXECUTORCH_BUILD_TESTS=ON) on macOS arm64: all 1741 tests pass. With the kernel changes reverted,ScalarIsCastToIntegerDtypefails.MostNegativeByMinusOneIsZeropasses on arm64 either way becausesdivdoes not trap; it guards the x86 crash.This PR was authored with AI assistance (Claude Code), with an additional review pass by Codex.