Skip to content

Fix integer divisor handling in portable remainder - #24348

Open
JakeStevens wants to merge 1 commit into
pytorch:mainfrom
JakeStevens:fix-remainder-int-divisor
Open

JakeStevens wants to merge 1 commit into
pytorch:mainfrom
JakeStevens:fix-remainder-int-divisor

Conversation

@JakeStevens

Copy link
Copy Markdown
Contributor

Summary

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 became 0 and reached a % 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_override computed INT_MIN % -1, which overflows. Like ATen's remainder_kernel, a divisor of -1 now returns 0.

These are the same issues #24170 fixes for fmod and div, found while reviewing that PR.

Test plan

Added MostNegativeByMinusOneIsZero, IntegerDivisionByZeroFails, ScalarIsCastToIntegerDtype, and EmptyInputWithZeroScalarDivisor to op_remainder_test.cpp; expected values match eager PyTorch 2.14. Built portable_kernels_test with CMake (Release, EXECUTORCH_BUILD_TESTS=ON) on macOS arm64: all 1741 tests pass. With the kernel changes reverted, ScalarIsCastToIntegerDtype fails. MostNegativeByMinusOneIsZero passes on arm64 either way because sdiv does not trap; it guards the x86 crash.

This PR was authored with AI assistance (Claude Code), with an additional review pass by Codex.

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).
@pytorch-bot

pytorch-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown

🔗 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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant