-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Unity][BYOC] Add cuBLAS backend #14291
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0683120
stub
masahi 91b8c0c
fixed build
masahi c493962
test stub
masahi 8e8fa35
basic gemm working
masahi 3ce49a6
transposed gemm work
masahi 9f48c2b
wip
masahi a834b0f
bias and epilogue work
masahi 5e7480e
support fp16 and transposed bias
masahi f9ce24e
support batched gemm
masahi bc0b2c5
clean up
masahi 021f6ae
access arguments properly
masahi 57db54f
expose ExtractArgIdx to python and use it in cutlass byoc
masahi 5238694
put matmul ir into common testing file
masahi 4019e05
updated for the latest rev
masahi 54ca782
pylint
masahi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """Pattern table for cuBLAS backend""" | ||
| import operator | ||
| from functools import reduce | ||
|
|
||
| import tvm | ||
| from tvm.relax import transform | ||
| from tvm.relax.transform import PatternCheckContext | ||
|
|
||
| from ..pattern_registry import get_patterns_with_prefix, register_patterns | ||
| from ..patterns import make_matmul_pattern | ||
|
|
||
|
|
||
| def _is_supported_dtype(lhs_dtype, rhs_dtype): | ||
| """Check if dtypes in the given workload are supported by cuBLAS BYOC.""" | ||
| return (lhs_dtype == "float16" and rhs_dtype == "float16") or ( | ||
| lhs_dtype == "float32" and rhs_dtype == "float32" | ||
| ) | ||
|
|
||
|
|
||
| def _check_matmul(context: PatternCheckContext) -> bool: | ||
| lhs = context.annotated_expr["lhs"] | ||
| rhs = context.annotated_expr["rhs"] | ||
|
|
||
| lhs_dtype = lhs.struct_info.dtype | ||
| rhs_dtype = rhs.struct_info.dtype | ||
| if not _is_supported_dtype(lhs_dtype, rhs_dtype): | ||
| return False | ||
|
|
||
| lhs_shape = lhs.struct_info.shape.values | ||
| rhs_shape = rhs.struct_info.shape.values | ||
|
|
||
| if not isinstance(lhs_shape[-1], (tvm.tir.expr.IntImm, int)): | ||
| # Reduction axis must be constant | ||
| return False | ||
|
|
||
| lhs_batches = reduce(operator.mul, lhs_shape[:-2], 1) | ||
| rhs_batches = reduce(operator.mul, rhs_shape[:-2], 1) | ||
|
|
||
| # cuBLASLt does not seem to support batched GEMM with one of matrices having | ||
| # one batch (with batch_stride 0). So for batched GEMM, the two batch counts | ||
| # must be equal. | ||
| return ( | ||
| (lhs_batches == 1 and rhs_batches == 1) | ||
| or isinstance(lhs_batches, tvm.tir.Var) | ||
| or isinstance(rhs_batches, tvm.tir.Var) | ||
| or (int(lhs_batches) == int(rhs_batches)) | ||
| ) | ||
|
|
||
|
|
||
| register_patterns( | ||
| [ | ||
| ( | ||
| "cublas.matmul", | ||
| *make_matmul_pattern( | ||
| with_bias=False, | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ( | ||
| "cublas.matmul_bias", | ||
| *make_matmul_pattern( | ||
| with_bias=True, | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ( | ||
| "cublas.matmul_bias_relu", | ||
| *make_matmul_pattern( | ||
| with_bias=True, | ||
| activation="relax.nn.relu", | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ( | ||
| "cublas.matmul_bias_gelu", | ||
| *make_matmul_pattern( | ||
| with_bias=True, | ||
| activation="relax.nn.gelu", | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ( | ||
| "cublas.matmul_transposed", | ||
| *make_matmul_pattern( | ||
| with_bias=False, | ||
| transposed_rhs=True, | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ( | ||
| "cublas.matmul_transposed_bias", | ||
| *make_matmul_pattern( | ||
| with_bias=True, | ||
| transposed_rhs=True, | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ( | ||
| "cublas.matmul_transposed_bias_relu", | ||
| *make_matmul_pattern( | ||
| with_bias=True, | ||
| activation="relax.nn.relu", | ||
| transposed_rhs=True, | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ( | ||
| "cublas.matmul_transposed_bias_gelu", | ||
| *make_matmul_pattern( | ||
| with_bias=True, | ||
| activation="relax.nn.gelu", | ||
| transposed_rhs=True, | ||
| ), | ||
| _check_matmul, | ||
| ), | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| def partition_for_cublas(mod): | ||
| """ | ||
| Partition the input module into cuBLAS-supported subgraphs. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| mod: tvm.IRModule | ||
| The IRModule to be partitioned. | ||
|
|
||
| Returns | ||
| ------- | ||
| mod: tvm.IRModule | ||
| The resulting IRModule, containing partitioned subgraphs to be | ||
| offloaded to the cuBLAS backend. | ||
| """ | ||
|
|
||
| patterns = get_patterns_with_prefix("cublas") | ||
| return transform.FuseOpsByPattern(patterns, bind_constants=False, annotate_codegen=True)(mod) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ | |
| from .nn import * | ||
| from .relay_translator import * | ||
| from .ast_printer import dump_ast | ||
| from .matmul import * | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Utilities to construct matmul workloads.""" | ||
| import tvm | ||
| from tvm.script import relax as R | ||
| from tvm.script.ir_builder import IRBuilder | ||
| from tvm.script.ir_builder import relax as relax_builder | ||
|
|
||
|
|
||
| def get_relax_matmul_module( | ||
| x_shape, | ||
| y_shape, | ||
| dtype, | ||
| transposed_y=False, | ||
| with_bias=False, | ||
| activation=None, | ||
| residual_bin_op=None, | ||
| residual_activation=None, | ||
| ): | ||
| """Create a matmul op followd by epilogue operations.""" | ||
| if transposed_y: | ||
| n = y_shape[-2] | ||
| else: | ||
| n = y_shape[-1] | ||
|
|
||
| with IRBuilder() as builder: | ||
| with relax_builder.function(): | ||
| R.func_name("main") | ||
| x = R.arg("x", R.Tensor(x_shape, dtype)) | ||
| y = R.arg("y", R.Tensor(y_shape, dtype)) | ||
| if with_bias: | ||
| bias = R.arg("bias", R.Tensor((n,), dtype)) | ||
|
|
||
| with R.dataflow() as frame: | ||
| if transposed_y: | ||
| axes = list(range(len(y_shape) - 2)) + [-1, -2] | ||
| y = R.emit(R.permute_dims(y, axes=axes)) | ||
| result = R.emit(R.matmul(x, y, out_dtype=dtype)) | ||
| if with_bias: | ||
| result = R.emit(result + bias) | ||
| if activation is not None: | ||
| result = R.emit(activation(result)) | ||
| if residual_bin_op is not None: | ||
| result = R.emit(residual_bin_op(result, x)) | ||
| if residual_activation is not None: | ||
| result = R.emit(residual_activation(result)) | ||
| R.output(result) | ||
|
|
||
| R.func_ret_value(frame.output_vars[0]) | ||
|
|
||
| func = builder.get() | ||
| return tvm.IRModule({"main": func}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @yelite this has been ported to cpp