Batch-transpose fusion into `FusedMatMul` sets `transBatchA` on inputs of different ranks, which ONNX Runtime can't compute
维护者通常 4 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 72/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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].
- 主要语言
- Python
- 星标
- 457
- 派生
- 149
- 平均合并
- 3 天 16 小时
- 30 天内合并 PR
- 24
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/onnxscript 的其他 Issue
-
`fuse_skip_rms_normalization` fuses a float16 residual into a norm whose scale is float32, which ONNX Runtime rejects可能已有人在做 @justinchuby 于 3 天前认领。 未关闭module: optimizer
难度 2/5 1-3 小时 新手友好度 76/100
microsoft/onnxscript#3155 · 3 条评论 · 1 个 reaction · 已指派 2 人 ·
维护者通常 4 天内回复
-
Constant folding uses onnx's plain reference ops, never onnx.reference.ops_optimized可能已有人在做 @todkarsant 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/onnxscript#3003 · 3 条评论 ·
维护者通常 4 天内回复
-
CI : Pin github actions to full commit SHA可能已有人在做 @Aniketsy 于 188 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
microsoft/onnxscript#2903 · 6 条评论 ·
维护者通常 4 天内回复
-
`SlicesSplit` rule makes `optimizer.optimize` quadratic in the number of `Slice` nodes可能已有人在做 @mayuriphad 于 2 天前认领。 未关闭module: optimizer
难度 4/5 3-5 天 新手友好度 68/100
microsoft/onnxscript#3154 ·
维护者通常 4 天内回复
-
optimize() crashes when constant folding produces a string tensor可能已有人在做 @nniiovoo 于 13 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 35/100
microsoft/onnxscript#3129 ·
维护者通常 4 天内回复
查看 microsoft/onnxscript 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 79/100
vllm-project/vllm-omni#10051 · 1 条评论 ·
维护者通常 1 天内回复
-
Bug Testing
难度 2/5 1-3 小时 新手友好度 78/100
SCIInstitute/SCIRun#2931 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 5 天内回复
-
author-page correction
难度 2/5 1-3 小时 新手友好度 64/100
acl-org/acl-anthology#10827 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 91/100
marimo-team/marimo#11768 ·
维护者通常 1 天内回复