blob: 4c9d9405947e94f635d43d1092a12a957e08285f [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_GPUKERNELCOMPONENTGROUP
25#define SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGROUP
26
27#include "components/Types.h"
28
29#include <cstdint>
30#include <cstdlib>
31#include <vector>
32
33namespace arm_compute
34{
35/** Forward declaration */
36class ITensorInfo;
37namespace experimental
38{
39namespace dynamic_fusion
40{
41class IGpuKernelComponent;
42/** A group of gpu kernel components to be fused together
43 * PRECONDITIONS:
44 * 1. Fusion is limited to a linear sequence of kernel components
45 * INVARIANTS:
46 * @note These preconditions and invariants are exactly the same as fusion constraints for kernel components
47 * 2. Max number of components that can be fused is @ref GpuKernelComponentGroup::max_fused_components (
48 * excluding any output or input (if any) components.
49 * The max number of output components are bound by the maximum number of dst tensors allowed for a component / component group
50 * )
51 * 3. The fusion is subject to the pattern: (Complex + Simple * | Simple + Simple * | Un-fusable) + Output?
52 * 4. All components but unfusable, have exactly 1 dst tensor
53 * 5. All fused components share the same @ref IGpuKernelComponent::Properties ( @ref UnitWorkloadStage etc. )
54 * 6. All fused components share the same tunable parameters like tile size
55 * 7. All fused components share the same dst tensor shape
56 * 8. All fused components' tensors share the same @ref DataLayout
57 * 9. Maximum number of dst tensors allowed for an component (including unfusable) / component group is @ref GpuKernelComponentGroup::max_dst_tensors
58 * This has an impact on the total number of components supported, which = max_fused_components + max_dst_tensors
59 */
60class GpuKernelComponentGroup
61{
62public:
63 using ComponentPtr = IGpuKernelComponent *;
64 /** Maximum number of components that can be fused into the same component group
65 */
66 static constexpr size_t max_fused_components = 64;
67 /** Maximum number of dst tensors allowed for a component / component
68 */
69 static constexpr size_t max_dst_tensors = 8;
70
71public:
72 /** Default constructor */
73 GpuKernelComponentGroup() = default;
74 /** Allow instances of this class to be copy constructed */
75 GpuKernelComponentGroup(const GpuKernelComponentGroup &) = default;
76 /** Allow instances of this class to be copied */
77 GpuKernelComponentGroup &operator=(const GpuKernelComponentGroup &) = default;
78 /** Allow instances of this class to be move constructed */
79 GpuKernelComponentGroup(GpuKernelComponentGroup &&) = default;
80 /** Allow instances of this class to be moved */
81 GpuKernelComponentGroup &operator=(GpuKernelComponentGroup &&) = default;
82 /** Add a component pointer into the group
83 * If the operation fails, then no change is made to the group
84 *
85 * @param[in] component Pointer to the component to be added
86 *
87 * @return true If the operation is successful
88 * @return false If the operation fails
89 */
90 bool add_component(ComponentPtr component);
91 /** Get source tensors of this group */
92 std::vector<const ITensorInfo *> get_src_tensors() const;
93 /** Get destination tensors of this group */
94 std::vector<const ITensorInfo *> get_dst_tensors() const;
95 /** Get tensor argument of this group
96 * A tensor is an argument if it is a source or destination tensor to the group
97 */
98 std::vector<const ITensorInfo *> get_argument_tensors() const;
99 /** Get the root (first) component of this group */
100 ComponentPtr get_root_component() const;
101 /** Get the last component of this group */
102 ComponentPtr get_last_component() const;
103 /** Get the previous component to the component with id @p id
104 *
105 * @param[in] id Component id of the component whose previous component is of concern
106 *
107 * @return ComponentPtr Pointer to the previous component of the one identified by @p id
108 */
109 ComponentPtr get_previous_component(ComponentId id) const;
110 /** Check if a @ref ITensorInfo is an "intermediate" tensor of the group
111 *
112 * An intermediate tensor is any tensor that is not an argument.
113 *
114 * @param[in] tensor @ref ITensorInfo to be looked up
115 *
116 * @return true If @p tensor is an intermediate tensor
117 * @return false Otherwise
118 */
119 bool is_intermediate_tensor(const ITensorInfo *tensor) const;
120 /** Get the number of components within the group */
121 size_t size() const;
122 /** Check if the component group is empty */
123 bool empty() const;
124 ComponentPtr &operator[](size_t index);
125 const ComponentPtr &operator[](size_t index) const;
126 typename std::vector<ComponentPtr>::iterator begin();
127 typename std::vector<ComponentPtr>::iterator end();
128 typename std::vector<ComponentPtr>::const_iterator begin() const;
129 typename std::vector<ComponentPtr>::const_iterator end() const;
130 typename std::vector<ComponentPtr>::const_iterator cbegin() const;
131 typename std::vector<ComponentPtr>::const_iterator cend() const;
132
133private:
134 std::vector<const ITensorInfo *> get_interm_tensors() const;
135
136 static bool is_tensor_in(const ITensorInfo *tensor, const std::vector<const ITensorInfo *> tensors);
137
138 std::vector<ComponentPtr> _components{};
139};
140} // namespace dynamic_fusion
141} // namespace experimental
142} // namespace arm_compute
143#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGROUP */