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_TEMPLATE_WRITER_IGPUTEMPLATECOMPONENTWRITER
25 #define SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_IGPUTEMPLATECOMPONENTWRITER
26 
27 #include "arm_compute/core/CL/CLCompileContext.h"
28 #include "arm_compute/core/ITensorInfo.h"
29 #include "arm_compute/core/Window.h"
30 #include "src/dynamic_fusion/sketch/ArgumentPack.h"
31 #include "src/dynamic_fusion/sketch/gpu/components/Types.h"
32 #include "src/dynamic_fusion/sketch/gpu/template_writer/GpuKernelVariableTable.h"
33 
34 namespace arm_compute
35 {
36 namespace experimental
37 {
38 namespace dynamic_fusion
39 {
40 /** Forward declaration */
41 class GpuKernelComponentGroup;
42 class GpuKernelVariableTable;
43 
44 /** An interface used by @ref ClTemplateWriter to write source code for a kernel component
45  */
46 class IGpuTemplateComponentWriter
47 {
48 public:
49     using ComponentGroup = GpuKernelComponentGroup;
50 
51     /**For now all kernel intermeditate/destination tensors are expected to be of type Tensor_4D_t_Buffer*/
52     static constexpr GpuKernelArgumentInfo::Type common_tensor_type = GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer;
53 
54 public:
55     /** Constructor
56      *
57      * @param[in] id      Component id
58      * @param[in] tensors Tensor arguments to the components
59      */
IGpuTemplateComponentWriter(ComponentId id,const ArgumentPack<ITensorInfo> & tensors)60     IGpuTemplateComponentWriter(ComponentId id, const ArgumentPack<ITensorInfo> &tensors)
61         : _id{ id }, _tensors{ tensors }
62     {
63     }
64     /** Destructor */
~IGpuTemplateComponentWriter()65     virtual ~IGpuTemplateComponentWriter()
66     {
67     }
68     /** Generate kernel component name */
69     virtual std::string get_name() const = 0;
70     /** Generate kernel component code template
71      *
72      * @param[in] comp_group Component group of which the component is a part of
73      *
74      * @return std::string Component code
75      */
76     virtual std::string get_component_code(const ComponentGroup &comp_group) const = 0;
77     /** Declare all variables used by the component in the @p vtable
78      *
79      * @param[out] vtable     Variable table
80      * @param[in]  comp_group Component group of which the component is a part of
81      */
82     virtual void declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const = 0;
83     /** Generate the tag look-up table used to instantiate the component code.
84      *
85      * @param[in] vtable     Variable table
86      * @param[in] comp_group Component group of which the component is a part of
87      *
88      * @return TagLUT  Tag lookup table
89      */
90     virtual TagLUT get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const = 0;
91     /** Generate additional macros used in the component */
get_additional_macros()92     virtual std::string get_additional_macros() const
93     {
94         return "";
95     }
96     /** Generate the build options used in the component
97      *
98      * @param[in] comp_group Component group of which the component is a part of
99      *
100      * @return CLBuildOptions Build options
101      */
get_build_options(const ComponentGroup & comp_group)102     virtual CLBuildOptions get_build_options(const ComponentGroup &comp_group) const
103     {
104         ARM_COMPUTE_UNUSED(comp_group);
105         return CLBuildOptions{};
106     }
107     /** Generate the component config id string used for tuning */
get_config_id()108     virtual std::string get_config_id() const
109     {
110         return "";
111     }
112     /** Generate the header list used in the component */
get_headers_list()113     virtual std::set<std::string> get_headers_list() const
114     {
115         return std::set<std::string> {};
116     }
117     /** Generate the execution window for the component */
get_window()118     virtual Window get_window() const
119     {
120         return Window{};
121     }
122     /** Get tensor arguments */
tensors()123     ArgumentPack<ITensorInfo> tensors() const
124     {
125         return _tensors;
126     }
127     /** Get component id */
id()128     ComponentId id() const
129     {
130         return _id;
131     }
132 
133 private:
134     ComponentId               _id{ -1 };
135     ArgumentPack<ITensorInfo> _tensors{};
136 };
137 } // namespace dynamic_fusion
138 } // namespace experimental
139 } // namespace arm_compute
140 #endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_IGPUTEMPLATECOMPONENTWRITER */
141