Skip to content

fix(converters): fmod returns the wrong sign when the float quotient rounds up - #4861

Merged
micwill755 merged 1 commit into
pytorch:mainfrom
usmanmateen:fix/fmod-quotient-rounding
Nov 25, 2026
Merged

micwill755 merged 1 commit into
pytorch:mainfrom
usmanmateen:fix/fmod-quotient-rounding

Conversation

@usmanmateen

Copy link
Copy Markdown
Contributor

Description

torch.fmod on floating point tensors returned a tiny value with the wrong sign when input / other rounded up to a whole number. In float32, fmod(4.2, 1.4) gave -1.19e-07 instead of 1.3999999, and fmod(x, 0.1) for x = 0.1, 0.2, ..., 10.0 was wrong for 43 of the 100 values (#4860).

impl.elementwise.fmod computes input - trunc_div(input, other) * other. For floats, trunc_div rounds the division before flooring it, so a quotient just below a whole number (2.99999991 for 4.2 / 1.4) comes out one too large, and input - quotient * other lands just past zero, with the sign opposite to input. The exact fmod result always has the sign of input (or is zero).

fmod now checks for that case on floating point inputs and adds |other| back with the sign of input, using impl.condition.where. Integer inputs return early, unchanged. Results that already have the right sign, zeros, NaN and inf are unchanged. remainder calls fmod, and its results for these inputs were already right and stay the same.

Fixes #4860

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

Testing

Added tests/py/dynamo/conversion/test_fmod_aten.py, with fmod.Tensor and remainder.Tensor on inputs whose float32 quotient rounds up (both signs of input and other), fmod.Scalar and remainder.Scalar with 1.4, and int32 fmod.

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 fmod in impl/elementwise/ops.py patched the same way. fmod, trunc_div and remainder are the same on main and in 2.14.0.

  • Without the change, the fmod.Tensor and fmod.Scalar cases fail. With it, test_fmod_aten.py passes (5 passed), and the reproducer in the issue matches PyTorch for all 100 values.
  • The remainder and fmod cases in test_remainder_aten.py and test_binary_ops_aten.py pass (15 passed), as do test_floor_div_aten.py, test_div_aten.py and test_trunc_aten.py (25 passed).
  • I compared NaN, ±inf, ±0 and zero-divisor inputs, and float16/bfloat16 multiples of 0.1, before and after the change: the outputs are the same.
  • black, isort, ruff, typos and the repo's mypy hook pass on the changed files.

Note: fmod(3.0, inf) gives NaN instead of 3.0, with and without this change, because trunc_div computes 0 * inf. I left that for a separate fix.

…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
@meta-cla meta-cla Bot added the cla signed label Oct 14, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Oct 14, 2026
@github-actions
github-actions Bot requested a review from apbose October 14, 2026 00:20
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`
@usmanmateen

Copy link
Copy Markdown
Contributor Author

@lanluo-nvidia, could you take a look at this when you have a moment? Happy to adjust anything.

@usmanmateen

Copy link
Copy Markdown
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.

@micwill755

Copy link
Copy Markdown
Collaborator

LGTM

@micwill755
micwill755 merged commit 2ab9cd1 into pytorch:main Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug] torch.fmod returns a tiny value with the wrong sign when the float quotient rounds up

2 participants