Skip to content

Fix integer divisor handling in portable remainder - #24348

Merged
JakeStevens merged 3 commits into
pytorch:mainfrom
JakeStevens:fix-remainder-int-divisor
Nov 5, 2026
Merged

JakeStevens merged 3 commits into
pytorch:mainfrom
JakeStevens:fix-remainder-int-divisor

Conversation

@JakeStevens

@JakeStevens JakeStevens commented Oct 30, 2026 •

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 (int32 and int64), TensorIntegerDivisionByZeroFails, ScalarIntegerDivisionByZeroFails, ScalarIsCastToIntegerDtype, ScalarIsCastToUnsignedDtype, 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 1743 tests pass. With the kernel's scalar zero check removed, ScalarIntegerDivisionByZeroFails, ScalarIsCastToIntegerDtype, and ScalarIsCastToUnsignedDtype fail. 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.

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

⏳ No Failures, 8 Pending

As of commit ebce162 with merge base 728e13f (image):
💚 Looks good so far! There are no failures yet. 💚

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
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@JakeStevens

Copy link
Copy Markdown
Contributor Author

note: there are some similar issues in other files, these are addressed in other PRs.

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).
The scalar zero-divisor assertion shared a test with the tensor one, and
since the kernel context's failure state is sticky it could never fail.
Split them, make the tensor half of MostNegativeByMinusOneIsZero start
from a non-zero out, cover int32, and add a uint8 scalar cast case.

This change was authored with AI assistance (Claude Code).
@JakeStevens
JakeStevens force-pushed the fix-remainder-int-divisor branch from bb176e7 to ec812eb Compare November 5, 2026 14:28
@JakeStevens
JakeStevens merged commit 8d34671 into pytorch:main Nov 5, 2026
@JakeStevens
JakeStevens deleted the fix-remainder-int-divisor branch November 5, 2026 19:57
JakeStevens added a commit that referenced this pull request Nov 24, 2026
…25292)

Portable `div.out_mode` and `div.Scalar_mode_out` computed `val_a /
val_b` before choosing a rounding mode, and `utils::floor_divide`
divided directly, so int32 or int64 `INT_MIN / -1` overflowed and killed
the process with SIGFPE on x86. `floor_divide` now returns the dividend
in that case, like ATen's `c10::div_floor_integer`, and a new
`utils::trunc_divide` does the same for trunc mode, so each mode does
only its own division.

While here, `fmod.Scalar_out` and `div.Scalar_mode_out` no longer reject
an integer zero divisor for an empty input. ATen checks the divisor per
element, and `remainder.Scalar_out` already behaves this way as of
#24348.

Fixes #25285.

### Test plan
New `MostNegativeByMinusOneWraps` tests for div (tensor and scalar,
trunc and floor) and floor_divide, int32 and int64, plus empty-input
zero-divisor tests for fmod and div. arm64 doesn't trap on `INT_MIN /
-1`, so I built Debug with
`-fsanitize=signed-integer-overflow,integer-divide-by-zero
-fno-sanitize-recover=all`. All 1919 `portable_kernels_test` tests pass,
and the div, floor_divide, fmod and remainder tests pass in
`optimized_kernels_test`. With only the kernel sources reverted to main,
the new tests trip UBSan at `op_div.cpp` (both overloads) and
`math_util.h`.

This PR was authored with AI assistance (Claude Code).
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.

2 participants