blob: 0a2369d357d50f18a785e4c4dd2e6d2a19f1aa73 [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#ifndef SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUOPERATORGROUP
25#define SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUOPERATORGROUP
26
27#include "arm_compute/core/ITensorInfo.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
SiCong Lif44bbc52022-08-29 18:25:51 +010029#include "src/dynamic_fusion/sketch/ArgumentPack.h"
30#include "src/dynamic_fusion/sketch/gpu/GpuOperatorProperties.h"
31#include "src/dynamic_fusion/sketch/utils/DependencyGraph.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032
SiCong Lif44bbc52022-08-29 18:25:51 +010033#include <map>
34
35namespace arm_compute
36{
37namespace experimental
38{
39namespace dynamic_fusion
40{
41using OperatorId = DependencyGraph::OperatorId;
42
43/** An operator for the sole purpose of validating fusion
44 */
45class Operator
46{
47public:
48 /** Default constructor */
49 Operator() = default;
50 /** Get Operator Id */
51 OperatorId id() const;
52 /** Get operator type */
53 GpuOperatorType operator_type() const;
54 /** Get tensor arguments */
55 ArgumentPack<ITensorInfo> tensors() const;
56 friend class GpuOperatorGroup;
57
58private:
59 Operator(OperatorId id, GpuOperatorType operator_type, const ArgumentPack<ITensorInfo> &tensors);
60 OperatorId _id{};
61 GpuOperatorType _operator_type{};
62 ArgumentPack<ITensorInfo> _tensors{};
63};
64
65/** A linear sequence of operators to be fused in a workload
66 * For the time being, this class is only used for validating operator fusion
67 * INVARIANTS:
68 * @note These invariants are exactly the same as operator fusion constraints
69 * 1. Fusion is limited to a linear sequence of operators
70 * 2. Max number of operators that can be fused is @ref GpuOperatorGroup::max_fused_operators
71 * 3. The fusion is subject to the pattern: Complex + Simple * | Simple + Simple * | Un-fusable
72 * 4. All operator but unfusable, have exactly 1 dst tensor
73 * 5. All fused operators share the same dst tensor shape
74 * 6. All fused operators' tensors share the same @ref DataLayout
75 */
76class GpuOperatorGroup
77{
78public:
79 static constexpr size_t max_fused_operators = 32;
80 /** Try adding (without actually adding) an operator to the group
81 *
Viet-Hoa Do04f46202022-12-14 14:49:56 +000082 * @param[in] op Operator to be added
83 * @param[in] is_output Whether this operator is the output operator.
SiCong Lif44bbc52022-08-29 18:25:51 +010084 *
85 * @return true If @p op can be added while maintaining the invariants
86 * @return false Otherwise
87 */
Viet-Hoa Do04f46202022-12-14 14:49:56 +000088 bool try_add_operator(const Operator &op, bool is_output = false) const;
SiCong Lif44bbc52022-08-29 18:25:51 +010089 /** Add an operator to the group
90 *
Viet-Hoa Do04f46202022-12-14 14:49:56 +000091 * @param[in] op Operator to be added
92 * @param[in] is_output Whether this operator is the output operator.
SiCong Lif44bbc52022-08-29 18:25:51 +010093 */
Viet-Hoa Do04f46202022-12-14 14:49:56 +000094 void add_operator(const Operator &op, bool is_output = false);
SiCong Lif44bbc52022-08-29 18:25:51 +010095 /** Create a new operator
96 *
97 * @param[in] operator_type @ref GpuOperatorType of the new operator
98 * @param[in] tensors Tensor arguments to the new operator
99 *
100 * @return Operator
101 */
102 Operator new_operator(const GpuOperatorType &operator_type, const ArgumentPack<ITensorInfo> &tensors) const;
103 /** Get the "root operator" of the group, which is the first operator in a linear sequence
104 * @return const Operator* Pointer to the root operator
105 */
106 const Operator *get_root_operator() const;
107
108private:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 DependencyGraph _graph{};
SiCong Lif44bbc52022-08-29 18:25:51 +0100110 std::map<OperatorId, Operator> _operators{};
111};
112} // namespace dynamic_fusion
113} // namespace experimental
114} // namespace arm_compute
115#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUOPERATORGROUP */