Compute Library
 22.08
ClKernelGraph.h
Go to the documentation of this file.
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 #ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION
25 #ifndef ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_CLKERNELGRAPH_H
26 #define ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_CLKERNELGRAPH_H
27 
34 #include "support/DeepCopy.h"
35 
36 namespace arm_compute
37 {
38 namespace experimental
39 {
40 namespace dynamic_fusion
41 {
42 struct ClKernelGraph;
43 class ClKernelBlueprint;
44 
45 enum class Complexity
46 {
47  Simple,
48  Complex
49 };
50 
51 /** Configurations for ClKernel
52  *
53  */
55 {
57  TileDescriptor tile_desc{};
58  StoreType store_type{};
59  friend bool operator==(const ClKernelConfig &config0, const ClKernelConfig &config1)
60  {
61  return config0.stage == config1.stage && config0.tile_desc == config1.tile_desc && config0.store_type == config1.store_type;
62  }
63 };
64 
66 {
67 public:
69  ClKernelTensor() = default;
70  ClKernelTensor(Id id, ITensorInfo *desc, MemoryType memory_type, const AuxMemoryInfo &memory_info)
71  : id{ id }, desc{ desc }, memory_type{ memory_type }, memory_info{ memory_info }
72  {
73  }
74  bool operator==(const ClKernelTensor &other) const
75  {
76  return desc == other.desc;
77  }
78 
79  Id id{};
80  ITensorInfo *desc{};
81  MemoryType memory_type{};
82  AuxMemoryInfo memory_info{};
83 };
84 
85 struct ClKernel
86 {
87 public:
89  ClKernel() = default;
90  virtual ~ClKernel() = default;
91  ClKernel(const ClKernel &kernel) = default;
92  ClKernel &operator=(const ClKernel &kernel) = default;
93  ClKernel(ClKernel &&kernel) = default;
94  ClKernel &operator=(ClKernel &&kernel) = default;
95  ClKernel(const ClKernelGraph *graph, Id id, const ClKernelConfig &config, const ITensorDescPack<ClKernelTensor> &tensors)
96  : _graph{ graph }, _id{ id }, _config{ config }, _tensors{ tensors }
97  {
98  }
99  virtual bool operator==(const ClKernel &other) const = 0;
100  virtual Complexity complexity() const = 0;
101  virtual Status generate(ClKernelBlueprint &bp) const = 0;
102  Id id() const
103  {
104  return _id;
105  }
107  {
108  return _tensors;
109  }
111  {
112  return _config;
113  }
114 
115 protected:
116  const ClKernelGraph *_graph {};
117  Id _id{};
118  ClKernelConfig _config{};
120 };
121 
123 {
124 public:
125  Complexity complexity() const override
126  {
127  return Complexity::Complex;
128  }
129  ClDirectConv2dKernel() = default;
130  ~ClDirectConv2dKernel() override = default;
132  : ClKernel{ graph, id, config, tensors }, desc{ desc }
133  {
134  }
135  static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ClDirectConv2dKernelDescriptor &conv2d_desc);
136  bool operator==(const ClKernel &other) const override;
137  Status generate(ClKernelBlueprint &bp) const override;
138 
140 };
141 
143 {
144 public:
145  Complexity complexity() const override
146  {
147  return Complexity::Simple;
148  }
149  ClElementwiseKernel() = default;
150  ~ClElementwiseKernel() override = default;
152  : ClKernel{ graph, id, config, tensors }, desc{ desc }
153  {
154  }
155  static Status validate(const ITensorInfo *lhs, const ITensorInfo *rhs, const ITensorInfo *dst);
156  bool operator==(const ClKernel &other) const override;
157  Status generate(ClKernelBlueprint &bp) const override;
158 
160 };
161 
162 struct ClFloorKernel : public ClKernel
163 {
164 public:
165  Complexity complexity() const override
166  {
167  return Complexity::Simple;
168  }
169  ClFloorKernel() = default;
170  ~ClFloorKernel() override = default;
171  ClFloorKernel(const ClKernelGraph *graph, Id id, const ClKernelConfig &config, const ClFloorKernelDescriptor &desc, const ITensorDescPack<ClKernelTensor> tensors)
172  : ClKernel{ graph, id, config, tensors }, desc{ desc }
173  {
174  }
175  static Status validate(const ITensorInfo *src, const ITensorInfo *dst);
176  bool operator==(const ClKernel &other) const override;
177  Status generate(ClKernelBlueprint &bp) const override;
178 
180 };
181 
183 {
184 public:
186  using KernelMap = std::map<Id, utils::memory::deep_unique_ptr<ClKernel>>;
187  using KernelTensorMap = std::map<Id, utils::memory::deep_unique_ptr<ClKernelTensor>>;
188 
189  ClKernelGraph() = default;
190  ~ClKernelGraph() = default;
191 
192  friend bool operator==(const ClKernelGraph &graph0, const ClKernelGraph &graph1)
193  {
194  return graph0.graph == graph1.graph && graph0.kernels == graph1.kernels && graph0.tensors == graph1.tensors;
195  }
196 
197  Status add_kernel_tensor(ITensorInfo *desc, MemoryType memory_type, const AuxMemoryInfo &memory_info, Id &tensor_id, Id merge_point = DependencyGraph::empty_id())
198  {
199  tensor_id = graph.add_tensor(merge_point);
200  if(tensors.find(tensor_id) == tensors.end())
201  {
202  tensors[tensor_id] = utils::memory::make_deep_unique<ClKernelTensor, ClKernelTensor>(tensor_id, desc, memory_type, memory_info);
203  }
204  return Status{};
205  }
206 
207  template <typename ContentT, typename KernelDescT>
208  Status add_kernel(const ClKernelConfig &config, const KernelDescT &desc, const ITensorDescPack<ClKernelTensor> &tensors, Id &kernel_id)
209  {
210  const auto src_tensors = tensors.get_const_src_tensors();
211  const auto dst_tensors = tensors.get_const_dst_tensors();
212  std::vector<Id> src_tensor_ids{};
213  std::vector<Id> dst_tensor_ids{};
214  for(const auto &t : src_tensors)
215  {
216  src_tensor_ids.push_back(t->id);
217  }
218  for(const auto &t : dst_tensors)
219  {
220  dst_tensor_ids.push_back(t->id);
221  }
222  kernel_id = graph.add_operator(src_tensor_ids, dst_tensor_ids).second;
223  auto k = utils::memory::make_deep_unique<ClKernel, ContentT>(this, kernel_id, config, desc, tensors);
224  kernels[kernel_id] = std::move(k);
225  return Status{};
226  }
227 
229  {
230  return kernels.at(id).get();
231  }
232  const ClKernel *get_kernel(Id id) const
233  {
234  return kernels.at(id).get();
235  }
236 
238  {
239  return tensors.at(id).get();
240  }
241  const ClKernelTensor *get_tensor(Id id) const
242  {
243  return tensors.at(id).get();
244  }
245 
247  KernelMap kernels{};
248  KernelTensorMap tensors{};
249 };
251 
252 std::vector<const ClKernel *> traverse(const ClKernelGraph &graph);
253 std::vector<ClKernel *> traverse(ClKernelGraph &graph);
254 
255 } // namespace dynamic_fusion
256 } // namespace experimental
257 } // namespace arm_compute
258 #endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_CLKERNELGRAPH_H
259 #endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
ClKernel(const ClKernelGraph *graph, Id id, const ClKernelConfig &config, const ITensorDescPack< ClKernelTensor > &tensors)
Definition: ClKernelGraph.h:95
The dependency graph of a workload, where the nodes are of 2 types: Tensor or Operator Represented as...
Status validate(const OperatorGraph &op_graph)
Return the validity of op_graph, usually after performing an operation (e.g.
ITensorDescPack< ClKernelTensor > tensors() const
std::vector< const TDesc * > get_const_src_tensors() const
Get the const ACL_SRC_* tensors.
ClElementwiseKernel(const ClKernelGraph *graph, Id id, const ClKernelConfig &config, const ClElementwiseKernelDescriptor &desc, const ITensorDescPack< ClKernelTensor > tensors)
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
std::vector< const ClKernel * > traverse(const ClKernelFusionGroup &group)
MemoryType
Type of memory used by a Workload Tensor.
Definition: IWorkload.h:59
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
ClDirectConv2dKernel(const ClKernelGraph *graph, Id id, const ClKernelConfig config, const ClDirectConv2dKernelDescriptor &desc, const ITensorDescPack< ClKernelTensor > tensors)
Intermediate representation of the final, complete kernel source.
Status generate(ClWorkload &workload, const ClWorkloadContext &ctx, const ClFusedKernelGraph &fused_kernel_graph)
ClKernelTensor(Id id, ITensorInfo *desc, MemoryType memory_type, const AuxMemoryInfo &memory_info)
Definition: ClKernelGraph.h:70
Describes when a Unit Workload is run.
Definition: IWorkload.h:43
friend bool operator==(const ClKernelConfig &config0, const ClKernelConfig &config1)
Definition: ClKernelGraph.h:59
const ClKernelTensor * get_tensor(Id id) const
ClFloorKernel(const ClKernelGraph *graph, Id id, const ClKernelConfig &config, const ClFloorKernelDescriptor &desc, const ITensorDescPack< ClKernelTensor > tensors)
Memory Info for a WorkloadTensor of Auxiliary memory type.
Definition: IWorkload.h:70
std::map< Id, utils::memory::deep_unique_ptr< ClKernel > > KernelMap
Status add_kernel(const ClKernelConfig &config, const KernelDescT &desc, const ITensorDescPack< ClKernelTensor > &tensors, Id &kernel_id)
std::map< Id, utils::memory::deep_unique_ptr< ClKernelTensor > > KernelTensorMap
Status add_kernel_tensor(ITensorInfo *desc, MemoryType memory_type, const AuxMemoryInfo &memory_info, Id &tensor_id, Id merge_point=DependencyGraph::empty_id())
friend bool operator==(const ClKernelGraph &graph0, const ClKernelGraph &graph1)
bool operator==(const OpTensor &t0, const OpTensor &t1)
std::vector< const TDesc * > get_const_dst_tensors() const
Get the const ACL_DST_* tensors.
bool operator==(const ClKernelTensor &other) const
Definition: ClKernelGraph.h:74