Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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_NEWEIGHTSRESHAPEKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_NEWEIGHTSRESHAPEKERNEL_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | class ITensor; |
| 32 | |
| 33 | /** NEON kernel to perform reshaping on the weights used by convolution and locally connected layer |
| 34 | * |
| 35 | * Rearranges each 3-dimensional kernel to a single row leading to a matrix with linearized kernels. |
| 36 | * In combination with the @ref NEIm2ColKernel can transform a convolution to a matrix multiplication. |
| 37 | * |
| 38 | * For example assuming a 3D weight kernel of 3x3 dimensions and depth of 2 we have: |
| 39 | * @f[ |
| 40 | * \left( \begin{array}{ccc} |
| 41 | * a000 & a001 & a002 \\ |
| 42 | * a010 & a011 & a012 \\ |
| 43 | * a020 & a021 & a022 \\ |
| 44 | * \end{array} \right) |
| 45 | * \left( \begin{array}{ccc} |
| 46 | * a100 & a101 & a102 \\ |
| 47 | * a110 & a111 & a112 \\ |
| 48 | * a120 & a121 & a122 \\ |
| 49 | * \end{array} \right) |
| 50 | * \rightarrow |
| 51 | * \left( \begin{array}{ccccccccc} |
| 52 | * a000 & a001 & a002 & a010 & a011 & a012 & a020 & a021 & a022 & a100 & a101 & a102 & a110 & a111 & a112 & a120 & a121 & a122 \\ |
| 53 | * \end{array} \right) |
| 54 | * @f] |
| 55 | */ |
| 56 | class NEWeightsReshapeKernel : public INEKernel |
| 57 | { |
| 58 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 59 | const char *name() const override |
| 60 | { |
| 61 | return "NEWeightsReshapeKernel"; |
| 62 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | /** Constructor.*/ |
| 64 | NEWeightsReshapeKernel(); |
| 65 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 66 | NEWeightsReshapeKernel(const NEWeightsReshapeKernel &) = delete; |
| 67 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 68 | NEWeightsReshapeKernel &operator=(const NEWeightsReshapeKernel &) = delete; |
| 69 | /** Allow instances of this class to be moved */ |
| 70 | NEWeightsReshapeKernel(NEWeightsReshapeKernel &&) = default; |
| 71 | /** Allow instances of this class to be moved */ |
| 72 | NEWeightsReshapeKernel &operator=(NEWeightsReshapeKernel &&) = default; |
| 73 | /** Default destructor */ |
| 74 | ~NEWeightsReshapeKernel() = default; |
| 75 | /** Set the input and output of the kernel. |
| 76 | * |
| 77 | * @param[in] input The input tensor to convert. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM] if shared, |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 78 | * and 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches] if unshared. Data types supported: QASYMM8/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | * @param[in] bias The shared biases tensor to append. Bias is 1D tensor with dimensions [OFM] if shared and 2D tensor with |
| 80 | * dimensions [OFM, num_patches] if unshared. Data types supported: Same as @p input |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 81 | * @warning Appending biases to weights reshaped matrix is not supported for quantized asymmetric types. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | * @param[out] output The output tensor. Data types supported: Same as @p input |
| 83 | */ |
| 84 | void configure(const ITensor *input, const ITensor *bias, ITensor *output); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 85 | /** Static function to check if given info will lead to a valid configuration of @ref NEWeightsReshapeKernel |
| 86 | * |
| 87 | * @param[in] input The input tensor to convert. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM] if shared, |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 88 | * and 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches] if unshared. Data types supported: QASYMM8/F16/F32 |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 89 | * @param[in] biases The shared biases tensor to append. Bias is 1D tensor with dimensions [OFM] if shared and 2D tensor with |
| 90 | * dimensions [OFM, num_patches] if unshared. Data types supported: Same as @p input |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 91 | * @warning Appending biases to weights reshaped matrix is not supported for quantized asymmetric types. |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 92 | * @param[in] output The output tensor. Should be a 2D Tensor. Data types supported: Same as @p input |
| 93 | * |
| 94 | * @return a status |
| 95 | */ |
| 96 | static Status validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | |
| 98 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 99 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | |
| 101 | private: |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 102 | const ITensor *_input; |
| 103 | const ITensor *_bias; |
| 104 | ITensor *_output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 106 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | #endif /*__ARM_COMPUTE_NEWEIGHTSRESHAPEKERNEL_H__ */ |