SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021, 2023 Arm Limited. |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 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 | */ |
SiCongLi | 3177861 | 2021-11-12 17:33:45 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_EXPERIMENTAL_POSTOPS |
| 25 | #define ARM_COMPUTE_EXPERIMENTAL_POSTOPS |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/experimental/IPostOp.h" |
| 28 | |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/ActivationLayerInfo.h" |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 31 | |
| 32 | #include <vector> |
| 33 | |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace experimental |
| 37 | { |
SiCongLi | 3177861 | 2021-11-12 17:33:45 +0000 | [diff] [blame] | 38 | /** (EXPERIMENTAL_POST_OPS) |
| 39 | * Implementation of specific IPostOps |
| 40 | */ |
| 41 | |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 42 | template <typename TensorRelatedT> |
| 43 | struct PostOpAct : public IPostOp<TensorRelatedT> |
| 44 | { |
| 45 | public: |
| 46 | PostOpAct(const ActivationLayerInfo &act_info) |
| 47 | : _act_info{ act_info } |
| 48 | { |
| 49 | } |
| 50 | // NOTE: PostOps do not own any resources pointed to by TensorRelatedT if it's a pointer type, thus allow shallow copy |
| 51 | ~PostOpAct() override = default; |
| 52 | PostOpAct(const PostOpAct &) = default; |
| 53 | PostOpAct &operator=(const PostOpAct &) = default; |
| 54 | PostOpAct(PostOpAct &&) = default; |
| 55 | PostOpAct &operator=(PostOpAct &&) = default; |
| 56 | |
| 57 | int prev_dst_pos() const override |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | PostOpType type() const override |
| 62 | { |
| 63 | return PostOpType::Activation; |
| 64 | } |
| 65 | std::vector<TensorRelatedT *> arguments() override |
| 66 | { |
| 67 | return {}; |
| 68 | } |
| 69 | std::vector<const TensorRelatedT *> arguments() const override |
| 70 | { |
| 71 | return {}; |
| 72 | } |
| 73 | std::unique_ptr<IPostOp<TensorRelatedT>> clone() const override |
| 74 | { |
| 75 | return std::make_unique<PostOpAct<TensorRelatedT>>(*this); |
| 76 | } |
| 77 | ActivationLayerInfo _act_info; |
| 78 | }; |
| 79 | |
| 80 | template <typename TensorRelatedT> |
| 81 | struct PostOpEltwiseAdd : public IPostOp<TensorRelatedT> |
| 82 | { |
| 83 | public: |
SiCongLi | eb8bd81 | 2021-10-29 15:05:49 +0100 | [diff] [blame] | 84 | PostOpEltwiseAdd(TensorRelatedT addend, int prev_dst_pos, ConvertPolicy policy) |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 85 | : _addend{ addend }, |
SiCongLi | eb8bd81 | 2021-10-29 15:05:49 +0100 | [diff] [blame] | 86 | _prev_dst_pos{ prev_dst_pos }, |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 87 | _policy{ policy } |
| 88 | { |
| 89 | } |
| 90 | // NOTE: PostOps do not own any resources pointed to by TensorRelatedT if it's a pointer type, thus allow shallow copy |
| 91 | ~PostOpEltwiseAdd() override = default; |
| 92 | PostOpEltwiseAdd(const PostOpEltwiseAdd &) = default; |
| 93 | PostOpEltwiseAdd &operator=(const PostOpEltwiseAdd &) = default; |
| 94 | PostOpEltwiseAdd(PostOpEltwiseAdd &&) = default; |
| 95 | PostOpEltwiseAdd &operator=(PostOpEltwiseAdd &&) = default; |
| 96 | int prev_dst_pos() const override |
| 97 | { |
SiCongLi | eb8bd81 | 2021-10-29 15:05:49 +0100 | [diff] [blame] | 98 | return _prev_dst_pos; |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 99 | } |
| 100 | PostOpType type() const override |
| 101 | { |
| 102 | return PostOpType::Eltwise_Add; |
| 103 | } |
| 104 | std::vector<TensorRelatedT *> arguments() override |
| 105 | { |
| 106 | return { &_addend }; |
| 107 | } |
| 108 | std::vector<const TensorRelatedT *> arguments() const override |
| 109 | { |
| 110 | return { &_addend }; |
| 111 | } |
| 112 | std::unique_ptr<IPostOp<TensorRelatedT>> clone() const override |
| 113 | { |
| 114 | return std::make_unique<PostOpEltwiseAdd<TensorRelatedT>>(*this); |
| 115 | } |
| 116 | TensorRelatedT _addend; |
SiCongLi | eb8bd81 | 2021-10-29 15:05:49 +0100 | [diff] [blame] | 117 | int _prev_dst_pos; |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 118 | ConvertPolicy _policy; |
| 119 | }; |
| 120 | |
ramelg01 | 6049eda | 2021-10-29 10:52:53 +0100 | [diff] [blame] | 121 | template <typename TensorRelatedT> |
| 122 | struct PostOpEltwisePRelu : public IPostOp<TensorRelatedT> |
| 123 | { |
| 124 | public: |
| 125 | PostOpEltwisePRelu(TensorRelatedT alpha_param, int prev_dst_pos, ConvertPolicy policy) |
| 126 | : _alpha_param{ alpha_param }, |
| 127 | _prev_dst_pos{ prev_dst_pos }, |
| 128 | _policy{ policy } |
| 129 | { |
| 130 | } |
| 131 | // NOTE: PostOps do not own any resources pointed to by TensorRelatedT if it's a pointer type, thus allow shallow copy |
| 132 | ~PostOpEltwisePRelu() override = default; |
| 133 | PostOpEltwisePRelu(const PostOpEltwisePRelu &) = default; |
| 134 | PostOpEltwisePRelu &operator=(const PostOpEltwisePRelu &) = default; |
| 135 | PostOpEltwisePRelu(PostOpEltwisePRelu &&) = default; |
| 136 | PostOpEltwisePRelu &operator=(PostOpEltwisePRelu &&) = default; |
| 137 | int prev_dst_pos() const override |
| 138 | { |
| 139 | return _prev_dst_pos; |
| 140 | } |
| 141 | PostOpType type() const override |
| 142 | { |
| 143 | return PostOpType::Eltwise_PRelu; |
| 144 | } |
| 145 | std::vector<TensorRelatedT *> arguments() override |
| 146 | { |
| 147 | return { &_alpha_param }; |
| 148 | } |
| 149 | std::vector<const TensorRelatedT *> arguments() const override |
| 150 | { |
| 151 | return { &_alpha_param }; |
| 152 | } |
| 153 | std::unique_ptr<IPostOp<TensorRelatedT>> clone() const override |
| 154 | { |
| 155 | return std::make_unique<PostOpEltwisePRelu<TensorRelatedT>>(*this); |
| 156 | } |
| 157 | TensorRelatedT _alpha_param; |
| 158 | int _prev_dst_pos; |
| 159 | ConvertPolicy _policy; |
| 160 | }; |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 161 | } // namespace experimental |
| 162 | } // namespace arm_compute |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 163 | #endif //ARM_COMPUTE_EXPERIMENTAL_POSTOPS |