blob: 178c83aa757deb37265c39559c2f4318ad0e3152 [file] [log] [blame]
SiCongLi1af54162021-10-06 15:25:57 +01001/*
2 * Copyright (c) 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_EXPERIMENTAL_IPOSTOP
25#define ARM_COMPUTE_EXPERIMENTAL_IPOSTOP
26
27#include <memory>
28#include <numeric>
29#include <vector>
30
31namespace arm_compute
32{
33namespace experimental
34{
35/** Type of Post Op */
36enum class PostOpType
37{
38 Activation,
39 Eltwise_Add,
ramelg016049eda2021-10-29 10:52:53 +010040 Eltwise_PRelu
SiCongLi1af54162021-10-06 15:25:57 +010041};
42/** An ordered sequence of type of Post Ops */
43using PostOpTypeSequence = std::vector<PostOpType>;
44/** An elementwise n-ary operation that can be appended to and fused with (at kernel-level) other operators
45 * It contains:
46 * 1. The attributes of the original operator.
47 * 2. Any additional tensor argument.
SiCongLieb8bd812021-10-29 15:05:49 +010048 * 3. The position of the previous op's dst tensor in its argument list ( @ref prev_dst_pos )
SiCongLi1af54162021-10-06 15:25:57 +010049 *
50 * For example, a series of chained ops:
51 *
52 * div(src1, relu(conv(src0, weights, bias, conv_info), act_info), div_info)
53 *
54 * translates to
55 *
56 * dst = conv(src0, weights, bias, conv_info) // main op
57 * dst = relu(dst, act_info) // previous dst is placed in the first (and only) argument
58 * dst = div(src1, dst, div_info) // previous dst is placed in the second argument
59 *
60 * which in turn translates to:
61 *
62 * main op: conv(src0, weights, bias, conv_info)
63 * post op1: relu(act_info, prev_dst_pos = 0)
64 * post op2: div(div_info, src1, prev_dst_pos = 1)
65 *
SiCongLieb8bd812021-10-29 15:05:49 +010066 * @note: On Broadcasting
67 * For n-ary post ops, the tensor arguments must not "widen" the dst tensor of the main op
68 * For example, for a dst of shape [14, 1, 34]:
69 * * post_op_arg1 = [1, 1, 34] is allowed: broadcast in dim 0
70 * * post_op_arg1 = [14, 1, 34] is allowed: no broadcast
71 * * post_op_arg1 = [1, 1, 34] is allowed: broadcast in dims 0 and 1
72 * * post_op_arg1 = [14, 15, 34] is NOT allowed: broadcast widens the dst tensor
73 *
74 * @note: PostOps do not own any resources pointed to by TensorRelatedT if it's a pointer type
75 * @note: If TensorRelatedT points to a resource, IPostOp assumes that resource is valid throughout its lifetime
SiCongLi1af54162021-10-06 15:25:57 +010076 * and the lifetime of its copies. This is almost guaranteed as IPostOp is only meant to be used at configure time
77 * after the ITensor or ITensorInfo objects are already constructed
78 */
79template <typename TensorRelatedT>
80struct IPostOp
81{
82 /** Get the arity of the post op
SiCongLieb8bd812021-10-29 15:05:49 +010083 * @note: that this is one fewer than the arity of the original op, because we implicitly pass the previous op's dst
SiCongLi1af54162021-10-06 15:25:57 +010084 * tensor as one of the arguments
85 */
86 size_t arity() const
87 {
88 return arguments().size();
89 }
90 /** The position of previous op's dst in current op's argument list */
91 virtual int prev_dst_pos() const = 0;
92 /** The IPostOp type */
93 virtual PostOpType type() const = 0;
94 /** The argument tensors
95 * The order of the argument tensor is strictly preserved
96 */
97 virtual std::vector<TensorRelatedT *> arguments() = 0;
98 virtual std::vector<const TensorRelatedT *> arguments() const = 0;
99 /** Clone method used in cases where PostOps are owned by unique_ptr
SiCongLieb8bd812021-10-29 15:05:49 +0100100 * @note: This performs a shallow copy of the TensorRelatedT if TensorRelatedT points to a resource
SiCongLi1af54162021-10-06 15:25:57 +0100101 */
102 virtual std::unique_ptr<IPostOp<TensorRelatedT>> clone() const = 0;
103 virtual ~IPostOp()
104 {
105 }
106};
107
108/** A sequence of PostOps that can be appended to the end of other operators */
109template <typename TensorRelatedT>
110class PostOpList
111{
112public:
113 /** Constructor */
114 PostOpList() = default;
115 /** Destructor */
116 ~PostOpList() = default;
117 PostOpList(const PostOpList &other)
118 {
119 for(const auto &op : other._post_ops)
120 {
121 this->_post_ops.push_back(op->clone());
122 }
123 }
124 PostOpList &operator=(const PostOpList &other)
125 {
126 PostOpList tmp{ other };
127 std::swap(tmp, *this);
128 return *this;
129 }
130 PostOpList(PostOpList &&other) = default;
131 PostOpList &operator=(PostOpList &&other) = default;
132
133 /** Add a new post op at the end of the list */
134 template <typename OpT, typename... Args>
135 void push_back_op(Args &&... args)
136 {
137 _post_ops.push_back(std::make_unique<OpT>(std::forward<Args>(args)...));
138 }
139
140 /** Number of post ops */
141 size_t size() const
142 {
143 return _post_ops.size();
144 }
145
146 /** Total number of post ops */
147 size_t total_num_arguments() const
148 {
149 return std::accumulate(_post_ops.begin(), _post_ops.end(), 0, [](size_t op1_arity, const auto & op2)
150 {
151 return op1_arity + op2->arity();
152 });
153 }
154
155 /** Get the underlying post op list */
156 std::vector<std::unique_ptr<IPostOp<TensorRelatedT>>> &get_list()
157 {
158 return _post_ops;
159 }
160 const std::vector<std::unique_ptr<IPostOp<TensorRelatedT>>> &get_list() const
161 {
162 return _post_ops;
163 }
164
165private:
166 std::vector<std::unique_ptr<IPostOp<TensorRelatedT>>> _post_ops{};
167};
168
169} // namespace experimental
170} // namespace arm_compute
ramelg016049eda2021-10-29 10:52:53 +0100171#endif //ARM_COMPUTE_EXPERIMENTAL_IPOSTOP