blob: 2b3c162eab231ff40ad76d2f2d48f0777ba6e776 [file] [log] [blame]
Georgios Pinitasc0b6f762020-11-02 01:37:17 +00001/*
2 * Copyright (c) 2020 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_NEGEMMCONV2D_H
25#define ARM_COMPUTE_NEGEMMCONV2D_H
26
27#include "arm_compute/runtime/FunctionDescriptors.h"
28#include "arm_compute/runtime/IFunction.h"
29#include "arm_compute/runtime/IMemoryManager.h"
30#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000031#include "arm_compute/runtime/NEON/functions/NEPermute.h"
32#include "arm_compute/runtime/Tensor.h"
33
34#include <memory>
35namespace arm_compute
36{
37// Forward declarations
38class ITensor;
Georgios Pinitasec2256b2020-12-03 18:51:58 +000039class NEGEMMAssemblyDispatch;
40
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000041/** Basic function to compute the convolution layer. This function calls the following NEON kernels/functions:
42 *
43 * Supports only NHWC data layout
44 *
45 * -# @ref NEGEMMAssemblyDispatch
46 * -# @ref NEActivationLayer, in case activation cannot be fused in the assembly dispatch
47 *
48 * Weights are transformed from OHWI to HWIO format using the following kernels:
49 * -# @ref NEPermute
50 */
51class NEGEMMConv2d : public IFunction
52{
53public:
54 /** Constructor */
55 NEGEMMConv2d(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr);
56 /** Prevent instances of this class from being copied (As this class contains pointers) */
57 NEGEMMConv2d(const NEGEMMConv2d &) = delete;
58 /** Default move constructor */
59 NEGEMMConv2d(NEGEMMConv2d &&) = default;
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 NEGEMMConv2d &operator=(const NEGEMMConv2d &) = delete;
62 /** Default move assignment operator */
63 NEGEMMConv2d &operator=(NEGEMMConv2d &&) = default;
Georgios Pinitasec2256b2020-12-03 18:51:58 +000064 /** Destructor */
65 ~NEGEMMConv2d();
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000066 /** Set the input and output tensors.
67 *
68 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
69 * while every optional dimension from 4 and above represent a batch of inputs.
70 * Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
71 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
72 * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
73 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
74 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
75 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
76 * Data types supported: Same as @p input.
77 * @param[in] info Convolution layer descriptor
78 */
79 void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const Conv2dInfo &info);
80 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConv2d
81 *
82 * @param[in] input Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
83 * while every optional dimension from 4 and above represent a batch of inputs.
84 * Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
85 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
86 * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
87 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
88 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
89 * @param[in] output Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
90 * Data types supported: Same as @p input.
91 * @param[in] info Contains padding and stride information described in @ref PadStrideInfo.
92 *
93 * @return a status
94 */
95 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &info);
96
97 // Inherited methods overridden:
98 void run() override;
99 void prepare() override;
100
101private:
Georgios Pinitasec2256b2020-12-03 18:51:58 +0000102 std::unique_ptr<NEGEMMAssemblyDispatch> _gemm_asm_func;
103 NEActivationLayer _activation_func;
104 NEPermute _weights_permute_func;
105 const ITensor *_original_weights;
106 Tensor _permuted_weights;
107 bool _is_prepared;
108 bool _run_activation;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000109};
110} // namespace arm_compute
111#endif /* ARM_COMPUTE_NEGEMMCONV2D_H */