You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 186ca41
Browse filesBrowse the repository at this point in the historyBrowse files
Keep int64 precision in portable div trunc mode and fmod (pytorch#24170)
### Summary
Fixespytorch#24169.
The portable `div.out_mode` and `div.Scalar_mode_out` kernels compute
the integer quotient with `/` and then call `std::trunc` on it, which
converts it to `double` and back. Integer division already truncates, so
`std::trunc` now only runs for floating-point types. For int64 values
above 2^53, `div(a, 3, rounding_mode="trunc")` gave
`1537228672809129216` instead of `1537228672809129302`.
`fmod.Tensor_out` and `fmod.Scalar_out` switched every integer compute
type to `Double`. Integer types now use `%` through a new
`utils::integer_fmod` in `math_util.h`, which returns 0 for a divisor of
-1 so the most negative value can't overflow. Float types keep the
existing `std::fmod` path, including the vectorized one for the Scalar
variant.
### Test plan
Added `OpDivOutTest.TruncModeKeepsInt64Precision` (Tensor and Scalar
divisor) and `OpFmodTest.Int64KeepsFullPrecision` (Tensor, Scalar, and a
divisor of -1 with `INT64_MIN`). The expected values come from eager
PyTorch.
I built `portable_kernels_test` with CMake (`EXECUTORCH_BUILD_TESTS=ON`,
Release) on Linux x86_64 (WSL2) at 5b14a2f. Before this change both new
tests fail; after it, the full `portable_kernels_test` run passes (1696
tests). `clang-format` 18.1.3 reports no changes on the changed files.
This PR was authored with AI assistance (Claude Code).
cc @nil-is-all@larryliu0820@manuelcandales@JakeStevens
0 commit comments