SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace experimental |
| 31 | { |
| 32 | namespace dynamic_fusion |
| 33 | { |
| 34 | namespace |
| 35 | { |
| 36 | std::vector<DependencyGraph::TensorId> get_tensor_ids(const std::vector<const ITensorInfo *> tensors) |
| 37 | { |
| 38 | std::vector<DependencyGraph::TensorId> tensor_ids{}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | std::transform(std::begin(tensors), std::end(tensors), std::back_inserter(tensor_ids), |
| 40 | [](const auto &t) { return t->id(); }); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 41 | return tensor_ids; |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | Operator::Operator(OperatorId id, GpuOperatorType operator_type, const ArgumentPack<ITensorInfo> &tensors) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | : _id{id}, _operator_type{operator_type}, _tensors{tensors} |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | OperatorId Operator::id() const |
| 52 | { |
| 53 | return _id; |
| 54 | } |
| 55 | |
| 56 | GpuOperatorType Operator::operator_type() const |
| 57 | { |
| 58 | return _operator_type; |
| 59 | } |
| 60 | |
| 61 | ArgumentPack<ITensorInfo> Operator::tensors() const |
| 62 | { |
| 63 | return _tensors; |
| 64 | } |
| 65 | |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 66 | bool GpuOperatorGroup::try_add_operator(const Operator &op, bool is_output) const |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 67 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | if (!_graph.try_add_operator_as_linear(op.id(), src_tensor_ids, dst_tensor_ids, is_output)) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 72 | { |
| 73 | return false; |
| 74 | } |
| 75 | // Constraint 2 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | if (_operators.size() >= max_fused_operators) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 77 | { |
| 78 | return false; |
| 79 | } |
| 80 | // Constraint 3.1: Pattern: (Unfusable) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | if (_operators.size() > 0 && get_root_operator()->operator_type() == GpuOperatorType::Unfusable) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 82 | { |
| 83 | return false; |
| 84 | } |
| 85 | // Constraint 3.2 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | if (_operators.size() > 0 && (op.operator_type() != GpuOperatorType::Simple)) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | // Constraint 4 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | if (op.operator_type() != GpuOperatorType::Unfusable && op.tensors().get_const_dst_tensors().size() != 1U) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 92 | { |
| 93 | return false; |
| 94 | } |
| 95 | // Constraint 5 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | if (_operators.size() > 0) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 97 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | for (const auto &t : root_dst_tensors) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 103 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 104 | if (detail::have_different_dimensions(t->tensor_shape(), first_dst_tensor->tensor_shape(), 0)) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 105 | { |
| 106 | return false; |
| 107 | } |
| 108 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 109 | for (const auto &t : dst_tensors) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 110 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 111 | if (detail::have_different_dimensions(t->tensor_shape(), first_dst_tensor->tensor_shape(), 0)) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 112 | { |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | // Constraint 6 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | if (_operators.size() > 0) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 119 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 124 | for (const auto &t : root_dst_tensors) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 125 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | if (t->data_layout() != first_dst_tensor_layout) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 127 | { |
| 128 | return false; |
| 129 | } |
| 130 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 131 | for (const auto &t : dst_tensors) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 132 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | if (t->data_layout() != first_dst_tensor_layout) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 134 | { |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | return true; |
| 140 | } |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 141 | void GpuOperatorGroup::add_operator(const Operator &op, bool is_output) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 142 | { |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 143 | ARM_COMPUTE_ERROR_ON(!try_add_operator(op, is_output)); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 144 | 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 Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 146 | _graph.add_operator_as_linear(op.id(), src_tensor_ids, dst_tensor_ids, is_output); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 147 | _operators[op.id()] = op; |
| 148 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 149 | Operator GpuOperatorGroup::new_operator(const GpuOperatorType &operator_type, |
| 150 | const ArgumentPack<ITensorInfo> &tensors) const |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 151 | { |
| 152 | auto new_id = static_cast<OperatorId>(_operators.size()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 153 | return Operator{new_id, operator_type, tensors}; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 154 | } |
| 155 | const Operator *GpuOperatorGroup::get_root_operator() const |
| 156 | { |
| 157 | const auto roots = _graph.get_root_ops(); |
| 158 | ARM_COMPUTE_ERROR_ON(roots.size() > 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 159 | if (roots.empty()) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 160 | { |
| 161 | return nullptr; |
| 162 | } |
| 163 | return &_operators.at(roots[0]); |
| 164 | } |
| 165 | |
| 166 | } // namespace dynamic_fusion |
| 167 | } // namespace experimental |
| 168 | } // namespace arm_compute |