Repository navigation
Commit 1725247
Fix sign and float64 precision of portable remainder (pytorch#23683)
### Summary
Fixes pytorch#23682.
ATen's `remainder` gives a result with the sign of the divisor.
`utils::remainder_override` in `kernels/portable/cpu/util/math_util.h`
follows that for floating-point types: it adds `b` when exactly one
operand is negative and the `fmod` result is nonzero, as the comment
above the overloads describes. The integer overload returned `a % b`, so
`-7 % 3` gave `-1` instead of `2`. This adds the same correction for
signed integer types.
The floating-point overload also kept the `fmod` result in a `float`, so
float64 results were rounded to float precision (`123456789.123456789 %
1000` gave `789.1234741210938` instead of `789.1234567910433`). It now
uses `CTYPE`.
Both `remainder.Tensor_out` and `remainder.Scalar_out` in the portable
kernels use this helper, and so does the Cadence HiFi `remainder`
operator.
### Test plan
Added two tests to `kernels/test/op_remainder_test.cpp`:
- `IntegerResultHasSignOfDivisor` covers all four sign combinations plus
exact multiples, for both the Tensor and Scalar variants.
- `DoubleKeepsDoublePrecision` checks a float64 case with `atol=1e-6`.
Rounding to float would be off by about `1.7e-5`.
I built `portable_kernels_test` with CMake (`EXECUTORCH_BUILD_TESTS=ON`)
on Linux x86_64 (WSL2). Before this change, both new tests fail; after
it, all three `OpRemainderOutTest` tests pass, and the full
`portable_kernels_test` run passes (1650 tests). `clang-format` 18.1.3
reports no changes on the two files.
This PR was authored with AI assistance (Claude Code).
cc @larryliu0820 @manuelcandales @JakeStevens @nil-is-all
Co-authored-by: Jacob Stevens <stevens.jacob1492@gmail.com>1 parent ff09af0 commit 1725247
2 files changed
Lines changed: 34 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
| 213 | + | |
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
224 | | - | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
225 | 231 | | |
226 | 232 | | |
227 | 233 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
0 commit comments