blob: 6217dcc3da745e8001161f21333ffc32c32d0f75 [file] [log] [blame]
SiCongLi31778612021-11-12 17:33:45 +00001/*
Ramy Elgammala8db6122023-05-08 03:33:43 +01002 * Copyright (c) 2021, 2023 Arm Limited.
SiCongLi31778612021-11-12 17:33:45 +00003 *
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_EXPERIMENTAL_POSTOPUTILS
25#define ARM_COMPUTE_EXPERIMENTAL_POSTOPUTILS
26
27#include "arm_compute/core/experimental/IPostOp.h"
28#include "arm_compute/core/experimental/PostOps.h"
29
30#include "arm_compute/core/experimental/Types.h"
31#include "support/Cast.h"
32
33#include <vector>
34
35/** (EXPERIMENTAL_POST_OPS) */
36namespace arm_compute
37{
38namespace experimental
39{
40/** Transform a PostOpList of type FromTensorT to one of type ToTensorT */
41template <typename FromTensorT, typename ToTensorT>
42PostOpList<ToTensorT> transform_post_op_list_arguments(const PostOpList<FromTensorT> &post_ops, std::function<ToTensorT(FromTensorT)> transform_arg)
43{
44 PostOpList<ToTensorT> transformed_post_ops;
SiCongLi31778612021-11-12 17:33:45 +000045 for(const auto &post_op : post_ops.get_list())
46 {
47 switch(post_op->type())
48 {
49 case PostOpType::Activation:
50 {
51 const auto _post_op = utils::cast::polymorphic_downcast<const PostOpAct<FromTensorT> *>(post_op.get());
52 transformed_post_ops.template push_back_op<PostOpAct<ToTensorT>>(_post_op->_act_info);
53 break;
54 }
55 case PostOpType::Eltwise_Add:
56 {
57 const auto _post_op = utils::cast::polymorphic_downcast<const PostOpEltwiseAdd<FromTensorT> *>(post_op.get());
58 transformed_post_ops.template push_back_op<PostOpEltwiseAdd<ToTensorT>>(transform_arg(_post_op->_addend), _post_op->_prev_dst_pos, _post_op->_policy);
59 break;
60 }
61 case PostOpType::Eltwise_PRelu:
62 {
63 const auto _post_op = utils::cast::polymorphic_downcast<const PostOpEltwisePRelu<FromTensorT> *>(post_op.get());
64 transformed_post_ops.template push_back_op<PostOpEltwisePRelu<ToTensorT>>(transform_arg(_post_op->_alpha_param), _post_op->_prev_dst_pos, _post_op->_policy);
65 break;
66 }
67 default:
68 {
69 ARM_COMPUTE_ERROR("Unsupported PostOpType");
70 }
71 }
SiCongLi31778612021-11-12 17:33:45 +000072 }
73 return transformed_post_ops;
74}
75
76/** Get post op argument TensorType from post op argument index in a flattened, ordered post op argument list */
77inline TensorType get_post_op_arg_type(size_t index)
78{
79 ARM_COMPUTE_ERROR_ON_MSG(static_cast<int>(index) > EXPERIMENTAL_ACL_POST_OP_ARG_LAST - EXPERIMENTAL_ACL_POST_OP_ARG_FIRST, "Post Op argument index is out of range");
80 return static_cast<TensorType>(EXPERIMENTAL_ACL_POST_OP_ARG_FIRST + static_cast<int>(index));
81}
82
83/** Get a sequence of PostOp Types from PostOpList */
84template <typename T>
85PostOpTypeSequence get_post_op_sequence(const PostOpList<T> &post_ops)
86{
87 PostOpTypeSequence post_op_sequence;
88 for(const auto &op : post_ops.get_list())
89 {
90 post_op_sequence.push_back(op->type());
91 }
92 return post_op_sequence;
93}
94
95} // namespace experimental
96} // namespace arm_compute
97#endif //ARM_COMPUTE_EXPERIMENTAL_POSTOPUTILS