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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed 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.
*/
#include "ir/Shape.h"
#include <cassert>
#include <functional>
#include <numeric>
#include <algorithm>
namespace onert
{
namespace ir
{
FeatureShape Shape::asFeature(Layout layout) const
{
assert(rank() == 4);
if (layout == Layout::NHWC)
{
// Feature Map in NHWC layout
// - Dimension(0) -> Batch
// - Dimension(1) -> Height
// - Dimension(2) -> Width
// - Dimension(3) -> Depth
const auto batch = dim(0);
const auto depth = dim(3);
const auto height = dim(1);
const auto width = dim(2);
return {batch, depth, height, width};
}
else if (layout == Layout::NCHW)
{
// Feature Map in NHWC layout
// - Dimension(0) -> Batch
// - Dimension(1) -> Depth
// - Dimension(2) -> Height
// - Dimension(3) -> Width
const auto batch = dim(0);
const auto depth = dim(1);
const auto height = dim(2);
const auto width = dim(3);
return {batch, depth, height, width};
}
else
{
throw std::runtime_error("Wrong Layout");
}
}
// Extended dimension is filled with 1.
void Shape::extendRank(int to_rank)
{
assert(to_rank - rank() >= 0);
_dimensions.insert(_dimensions.cbegin(), to_rank - rank(), 1);
}
uint64_t Shape::num_elements() const
{
// All of the nodes must have non-negative dimension
assert(std::all_of(_dimensions.begin(), _dimensions.end(),
[](const int32_t &v) { return (v >= 0); }));
return std::accumulate(_dimensions.cbegin(), _dimensions.cend(), UINT64_C(1),
std::multiplies<uint64_t>());
}
Shape permuteShape(const Shape &shape, Layout frontend_layout, Layout backend_layout)
{
assert(shape.rank() <= 4);
Shape backend_shape{shape};
if (shape.rank() == 4 && frontend_layout == Layout::NHWC && backend_layout == Layout::NCHW)
{
backend_shape.dim(1) = shape.dim(3);
backend_shape.dim(2) = shape.dim(1);
backend_shape.dim(3) = shape.dim(2);
}
else if (shape.rank() == 4 && frontend_layout == Layout::NCHW && backend_layout == Layout::NHWC)
{
backend_shape.dim(1) = shape.dim(2);
backend_shape.dim(2) = shape.dim(3);
backend_shape.dim(3) = shape.dim(1);
}
return backend_shape;
}
} // namespace ir
} // namespace onert
|