forked from OpenPPL/ppl.nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor_shape.h
287 lines (263 loc) · 8.98 KB
/
tensor_shape.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// 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.
#ifndef _ST_HPC_PPL_NN_COMMON_TENSOR_SHAPE_H_
#define _ST_HPC_PPL_NN_COMMON_TENSOR_SHAPE_H_
#include "ppl/common/types.h"
#include "ppl/nn/common/common.h"
#include <vector>
namespace ppl { namespace nn {
class PPLNN_PUBLIC TensorShape final {
private:
static const uint32_t kAxisC = 1;
private:
bool is_scalar_;
ppl::common::datatype_t data_type_;
ppl::common::dataformat_t data_format_;
std::vector<int64_t> dims_;
std::vector<uint16_t> padding0_;
std::vector<uint16_t> padding1_;
private:
static int16_t CalcPadding(int64_t dim, uint32_t alignment) {
return static_cast<int16_t>((((uintptr_t)dim + (uintptr_t)alignment - 1) & ~((uintptr_t)alignment - 1)) -
(uintptr_t)dim);
}
public:
TensorShape()
: is_scalar_(false), data_type_(ppl::common::DATATYPE_UNKNOWN), data_format_(ppl::common::DATAFORMAT_UNKNOWN) {}
TensorShape(const TensorShape& other) = default;
TensorShape& operator=(const TensorShape& other) = default;
uint32_t GetRealDimCount() const {
return dims_.size();
};
uint32_t GetDimCount() const {
if (is_scalar_) {
return 1;
}
return dims_.size();
}
int64_t GetDim(uint32_t which) const {
if (is_scalar_) {
return 1;
}
return dims_[which];
}
const int64_t* GetDims() const {
return dims_.data();
}
uint16_t GetPadding0(uint32_t which) const {
if (is_scalar_) {
return 0;
}
return padding0_[which];
}
uint16_t GetPadding1(uint32_t which) const {
if (is_scalar_) {
return 0;
}
return padding1_[which];
}
const uint16_t* GetPadding0s() const {
return padding0_.data();
}
const uint16_t* GetPadding1s() const {
return padding1_.data();
}
ppl::common::datatype_t GetDataType() const {
return data_type_;
}
ppl::common::dataformat_t GetDataFormat() const {
return data_format_;
}
void SetDataType(ppl::common::datatype_t dt) {
data_type_ = dt;
}
void CalcPadding() {
if (data_format_ == ppl::common::DATAFORMAT_NDARRAY) {
for (uint32_t i = 0; i < dims_.size(); ++i) {
padding0_[i] = 0;
padding1_[i] = 0;
}
} else if (dims_.size() >= 2) {
if (data_format_ == ppl::common::DATAFORMAT_N2CX) {
padding1_[TensorShape::kAxisC] = CalcPadding(dims_[1], 2);
} else if (data_format_ == ppl::common::DATAFORMAT_N4CX) {
padding1_[TensorShape::kAxisC] = CalcPadding(dims_[1], 4);
} else if (data_format_ == ppl::common::DATAFORMAT_N8CX) {
padding1_[TensorShape::kAxisC] = CalcPadding(dims_[1], 8);
} else if (data_format_ == ppl::common::DATAFORMAT_N16CX) {
padding1_[TensorShape::kAxisC] = CalcPadding(dims_[1], 16);
} else if (data_format_ == ppl::common::DATAFORMAT_N32CX) {
padding1_[TensorShape::kAxisC] = CalcPadding(dims_[1], 32);
} else if (data_format_ == ppl::common::DATAFORMAT_N16CX) {
padding1_[TensorShape::kAxisC] = CalcPadding(dims_[1], 16);
} else if (data_format_ == ppl::common::DATAFORMAT_NHWC) {
padding1_[TensorShape::kAxisC] = CalcPadding(dims_[1], 8);
}
}
}
void SetDataFormat(ppl::common::dataformat_t data_format) {
// do not change padding if data format is not changed
if (data_format != data_format_) {
data_format_ = data_format;
CalcPadding();
}
}
void ReshapeAsScalar() {
dims_.clear();
padding0_.clear();
padding1_.clear();
is_scalar_ = true;
}
void Reshape(const int64_t* dims, uint32_t dim_count) {
DoResize(dim_count);
if (dim_count == 0) {
is_scalar_ = true;
return;
}
is_scalar_ = false;
for (uint32_t i = 0; i < dim_count; ++i) {
dims_[i] = dims[i];
}
CalcPadding();
}
void Reshape(const std::vector<int64_t>& dims) {
return Reshape(dims.data(), dims.size());
}
void SetPadding0(uint32_t which, uint16_t padding) {
padding0_[which] = padding;
}
void SetPadding1(uint32_t which, uint16_t padding) {
padding1_[which] = padding;
}
void SetDim(uint32_t which, int64_t dim) {
dims_[which] = dim;
}
void SetDimCount(uint32_t dc) {
DoResize(dc);
is_scalar_ = (dc == 0);
}
uint64_t GetElementsIncludingPadding() const {
if (dims_.empty()) {
return is_scalar_ ? 1 : 0;
}
uint64_t accu = 1;
for (uint32_t i = 0; i < dims_.size(); ++i) {
accu *= (dims_[i] + padding0_[i] + padding1_[i]);
}
return accu;
}
uint64_t GetElementsExcludingPadding() const {
if (dims_.empty()) {
return is_scalar_ ? 1 : 0;
}
uint64_t accu = 1;
for (uint32_t i = 0; i < dims_.size(); ++i) {
accu *= dims_[i];
}
return accu;
}
uint64_t GetBytesIncludingPadding() const {
return ppl::common::GetSizeOfDataType(data_type_) * GetElementsIncludingPadding();
}
uint64_t GetBytesExcludingPadding() const {
return ppl::common::GetSizeOfDataType(data_type_) * GetElementsExcludingPadding();
}
uint64_t GetElementsFromDimensionIncludingPadding(uint32_t which) const {
if (dims_.empty()) {
return is_scalar_ ? 1 : 0;
}
uint64_t accu = 1;
for (uint32_t i = which; i < dims_.size(); ++i) {
accu *= (dims_[i] + padding0_[i] + padding1_[i]);
}
return accu;
}
uint64_t GetElementsToDimensionIncludingPadding(uint32_t which) const {
if (dims_.empty()) {
return is_scalar_ ? 1 : 0;
}
uint64_t accu = 1;
for (uint32_t i = 0; i < which; ++i) {
accu *= (dims_[i] + padding0_[i] + padding1_[i]);
}
return accu;
}
uint64_t GetElementsFromDimensionExcludingPadding(uint32_t which) const {
if (dims_.empty()) {
return is_scalar_ ? 1 : 0;
}
uint64_t accu = 1;
for (uint32_t i = which; i < dims_.size(); ++i) {
accu *= dims_[i];
}
return accu;
}
uint64_t GetElementsToDimensionExcludingPadding(uint32_t which) const {
if (dims_.empty()) {
return is_scalar_ ? 1 : 0;
}
uint64_t accu = 1;
for (uint32_t i = 0; i < which; ++i) {
accu *= dims_[i];
}
return accu;
}
uint64_t GetBytesToDimesionIncludingPadding(uint32_t which) const {
return ppl::common::GetSizeOfDataType(data_type_) * GetElementsToDimensionIncludingPadding(which);
}
uint64_t GetBytesToDimesionExcludingPadding(uint32_t which) const {
return ppl::common::GetSizeOfDataType(data_type_) * GetElementsToDimensionExcludingPadding(which);
}
uint64_t GetBytesFromDimesionIncludingPadding(uint32_t which) const {
return ppl::common::GetSizeOfDataType(data_type_) * GetElementsFromDimensionIncludingPadding(which);
}
uint64_t GetBytesFromDimesionExcludingPadding(uint32_t which) const {
return ppl::common::GetSizeOfDataType(data_type_) * GetElementsFromDimensionExcludingPadding(which);
}
void Clear() {
is_scalar_ = false;
data_type_ = ppl::common::DATATYPE_UNKNOWN;
data_format_ = ppl::common::DATAFORMAT_UNKNOWN;
dims_.clear();
padding0_.clear();
padding1_.clear();
}
bool IsScalar() const {
return is_scalar_;
}
bool IsEmpty() const {
if (is_scalar_) {
return false;
}
if (dims_.empty()) {
return true;
}
uint64_t accu = 1;
for (uint32_t i = 0; i < dims_.size(); ++i) {
accu *= dims_[i];
}
return (accu == 0);
}
private:
void DoResize(uint32_t dim_count) {
dims_.resize(dim_count, 0);
padding0_.resize(dim_count, 0);
padding1_.resize(dim_count, 0);
}
};
}} // namespace ppl::nn
#endif