Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ def create_convert_map(
"matmul.default": self._binary_op(
partial(relax.op.linear_algebra.matmul, out_dtype="float32"), operator.matmul
),
"mm.default": self._binary_op(
partial(relax.op.linear_algebra.matmul, out_dtype="float32"), operator.matmul
),
Comment on lines +437 to +439
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The implementation for mm.default is identical to matmul.default (lines 434-436) and bmm.default (lines 477-479). To improve maintainability and reduce code duplication, consider defining a common handler for these matrix multiplication operations and reusing it.

For example:

    matmul_handler = self._binary_op(
        partial(relax.op.linear_algebra.matmul, out_dtype="float32"), operator.matmul
    )
    # ...
    return {
        # ...
        "matmul.default": matmul_handler,
        "mm.default": matmul_handler,
        # ...
        "bmm.default": matmul_handler,
        # ...
    }

"max.other": self._binary_op(relax.op.maximum, max),
"min.other": self._binary_op(relax.op.minimum, min),
"max.default": self._unary_op(relax.op.max),
Expand Down
26 changes: 26 additions & 0 deletions tests/python/relax/test_frontend_from_exported_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -5914,6 +5914,32 @@ def main(
verify_model(Model(), example_args, {}, Expected)


def test_mm():
class MatrixMultiply(Module):
def forward(self, a, b):
return torch.mm(a, b)

example_args = (
torch.randn(2, 3, dtype=torch.float32),
torch.randn(3, 4, dtype=torch.float32),
)

@tvm.script.ir_module
class Expected:
@R.function
def main(
a: R.Tensor((2, 3), dtype="float32"),
b: R.Tensor((3, 4), dtype="float32"),
) -> R.Tuple(R.Tensor((2, 4), dtype="float32")):
with R.dataflow():
lv: R.Tensor((2, 4), dtype="float32") = R.matmul(a, b, out_dtype="float32")
gv: R.Tuple(R.Tensor((2, 4), dtype="float32")) = (lv,)
R.output(gv)
return gv

verify_model(MatrixMultiply(), example_args, {}, Expected)


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