blob: 4eee3963c2ca39d82817d26dec989d06dc3013ee [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_TEMPLATE_WRITER_GPUKERNELVARIABLETABLE
25#define SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_GPUKERNELVARIABLETABLE
26
27#include "arm_compute/core/ITensorInfo.h"
28#include "src/dynamic_fusion/sketch/gpu/GpuKernelArgument.h"
29#include "support/Requires.h"
30#include "support/StringSupport.h"
31
32#include <set>
33#include <string>
34#include <type_traits>
35
36namespace arm_compute
37{
38namespace experimental
39{
40namespace dynamic_fusion
41{
42/** A table of all the variables used in the kernel
43 * Since fusion is restricted to a linear sequence of components in a kernel, only a single "intermediate variable" (the accumulator) is allowed.
44 * Each kernel has exactly one variable table
45 */
46class GpuKernelVariableTable
47{
48public:
49 /** A tensor variable whose main purposes are:
50 * - Hold the newly assigned @ref GpuKernelArgumentInfo for the associated tensor info
51 * - Hold the generated variable name for the associated tensor info
52 */
53 struct TensorVariable
54 {
55 public:
56 TensorVariable() = default;
57 TensorVariable(const TensorVariable &) = default;
58 TensorVariable &operator=(const TensorVariable &) = default;
59 ITensorInfo::Id id{ ITensorInfo::invalid_tensor_id };
60 std::string uniq_name{ "empty" }; // Unique name, also the final variable name used in the built code
61 GpuKernelArgumentInfo kernel_argument_info{};
62 bool has_valid_id() const
63 {
64 return id != ITensorInfo::invalid_tensor_id;
65 }
66 };
67 using VariableList = std::vector<TensorVariable>;
68
69public:
70 /** Declare a @ref TensorVariable for a corresponding tensor info.
71 *
72 * @note: Later re-declaration of the intermediate variable will overwrite the previous association to the @ref ITensorInfo
73 * Therefore, the order of declaration is important. It's assumed that the components declaring the variable is already in correct order
74 *
75 * @param[in] tensor Tensor info with which the new variable is associated
76 * @param[in] argument_info Kernel argument information
77 * @param[in] is_interm If the new variable is an intermediate variable
78 * @param[in] alias Alias for the variable. Will be used as part of the variable name
79 */
80 void declare_variable(const ITensorInfo *tensor, GpuKernelArgumentInfo argument_info, bool is_interm = false, const std::string &alias = "unnamed");
81 /** Get the @ref TensorVariable associated with @p tensor
82 *
83 * @param[in] tensor Tensor info to be queried
84 *
85 * @return TensorVariable
86 */
87 TensorVariable get_variable(const ITensorInfo *tensor) const;
88 /** Get the @ref TensorVariable list associated with @p tensors
89 * @note Empty tensors are skipped
90 *
91 * @param[in] tensors List of tensor infos to be queried
92 *
93 * @return VariableList
94 */
95 VariableList get_variable_list(const std::vector<const ITensorInfo *> &tensors) const;
96
97private:
98 std::map<ITensorInfo::Id, TensorVariable> _vars{}; /**< Non-intermediate (function parameter) variables*/
99 TensorVariable _interm_var{}; /**< Intermediate variable */
100 std::set<ITensorInfo::Id> _interm_tensors{}; /**< Tensors associated with the single intermediate variable */
101};
102
103/** A tag value will substitute a tag in a string template during its instantiation */
104struct TagVal
105{
106 /** Default constructor */
107 TagVal() = default;
108 /** Construct a @ref TagVal from a @ref GpuKernelVariableTable::TensorVariable */
109 TagVal(const GpuKernelVariableTable::TensorVariable &var);
110 /** Construct a @ref TagVal from an integral type */
111 template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)>
112 TagVal(T val)
113 : value{ support::cpp11::to_string(val) }
114 {
115 }
116 /** Construct a @ref TagVal from a string */
117 TagVal(const std::string &val);
118 /** Construct a @ref TagVal from a c-style string */
119 TagVal(const char *val);
120 /** Construct a @ref TagVal from a @ref DataType */
121 TagVal(const DataType &data_type);
122 /** Get the value of the TagVal as a converted string */
123 std::string value{};
124};
125
126/** A tag used in a string template is a placeholder string to be substituted by real values during template instantiation */
127using Tag = std::string;
128
129/** Tag lookup table. It is used to instantiate a string template */
130using TagLUT = std::unordered_map<Tag, TagVal>;
131
132} // namespace dynamic_fusion
133} // namespace experimental
134} // namespace arm_compute
135#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_GPUKERNELVARIABLETABLE */