Repository navigation
fix(converters): fmod returns the wrong sign when the float quotient rounds up - #4861
Merged
Merged
Conversation
…rounds up fmod computes input - trunc_div(input, other) * other. For floating point inputs, trunc_div rounds input / other before truncating it, so a quotient just below a whole number comes out one too large: in float32, 4.2 / 1.4 rounds to 3.0, and fmod(4.2, 1.4) gave -1.2e-07 instead of 1.4. The result then has the sign opposite to input, which fmod never returns. When that happens, add |other| back with the sign of input. Fixes pytorch#4860
This was referenced Oct 14, 2026
usmanmateen
added a commit
to usmanmateen/torchparity
that referenced
this pull request
Oct 14, 2026
## What this changes Adds the Torch-TensorRT float32 `fmod` report (issue pytorch/TensorRT#4860, pull request pytorch/TensorRT#4861) to `known_issues.toml`. The Torch-TensorRT `fmod` cell in RESULTS.md now links it, and the README lists it under the problems found with torchparity. Closes #14. ## How it was checked - [x] `pytest` passes (no code changes) - [x] `ruff check .` passes - [x] If results changed: regenerated with `torchparity report results/*.json -o RESULTS.md --readme README.md`
Contributor
Author
|
@lanluo-nvidia, could you take a look at this when you have a moment? Happy to adjust anything. |
Contributor
Author
|
@micwill755 @apbose could one of you take a look at this one when you have time? It's the fmod counterpart to #4850. |
Collaborator
|
LGTM |
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.
Description
torch.fmodon floating point tensors returned a tiny value with the wrong sign wheninput / otherrounded up to a whole number. In float32,fmod(4.2, 1.4)gave-1.19e-07instead of1.3999999, andfmod(x, 0.1)forx = 0.1, 0.2, ..., 10.0was wrong for 43 of the 100 values (#4860).impl.elementwise.fmodcomputesinput - trunc_div(input, other) * other. For floats,trunc_divrounds the division before flooring it, so a quotient just below a whole number (2.99999991 for4.2 / 1.4) comes out one too large, andinput - quotient * otherlands just past zero, with the sign opposite toinput. The exactfmodresult always has the sign ofinput(or is zero).fmodnow checks for that case on floating point inputs and adds|other|back with the sign ofinput, usingimpl.condition.where. Integer inputs return early, unchanged. Results that already have the right sign, zeros, NaN and inf are unchanged.remaindercallsfmod, and its results for these inputs were already right and stay the same.Fixes #4860
Type of change
Checklist:
Testing
Added
tests/py/dynamo/conversion/test_fmod_aten.py, withfmod.Tensorandremainder.Tensoron inputs whose float32 quotient rounds up (both signs ofinputandother),fmod.Scalarandremainder.Scalarwith1.4, andint32fmod.I ran the converter tests from this branch against the torch-tensorrt 2.14.0 wheel (PyTorch 2.14.1+cu130, TensorRT 11.1, RTX 5060 Ti), with
fmodinimpl/elementwise/ops.pypatched the same way.fmod,trunc_divandremainderare the same on main and in 2.14.0.fmod.Tensorandfmod.Scalarcases fail. With it,test_fmod_aten.pypasses (5 passed), and the reproducer in the issue matches PyTorch for all 100 values.remainderandfmodcases intest_remainder_aten.pyandtest_binary_ops_aten.pypass (15 passed), as dotest_floor_div_aten.py,test_div_aten.pyandtest_trunc_aten.py(25 passed).Note:
fmod(3.0, inf)gives NaN instead of 3.0, with and without this change, becausetrunc_divcomputes0 * inf. I left that for a separate fix.