Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +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 "arm_compute/graph/nodes/ConvolutionLayer.h" |
| 25 | |
Michalis Spyrou | ed194b1 | 2017-10-31 15:04:34 +0000 | [diff] [blame] | 26 | #include "arm_compute/graph/Error.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/functions/CLDirectConvolutionLayer.h" |
| 29 | #include "arm_compute/runtime/IFunction.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h" |
Georgios Pinitas | 7ef9018 | 2018-02-05 19:58:15 +0000 | [diff] [blame] | 32 | #include "arm_compute/runtime/NEON/functions/NEWinogradLayer.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 33 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 34 | #include "utils/GraphTypePrinter.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 35 | #include "utils/TypePrinter.h" |
| 36 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 37 | #include <tuple> |
| 38 | #include <vector> |
| 39 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 40 | using namespace arm_compute::graph; |
| 41 | |
| 42 | namespace |
| 43 | { |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 44 | /** Calculates the output shaped of the convolution layer |
| 45 | * |
| 46 | * @param[in] input_shape Input tensor shape |
| 47 | * @param[in] weights_shape Weights shape |
| 48 | * @param[in] conv_info Convolution information (padding, stride, etc.) |
| 49 | * |
| 50 | * @return The expected output tensor shape |
| 51 | */ |
| 52 | TensorShape calculate_convolution_layer_output_shape(const TensorShape &input_shape, const TensorShape &weights_shape, const PadStrideInfo &conv_info) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 53 | { |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 54 | unsigned int output_width = 0; |
| 55 | unsigned int output_height = 0; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 56 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 57 | // Get output width and height |
| 58 | std::tie(output_width, output_height) = arm_compute::scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info); |
| 59 | |
| 60 | // Create output shape |
| 61 | TensorShape output_shape = input_shape; |
| 62 | output_shape.set(0, output_width); |
| 63 | output_shape.set(1, output_height); |
| 64 | output_shape.set(2, weights_shape[3]); |
| 65 | |
| 66 | return output_shape; |
| 67 | } |
| 68 | |
| 69 | // Instantiate GEMM based convolution layer |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 70 | template <typename ConvolutionType, typename TensorType, TargetHint target_hint> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 71 | std::unique_ptr<arm_compute::IFunction> instantiate_function(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output, |
| 72 | const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 73 | { |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 74 | auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>(); |
| 75 | conv->configure( |
| 76 | dynamic_cast<TensorType *>(input), |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 77 | dynamic_cast<TensorType *>(weights), |
| 78 | dynamic_cast<TensorType *>(biases), |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 79 | dynamic_cast<TensorType *>(output), |
| 80 | conv_info, weights_info); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 81 | return std::move(conv); |
| 82 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 83 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 84 | // Instantiate direct convolution layer |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 85 | template <typename ConvolutionType, typename TensorType, TargetHint target_hint> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 86 | std::unique_ptr<arm_compute::IFunction> instantiate_direct_function(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output, |
| 87 | const PadStrideInfo &conv_info) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 88 | { |
| 89 | auto conv = arm_compute::support::cpp14::make_unique<ConvolutionType>(); |
| 90 | conv->configure( |
| 91 | dynamic_cast<TensorType *>(input), |
| 92 | dynamic_cast<TensorType *>(weights), |
| 93 | dynamic_cast<TensorType *>(biases), |
| 94 | dynamic_cast<TensorType *>(output), |
| 95 | conv_info); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 96 | return std::move(conv); |
| 97 | } |
| 98 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 99 | template <TargetHint target_hint> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 100 | std::unique_ptr<arm_compute::IFunction> instantiate(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output, |
| 101 | const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 102 | ConvolutionMethodHint conv_method); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 103 | |
| 104 | template <> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 105 | std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output, |
| 106 | const PadStrideInfo &conv_info, |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 107 | const WeightsInfo &weights_info, |
| 108 | ConvolutionMethodHint conv_method) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 109 | { |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 110 | if((conv_method == ConvolutionMethodHint::DIRECT) |
| 111 | && arm_compute::CLDirectConvolutionLayer::validate(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), conv_info)) // NOLINT |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 112 | { |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 113 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLDirectConvolutionLayer"); |
| 114 | return instantiate_direct_function<arm_compute::CLDirectConvolutionLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, weights, biases, output, conv_info); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 118 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer"); |
| 119 | return instantiate_function<arm_compute::CLConvolutionLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, weights, biases, output, conv_info, weights_info); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 120 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | template <> |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 124 | std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(arm_compute::ITensor *input, arm_compute::ITensor *weights, arm_compute::ITensor *biases, arm_compute::ITensor *output, |
| 125 | const PadStrideInfo &conv_info, |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 126 | const WeightsInfo &weights_info, |
| 127 | ConvolutionMethodHint conv_method) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 128 | { |
Georgios Pinitas | 7ef9018 | 2018-02-05 19:58:15 +0000 | [diff] [blame] | 129 | const unsigned int kernel_size_x = weights->info()->tensor_shape().x(); |
| 130 | const unsigned int kernel_size_y = weights->info()->tensor_shape().y(); |
| 131 | const unsigned int conv_stride_x = conv_info.stride().first; |
| 132 | const unsigned int conv_stride_y = conv_info.stride().second; |
| 133 | |
| 134 | bool is_square_kernel = (kernel_size_x == kernel_size_y); |
| 135 | bool has_same_stride = (conv_stride_x == conv_stride_y); |
| 136 | |
| 137 | // TODO (COMPID-765) : Winograd should have a validate function |
| 138 | if(conv_method == ConvolutionMethodHint::WINOGRAD && is_square_kernel && ((kernel_size_x == 3) || (kernel_size_x == 5)) && has_same_stride && (conv_info.stride().first == 1)) |
| 139 | { |
| 140 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEWinogradConvolutionLayer"); |
| 141 | return instantiate_direct_function<arm_compute::NEWinogradLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output, conv_info); |
| 142 | } |
| 143 | else if((conv_method == ConvolutionMethodHint::DIRECT) |
| 144 | && arm_compute::NEDirectConvolutionLayer::validate(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), conv_info)) // NOLINT |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 145 | { |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 146 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEDirectConvolutionLayer"); |
| 147 | return instantiate_direct_function<arm_compute::NEDirectConvolutionLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output, conv_info); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 148 | } |
| 149 | else |
| 150 | { |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 151 | ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer"); |
| 152 | return instantiate_function<arm_compute::NEConvolutionLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output, conv_info, weights_info); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 153 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 154 | } |
| 155 | } // namespace |
| 156 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 157 | /** Grouped Convolution function */ |
| 158 | class GroupedConvolutionFunction final : public arm_compute::IFunction |
| 159 | { |
| 160 | public: |
| 161 | /** Default Constructor */ |
| 162 | GroupedConvolutionFunction() |
| 163 | : _convolutions() |
| 164 | { |
| 165 | } |
| 166 | /** Default Destructor */ |
| 167 | ~GroupedConvolutionFunction() final = default; |
| 168 | /** Prevent instances from being copy constructed */ |
| 169 | GroupedConvolutionFunction(const GroupedConvolutionFunction &) = delete; |
| 170 | /** Prevent instances from being copy assigned */ |
| 171 | GroupedConvolutionFunction &operator=(const GroupedConvolutionFunction &) = delete; |
| 172 | /** Allow instances to be move constructed */ |
| 173 | GroupedConvolutionFunction(GroupedConvolutionFunction &&) noexcept = default; |
| 174 | /** Allow instances to be move assigned */ |
| 175 | GroupedConvolutionFunction &operator=(GroupedConvolutionFunction &&) noexcept = default; |
| 176 | /** Adds a convolution |
| 177 | * |
| 178 | * @param convolution Convolution function to add |
| 179 | */ |
| 180 | void add_convolution_function(std::unique_ptr<IFunction> convolution) |
| 181 | { |
| 182 | _convolutions.emplace_back(std::move(convolution)); |
| 183 | } |
| 184 | |
| 185 | // Inherited methods overriden: |
| 186 | void run() override |
| 187 | { |
| 188 | for(auto &c : _convolutions) |
| 189 | { |
| 190 | c->run(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private: |
| 195 | std::vector<std::unique_ptr<IFunction>> _convolutions; |
| 196 | }; |
| 197 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 198 | std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 199 | { |
Michalis Spyrou | ed194b1 | 2017-10-31 15:04:34 +0000 | [diff] [blame] | 200 | ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 201 | |
| 202 | arm_compute::ITensor *in = input->tensor(); |
| 203 | arm_compute::ITensor *out = output->tensor(); |
| 204 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 205 | // Set weights and biases info |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 206 | if(_weights.tensor() == nullptr) |
| 207 | { |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 208 | TensorInfo info = TensorInfo(TensorShape(_conv_width, _conv_height, in->info()->dimension(2) / _num_groups, _ofm), |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 209 | in->info()->num_channels(), |
| 210 | in->info()->data_type(), |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 211 | in->info()->fixed_point_position()); |
| 212 | info.set_quantization_info(_weights_quant_info); |
| 213 | _weights.set_info(std::move(info)); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 214 | } |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 215 | if(_biases.has_accessor() && _biases.tensor() == nullptr) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 216 | { |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 217 | DataType dt = in->info()->data_type(); |
| 218 | _biases.set_info(TensorInfo(TensorShape(_ofm), in->info()->num_channels(), is_data_type_quantized_asymmetric(dt) ? DataType::S32 : dt, in->info()->fixed_point_position())); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | std::unique_ptr<arm_compute::IFunction> func; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 222 | _target_hint = ctx.hints().target_hint(); |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 223 | const ConvolutionMethodHint conv_method_hint = ctx.hints().convolution_method_hint(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 224 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 225 | // Check if the weights and biases are loaded |
| 226 | bool weights_are_loaded = _weights.tensor() != nullptr; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 227 | bool biases_are_loaded = _biases.has_accessor() ? _biases.tensor() != nullptr : true; |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 228 | |
| 229 | // Set bias and weights target |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 230 | _weights.set_target(_target_hint); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 231 | if(_biases.has_accessor()) |
| 232 | { |
| 233 | _biases.set_target(_target_hint); |
| 234 | } |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 235 | |
| 236 | // Calculate output shape |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 237 | TensorShape output_shape = calculate_convolution_layer_output_shape(in->info()->tensor_shape(), _weights.info().tensor_shape(), _conv_info); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 238 | |
| 239 | // Output auto inizialitation if not yet initialized |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 240 | arm_compute::auto_init_if_empty(*out->info(), output_shape, 1, in->info()->data_type(), in->info()->fixed_point_position(), |
| 241 | (_out_quant_info.empty()) ? in->info()->quantization_info() : _out_quant_info); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 242 | |
| 243 | // Create appropriate convolution function |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 244 | if(_num_groups == 1) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 245 | { |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 246 | func = instantiate_convolution(in, out, conv_method_hint); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | { |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 250 | func = instantiate_grouped_convolution(in, out, conv_method_hint); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Fill weights |
| 254 | if(!weights_are_loaded) |
| 255 | { |
| 256 | _weights.allocate_and_fill_if_needed(); |
| 257 | } |
| 258 | // Fill biases |
| 259 | if(!biases_are_loaded) |
| 260 | { |
| 261 | _biases.allocate_and_fill_if_needed(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 264 | ARM_COMPUTE_LOG_GRAPH_INFO(" Data Type: " << in->info()->data_type() |
| 265 | << " Input Shape: " << in->info()->tensor_shape() |
| 266 | << " Weights shape: " << _weights.info().tensor_shape() |
| 267 | << " Biases Shape: " << _biases.info().tensor_shape() |
| 268 | << " Output Shape: " << out->info()->tensor_shape() |
| 269 | << " PadStrideInfo: " << _conv_info |
| 270 | << " Groups: " << _num_groups |
| 271 | << " WeightsInfo: " << _weights_info |
| 272 | << std::endl); |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 273 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 274 | return func; |
| 275 | } |
| 276 | |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 277 | std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 278 | { |
| 279 | std::unique_ptr<arm_compute::IFunction> func; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 280 | if(_target_hint == TargetHint::OPENCL) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 281 | { |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 282 | func = instantiate<TargetHint::OPENCL>(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 283 | } |
| 284 | else |
| 285 | { |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 286 | func = instantiate<TargetHint::NEON>(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 287 | } |
| 288 | return func; |
| 289 | } |
| 290 | |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 291 | std::unique_ptr<arm_compute::IFunction> ConvolutionLayer::instantiate_grouped_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 292 | { |
| 293 | // Get tensor shapes |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 294 | TensorShape input_shape = input->info()->tensor_shape(); |
| 295 | TensorShape output_shape = output->info()->tensor_shape(); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 296 | TensorShape weights_shape = _weights.info().tensor_shape(); |
| 297 | TensorShape biases_shape = _biases.info().tensor_shape(); |
| 298 | |
| 299 | ARM_COMPUTE_ERROR_ON_MSG((input_shape.z() % _num_groups) != 0, "Input depth not multiple of the number of groups!"); |
| 300 | ARM_COMPUTE_ERROR_ON_MSG((output_shape.z() % _num_groups) != 0, "Output depth not multiple of the number of groups!"); |
| 301 | ARM_COMPUTE_ERROR_ON_MSG((weights_shape[3] % _num_groups) != 0, "Number of kernels not multiple of the number of groups!"); |
| 302 | ARM_COMPUTE_ERROR_ON_MSG((biases_shape.x() % _num_groups) != 0, "Biases not multiple of the number of groups!"); |
| 303 | |
| 304 | // Create a grouped convolution function |
| 305 | auto grouped_conv = arm_compute::support::cpp14::make_unique<GroupedConvolutionFunction>(); |
| 306 | |
| 307 | // Create sub-tensors vectors |
| 308 | _is = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups); |
| 309 | _os = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups); |
| 310 | _ws = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups); |
| 311 | _bs = arm_compute::support::cpp14::make_unique<SubTensor[]>(_num_groups); |
| 312 | |
| 313 | // Calculate sub-tensor splits |
| 314 | const int input_split = input_shape.z() / _num_groups; |
| 315 | const int output_split = output_shape.z() / _num_groups; |
| 316 | const int weights_split = weights_shape[3] / _num_groups; |
| 317 | const int biases_split = biases_shape.x() / _num_groups; |
| 318 | |
| 319 | // Calculate sub-tensor shapes |
| 320 | input_shape.set(2, input_split); |
| 321 | output_shape.set(2, output_split); |
| 322 | weights_shape.set(3, weights_split); |
| 323 | biases_shape.set(0, biases_split); |
| 324 | |
| 325 | // Configure sub-tensors |
| 326 | for(int i = 0; i < static_cast<int>(_num_groups); ++i) |
| 327 | { |
| 328 | // Create convolution function |
| 329 | std::unique_ptr<arm_compute::IFunction> func; |
| 330 | |
| 331 | // Calculate sub-tensors starting coordinates |
| 332 | Coordinates input_coord(0, 0, input_split * i); |
| 333 | Coordinates output_coord(0, 0, output_split * i); |
| 334 | Coordinates weights_coord(0, 0, 0, weights_split * i); |
| 335 | Coordinates biases_coord(biases_split * i); |
| 336 | |
| 337 | // Create sub-tensors for input, output, weights and bias |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 338 | auto hint_to_use = (_target_hint == TargetHint::OPENCL) ? TargetHint::OPENCL : TargetHint::NEON; |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 339 | _is[i] = SubTensor(input, input_shape, input_coord, hint_to_use); |
| 340 | _os[i] = SubTensor(output, output_shape, output_coord, hint_to_use); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 341 | _ws[i] = SubTensor(_weights.tensor(), weights_shape, weights_coord, hint_to_use); |
| 342 | _bs[i] = SubTensor(_biases.tensor(), biases_shape, biases_coord, hint_to_use); |
| 343 | |
| 344 | // Instantiate convolution function |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 345 | if(_target_hint == TargetHint::OPENCL) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 346 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 347 | func = instantiate<TargetHint::OPENCL>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 348 | } |
| 349 | else |
| 350 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 351 | func = instantiate<TargetHint::NEON>(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Add convolution function to the list of convolutions for the grouped convolution |
| 355 | grouped_conv->add_convolution_function(std::move(func)); |
| 356 | } |
| 357 | |
| 358 | return std::move(grouped_conv); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 359 | } |