1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "Conv3D.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
27*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
28*c217d954SCole Faust #include "support/Requires.h"
29*c217d954SCole Faust #include "tests/validation/reference/UtilsQuantizedAsymm.h"
30*c217d954SCole Faust
31*c217d954SCole Faust // Source/Destination Tensor shape indices (N D H W C)
32*c217d954SCole Faust constexpr unsigned int batch_dim = 4u;
33*c217d954SCole Faust constexpr unsigned int depth_dim = 3u;
34*c217d954SCole Faust constexpr unsigned int height_dim = 2u;
35*c217d954SCole Faust constexpr unsigned int width_dim = 1u;
36*c217d954SCole Faust constexpr unsigned int channel_dim = 0u;
37*c217d954SCole Faust
38*c217d954SCole Faust // Weight tensor shape indices (D H W Cin Cout)
39*c217d954SCole Faust constexpr unsigned int weights_depth_dim = 4u;
40*c217d954SCole Faust constexpr unsigned int weights_height_dim = 3u;
41*c217d954SCole Faust constexpr unsigned int weights_width_dim = 2u;
42*c217d954SCole Faust constexpr unsigned int weights_CHin_dim = 1u;
43*c217d954SCole Faust constexpr unsigned int weights_CHout_dim = 0u;
44*c217d954SCole Faust
45*c217d954SCole Faust namespace arm_compute
46*c217d954SCole Faust {
47*c217d954SCole Faust namespace test
48*c217d954SCole Faust {
49*c217d954SCole Faust namespace validation
50*c217d954SCole Faust {
51*c217d954SCole Faust namespace reference
52*c217d954SCole Faust {
53*c217d954SCole Faust namespace
54*c217d954SCole Faust {
is_valid_pixel(int i,int min,int max)55*c217d954SCole Faust inline bool is_valid_pixel(int i, int min, int max)
56*c217d954SCole Faust {
57*c217d954SCole Faust return (i >= min && i < max);
58*c217d954SCole Faust }
59*c217d954SCole Faust
60*c217d954SCole Faust // Evaluate the weights against an element in a given tensor.
61*c217d954SCole Faust template < typename T, typename TB, typename std::enable_if < validation::is_floating_point<T>::value &&validation::is_floating_point<TB>::value, int >::type = 0 >
calculate_conv3d(const SimpleTensor<T> & src,const SimpleTensor<T> & weights,const SimpleTensor<TB> & bias,const Size3D & dilation,int batch,int z_start,int y_start,int x_start,int ch_out,UniformQuantizationInfo oq_info)62*c217d954SCole Faust T calculate_conv3d(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const Size3D &dilation, int batch,
63*c217d954SCole Faust int z_start, int y_start, int x_start, int ch_out, UniformQuantizationInfo oq_info)
64*c217d954SCole Faust {
65*c217d954SCole Faust ARM_COMPUTE_UNUSED(oq_info);
66*c217d954SCole Faust
67*c217d954SCole Faust const unsigned int weights_width = weights.shape()[weights_width_dim];
68*c217d954SCole Faust const unsigned int weights_height = weights.shape()[weights_height_dim];
69*c217d954SCole Faust const unsigned int weights_depth = weights.shape()[weights_depth_dim];
70*c217d954SCole Faust
71*c217d954SCole Faust const unsigned int src_channels = src.shape()[channel_dim];
72*c217d954SCole Faust const unsigned int src_width = src.shape()[width_dim];
73*c217d954SCole Faust const unsigned int src_height = src.shape()[height_dim];
74*c217d954SCole Faust const unsigned int src_depth = src.shape()[depth_dim];
75*c217d954SCole Faust
76*c217d954SCole Faust T total(0);
77*c217d954SCole Faust for(unsigned int weight_d = 0; weight_d < weights_depth; ++weight_d)
78*c217d954SCole Faust {
79*c217d954SCole Faust const int idx_z = z_start + dilation.depth * weight_d;
80*c217d954SCole Faust for(unsigned int weight_y = 0; weight_y < weights_height; ++weight_y)
81*c217d954SCole Faust {
82*c217d954SCole Faust const int idx_y = y_start + dilation.height * weight_y;
83*c217d954SCole Faust for(unsigned int weight_x = 0; weight_x < weights_width; ++weight_x)
84*c217d954SCole Faust {
85*c217d954SCole Faust const int idx_x = x_start + dilation.width * weight_x;
86*c217d954SCole Faust
87*c217d954SCole Faust //Check if the point is within padding
88*c217d954SCole Faust const bool is_x_valid = is_valid_pixel(idx_x, 0, src_width);
89*c217d954SCole Faust const bool is_y_valid = is_valid_pixel(idx_y, 0, src_height);
90*c217d954SCole Faust const bool is_z_valid = is_valid_pixel(idx_z, 0, src_depth);
91*c217d954SCole Faust const bool is_invalid_pixel = !(is_x_valid && is_y_valid && is_z_valid);
92*c217d954SCole Faust if(is_invalid_pixel)
93*c217d954SCole Faust {
94*c217d954SCole Faust continue;
95*c217d954SCole Faust }
96*c217d954SCole Faust
97*c217d954SCole Faust for(unsigned int ch_in = 0; ch_in < src_channels; ++ch_in)
98*c217d954SCole Faust {
99*c217d954SCole Faust const T *in_ptr = src.data();
100*c217d954SCole Faust const T *w_ptr = weights.data();
101*c217d954SCole Faust
102*c217d954SCole Faust const int in_offset = coord2index(src.shape(), Coordinates{ ch_in, idx_x, idx_y, idx_z, batch });
103*c217d954SCole Faust const int weight_offset = coord2index(weights.shape(), Coordinates{ ch_out, ch_in, weight_x, weight_y, weight_d });
104*c217d954SCole Faust T input_value = in_ptr[in_offset];
105*c217d954SCole Faust T weight_value = w_ptr[weight_offset];
106*c217d954SCole Faust total += (input_value * weight_value);
107*c217d954SCole Faust }
108*c217d954SCole Faust }
109*c217d954SCole Faust }
110*c217d954SCole Faust }
111*c217d954SCole Faust
112*c217d954SCole Faust const TB *b_ptr = bias.data();
113*c217d954SCole Faust TB bias_value = b_ptr[ch_out];
114*c217d954SCole Faust
115*c217d954SCole Faust return total + bias_value;
116*c217d954SCole Faust }
117*c217d954SCole Faust
118*c217d954SCole Faust template < typename T, typename TB, ARM_COMPUTE_REQUIRES_TA(std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value) >
calculate_conv3d(const SimpleTensor<T> & src,const SimpleTensor<T> & weights,const SimpleTensor<TB> & bias,const Size3D & dilation,int batch,int z_start,int y_start,int x_start,int ch_out,UniformQuantizationInfo oq_info)119*c217d954SCole Faust T calculate_conv3d(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const Size3D &dilation, int batch,
120*c217d954SCole Faust int z_start, int y_start, int x_start, int ch_out, UniformQuantizationInfo oq_info)
121*c217d954SCole Faust {
122*c217d954SCole Faust const unsigned int weights_width = weights.shape()[weights_width_dim];
123*c217d954SCole Faust const unsigned int weights_height = weights.shape()[weights_height_dim];
124*c217d954SCole Faust const unsigned int weights_depth = weights.shape()[weights_depth_dim];
125*c217d954SCole Faust
126*c217d954SCole Faust const unsigned int src_channels = src.shape()[channel_dim];
127*c217d954SCole Faust const unsigned int src_width = src.shape()[width_dim];
128*c217d954SCole Faust const unsigned int src_height = src.shape()[height_dim];
129*c217d954SCole Faust const unsigned int src_depth = src.shape()[depth_dim];
130*c217d954SCole Faust
131*c217d954SCole Faust const UniformQuantizationInfo iq_info = src.quantization_info().uniform();
132*c217d954SCole Faust const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
133*c217d954SCole Faust
134*c217d954SCole Faust const int input_offset = -iq_info.offset;
135*c217d954SCole Faust const float input_scale = iq_info.scale;
136*c217d954SCole Faust int weights_offset = -wq_info.offset;
137*c217d954SCole Faust float weights_scale = wq_info.scale;
138*c217d954SCole Faust const int output_offset = oq_info.offset;
139*c217d954SCole Faust const float output_scale = oq_info.scale;
140*c217d954SCole Faust
141*c217d954SCole Faust int output_multiplier = 0;
142*c217d954SCole Faust int output_shift = 0;
143*c217d954SCole Faust const float multiplier = input_scale * weights_scale / output_scale;
144*c217d954SCole Faust arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
145*c217d954SCole Faust
146*c217d954SCole Faust int32_t total(0);
147*c217d954SCole Faust for(unsigned int weight_d = 0; weight_d < weights_depth; ++weight_d)
148*c217d954SCole Faust {
149*c217d954SCole Faust const int idx_z = z_start + dilation.depth * weight_d;
150*c217d954SCole Faust for(unsigned int weight_y = 0; weight_y < weights_height; ++weight_y)
151*c217d954SCole Faust {
152*c217d954SCole Faust const int idx_y = y_start + dilation.height * weight_y;
153*c217d954SCole Faust for(unsigned int weight_x = 0; weight_x < weights_width; ++weight_x)
154*c217d954SCole Faust {
155*c217d954SCole Faust const int idx_x = x_start + dilation.width * weight_x;
156*c217d954SCole Faust
157*c217d954SCole Faust //Check if the point is within padding
158*c217d954SCole Faust const bool is_x_valid = is_valid_pixel(idx_x, 0, src_width);
159*c217d954SCole Faust const bool is_y_valid = is_valid_pixel(idx_y, 0, src_height);
160*c217d954SCole Faust const bool is_z_valid = is_valid_pixel(idx_z, 0, src_depth);
161*c217d954SCole Faust const bool is_invalid_pixel = !(is_x_valid && is_y_valid && is_z_valid);
162*c217d954SCole Faust if(is_invalid_pixel)
163*c217d954SCole Faust {
164*c217d954SCole Faust continue;
165*c217d954SCole Faust }
166*c217d954SCole Faust
167*c217d954SCole Faust for(unsigned int ch_in = 0; ch_in < src_channels; ++ch_in)
168*c217d954SCole Faust {
169*c217d954SCole Faust const T *in_ptr = src.data();
170*c217d954SCole Faust const T *w_ptr = weights.data();
171*c217d954SCole Faust
172*c217d954SCole Faust const int in_offset = coord2index(src.shape(), Coordinates{ ch_in, idx_x, idx_y, idx_z, batch });
173*c217d954SCole Faust const int weight_offset = coord2index(weights.shape(), Coordinates{ ch_out, ch_in, weight_x, weight_y, weight_d });
174*c217d954SCole Faust T input_value = in_ptr[in_offset];
175*c217d954SCole Faust T weight_value = w_ptr[weight_offset];
176*c217d954SCole Faust total += ((input_value + input_offset) * (weight_value + weights_offset));
177*c217d954SCole Faust }
178*c217d954SCole Faust }
179*c217d954SCole Faust }
180*c217d954SCole Faust }
181*c217d954SCole Faust
182*c217d954SCole Faust const TB *b_ptr = bias.data();
183*c217d954SCole Faust TB bias_value = b_ptr[ch_out];
184*c217d954SCole Faust
185*c217d954SCole Faust total += bias_value;
186*c217d954SCole Faust
187*c217d954SCole Faust return validation::quantize_down_scale_by_fixedpoint(total, output_multiplier, output_shift, output_offset,
188*c217d954SCole Faust std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
189*c217d954SCole Faust }
190*c217d954SCole Faust } // namespace
191*c217d954SCole Faust
192*c217d954SCole Faust template <typename T, typename TB>
conv3d(const SimpleTensor<T> & src,const SimpleTensor<T> & weights,const SimpleTensor<TB> & bias,SimpleTensor<T> & dst,const Conv3dInfo & conv3d_info)193*c217d954SCole Faust SimpleTensor<T> conv3d(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &dst, const Conv3dInfo &conv3d_info)
194*c217d954SCole Faust {
195*c217d954SCole Faust // Compute reference
196*c217d954SCole Faust const unsigned int batch_size = src.shape()[batch_dim];
197*c217d954SCole Faust const unsigned int dst_width = dst.shape()[width_dim];
198*c217d954SCole Faust const unsigned int dst_height = dst.shape()[height_dim];
199*c217d954SCole Faust const unsigned int dst_depth = dst.shape()[depth_dim];
200*c217d954SCole Faust const unsigned int src_channels = src.shape()[channel_dim];
201*c217d954SCole Faust const unsigned int weights_out_ch = weights.shape()[weights_CHout_dim];
202*c217d954SCole Faust const unsigned int dst_channels = dst.shape()[channel_dim];
203*c217d954SCole Faust const size_t pad_left = conv3d_info.padding.left;
204*c217d954SCole Faust const size_t pad_top = conv3d_info.padding.top;
205*c217d954SCole Faust const size_t pad_front = conv3d_info.padding.front;
206*c217d954SCole Faust const size_t stride_x = conv3d_info.stride.x();
207*c217d954SCole Faust const size_t stride_y = conv3d_info.stride.y();
208*c217d954SCole Faust const size_t stride_z = conv3d_info.stride.z();
209*c217d954SCole Faust
210*c217d954SCole Faust const TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_conv3d_shape(src.shape(), weights.shape(), conv3d_info);
211*c217d954SCole Faust
212*c217d954SCole Faust ARM_COMPUTE_UNUSED(src_channels, weights_out_ch, dst_channels, dst_shape, weights_CHin_dim);
213*c217d954SCole Faust // Number of batches of source and destination tensors must match.
214*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.shape()[batch_dim] != dst.shape()[batch_dim]);
215*c217d954SCole Faust // Input channels in the source and weights must match.
216*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src_channels != weights.shape()[weights_CHin_dim]);
217*c217d954SCole Faust // Weight channels in the destination and weights must match.
218*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(weights_out_ch != dst_channels);
219*c217d954SCole Faust // Bias must match the number of destination channels.
220*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(bias.shape()[0] != dst_channels);
221*c217d954SCole Faust // Compare given dst tensor shape with expected shape.
222*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(dst.shape() != dst_shape);
223*c217d954SCole Faust
224*c217d954SCole Faust for(unsigned int batch = 0; batch < batch_size; ++batch)
225*c217d954SCole Faust {
226*c217d954SCole Faust for(unsigned int z_out = 0; z_out < dst_depth; ++z_out)
227*c217d954SCole Faust {
228*c217d954SCole Faust const int z_start = (z_out * stride_z) - pad_front;
229*c217d954SCole Faust for(unsigned int y_out = 0; y_out < dst_height; ++y_out)
230*c217d954SCole Faust {
231*c217d954SCole Faust const int y_start = (y_out * stride_y) - pad_top;
232*c217d954SCole Faust for(unsigned int x_out = 0; x_out < dst_width; ++x_out)
233*c217d954SCole Faust {
234*c217d954SCole Faust const int x_start = (x_out * stride_x) - pad_left;
235*c217d954SCole Faust for(unsigned int ch_out = 0; ch_out < dst_channels; ++ch_out)
236*c217d954SCole Faust {
237*c217d954SCole Faust T *out_ptr = dst.data();
238*c217d954SCole Faust
239*c217d954SCole Faust const int out_offset = coord2index(dst.shape(), Coordinates{ ch_out, x_out, y_out, z_out, batch });
240*c217d954SCole Faust out_ptr[out_offset] = calculate_conv3d<T, TB>(src, weights, bias, conv3d_info.dilation, batch, z_start, y_start, x_start, ch_out, dst.quantization_info().uniform());
241*c217d954SCole Faust }
242*c217d954SCole Faust }
243*c217d954SCole Faust }
244*c217d954SCole Faust }
245*c217d954SCole Faust }
246*c217d954SCole Faust return dst;
247*c217d954SCole Faust }
248*c217d954SCole Faust
249*c217d954SCole Faust template SimpleTensor<float> conv3d(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, SimpleTensor<float> &dst,
250*c217d954SCole Faust const Conv3dInfo &conv3d_info);
251*c217d954SCole Faust template SimpleTensor<half> conv3d(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, SimpleTensor<half> &dst,
252*c217d954SCole Faust const Conv3dInfo &conv3d_info);
253*c217d954SCole Faust template SimpleTensor<uint8_t> conv3d(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, SimpleTensor<uint8_t> &dst,
254*c217d954SCole Faust const Conv3dInfo &conv3d_info);
255*c217d954SCole Faust template SimpleTensor<int8_t> conv3d(const SimpleTensor<int8_t> &src, const SimpleTensor<int8_t> &weights, const SimpleTensor<int32_t> &bias, SimpleTensor<int8_t> &dst,
256*c217d954SCole Faust const Conv3dInfo &conv3d_info);
257*c217d954SCole Faust } // namespace reference
258*c217d954SCole Faust } // namespace validation
259*c217d954SCole Faust } // namespace test
260*c217d954SCole Faust } // namespace arm_compute