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