1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2021 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_RUNTIME_CL_MLGO_HEURISTIC_TREE_H 25*c217d954SCole Faust #define SRC_RUNTIME_CL_MLGO_HEURISTIC_TREE_H 26*c217d954SCole Faust 27*c217d954SCole Faust #include "arm_compute/core/Types.h" 28*c217d954SCole Faust #include "src/runtime/CL/mlgo/Common.h" 29*c217d954SCole Faust 30*c217d954SCole Faust #include <map> 31*c217d954SCole Faust #include <memory> 32*c217d954SCole Faust #include <string> 33*c217d954SCole Faust #include <utility> 34*c217d954SCole Faust 35*c217d954SCole Faust namespace arm_compute 36*c217d954SCole Faust { 37*c217d954SCole Faust namespace mlgo 38*c217d954SCole Faust { 39*c217d954SCole Faust /** Conditional ops */ 40*c217d954SCole Faust enum class ConditionalOp 41*c217d954SCole Faust { 42*c217d954SCole Faust EQ, /**< Equal */ 43*c217d954SCole Faust LT, /**< Less than */ 44*c217d954SCole Faust LE, /**< Less than or equal to */ 45*c217d954SCole Faust GT, /**< Greater than */ 46*c217d954SCole Faust GE, /**< Greater than or equal to */ 47*c217d954SCole Faust }; 48*c217d954SCole Faust 49*c217d954SCole Faust /** A branch condition expression evaluating: feature op threshold */ 50*c217d954SCole Faust struct Condition 51*c217d954SCole Faust { 52*c217d954SCole Faust std::string feature; /**< Feature name */ 53*c217d954SCole Faust ConditionalOp op; /**< Condtional op */ 54*c217d954SCole Faust float threshold; /**< Threshold value */ 55*c217d954SCole Faust }; 56*c217d954SCole Faust 57*c217d954SCole Faust /** GEMM Shape used for query */ 58*c217d954SCole Faust struct GEMMShape 59*c217d954SCole Faust { 60*c217d954SCole Faust unsigned int m; /**< Number of rows for the lhs matrix. Lhs matrix NOT transposed */ 61*c217d954SCole Faust unsigned int n; /**< Number of columns for the rhs matrix. Rhs matrix NOT transposed */ 62*c217d954SCole Faust unsigned int k; /**< Number of rows for the rhs matrix. Rhs matrix NOT transposed */ 63*c217d954SCole Faust unsigned int b; /**< Batch size */ 64*c217d954SCole Faust }; 65*c217d954SCole Faust 66*c217d954SCole Faust /** A binary decision tree based heuristic */ 67*c217d954SCole Faust class HeuristicTree 68*c217d954SCole Faust { 69*c217d954SCole Faust public: 70*c217d954SCole Faust using NodeID = size_t; 71*c217d954SCole Faust using TreeID = size_t; 72*c217d954SCole Faust using Index = std::tuple<HeuristicType, std::string, DataType>; 73*c217d954SCole Faust enum class NodeType 74*c217d954SCole Faust { 75*c217d954SCole Faust Branch, 76*c217d954SCole Faust Leaf 77*c217d954SCole Faust }; 78*c217d954SCole Faust struct Node 79*c217d954SCole Faust { 80*c217d954SCole Faust virtual NodeType type() const = 0; 81*c217d954SCole Faust virtual ~Node() = default; 82*c217d954SCole Faust }; 83*c217d954SCole Faust 84*c217d954SCole Faust struct BranchNode : public Node 85*c217d954SCole Faust { BranchNodeBranchNode86*c217d954SCole Faust BranchNode(NodeID id, Condition cond, NodeID t_node, NodeID f_node) 87*c217d954SCole Faust : id{ id }, condition{ cond }, true_node{ t_node }, false_node{ f_node } 88*c217d954SCole Faust { 89*c217d954SCole Faust } typeBranchNode90*c217d954SCole Faust NodeType type() const override 91*c217d954SCole Faust { 92*c217d954SCole Faust return NodeType::Branch; 93*c217d954SCole Faust } 94*c217d954SCole Faust NodeID id; 95*c217d954SCole Faust Condition condition; 96*c217d954SCole Faust NodeID true_node; 97*c217d954SCole Faust NodeID false_node; 98*c217d954SCole Faust }; 99*c217d954SCole Faust 100*c217d954SCole Faust template <typename T> 101*c217d954SCole Faust struct LeafNode : public Node 102*c217d954SCole Faust { LeafNodeLeafNode103*c217d954SCole Faust LeafNode(NodeID id, T val) 104*c217d954SCole Faust : id{ id }, value{ val } 105*c217d954SCole Faust { 106*c217d954SCole Faust } typeLeafNode107*c217d954SCole Faust NodeType type() const override 108*c217d954SCole Faust { 109*c217d954SCole Faust return NodeType::Leaf; 110*c217d954SCole Faust } 111*c217d954SCole Faust NodeID id; 112*c217d954SCole Faust T value; 113*c217d954SCole Faust }; 114*c217d954SCole Faust 115*c217d954SCole Faust public: 116*c217d954SCole Faust /** Constructor */ 117*c217d954SCole Faust HeuristicTree(); 118*c217d954SCole Faust /** Constructor */ 119*c217d954SCole Faust HeuristicTree(TreeID id, HeuristicType h_type, const std::string &ip_target, DataType data_type); 120*c217d954SCole Faust // Since the HeuristicTree is a handle that owns the the nodes, it is move-only 121*c217d954SCole Faust /** Prevent copy construction */ 122*c217d954SCole Faust HeuristicTree(const HeuristicTree &) = delete; 123*c217d954SCole Faust /** Prevent copy assignment */ 124*c217d954SCole Faust HeuristicTree &operator=(const HeuristicTree &) = delete; 125*c217d954SCole Faust /** Move constructor */ 126*c217d954SCole Faust HeuristicTree(HeuristicTree &&other) noexcept = default; 127*c217d954SCole Faust /** Move assignment */ 128*c217d954SCole Faust HeuristicTree &operator=(HeuristicTree &&other) = default; 129*c217d954SCole Faust 130*c217d954SCole Faust /** Query a leaf value given a gemm shape 131*c217d954SCole Faust * 132*c217d954SCole Faust * @tparam T Leaf value type 133*c217d954SCole Faust * @param shape A @ref GEMMShape for the query 134*c217d954SCole Faust * @return std::pair<bool, T> Outcome contains bool, signalling if the query succeeded or not 135*c217d954SCole Faust */ 136*c217d954SCole Faust template <typename T> 137*c217d954SCole Faust std::pair<bool, T> query(GEMMShape shape) const; 138*c217d954SCole Faust 139*c217d954SCole Faust /** Add a leaf node 140*c217d954SCole Faust * 141*c217d954SCole Faust * @tparam T Leaf value type 142*c217d954SCole Faust * @param id Leaf node ID 143*c217d954SCole Faust * @param leaf_value Leaf node value 144*c217d954SCole Faust * @return bool If the addition succeeded or not 145*c217d954SCole Faust */ 146*c217d954SCole Faust template <typename T> 147*c217d954SCole Faust bool add_leaf(NodeID id, T leaf_value); 148*c217d954SCole Faust /** Add a branch node 149*c217d954SCole Faust * 150*c217d954SCole Faust * @param id Branch node ID 151*c217d954SCole Faust * @param cond Branch node @ref Condition 152*c217d954SCole Faust * @param true_node True node's ID 153*c217d954SCole Faust * @param false_node False node's ID 154*c217d954SCole Faust * @return bool If the addition succeeded or not 155*c217d954SCole Faust */ 156*c217d954SCole Faust bool add_branch(NodeID id, Condition cond, NodeID true_node, NodeID false_node); 157*c217d954SCole Faust 158*c217d954SCole Faust /** Get tree ID 159*c217d954SCole Faust * @return TreeID 160*c217d954SCole Faust */ id()161*c217d954SCole Faust TreeID id() const 162*c217d954SCole Faust { 163*c217d954SCole Faust return _id; 164*c217d954SCole Faust } 165*c217d954SCole Faust 166*c217d954SCole Faust /** Get tree index 167*c217d954SCole Faust * @return Index 168*c217d954SCole Faust */ index()169*c217d954SCole Faust Index index() const 170*c217d954SCole Faust { 171*c217d954SCole Faust return std::make_tuple(_heuristic_type, _ip_target, _data_type); 172*c217d954SCole Faust } 173*c217d954SCole Faust 174*c217d954SCole Faust /** Check if tree is valid 175*c217d954SCole Faust * @return bool 176*c217d954SCole Faust */ 177*c217d954SCole Faust bool check(); 178*c217d954SCole Faust 179*c217d954SCole Faust private: 180*c217d954SCole Faust static constexpr size_t _max_query_depth{ 1000 }; // Maximum depth of query 181*c217d954SCole Faust static constexpr size_t _max_num_nodes{ 100000 }; // Maximum number of nodes contained by the tree 182*c217d954SCole Faust static constexpr NodeID _root{ 0 }; // Root tree ID 183*c217d954SCole Faust 184*c217d954SCole Faust private: 185*c217d954SCole Faust bool check_if_structurally_correct() const; 186*c217d954SCole Faust 187*c217d954SCole Faust private: 188*c217d954SCole Faust TreeID _id; /**< Heuristic tree ID */ 189*c217d954SCole Faust HeuristicType _heuristic_type; /**< Heuristic type */ 190*c217d954SCole Faust std::string _ip_target; /**< IP target associated with the tree */ 191*c217d954SCole Faust DataType _data_type; /**< Data type associated with the tree */ 192*c217d954SCole Faust std::map<NodeID, std::unique_ptr<Node>> _tree; /**< Tree representation */ 193*c217d954SCole Faust }; 194*c217d954SCole Faust } // namespace mlgo 195*c217d954SCole Faust 196*c217d954SCole Faust } // namespace arm_compute 197*c217d954SCole Faust 198*c217d954SCole Faust #endif //SRC_RUNTIME_CL_MLGO_HEURISTIC_TREE_H