• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-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_NEACCUMULATEKERNEL_H
25 #define ARM_COMPUTE_NEACCUMULATEKERNEL_H
26 
27 #include "src/core/NEON/INESimpleKernel.h"
28 
29 #include <cstdint>
30 
31 namespace arm_compute
32 {
33 class ITensor;
34 
35 /** Interface for the accumulate kernel
36  *
37  * Accumulation is computed by:
38  * @f[ accum(x,y) = accum(x,y) + input(x,y) @f]
39  */
40 class NEAccumulateKernel : public INESimpleKernel
41 {
42 public:
name()43     const char *name() const override
44     {
45         return "NEAccumulateKernel";
46     }
47     /** Default constructor */
48     NEAccumulateKernel() = default;
49     /** Prevent instances of this class from being copied (As this class contains pointers) */
50     NEAccumulateKernel(const NEAccumulateKernel &) = delete;
51     /** Prevent instances of this class from being copied (As this class contains pointers) */
52     NEAccumulateKernel &operator=(const NEAccumulateKernel &) = delete;
53     /** Allow instances of this class to be moved */
54     NEAccumulateKernel(NEAccumulateKernel &&) = default;
55     /** Allow instances of this class to be moved */
56     NEAccumulateKernel &operator=(NEAccumulateKernel &&) = default;
57     /** Default destructor */
58     ~NEAccumulateKernel() = default;
59     /** Set the input and accumulation tensors
60      *
61      * @param[in]  input Source tensor. Data type supported: U8.
62      * @param[out] accum Destination tensor. Data type supported: S16.
63      */
64     void configure(const ITensor *input, ITensor *accum);
65 
66     // Inherited methods overridden:
67     void run(const Window &window, const ThreadInfo &info) override;
68 };
69 
70 /** Interface for the accumulate weighted kernel
71  *
72  * Weighted accumulation is computed:
73  * @f[ accum(x,y) = (1 - \alpha)*accum(x,y) + \alpha*input(x,y) @f]
74  *
75  * Where @f$ 0 \le \alpha \le 1 @f$
76  * Conceptually, the rounding for this is defined as:
77  * @f[ output(x,y)= uint8( (1 - \alpha) * float32( int32( output(x,y) ) ) + \alpha * float32( int32( input(x,y) ) ) ) @f]
78 */
79 class NEAccumulateWeightedKernel : public INESimpleKernel
80 {
81 public:
name()82     const char *name() const override
83     {
84         return "NEAccumulateWeightedKernel";
85     }
86     /** Default constructor */
87     NEAccumulateWeightedKernel();
88     /** Prevent instances of this class from being copied (As this class contains pointers) */
89     NEAccumulateWeightedKernel(const NEAccumulateWeightedKernel &) = delete;
90     /** Prevent instances of this class from being copied (As this class contains pointers) */
91     NEAccumulateWeightedKernel &operator=(const NEAccumulateWeightedKernel &) = delete;
92     /** Allow instances of this class to be moved */
93     NEAccumulateWeightedKernel(NEAccumulateWeightedKernel &&) = default;
94     /** Allow instances of this class to be moved */
95     NEAccumulateWeightedKernel &operator=(NEAccumulateWeightedKernel &&) = default;
96     /** Default destructor */
97     ~NEAccumulateWeightedKernel() = default;
98     /** Set the input and accumulation tensors, and the scale value
99      *
100      * @param[in]     input Source tensor. Data type supported: U8.
101      * @param[in]     alpha Scalar value in the range [0.0f, 1.0f]
102      * @param[in,out] accum Accumulated tensor. Data type supported: U8.
103      */
104     void configure(const ITensor *input, float alpha, ITensor *accum);
105 
106     // Inherited methods overridden:
107     void run(const Window &window, const ThreadInfo &info) override;
108 
109 protected:
110     float _alpha;
111 };
112 
113 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
114 /** Interface for the accumulate weighted kernel using F16 */
115 class NEAccumulateWeightedFP16Kernel : public NEAccumulateWeightedKernel
116 {
117 public:
name()118     const char *name() const override
119     {
120         return "NEAccumulateWeightedFP16Kernel";
121     }
122     /** Default constructor */
123     NEAccumulateWeightedFP16Kernel() = default;
124     /** Prevent instances of this class from being copied (As this class contains pointers) */
125     NEAccumulateWeightedFP16Kernel(const NEAccumulateWeightedFP16Kernel &) = delete;
126     /** Prevent instances of this class from being copied (As this class contains pointers) */
127     NEAccumulateWeightedFP16Kernel &operator=(const NEAccumulateWeightedFP16Kernel &) = delete;
128     /** Allow instances of this class to be moved */
129     NEAccumulateWeightedFP16Kernel(NEAccumulateWeightedFP16Kernel &&) = default;
130     /** Allow instances of this class to be moved */
131     NEAccumulateWeightedFP16Kernel &operator=(NEAccumulateWeightedFP16Kernel &&) = default;
132     /** Default destructor */
133     ~NEAccumulateWeightedFP16Kernel() = default;
134     // Inherited methods overridden:
135     void run(const Window &window, const ThreadInfo &info) override;
136 };
137 #else  /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
138 /** Interface for the accumulate weighted kernel using F16 */
139 using NEAccumulateWeightedFP16Kernel = NEAccumulateWeightedKernel;
140 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
141 
142 /** Interface for the accumulate squared kernel
143  *
144  * The accumulation of squares is computed:
145  * @f[ accum(x,y) = saturate_{int16} ( (uint16) accum(x,y) + (((uint16)(input(x,y)^2)) >> (shift)) ) @f]
146  *
147  * Where @f$ 0 \le shift \le 15 @f$
148 */
149 class NEAccumulateSquaredKernel : public INESimpleKernel
150 {
151 public:
name()152     const char *name() const override
153     {
154         return "NEAccumulateSquaredKernel";
155     }
156     /** Default constructor */
157     NEAccumulateSquaredKernel();
158     /** Prevent instances of this class from being copied (As this class contains pointers) */
159     NEAccumulateSquaredKernel(const NEAccumulateSquaredKernel &) = delete;
160     /** Prevent instances of this class from being copied (As this class contains pointers) */
161     NEAccumulateSquaredKernel &operator=(const NEAccumulateSquaredKernel &) = delete;
162     /** Allow instances of this class to be moved */
163     NEAccumulateSquaredKernel(NEAccumulateSquaredKernel &&) = default;
164     /** Allow instances of this class to be moved */
165     NEAccumulateSquaredKernel &operator=(NEAccumulateSquaredKernel &&) = default;
166     /** Default destructor */
167     ~NEAccumulateSquaredKernel() = default;
168     /** Set the input and accumulation tensors and the shift value.
169      *
170      * @param[in]     input Source tensor. Data type supported: U8.
171      * @param[in]     shift Shift value in the range of [0, 15]
172      * @param[in,out] accum Accumulated tensor. Data type supported: S16.
173      */
174     void configure(const ITensor *input, uint32_t shift, ITensor *accum);
175 
176     // Inherited methods overridden:
177     void run(const Window &window, const ThreadInfo &info) override;
178 
179 private:
180     uint32_t _shift;
181 };
182 } // namespace arm_compute
183 #endif /*ARM_COMPUTE_NEACCUMULATEKERNEL_H */
184