Compute Library
 22.08
ClCompositeOperator.cpp
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
26 
30 #include "support/Cast.h"
31 
32 namespace arm_compute
33 {
34 namespace experimental
35 {
36 namespace dynamic_fusion
37 {
38 namespace
39 {
40 Status add_tensor_to_tensor_pack(int wk_tensor_id, ICLTensor *tensor, const ClWorkload &workload, TensorPackMap &prepare_pack_map, TensorPackMap &run_pack_map)
41 {
42  if(tensor == nullptr)
43  {
44  return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Trying to add a nullptr into the tensor packs");
45  }
46  const auto bp_tensor_id = workload.tensors.at(wk_tensor_id).kernel_arg.arg_id; // blueprint tensor id
47  std::vector<ClWorkload::UnitWorkId> uwk_ids{};
48  const auto src_uwk_ids = workload.graph.src_ops_from_tensor(wk_tensor_id);
49  const auto dst_uwk_ids = workload.graph.dst_ops_from_tensor(wk_tensor_id);
50  uwk_ids.insert(uwk_ids.end(), src_uwk_ids.begin(), src_uwk_ids.end());
51  uwk_ids.insert(uwk_ids.end(), dst_uwk_ids.begin(), dst_uwk_ids.end());
52 
53  for(auto uwk_id : uwk_ids)
54  {
55  TensorPackMap *pack_map = nullptr;
56  const auto uwk_stage = workload.unit_workloads.at(uwk_id).stage.stage;
57  switch(uwk_stage)
58  {
60  pack_map = &run_pack_map;
61  break;
63  pack_map = &prepare_pack_map;
64  break;
65  default:
66  return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported workload stage");
67  }
68 
69  ITensorPack *tensor_pack = pack_map->find_tensor_pack(uwk_id);
70  if(tensor_pack == nullptr)
71  {
72  pack_map->add_tensor_pack(uwk_id, ITensorPack{ { bp_tensor_id, tensor } });
73  }
74  else
75  {
76  tensor_pack->add_tensor(bp_tensor_id, tensor);
77  }
78  }
79  return Status{};
80 }
81 
82 } // namespace
83 
85 {
86  auto tensor_pack = _tensor_packs.find(uwk_id);
87  if(tensor_pack != _tensor_packs.end())
88  {
89  return &(tensor_pack->second);
90  }
91  return nullptr;
92 }
93 
95 {
96  return _tensor_packs.at(uwk_id);
97 }
98 
100 {
101  _tensor_packs[uwk_id] = tensor_pack;
102 }
103 
104 Status bind_tensors(ClAuxTensorData &aux_tensor_data, TensorPackMap &prepare_pack_map, TensorPackMap &run_pack_map, const ClWorkload &workload, const OpTensorBinding &op_tensors)
105 {
106  for(auto tensor : workload.tensors)
107  {
108  const auto wk_tensor_id = tensor.first; // workload tensor id
109  ICLTensor *tensor_object = nullptr;
110  if(tensor.second.memory_type == MemoryType::Core)
111  {
112  const auto op_tensor_id = workload.op_tensor_id_lut.at(wk_tensor_id);
113  auto op_tensor_find = op_tensors.find(op_tensor_id);
114  if(op_tensor_find == op_tensors.end())
115  {
116  return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Cannot find binding for some operator tensor");
117  }
118  tensor_object = utils::cast::polymorphic_downcast<ICLTensor *>(op_tensor_find->second);
119  }
120  else if(tensor.second.memory_type == MemoryType::Auxiliary)
121  {
122  // Create aux tensor CLTensor object
123  const TensorInfo tensor_info = *tensor.second.info;
124  const auto memory_info = tensor.second.memory_info;
125  tensor_object = aux_tensor_data.add_aux_tensor(wk_tensor_id, tensor_info, memory_info);
126  }
127  else
128  {
129  return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported tensor memory type");
130  }
131 
132  const auto st = add_tensor_to_tensor_pack(wk_tensor_id, tensor_object, workload, prepare_pack_map, run_pack_map);
134  }
135  return Status{};
136 }
137 
138 CLTensor *ClAuxTensorData::add_aux_tensor(int tensor_id, const ITensorInfo &tensor_info, const AuxMemoryInfo &memory_info)
139 {
140  auto find_tensor_pair = _owned_tensors.find(tensor_id);
141  if(find_tensor_pair == _owned_tensors.end())
142  {
143  return find_tensor_pair->second.get();
144  }
145  else
146  {
147  auto tensor = std::make_unique<CLTensor>();
148  auto inserted_pair = _owned_tensors.emplace(tensor_id, std::move(tensor)).first;
149  auto new_tensor = inserted_pair->second.get();
150  _tensors.emplace_back(new_tensor, tensor_info, memory_info);
151  return new_tensor;
152  }
153 }
154 
155 std::vector<ClAuxTensorData::DataView> &ClAuxTensorData::get_tensors()
156 {
157  return _tensors;
158 }
159 struct ClCompositeOperator::Implementation
160 {
161  std::map<UnitWorkload::Id, std::unique_ptr<ClCompositeKernel>> _kernels{};
162  std::map<UnitWorkload::Id, std::unique_ptr<ClCompositeKernel>> _kernels_prep{};
163  ClWorkload _workload{};
164  bool _is_prepared{ false };
165 };
166 
168  : _impl{ std::make_unique<Implementation>() }
169 {
170 }
171 
173 
175 {
177  _impl->_workload = workload;
178 
179  // Traverse workloads in topological order
180  const auto sorted = workload.graph.topological_sort().second;
181  for(const auto &node : sorted)
182  {
183  auto work = workload.unit_workloads.at(node.op);
184  auto stage = work.stage.stage;
185  auto k = std::make_unique<ClCompositeKernel>();
186  k->configure(ctx, work.code);
187 
188  switch(stage)
189  {
191  _impl->_kernels.emplace(work.id, std::move(k));
192  break;
194  _impl->_kernels_prep.emplace(work.id, std::move(k));
195  break;
196  default:
197  ARM_COMPUTE_ERROR("Invalid stage");
198  }
199  break;
200  }
201 }
202 
204 {
205  return workload.status;
206 }
207 
209 {
210  if(!_impl->_is_prepared)
211  {
212  for(auto &id_kernel_pair : _impl->_kernels_prep)
213  {
214  const bool flush_queue = false;
215  const auto uwk_id = id_kernel_pair.first;
216  auto kernel = id_kernel_pair.second.get();
217  CLScheduler::get().enqueue_op(*kernel, tensor_pack_map.get_tensor_pack(uwk_id), ClExecutionDescriptor{}, flush_queue);
218  }
219 
220  _impl->_is_prepared = true;
221  }
222 }
223 
225 {
226  ARM_COMPUTE_ERROR_ON_MSG(!_impl->_is_prepared, "Operator is not prepared");
227 
228  for(auto &id_kernel_pair : _impl->_kernels)
229  {
230  // Flush the command queue on the last kernel
231  const bool flush_queue = false;
232  const auto uwk_id = id_kernel_pair.first;
233  auto kernel = id_kernel_pair.second.get();
234  CLScheduler::get().enqueue_op(*kernel, tensor_pack_map.get_tensor_pack(uwk_id), ClExecutionDescriptor{}, flush_queue);
235  }
236 }
237 
238 } // namespace dynamic_fusion
239 } // namespace experimental
240 } // namespace arm_compute
241 #endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
DependencyGraph graph
Dependency graph of the workload tensors and the unit workloads.
Definition: IWorkload.h:125
static CLScheduler & get()
Access the scheduler singleton.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
std::map< Tid, ClWorkloadTensor > tensors
Workload tensors.
Definition: ClWorkload.h:200
void add_tensor_pack(UnitWorkload::Id uwk_id, const ITensorPack &tensor_pack)
Add a tensor pack and associate it with unit workload Id uwk_id.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Core memory used by the Workload Tensor, e.g.
Status class.
Definition: Error.h:52
std::pair< Status, std::vector< OpPack > > topological_sort() const
Sort the graph in a topological order.
CLTensor * add_aux_tensor(int tensor_id, const ITensorInfo &tensor_info, const AuxMemoryInfo &memory_info)
Add auxiliary tensor.
Copyright (c) 2017-2022 Arm Limited.
std::vector< DataView > & get_tensors()
Get views of all auxiliary tensors.
Descriptor containing information required to run a single ClWorkload.
Definition: ClWorkload.h:91
void run(TensorPackMap &tensor_pack_map)
Enqueue run workloads.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush=true)
Schedule the execution of the passed kernel if possible.
Auxiliary memory required by the Workload Tensor, e.g.
CLCompileContext class.
Status status
For compatibility with the IOperator validate method.
Definition: ClWorkload.h:202
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
void prepare(TensorPackMap &tensor_pack_map)
Enqueue prepare workloads.
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
Memory Info for a WorkloadTensor of Auxiliary memory type.
Definition: IWorkload.h:70
std::map< OpTensor, ITensor * > OpTensorBinding
Map OpTensor handles to their corresponding ITensor memory.
std::map< Tid, OpTensor::Id > op_tensor_id_lut
Map from ClWorkloadTensor to SRC and DST Operator Tensors (no need to store "intermediate" Operator T...
Definition: ClWorkload.h:201
void configure(const CLCompileContext &ctx, const ClWorkload &workload)
Configures a ClCompositeOperator with a ClWorkload This includes the compilation of Cl kernels inside...
Tensor packing service.
Definition: ITensorPack.h:39
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
Holder of any auxiliary CLTensors required by a ClWorkload.
std::map< UnitWorkId, ClUnitWorkload > unit_workloads
Unit workloads to run.
Definition: ClWorkload.h:199
static Status validate(const ClWorkload &workload)
Validate ClWorkload workload.
Map a kernel (as identified by its unit workload id) to its corresponding tensor pack.
Status bind_tensors(ClAuxTensorData &aux_tensor_data, TensorPackMap &prepare_pack_map, TensorPackMap &run_pack_map, const ClWorkload &workload, const OpTensorBinding &op_tensors)
Bind tensor memory to packs used by prepare and run methods.
ITensorPack & get_tensor_pack(UnitWorkload::Id uwk_id)
Get a tensor pack associated with uwk_id.
ITensorPack * find_tensor_pack(UnitWorkload::Id uwk_id)
Find a tensor pack associated with the unit workload Id uwk_id.
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41