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 | #ifndef SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGROUP |
| 25 | #define SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGROUP |
| 26 | |
| 27 | #include "components/Types.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 28 | #include <cstdint> |
| 29 | #include <cstdlib> |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 30 | #include <map> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include <set> |
| 32 | #include <vector> |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | /** Forward declaration */ |
| 37 | class ITensorInfo; |
| 38 | namespace experimental |
| 39 | { |
| 40 | namespace dynamic_fusion |
| 41 | { |
| 42 | class 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 | */ |
| 61 | class GpuKernelComponentGroup |
| 62 | { |
| 63 | public: |
| 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 | |
| 72 | public: |
| 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 Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 92 | /** 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 96 | /** 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 102 | /** 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; |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 112 | /** Check if an @ref ITensorInfo is an input tensor of the group. |
| 113 | * |
| 114 | * @param[in] tensor @ref ITensorInfo to be looked up. |
| 115 | * |
| 116 | * @return true if @p tensor is an input tensor of the group, otherwise false. |
| 117 | */ |
| 118 | bool is_input_tensor(const ITensorInfo *tensor) const; |
| 119 | /** Get the list of temporary tiles that need to be declared */ |
| 120 | std::vector<const ITensorInfo *> get_tiles() const; |
| 121 | /** Get the shared tile that can be used to store temporary data of the specified tensor. |
| 122 | * |
| 123 | * @param[in] tensor @ref ITensorInfo to be looked up. |
| 124 | * |
| 125 | * @return @ref ITensorInfo that is used to store temporary data of @p tensor. |
| 126 | **/ |
| 127 | const ITensorInfo *get_tile_for_tensor(const ITensorInfo *tensor) const; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 128 | /** Get the number of components within the group */ |
| 129 | size_t size() const; |
| 130 | /** Check if the component group is empty */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 131 | bool empty() const; |
| 132 | ComponentPtr &operator[](size_t index); |
| 133 | const ComponentPtr &operator[](size_t index) const; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 134 | typename std::vector<ComponentPtr>::iterator begin(); |
| 135 | typename std::vector<ComponentPtr>::iterator end(); |
| 136 | typename std::vector<ComponentPtr>::const_iterator begin() const; |
| 137 | typename std::vector<ComponentPtr>::const_iterator end() const; |
| 138 | typename std::vector<ComponentPtr>::const_iterator cbegin() const; |
| 139 | typename std::vector<ComponentPtr>::const_iterator cend() const; |
| 140 | |
| 141 | private: |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 142 | std::vector<ComponentPtr> _components{}; |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 143 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | bool _finalized{false}; |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 145 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 146 | std::vector<const ITensorInfo *> _argument_tensors{}; |
| 147 | std::set<const ITensorInfo *> _input_tensors{}; |
| 148 | std::set<const ITensorInfo *> _interm_tensors{}; |
| 149 | const ITensorInfo *_any_output_tensor{nullptr}; |
| 150 | std::vector<const ITensorInfo *> _tiles{}; |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 151 | std::map<const ITensorInfo *, const ITensorInfo *> _tile_map{}; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 152 | }; |
| 153 | } // namespace dynamic_fusion |
| 154 | } // namespace experimental |
| 155 | } // namespace arm_compute |
| 156 | #endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGROUP */ |