-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathConvolution.hpp
More file actions
299 lines (262 loc) · 9.11 KB
/
Convolution.hpp
File metadata and controls
299 lines (262 loc) · 9.11 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
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
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef UTENSOR_CONVOLUTION_OPS_H
#define UTENSOR_CONVOLUTION_OPS_H
#include <algorithm>
#include <limits>
#include "Convolution_kernels.hpp"
#include "uTensor/core/operatorBase.hpp"
namespace uTensor {
namespace ReferenceOperators {
namespace Conv2dConstants {
// https://github.com/tensorflow/tensorflow/blob/28d1ad34bb59e3e1631b5807eebc46563ef3382c/tensorflow/lite/kernels/internal/reference/conv.h#L56-L57
constexpr int input_batch_dim = 0;
constexpr int input_height_dim = 0;
constexpr int input_witdh_dim = 0;
constexpr int input_channel_dim = 0;
constexpr int filter_height_dim = 1;
constexpr int filter_width_dim = 2;
constexpr int filter_in_channels_dim = 3;
constexpr int filter_out_channels_dim = 0;
constexpr int output_height_dim = 1;
constexpr int output_width_dim = 2;
}
// Can use these intermediate types to make the convolution operator more
// generic. Maxpool, conv, average pool, median etc. are all basically the same
// operation with target functions.
template <typename T>
class ConvFilter {
T tmp;
const Tensor& filter;
public:
ConvFilter(const Tensor& filter) : tmp(0), filter(filter) {}
inline void reset() { tmp = 0; }
inline void PartialCompute(const T& input_value, int i, int j, int k, int l) {
const T filter_value = filter(i, j, k, l);
tmp += (input_value * filter_value);
}
inline T finalize() const { return tmp; }
// https://github.com/tensorflow/tensorflow/blob/28d1ad34bb59e3e1631b5807eebc46563ef3382c/tensorflow/lite/kernels/internal/reference/conv.h#L56-L57
inline int16_t height() const { return filter->get_shape()[Conv2dConstants::filter_height_dim]; }
inline int16_t width() const { return filter->get_shape()[Conv2dConstants::filter_width_dim]; }
inline int16_t in_channels() const { return filter->get_shape()[Conv2dConstants::filter_in_channels_dim]; }
inline int16_t out_channels() const { return filter->get_shape()[Conv2dConstants::filter_out_channels_dim]; }
};
// Specialization for quantization
template <>
class ConvFilter<int8_t> {
float tmp;
const Tensor& filter;
public:
ConvFilter(const Tensor& filter) : tmp(0), filter(filter) {}
inline void reset() { tmp = 0; }
inline void PartialCompute(const float& input_value, int i, int j, int k, int l) {
const int32_t fv32 = static_cast<int32_t>(static_cast<int8_t>(filter(i, j, k, l)));
const int32_t zp = filter->get_quantization_params().get_zeroP_for_channel(i);
const float scale = filter->get_quantization_params().get_scale_for_channel(i);
const float filter_value = (fv32 - zp)*scale;
tmp += (input_value * filter_value);
}
inline float finalize() const { return tmp; }
inline int16_t height() const { return filter->get_shape()[Conv2dConstants::filter_height_dim]; }
inline int16_t width() const { return filter->get_shape()[Conv2dConstants::filter_width_dim]; }
inline int16_t in_channels() const { return filter->get_shape()[Conv2dConstants::filter_in_channels_dim]; }
inline int16_t out_channels() const { return filter->get_shape()[Conv2dConstants::filter_out_channels_dim]; }
};
template <typename T>
class MaxFilter {
T tmp;
int16_t h;
int16_t w;
int16_t c;
public:
MaxFilter(int16_t h, int16_t w, int16_t c) : h(h), w(w), c(c) {}
inline void reset() { tmp = std::numeric_limits<T>::lowest(); }
inline void PartialCompute(const T& input_value, int i, int j, int k, int l) {
tmp = std::max(tmp, input_value);
}
inline T finalize() const { return tmp; }
inline int16_t height() const { return h; }
inline int16_t width() const { return w; }
inline int16_t in_channels() const { return 1; }
inline int16_t out_channels() const { return c; }
};
template <typename T>
class MinFilter {
T tmp;
int16_t h;
int16_t w;
int16_t c;
public:
MinFilter(int16_t h, int16_t w, int16_t c) : h(h), w(w), c(c) {}
inline void reset() { tmp = std::numeric_limits<T>::max(); }
inline void PartialCompute(const T& input_value, int i, int j, int k, int l) {
tmp = std::min(tmp, input_value);
}
inline T finalize() const { return tmp; }
inline int16_t height() const { return h; }
inline int16_t width() const { return w; }
inline int16_t in_channels() const { return 1; }
inline int16_t out_channels() const { return c; }
};
template <typename T>
class AvgFilter {
T tmp;
int16_t w;
int16_t h;
int16_t c;
public:
AvgFilter(int16_t h, int16_t w, int16_t c) : h(h), w(w), c(c) {}
inline void reset() { tmp = 0; }
inline void PartialCompute(const T& input_value, int i, int j, int k, int l) {
tmp += input_value;
}
inline T finalize() const {
return tmp / (w * h);
} //(static_cast<T>(w*h)); }
inline int16_t height() const { return h; }
inline int16_t width() const { return w; }
inline int16_t in_channels() const { return 1; }
inline int16_t out_channels() const { return c; }
};
template<typename T>
class NoBias {
public:
T operator()(int32_t i) { return 0; }
};
template<typename T>
class wBias {
public:
wBias(const Tensor& t) : t(t) {}
T operator()(int32_t i) { return static_cast<T>(t(i)); }
private:
const Tensor& t;
};
template<>
class wBias<int8_t> {
public:
wBias(const Tensor& t) : t(t) {}
float operator()(int32_t i) {
const int32_t b32 = static_cast<int32_t>(t(i));
const float scale = t->get_quantization_params().get_scale_for_channel(i);
const int32_t zp = t->get_quantization_params().get_zeroP_for_channel(i);
return (b32 - zp)*scale;
}
private:
const Tensor& t;
};
template <typename T>
class Conv2dOperator : public OperatorInterface<3, 1> {
public:
enum names_in : uint8_t { in, filter, bias };
enum names_out : uint8_t { out };
Conv2dOperator(std::initializer_list<uint16_t> strides, Padding padding)
: _padding(padding) {
for(int j = 0; j < 4; j++){
_stride[j] = 1;
}
int i = 0;
if(strides.size() == 2){
i = 1; // Offset the stride loc
}
for (auto s : strides) {
_stride[i++] = s;
}
}
protected:
virtual void compute();
private:
uint16_t _stride[4];
Padding _padding;
};
template <typename T>
void Conv2dOperator<T>::compute() {
bool have_bias = inputs.has(bias);
ConvFilter<T> conv(inputs[filter].tensor());
if(have_bias) {
wBias<T> w_bias(inputs[bias].tensor());
generic_convolution_kernel<T, ConvFilter<T>>(
outputs[out].tensor(), inputs[in].tensor(), conv, w_bias, _padding, _stride);
} else {
NoBias<T> no_bias;
generic_convolution_kernel<T, ConvFilter<T>>(
outputs[out].tensor(), inputs[in].tensor(), conv, no_bias, _padding, _stride);
}
}
// Specialization for symmetric quantization
template <>
void Conv2dOperator<int8_t>::compute();
template <typename T>
class DepthwiseSeparableConvOperator : public OperatorInterface<3, 1> {
public:
enum names_in : uint8_t { in, depthwise_filter, pointwise_filter };
enum names_out : uint8_t { out };
// TODO Add dialations
DepthwiseSeparableConvOperator(std::initializer_list<uint16_t> strides,
Padding padding)
: _padding(padding) {
int i = 0;
for (auto s : strides) {
_stride[i++] = s;
}
}
protected:
virtual void compute() {
TensorShape& in_shape = inputs[in].tensor()->get_shape();
TensorShape& df_shape = inputs[depthwise_filter].tensor()->get_shape();
TensorShape& pf_shape = inputs[pointwise_filter].tensor()->get_shape();
TensorShape& out_shape = outputs[out].tensor()->get_shape();
if (in_shape[3] != df_shape[2]) {
Context::get_default_context()->throwError(
new InvalidTensorDimensionsError);
}
if (pf_shape[0] != 1 || pf_shape[1] != 1) {
Context::get_default_context()->throwError(
new InvalidTensorDimensionsError);
}
depthwise_separable_convolution_kernel<T>(
outputs[out].tensor(), inputs[in].tensor(),
inputs[depthwise_filter].tensor(), inputs[pointwise_filter].tensor(),
_padding, _stride);
}
private:
uint16_t _stride[4];
Padding _padding;
};
template <typename T, typename Filter>
class GenericPoolOperator : public OperatorInterface<1, 1> {
public:
enum names_in : uint8_t { in };
enum names_out : uint8_t { out };
// TODO Add dialations
GenericPoolOperator(std::initializer_list<uint16_t> k_size,
std::initializer_list<uint16_t> strides, Padding padding)
: _padding(padding) {
int i = 0;
for (auto s : strides) {
_stride[i++] = s;
}
i = 0;
for (auto k : k_size) {
_k_size[i++] = k;
}
}
protected:
virtual void compute() {
TensorShape& in_shape = inputs[in].tensor()->get_shape();
Filter filter(_k_size[0], _k_size[1], in_shape[3]);
generic_pool_convolution_kernel<T, Filter>(
outputs[out].tensor(), inputs[in].tensor(), filter, _padding, _stride);
}
private:
uint16_t _k_size[2];
uint16_t _stride[4];
Padding _padding;
};
template <typename T>
using MaxPoolOperator = GenericPoolOperator<T, MaxFilter<T>>;
template <typename T>
using AvgPoolOperator = GenericPoolOperator<T, AvgFilter<T>>;
template<typename T>
using MinPoolOperator = GenericPoolOperator<T, MinFilter<T>>;
}
} // namespace uTensor
#endif