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() |
| 40 | : _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void GCConvolutionLayerReshapeWeights::configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, bool transpose1xW) |
| 45 | { |
| 46 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F16, DataType::F32); |
| 47 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
| 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()); |
| 59 | const unsigned bias_element = (append_biases) ? 1 : 0; |
| 60 | const IGCTensor *biases_to_use = (append_biases) ? biases : nullptr; |
| 61 | |
| 62 | _transpose1xW = transpose1xW; |
| 63 | |
| 64 | if(transpose1xW) |
| 65 | { |
| 66 | // Create tensor to store the reshaped weights |
| 67 | const unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 68 | const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element; |
| 69 | TensorShape shape_wr(mat_weights_cols, mat_weights_rows); |
| 70 | const DataType dt = weights->info()->data_type(); |
| 71 | const int fixed_point_position = weights->info()->fixed_point_position(); |
| 72 | TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position); |
| 73 | |
| 74 | _weights_reshaped.allocator()->init(info_wr); |
| 75 | _weights_reshape_kernel.configure(weights, biases_to_use, &_weights_reshaped); |
| 76 | _weights_transposed_kernel.configure(&_weights_reshaped, output); |
| 77 | _weights_reshaped.allocator()->allocate(); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | _weights_reshape_kernel.configure(weights, biases_to_use, output); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void GCConvolutionLayerReshapeWeights::run() |
| 86 | { |
| 87 | GCScheduler::get().dispatch(_weights_reshape_kernel); |
| 88 | if(_transpose1xW) |
| 89 | { |
| 90 | GCScheduler::get().dispatch(_weights_transposed_kernel); |
| 91 | } |
| 92 | } |
| 93 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 94 | GCConvolutionLayer::GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 95 | : _memory_group(std::move(memory_manager)), _reshape_weights(), _input_im2col_kernel(), _input_interleave_kernel(), _mm_kernel(), _output_col2im_kernel(), _fill_border(), _input_im2col_reshaped(), |
| 96 | _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _tmp_output(), _append_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false) |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
| 100 | void GCConvolutionLayer::configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output, bool is_interleaved_transposed) |
| 101 | { |
| 102 | _mm_kernel.configure(input, weights, output, 1.f, is_interleaved_transposed); |
| 103 | } |
| 104 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame^] | 105 | void GCConvolutionLayer::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
| 106 | const Size2D &dilation) |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 107 | { |
| 108 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 109 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 110 | ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2)); |
| 111 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
| 112 | |
| 113 | if(biases != nullptr) |
| 114 | { |
| 115 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 116 | ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3)); |
| 117 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 118 | } |
| 119 | |
| 120 | const DataType dt = input->info()->data_type(); |
| 121 | |
| 122 | _append_bias = (biases != nullptr); |
| 123 | _are_weights_reshaped = weights_info.are_reshaped(); |
| 124 | |
| 125 | const unsigned bias_element = (_append_bias) ? 1 : 0; |
| 126 | const IGCTensor *biases_to_use = (_append_bias) ? biases : nullptr; |
| 127 | |
| 128 | // Get parameters from conv_info |
| 129 | unsigned int stride_x = 0; |
| 130 | unsigned int stride_y = 0; |
| 131 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 132 | |
| 133 | // Get convolved dimensions |
| 134 | unsigned int conv_w = 0; |
| 135 | unsigned int conv_h = 0; |
| 136 | |
| 137 | const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0); |
| 138 | const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1); |
| 139 | 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^] | 140 | conv_info, dilation); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 141 | |
| 142 | // Check if its a "fully connected" convolution |
| 143 | _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1)); |
| 144 | const bool run_interleaved = (!_is_fully_connected_convolution); |
| 145 | |
| 146 | unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 147 | unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element; |
| 148 | |
| 149 | // Reshape weights if needed |
| 150 | if(_are_weights_reshaped) |
| 151 | { |
| 152 | if(_is_fully_connected_convolution) |
| 153 | { |
| 154 | mat_weights_cols = weights->info()->dimension(0); |
| 155 | mat_weights_rows = weights->info()->dimension(1); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | mat_weights_cols = weights_info.num_kernels(); |
| 160 | const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4; |
| 161 | mat_weights_rows = quarter_reshaped_cols + bias_element; |
| 162 | } |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | if(_is_fully_connected_convolution) |
| 167 | { |
| 168 | // Create tensor to store the reshaped weights |
| 169 | int num_elems_read_per_iteration_x = 1; |
| 170 | if(dt == DataType::F16) |
| 171 | { |
| 172 | num_elems_read_per_iteration_x = 2; |
| 173 | } |
| 174 | TensorShape shape_wr((ceil_to_multiple(mat_weights_cols, num_elems_read_per_iteration_x)), mat_weights_rows); |
| 175 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_wr)); |
| 176 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, false /* 1xW transpose */); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | // Create tensor to store transposed weights |
| 181 | const float transpose_width = 16.0f / input->info()->element_size(); |
| 182 | TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width))); |
| 183 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_wt)); |
| 184 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, true /* 1xW transpose */); |
| 185 | } |
| 186 | weights = &_weights_reshaped; |
| 187 | } |
| 188 | |
| 189 | // Create tensor to store im2col reshaped inputs |
| 190 | const unsigned int mat_input_cols = mat_weights_rows; |
| 191 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 192 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 193 | shape_im2col.set(0, mat_input_cols); |
| 194 | shape_im2col.set(1, mat_input_rows); |
| 195 | shape_im2col.set(2, 1); |
| 196 | |
| 197 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 198 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position()); |
| 199 | _input_im2col_reshaped.allocator()->init(im2col_reshaped_info); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 200 | _memory_group.manage(&_input_im2col_reshaped); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 201 | |
| 202 | // Create tensor (interleave) to prepare input tensor for GEMM |
| 203 | if(run_interleaved) |
| 204 | { |
| 205 | TensorShape shape_interleaved = shape_im2col; |
| 206 | shape_interleaved.set(0, shape_interleaved.x() * 4); |
| 207 | shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f)); |
| 208 | |
| 209 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 210 | TensorInfo interleaved_info(shape_interleaved, 1, dt, input->info()->fixed_point_position()); |
| 211 | _input_interleaved_reshaped.allocator()->init(interleaved_info); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 212 | _memory_group.manage(&_input_interleaved_reshaped); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Create GEMM output tensor |
| 216 | TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape(); |
| 217 | shape_gemm.set(0, mat_weights_cols); |
| 218 | shape_gemm.set(1, mat_input_rows); |
| 219 | const DataType gemm_data_type = dt; |
| 220 | |
| 221 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 222 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position()); |
| 223 | _gemm_output.allocator()->init(info_gemm); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 224 | _memory_group.manage(&_gemm_output); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 225 | |
| 226 | // Configure kernels |
| 227 | if(dt == DataType::F16) |
| 228 | { |
| 229 | BorderSize border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left()); |
| 230 | input->info()->extend_padding(border_size); |
| 231 | _fill_border.configure(input, border_size, BorderMode::CONSTANT, PixelValue(0)); // for PAD of im2col fp16: consider it as border |
| 232 | } |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame^] | 233 | _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] | 234 | |
| 235 | // Configure matrix multiply |
| 236 | if(run_interleaved) |
| 237 | { |
| 238 | _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped); |
| 239 | configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output); |
| 240 | _input_interleaved_reshaped.allocator()->allocate(); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, false); |
| 245 | } |
| 246 | _input_im2col_reshaped.allocator()->allocate(); |
| 247 | |
| 248 | // Configure Col2Im |
| 249 | _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h)); |
| 250 | _gemm_output.allocator()->allocate(); |
| 251 | |
| 252 | 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"); |
| 253 | |
| 254 | // Allocate intermediate tensor |
| 255 | if(!_are_weights_reshaped) |
| 256 | { |
| 257 | _weights_reshaped.allocator()->allocate(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void GCConvolutionLayer::run() |
| 262 | { |
| 263 | // Run weights reshaping (Runs once for every configure) |
| 264 | if(!_are_weights_reshaped) |
| 265 | { |
| 266 | _are_weights_reshaped = true; |
| 267 | _reshape_weights.run(); |
| 268 | } |
| 269 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 270 | _memory_group.acquire(); |
| 271 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 272 | // Run im2col |
| 273 | GCScheduler::get().dispatch(_fill_border); |
| 274 | GCScheduler::get().memory_barrier(); |
| 275 | GCScheduler::get().dispatch(_input_im2col_kernel); |
| 276 | |
| 277 | if(!_is_fully_connected_convolution) |
| 278 | { |
| 279 | GCScheduler::get().memory_barrier(); |
| 280 | // Run interleave4x4 |
| 281 | GCScheduler::get().dispatch(_input_interleave_kernel); |
| 282 | } |
| 283 | |
| 284 | GCScheduler::get().memory_barrier(); |
| 285 | // Runs matrix multiply on reshaped matrices |
| 286 | GCScheduler::get().dispatch(_mm_kernel); |
| 287 | |
| 288 | GCScheduler::get().memory_barrier(); |
| 289 | // Reshape output matrix |
| 290 | GCScheduler::get().dispatch(_output_col2im_kernel, false); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 291 | |
| 292 | _memory_group.release(); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 293 | } |