ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Conv2D.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "Conv2D.h"
19#include "QuantizationHelpers.h"
20#include "Common.h"
21
22#include "mir/Tensor.h"
23
24#include <cmath>
25
26namespace mir_interpreter
27{
28
29using namespace mir;
30
31static std::int32_t calcOffset(const Shape &shape, std::int32_t i0, std::int32_t i1,
32 std::int32_t i2, std::int32_t i3)
33{
34 return ((i0 * shape.dim(1) + i1) * shape.dim(2) + i2) * shape.dim(3) + i3;
35}
36
37template <typename T> struct Conv2DImpl
38{
39 static void run(const TensorVariant &input, const TensorVariant &kernel,
40 const Conv2DOpAttributes &attributes, TensorVariant &result,
41 const TensorVariant *fused_bias);
42};
43
44template <typename T>
45void Conv2DImpl<T>::run(const TensorVariant &input, const TensorVariant &kernel,
46 const Conv2DOpAttributes &attributes, TensorVariant &result,
47 const TensorVariant *fused_bias)
48{
49 const auto *input_data = reinterpret_cast<const T *>(input.atOffset(0));
50 const auto *kernel_data = reinterpret_cast<const T *>(kernel.atOffset(0));
51 auto *result_data = reinterpret_cast<T *>(result.atOffset(0));
52
53 const Shape &input_shape = input.getShape();
54 const Shape &output_shape = result.getShape();
55 const Shape &kernel_shape = kernel.getShape();
56
57 const std::vector<std::int32_t> &strides = attributes.strides;
58 const std::vector<std::int32_t> &padding_before = attributes.padding_before;
59 const std::int32_t num_groups = attributes.num_groups;
60 assert(attributes.data_format == DataFormat::NHWC);
61
62 const std::int32_t batch_size = output_shape.dim(0);
63 const std::int32_t output_height = output_shape.dim(1);
64 const std::int32_t output_width = output_shape.dim(2);
65 const std::int32_t kernel_height = kernel_shape.dim(1);
66 const std::int32_t kernel_width = kernel_shape.dim(2);
67 const std::int32_t input_height = input_shape.dim(1);
68 const std::int32_t input_width = input_shape.dim(2);
69
70 const std::int32_t num_in_channels = input_shape.dim(3);
71 const std::int32_t num_out_channels = output_shape.dim(3);
72
73 assert(num_in_channels % num_groups == 0);
74 assert(num_out_channels % num_groups == 0);
75
76 const std::int32_t out_group_size = num_out_channels / num_groups;
77 const std::int32_t in_group_size = num_in_channels / num_groups;
78
79 assert(kernel_shape.dim(3) == in_group_size);
80 assert(kernel_shape.dim(0) == num_out_channels);
81
82 for (std::int32_t batch = 0; batch < batch_size; ++batch)
83 {
84 for (std::int32_t out_y = 0; out_y < output_height; ++out_y)
85 {
86 for (std::int32_t out_x = 0; out_x < output_width; ++out_x)
87 {
88 for (std::int32_t group = 0; group < num_groups; ++group)
89 {
90 const std::int32_t out_group_offset = group * out_group_size;
91 const std::int32_t in_group_offset = group * in_group_size;
92
93 for (std::int32_t out_c = 0; out_c < out_group_size; ++out_c)
94 {
95 const std::int32_t in_y_origin = (out_y * strides[0]) - padding_before[0];
96 const std::int32_t in_x_origin = (out_x * strides[1]) - padding_before[1];
97
98 T sum = 0.0f;
99
100 for (std::int32_t kernel_y = 0; kernel_y < kernel_height; ++kernel_y)
101 {
102 for (std::int32_t kernel_x = 0; kernel_x < kernel_width; ++kernel_x)
103 {
104 for (std::int32_t in_c = 0; in_c < in_group_size; ++in_c)
105 {
106 const std::int32_t in_y = in_y_origin + kernel_y;
107 const std::int32_t in_x = in_x_origin + kernel_x;
108
109 if ((in_y >= 0 && in_y < input_height) && (in_x >= 0 && in_x < input_width))
110 {
111 const std::int32_t in_offset =
112 calcOffset(input_shape, batch, in_y, in_x, in_group_offset + in_c);
113 const std::int32_t kernel_offset =
114 calcOffset(kernel_shape, out_group_offset + out_c, kernel_y, kernel_x, in_c);
115 const T input_val = input_data[in_offset];
116 const T kernel_val = kernel_data[kernel_offset];
117 sum += kernel_val * input_val;
118 }
119 }
120 }
121 }
122
123 const std::int32_t out_offset =
124 calcOffset(output_shape, batch, out_y, out_x, out_group_offset + out_c);
125 result_data[out_offset] = sum;
126 }
127 }
128 }
129 }
130 }
131}
132
133template <> struct Conv2DImpl<uint8_t>
134{
135 static void run(const TensorVariant &input, const TensorVariant &kernel,
136 const Conv2DOpAttributes &attributes, TensorVariant &result,
137 const TensorVariant *fused_bias);
138};
139
140void Conv2DImpl<uint8_t>::run(const TensorVariant &input, const TensorVariant &kernel,
141 const Conv2DOpAttributes &attributes, TensorVariant &result,
142 const TensorVariant *fused_bias)
143{
144 if (!fused_bias)
145 {
146 throw std::runtime_error{"Quantized Conv2D cannot be executed without fused bias"};
147 }
148
149 const auto &input_type = input.getType();
150 const auto &kernel_type = kernel.getType();
151 const auto &bias_type = fused_bias->getType();
152 const auto &output_type = result.getType();
153 (void)bias_type;
154
155 assert(input_type.isQuantized());
156 assert(kernel_type.isQuantized());
157 assert(bias_type.isQuantized());
158 assert(output_type.isQuantized());
159 assert(input_type.getElementType() == DataType::UINT8);
160 assert(kernel_type.getElementType() == DataType::UINT8);
161 assert(bias_type.getElementType() == DataType::INT32);
162 assert(output_type.getElementType() == DataType::UINT8);
163
164 int32_t input_offset = -input_type.getQuantization().getZeroPoint();
165 int32_t kernel_offset = -kernel_type.getQuantization().getZeroPoint();
166 int32_t output_offset = output_type.getQuantization().getZeroPoint();
167
168 double input_scale = input_type.getQuantization().getScale();
169 double kernel_scale = kernel_type.getQuantization().getScale();
170 double output_scale = output_type.getQuantization().getScale();
171
172 double real_multiplier = input_scale * kernel_scale / output_scale;
173 int32_t output_multiplier = 0;
174 int output_shift = 0;
175 QuantizeMultiplier(real_multiplier, &output_multiplier, &output_shift);
176
177 const Shape &in_shape = input.getShape();
178 const Shape &kernel_shape = kernel.getShape();
179 const Shape &out_shape = result.getShape();
180 const auto &strides = attributes.strides;
181 const std::vector<int32_t> &pads = attributes.padding_before;
182 assert(attributes.num_groups == 1);
183 assert(attributes.data_format == DataFormat::NHWC);
184
185 assert(in_shape.rank() == 4);
186 assert(kernel_shape.rank() == 4);
187 assert(kernel_shape.dim(3) == in_shape.dim(3));
188 assert(kernel_shape.dim(0) == out_shape.dim(3));
189 assert(strides.size() == 2);
190 assert(pads.size() == 2);
191
192 int32_t stride_height = strides[0];
193 int32_t stride_width = strides[1];
194
195 int32_t pad_height = pads[0];
196 int32_t pad_width = pads[1];
197
198 int32_t input_height = in_shape.dim(1);
199 int32_t input_width = in_shape.dim(2);
200
201 Tensor<uint8_t> input_accessor(input);
202 Tensor<uint8_t> kernel_accessor(kernel);
203 Tensor<int32_t> bias_accessor(*fused_bias);
204 Tensor<uint8_t> res_accessor(result);
205
206 int32_t output_min = std::numeric_limits<uint8_t>::min();
207 int32_t output_max = std::numeric_limits<uint8_t>::max();
208
209 for (int batch = 0; batch < out_shape.dim(0); ++batch)
210 {
211 for (int out_y = 0; out_y < out_shape.dim(1); ++out_y)
212 {
213 for (int out_x = 0; out_x < out_shape.dim(2); ++out_x)
214 {
215 for (int out_channel = 0; out_channel < out_shape.dim(3); ++out_channel)
216 {
217 const int in_x_origin = (out_x * stride_width) - pad_width;
218 const int in_y_origin = (out_y * stride_height) - pad_height;
219 int32_t acc = 0;
220 for (int filter_y = 0; filter_y < kernel_shape.dim(1); ++filter_y)
221 {
222 for (int filter_x = 0; filter_x < kernel_shape.dim(2); ++filter_x)
223 {
224 for (int in_channel = 0; in_channel < kernel_shape.dim(3); ++in_channel)
225 {
226 const int in_x = in_x_origin + filter_x;
227 const int in_y = in_y_origin + filter_y;
228 // If the location is outside the bounds of the input image,
229 // use zero as a default value.
230 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
231 {
232 Index in_index{batch, in_y, in_x, in_channel};
233 Index ker_index{out_channel, filter_y, filter_x, in_channel};
234 int32_t input_val = input_accessor.at(in_index);
235 int32_t kernel_val = kernel_accessor.at(ker_index);
236 acc += (kernel_val + kernel_offset) * (input_val + input_offset);
237 }
238 }
239 }
240 }
241 acc += bias_accessor.at(Index{out_channel});
242 acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
243 acc += output_offset;
244 acc = std::max(acc, output_min);
245 acc = std::min(acc, output_max);
246 Index out_index{batch, out_y, out_x, out_channel};
247 res_accessor.at(out_index) = static_cast<uint8_t>(acc);
248 }
249 }
250 }
251 }
252}
253
254void Conv2D(const mir::TensorVariant &input, const mir::TensorVariant &kernel,
255 const mir::Conv2DOpAttributes &attributes, mir::TensorVariant &result,
256 const mir::TensorVariant *fused_bias)
257{
258 dispatch<Conv2DImpl>(result.getElementType(), input, kernel, attributes, result, fused_bias);
259}
260
261} // namespace mir_interpreter
int32_t & dim(int32_t axis) noexcept
Definition Shape.h:47
int32_t rank() const
Definition Shape.h:43
T at(const Index &id) const
Definition Tensor.h:31
char * atOffset(int32_t offset) const
const TensorType & getType() const
const Shape & getShape() const
const luci_interpreter::RuntimeShape output_shape
int32_t calcOffset(const Shape &shape, int32_t d0, int32_t d1, int32_t d2, int32_t d3)
Definition Utils.h:75
int32_t MultiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
void Conv2D(const mir::TensorVariant &input, const mir::TensorVariant &kernel, const mir::Conv2DOpAttributes &attributes, mir::TensorVariant &result, const mir::TensorVariant *fused_bias)
Definition Conv2D.cpp:254
void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
Definition Shape.h:28
std::vector< std::int32_t > strides
Definition Attributes.h:31
std::int32_t num_groups
Definition Attributes.h:34
std::vector< std::int32_t > padding_before
Definition Attributes.h:32
static void run(const TensorVariant &input, const TensorVariant &kernel, const Conv2DOpAttributes &attributes, TensorVariant &result, const TensorVariant *fused_bias)
Definition Conv2D.cpp:45