blob: 5dbf2f660dae09eb1a56a2d25e4f26e17cdb91b2 [file] [log] [blame]
SiCong Lib63b1192022-01-28 18:24:39 +00001/*
2 * Copyright (c) 2022 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 ENABLE_EXPERIMENTAL_DYNAMIC_FUSION
25#error "This experimental feature must be enabled with -DENABLE_EXPERIMENTAL_DYNAMIC_FUSION"
26#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
27#include "arm_compute/core/experimental/OperatorGraph.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "src/core/experimental/dynamic_fusion/WorkloadImpl/OperatorGraphImpl.h"
30#include "src/core/helpers/AutoConfiguration.h"
31
32namespace arm_compute
33{
34namespace experimental
35{
36namespace dynamic_fusion
37{
38namespace
39{
40void check_dependency_graph_op_success(OperatorGraph &graph, const Status &status)
41{
42 if(!bool(status))
43 {
44 graph.impl()->status = Status{ status.error_code(), "Cycles or loops are not allowed" };
45 }
46}
47
48// Check if there are more than one roots in the graph
49void check_multiple_roots(OperatorGraph &graph)
50{
51 if(graph.impl()->graph.get_root_ops().size() > 1)
52 {
53 graph.impl()->status = Status{ ErrorCode::RUNTIME_ERROR, "Multiple roots are not allowed" };
54 }
55}
56
57void check_execution_shape(OperatorGraph &graph, const ITensorInfo &dst_info)
58{
59 const auto roots = graph.impl()->graph.get_root_ops();
60 for(auto root : roots)
61 {
62 // We assume exactly 1 dst tensor for all operators
63 const auto root_info = graph.impl()->tensors[graph.impl()->graph.dst_tensors(root)[0]]->get_tensor_info();
64 for(unsigned int dim = 0; dim < root_info->num_dimensions(); ++dim)
65 {
66 if(root_info->dimension(dim) != dst_info.dimension(dim))
67 {
68 graph.impl()->status = Status{ ErrorCode::RUNTIME_ERROR, "Cannot change execution space" };
69 return;
70 }
71 }
72 }
73}
74} // namespace
75
76OpTensor::OpTensor(Id id)
77 : _id{ id }
78{
79}
80
81OpTensor::Id OpTensor::id() const
82{
83 return _id;
84}
85
86bool operator<(const OpTensor &t0, const OpTensor &t1)
87{
88 return t0.id() < t1.id();
89}
90
91Operator::Operator(Id id)
92 : _id{ id }
93{
94}
95
96Operator::Id Operator::id() const
97{
98 return _id;
99}
100
101bool operator<(const Operator &op0, const Operator &op1)
102{
103 return op0.id() < op1.id();
104}
105
106OperatorGraph::OperatorGraph()
107 : _impl{ std::make_unique<Implementation>() }
108{
109}
110
111OperatorGraph::~OperatorGraph() = default;
112
113OperatorGraph::Implementation *OperatorGraph::impl()
114{
115 return _impl.get();
116}
117
118const OperatorGraph::Implementation *OperatorGraph::impl() const
119{
120 return _impl.get();
121}
122
123Status validate(const OperatorGraph &graph)
124{
125 return graph.impl()->status;
126}
127
128OpTensor add_tensor(OperatorGraph &graph, ITensorInfo &info)
129{
130 auto id = graph.impl()->graph.add_tensor();
131 OpTensor op_tensor(id);
132 graph.impl()->add_tensor(id, &info);
133 return op_tensor;
134}
135
136Operator add_op_conv2d(OperatorGraph &graph, const Conv2dDescriptor &desc, OpTensor input, OpTensor weights, OpTensor bias, OpTensor dst)
137{
138 // Check if map is empty as a complex operator can only be root
139 if(!graph.impl()->graph.get_root_ops().empty())
140 {
141 graph.impl()->status = Status{ ErrorCode::RUNTIME_ERROR, "Cannot add multiple complex operators" };
142 return Operator{};
143 }
144
145 std::pair<Status, DependencyGraph::Id> status_id;
146
147 if(bias.id() == -1)
148 {
149 status_id = graph.impl()->graph.add_operator({ input.id(), weights.id() }, { dst.id() });
150 }
151 else
152 {
153 status_id = graph.impl()->graph.add_operator({ input.id(), weights.id(), bias.id() }, { dst.id() });
154 }
155
156 check_dependency_graph_op_success(graph, status_id.first);
157
158 Operator op_node(status_id.second);
159
160 // Infer TensorInfo
161 OpTensorContent *dst_tensor = graph.impl()->tensors[dst.id()].get();
162 if(dst_tensor->get_tensor_info()->total_size() == 0)
163 {
164 auto src = graph.impl()->tensors[input.id()]->get_tensor_info();
165 auto wts = graph.impl()->tensors[weights.id()]->get_tensor_info();
166 auto shape = misc::shape_calculator::compute_deep_convolution_shape(src->tensor_shape(), src->data_layout(), wts->tensor_shape(), PadStrideInfo(desc.stride.x(), desc.stride.y(), desc.pad.left,
167 desc.pad.right,
168 desc.pad.top, desc.pad.bottom, DimensionRoundingType::FLOOR)); // use the default DimensionRoundingType
169
170 auto_init_if_empty(*(dst_tensor->get_tensor_info()), src->clone()->set_tensor_shape(shape));
171 }
172
173 // Check execution space
174 auto dst_info = dst_tensor->get_tensor_info();
175 check_execution_shape(graph, *dst_info);
176
177 ITensorDescPack<OpTensorContent> tensors;
178 tensors.add_const_tensor(ACL_SRC_0, graph.impl()->tensors[input.id()].get());
179 tensors.add_const_tensor(ACL_SRC_1, graph.impl()->tensors[weights.id()].get());
180 if(bias.id() != -1)
181 {
182 tensors.add_const_tensor(ACL_SRC_2, graph.impl()->tensors[bias.id()].get());
183 }
184 tensors.add_const_tensor(ACL_DST_0, graph.impl()->tensors[dst.id()].get());
185
186 graph.impl()->add_node<Conv2dContent>(status_id.second, desc, tensors);
187 check_multiple_roots(graph);
188
189 return op_node;
190}
191
192Operator add_op_conv2d(OperatorGraph &graph, const Conv2dDescriptor &desc, OpTensor input, OpTensor weights, OpTensor dst)
193{
194 return add_op_conv2d(graph, desc, input, weights, OpTensor(-1), dst);
195}
196
197void force_conv2d_method(OperatorGraph &graph, Operator conv2d, ConvolutionMethod method)
198{
199 auto node = utils::cast::polymorphic_downcast<Conv2dContent *>(graph.impl()->operators[conv2d.id()].get());
200 node->set_method(method);
201}
202
203Operator add_op_elementwise_add(OperatorGraph &graph, const AddDescriptor &desc, OpTensor lhs, OpTensor rhs, OpTensor dst)
204{
205 auto id = graph.impl()->graph.add_operator({ rhs.id(), lhs.id() }, { dst.id() });
206 check_dependency_graph_op_success(graph, id.first);
207
208 Operator op_node(id.second);
209
210 // Infer TensorInfo
211 auto node_lhs = graph.impl()->tensors[lhs.id()]->get_tensor_info();
212 auto node_rhs = graph.impl()->tensors[rhs.id()]->get_tensor_info();
213 OpTensorContent *node_dst = graph.impl()->tensors[dst.id()].get();
214
215 if(node_dst->get_tensor_info()->total_size() == 0)
216 {
217 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*node_rhs, *node_lhs);
218 auto_init_if_empty(*(node_dst->get_tensor_info()), node_lhs->clone()->set_tensor_shape(broadcast_pair.first));
219 }
220
221 // Check execution space
222 auto dst_info = node_dst->get_tensor_info();
223 check_execution_shape(graph, *dst_info);
224
225 ITensorDescPack<OpTensorContent> tensors;
226 tensors.add_const_tensor(ACL_SRC_0, graph.impl()->tensors[lhs.id()].get());
227 tensors.add_const_tensor(ACL_SRC_1, graph.impl()->tensors[rhs.id()].get());
228 tensors.add_const_tensor(ACL_DST_0, graph.impl()->tensors[dst.id()].get());
229 graph.impl()->add_node<AddContent>(id.second, desc, tensors);
230 check_multiple_roots(graph);
231
232 return op_node;
233}
234} // namespace dynamic_fusion
235} // namespace experimental
236} // namespace arm_compute