Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 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 | |
| 25 | #include "arm_compute/runtime/GLES_COMPUTE/functions/GCConvolutionLayer.h" |
| 26 | |
| 27 | #include "arm_compute/core/PixelValue.h" |
| 28 | #include "arm_compute/core/Size2D.h" |
| 29 | #include "arm_compute/core/Utils.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" |
| 32 | |
| 33 | #include <cmath> |
| 34 | #include <memory> |
| 35 | #include <tuple> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | GCConvolutionLayerReshapeWeights::GCConvolutionLayerReshapeWeights() |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 40 | : _weights_reshape_kernel() |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 44 | void GCConvolutionLayerReshapeWeights::configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output) |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 45 | { |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 47 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F16, DataType::F32); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 48 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
| 49 | |
| 50 | if(biases != nullptr) |
| 51 | { |
| 52 | ARM_COMPUTE_ERROR_ON(is_data_type_quantized_asymmetric(weights->info()->data_type())); |
| 53 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 54 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3)); |
| 55 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 56 | } |
| 57 | |
| 58 | const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type()); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 59 | const IGCTensor *biases_to_use = (append_biases) ? biases : nullptr; |
| 60 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 61 | _weights_reshape_kernel.configure(weights, biases_to_use, output); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void GCConvolutionLayerReshapeWeights::run() |
| 65 | { |
| 66 | GCScheduler::get().dispatch(_weights_reshape_kernel); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 69 | GCConvolutionLayer::GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 70 | : _memory_group(std::move(memory_manager)), _reshape_weights(), _input_im2col_kernel(), _mm_gemm(), _output_col2im_kernel(), _fill_border(), _activationlayer_function(), _original_weights(nullptr), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 71 | _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _tmp_output(), _is_activationlayer_enabled(false), _is_prepared(false) |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 75 | void GCConvolutionLayer::configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output) |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 76 | { |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
| 78 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info())); |
| 79 | |
| 80 | _mm_gemm.configure(input, weights, nullptr, output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */)); |
| 81 | } |
| 82 | |
| 83 | Status GCConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output) |
| 84 | { |
| 85 | // Perform validation step on Matrix multiply function |
| 86 | GCGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */)); |
| 87 | return Status{}; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 90 | void GCConvolutionLayer::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 91 | const Size2D &dilation, const ActivationLayerInfo &act_info) |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 92 | { |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 93 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 94 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 95 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 96 | ARM_COMPUTE_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
| 97 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2)); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 98 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
| 99 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 100 | _is_prepared = false; |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 101 | _original_weights = weights; |
| 102 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 103 | if(biases != nullptr) |
| 104 | { |
| 105 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 106 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3)); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 108 | } |
| 109 | |
| 110 | const DataType dt = input->info()->data_type(); |
| 111 | |
Michele Di Giorgio | b8fc60f | 2018-04-25 11:58:07 +0100 | [diff] [blame] | 112 | // Set the GPU target for im2col and col2im |
| 113 | _input_im2col_kernel.set_target(GCScheduler::get().get_target()); |
| 114 | _output_col2im_kernel.set_target(GCScheduler::get().get_target()); |
| 115 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 116 | const bool append_bias = (biases != nullptr); |
| 117 | const unsigned bias_element = (append_bias) ? 1 : 0; |
| 118 | const IGCTensor *biases_to_use = (append_bias) ? biases : nullptr; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 119 | |
| 120 | // Get parameters from conv_info |
| 121 | unsigned int stride_x = 0; |
| 122 | unsigned int stride_y = 0; |
| 123 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 124 | |
| 125 | // Get convolved dimensions |
| 126 | unsigned int conv_w = 0; |
| 127 | unsigned int conv_h = 0; |
| 128 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 129 | const unsigned int kernel_width = weights->info()->dimension(0); |
| 130 | const unsigned int kernel_height = weights->info()->dimension(1); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 131 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 132 | conv_info, dilation); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 133 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 134 | unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 135 | unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element; |
| 136 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 137 | // _weights_reshaped will be auto configured in the kernel. |
| 138 | // Just append biases and do not transpose 1xW as it will be reshaped in GCGEMM |
| 139 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped); |
| 140 | |
| 141 | weights = &_weights_reshaped; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 142 | |
| 143 | // Create tensor to store im2col reshaped inputs |
| 144 | const unsigned int mat_input_cols = mat_weights_rows; |
| 145 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 146 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 147 | shape_im2col.set(0, mat_input_cols); |
| 148 | shape_im2col.set(1, mat_input_rows); |
| 149 | shape_im2col.set(2, 1); |
| 150 | |
| 151 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 152 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 153 | _input_im2col_reshaped.allocator()->init(im2col_reshaped_info); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 154 | _memory_group.manage(&_input_im2col_reshaped); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 155 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 156 | // Create GEMM output tensor |
| 157 | TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape(); |
| 158 | shape_gemm.set(0, mat_weights_cols); |
| 159 | shape_gemm.set(1, mat_input_rows); |
| 160 | const DataType gemm_data_type = dt; |
| 161 | |
| 162 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 163 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 164 | _gemm_output.allocator()->init(info_gemm); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 165 | _memory_group.manage(&_gemm_output); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 166 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 167 | if(dt == DataType::F16) |
| 168 | { |
| 169 | BorderSize border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left()); |
| 170 | input->info()->extend_padding(border_size); |
| 171 | _fill_border.configure(input, border_size, BorderMode::CONSTANT, PixelValue(0)); // for PAD of im2col fp16: consider it as border |
| 172 | } |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 173 | // Configure im2col |
| 174 | _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 175 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 176 | // Configure GEMM |
| 177 | configure_mm(&_input_im2col_reshaped, weights, &_gemm_output); |
| 178 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 179 | _input_im2col_reshaped.allocator()->allocate(); |
| 180 | |
| 181 | // Configure Col2Im |
| 182 | _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h)); |
| 183 | _gemm_output.allocator()->allocate(); |
| 184 | |
| 185 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one"); |
| 186 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 187 | //Configure Activation Layer |
| 188 | _is_activationlayer_enabled = act_info.enabled(); |
| 189 | |
| 190 | if(_is_activationlayer_enabled) |
| 191 | { |
| 192 | _activationlayer_function.configure(output, nullptr, act_info); |
| 193 | } |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 194 | |
| 195 | ARM_COMPUTE_UNUSED(weights_info); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void GCConvolutionLayer::run() |
| 199 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 200 | prepare(); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 201 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 202 | _memory_group.acquire(); |
| 203 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 204 | // Run im2col |
| 205 | GCScheduler::get().dispatch(_fill_border); |
| 206 | GCScheduler::get().memory_barrier(); |
| 207 | GCScheduler::get().dispatch(_input_im2col_kernel); |
| 208 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 209 | // Run gemm on reshaped matrices |
| 210 | _mm_gemm.run(); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 211 | GCScheduler::get().memory_barrier(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 212 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 213 | // Reshape output matrix |
| 214 | GCScheduler::get().dispatch(_output_col2im_kernel, false); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 215 | GCScheduler::get().memory_barrier(); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 216 | |
| 217 | _memory_group.release(); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 218 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 219 | // Run Activation Layer |
| 220 | if(_is_activationlayer_enabled) |
| 221 | { |
| 222 | _activationlayer_function.run(); |
| 223 | } |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 224 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 225 | |
| 226 | void GCConvolutionLayer::prepare() |
| 227 | { |
| 228 | if(!_is_prepared) |
| 229 | { |
| 230 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 231 | |
| 232 | // Run weights reshaping and mark as unused |
| 233 | _weights_reshaped.allocator()->allocate(); |
| 234 | _reshape_weights.run(); |
| 235 | |
| 236 | // Mark original weights tensor as unused |
| 237 | _original_weights->mark_as_unused(); |
| 238 | |
| 239 | _is_prepared = true; |
| 240 | } |
| 241 | } |