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

[BYOC] Add layout check and update shape check for cublas FP8 BYOC #16895

Merged
merged 1 commit into from
Apr 17, 2024
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
28 changes: 24 additions & 4 deletions python/tvm/relax/backend/contrib/cublas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from functools import reduce

import tvm
from tvm import DataType
from tvm.relax import transform
from tvm.relax.transform import PatternCheckContext

Expand Down Expand Up @@ -68,11 +69,30 @@ def _check_matmul(context: PatternCheckContext) -> bool:
# Rows number must be multiples of 4 for IGEMM
return False
elif lhs_dtype == "e4m3_float8" and rhs_dtype == "e4m3_float8":
# Matrix dimensions must be multiples of 16. This requirement is missing from the cuBLAS
# docs, but it was observed during testing.
if not isinstance(rhs_shape[-1], (tvm.tir.expr.IntImm, int)) or rhs_shape[-1] % 16 != 0:
matmul_rhs_var = matmul_call.args[1]
rhs_transposed = False
if matmul_rhs_var in context.matched_bindings:
matmul_rhs_call = context.matched_bindings[matmul_rhs_var]
assert (
isinstance(matmul_rhs_call, tvm.relax.Call)
and matmul_rhs_call.op.name == "relax.permute_dims"
)
Comment on lines +76 to +79
Copy link
Contributor

Choose a reason for hiding this comment

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

I am Ok, thank you! Just a nit question: do we need here assert for the case when rhs_call is something but not permute_dims? Just to leave rhs_transposed == False and return False in the next IF (without crash):

        if not rhs_transposed:
            # cuBLAS FP8 operations require rhs being transposed
            return False

Copy link
Member Author

Choose a reason for hiding this comment

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

if matmul_rhs_var in context.matched_bindings: this condition implies rhs is transposed (it's the only pattern that rhs is another binding being matched), so I added an assertion here, it won't crash if we have non-transposed rhs

rhs_transposed = True

if not rhs_transposed:
# cuBLAS FP8 operations require rhs being transposed
return False
if not isinstance(rhs_shape[-2], (tvm.tir.expr.IntImm, int)) or rhs_shape[-2] % 16 != 0:

# cuBLAS FP8 operations require all tensors being aligned to 16 bytes.
if (
not isinstance(rhs_shape[-1], (tvm.tir.expr.IntImm, int))
or rhs_shape[-1] % (16 // DataType(lhs_dtype).itemsize()) != 0
):
return False
if (
not isinstance(rhs_shape[-2], (tvm.tir.expr.IntImm, int))
or rhs_shape[-2] % (16 // DataType(out_dtype).itemsize()) != 0
):
return False

lhs_batches = reduce(operator.mul, lhs_shape[:-2], 1)
Expand Down
20 changes: 12 additions & 8 deletions tests/python/relax/test_codegen_cublas.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,21 @@ def test_matmul_fp8_offload(


@pytest.mark.parametrize(
"M, N, K, out_dtype, partition_done",
"M, N, K, out_dtype, transposed_y, partition_done",
[
(15, 64, 32, "float32", True),
(15, 64, 32, "e4m3_float8", True),
(15, 64, 32, "e5m2_float8", False),
(16, 32, 60, "float32", False),
(16, 30, 64, "float32", False),
(15, 64, 32, "float32", True, True),
(15, 64, 32, "e4m3_float8", True, True),
(15, 64, 32, "e5m2_float8", True, False),
(16, 32, 60, "float32", True, False),
(16, 30, 64, "float32", True, False),
(16, 8, 16, "float16", True, True),
(16, 16, 16, "float16", False, False),
],
)
def test_cublas_partition_fp8_matmul(M, N, K, out_dtype, partition_done):
mod = get_relax_matmul_module((M, K), (N, K), "e4m3_float8", out_dtype, transposed_y=True)
def test_cublas_partition_fp8_matmul(M, N, K, out_dtype, transposed_y, partition_done):
mod = get_relax_matmul_module(
(M, K), (N, K), "e4m3_float8", out_dtype, transposed_y=transposed_y
)
mod = partition_for_cublas(mod)
func_name = "relax_matmul_cublas" if partition_done else "R.matmul"
assert func_name in mod["main"].script()
Expand Down
Loading