Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TOPI][batch_matmul] Allow cblas batch_matmul implicit batch_size broadcast #8250

Merged
merged 2 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions python/tvm/topi/x86/batch_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ def batch_matmul_blas_common(cfg, x, y, out_shape, lib):
assert len(x.shape) == 3 and len(y.shape) == 3, "only support 3-dim batch_matmul"
XB, M, XK = get_const_tuple(x.shape)
YB, N, YK = get_const_tuple(y.shape)
assert XB == YB, "batch dimension doesn't match"
assert (XB == YB) or (YB == 1) or (XB == 1), "batch dimension doesn't match"
comaniac marked this conversation as resolved.
Show resolved Hide resolved
assert XK == YK, "shapes of x and y is inconsistent"
if out_shape is not None:
assert out_shape[0] == XB, "got invalid output shape"
assert out_shape[0] in (XB, YB), "got invalid output shape"
assert out_shape[1] == M, "got invalid output shape"
assert out_shape[2] == N, "got invalid output shape"
cfg.add_flop(XB * M * N * XK * 2)
Expand Down
47 changes: 26 additions & 21 deletions tests/python/contrib/test_cblas.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ def test_quantized_matmul_add():


def verify_batch_matmul(
batch, m, l, n, lib, transa=False, transb=False, iterative=False, dtype="float32"
batch_a, batch_b, m, l, n, lib, transa=False, transb=False, iterative=False, dtype="float32"
):
ashape = (batch, l, n) if transa else (batch, n, l)
bshape = (batch, m, l) if transb else (batch, l, m)
batch = batch_a
comaniac marked this conversation as resolved.
Show resolved Hide resolved
ashape = (batch_a, l, n) if transa else (batch_a, n, l)
bshape = (batch_b, m, l) if transb else (batch_b, l, m)
A = te.placeholder(ashape, name="A", dtype=dtype)
B = te.placeholder(bshape, name="B", dtype=dtype)
C = cblas.batch_matmul(A, B, transa, transb)
Expand Down Expand Up @@ -207,24 +208,28 @@ def verify(target="llvm"):


def test_batch_matmul():
verify_batch_matmul(16, 235, 128, 1024, cblas)
verify_batch_matmul(16, 235, 128, 1024, cblas, True, False)
verify_batch_matmul(16, 235, 128, 1024, cblas, False, True)
verify_batch_matmul(16, 235, 128, 1024, cblas, True, True)
verify_batch_matmul(16, 235, 128, 1024, mkl)
verify_batch_matmul(16, 235, 128, 1024, mkl, True, False)
verify_batch_matmul(16, 235, 128, 1024, mkl, False, True)
verify_batch_matmul(16, 235, 128, 1024, mkl, True, True)
verify_batch_matmul(1, 1, 16, 3, cblas)
verify_batch_matmul(1, 1, 16, 3, cblas, True, False)
verify_batch_matmul(1, 1, 16, 3, cblas, False, False)
verify_batch_matmul(1, 1, 16, 3, cblas, True, True)
verify_batch_matmul(1, 1, 16, 3, cblas, iterative=True)
verify_batch_matmul(1, 1, 16, 3, mkl)
verify_batch_matmul(1, 1, 16, 3, mkl, True, False)
verify_batch_matmul(1, 1, 16, 3, mkl, False, False)
verify_batch_matmul(1, 1, 16, 3, mkl, True, True)
verify_batch_matmul(1, 1, 16, 3, mkl, iterative=True)
verify_batch_matmul(16, 16, 235, 128, 1024, cblas)
verify_batch_matmul(16, 16, 235, 128, 1024, cblas, True, False)
verify_batch_matmul(16, 16, 235, 128, 1024, cblas, False, True)
verify_batch_matmul(16, 16, 235, 128, 1024, cblas, True, True)
verify_batch_matmul(16, 16, 235, 128, 1024, mkl)
verify_batch_matmul(16, 16, 235, 128, 1024, mkl, True, False)
verify_batch_matmul(16, 16, 235, 128, 1024, mkl, False, True)
verify_batch_matmul(16, 16, 235, 128, 1024, mkl, True, True)
verify_batch_matmul(16, 1, 235, 128, 1024, cblas)
verify_batch_matmul(16, 1, 235, 128, 1024, cblas, iterative=True)
verify_batch_matmul(16, 1, 235, 128, 1024, mkl)
verify_batch_matmul(16, 1, 235, 128, 1024, mkl, iterative=True)
verify_batch_matmul(1, 1, 1, 16, 3, cblas)
verify_batch_matmul(1, 1, 1, 16, 3, cblas, True, False)
verify_batch_matmul(1, 1, 1, 16, 3, cblas, False, False)
verify_batch_matmul(1, 1, 1, 16, 3, cblas, True, True)
verify_batch_matmul(1, 1, 1, 16, 3, cblas, iterative=True)
verify_batch_matmul(1, 1, 1, 16, 3, mkl)
verify_batch_matmul(1, 1, 1, 16, 3, mkl, True, False)
verify_batch_matmul(1, 1, 1, 16, 3, mkl, False, False)
verify_batch_matmul(1, 1, 1, 16, 3, mkl, True, True)
verify_batch_matmul(1, 1, 1, 16, 3, mkl, iterative=True)


if __name__ == "__main__":
Expand Down