Batch-transpose fusion into `FusedMatMul` sets `transBatchA` on inputs of different ranks, which ONNX Runtime can't compute
Maintainer thường phản hồi trong vòng 4 ngày
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- 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
- Lĩnh vực
- backend, machine-learning
Hướng nghiên cứu
Start by finding the ORT fusion rules that fold Transpose into FusedMatMul as transBatchA or transBatchB, then trace how they inspect input shapes and ranks. Reproduce the reported case with rank 3 x and rank 2 w, and check that the fusion is skipped when the ranks differ. Done means the optimized graph runs in ONNX Runtime and matches the unfused result.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
The rules that fold a Transpose into FusedMatMul as transBatchA / transBatchB don't check the other input's rank. ONNX Runtime's FusedMatMul only batch-transposes when both inputs have the same rank, of at least 3, so the fused graph fails at runtime. A projection of a transposed activation, Transpose(x, [1, 0, 2]) @ w with x rank 3 and w rank 2, is enough.
Repro (onnxscript 0.7.2, onnxruntime 1.31.1):
"""onnxscript folds a batch `Transpose` into `FusedMatMul` (`transBatchA`) when the other input has a lower rank, which
ONNX Runtime can't compute.
`Div(MatMul(Transpose(x, perm=[1, 0, 2]), w), 2)` with `x` rank 3 and `w` rank 2 (a projection of a transposed
activation). onnxscript's ORT fusions turn it into `FusedMatMul(x, w, transBatchA=1, alpha=0.5)`.
"""
import numpy as np
import onnx_ir as ir
import onnxruntime as ort
from onnx import TensorProto, helper
from onnxscript.rewriter.ort_fusions import optimize_for_ort
x = helper.make_tensor_value_info("x", TensorProto.FLOAT, [4, 2, 8])
w = helper.make_tensor("w", TensorProto.FLOAT, [8, 16], np.random.default_rng(0).standard_normal((8, 16)).astype(np.float32).ravel())
two = helper.make_tensor("two", TensorProto.FLOAT, [], [2.0])
nodes = [
helper.make_node("Transpose", ["x"], ["xt"], perm=[1, 0, 2]),
helper.make_node("MatMul", ["xt", "w"], ["product"]),
helper.make_node("Div", ["product", "two"], ["y"]),
]
y = helper.make_tensor_value_info("y", TensorProto.FLOAT, [2, 4, 16])
proto = helper.make_model(helper.make_graph(nodes, "g", [x], [y], [w, two]), opset_imports=[helper.make_opsetid("", 20)], ir_version=10)
feeds = {"x": np.random.default_rng(1).standard_normal((4, 2, 8)).astype(np.float32)}
expected = ort.InferenceSession(proto.SerializeToString(), providers=["CPUExecutionProvider"]).run(None, feeds)[0]
model, _ = optimize_for_ort(ir.serde.deserialize_model(proto))
for node in model.graph.all_nodes():
print(node.op_type, {k: a.value for k, a in node.attributes.items()}, [str(v.shape) for v in node.inputs if v is not None])
try:
got = ort.InferenceSession(ir.to_proto(model).SerializeToString(), providers=["CPUExecutionProvider"]).run(None, feeds)[0]
print("fused graph max diff:", np.abs(got - expected).max())
except Exception as error:
print("ONNX Runtime:", str(error).split("Status Message: ")[-1].strip())
FusedMatMul {'alpha': 0.5, 'transBatchA': 1} ['[4,2,8]', '[8,16]']
ONNX Runtime: .../matmul_helper.h:73 ... left_num_dims > 2 && left_num_dims == right_num_dims was false. Two inputs should have same rank and rank >= 3 if transBatchA or transBatchB is true
The unfused graph runs and gives [2, 4, 16].
- Ngôn ngữ chính
- Python
- Star
- 457
- Fork
- 149
- Merge trung bình
- 3 ngày 20 giờ
- Pull request đã merge (30 ngày)
- 22
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
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 microsoft/onnxscript
-
`fuse_skip_rms_normalization` fuses a float16 residual into a norm whose scale is float32, which ONNX Runtime rejectsCó thể đã có người làm @aanelluri đã nhận 1 ngày trước. Đang mởmodule: optimizer
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
microsoft/onnxscript#3155 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 4 ngày
-
Constant folding uses onnx's plain reference ops, never onnx.reference.ops_optimizedCó 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
microsoft/onnxscript#3003 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 4 ngày
-
CI : Pin github actions to full commit SHACó thể đã có người làm @Aniketsy đã nhận 185 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
microsoft/onnxscript#2903 · 6 bình luận ·
Maintainer thường phản hồi trong vòng 4 ngày
-
module: optimizer
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 68/100
microsoft/onnxscript#3154 ·
Maintainer thường phản hồi trong vòng 4 ngày
-
optimize() crashes when constant folding produces a string tensorCó thể đã có người làm @nniiovoo đã nhận 10 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
microsoft/onnxscript#3129 ·
Maintainer thường phản hồi trong vòng 4 ngày
Tất cả issue của microsoft/onnxscript
Issue tương tự
-
follow-up needs-triage S3: minor
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
manaflow-ai/cmux#36856 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation good first issue help wanted
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Shell framework build fails at "copy the shader libraries": the iOS shader libraries are now one file b-<chain>.imgCó thể đã có người làm @xiaofeng-han đã nhận hôm nay. Đang mởbuild ios
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày