🐛 [Bug] torch.fmod returns a tiny value with the wrong sign when the float quotient rounds up
维护者通常 3 天内回复
评估
调研方向
从 impl.elementwise.fmod 及其辅助函数 trunc_div(位于 py/torch_tensorrt/dynamo/conversion/impl/elementwise/ops.py)开始,检查在计算余数之前浮点商是如何向下取整的。在 CUDA 机器上运行 issue 中的复现脚本,并将 4.2 / 1.4 以及 0.1 回绕的情况与 torch.fmod 进行比较。完成标准是:浮点 fmod 的结果在符号和数值上与 PyTorch 一致,且整数行为保持不变。
由索引模型根据 Issue 内容生成。
描述
Bug Description
torch.fmod(a, b) on floating point tensors returns a tiny value with the wrong sign when a / b rounds up to a whole number. In float32, torch.fmod(4.2, 1.4) is 1.3999999 in PyTorch and -1.19e-07 in Torch-TensorRT. The result of fmod always has the sign of a, so a negative result for a positive a is never right. It shows up when wrapping values by a period: fmod(x, 0.1) for x = 0.1, 0.2, ..., 10.0 differs from PyTorch for 43 of the 100 values.
impl.elementwise.fmod in py/torch_tensorrt/dynamo/conversion/impl/elementwise/ops.py computes input - trunc_div(input, other) * other, and trunc_div takes FLOOR_DIV of |input| and |other|. For floats, the division is rounded before it's floored. In float32, 4.2 / 1.4 is 2.99999991, which rounds to 3.0, so the quotient comes out as 3 instead of 2 and input - 3 * other is just below zero. PyTorch computes fmod exactly and gets 2.
torch.remainder (%) gives the right result for these inputs.
To Reproduce
import torch
import torch_tensorrt
class Fmod(torch.nn.Module):
def forward(self, a, b):
return torch.fmod(a, b)
def trt(model, *args):
return torch_tensorrt.compile(model.eval().cuda(), ir="dynamo", inputs=list(args), min_block_size=1)(*args)
a = torch.tensor([4.2, -4.2, 0.7, 5.0], device="cuda")
b = torch.tensor([1.4, 1.4, 0.1, 3.0], device="cuda")
print(Fmod()(a, b).tolist())
print(trt(Fmod(), a, b).tolist())
# Wrapping multiples of 0.1 by 0.1
x = torch.arange(1, 101, device="cuda") * 0.1
period = torch.full_like(x, 0.1)
expected, got = Fmod()(x, period), trt(Fmod(), x, period)
print((got - expected).abs().gt(1e-3).sum().item(), "of", x.numel(), "differ")
Output:
[1.3999998569488525, -1.3999998569488525, 0.09999997913837433, 2.0]
[-1.1920928955078125e-07, 1.1920928955078125e-07, -2.2351741790771484e-08, 2.0]
43 of 100 differ
Expected behavior
The same results as PyTorch, which always have the sign of a.
Environment
- Torch-TensorRT Version: 2.14.0 (pip).
fmodandtrunc_divare the same on main. - PyTorch Version: 2.14.1+cu130
- CPU Architecture: x86_64
- OS: Ubuntu 26.04 (WSL2)
- How you installed PyTorch: pip
- Python version: 3.12
- CUDA version: 13.0 (TensorRT 11.1.0.106)
- GPU models and configuration: NVIDIA GeForce RTX 5060 Ti, driver 596.49
Additional context
I found this with torchparity, a tool I'm writing that runs PyTorch ops through converters and compares the results with eager PyTorch. I have a fix with tests and can open a PR. When the result has the sign opposite to input, it adds |other| back with the sign of input. Integer inputs are unchanged.
#4786 changes trunc_div for large integers and doesn't cover this.
- 主要语言
- Python
- 星标
- 3k
- 派生
- 421
- 平均合并
- 4 天 7 小时
- 30 天内合并 PR
- 62
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytorch/TensorRT 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 3 天内回复
-
Removing ptq.h未关闭Story: Build & Install & Packaging
难度 1/5 1 小时以内 新手友好度 65/100
维护者通常 3 天内回复
-
难度 4/5 3-5 天 新手友好度 22/100
维护者通常 3 天内回复
-
难度 4/5 3-5 天 新手友好度 18/100
维护者通常 3 天内回复
-
难度 3/5 半天 新手友好度 65/100
维护者通常 3 天内回复
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 65/100
StevenBlack/hosts#3299 ·
-
`garak --report` overwrites the input report when its name has no ".report"可能已有人在做 @0z5a 今天认领。 未关闭needs-triage
难度 2/5 半天 新手友好度 72/100
维护者通常 2 天内回复
-
plot_graph crashes when a mesh node has no m2m edge可能已有人在做 @tygvi 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
mllam/neural-lam#883 · 1 条评论 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
future-agi/future-agi#4092 · 1 条评论 ·
维护者通常 1 天内回复
-
transcript analytics: a text whose only protected spans the prose pass redacts is withheld whole未关闭
难度 1/5 1 小时以内 新手友好度 82/100
WingedGuardian/GENesis-AGI#4641 ·
维护者通常 1 天内回复