Repository navigation
Fix sign and float64 precision of portable remainder - #23683
Open
usmanmateen wants to merge 1 commit into
Open
usmanmateen wants to merge 1 commit into
usmanmateen wants to merge 1 commit into
Conversation
ATen's remainder takes the sign of the divisor, but the integer overload of remainder_override returned a % b, which takes the sign of the dividend. The floating-point overload already corrected this, but stored the fmod result in a float, so double results lost precision. Fixes pytorch#23682
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23683
Note: Links to docs will display an error until the docs builds have been completed.
|
Author
|
@pytorchbot label "release notes: ops & kernels" |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #23682.
ATen's
remaindergives a result with the sign of the divisor.utils::remainder_overrideinkernels/portable/cpu/util/math_util.hfollows that for floating-point types: it addsbwhen exactly one operand is negative and thefmodresult is nonzero, as the comment above the overloads describes. The integer overload returneda % b, so-7 % 3gave-1instead of2. This adds the same correction for signed integer types.The floating-point overload also kept the
fmodresult in afloat, so float64 results were rounded to float precision (123456789.123456789 % 1000gave789.1234741210938instead of789.1234567910433). It now usesCTYPE.Both
remainder.Tensor_outandremainder.Scalar_outin the portable kernels use this helper, and so does the Cadence HiFiremainderoperator.Test plan
Added two tests to
kernels/test/op_remainder_test.cpp:IntegerResultHasSignOfDivisorcovers all four sign combinations plus exact multiples, for both the Tensor and Scalar variants.DoubleKeepsDoublePrecisionchecks a float64 case withatol=1e-6. Rounding to float would be off by about1.7e-5.I built
portable_kernels_testwith CMake (EXECUTORCH_BUILD_TESTS=ON) on Linux x86_64 (WSL2). Before this change, both new tests fail; after it, all threeOpRemainderOutTesttests pass, and the fullportable_kernels_testrun passes (1650 tests).clang-format18.1.3 reports no changes on the two files.This PR was authored with AI assistance (Claude Code).
cc @nil-is-all