forked from LearningInfiniTensor/TinyInfiniTensor
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtranspose.cc
More file actions
63 lines (56 loc) · 1.96 KB
/
transpose.cc
File metadata and controls
63 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "operators/transpose.h"
namespace infini
{
TransposeObj::TransposeObj(GraphObj *graph, Tensor input, Tensor output,
vector<int> permute)
: OperatorObj(OpType::Transpose, {input}, {output})
{
auto rank = input->getRank();
if (permute.empty())
{
for (size_t i = 0; i < rank; ++i)
{
transposePermute[i] = i;
}
}
else
{
IT_ASSERT(rank == permute.size());
transposePermute = std::move(permute);
}
IT_ASSERT(checkValid(graph));
}
optional<vector<Shape>> TransposeObj::inferShape(const TensorVec &inputs)
{
const auto A = inputs[0];
auto input_dim = A->getDims();
auto output_dim = input_dim;
int rank = A->getRank();
// =================================== 作业 ===================================
// TODO:修改 output_dim,返回正确的 transpose 后的 shape
// REF: https://onnx.ai/onnx/operators/onnx__Transpose.html#transpose-21
// =================================== 作业 ===================================
auto &perm = this->transposePermute;
if (perm.empty()) {//为空则不用转置
for (int i = 0; i < rank; ++i) {
output_dim[i] = input_dim[rank - 1 - i];
}
} else {
for (int i = 0; i < rank; ++i) {
output_dim[i] = input_dim[perm[i]];
}
}
// 4. 返回结果
return vector<Shape>{output_dim};
}
std::string TransposeObj::toString() const
{
std::ostringstream os;
os << type.toString() << "[" << getGuid() << "]";
os << "(";
os << vecToString(inputs[0]->getDims()) << ",";
os << "input=" << inputs[0]->getGuid() << ",";
os << "output=" << outputs[0]->getGuid() << ")";
return os.str();
}
}; // namespace infini