Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | #include "arm_compute/runtime/CL/functions/CLWinogradConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | |
| 32 | using namespace arm_compute; |
| 33 | |
| 34 | CLWinogradConvolutionLayer::CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 35 | : _memory_group(memory_manager), _batched_mm(memory_manager), _input_transform(), _filter_transform(), _output_transform(), _activationlayer_function(), _input0(), _input1(), _batched_mm_output(), |
| 36 | _is_first_run(true), _is_activationlayer_enabled(false) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 40 | void CLWinogradConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 41 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 42 | // Get indices for the width and height |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 43 | const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 44 | const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
| 45 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 46 | // Input shape |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame^] | 47 | const TensorShape input_shape = input->info()->tensor_shape(); |
| 48 | const unsigned int input_w = input->info()->tensor_shape()[idx_width]; |
| 49 | const unsigned int input_h = input->info()->tensor_shape()[idx_height]; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 50 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 51 | // Kernel size |
| 52 | const unsigned int kernel_w = weights->info()->tensor_shape()[idx_width]; |
| 53 | const unsigned int kernel_h = weights->info()->tensor_shape()[idx_height]; |
| 54 | |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame^] | 55 | //Winograd output tile |
| 56 | const Size2D output_tile = (Size2D(kernel_w, kernel_h) == Size2D(3U, 3U) && input_w <= 4 && input_h <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U); |
| 57 | |
| 58 | const WinogradInfo winograd_info = WinogradInfo(output_tile, |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 59 | Size2D(kernel_w, kernel_h), |
| 60 | Size2D(input_shape[idx_width], input_shape[idx_height]), |
| 61 | conv_info, |
| 62 | input->info()->data_layout()); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 63 | |
| 64 | // Manage intermediate tensors |
| 65 | _memory_group.manage(&_input0); |
| 66 | _memory_group.manage(&_batched_mm_output); |
| 67 | |
| 68 | // Do not manage _input1 as it contains the weights |
| 69 | |
| 70 | // Configure input transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 71 | _input_transform.configure(input, &_input0, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 72 | |
| 73 | // Configure filter transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 74 | _filter_transform.configure(weights, &_input1, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 75 | |
| 76 | // Configure batched matrix multiply |
| 77 | _batched_mm.configure(&_input0, &_input1, nullptr, &_batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)); |
| 78 | |
| 79 | // Configure output transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 80 | _output_transform.configure(&_batched_mm_output, biases, output, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 81 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 82 | // Configure activation layer |
| 83 | _is_activationlayer_enabled = act_info.enabled(); |
| 84 | if(_is_activationlayer_enabled) |
| 85 | { |
| 86 | _activationlayer_function.configure(output, nullptr, act_info); |
| 87 | } |
| 88 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 89 | // Allocate temporary tensors |
| 90 | _input0.allocator()->allocate(); |
| 91 | _input1.allocator()->allocate(); |
| 92 | _batched_mm_output.allocator()->allocate(); |
| 93 | } |
| 94 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 95 | Status CLWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 96 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 97 | { |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 98 | // Get indeces for the width and height |
| 99 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 100 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 101 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 102 | // Input shape |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame^] | 103 | const TensorShape input_shape = input->tensor_shape(); |
| 104 | const unsigned int input_w = input->tensor_shape()[idx_width]; |
| 105 | const unsigned int input_h = input->tensor_shape()[idx_height]; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 106 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 107 | // Kernel size |
| 108 | const unsigned int kernel_w = weights->tensor_shape()[idx_width]; |
| 109 | const unsigned int kernel_h = weights->tensor_shape()[idx_height]; |
| 110 | |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame^] | 111 | //Winograd output tile |
| 112 | const Size2D output_tile = (Size2D(kernel_w, kernel_h) == Size2D(3U, 3U) && input_w <= 4 && input_h <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U); |
| 113 | |
| 114 | const WinogradInfo winograd_info = WinogradInfo(output_tile, |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 115 | Size2D(kernel_w, kernel_h), |
| 116 | Size2D(input_shape[idx_width], input_shape[idx_height]), |
| 117 | conv_info, |
| 118 | input->data_layout()); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 119 | |
| 120 | // Validate input transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 121 | const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 122 | const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 123 | ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradInputTransform::validate(input, &input0, winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 124 | |
| 125 | // Validate filter transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 126 | const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 127 | const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 128 | ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradFilterTransformKernel::validate(weights, &input1, winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 129 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 130 | // Validate batched matrix multiply |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 131 | TensorShape batched_mm_output_shape = input0.tensor_shape(); |
| 132 | batched_mm_output_shape[0] = input1.tensor_shape()[0]; |
| 133 | const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape); |
| 134 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input0, &input1, nullptr, &batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/))); |
| 135 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 136 | // Configure output transform |
| 137 | ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradOutputTransformKernel::validate(&batched_mm_output, biases, output, winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 138 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 139 | // Validate Activation Layer |
| 140 | if(act_info.enabled()) |
| 141 | { |
| 142 | CLActivationLayer::validate(output, nullptr, act_info); |
| 143 | } |
| 144 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 145 | return Status{}; |
| 146 | } |
| 147 | |
| 148 | void CLWinogradConvolutionLayer::run() |
| 149 | { |
| 150 | if(_is_first_run) |
| 151 | { |
| 152 | // Run filter transform |
| 153 | CLScheduler::get().enqueue(_filter_transform, false); |
| 154 | |
| 155 | _is_first_run = false; |
| 156 | } |
| 157 | |
| 158 | _memory_group.acquire(); |
| 159 | |
| 160 | // Run input transform |
| 161 | _input_transform.run(); |
| 162 | |
| 163 | // Run batched matrix multiplication |
| 164 | _batched_mm.run(); |
| 165 | |
| 166 | // Run output transform |
| 167 | CLScheduler::get().enqueue(_output_transform); |
| 168 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 169 | if(_is_activationlayer_enabled) |
| 170 | { |
| 171 | _activationlayer_function.run(); |
| 172 | } |
| 173 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 174 | _memory_group.release(); |
| 175 | } |