1 /*
2  * Copyright (c) 2022-2023 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 #include "GpuKernelVariableTable.h"
25 #include "arm_compute/core/CL/CLHelpers.h"
26 #include "arm_compute/core/ITensorInfo.h"
27 #include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
28 
29 namespace arm_compute
30 {
31 namespace experimental
32 {
33 namespace dynamic_fusion
34 {
declare_variable(const GpuKernelComponentGroup & comp_group,const ITensorInfo * tensor,GpuKernelArgumentInfo argument_info,const std::string & alias)35 void GpuKernelVariableTable::declare_variable(const GpuKernelComponentGroup &comp_group, const ITensorInfo *tensor, GpuKernelArgumentInfo argument_info, const std::string &alias)
36 {
37     ARM_COMPUTE_ERROR_ON_MSG(!tensor->has_valid_id(), "Tensor info with valid id expected");
38 
39     // Do not re-declare if the variable associated with the tensor has already been declared
40     auto it = _vars.find(tensor->id());
41 
42     if(it != _vars.end())
43     {
44         ARM_COMPUTE_ERROR_ON(!(it->second.kernel_argument_info == argument_info));
45         return;
46     }
47 
48     const auto target = comp_group.get_tile_for_tensor(tensor);
49 
50     if(target != tensor)
51     {
52         // If the tensor uses a shared tile, don't declare another variable.
53         it = _vars.find(target->id());
54 
55         ARM_COMPUTE_ERROR_ON_MSG(
56             it == _vars.end(),
57             "The variable used for this tensor must have been declared.");
58 
59         _vars[tensor->id()] = it->second;
60     }
61     else
62     {
63         // Declare variable associated with the tensor
64         std::stringstream ss;
65         ss << alias << "_t" << abs(tensor->id());
66         const auto     uniq_name = ss.str();
67         TensorVariable var{ tensor->id(), uniq_name, argument_info };
68 
69         _vars.emplace(tensor->id(), var);
70     }
71 }
72 
get_variable(const ITensorInfo * tensor) const73 GpuKernelVariableTable::TensorVariable GpuKernelVariableTable::get_variable(const ITensorInfo *tensor) const
74 {
75     const auto var = _vars.at(tensor->id());
76     return var;
77 }
78 
get_variable_list(const std::vector<const ITensorInfo * > & tensors) const79 GpuKernelVariableTable::VariableList GpuKernelVariableTable::get_variable_list(const std::vector<const ITensorInfo *> &tensors) const
80 {
81     VariableList vars{};
82     for(const auto &tensor : tensors)
83     {
84         if(!tensor->has_valid_id())
85         {
86             continue;
87         }
88         vars.push_back(get_variable(tensor));
89     }
90     return vars;
91 }
92 
TagVal(const GpuKernelVariableTable::TensorVariable & var)93 TagVal::TagVal(const GpuKernelVariableTable::TensorVariable &var)
94     : value{ var.uniq_name }
95 {
96 }
97 
TagVal(const std::string & val)98 TagVal::TagVal(const std::string &val)
99     : value{ val }
100 {
101 }
102 
TagVal(const char * val)103 TagVal::TagVal(const char *val)
104     : value{ std::string(val) }
105 {
106 }
107 
TagVal(const DataType & data_type)108 TagVal::TagVal(const DataType &data_type)
109     : value{ get_cl_type_from_data_type(data_type) }
110 {
111 }
112 } // namespace dynamic_fusion
113 } // namespace experimental
114 } // namespace arm_compute
115