blob: 3fbcee6c2109d072c6438559c9397af1a0bef7f4 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +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 */
Manuel Bottini7b9998d2019-10-21 17:59:07 +010024#ifndef ARM_COMPUTE_CLREDUCTIONOPERATION_H
25#define ARM_COMPUTE_CLREDUCTIONOPERATION_H
Michalis Spyrou04f089c2017-08-08 17:42:38 +010026
Michalis Spyrou04f089c2017-08-08 17:42:38 +010027#include "arm_compute/runtime/CL/CLTensor.h"
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010028#include "arm_compute/runtime/CL/functions/CLReshapeLayer.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010029#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010030#include "arm_compute/runtime/IMemoryManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010031#include "arm_compute/runtime/MemoryGroup.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010032
Michalis Spyrou04f089c2017-08-08 17:42:38 +010033#include <memory>
Michalis Spyrou04f089c2017-08-08 17:42:38 +010034
35namespace arm_compute
36{
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010037// Forward declarations
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010038class CLCompileContext;
39class CLFillBorderKernel;
40class CLReductionOperationKernel;
Michalis Spyrou04f089c2017-08-08 17:42:38 +010041class ICLTensor;
42
43/** Perform reduction operation.
44 */
45class CLReductionOperation : public IFunction
46{
47public:
Alex Gildayc357c472018-03-21 13:54:09 +000048 /** Default Constructor.
49 *
50 * @param[in] memory_manager (Optional) Memory manager.
51 */
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010052 CLReductionOperation(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010053 /** Default Destructor */
54 ~CLReductionOperation();
55 /** Prevent instances of this class from being copied (As this class contains pointers) */
56 CLReductionOperation(const CLReductionOperation &) = delete;
57 /** Default move constructor */
58 CLReductionOperation(CLReductionOperation &&) = default;
59 /** Prevent instances of this class from being copied (As this class contains pointers) */
60 CLReductionOperation &operator=(const CLReductionOperation &) = delete;
61 /** Default move assignment operator */
62 CLReductionOperation &operator=(CLReductionOperation &&) = default;
Michalis Spyrou04f089c2017-08-08 17:42:38 +010063
64 /** Set the input and output tensors.
65 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010066 * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32/S32.
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010067 * @param[out] output Destination tensor. Data types and data layouts supported: Same as @p input.
68 * @param[in] axis Axis along which to reduce. Supported reduction axis : 0, 1, 2, 3
Manuel Bottini7b9998d2019-10-21 17:59:07 +010069 * @param[in] op Reduction operation to perform. Operations supported: MEAN_SUM, PROD, SUM_SQUARE, SUM, MIN, MAX
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010070 * @param[in] keep_dims (Optional) Whether to keep the reduced dimension after the operation. Defaults to true.
Michalis Spyrou04f089c2017-08-08 17:42:38 +010071 */
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010072 void configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims = true);
Manuel Bottini2b84be52020-04-08 10:15:51 +010073 /** Set the input and output tensors.
74 *
75 * @param[in] compile_context The compile context to be used.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010076 * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32/S32.
Manuel Bottini2b84be52020-04-08 10:15:51 +010077 * @param[out] output Destination tensor. Data types and data layouts supported: Same as @p input.
78 * @param[in] axis Axis along which to reduce. Supported reduction axis : 0, 1, 2, 3
79 * @param[in] op Reduction operation to perform. Operations supported: MEAN_SUM, PROD, SUM_SQUARE, SUM, MIN, MAX
80 * @param[in] keep_dims (Optional) Whether to keep the reduced dimension after the operation. Defaults to true.
81 */
82 void configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, bool keep_dims = true);
Michalis Spyrou04f089c2017-08-08 17:42:38 +010083
John Richardson62385bc2018-04-20 13:11:36 +010084 /** Static function to check if given info will lead to a valid configuration of @ref CLReductionOperation.
85 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010086 * @param[in] input Source tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32/S32.
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010087 * @param[in] output Destination tensor info. Data types and data layouts supported: Same as @p input.
88 * @param[in] axis Axis along which to reduce. Supported reduction axis : 0, 1, 2, 3
Manuel Bottini7b9998d2019-10-21 17:59:07 +010089 * @param[in] op Reduction operation to perform. Operations supported: MEAN_SUM, PROD, SUM_SQUARE, SUM, MIN, MAX
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010090 * @param[in] keep_dims (Optional) Whether to keep the reduced dimension after the operation. Defaults to true.
John Richardson62385bc2018-04-20 13:11:36 +010091 *
92 * @return a status
93 */
Sang-Hoon Park2697fd82019-10-15 16:49:24 +010094 static Status validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims = true);
John Richardson62385bc2018-04-20 13:11:36 +010095
Michalis Spyrou04f089c2017-08-08 17:42:38 +010096 // Inherited methods overridden:
97 void run() override;
98
99private:
Sang-Hoon Park2697fd82019-10-15 16:49:24 +0100100 ICLTensor *configure_intermediate_result_vector(ICLTensor *input, ICLTensor *output);
101
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100102 MemoryGroup _memory_group;
103 std::vector<CLTensor> _results_vector;
104 std::vector<std::unique_ptr<CLReductionOperationKernel>> _reduction_kernels_vector;
105 std::vector<std::unique_ptr<CLFillBorderKernel>> _border_handlers_vector;
106 CLReshapeLayer _reshape;
107 unsigned int _num_of_stages;
108 unsigned int _reduction_axis;
109 bool _is_serial;
110 bool _is_reshape_required;
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100111};
Manuel Bottinib412fab2018-12-10 17:40:23 +0000112} // namespace arm_compute
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100113#endif /* ARM_COMPUTE_CLREDUCTIONOPERATION_H */