Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Portable floor_divide returns non-whole numbers for floats and wrong values for a zero divisor

オープン
#23,684 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@JakeStevens がすでに取り組んでいます。

2026年10月12日 から。

  • #23685 @usmanmateen による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
cpp, python, pytorch
領域
backend, testing

調査の方向性

Start with kernels/portable/cpu/util/math_util.h and inspect utils::floor_divide; the issue says the same helper is used by portable div and by Cadence HiFi and Fusion G3 div operators. Compare its floating-point behavior with ATen's div_floor_floating, focusing on rounding errors and zero divisors, then add tests for the reported cases. Done means floating-point results match eager behavior for those cases while integer behavior remains unaffected.

索引モデルが issue の本文から書いたものです。

説明

bug module: kernels
🐛 Describe the bug

For floating-point inputs, the portable floor_divide kernel often returns a value that is not a whole number, and it returns the wrong value when dividing by zero. div.Tensor_mode / div.Scalar_mode with rounding_mode="floor" use the same helper (utils::floor_divide in kernels/portable/cpu/util/math_util.h), so they behave the same way.

  1. Results that aren't whole numbers. The helper returns (a - fmod(a, b)) / b, minus 1 if the signs differ. That quotient can be off from a whole number by a rounding error. ATen's div_floor_floating then floors it and rounds up if the remainder is above 0.5; the portable helper skips that step. With random inputs (a in [-10, 10], b in [0.01, 2.01]), about 7% of float64 results and 6% of float32 results differ from eager. When the result is converted to an integer, as is common for bucket or index computations, it comes out one too small for positive results (2.9999999999999996 → 2) and one too large for negative ones (-3.9999999999999996 → -3).
  2. Division by zero. For b == 0 the helper returns ±inf based on the sign of a. ATen returns a / b, so 0.0 // 0.0 is nan instead of inf, nan // 0.0 is nan instead of inf, and a -0.0 divisor flips the sign (1.0 // -0.0 is -inf).
import torch
from executorch.exir import to_edge
from executorch.runtime import Runtime


class FloorDiv(torch.nn.Module):
    def forward(self, a, b):
        return torch.div(a, b, rounding_mode="floor")


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


a = torch.tensor([4.2, 0.7, -4.2], dtype=torch.float64)
b = torch.tensor([1.4, 0.1, 1.4], dtype=torch.float64)
print(torch.div(a, b, rounding_mode="floor").tolist(), run(a, b).tolist())
# [3.0, 6.0, -4.0] [2.9999999999999996, 6.000000000000001, -3.9999999999999996]
print(torch.div(a, b, rounding_mode="floor").long().tolist(), run(a, b).long().tolist())
# [3, 6, -4] [2, 6, -3]

a = torch.tensor([-6.4278764724731445])
b = torch.tensor([1.870701551437378])
print(torch.floor_divide(a, b).item(), run(a, b).item())
# -4.0 -3.999999761581421

a = torch.tensor([0.0, float("nan"), 1.0])
b = torch.tensor([0.0, 0.0, -0.0])
print(torch.floor_divide(a, b).tolist(), run(a, b).tolist())
# [nan, nan, -inf] [inf, inf, inf]

Integer inputs are not affected. utils::floor_divide is also used by the Cadence HiFi and Fusion G3 div operators. 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).

cc @larryliu0820 @manuelcandales @JakeStevens

主要言語
Python
スター
5.1k
フォーク
1.2k
平均マージ
2日 8時間
マージ済み PR(30日)
610

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pytorch/executorch のほかの issue

pytorch/executorch の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。