Compute Library
 22.08
OperatorGraphImpl.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_OPERATORGRAPHIMPL
26 #define ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_OPERATORGRAPHIMPL
27 
30 
31 #include "support/Cast.h"
32 #include "support/DeepCopy.h"
33 
34 #include <map>
35 #include <tuple>
36 #include <type_traits>
37 
38 namespace arm_compute
39 {
40 namespace experimental
41 {
42 namespace dynamic_fusion
43 {
45 {
46  Complex = 0,
47  Simple
48 };
49 
50 struct ClKernelGraph;
52 {
53 public:
55  OpTensorContent() = default;
57  : id{ id }, desc{}
58  {
59  }
61  : id{ id }, desc{ desc }
62  {
63  }
64  ~OpTensorContent() = default;
65  OpTensorContent(const OpTensorContent &) = default;
66  OpTensorContent &operator=(const OpTensorContent &) = default;
67  OpTensorContent(OpTensorContent &&) = default;
68  OpTensorContent &operator=(OpTensorContent &&) = default;
69  bool operator==(const OpTensorContent &other) const
70  {
71  return desc == other.desc;
72  }
73 
75  {
76  return desc;
77  }
79  {
80  return desc;
81  }
82 
83  Id id{};
84  ITensorInfo *desc{};
85 };
86 
88 {
89 public:
91  OperatorContent() = default;
93  : _graph{ graph }, _id{ id }, _tensors{ tensors }
94  {
95  }
96  OperatorContent(const OperatorContent &op) = default;
97  OperatorContent &operator=(const OperatorContent &op) = default;
98  OperatorContent(OperatorContent &&op) = default;
99  OperatorContent &operator=(OperatorContent &&op) = default;
100  virtual ~OperatorContent() = default;
101  virtual OperatorComplexity complexity() const = 0;
102  virtual bool operator==(const OperatorContent &other) const = 0;
103  virtual Status translate(ClKernelGraph &kernel_graph) const = 0;
104 
105 protected:
106  const OperatorGraph::Implementation *_graph {};
107  Id _id{};
109 };
110 
112 {
113 public:
114  Conv2dContent() = default;
116  : OperatorContent(graph, id, tensors), desc(desc), forced_method(), forced_method_enabled(false)
117  {
118  }
119  // Temporary. Do not need to pass ConvolutionMethod
121  : OperatorContent(graph, id, tensors), desc(desc), forced_method(method), forced_method_enabled(true)
122  {
123  }
124  ~Conv2dContent() = default;
125  Conv2dContent(const Conv2dContent &) = default;
126  Conv2dContent &operator=(const Conv2dContent &) = default;
127  Conv2dContent(Conv2dContent &&) = default;
128  Conv2dContent &operator=(Conv2dContent &&) = default;
129  bool operator==(const OperatorContent &other) const override;
131  {
133  }
135  {
136  forced_method_enabled = true;
137  forced_method = method;
138  }
139 
140  Status translate(ClKernelGraph &kernel_graph) const override;
141  /** Replicate heuristics of @ref ClConv2d::get_convolution_method(), except that non-supported data types and data layouts are removed from the heuristics
142  *
143  * @param src
144  * @param weights
145  * @param dst
146  * @param conv2d_desc
147  * @param gpu_target
148  * @return ConvolutionMethod
149  */
150  static ConvolutionMethod select_conv_method(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const Conv2dDescriptor &conv2d_desc, const GPUTarget gpu_target);
151 
154  bool forced_method_enabled{ false };
155 
156 private:
157  Status translate_direct_conv2d(ClKernelGraph &kernel_graph) const;
158 };
159 
161 {
162 public:
163  ElementwiseContent() = default;
165  : OperatorContent(graph, id, tensors), desc(desc)
166  {
167  }
168  ~ElementwiseContent() = default;
169  ElementwiseContent(const ElementwiseContent &) = default;
170  ElementwiseContent &operator=(const ElementwiseContent &) = default;
172  ElementwiseContent &operator=(ElementwiseContent &&) = default;
173  bool operator==(const OperatorContent &other) const override;
175  {
177  }
178  Status translate(ClKernelGraph &kernel_graph) const override;
179 
180 private:
181  ElementwiseDescriptor desc{};
182 };
183 
185 {
186 public:
187  FloorContent() = default;
189  : OperatorContent(graph, id, tensors), desc(desc)
190  {
191  }
192  ~FloorContent() = default;
193  FloorContent(const FloorContent &) = default;
194  FloorContent &operator=(const FloorContent &) = default;
195  FloorContent(FloorContent &&) = default;
196  FloorContent &operator=(FloorContent &&) = default;
197  bool operator==(const OperatorContent &other) const override;
199  {
201  }
202  Status translate(ClKernelGraph &kernel_graph) const override;
203 
204 private:
205  FloorDescriptor desc{};
206 };
207 
209 {
210 public:
211  template <typename ContentT, typename... Args>
212  void add_node(Operator::Id id, Args &&... args)
213  {
214  operators[id] = utils::memory::make_deep_unique<OperatorContent, ContentT>(this, id, std::forward<Args>(args)...);
215  }
216 
217  template <typename... Args>
218  void add_tensor(OpTensor::Id id, Args &&... args)
219  {
220  tensors[id] = utils::memory::make_deep_unique<OpTensorContent, OpTensorContent>(id, std::forward<Args>(args)...);
221  }
222 
224  using OperatorMap = std::map<Operator::Id, utils::memory::deep_unique_ptr<OperatorContent>>;
225  using OpTensorMap = std::map<OpTensor::Id, utils::memory::deep_unique_ptr<OpTensorContent>>;
226 
227  Implementation() = default;
228  ~Implementation() = default;
229 
231  {
232  return graph0.graph == graph1.graph && graph0.operators == graph1.operators && graph0.tensors == graph1.tensors;
233  }
234 
235  Dependency graph{};
236  OperatorMap operators{};
237  OpTensorMap tensors{};
238  Status status{};
239 };
240 
241 std::vector<const OperatorContent *> traverse(const OperatorGraph::Implementation &graph);
242 
243 std::vector<OperatorContent *> traverse(OperatorGraph::Implementation &graph);
244 
246 
247 } // namespace dynamic_fusion
248 } // namespace experimental
249 } // namespace arm_compute
250 
251 #endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_OPERATORGRAPHIMPL
252 #endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
std::map< Operator::Id, utils::memory::deep_unique_ptr< OperatorContent > > OperatorMap
The dependency graph of a workload, where the nodes are of 2 types: Tensor or Operator Represented as...
Conv2dContent(const OperatorGraph::Implementation *graph, Id id, const Conv2dDescriptor &desc, const ITensorDescPack< OpTensorContent > &tensors, ConvolutionMethod method)
friend bool operator==(const OperatorGraph::Implementation &graph0, const OperatorGraph::Implementation &graph1)
ElementwiseContent(const OperatorGraph::Implementation *graph, Id id, const ElementwiseDescriptor &desc, const ITensorDescPack< OpTensorContent > &tensors)
bool operator==(const OpTensorContent &other) const
FloorContent(const OperatorGraph::Implementation *graph, Id id, const FloorDescriptor &desc, const ITensorDescPack< OpTensorContent > &tensors)
Conv2dContent(const OperatorGraph::Implementation *graph, Id id, const Conv2dDescriptor &desc, const ITensorDescPack< OpTensorContent > &tensors)
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
std::map< OpTensor::Id, utils::memory::deep_unique_ptr< OpTensorContent > > OpTensorMap
Status class.
Definition: Error.h:52
std::vector< const ClKernel * > traverse(const ClKernelFusionGroup &group)
ConvolutionMethod
Available ConvolutionMethod.
Definition: Types.h:134
Status translate(ClKernelGraph &kernel_graph, const OperatorGraph::Implementation &op_graph)
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
Descriptor for Elementwise binary operation.
Descriptor for Conv2dDescriptor operation.
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
OperatorContent(const OperatorGraph::Implementation *graph, Id id, const ITensorDescPack< OpTensorContent > &tensors)
bool operator==(const OpTensor &t0, const OpTensor &t1)