Repository navigation
Fix integer divisor handling in portable remainder - #24348
Conversation
🔗 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 PendingAs of commit ebce162 with merge base 728e13f ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
|
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).
bb176e7 to
ec812eb
Compare
…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).
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(int32 and int64),TensorIntegerDivisionByZeroFails,ScalarIntegerDivisionByZeroFails,ScalarIsCastToIntegerDtype,ScalarIsCastToUnsignedDtype, andEmptyInputWithZeroScalarDivisortoop_remainder_test.cpp; expected values match eager PyTorch 2.14. Builtportable_kernels_testwith CMake (Release,EXECUTORCH_BUILD_TESTS=ON) on macOS arm64: all 1743 tests pass. With the kernel's scalar zero check removed,ScalarIntegerDivisionByZeroFails,ScalarIsCastToIntegerDtype, andScalarIsCastToUnsignedDtypefail.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.