Adnan AlSinan | e4563a0 | 2021-09-01 15:32:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. |
| 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" |
| 25 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 26 | |
| 27 | // Source/Destination Tensor shape indices (N D H W C) |
| 28 | constexpr unsigned int batch_dim = 4u; |
| 29 | constexpr unsigned int depth_dim = 3u; |
| 30 | constexpr unsigned int height_dim = 2u; |
| 31 | constexpr unsigned int width_dim = 1u; |
| 32 | constexpr unsigned int channel_dim = 0u; |
| 33 | |
| 34 | // Weight tensor shape indices (D H W Cin Cout) |
| 35 | constexpr unsigned int weights_depth_dim = 4u; |
| 36 | constexpr unsigned int weights_height_dim = 3u; |
| 37 | constexpr unsigned int weights_width_dim = 2u; |
| 38 | constexpr unsigned int weights_CHin_dim = 1u; |
| 39 | constexpr unsigned int weights_CHout_dim = 0u; |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace test |
| 44 | { |
| 45 | namespace validation |
| 46 | { |
| 47 | namespace reference |
| 48 | { |
| 49 | namespace |
| 50 | { |
| 51 | inline bool is_valid_pixel(int i, int min, int max) |
| 52 | { |
| 53 | return (i >= min && i < max); |
| 54 | } |
| 55 | // Evaluate the weights against an element in a given tensor. |
| 56 | template <typename T> |
| 57 | T calculate_conv3d(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const Size3D &dilation, int batch, |
| 58 | int z_start, int y_start, int x_start, int ch_out) |
| 59 | { |
| 60 | const unsigned int weights_width = weights.shape()[weights_width_dim]; |
| 61 | const unsigned int weights_height = weights.shape()[weights_height_dim]; |
| 62 | const unsigned int weights_depth = weights.shape()[weights_depth_dim]; |
| 63 | |
| 64 | const unsigned int src_channels = src.shape()[channel_dim]; |
| 65 | const unsigned int src_width = src.shape()[width_dim]; |
| 66 | const unsigned int src_height = src.shape()[height_dim]; |
| 67 | const unsigned int src_depth = src.shape()[depth_dim]; |
| 68 | |
| 69 | T total(0); |
| 70 | for(unsigned int weight_d = 0; weight_d < weights_depth; ++weight_d) |
| 71 | { |
| 72 | const int idx_z = z_start + dilation.depth * weight_d; |
| 73 | for(unsigned int weight_y = 0; weight_y < weights_height; ++weight_y) |
| 74 | { |
| 75 | const int idx_y = y_start + dilation.height * weight_y; |
| 76 | for(unsigned int weight_x = 0; weight_x < weights_width; ++weight_x) |
| 77 | { |
| 78 | const int idx_x = x_start + dilation.width * weight_x; |
| 79 | |
| 80 | //Check if the point is within padding |
| 81 | const bool is_x_valid = is_valid_pixel(idx_x, 0, src_width); |
| 82 | const bool is_y_valid = is_valid_pixel(idx_y, 0, src_height); |
| 83 | const bool is_z_valid = is_valid_pixel(idx_z, 0, src_depth); |
| 84 | const bool is_invalid_pixel = !(is_x_valid && is_y_valid && is_z_valid); |
| 85 | if(is_invalid_pixel) |
| 86 | { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | for(unsigned int ch_in = 0; ch_in < src_channels; ++ch_in) |
| 91 | { |
| 92 | const T *in_ptr = src.data(); |
| 93 | const T *w_ptr = weights.data(); |
| 94 | |
| 95 | const int in_offset = coord2index(src.shape(), Coordinates{ ch_in, idx_x, idx_y, idx_z, batch }); |
| 96 | const int weight_offset = coord2index(weights.shape(), Coordinates{ ch_out, ch_in, weight_x, weight_y, weight_d }); |
| 97 | T input_value = in_ptr[in_offset]; |
| 98 | T weight_value = w_ptr[weight_offset]; |
| 99 | total += (input_value * weight_value); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return total; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | template <typename T> |
| 109 | SimpleTensor<T> conv3d(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<T> &bias, SimpleTensor<T> &dst, const Conv3dInfo &conv3d_info) |
| 110 | { |
| 111 | // Compute reference |
| 112 | const unsigned int batch_size = src.shape()[batch_dim]; |
| 113 | const unsigned int dst_width = dst.shape()[width_dim]; |
| 114 | const unsigned int dst_height = dst.shape()[height_dim]; |
| 115 | const unsigned int dst_depth = dst.shape()[depth_dim]; |
| 116 | const unsigned int src_channels = src.shape()[channel_dim]; |
| 117 | const unsigned int weights_out_ch = weights.shape()[weights_CHout_dim]; |
| 118 | const unsigned int dst_channels = dst.shape()[channel_dim]; |
| 119 | const size_t pad_left = conv3d_info.padding.left; |
| 120 | const size_t pad_top = conv3d_info.padding.top; |
| 121 | const size_t pad_front = conv3d_info.padding.front; |
| 122 | const size_t stride_x = conv3d_info.stride.x(); |
| 123 | const size_t stride_y = conv3d_info.stride.y(); |
| 124 | const size_t stride_z = conv3d_info.stride.z(); |
| 125 | |
| 126 | const TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_conv3d_shape(src.shape(), weights.shape(), conv3d_info); |
| 127 | |
Adnan AlSinan | 2ec6163 | 2021-09-16 11:49:35 +0100 | [diff] [blame] | 128 | 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] | 129 | // Number of batches of source and destination tensors must match. |
| 130 | ARM_COMPUTE_ERROR_ON(src.shape()[batch_dim] != dst.shape()[batch_dim]); |
| 131 | // Input channels in the source and weights must match. |
| 132 | ARM_COMPUTE_ERROR_ON(src_channels != weights.shape()[weights_CHin_dim]); |
| 133 | // Weight channels in the destination and weights must match. |
| 134 | ARM_COMPUTE_ERROR_ON(weights_out_ch != dst_channels); |
| 135 | // Bias must match the number of destination channels. |
| 136 | ARM_COMPUTE_ERROR_ON(bias.shape()[0] != dst_channels); |
| 137 | // Compare given dst tensor shape with expected shape. |
| 138 | ARM_COMPUTE_ERROR_ON(dst.shape() != dst_shape); |
| 139 | |
| 140 | for(unsigned int batch = 0; batch < batch_size; ++batch) |
| 141 | { |
| 142 | for(unsigned int z_out = 0; z_out < dst_depth; ++z_out) |
| 143 | { |
Giorgio Arena | 5c002ec | 2021-10-12 16:00:40 +0100 | [diff] [blame] | 144 | const int z_start = (z_out * stride_z) - pad_front; |
Adnan AlSinan | e4563a0 | 2021-09-01 15:32:03 +0100 | [diff] [blame] | 145 | for(unsigned int y_out = 0; y_out < dst_height; ++y_out) |
| 146 | { |
| 147 | const int y_start = (y_out * stride_y) - pad_top; |
| 148 | for(unsigned int x_out = 0; x_out < dst_width; ++x_out) |
| 149 | { |
Giorgio Arena | 5c002ec | 2021-10-12 16:00:40 +0100 | [diff] [blame] | 150 | const int x_start = (x_out * stride_x) - pad_left; |
Adnan AlSinan | e4563a0 | 2021-09-01 15:32:03 +0100 | [diff] [blame] | 151 | for(unsigned int ch_out = 0; ch_out < dst_channels; ++ch_out) |
| 152 | { |
| 153 | T weighted_value = calculate_conv3d<T>(src, weights, conv3d_info.dilation, batch, z_start, |
| 154 | y_start, x_start, ch_out); |
| 155 | T *out_ptr = dst.data(); |
| 156 | const T *b_ptr = bias.data(); |
| 157 | T bias_value(0); |
| 158 | const int out_offset = coord2index(dst.shape(), Coordinates{ ch_out, x_out, y_out, z_out, batch }); |
| 159 | bias_value = b_ptr[ch_out]; |
| 160 | out_ptr[out_offset] = weighted_value + bias_value; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return dst; |
| 167 | } |
| 168 | |
| 169 | template SimpleTensor<float> conv3d(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, SimpleTensor<float> &dst, |
| 170 | const Conv3dInfo &conv3d_info); |
| 171 | template SimpleTensor<half> conv3d(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, SimpleTensor<half> &dst, |
| 172 | const Conv3dInfo &conv3d_info); |
| 173 | } // namespace reference |
| 174 | } // namespace validation |
| 175 | } // namespace test |
| 176 | } // namespace arm_compute |