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