Skip to content

Commit 1725247

Browse files
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

File tree

‎kernels/portable/cpu/util/math_util.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ template <
210210
typename std::enable_if<std::is_floating_point<CTYPE>::value, int>::type =
211211
0>
212212
CTYPE remainder_override(CTYPE a, CTYPE b) {
213-
float rem = std::fmod(a, b);
213+
CTYPE rem = std::fmod(a, b);
214214
if (((a < 0) ^ (b < 0)) && rem != 0) {
215215
rem += b;
216216
}
@@ -221,7 +221,13 @@ template <
221221
typename CTYPE,
222222
typename std::enable_if<std::is_integral<CTYPE>::value, int>::type = 0>
223223
CTYPE remainder_override(CTYPE a, CTYPE b) {
224-
return a % b;
224+
CTYPE rem = a % b;
225+
if constexpr (std::is_signed<CTYPE>::value) {
226+
if (((a < 0) ^ (b < 0)) && rem != 0) {
227+
rem += b;
228+
}
229+
}
230+
return rem;
225231
}
226232

227233
} // namespace utils

‎kernels/test/op_remainder_test.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,29 @@ TEST_F(OpRemainderOutTest, SmokeTest) {
4949
op_remainder_tensor_out(self, other, out);
5050
EXPECT_TENSOR_CLOSE(out, out_expected);
5151
}
52+
53+
TEST_F(OpRemainderOutTest, IntegerResultHasSignOfDivisor) {
54+
TensorFactory<ScalarType::Long> tf;
55+
56+
Tensor self = tf.make({6}, {7, -7, 7, -7, 6, -6});
57+
Tensor other = tf.make({6}, {3, 3, -3, -3, 3, -3});
58+
Tensor out = tf.zeros({6});
59+
op_remainder_tensor_out(self, other, out);
60+
EXPECT_TENSOR_EQ(out, tf.make({6}, {1, 2, -2, -1, 0, 0}));
61+
62+
Tensor scalar_out = tf.zeros({6});
63+
op_remainder_scalar_out(self, 3, scalar_out);
64+
EXPECT_TENSOR_EQ(scalar_out, tf.make({6}, {1, 2, 1, 2, 0, 0}));
65+
}
66+
67+
TEST_F(OpRemainderOutTest, DoubleKeepsDoublePrecision) {
68+
TensorFactory<ScalarType::Double> tf;
69+
70+
Tensor self = tf.make({2}, {123456789.123456789, -123456789.123456789});
71+
Tensor other = tf.make({2}, {1000.0, 1000.0});
72+
Tensor out = tf.zeros({2});
73+
op_remainder_tensor_out(self, other, out);
74+
// Rounding to float would be off by about 1.7e-5
75+
EXPECT_TENSOR_CLOSE_WITH_TOL(
76+
out, tf.make({2}, {789.123456789, 210.876543211}), 0, 1e-6);
77+
}

0 commit comments

Comments
 (0)