[Bug][Relax] ReduceMin(+inf) returns FLT_MAX, and ReduceSum is wrong for long reductions (0.27.0.post1)
Maintainer thường phản hồi trong vòng 3 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- compilers, machine-learning, performance
Hướng nghiên cứu
Start by tracing the ONNX Relax frontend path for ReduceMin, ReduceMax, and ReduceSum, then follow how LLVM lowering implements their accumulators and long-axis reductions. Reproduce the two cases with the supplied standalone snippets on LLVM; done means infinities reduce to the expected values and long float32 sums agree within ordinary rounding.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Expected behavior
After tvm.relax.frontend.onnx.from_onnx and relax.build(..., target="llvm"):
ReduceMinover a tensor whose values are all+infshould return+inf, matching IEEE-754 and PyTorchtorch.amin.ReduceMaxover a tensor whose values are all-infshould return-inf, matchingtorch.amax.ReduceSumof a long vector of finitefloat32values should match afloat32(and, for exact integer-valued inputs such as all5.0with a representable sum, afloat64) reduction, up to ordinary rounding.
A plausible cause of class 1 is initializing the min (resp. max) accumulator with FLT_MAX (resp. -FLT_MAX) rather than +inf / -inf. Then min(FLT_MAX, +inf) stays FLT_MAX.
Actual behavior
On TVM 0.27.0.post1 (LLVM Relax VM):
1. ReduceMin of +inf returns FLT_MAX; ReduceMax of -inf returns -FLT_MAX
ReduceMin([inf, inf])
torch: inf
tvm: 3.4028234663852886e+38
ReduceMax([-inf, -inf])
torch: -inf
tvm: -3.4028234663852886e+38
Mixed and opposite-signed cases are already correct:
ReduceMin([1.0, inf]) torch=1.0 tvm=1.0
ReduceMax([-1.0, -inf]) torch=-1.0 tvm=-1.0
ReduceMin([-inf, -inf]) torch=-inf tvm=-inf
ReduceMax([inf, inf]) torch=inf tvm=inf
Exp itself overflows to +inf on both sides (Exp(89) and above). The mismatch appears when a reduction of those infinities is taken (for example ReduceSum + Exp + ReduceMin).
ReduceSum / ReduceMean / ReduceProd of ±inf already match PyTorch in the cases tried.
2. ReduceSum is inaccurate once the reduced axis is long
All-5.0 vectors (the sum is an exact float32 / float64 integer). n=2**21 and n=3_000_000 match; n=2**22 and n=2**24 do not:
n=2097152 fill=5.0 torch.float32=10485760.0 torch.float64=10485760.0 tvm=10485760.0 tvm-torch=0.0
n=3000000 fill=5.0 torch.float32=15000000.0 torch.float64=15000000.0 tvm=15000000.0 tvm-torch=0.0
n=4194304 fill=5.0 torch.float32=20971520.0 torch.float64=20971520.0 tvm=20132660.0 tvm-torch=-838860.0
n=16777216 fill=5.0 torch.float32=83886080.0 torch.float64=83886080.0 tvm=73819752.0 tvm-torch=-10066328.0
The first failing length found in a size scan was between 3,000,000 and 3,500,000. The error is far larger than ordinary float32 rounding (the all-5.0 sums above are exact in both float32 and float64 on PyTorch).
Environment
- OS: Ubuntu 22.04.5 LTS (x86_64), glibc 2.35
- Python: 3.11.17
- TVM: 0.27.0.post1 (PyPI wheel /
apache-tvm) - Target:
llvm, Relax VM - ONNX: 1.23.2
- PyTorch: 2.14.0+cpu (numeric oracle only)
Steps to reproduce
Each snippet is standalone. It builds a tiny ONNX opset-18 model and runs it through from_onnx + relax.build(..., target="llvm").
import numpy as np
import torch
from onnx import helper, numpy_helper, TensorProto
import tvm
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx
def make_reduce(op, in_shape, out_shape):
X = helper.make_tensor_value_info("X", TensorProto.FLOAT, list(in_shape))
Y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, list(out_shape))
axes = numpy_helper.from_array(np.array([0], dtype=np.int64), "axes")
node = helper.make_node(op, ["X", "axes"], ["Y"], keepdims=0)
g = helper.make_graph([node], "g", [X], [Y], initializer=[axes])
return helper.make_model(g, opset_imports=[helper.make_opsetid("", 18)], ir_version=10)
def run_tvm(model, x):
mod = from_onnx(
model,
shape_dict={"X": list(x.shape)},
dtype_dict={"X": "float32"},
sanitize_input_names=False,
)
try:
from tvm.relax.frontend import detach_params
mod, params = detach_params(mod)
params = params or None
except Exception:
params = None
ex = relax.build(mod, target="llvm", params=params)
vm = relax.VirtualMachine(ex, tvm.cpu())
out = vm["main"](tvm.runtime.tensor(x.contiguous().numpy(), tvm.cpu()))
arr = out.numpy() if hasattr(out, "numpy") else out[0].numpy()
return torch.from_numpy(np.array(arr).copy())
# --- Class 1 ---
x = torch.tensor([float("inf"), float("inf")], dtype=torch.float32)
print("ReduceMin +inf torch", float(torch.amin(x, 0)),
"tvm", float(run_tvm(make_reduce("ReduceMin", [2], []), x)))
x = torch.tensor([float("-inf"), float("-inf")], dtype=torch.float32)
print("ReduceMax -inf torch", float(torch.amax(x, 0)),
"tvm", float(run_tvm(make_reduce("ReduceMax", [2], []), x)))
# --- Class 2 ---
for n in (2**21, 2**22, 2**24):
x = torch.full((n,), 5.0, dtype=torch.float32)
tvm_v = float(run_tvm(make_reduce("ReduceSum", [n], []), x))
print(f"n={n} torch={float(x.sum())} tvm={tvm_v} diff={tvm_v-float(x.sum())}")
- Ngôn ngữ chính
- Python
- Star
- 13.9k
- Fork
- 4.1k
- Merge trung bình
- 4 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 22
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/tvm
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is NoneCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 3 ngày
-
[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensorsĐang mởneeds-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 3 ngày
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradientsCó thể đã có người làm @pierceluu10 đã nhận 4 ngày trước. Đang mởneeds-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 3 ngày
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputsCó thể đã có người làm @tandede đã nhận 9 ngày trước. Đang mởneeds-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 3 ngày
-
needs-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
apache/tvm#21043 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
Issue tương tự
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 78/100
web-push-libs/pywebpush#216 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
AgriciDaniel/claude-seo#572 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
area:print-dispatch
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 92/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area: docs domain: technical-writing good first issue type: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
Deepak3699/Ai_Mentor#919 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày