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

Đang mở Phù hợp với người mới
#3,200 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, pytorch
Lĩnh vực
backend, testing

Hướng nghiên cứu

Đọc aten_scatter_reduce trong onnxscript/function_libs/torch_lib/ops/core.py quanh các dòng 9169–9198, sau đó kiểm tra các bài kiểm thử hiện có cho scatter reduce. Tái hiện lỗi với include_self=False và các giá trị nguồn vô cực cho amax và amin trên các kiểu dữ liệu dấu phẩy động được liệt kê. Hoàn tất khi kết quả ONNX đã xuất giữ nguyên các giá trị vô cực mong đợi và hành vi với số nguyên vẫn chính xác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.)

Ngôn ngữ chính
Python
Star
456
Fork
159
Merge trung bình
3 ngày 5 giờ
Pull request đã merge (30 ngày)
23

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/onnxscript

Tất cả issue của microsoft/onnxscript

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.