Compute Library
 21.02
NEGEMMConv2d.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021 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 
33 
34 #include <memory>
35 namespace arm_compute
36 {
37 // Forward declarations
38 class ITensor;
39 class NEGEMMAssemblyDispatch;
40 
41 /** 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  */
51 class NEGEMMConv2d : public IFunction
52 {
53 public:
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;
64  /** Destructor */
65  ~NEGEMMConv2d();
66  /** 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 
101 private:
102  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;
109 };
110 } // namespace arm_compute
111 #endif /* ARM_COMPUTE_NEGEMMCONV2D_H */
Base class for all functions.
Definition: IFunction.h:30
~NEGEMMConv2d()
Destructor.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Basic function to run cpu::kernels::CpuPermuteKernel.
Definition: NEPermute.h:40
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &info)
Static function to check if given info will lead to a valid configuration of NEGEMMConv2d.
NEGEMMConv2d & operator=(const NEGEMMConv2d &)=delete
Prevent instances of this class from being copied (As this class contains pointers) ...
Basic implementation of the tensor interface.
Definition: Tensor.h:37
Descriptor used by the Convolution function.
Basic function to run cpu::kernels::CpuActivationKernel.
void run() override
Run the kernels contained in the function.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
NEGEMMConv2d(const std::shared_ptr< IMemoryManager > &memory_manager=nullptr)
Constructor.
Basic function to compute the convolution layer.
Definition: NEGEMMConv2d.h:51
void prepare() override
Prepare the function for executing.
void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const Conv2dInfo &info)
Set the input and output tensors.