ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DepthwiseConvolutionLayer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
21
23{
24
25void DepthwiseConvolutionLayer::prepareF32()
26{
28 return;
29
30 // DepthwiseConvOp cpu kernel needs additional memory to perform with multi-
31 // threads. So, we allocate it here and pass it to the kernel.
32 const int64_t k_packet_size = nnfw::cker::eigen_support::kPacketSize<float>();
33
34 const auto out_shape = getShape(_output);
35 const auto filter_shape = getShape(_kernel);
36 const int batch = out_shape.Dims(0);
37 const int out_depth = out_shape.Dims(3);
38 const int filter_rows = filter_shape.Dims(1);
39 const int filter_cols = filter_shape.Dims(2);
40
41 const int filter_spatial_size = filter_rows * filter_cols;
42 const int padded_filter_inner_dim_size =
43 ((out_depth + k_packet_size - 1) / k_packet_size) * k_packet_size;
44
45 _use_padded_filter = (out_depth % k_packet_size) == 0 ? false : true;
46
47 // prepare padded_filter buffer for cker
48 auto padded_filter_info = ir::OperandInfo(_kernel->get_info());
49 padded_filter_info.shape({batch, filter_spatial_size, padded_filter_inner_dim_size});
50 _padded_filter = std::make_unique<Tensor>(padded_filter_info, nullptr);
51 _padded_filter->setBuffer(std::make_shared<basic::Allocator>(_padded_filter->total_size()));
52
53 // prepare out_bprop and in_bprop buffer for cker
54 const int thread_count = nnfw::cker::eigen_support::getThreadCount() + 1;
55
56 auto filter_buffers_info = ir::OperandInfo(_kernel->get_info());
57 filter_buffers_info.shape({thread_count, filter_spatial_size, padded_filter_inner_dim_size});
58 _filter_buffers = std::make_unique<Tensor>(filter_buffers_info, nullptr);
59 _filter_buffers->setBuffer(std::make_shared<basic::Allocator>(_filter_buffers->total_size()));
60}
61
63{
64 float output_activation_min = 0, output_activation_max = 0;
65 CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
66
68 op_params.stride_width = _strideWidth;
69 op_params.stride_height = _strideHeight;
74 op_params.depth_multiplier = _multiplier;
75 op_params.float_activation_min = output_activation_min;
76 op_params.float_activation_max = output_activation_max;
77
78 // Since DepthwiseConvOp does not support dilation and different W/H stride yet,
79 // it uses the existing kernel in this case.
81 {
82 nnfw::cker::DepthwiseConvOp(op_params, getShape(_input), getBuffer<float>(_input),
83 getShape(_kernel), getBuffer<float>(_kernel), getShape(_bias),
84 getBuffer<float>(_bias), getBuffer<float>(_padded_filter.get()),
85 _use_padded_filter, getBuffer<float>(_filter_buffers.get()),
86 getShape(_output), getBuffer<float>(_output));
87 }
88 else
89 {
90 nnfw::cker::DepthwiseConv<float, float>(
91 op_params, getShape(_input), getBuffer<float>(_input), getShape(_kernel),
92 getBuffer<float>(_kernel), getShape(_bias), getBuffer<float>(_bias), getShape(_output),
93 getBuffer<float>(_output), _external_context->ruy_context());
94 }
95}
96
98{
99 int32_t output_activation_min = 0;
100 int32_t output_activation_max = 0;
102 &output_activation_max);
103
104 double real_multiplier = 0.0;
105 int32_t output_multiplier = 0;
106 int32_t output_shift = 0;
108 QuantizeMultiplier(real_multiplier, &output_multiplier, &output_shift);
109
111 op_params.stride_width = _strideWidth;
112 op_params.stride_height = _strideHeight;
117 op_params.depth_multiplier = _multiplier;
118 op_params.input_offset = -_input->data_zero_point();
119 op_params.weights_offset = -_kernel->data_zero_point();
121 op_params.output_multiplier = output_multiplier;
122 op_params.output_shift = output_shift;
123 op_params.quantized_activation_min = output_activation_min;
124 op_params.quantized_activation_max = output_activation_max;
125
126 nnfw::cker::DepthwiseConv<uint8_t, int32_t>(
127 op_params, getShape(_input), getBuffer<uint8_t>(_input), getShape(_kernel),
128 getBuffer<uint8_t>(_kernel), getShape(_bias), getBuffer<int32_t>(_bias), getShape(_output),
129 getBuffer<uint8_t>(_output), _external_context->ruy_context());
130}
131
133{
137 op_params.stride_width = _strideWidth;
138 op_params.stride_height = _strideHeight;
141 op_params.depth_multiplier = _multiplier;
142 op_params.input_offset = -_input->data_zero_point();
144 int32_t output_activation_min = 0;
145 int32_t output_activation_max = 0;
147 &output_activation_max);
148 op_params.quantized_activation_min = output_activation_min;
149 op_params.quantized_activation_max = output_activation_max;
150 // NOTE: The following fields of ConvParams are not used:
151 // padding_type, weights_offset, output_{multiplier,shift}, float_activation_{min,max}
152
154 op_params, _per_channel_output_multiplier.data(), _per_channel_output_shift.data(),
155 getShape(_input), getBuffer<uint8_t>(_input), getShape(_kernel), getBuffer<uint8_t>(_kernel),
156 _kernel->data_zero_points().data(), getShape(_bias), getBuffer<int32_t>(_bias),
157 getShape(_output), getBuffer<uint8_t>(_output));
158}
159
161{
162 if (!_prepared)
163 {
164 prepareQ8i();
165 _prepared = true;
166 }
167
168 int32_t output_activation_min = 0;
169 int32_t output_activation_max = 0;
171 &output_activation_max);
172
177 op_params.depth_multiplier = _multiplier;
178 op_params.stride_width = _strideWidth;
179 op_params.stride_height = _strideHeight;
182 op_params.input_offset = -_input->data_zero_point();
183 op_params.weights_offset = 0;
185 op_params.quantized_activation_min = output_activation_min;
186 op_params.quantized_activation_max = output_activation_max;
187
189 op_params, _per_channel_output_multiplier.data(), _per_channel_output_shift.data(),
190 getShape(_input), getBuffer<int8_t>(_input), getShape(_kernel), getBuffer<int8_t>(_kernel),
191 getShape(_bias), getBuffer<int32_t>(_bias), getShape(_output), getBuffer<int8_t>(_output),
192 _external_context->ruy_context());
193}
194
196{
197 if (!_prepared)
198 {
199 prepareQ8iHybridPerChannel();
200 _prepared = true;
201 }
202
203 float output_activation_min = 0, output_activation_max = 0;
204 CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
205
206 auto input_shape = getShape(_input);
207 const int batch_size = input_shape.Dims(0);
208 const int input_size = input_shape.FlatSize() / batch_size;
209
210 auto scaling_factors_ptr = _input_scaling_factors.data();
211 auto input_offsets_ptr = _input_offsets.data();
212
213 for (int b = 0; b < batch_size; ++b)
214 {
215 const int offset = b * input_size;
216 nnfw::cker::PortableAsymmetricQuantizeFloats(getBuffer<float>(_input) + offset, input_size,
217 _input_quantized.data() + offset,
218 &scaling_factors_ptr[b], &input_offsets_ptr[b]);
219 }
220
224 op_params.depth_multiplier = _multiplier;
225 op_params.stride_width = _strideWidth;
226 op_params.stride_height = _strideHeight;
229 op_params.float_activation_min = output_activation_min;
230 op_params.float_activation_max = output_activation_max;
231
233 op_params, _input_scaling_factors.data(), getShape(_input), _input_quantized.data(),
234 getShape(_kernel), getBuffer<int8_t>(_kernel), getShape(_bias), getBuffer<float>(_bias),
235 getShape(_output), getBuffer<float>(_output), _kernel->data_scales().data(),
236 _input_offsets.data());
237}
238
239void DepthwiseConvolutionLayer::prepareQ8i()
240{
243 _kernel->data_scales().size(), getShape(_kernel).Dims(3), _per_channel_output_multiplier,
244 _per_channel_output_shift);
245}
246
247void DepthwiseConvolutionLayer::prepareQ8uPerChannel()
248{
251 _kernel->data_scales().size(), getShape(_kernel).Dims(3), _per_channel_output_multiplier,
252 _per_channel_output_shift);
253}
254
255void DepthwiseConvolutionLayer::prepareQ8iHybridPerChannel()
256{
257 // allocate memory for activation quantization.
258 // - quantized values (int8_t type and same shape of original input)
259 // - quantization params (= scale/zeropoint for each input)
260 auto input_shape = getShape(_input);
261 const int batch_size = input_shape.Dims(0);
262 const int input_size = input_shape.FlatSize() / batch_size;
263 _input_quantized.resize(input_size);
264 // TODO: Optimize the case of batch_size = 1
265 _input_scaling_factors.resize(batch_size);
266 _input_offsets.resize(batch_size);
267}
268
269void DepthwiseConvolutionLayer::ensureQ8iHybridPerChannel()
270{
271 // ensure weight is per-channel quantized.
272 int32_t kernel_input_channel = getShape(_kernel).Dims(3);
273 // zero_points comes from flatbuffer vector. Its size is within uint32_t range.
274 size_t kernel_zerop_cnt = _kernel->data_scales().size();
275 // promote to int64_t to compare int32_t and uint32_t
276 if ((int64_t)kernel_input_channel != (int64_t)kernel_zerop_cnt)
277 throw std::runtime_error{"DConv2D hybrid supports only per-channel quantized weight."};
278}
279
281 const IPortableTensor *input, const IPortableTensor *kernel, const IPortableTensor *bias,
282 const uint32_t paddingLeft, const uint32_t paddingRight, const uint32_t paddingTop,
283 const uint32_t paddingBottom, const uint32_t strideWidth, const uint32_t strideHeight,
284 const uint32_t multiplier, const uint32_t dilationWidth, const uint32_t dilationHeight,
285 const ir::Activation activation, IPortableTensor *output,
286 const std::shared_ptr<ExternalContext> &external_context)
287{
288 _input = input;
289 _kernel = kernel;
290 _bias = bias;
291 _paddingLeft = paddingLeft;
292 _paddingRight = paddingRight;
293 _paddingTop = paddingTop;
294 _paddingBottom = paddingBottom;
295 _strideWidth = strideWidth;
296 _strideHeight = strideHeight;
297 _multiplier = multiplier;
298 _dilationWidth = dilationWidth;
299 _dilationHeight = dilationHeight;
300 _activation = activation;
301 _output = output;
302 _external_context = external_context;
303 _is_hybrid = _input->data_type() == OperandType::FLOAT32 &&
304 _kernel->data_type() == OperandType::QUANT_INT8_SYMM;
305
306 if (_is_hybrid)
307 {
308 ensureQ8iHybridPerChannel();
309 prepareQ8iHybridPerChannel();
310 _prepared = true;
311 }
312 else if (_input->data_type() == OperandType::FLOAT32)
313 {
314 prepareF32();
315 }
316 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
317 {
319 {
320 prepareQ8i();
321 _prepared = true;
322 }
323 }
324 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM && _kernel->is_constant() &&
326 {
327 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
328 if (per_channel_quantized)
329 {
330 prepareQ8uPerChannel();
331 _prepared = true;
332 }
333 }
334}
335
337{
338 if (_is_hybrid)
339 {
341 }
342 else if (_input->data_type() == OperandType::FLOAT32)
343 {
344 convFloat32();
345 }
346 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
347 {
348 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
349 if (per_channel_quantized)
351 else
353 }
354 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
355 {
356 convQ8i();
357 }
358 else
359 {
360 throw std::runtime_error{"DepthwiseConv: unsupported data type"};
361 }
362}
363
364} // namespace onert::backend::cpu::ops
int32_t Dims(int i) const
Definition Shape.h:92
A tensor class that is portable for other backends.
const std::vector< float > & data_scales() const override final
float data_scale() const override final
const ir::OperandInfo & get_info() const
int32_t data_zero_point() const override final
const std::vector< int32_t > & data_zero_points() const override
ir::DataType data_type() const override final
bool is_dynamic() const override final
Return true if the tensor needs dynamic allocation, meaning that during compile-time the outpus shape...
bool is_constant() const override final
Return true if the tensor is constant.
void configure(const IPortableTensor *input, const IPortableTensor *kernel, const IPortableTensor *bias, const uint32_t paddingLeft, const uint32_t paddingRight, const uint32_t paddingTop, const uint32_t paddingBottom, const uint32_t strideW, const uint32_t strideH, const uint32_t multiplier, const uint32_t dilationWidth, const uint32_t dilationHeight, const ir::Activation activation, IPortableTensor *output, const std::shared_ptr< ExternalContext > &external_context)
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
void DepthwiseConvPerChannel(const DepthwiseConvParams &params, const int32_t *output_multiplier, const int32_t *output_shift, const Shape &input_shape, const int8_t *input_data, const Shape &filter_shape, const int8_t *filter_data, const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape, int8_t *output_data, ruy::Context *ruy_context)
void DepthwiseConvPerChannel(const DepthwiseConvParams &params, const int32_t *output_multiplier, const int32_t *output_shift, const Shape &input_shape, const uint8_t *input_data, const Shape &filter_shape, const uint8_t *filter_data, const int32_t *filter_zeropoint, const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape, uint8_t *output_data)
void DepthwiseConvHybridPerChannel(const DepthwiseConvParams &params, float *scaling_factors_ptr, const Shape &input_shape, const int8_t *input_data, const Shape &filter_shape, const int8_t *filter_data, const Shape &bias_shape, const float *bias_data, const Shape &output_shape, float *output_data, const float *per_channel_scale, int32_t *input_offset)
void DepthwiseConvOp(const DepthwiseConvParams &params, const Shape &input_shape, const float *input_data, const Shape &filter_shape, const float *filter_data, const Shape &bias_shape, const float *bias_data, float *padded_filter_data, bool pad_filter, float *filter_buffers_data, const Shape &output_shape, float *output_data)
void PortableAsymmetricQuantizeFloats(const float *values, const int size, int8_t *quantized_values, float *scaling_factor, int32_t *offset)
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
void GetQuantizedConvolutionMultipliersAndShifts(float input_scale, float output_scale, const float *filter_scales, size_t filter_scales_size, int num_channels, std::vector< int32_t > &per_channel_output_multiplier, std::vector< int > &per_channel_output_shift)
void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
void CalculateActivationRangeQuantized(ir::Activation activation, const IPortableTensor *output, int32_t *act_min, int32_t *act_max)
void GetQuantizedConvolutionMultiplier(const IPortableTensor *input, const IPortableTensor *filter, const IPortableTensor *bias, const IPortableTensor *output, double *multiplier)
void CalculateActivationRange(ir::Activation activation, T *activation_min, T *activation_max)
Definition Dims.h:26
PaddingValues padding_values
Definition Types.h:234