Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Portable remainder kernel: wrong sign for integers and float64 rounded to float

未关闭 适合新手
#23,682 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@usmanmateen 已经在做这个了。

开始于 2026年10月11日。

  • #23683 来自 @usmanmateen —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, python, pytorch

调研方向

阅读 kernels/portable/cpu/util/math_util.h 和现有的 op_remainder_test.cpp。根据 issue 中的符号和精度情况检查整数和浮点数重载,包括标量形式。测试覆盖这些情况且 portable kernels 与 PyTorch 的结果一致时,即为完成。

由索引模型根据 Issue 内容生成。

描述

🐛 Describe the bug

The portable remainder kernels give results that differ from PyTorch in two cases.

  1. Integers with mixed signs. ATen's remainder takes the sign of the divisor (-7 % 3 == 2), but for integer types utils::remainder_override in kernels/portable/cpu/util/math_util.h returns a % b, which takes the sign of the dividend (-1). The floating-point overload already adds b in this case, as the comment above both overloads describes. The integer one doesn't.
  2. float64 loses precision. The floating-point overload stores std::fmod(a, b) in a float, so double results are rounded to float precision.
import torch
from executorch.exir import to_edge
from executorch.runtime import Runtime


class Rem(torch.nn.Module):
    def forward(self, a, b):
        return torch.remainder(a, b)


def run(a, b):
    prog = to_edge(torch.export.export(Rem(), (a, b))).to_executorch()
    return Runtime.get().load_program(prog.buffer).load_method("forward").execute([a, b])[0]


a = torch.tensor([7, -7, 7, -7])
b = torch.tensor([3, 3, -3, -3])
print(torch.remainder(a, b).tolist(), run(a, b).tolist())
# [1, 2, -2, -1] [1, -1, 1, -1]

a = torch.tensor([123456789.123456789], dtype=torch.float64)
b = torch.tensor([1000.0], dtype=torch.float64)
print(torch.remainder(a, b).item(), run(a, b).item())
# 789.1234567910433 789.1234741210938

The integer case affects int8, int32 and int64, and remainder.Scalar too: torch.remainder(x, 3) gives -1 for x = -1 instead of 2. With the XNNPACK partitioner the result is the same, since these ops run on the portable kernel. A model that uses Python's % on integer tensors (for example to wrap positions or bucket ids) gets different values after export.

The existing op_remainder_test.cpp only covers 46 % 4. I have a fix with tests and will open a PR.

Versions

executorch 1.5.1 (math_util.h is the same on main at f2575be), torch 2.14.1+cpu, Python 3.12, Linux x86_64 (WSL2).

主要语言
Python
星标
5.1k
派生
1.2k
平均合并
2 天 9 小时
30 天内合并 PR
632

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pytorch/executorch 的其他 Issue

查看 pytorch/executorch 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。