blob: 1a260edc9668e27392e4ab4a2e70b2addad48e36 [file] [log] [blame]
Manuel Bottini29599d02021-07-06 15:01:35 +01001/*
Giorgio Arena5ae8d802021-11-18 18:02:13 +00002 * Copyright (c) 2017-2022 Arm Limited.
Manuel Bottini29599d02021-07-06 15:01:35 +01003 *
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_CPU_WEIGHTSRESHAPE_KERNEL_H
25#define ARM_COMPUTE_CPU_WEIGHTSRESHAPE_KERNEL_H
26
27#include "src/core/common/Macros.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010028#include "src/cpu/ICpuKernel.h"
Manuel Bottini29599d02021-07-06 15:01:35 +010029
30namespace arm_compute
31{
32namespace cpu
33{
34namespace kernels
35{
36/** Kernel to perform reshaping on the weights used by convolution and locally connected layer
37 *
38 * Rearranges each 3-dimensional kernel to a single row leading to a matrix with linearized kernels.
39 * In combination with the @ref cpu::kernels::CpuIm2ColKernel can transform a convolution to a matrix multiplication.
40 *
41 * For example assuming a 3D weight kernel of 3x3 dimensions and depth of 2 we have:
42 * @f[
43 * \left( \begin{array}{ccc}
44 * a000 & a001 & a002 \\
45 * a010 & a011 & a012 \\
46 * a020 & a021 & a022 \\
47 * \end{array} \right)
48 * \left( \begin{array}{ccc}
49 * a100 & a101 & a102 \\
50 * a110 & a111 & a112 \\
51 * a120 & a121 & a122 \\
52 * \end{array} \right)
53 * \rightarrow
54 * \left( \begin{array}{ccccccccc}
55 * a000 & a001 & a002 & a010 & a011 & a012 & a020 & a021 & a022 & a100 & a101 & a102 & a110 & a111 & a112 & a120 & a121 & a122 \\
56 * \end{array} \right)
57 * @f]
58 */
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +020059class CpuWeightsReshapeKernel : public ICpuKernel<CpuWeightsReshapeKernel>
Manuel Bottini29599d02021-07-06 15:01:35 +010060{
61public:
62 /** Default constructor */
63 CpuWeightsReshapeKernel() = default;
64 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuWeightsReshapeKernel);
65 /** Set the input and output of the kernel.
66 *
67 * @param[in] src The input tensor info to convert. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM] if shared,
68 * and 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches] if unshared.
69 * Data types supported: All
70 * @param[in] biases The shared biases tensor info to append. Bias is 1D tensor with dimensions [OFM] if shared and 2D tensor with
71 * dimensions [OFM, num_patches] if unshared. Data types supported: Same as @p input
72 * @warning Appending biases to weights reshaped matrix is not supported for quantized asymmetric types.
73 * @param[out] dst The output tensor info. Data types supported: Same as @p src
74 */
75 void configure(const ITensorInfo *src, const ITensorInfo *biases, ITensorInfo *dst);
76 /** Static function to check if given info will lead to a valid configuration
77 *
78 * Similar to CpuWeightsReshapeKernel::configure()
79 *
80 * @return a status
81 */
82 static Status validate(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst);
83
84 // Inherited methods overridden:
85 void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
86 const char *name() const override;
87};
88} // namespace kernels
89} // namespace cpu
90} // namespace arm_compute
91#endif /* ARM_COMPUTE_CPU_WEIGHTSRESHAPE_KERNEL_H */