1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2022 Arm Limited. 3*c217d954SCole Faust * 4*c217d954SCole Faust * SPDX-License-Identifier: MIT 5*c217d954SCole Faust * 6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy 7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to 8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the 9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is 11*c217d954SCole Faust * furnished to do so, subject to the following conditions: 12*c217d954SCole Faust * 13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all 14*c217d954SCole Faust * copies or substantial portions of the Software. 15*c217d954SCole Faust * 16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*c217d954SCole Faust * SOFTWARE. 23*c217d954SCole Faust */ 24*c217d954SCole Faust #ifndef SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK 25*c217d954SCole Faust #define SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK 26*c217d954SCole Faust 27*c217d954SCole Faust #include "arm_compute/core/experimental/Types.h" 28*c217d954SCole Faust #include <unordered_map> 29*c217d954SCole Faust #include <vector> 30*c217d954SCole Faust 31*c217d954SCole Faust namespace arm_compute 32*c217d954SCole Faust { 33*c217d954SCole Faust namespace experimental 34*c217d954SCole Faust { 35*c217d954SCole Faust namespace dynamic_fusion 36*c217d954SCole Faust { 37*c217d954SCole Faust /** This is a generic class that packs the arguments of an operator. For now, it is only used for tensor-related types 38*c217d954SCole Faust * Examples of "tensor-related types": @ref ITensorInfo, @ref ITensor, @ref ICLTensor 39*c217d954SCole Faust * 40*c217d954SCole Faust * The argument id is the position of the argument within the pack, and is represented by @ref TensorType 41*c217d954SCole Faust * 42*c217d954SCole Faust * @tparam T Tensor-related type 43*c217d954SCole Faust */ 44*c217d954SCole Faust template <typename T> 45*c217d954SCole Faust class ArgumentPack 46*c217d954SCole Faust { 47*c217d954SCole Faust public: 48*c217d954SCole Faust /** @ref TensorType encodes the position of a tensor argument within the pack */ 49*c217d954SCole Faust using Id = TensorType; 50*c217d954SCole Faust /** A single argument element within the pack 51*c217d954SCole Faust * It contains either a const pointer or a non-const pointer to the Tensor-related type T, but never at the same time 52*c217d954SCole Faust */ 53*c217d954SCole Faust struct PackElement 54*c217d954SCole Faust { 55*c217d954SCole Faust PackElement() = default; 56*c217d954SCole Faust PackElement(const PackElement &elem) = default; 57*c217d954SCole Faust PackElement &operator=(const PackElement &elem) = default; 58*c217d954SCole Faust PackElement(PackElement &&elem) = default; 59*c217d954SCole Faust PackElement &operator=(PackElement &&elem) = default; PackElementPackElement60*c217d954SCole Faust PackElement(Id id, T *tensor) 61*c217d954SCole Faust : id(id), tensor(tensor), ctensor(nullptr) 62*c217d954SCole Faust { 63*c217d954SCole Faust } PackElementPackElement64*c217d954SCole Faust PackElement(Id id, const T *ctensor) 65*c217d954SCole Faust : id(id), tensor(nullptr), ctensor(ctensor) 66*c217d954SCole Faust { 67*c217d954SCole Faust } 68*c217d954SCole Faust 69*c217d954SCole Faust Id id{ ACL_UNKNOWN }; /**< Argument id within the pack */ 70*c217d954SCole Faust T *tensor{ nullptr }; /**< Non-const pointer to tensor-related object */ 71*c217d954SCole Faust const T *ctensor 72*c217d954SCole Faust { 73*c217d954SCole Faust nullptr 74*c217d954SCole Faust }; /**< Const pointer to tensor-related object */ 75*c217d954SCole Faust }; 76*c217d954SCole Faust 77*c217d954SCole Faust public: 78*c217d954SCole Faust /** Default constructor */ 79*c217d954SCole Faust ArgumentPack() = default; 80*c217d954SCole Faust /** Destructor */ 81*c217d954SCole Faust ~ArgumentPack() = default; 82*c217d954SCole Faust /** Allow instances of this class to be copy constructed */ 83*c217d954SCole Faust ArgumentPack<T>(const ArgumentPack<T> &other) = default; 84*c217d954SCole Faust /** Allow instances of this class to be copied */ 85*c217d954SCole Faust ArgumentPack<T> &operator=(const ArgumentPack<T> &other) = default; 86*c217d954SCole Faust /** Allow instances of this class to be move constructed */ 87*c217d954SCole Faust ArgumentPack<T>(ArgumentPack<T> &&other) = default; 88*c217d954SCole Faust /** Allow instances of this class to be moved */ 89*c217d954SCole Faust ArgumentPack<T> &operator=(ArgumentPack<T> &&other) = default; 90*c217d954SCole Faust /** Initializer list Constructor */ ArgumentPack(const std::initializer_list<PackElement> & l)91*c217d954SCole Faust ArgumentPack(const std::initializer_list<PackElement> &l) 92*c217d954SCole Faust : _pack{} 93*c217d954SCole Faust { 94*c217d954SCole Faust for(const auto &e : l) 95*c217d954SCole Faust { 96*c217d954SCole Faust _pack[e.id] = e; 97*c217d954SCole Faust } 98*c217d954SCole Faust } 99*c217d954SCole Faust /** Add tensor to the pack 100*c217d954SCole Faust * 101*c217d954SCole Faust * @param[in] id ID of the tensor to add 102*c217d954SCole Faust * @param[in] tensor Tensor to add 103*c217d954SCole Faust */ add_tensor(Id id,T * tensor)104*c217d954SCole Faust void add_tensor(Id id, T *tensor) 105*c217d954SCole Faust { 106*c217d954SCole Faust _pack[id] = PackElement(id, tensor); 107*c217d954SCole Faust } 108*c217d954SCole Faust /** Add const tensor to the pack 109*c217d954SCole Faust * 110*c217d954SCole Faust * @param[in] id ID of the tensor to add 111*c217d954SCole Faust * @param[in] tensor Tensor to add 112*c217d954SCole Faust */ add_const_tensor(Id id,const T * tensor)113*c217d954SCole Faust void add_const_tensor(Id id, const T *tensor) 114*c217d954SCole Faust { 115*c217d954SCole Faust _pack[id] = PackElement(id, tensor); 116*c217d954SCole Faust } 117*c217d954SCole Faust /** Get tensor of a given id from the pack 118*c217d954SCole Faust * 119*c217d954SCole Faust * @param[in] id ID of tensor to extract 120*c217d954SCole Faust * 121*c217d954SCole Faust * @return The pointer to the tensor if exist and is non-const else nullptr 122*c217d954SCole Faust */ get_tensor(Id id)123*c217d954SCole Faust T *get_tensor(Id id) 124*c217d954SCole Faust { 125*c217d954SCole Faust auto it = _pack.find(id); 126*c217d954SCole Faust return it != _pack.end() ? it->second.tensor : nullptr; 127*c217d954SCole Faust } 128*c217d954SCole Faust /** Get constant tensor of a given id 129*c217d954SCole Faust * 130*c217d954SCole Faust * @param[in] id ID of tensor to extract 131*c217d954SCole Faust * 132*c217d954SCole Faust * @return The pointer to the tensor (const or not) if exist else nullptr 133*c217d954SCole Faust */ get_const_tensor(Id id)134*c217d954SCole Faust const T *get_const_tensor(Id id) const 135*c217d954SCole Faust { 136*c217d954SCole Faust auto it = _pack.find(id); 137*c217d954SCole Faust if(it != _pack.end()) 138*c217d954SCole Faust { 139*c217d954SCole Faust return it->second.ctensor != nullptr ? it->second.ctensor : it->second.tensor; 140*c217d954SCole Faust } 141*c217d954SCole Faust return nullptr; 142*c217d954SCole Faust } 143*c217d954SCole Faust /** Remove the tensor stored with the given id 144*c217d954SCole Faust * 145*c217d954SCole Faust * @param[in] id ID of tensor to remove 146*c217d954SCole Faust */ remove_tensor(Id id)147*c217d954SCole Faust void remove_tensor(Id id) 148*c217d954SCole Faust { 149*c217d954SCole Faust _pack.erase(id); 150*c217d954SCole Faust } 151*c217d954SCole Faust /** Pack size accessor 152*c217d954SCole Faust * 153*c217d954SCole Faust * @return Number of tensors registered to the pack 154*c217d954SCole Faust */ size()155*c217d954SCole Faust size_t size() const 156*c217d954SCole Faust { 157*c217d954SCole Faust return _pack.size(); 158*c217d954SCole Faust } 159*c217d954SCole Faust /** Checks if pack is empty 160*c217d954SCole Faust * 161*c217d954SCole Faust * @return True if empty else false 162*c217d954SCole Faust */ empty()163*c217d954SCole Faust bool empty() const 164*c217d954SCole Faust { 165*c217d954SCole Faust return _pack.empty(); 166*c217d954SCole Faust } 167*c217d954SCole Faust /** Get the ACL_SRC_* tensors 168*c217d954SCole Faust * 169*c217d954SCole Faust * @return std::vector<T *> 170*c217d954SCole Faust */ get_src_tensors()171*c217d954SCole Faust std::vector<T *> get_src_tensors() 172*c217d954SCole Faust { 173*c217d954SCole Faust std::vector<T *> src_tensors{}; 174*c217d954SCole Faust for(int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id) 175*c217d954SCole Faust { 176*c217d954SCole Faust auto tensor = get_tensor(static_cast<TensorType>(id)); 177*c217d954SCole Faust if(tensor != nullptr) 178*c217d954SCole Faust { 179*c217d954SCole Faust src_tensors.push_back(tensor); 180*c217d954SCole Faust } 181*c217d954SCole Faust } 182*c217d954SCole Faust return src_tensors; 183*c217d954SCole Faust } 184*c217d954SCole Faust /** Get the const ACL_SRC_* tensors 185*c217d954SCole Faust * 186*c217d954SCole Faust * @return std::vector<const T *> 187*c217d954SCole Faust */ get_const_src_tensors()188*c217d954SCole Faust std::vector<const T *> get_const_src_tensors() const 189*c217d954SCole Faust { 190*c217d954SCole Faust std::vector<const T *> src_tensors{}; 191*c217d954SCole Faust for(int id = static_cast<int>(TensorType::ACL_SRC); id <= static_cast<int>(TensorType::ACL_SRC_END); ++id) 192*c217d954SCole Faust { 193*c217d954SCole Faust auto tensor = get_const_tensor(static_cast<TensorType>(id)); 194*c217d954SCole Faust if(tensor != nullptr) 195*c217d954SCole Faust { 196*c217d954SCole Faust src_tensors.push_back(tensor); 197*c217d954SCole Faust } 198*c217d954SCole Faust } 199*c217d954SCole Faust return src_tensors; 200*c217d954SCole Faust } 201*c217d954SCole Faust /** Get the ACL_DST_* tensors 202*c217d954SCole Faust * 203*c217d954SCole Faust * @return std::vector<T *> 204*c217d954SCole Faust */ get_dst_tensors()205*c217d954SCole Faust std::vector<T *> get_dst_tensors() 206*c217d954SCole Faust { 207*c217d954SCole Faust std::vector<T *> dst_tensors{}; 208*c217d954SCole Faust for(int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id) 209*c217d954SCole Faust { 210*c217d954SCole Faust auto tensor = get_tensor(static_cast<TensorType>(id)); 211*c217d954SCole Faust if(tensor != nullptr) 212*c217d954SCole Faust { 213*c217d954SCole Faust dst_tensors.push_back(tensor); 214*c217d954SCole Faust } 215*c217d954SCole Faust } 216*c217d954SCole Faust return dst_tensors; 217*c217d954SCole Faust } 218*c217d954SCole Faust /** Get the const ACL_DST_* tensors 219*c217d954SCole Faust * 220*c217d954SCole Faust * @return std::vector<const T *> 221*c217d954SCole Faust */ get_const_dst_tensors()222*c217d954SCole Faust std::vector<const T *> get_const_dst_tensors() const 223*c217d954SCole Faust { 224*c217d954SCole Faust std::vector<const T *> dst_tensors{}; 225*c217d954SCole Faust for(int id = static_cast<int>(TensorType::ACL_DST); id <= static_cast<int>(TensorType::ACL_DST_END); ++id) 226*c217d954SCole Faust { 227*c217d954SCole Faust auto tensor = get_const_tensor(static_cast<TensorType>(id)); 228*c217d954SCole Faust if(tensor != nullptr) 229*c217d954SCole Faust { 230*c217d954SCole Faust dst_tensors.push_back(tensor); 231*c217d954SCole Faust } 232*c217d954SCole Faust } 233*c217d954SCole Faust return dst_tensors; 234*c217d954SCole Faust } 235*c217d954SCole Faust 236*c217d954SCole Faust private: 237*c217d954SCole Faust std::unordered_map<int, PackElement> _pack{}; /**< Container with the packed tensors */ 238*c217d954SCole Faust }; 239*c217d954SCole Faust } // namespace dynamic_fusion 240*c217d954SCole Faust } // namespace experimental 241*c217d954SCole Faust } // namespace arm_compute 242*c217d954SCole Faust #endif /* SRC_DYNAMIC_FUSION_SKETCH_ARGUMENTPACK */ 243