Compute Library
 22.08
ClCompositeOperator.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_DYNAMIC_FUSION_CLCOMPOSITEOPERATOR_H
26 #define ARM_COMPUTE_EXPERIMENTAL_DYNAMIC_FUSION_CLCOMPOSITEOPERATOR_H
27 
31 
33 
34 #include <memory>
35 
36 namespace arm_compute
37 {
38 namespace experimental
39 {
40 namespace dynamic_fusion
41 {
42 /** Map OpTensor handles to their corresponding ITensor memory
43  */
44 using OpTensorBinding = std::map<OpTensor, ITensor *>;
45 
46 /** Map a kernel (as identified by its unit workload id) to its corresponding tensor pack
47  *
48  * @note External user should not use the add_tensor_pack method to alter this tensor pack map, and should only use the map returned by @ref bind_tensors
49  */
51 {
52 public:
53  /** Find a tensor pack associated with the unit workload Id @p uwk_id
54  *
55  * @param[in] uwk_id unit workload Id associated with the tensor pack
56  *
57  * @return ITensorPack*
58  */
60  /** Get a tensor pack associated with @p uwk_id. Throws a exception if it cannot be found.
61  *
62  * @param[in] uwk_id unit workload Id associated with the tensor pack
63  *
64  * @return ITensorPack*
65  */
67  /** Add a tensor pack and associate it with unit workload Id @p uwk_id
68  * @note Should not be used by external user
69  *
70  * @param[in] uwk_id unit workload Id associated with the tensor pack
71  * @param[in] tensor_pack Tensor Pack to be added
72  */
73  void add_tensor_pack(UnitWorkload::Id uwk_id, const ITensorPack &tensor_pack);
74 
75 private:
76  std::map<UnitWorkload::Id, ITensorPack> _tensor_packs{};
77 };
78 
79 /** Holder of any auxiliary CLTensors required by a ClWorkload.
80  *
81  * @note The tensors are not allocated by default, and require the user to explicitly allocate them using the TensorInfo and AuxMemoryInfo
82  *
83  * @note This data holder must remain valid until the ClCompositeOperator that it's passed to is out of scope
84  *
85  * @note External user should not use the add_aux_tensor method, and should only use the data returned by @ref bind_tensors
86  */
88 {
89 public:
90  /** A view of a single auxiliary data and the associated TensorInfo and AuxMemoryInfo
91  */
92  struct DataView
93  {
94  DataView() = default;
95  DataView(CLTensor *tensor, const TensorInfo &tensor_info, const AuxMemoryInfo &memory_info)
96  : tensor{ tensor }, tensor_info{ tensor_info }, memory_info{ memory_info }
97  {
98  }
99  ~DataView() = default;
100  DataView(const DataView &other) = default;
101  DataView &operator=(const DataView &other) = default;
102  DataView(DataView &&other) = default;
103  DataView &operator=(DataView &&other) = default;
104  CLTensor *tensor{}; /**< Pointer to the auxiliary tensor */
105  TensorInfo tensor_info{}; /**< Associated TensorInfo */
106  AuxMemoryInfo memory_info{}; /**< Memory requirement */
107  };
108 
109  /** Add auxiliary tensor.
110  *
111  * @note Should not be used by external user
112  *
113  * @param[in] tensor_id Any Id that can uniquely identify an auxiliary tensor. Usually ClWorkloadTensor Id
114  * @param[in] tensor_info TensorInfo associated with the tensor
115  * @param[in] memory_info Memory requirements
116  *
117  * @return CLTensor* if successfully added, otherwise nullptr
118  */
119  CLTensor *add_aux_tensor(int tensor_id, const ITensorInfo &tensor_info, const AuxMemoryInfo &memory_info);
120 
121  /** Get views of all auxiliary tensors. This is mainly used for allocating the auxiliary tensors.
122  *
123  * @return std::vector<DataView>&
124  */
125  std::vector<DataView> &get_tensors();
126 
127 private:
128  std::map<int, std::unique_ptr<CLTensor>> _owned_tensors{};
129  std::vector<DataView> _tensors{};
130 };
131 
132 /** Bind tensor memory to packs used by prepare and run methods. Create auxiliary tensor objects and their memory requirements if needed
133  *
134  * @note This is the only method for external user to create ClAuxTensorData, and the prepare and run TensorPackMaps
135  *
136  * @param[out] aux_tensor_data Auxiliary Tensors required by the workload
137  * @param[out] prepare_pack_map TensorPackMap used by the prepare method
138  * @param[out] run_pack_map TensorPackMap used by the run method
139  * @param[in] workload ClWorkload to bind the tensors to
140  * @param[in] op_tensors CLTensor memory objects mapped from Core OpTensors
141  *
142  * @return Status
143  */
144 Status bind_tensors(ClAuxTensorData &aux_tensor_data, TensorPackMap &prepare_pack_map, TensorPackMap &run_pack_map, const ClWorkload &workload, const OpTensorBinding &op_tensors);
145 
146 /** Operator runtime to run a @ref ClWorkload
147  *
148  * @note User must explicitly call prepare before run otherwise run will fail.
149  *
150  */
152 {
153 public:
156  /** Configures a @ref ClCompositeOperator with a @ref ClWorkload
157  * This includes the compilation of Cl kernels inside the @ref ClWorkload
158  *
159  * @param[in] ctx CLCompileContext
160  * @param[in] workload ClWorkload to configure with
161  */
162  void configure(const CLCompileContext &ctx, const ClWorkload &workload);
163  /** Validate ClWorkload @p workload
164  *
165  * @param[in] workload ClWorkload to be validated
166  *
167  * @return Status
168  */
169  static Status validate(const ClWorkload &workload);
170  /** Enqueue prepare workloads
171  *
172  * @param tensor_pack_map Tensors required by the prepare workloads
173  */
174  void prepare(TensorPackMap &tensor_pack_map);
175  /** Enqueue run workloads
176  *
177  * @param tensor_pack_map Tensors required by the run workloads
178  */
179  void run(TensorPackMap &tensor_pack_map);
180 
181 private:
182  struct Implementation;
183  std::unique_ptr<Implementation> _impl;
184 };
185 
186 } // namespace dynamic_fusion
187 } // namespace experimental
188 } // namespace arm_compute
189 #endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMIC_FUSION_CLCOMPOSITEOPERATOR_H
190 #endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
DataView(CLTensor *tensor, const TensorInfo &tensor_info, const AuxMemoryInfo &memory_info)
Status validate(const OperatorGraph &op_graph)
Return the validity of op_graph, usually after performing an operation (e.g.
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.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Copyright (c) 2017-2022 Arm Limited.
A view of a single auxiliary data and the associated TensorInfo and AuxMemoryInfo.
CLCompileContext class.
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.
Tensor packing service.
Definition: ITensorPack.h:39
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
im2col_func configure(src_target.info(), dst_target.info(), spatial_kernel, conv_info, has_bias)
Holder of any auxiliary CLTensors required by a ClWorkload.
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