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 | #ifndef __ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H__ |
| 25 | #define __ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/core/CL/kernels/CLWinogradFilterTransformKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLWinogradOutputTransformKernel.h" |
| 29 | #include "arm_compute/core/Types.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/functions/CLActivationLayer.h" |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 31 | #include "arm_compute/runtime/CL/functions/CLGEMM.h" |
| 32 | #include "arm_compute/runtime/CL/functions/CLWinogradInputTransform.h" |
| 33 | #include "arm_compute/runtime/IFunction.h" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | class ICLTensor; |
| 38 | |
| 39 | /** Basic function to execute Winograd-based convolution on OpenCL. This function calls the following OpenCL functions/kernels: |
| 40 | * |
| 41 | * -# @ref CLWinogradInputTransform |
| 42 | * -# @ref CLWinogradFilterTransformKernel (only once) |
| 43 | * -# @ref CLGEMM |
| 44 | * -# @ref CLWinogradOutputTransformKernel |
| 45 | * |
| 46 | */ |
| 47 | class CLWinogradConvolutionLayer : public IFunction |
| 48 | { |
| 49 | public: |
| 50 | /** Default constructor */ |
| 51 | CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 52 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 53 | CLWinogradConvolutionLayer(const CLWinogradConvolutionLayer &) = delete; |
| 54 | /** Default move constructor */ |
| 55 | CLWinogradConvolutionLayer(CLWinogradConvolutionLayer &&) = default; |
| 56 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 57 | CLWinogradConvolutionLayer &operator=(const CLWinogradConvolutionLayer &) = delete; |
| 58 | /** Default move assignment operator */ |
| 59 | CLWinogradConvolutionLayer &operator=(CLWinogradConvolutionLayer &&) = default; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 60 | /** Set the input and output tensors. |
| 61 | * |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame^] | 62 | * @note: This function only works with 3x3,3x1,1x3,5x5,5x1 and 1x5 kernels along with unit strides for NCHW data layout |
| 63 | * @note: This function only works with 3x3, 3x1, 1x3 and 5x5 kernels along with unit strides for NHWC data layout |
| 64 | * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 65 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 66 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 67 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 68 | * Data types supported: F32. |
| 69 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 70 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input |
| 71 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 72 | * Data types supported: Same as @p input. |
| 73 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 74 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 75 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 76 | * available which may introduce a drop of accuracy as well. Default is false |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 77 | */ |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 78 | void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 79 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 80 | /** Static function to check if given info will lead to a valid configuration of @ref CLWinogradConvolutionLayer |
| 81 | * |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame^] | 82 | * @note: This function only works with 3x3,3x1,1x3,5x5,5x1 and 1x5 kernels along with unit strides for NCHW data layout |
| 83 | * @note: This function only works with 3x3 and 5x5 kernels along with unit strides for NHWC data layout |
| 84 | * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 85 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 86 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 87 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 88 | * Data types supported: F32. |
| 89 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 90 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input |
| 91 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 92 | * Data types supported: Same as @p input. |
| 93 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 94 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 95 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 96 | * available which may introduce a drop of accuracy as well. Default is false |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 97 | * |
| 98 | * @return a status |
| 99 | */ |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 100 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 101 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 102 | |
| 103 | // Inherited methods overridden: |
| 104 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 105 | void prepare() override; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | CLMemoryGroup _memory_group; |
| 109 | CLGEMM _batched_mm; |
| 110 | CLWinogradInputTransform _input_transform; |
| 111 | CLWinogradFilterTransformKernel _filter_transform; |
| 112 | CLWinogradOutputTransformKernel _output_transform; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 113 | CLActivationLayer _activationlayer_function; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 114 | CLTensor _input0; |
| 115 | CLTensor _input1; |
| 116 | CLTensor _batched_mm_output; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 117 | const ICLTensor *_original_weights; |
| 118 | bool _is_prepared; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 119 | bool _is_activationlayer_enabled; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 120 | }; |
| 121 | } |
| 122 | #endif /* __ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H__ */ |