blob: aec8b9db4f0939b98c736ea84683d697287f54a8 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
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#include "src/dynamic_fusion/sketch/gpu/GpuOperatorGroup.h"
25
26#include "arm_compute/core/Validate.h"
27
28namespace arm_compute
29{
30namespace experimental
31{
32namespace dynamic_fusion
33{
34namespace
35{
36std::vector<DependencyGraph::TensorId> get_tensor_ids(const std::vector<const ITensorInfo *> tensors)
37{
38 std::vector<DependencyGraph::TensorId> tensor_ids{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 std::transform(std::begin(tensors), std::end(tensors), std::back_inserter(tensor_ids),
40 [](const auto &t) { return t->id(); });
SiCong Lif44bbc52022-08-29 18:25:51 +010041 return tensor_ids;
42}
43
44} // namespace
45
46Operator::Operator(OperatorId id, GpuOperatorType operator_type, const ArgumentPack<ITensorInfo> &tensors)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 : _id{id}, _operator_type{operator_type}, _tensors{tensors}
SiCong Lif44bbc52022-08-29 18:25:51 +010048{
49}
50
51OperatorId Operator::id() const
52{
53 return _id;
54}
55
56GpuOperatorType Operator::operator_type() const
57{
58 return _operator_type;
59}
60
61ArgumentPack<ITensorInfo> Operator::tensors() const
62{
63 return _tensors;
64}
65
Viet-Hoa Do04f46202022-12-14 14:49:56 +000066bool GpuOperatorGroup::try_add_operator(const Operator &op, bool is_output) const
SiCong Lif44bbc52022-08-29 18:25:51 +010067{
68 const auto src_tensor_ids = get_tensor_ids(op.tensors().get_const_src_tensors());
69 const auto dst_tensor_ids = get_tensor_ids(op.tensors().get_const_dst_tensors());
70 // Constraint 1
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 if (!_graph.try_add_operator_as_linear(op.id(), src_tensor_ids, dst_tensor_ids, is_output))
SiCong Lif44bbc52022-08-29 18:25:51 +010072 {
73 return false;
74 }
75 // Constraint 2
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 if (_operators.size() >= max_fused_operators)
SiCong Lif44bbc52022-08-29 18:25:51 +010077 {
78 return false;
79 }
80 // Constraint 3.1: Pattern: (Unfusable)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 if (_operators.size() > 0 && get_root_operator()->operator_type() == GpuOperatorType::Unfusable)
SiCong Lif44bbc52022-08-29 18:25:51 +010082 {
83 return false;
84 }
85 // Constraint 3.2
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 if (_operators.size() > 0 && (op.operator_type() != GpuOperatorType::Simple))
SiCong Lif44bbc52022-08-29 18:25:51 +010087 {
88 return false;
89 }
90 // Constraint 4
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091 if (op.operator_type() != GpuOperatorType::Unfusable && op.tensors().get_const_dst_tensors().size() != 1U)
SiCong Lif44bbc52022-08-29 18:25:51 +010092 {
93 return false;
94 }
95 // Constraint 5
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 if (_operators.size() > 0)
SiCong Lif44bbc52022-08-29 18:25:51 +010097 {
98 const auto root_dst_tensors = get_root_operator()->tensors().get_const_dst_tensors();
99 ARM_COMPUTE_ERROR_ON(root_dst_tensors.empty());
100 const auto first_dst_tensor = root_dst_tensors[0];
101 const auto dst_tensors = op.tensors().get_const_dst_tensors();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 for (const auto &t : root_dst_tensors)
SiCong Lif44bbc52022-08-29 18:25:51 +0100103 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 if (detail::have_different_dimensions(t->tensor_shape(), first_dst_tensor->tensor_shape(), 0))
SiCong Lif44bbc52022-08-29 18:25:51 +0100105 {
106 return false;
107 }
108 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 for (const auto &t : dst_tensors)
SiCong Lif44bbc52022-08-29 18:25:51 +0100110 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111 if (detail::have_different_dimensions(t->tensor_shape(), first_dst_tensor->tensor_shape(), 0))
SiCong Lif44bbc52022-08-29 18:25:51 +0100112 {
113 return false;
114 }
115 }
116 }
117 // Constraint 6
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 if (_operators.size() > 0)
SiCong Lif44bbc52022-08-29 18:25:51 +0100119 {
120 const auto root_dst_tensors = get_root_operator()->tensors().get_const_dst_tensors();
121 ARM_COMPUTE_ERROR_ON(root_dst_tensors.empty());
122 const auto first_dst_tensor_layout = root_dst_tensors[0]->data_layout();
123 const auto dst_tensors = op.tensors().get_const_dst_tensors();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 for (const auto &t : root_dst_tensors)
SiCong Lif44bbc52022-08-29 18:25:51 +0100125 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 if (t->data_layout() != first_dst_tensor_layout)
SiCong Lif44bbc52022-08-29 18:25:51 +0100127 {
128 return false;
129 }
130 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131 for (const auto &t : dst_tensors)
SiCong Lif44bbc52022-08-29 18:25:51 +0100132 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 if (t->data_layout() != first_dst_tensor_layout)
SiCong Lif44bbc52022-08-29 18:25:51 +0100134 {
135 return false;
136 }
137 }
138 }
139 return true;
140}
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000141void GpuOperatorGroup::add_operator(const Operator &op, bool is_output)
SiCong Lif44bbc52022-08-29 18:25:51 +0100142{
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000143 ARM_COMPUTE_ERROR_ON(!try_add_operator(op, is_output));
SiCong Lif44bbc52022-08-29 18:25:51 +0100144 const auto src_tensor_ids = get_tensor_ids(op.tensors().get_const_src_tensors());
145 const auto dst_tensor_ids = get_tensor_ids(op.tensors().get_const_dst_tensors());
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000146 _graph.add_operator_as_linear(op.id(), src_tensor_ids, dst_tensor_ids, is_output);
SiCong Lif44bbc52022-08-29 18:25:51 +0100147 _operators[op.id()] = op;
148}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149Operator GpuOperatorGroup::new_operator(const GpuOperatorType &operator_type,
150 const ArgumentPack<ITensorInfo> &tensors) const
SiCong Lif44bbc52022-08-29 18:25:51 +0100151{
152 auto new_id = static_cast<OperatorId>(_operators.size());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 return Operator{new_id, operator_type, tensors};
SiCong Lif44bbc52022-08-29 18:25:51 +0100154}
155const Operator *GpuOperatorGroup::get_root_operator() const
156{
157 const auto roots = _graph.get_root_ops();
158 ARM_COMPUTE_ERROR_ON(roots.size() > 1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 if (roots.empty())
SiCong Lif44bbc52022-08-29 18:25:51 +0100160 {
161 return nullptr;
162 }
163 return &_operators.at(roots[0]);
164}
165
166} // namespace dynamic_fusion
167} // namespace experimental
168} // namespace arm_compute