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