-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
311 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -19,6 +19,8 @@ namespace infini | |
Concat, | ||
Div, | ||
Mul, | ||
Reshape, | ||
MatMul, | ||
Relu, | ||
Sub, | ||
Transpose, | ||
|
This file contains 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 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 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,60 @@ | ||
#pragma once | ||
#include "core/operator.h" | ||
|
||
namespace infini | ||
{ | ||
/** | ||
* @brief Matrix multiplication. | ||
* | ||
*/ | ||
class MatmulObj : public OperatorObj | ||
{ | ||
private: | ||
// InfiniTensor assumes a row-major tensor layout. `transA`=false means | ||
// default dims, true means A should be transposed before matmul. This is in | ||
// oppsite to the column-major BLAS. | ||
bool transA, transB; | ||
|
||
// Auxiliary attributes which are not a part of operator attributes. | ||
int m, n, k; | ||
|
||
public: | ||
/** | ||
* @brief Matmul operator with batch broadcast and tensor transpose | ||
* supports. Only one tensor with singe batch can be broadcasted due to the | ||
* BLAS interface restriction. Tranpose indicates whether the last two | ||
* dimensions should be transposed before Matmul and does not affect other | ||
* leading dimensions. | ||
* | ||
* Matmul show how operators are defined in InfiniTensor. The constructor of | ||
* an operator can create output tensors for the operator or not, which | ||
* depends on `graph`. | ||
* | ||
* @param graph The computation graph that this operator belongs to. | ||
* @param A The input tensor. | ||
* @param B The input tensor. | ||
* @param C C is the output of Matmul. If outputs are going to be created in | ||
* the constructor, C should be an empty Ref. | ||
* @param transA If matrix A should be transposed when computing. | ||
* @param transB If matrix B should be transposed when computing. | ||
*/ | ||
MatmulObj(GraphObj *graph, Tensor A, Tensor B, Tensor C, | ||
bool transA = false, bool transB = false); | ||
OP_CLONE(MatmulObj); | ||
|
||
std::string toString() const override; | ||
optional<vector<Shape>> inferShape(const TensorVec &inputs) override; | ||
|
||
int numInputs() const override { return inputs.size(); } | ||
int numOutputs() const override { return 1; } | ||
|
||
bool getTransA() const { return transA; } | ||
bool getTransB() const { return transB; } | ||
void setTransA(bool transA) { this->transA = transA; } | ||
void setTransB(bool transB) { this->transB = transB; } | ||
int getM() const { return m; } | ||
int getN() const { return n; } | ||
int getK() const { return k; } | ||
}; | ||
|
||
} // namespace infini |
This file contains 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 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 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,34 @@ | ||
#include "operators/matmul.h" | ||
#include "utils/operator_utils.h" | ||
#include <numeric> | ||
|
||
namespace infini | ||
{ | ||
|
||
MatmulObj::MatmulObj(GraphObj *graph, Tensor A, Tensor B, Tensor C, bool transA, | ||
bool transB) | ||
: OperatorObj(OpType::MatMul, TensorVec{A, B}, {C}), | ||
transA(transA), transB(transB) | ||
{ | ||
IT_ASSERT(checkValid(graph)); | ||
} | ||
|
||
string MatmulObj::toString() const | ||
{ | ||
std::ostringstream os; | ||
os << "Matmul([" << (transA ? "A^T" : "A") << "," << (transB ? "B^T" : "B]") | ||
<< ",A=" << inputs[0]->getGuid() | ||
<< ",B=" << inputs[1]->getGuid() << ",C=" << outputs[0]->getGuid() | ||
<< ",mnk=[" << m << "," << n << "," << k << "])"; | ||
return os.str(); | ||
} | ||
|
||
optional<vector<Shape>> MatmulObj::inferShape(const TensorVec &inputs) | ||
{ | ||
// =================================== 作业 =================================== | ||
// TODO:返回经过 matmul 操作后的 shape | ||
// REF: https://github.com/onnx/onnx/blob/main/docs/Operators.md#gemm | ||
// =================================== 作业 =================================== | ||
} | ||
|
||
} // namespace infini |
Oops, something went wrong.