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