Skip to content

Commit d625e2b

Browse files
committed
[Fix][Relax] Skip constant LHS in CombineParallelMatmul
1 parent e345029 commit d625e2b

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎src/relax/transform/combine_parallel_matmul.cc‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ std::vector<BranchInfo> GetBranchInfo(Function f) {
295295
if (!match) return;
296296

297297
auto matmul_call = match.value()[matmul_pat].as_or_throw<Call>();
298-
auto matmul_lhs = matmul_call->args[0].as_or_throw<Var>();
298+
const auto* matmul_lhs = matmul_call->args[0].as<VarNode>();
299+
if (matmul_lhs == nullptr) return;
299300

300301
std::optional<int> bias_dim = std::nullopt;
301302
std::optional<std::string> activation = std::nullopt;
@@ -310,7 +311,7 @@ std::vector<BranchInfo> GetBranchInfo(Function f) {
310311
}
311312
}
312313

313-
if (auto it = groups.find(matmul_lhs.get()); it != groups.end()) {
314+
if (auto it = groups.find(matmul_lhs); it != groups.end()) {
314315
// Create a new branch in the existing parallel matmul subtree, and
315316
// invalidate bias and activation information when needed.
316317
BranchInfo* branch = &it->second;
@@ -326,7 +327,7 @@ std::vector<BranchInfo> GetBranchInfo(Function f) {
326327
}
327328
} else {
328329
// Create a new subgraph with one matmul
329-
groups[matmul_lhs.get()] = {1, bias_dim, activation};
330+
groups[matmul_lhs] = {1, bias_dim, activation};
330331
}
331332
});
332333

‎tests/python/relax/test_transform_combine_parallel_matmul.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,5 +743,18 @@ def before(
743743
tvm_tirx.ir.assert_structural_equal(after, before)
744744

745745

746+
def test_skip_matmul_with_constant_lhs():
747+
@R.function(private=True)
748+
def before(x: R.Tensor((2, 2), "float32")) -> R.Tensor((2, 2), "float32"):
749+
with R.dataflow():
750+
out = R.matmul(R.const([[1.0, 2.0], [3.0, 4.0]], "float32"), x)
751+
R.output(out)
752+
return out
753+
754+
after = CombineParallelMatmul()(tvm_tirx.IRModule.from_expr(before))["main"]
755+
756+
tvm_tirx.ir.assert_structural_equal(after, before)
757+
758+
746759
if __name__ == "__main__":
747760
tvm_tirx.testing.main()

0 commit comments

Comments
 (0)