[Bug][Relax][ONNX] MaxPool with auto_pad=SAME_LOWER computes floor instead of ceil

Đang mở Phù hợp với người mới
#21,039 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 3 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
74/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

Hướng nghiên cứu

Bắt đầu bằng cách chạy tệp đính kèm repro.py, tệp này so sánh onnxruntime với bộ nhập ONNX của Relax cho auto_pad=SAME_LOWER. Sau đó tìm hàm trợ giúp Pool._impl_v1 trong frontend ONNX của Relax và so sánh cách nó suy ra kích thước đầu ra cho SAME_UPPER (ceil) so với SAME_LOWER (floor). Đường đi AveragePool với cùng thuộc tính đã trả về (1,1,6,6) và là tham chiếu hoạt động đúng. Hoàn thành khi repro in ra NOT REPRODUCED với kết quả (1,1,6,6) gần với onnxruntime, cùng với một kiểm thử hồi quy đặt cạnh các kiểm thử frontend pooling hiện có.

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

Mô tả

needs-triage type: bug
Bug Description

The ONNX MaxPool schema defines the output extent for both SAME_UPPER and SAME_LOWER as ceil(input / stride); only the side that receives the padding differs. Pool._impl_v1 uses ceil for SAME_UPPER but floor for SAME_LOWER.

Expected Behavior

For input (1,1,11,11), kernel 2, stride 2 and auto_pad=SAME_LOWER, the output shape must be (1,1,6,6) with the values returned by onnxruntime / the ONNX reference evaluator.

Actual Behavior

The importer builds a (1,1,5,5) pooling and the values differ as well. AveragePool with the same attributes returns (1,1,6,6) in TVM, onnxruntime and the ONNX reference evaluator, so TVM's own two pooling paths disagree. Note that onnx.reference's MaxPool also floors (5x5) but produces yet another set of values; only onnxruntime follows the spec's ceil.

Real-world Impact & Practical Relevance

SAME_LOWER appears in models exported from frameworks that pad on the lower side. The silent shape reduction breaks downstream Reshape/Concat/Add graphs that were valid before import.

Environment
  • Apache TVM: 0.27.0.post1 (PyPI wheel, published 2026-09-29); the buggy code path is unchanged on main at 3d89c4991c5a37492f470800231a5ea8f57a95c6 (2026-11-22, static inspection).
  • OS / Python: Ubuntu 22.04.5 / Python 3.10.12, LLVM CPU target.
  • onnx 1.13.1, onnxruntime 1.20.0, NumPy 1.26.4.
Steps to Reproduce

Save the following as repro.py and run python repro.py (exit status 1
means the bug is reproduced):

#!/usr/bin/env python3
"""BUG-0101 -- [Relax ONNX frontend] MaxPool with auto_pad=SAME_LOWER uses floor instead of ceil.

ONNX schema: for auto_pad=SAME_UPPER and SAME_LOWER the output extent is
ceil(input / stride); only the padding side differs.  For input 11, kernel 2,
stride 2 the spec output is 6.

Expected: TVM returns shape (1,1,6,6) equal to onnxruntime.
Actual  : TVM returns (1,1,5,5) with different values (Pool._impl_v1).

Exit status: 0 if TVM matches ONNX Runtime, 1 if the bug is reproduced.
Run: python repro.py [--target llvm]
"""
import argparse
import os

os.environ.update({k: "1" for k in ("OMP_NUM_THREADS", "MKL_NUM_THREADS",
                                    "OPENBLAS_NUM_THREADS", "TVM_NUM_THREADS")})
import numpy as np
import onnx
import onnxruntime as ort
import tvm
from onnx import TensorProto as T
from onnx import helper
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx


def vi(name, dt, shape):
    return helper.make_tensor_value_info(name, dt, shape)


def make_model(nodes, inputs, outputs, inits=(), opset=17):
    g = helper.make_graph(nodes, "g", inputs, outputs, initializer=list(inits))
    m = helper.make_model(g, opset_imports=[helper.make_opsetid("", opset)])
    m.ir_version = 8
    onnx.checker.check_model(m)
    return m


def run_ort(m, feeds):
    s = ort.InferenceSession(m.SerializeToString(), providers=["CPUExecutionProvider"])
    return s.run(None, feeds)


def run_tvm(m, feeds, target):
    mod = from_onnx(m, shape_dict={k: list(v.shape) for k, v in feeds.items()})
    mod = relax.transform.LegalizeOps()(mod)
    ex = tvm.compile(mod, target=target)
    vm = relax.VirtualMachine(ex, tvm.cpu())
    out = vm["main"](*feeds.values())
    if not isinstance(out, (list, tuple)):
        out = [out]
    return [o.numpy() if hasattr(o, "numpy") else o for o in out]


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--target", default="llvm")
    args = ap.parse_args()

    x = np.arange(121, dtype="float32").reshape(1, 1, 11, 11)
    node = helper.make_node("MaxPool", ["x"], ["y"], kernel_shape=[2, 2],
                            strides=[2, 2], auto_pad="SAME_LOWER", ceil_mode=0)
    m = make_model([node], [vi("x", T.FLOAT, [1, 1, 11, 11])],
                   [vi("y", T.FLOAT, [1, 1, 6, 6])])
    ref = run_ort(m, {"x": x})[0]
    got = run_tvm(m, {"x": x}, args.target)[0]

    print("input (1,1,11,11) kernel=2 stride=2 auto_pad=SAME_LOWER")
    print("ONNX Runtime:", ref.shape)
    print("TVM         :", got.shape)
    ok = ref.shape == got.shape
    if ok:
        print("max|diff| =", float(np.max(np.abs(ref - got))))
        ok = bool(np.allclose(ref, got, rtol=1e-6, atol=1e-6))
    print("NOT REPRODUCED (fixed?)" if ok else "BUG REPRODUCED")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())

Error Log / Traceback
input (1,1,11,11) kernel=2 stride=2 auto_pad=SAME_LOWER
ONNX Runtime: (1, 1, 6, 6)
TVM         : (1, 1, 5, 5)
BUG REPRODUCED
Prior Report Search

No earlier public issue matching the SAME_LOWER floor/ceil asymmetry was found. Note that onnx.reference also floors for MaxPool (while its AveragePool does ceil), so the strongest evidence is the schema text plus onnxruntime plus TVM's own AveragePool path. Search: MaxPool SAME_LOWER, MaxPool auto_pad.

repro.py

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

  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 apache/tvm

Tất cả issue của apache/tvm

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.