[Bug][torchlib] aten_scatter_reduce(include_self=False) uses finite finfo.min/max instead of -inf/+inf, corrupting +/-inf inputs

オープン 初心者向け
#3,200 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch
領域
backend, testing

調査の方向性

onnxscript/function_libs/torch_lib/ops/core.pyの9169~9198行付近にあるaten_scatter_reduceを読み、既存のscatter reduceテストを確認してください。指定された浮動小数点データ型全体で、include_self=Falseと無限大のソース値を使い、amaxとaminを再現してください。エクスポートされたONNXの結果で期待される無限大が保持され、整数の動作も正しい状態になれば完了です。

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

説明

Describe the bug

When include_self=False and reduce="amax" or reduce="amin", aten_scatter_reduce in onnxscript/function_libs/torch_lib/ops/core.py (lines 9169–9198) pre-fills impacted target indices before calling op.ScatterElements(..., reduction="max" | "min").

Although the comment on line 9169 notes # fill a dummy value (inf or -inf), the implementation fills finite np.finfo(dtype).min / np.finfo(dtype).max (±3.4028235e+38 for float32, ±65504.0 for float16) and torch.finfo(torch.bfloat16).min / max (±3.3895314e+38 for bfloat16) instead of IEEE-754 -inf / +inf.

As a result, whenever src contains -inf (under reduce="amax") or +inf (under reduce="amin"), max(-3.4028235e38, -inf) / min(+3.4028235e38, +inf) silently produces a finite float instead of -inf / +inf across float16, bfloat16, float32, and float64.

Minimal Reproduction
import onnx.reference
import torch


class ScatterReduceMaxNoSelf(torch.nn.Module):
    def forward(self, x: torch.Tensor, index: torch.Tensor, src: torch.Tensor) -> torch.Tensor:
        return torch.scatter_reduce(x, 0, index, src, reduce="amax", include_self=False)


model = ScatterReduceMaxNoSelf()
x = torch.tensor([10.0, 20.0], dtype=torch.float32)
index = torch.tensor([0], dtype=torch.int64)
src = torch.tensor([-float("inf")], dtype=torch.float32)

eager_out = model(x, index, src)
prog = torch.onnx.export(model, (x, index, src), dynamo=True)
ref = onnx.reference.ReferenceEvaluator(prog.model_proto)
(onnx_out,) = ref.run(None, {"x": x.numpy(), "index": index.numpy(), "src": src.numpy()})

print("PyTorch eager:", eager_out)  # tensor([-inf, 20.])
print("ONNX exported:", onnx_out)   # [-3.4028235e+38  20.       ]
Root Cause

In onnxscript/function_libs/torch_lib/ops/core.py (aten_scatter_reduce):

  • reduce == "amax" uses np.finfo(np_dtype).min / torch.finfo(torch.bfloat16).min for floating-point dtypes instead of -float("inf").
  • reduce == "amin" uses np.finfo(np_dtype).max / torch.finfo(torch.bfloat16).max for floating-point dtypes instead of +float("inf").
    (Integer dtypes do not have ±inf, so np.iinfo(np_dtype).min / max remains the exact identity there.)
Environment
  • onnxscript: 0.7.2 and main (f51083c)
  • torch: 2.11.0+cpu
  • onnx: 1.23.2

(Note: I noticed #3094 is currently open touching aten_scatter_reduce for reduce="mean", so I avoided opening a competing PR to prevent merge conflicts—happy to submit a small fix with float/int dtype tests once #3094 lands if helpful.)

主要言語
Python
スター
456
フォーク
159
平均マージ
3日 5時間
マージ済み PR(30日)
23

環境構築

はじめの一歩

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

microsoft/onnxscript のほかの issue

microsoft/onnxscript の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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