forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 7
[BYOC] Enable mcBlas/mcDnn #14
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
Open
JoieAli
wants to merge
4
commits into
MetaX-MACA:v0.18.0
Choose a base branch
from
JoieAli:v0.18.0
base: v0.18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,88 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /*! | ||
| * \brief External function interface to mcBLAS libraries | ||
| * \file mcblas.h | ||
| */ | ||
| #ifndef TVM_TOPI_CONTRIB_MCBLAS_H_ | ||
| #define TVM_TOPI_CONTRIB_MCBLAS_H_ | ||
|
|
||
| #include <tvm/te/operation.h> | ||
| #include <tvm/topi/detail/extern.h> | ||
|
|
||
| namespace tvm { | ||
| namespace topi { | ||
| namespace contrib { | ||
|
|
||
| using namespace tvm::te; | ||
| using namespace topi::detail; | ||
| /*! | ||
| * \brief Create an op that multiplies lhs and rhs with mcBLAS | ||
| * | ||
| * \param lhs The left matrix operand | ||
| * \param rhs The right matrix operand | ||
| * \param transa Whether to transpose lhs | ||
| * \param transb Whether to transpose rhs | ||
| * | ||
| * \return The output tensor | ||
| */ | ||
| inline Tensor mcblas_matmul(const Tensor& lhs, const Tensor& rhs, bool transa, bool transb) { | ||
| auto n = transa ? lhs->shape[1] : lhs->shape[0]; | ||
| auto m = transb ? rhs->shape[0] : rhs->shape[1]; | ||
|
|
||
| return make_extern( | ||
| {{n, m}}, {lhs->dtype}, {lhs, rhs}, | ||
| [&](Array<Buffer> ins, Array<Buffer> outs) { | ||
| return call_packed({StringImm("tvm.contrib.mcblas.matmul"), pack_buffer(ins[0]), | ||
| pack_buffer(ins[1]), pack_buffer(outs[0]), transa, transb}); | ||
| }, | ||
| "C", "", {})[0]; | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Create an op that multiplies batch matrices | ||
| * lhs and rhs with mcBLAS | ||
| * | ||
| * \param lhs The left matrix operand | ||
| * \param rhs The right matrix operand | ||
| * \param transa Whether to transpose lhs | ||
| * \param transb Whether to transpose rhs | ||
| * | ||
| * \return The output tensor | ||
| */ | ||
| inline Tensor mcblas_batch_matmul(const Tensor& lhs, const Tensor& rhs, bool transa, bool transb) { | ||
| auto b = lhs->shape[0]; | ||
| auto n = transa ? lhs->shape[2] : lhs->shape[1]; | ||
| auto m = transb ? rhs->shape[1] : rhs->shape[2]; | ||
|
|
||
| return make_extern( | ||
| {{b, n, m}}, {lhs->dtype}, {lhs, rhs}, | ||
| [&](Array<Buffer> ins, Array<Buffer> outs) { | ||
| return call_packed({StringImm("tvm.contrib.mcblas.batch_matmul"), pack_buffer(ins[0]), | ||
| pack_buffer(ins[1]), pack_buffer(outs[0]), transa, transb}); | ||
| }, | ||
| "C", "", {})[0]; | ||
| } | ||
|
|
||
| } // namespace contrib | ||
| } // namespace topi | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_TOPI_CONTRIB_MCBLAS_H_ |
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,99 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file maca/dense.h | ||
| * \brief MACA schedule for dense operation | ||
| */ | ||
| #ifndef TVM_TOPI_MACA_DENSE_H_ | ||
| #define TVM_TOPI_MACA_DENSE_H_ | ||
|
|
||
| #include <tvm/target/generic_func.h> | ||
| #include <tvm/te/operation.h> | ||
| #include <tvm/te/schedule_pass.h> | ||
| #include <tvm/topi/contrib/mcblas.h> | ||
| #include <tvm/topi/cuda/dense.h> | ||
| #include <tvm/topi/detail/array_utils.h> | ||
| #include <tvm/topi/generic/extern.h> | ||
| #include <tvm/topi/nn/dense.h> | ||
| #include <tvm/topi/tags.h> | ||
|
|
||
| namespace tvm { | ||
| namespace topi { | ||
|
|
||
| using namespace tvm::te; | ||
|
|
||
| namespace maca { | ||
| /*! | ||
| * \brief Implementation of dense for MACA backend | ||
| * | ||
| * \param target The target device | ||
| * \param data Tensor with shape [batch, in_dim] | ||
| * \param weight Tensor with shape [out_dim, in_dim] | ||
| * \param bias Tensor with shape [out_dim]. Optional; to omit bias, pass Tensor() | ||
| * \param out_dtype Output data type. Used for mixed precision. | ||
| * | ||
| * \return Tensor with shape [batch, out_dim] | ||
| */ | ||
| inline tvm::te::Tensor dense_maca(const Target& target, const tvm::te::Tensor& data, | ||
| const tvm::te::Tensor& weight, const tvm::te::Tensor& bias, | ||
| const DataType& out_dtype) { | ||
| ICHECK_EQ(data->shape.size(), 2) << "dense requires 2-D data"; | ||
| ICHECK_EQ(weight->shape.size(), 2) << "dense requires 2-D weight"; | ||
| if (bias.defined()) { | ||
| ICHECK_EQ(bias->shape.size(), 1) << "dense requires 1-D bias"; | ||
| } | ||
|
|
||
| auto batch = data->shape[0]; | ||
| auto in_dim = data->shape[1]; | ||
| auto out_dim = weight->shape[0]; | ||
|
|
||
| if (target->GetLibs().count("mcblas")) { | ||
| ICHECK_EQ(data->dtype, out_dtype) << "Mixed precision not supported."; | ||
| auto mm = topi::contrib::mcblas_matmul(data, weight, false, true); | ||
| if (bias.defined()) { | ||
| mm = tvm::te::compute( | ||
| {batch, out_dim}, [&](Var i, Var j) { return mm(i, j) + bias(j); }, "tensor", kBroadcast); | ||
| } | ||
|
|
||
| return mm; | ||
| } else { | ||
| return topi::nn::dense(data, weight, bias, out_dtype); | ||
| } | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Create a MACA schedule for dense | ||
| * | ||
| * \param target The target to generate a schedule for. | ||
| * \param outs The output tensors. | ||
| * | ||
| * \return A schedule for the given ops. | ||
| */ | ||
| inline Schedule schedule_dense(const Target& target, const Array<Tensor>& outs) { | ||
| if (target->kind->name == "maca" && target->GetLibs().count("mcblas")) { | ||
| return topi::generic::schedule_extern(target, outs); | ||
| } | ||
| return topi::cuda::schedule_dense(target, outs); | ||
| } | ||
|
|
||
| } // namespace maca | ||
| } // namespace topi | ||
| } // namespace tvm | ||
| #endif // TVM_TOPI_MACA_DENSE_H_ |
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,86 @@ | ||
| # 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. | ||
| """External function interface to mcBLAS libraries.""" | ||
| import tvm | ||
| from tvm import te | ||
|
|
||
|
|
||
| def matmul(lhs, rhs, transa=False, transb=False, dtype=None): | ||
| """Create an extern op that compute matrix mult of A and rhs with mcBLAS | ||
|
|
||
| Parameters | ||
| ---------- | ||
| lhs : Tensor | ||
| The left matrix operand | ||
| rhs : Tensor | ||
| The right matrix operand | ||
| transa : bool | ||
| Whether transpose lhs | ||
| transb : bool | ||
| Whether transpose rhs | ||
|
|
||
| Returns | ||
| ------- | ||
| C : Tensor | ||
| The result tensor. | ||
| """ | ||
| n = lhs.shape[1] if transa else lhs.shape[0] | ||
| m = rhs.shape[0] if transb else rhs.shape[1] | ||
| dtype = dtype if dtype is not None else lhs.dtype | ||
| return te.extern( | ||
| (n, m), | ||
| [lhs, rhs], | ||
| lambda ins, outs: tvm.tir.call_packed( | ||
| "tvm.contrib.mcblas.matmul", ins[0], ins[1], outs[0], transa, transb | ||
| ), | ||
| dtype=dtype, | ||
| name="matmul_mcblas", | ||
| ) | ||
|
|
||
|
|
||
| def batch_matmul(lhs, rhs, transa=False, transb=False, dtype=None): | ||
| """Create an extern op that compute batch matrix mult of A and rhs with mcBLAS | ||
|
|
||
| Parameters | ||
| ---------- | ||
| lhs : Tensor | ||
| The left matrix operand | ||
| rhs : Tensor | ||
| The right matrix operand | ||
| transa : bool | ||
| Whether transpose lhs | ||
| transb : bool | ||
| Whether transpose rhs | ||
|
|
||
| Returns | ||
| ------- | ||
| C : Tensor | ||
| The result tensor. | ||
| """ | ||
| b = lhs.shape[0] | ||
| n = lhs.shape[2] if transa else lhs.shape[1] | ||
| m = rhs.shape[1] if transb else rhs.shape[2] | ||
| dtype = dtype if dtype is not None else lhs.dtype | ||
| return te.extern( | ||
| (b, n, m), | ||
| [lhs, rhs], | ||
| lambda ins, outs: tvm.tir.call_packed( | ||
| "tvm.contrib.mcblas.batch_matmul", ins[0], ins[1], outs[0], transa, transb | ||
| ), | ||
| dtype=dtype, | ||
| name="batch_matmul_mcblas", | ||
| ) |
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,54 @@ | ||
| # 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. | ||
| """External function interface to mcBLASlt libraries.""" | ||
| import tvm | ||
| from tvm import te | ||
|
|
||
|
|
||
| def matmul(lhs, rhs, transa=False, transb=False, n=0, m=0, dtype=None): | ||
| """Create an extern op that compute matrix mult of A and rhs with mcBLAS | ||
|
|
||
| Parameters | ||
| ---------- | ||
| lhs : Tensor | ||
| The left matrix operand | ||
| rhs : Tensor | ||
| The right matrix operand | ||
| transa : bool | ||
| Whether transpose lhs | ||
| transb : bool | ||
| Whether transpose rhs | ||
|
|
||
| Returns | ||
| ------- | ||
| C : Tensor | ||
| The result tensor. | ||
| """ | ||
| if n == 0: | ||
| n = lhs.shape[1] if transa else lhs.shape[0] | ||
| if m == 0: | ||
| m = rhs.shape[0] if transb else rhs.shape[1] | ||
| dtype = dtype if dtype is not None else lhs.dtype | ||
| return te.extern( | ||
| (n, m), | ||
| [lhs, rhs], | ||
| lambda ins, outs: tvm.tir.call_packed( | ||
| "tvm.contrib.mcblaslt.matmul", ins[0], ins[1], outs[0], transa, transb | ||
| ), | ||
| dtype=dtype, | ||
| name="C", | ||
| ) | ||
Oops, something went wrong.
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.
There are a couple of minor issues here for better clarity and maintainability:
namefor thete.externcall on line 53 is "C", which is very generic. It would be better to use a more descriptive name like "matmul_mcblaslt" to avoid potential name clashes and improve readability.