Compute Library
 21.05
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 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  * Valid data layouts:
69  * - All
70  *
71  * Valid data type configurations:
72  * |src0 |src1 |src2 |dst |
73  * |:--------------|:--------------|:--------------|:--------------|
74  * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
75  * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
76  * |F16 |F16 |F16 |F16 |
77  * |F32 |F32 |F32 |F32 |
78  * |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 |
79  *
80  * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
81  * while every optional dimension from 4 and above represent a batch of inputs.
82  * Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
83  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
84  * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
85  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
86  * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
87  * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
88  * Data types supported: Same as @p input.
89  * @param[in] info Convolution layer descriptor
90  */
91  void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const Conv2dInfo &info);
92  /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConv2d
93  *
94  * @param[in] input Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
95  * while every optional dimension from 4 and above represent a batch of inputs.
96  * Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
97  * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
98  * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
99  * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
100  * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
101  * @param[in] output Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
102  * Data types supported: Same as @p input.
103  * @param[in] info Contains padding and stride information described in @ref PadStrideInfo.
104  *
105  * @return a status
106  */
107  static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &info);
108 
109  // Inherited methods overridden:
110  void run() override;
111  void prepare() override;
112 
113 private:
114  std::unique_ptr<NEGEMMAssemblyDispatch> _gemm_asm_func;
115  NEActivationLayer _activation_func;
116  NEPermute _weights_permute_func;
117  const ITensor *_original_weights;
118  Tensor _permuted_weights;
119  bool _is_prepared;
120  bool _run_activation;
121 };
122 } // namespace arm_compute
123 #endif /* ARM_COMPUTE_NEGEMMCONV2D_H */
Base class for all functions.
Definition: IFunction.h:30
Store the tensor'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 CPU 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.