Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/relax/transform/combine_parallel_matmul.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ ffi::TypedFunction<ffi::Map<Var, Expr>(ffi::Map<DFPattern, Var>, ffi::Map<Var, E
ffi::Map<Var, Expr> replacements;

for (const auto& [rhs_dim, indices] : GroupShapes(rhs_shapes)) {
if (indices.size() == 1 || !batch_dims_compatible(rhs_dim, indices, rhs_shapes)) continue;
if (rhs_dim < 2 || indices.size() == 1 ||
!batch_dims_compatible(rhs_dim, indices, rhs_shapes)) {
continue;
}

auto lhs = matchings[patterns.input];
int lhs_dim = GetTensorType(lhs)->ndim;
if (lhs_dim < 2) continue;

const auto& patterns_to_replace = [&patterns, &branch_info]() {
if (branch_info.activation) return patterns.activation;
Expand Down Expand Up @@ -243,7 +248,6 @@ ffi::TypedFunction<ffi::Map<Var, Expr>(ffi::Map<DFPattern, Var>, ffi::Map<Var, E
sections.push_back(IntImm::Int64(split_index));
}

int lhs_dim = GetTensorType(lhs)->ndim;
int split_axis = std::max<int>(lhs_dim, rhs_dim) - 1;
auto chunks = split(matmul_combined, sections, split_axis);

Expand Down
38 changes: 38 additions & 0 deletions tests/python/relax/test_transform_combine_parallel_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,5 +733,43 @@ def before(
tvm.ir.assert_structural_equal(after, before)


def test_skip_vector_lhs():
@R.function(private=True)
def before(
x: R.Tensor((16,), "float32"),
w0: R.Tensor((16, 32), "float32"),
w1: R.Tensor((16, 32), "float32"),
):
with R.dataflow():
y0 = R.matmul(x, w0)
y1 = R.matmul(x, w1)
out = (y0, y1)
R.output(out)
return out

after = CombineParallelMatmul()(tvm.IRModule.from_expr(before))["main"]

tvm.ir.assert_structural_equal(after, before)


def test_skip_vector_rhs():
@R.function(private=True)
def before(
x: R.Tensor((8, 16), "float32"),
w0: R.Tensor((16,), "float32"),
w1: R.Tensor((16,), "float32"),
):
with R.dataflow():
y0 = R.matmul(x, w0)
y1 = R.matmul(x, w1)
out = (y0, y1)
R.output(out)
return out

after = CombineParallelMatmul()(tvm.IRModule.from_expr(before))["main"]

tvm.ir.assert_structural_equal(after, before)


if __name__ == "__main__":
tvm.testing.main()
Loading