Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | #ifndef __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ |
| 25 | #define __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
| 29 | #include "arm_compute/core/CL/kernels/CLCol2ImKernel.h" |
| 30 | #include "arm_compute/core/CL/kernels/CLFillBorderKernel.h" |
| 31 | #include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h" |
| 32 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h" |
| 33 | #include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h" |
| 34 | #include "arm_compute/core/CL/kernels/CLIm2ColKernel.h" |
| 35 | #include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h" |
| 36 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | #include "arm_compute/runtime/CL/CLTensor.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 39 | #include "arm_compute/runtime/IMemoryManager.h" |
| 40 | |
| 41 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | class ICLTensor; |
| 46 | |
| 47 | /** Function to reshape and transpose the weights. This function calls the following kernels: |
| 48 | * -# @ref CLWeightsReshapeKernel |
| 49 | * -# @ref CLGEMMTranspose1xWKernel |
| 50 | */ |
| 51 | class CLConvolutionLayerReshapeWeights : public IFunction |
| 52 | { |
| 53 | public: |
| 54 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 55 | CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | /** Set the input and output tensors. |
| 57 | * |
Gian Marco Iodice | 7d323a6 | 2017-07-05 20:05:23 +0100 | [diff] [blame] | 58 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QS16/F16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 60 | * @param[out] output Destination tensor. Data types supported: Same as @p weights. |
| 61 | * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise. |
| 62 | * Data types supported: Same as @p weights. |
| 63 | */ |
| 64 | void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW); |
| 65 | // Inherited methods overridden: |
| 66 | void run() override; |
| 67 | |
| 68 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 69 | CLMemoryGroup _memory_group; |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 70 | CLWeightsReshapeKernel _weights_reshape_kernel; |
| 71 | CLGEMMTranspose1xWKernel _weights_transposed_kernel; |
| 72 | CLTensor _weights_reshaped; |
| 73 | bool _transpose1xW; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels: |
| 77 | * |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 78 | * -# @ref CLWeightsReshapeKernel (executed only once for each configuration) |
| 79 | * -# @ref CLGEMMTranspose1xWKernel (executed only once for each configuration) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | * -# @ref CLIm2ColKernel |
| 81 | * -# @ref CLGEMMInterleave4x4Kernel |
| 82 | * -# @ref CLGEMMMatrixMultiplyKernel |
| 83 | * -# @ref CLCol2ImKernel |
| 84 | */ |
| 85 | class CLConvolutionLayer : public IFunction |
| 86 | { |
| 87 | public: |
| 88 | /** Default constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 89 | CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | /** Set the input and output tensors. |
| 91 | * |
| 92 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 93 | * while every optional dimension from 4 and above represent a batch of inputs. |
Gian Marco Iodice | 7d323a6 | 2017-07-05 20:05:23 +0100 | [diff] [blame] | 94 | * Data types supported: QS8/QS16/F16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 96 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported:Same as @p input. |
| 97 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 98 | * Data types supported: Same as @p input. |
| 99 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 100 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
| 101 | * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
| 102 | */ |
| 103 | void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo()); |
| 104 | |
| 105 | // Inherited methods overridden: |
| 106 | void run() override; |
| 107 | |
| 108 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 109 | CLMemoryGroup _memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | CLConvolutionLayerReshapeWeights _reshape_weights; |
| 111 | CLIm2ColKernel _input_im2col_kernel; |
| 112 | CLGEMMInterleave4x4Kernel _input_interleave_kernel; |
| 113 | CLGEMMMatrixMultiplyKernel _mm_kernel; |
| 114 | CLCol2ImKernel _output_col2im_kernel; |
| 115 | CLTensor _input_im2col_reshaped; |
| 116 | CLTensor _input_interleaved_reshaped; |
| 117 | CLTensor _weights_reshaped; |
| 118 | CLTensor _weights_transposed; |
| 119 | CLTensor _gemm_output; |
| 120 | bool _has_bias; |
| 121 | bool _is_fully_connected_convolution; |
| 122 | bool _are_weights_reshaped; |
| 123 | }; |
| 124 | } |
| 125 | #endif /* __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ */ |