Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | #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" |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 39 | #include "arm_compute/runtime/CL/functions/CLGEMM.h" |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 40 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" |
| 41 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 42 | #include "arm_compute/runtime/IMemoryManager.h" |
| 43 | |
| 44 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | |
| 46 | namespace arm_compute |
| 47 | { |
| 48 | class ICLTensor; |
| 49 | |
| 50 | /** Function to reshape and transpose the weights. This function calls the following kernels: |
| 51 | * -# @ref CLWeightsReshapeKernel |
| 52 | * -# @ref CLGEMMTranspose1xWKernel |
| 53 | */ |
| 54 | class CLConvolutionLayerReshapeWeights : public IFunction |
| 55 | { |
| 56 | public: |
| 57 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 58 | CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | /** Set the input and output tensors. |
| 60 | * |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 61 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 62 | * Data type supported: QS8/QASYMM8/QS16/F16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 64 | * @param[out] output Destination tensor. Data types supported: Same as @p weights. |
| 65 | * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise. |
| 66 | * Data types supported: Same as @p weights. |
| 67 | */ |
| 68 | void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW); |
| 69 | // Inherited methods overridden: |
| 70 | void run() override; |
| 71 | |
| 72 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 73 | CLMemoryGroup _memory_group; |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 74 | CLWeightsReshapeKernel _weights_reshape_kernel; |
| 75 | CLGEMMTranspose1xWKernel _weights_transposed_kernel; |
| 76 | CLTensor _weights_reshaped; |
| 77 | bool _transpose1xW; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 80 | /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | * |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 82 | * Note: weights already reshaped for quantized asymmetric is not supported |
| 83 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | * -# @ref CLIm2ColKernel |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 85 | * -# @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 86 | * -# @ref CLGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | * -# @ref CLCol2ImKernel |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 88 | * |
| 89 | * if the weights are already reshaped: |
| 90 | * -# @ref CLGEMMInterleave4x4Kernel |
| 91 | * -# @ref CLGEMMMatrixMultiplyKernel |
| 92 | * else |
| 93 | * -# @ref CLGEMM |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | */ |
| 95 | class CLConvolutionLayer : public IFunction |
| 96 | { |
| 97 | public: |
| 98 | /** Default constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 99 | CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | /** Set the input and output tensors. |
| 101 | * |
| 102 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 103 | * while every optional dimension from 4 and above represent a batch of inputs. |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 104 | * Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 105 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 106 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 107 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 109 | * Data types supported: Same as @p input. |
| 110 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
Giorgio Arena | 3972528 | 2017-12-12 15:04:43 +0000 | [diff] [blame] | 111 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
| 112 | * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | */ |
| 114 | void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo()); |
| 115 | |
| 116 | // Inherited methods overridden: |
| 117 | void run() override; |
| 118 | |
| 119 | private: |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 120 | /** Configures the appropriate matrix multiply routine |
| 121 | * |
| 122 | * @param input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 123 | * @param weights Weights tensor. Data type supported: Same as @p input. |
| 124 | * @param output Output tensor. Data types supported: Same as @p input, |
| 125 | * except for input of QASYMM8 type where output should be of S32 type. |
| 126 | * @param is_interleaved_transposed Flag that signals if matrix is interleaved transposed |
| 127 | */ |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 128 | void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool is_interleaved_transposed, bool are_weights_reshaped); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | CLMemoryGroup _memory_group; |
| 132 | CLConvolutionLayerReshapeWeights _reshape_weights; |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 133 | CLIm2ColKernel _im2col_kernel; |
| 134 | CLGEMMInterleave4x4Kernel _interleave_kernel; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 135 | CLGEMMMatrixMultiplyKernel _mm_kernel; |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 136 | CLGEMM _mm_gemm; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 137 | CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
| 138 | CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 139 | CLCol2ImKernel _col2im_kernel; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 140 | |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 141 | CLTensor _im2col_output; |
| 142 | CLTensor _interleave_output; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 143 | CLTensor _weights_reshaped; |
| 144 | CLTensor _weights_transposed; |
| 145 | CLTensor _gemm_output; |
| 146 | CLTensor _tmp_output; |
| 147 | |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 148 | bool _are_weights_reshaped; |
| 149 | bool _is_quantized; |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 150 | bool _is_interleaved_transposed; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | }; |
| 152 | } |
| 153 | #endif /* __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ */ |