|
| 1 | +/*! |
| 2 | + * Copyright (c) 2016 by Contributors |
| 3 | + * \file packed_func_ext.h |
| 4 | + * \brief Extension package to PackedFunc |
| 5 | + * This enales pass NodeRef types into/from PackedFunc. |
| 6 | + */ |
| 7 | +#ifndef TVM_PACKED_FUNC_EXT_H_ |
| 8 | +#define TVM_PACKED_FUNC_EXT_H_ |
| 9 | + |
| 10 | +#include <sstream> |
| 11 | +#include <string> |
| 12 | +#include <memory> |
| 13 | +#include <type_traits> |
| 14 | + |
| 15 | +#include "./base.h" |
| 16 | +#include "./expr.h" |
| 17 | + |
| 18 | +namespace tvm { |
| 19 | +using runtime::TVMArgs; |
| 20 | +using runtime::TVMRetValue; |
| 21 | +using runtime::PackedFunc; |
| 22 | + |
| 23 | +namespace runtime { |
| 24 | +/*! |
| 25 | + * \brief Runtime type checker for node type. |
| 26 | + * \tparam T the type to be checked. |
| 27 | + */ |
| 28 | +template<typename T> |
| 29 | +struct NodeTypeChecker { |
| 30 | + static inline bool Check(Node* sptr) { |
| 31 | + // This is the only place in the project where RTTI is used |
| 32 | + // It can be turned off, but will make non strict checking. |
| 33 | + // TODO(tqchen) possibly find alternative to turn of RTTI |
| 34 | + using ContainerType = typename T::ContainerType; |
| 35 | + return (dynamic_cast<ContainerType*>(sptr) != nullptr); |
| 36 | + } |
| 37 | + static inline void PrintName(std::ostringstream& os) { // NOLINT(*) |
| 38 | + using ContainerType = typename T::ContainerType; |
| 39 | + os << ContainerType::_type_key; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +template<typename T> |
| 44 | +struct NodeTypeChecker<Array<T> > { |
| 45 | + static inline bool Check(Node* sptr) { |
| 46 | + if (sptr == nullptr) return false; |
| 47 | + if (!sptr->is_type<ArrayNode>()) return false; |
| 48 | + ArrayNode* n = static_cast<ArrayNode*>(sptr); |
| 49 | + for (const auto& p : n->data) { |
| 50 | + if (!NodeTypeChecker<T>::Check(p.get())) return false; |
| 51 | + } |
| 52 | + return true; |
| 53 | + } |
| 54 | + static inline void PrintName(std::ostringstream& os) { // NOLINT(*) |
| 55 | + os << "array<"; |
| 56 | + NodeTypeChecker<T>::PrintName(os); |
| 57 | + os << ">"; |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +template<typename K, typename V> |
| 62 | +struct NodeTypeChecker<Map<K, V> > { |
| 63 | + static inline bool Check(Node* sptr) { |
| 64 | + if (sptr == nullptr) return false; |
| 65 | + if (!sptr->is_type<MapNode>()) return false; |
| 66 | + MapNode* n = static_cast<MapNode*>(sptr); |
| 67 | + for (const auto& kv : n->data) { |
| 68 | + if (!NodeTypeChecker<K>::Check(kv.first.get())) return false; |
| 69 | + if (!NodeTypeChecker<V>::Check(kv.second.get())) return false; |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | + static inline void PrintName(std::ostringstream& os) { // NOLINT(*) |
| 74 | + os << "map<"; |
| 75 | + NodeTypeChecker<K>::PrintName(os); |
| 76 | + os << ','; |
| 77 | + NodeTypeChecker<V>::PrintName(os); |
| 78 | + os << '>'; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +template<typename T> |
| 83 | +inline std::string NodeTypeName() { |
| 84 | + std::ostringstream os; |
| 85 | + NodeTypeChecker<T>::PrintName(os); |
| 86 | + return os.str(); |
| 87 | +} |
| 88 | + |
| 89 | +// extensions for tvm arg value |
| 90 | + |
| 91 | +template<typename TNodeRef, typename> |
| 92 | +inline TVMArgValue::operator TNodeRef() const { |
| 93 | + static_assert( |
| 94 | + std::is_base_of<NodeRef, TNodeRef>::value, |
| 95 | + "Conversion only works for NodeRef"); |
| 96 | + if (type_code_ == kNull) return TNodeRef(); |
| 97 | + TVM_CHECK_TYPE_CODE(type_code_, kNodeHandle); |
| 98 | + std::shared_ptr<Node>& sptr = *ptr<std::shared_ptr<Node> >(); |
| 99 | + CHECK(NodeTypeChecker<TNodeRef>::Check(sptr.get())) |
| 100 | + << "Expected type " << NodeTypeName<TNodeRef>() |
| 101 | + << " but get " << sptr->type_key(); |
| 102 | + return TNodeRef(sptr); |
| 103 | +} |
| 104 | + |
| 105 | +inline TVMArgValue::operator Halide::Expr() const { |
| 106 | + if (type_code_ == kNull) return Expr(); |
| 107 | + if (type_code_ == kInt) { |
| 108 | + return Expr(static_cast<int>(value_.v_int64)); |
| 109 | + } |
| 110 | + if (type_code_ == kFloat) { |
| 111 | + return Expr(static_cast<float>(value_.v_float64)); |
| 112 | + } |
| 113 | + TVM_CHECK_TYPE_CODE(type_code_, kNodeHandle); |
| 114 | + std::shared_ptr<Node>& sptr = *ptr<std::shared_ptr<Node> >(); |
| 115 | + if (sptr->is_type<IterVarNode>()) { |
| 116 | + return IterVar(sptr)->var; |
| 117 | + } |
| 118 | + CHECK(NodeTypeChecker<Expr>::Check(sptr.get())) |
| 119 | + << "Expected type " << NodeTypeName<Expr>() |
| 120 | + << " but get " << sptr->type_key(); |
| 121 | + return Expr(sptr); |
| 122 | +} |
| 123 | + |
| 124 | +inline std::shared_ptr<Node>& TVMArgValue::node_sptr() { |
| 125 | + TVM_CHECK_TYPE_CODE(type_code_, kNodeHandle); |
| 126 | + return *ptr<std::shared_ptr<Node> >(); |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +template<typename TNodeRef, typename> |
| 131 | +inline bool TVMArgValue::IsNodeType() const { |
| 132 | + TVM_CHECK_TYPE_CODE(type_code_, kNodeHandle); |
| 133 | + std::shared_ptr<Node>& sptr = |
| 134 | + *ptr<std::shared_ptr<Node> >(); |
| 135 | + return NodeTypeChecker<TNodeRef>::Check(sptr.get()); |
| 136 | +} |
| 137 | + |
| 138 | +// extensions for TVMRetValue |
| 139 | +inline TVMRetValue& TVMRetValue::operator=( |
| 140 | + const std::shared_ptr<Node>& other) { |
| 141 | + SwitchToClass<std::shared_ptr<Node> >(kNodeHandle, other); |
| 142 | + return *this; |
| 143 | +} |
| 144 | + |
| 145 | +inline TVMRetValue& TVMRetValue::operator=(const NodeRef& other) { |
| 146 | + SwitchToClass<std::shared_ptr<Node> >(kNodeHandle, other.node_); |
| 147 | + return *this; |
| 148 | +} |
| 149 | + |
| 150 | +template<typename TNodeRef, typename> |
| 151 | +inline TVMRetValue::operator TNodeRef() const { |
| 152 | + static_assert( |
| 153 | + std::is_base_of<NodeRef, TNodeRef>::value, |
| 154 | + "Conversion only works for NodeRef"); |
| 155 | + if (type_code_ == kNull) return TNodeRef(); |
| 156 | + TVM_CHECK_TYPE_CODE(type_code_, kNodeHandle); |
| 157 | + return TNodeRef(*ptr<std::shared_ptr<Node> >()); |
| 158 | +} |
| 159 | + |
| 160 | +inline void TVMArgsSetter::operator()(size_t i, NodeRef& other) const { // NOLINT(*) |
| 161 | + values_[i].v_handle = &(other.node_); |
| 162 | + type_codes_[i] = kNodeHandle; |
| 163 | +} |
| 164 | + |
| 165 | +// Type related stuffs |
| 166 | +inline Type TVMType2Type(TVMType t) { |
| 167 | + return Type(static_cast<halide_type_code_t>(t.code), t.bits, t.lanes); |
| 168 | +} |
| 169 | + |
| 170 | +inline TVMType Type2TVMType(Type t) { |
| 171 | + TVMType ret; |
| 172 | + ret.code = static_cast<uint8_t>(t.code()); |
| 173 | + ret.bits = static_cast<uint8_t>(t.bits()); |
| 174 | + ret.lanes = static_cast<uint16_t>(t.lanes()); |
| 175 | + return ret; |
| 176 | +} |
| 177 | + |
| 178 | +inline TVMRetValue& TVMRetValue::operator=(const Halide::Type& t) { |
| 179 | + return this->operator=(Type2TVMType(t)); |
| 180 | +} |
| 181 | + |
| 182 | +inline TVMRetValue::operator Halide::Type() const { |
| 183 | + return TVMType2Type(operator TVMType()); |
| 184 | +} |
| 185 | + |
| 186 | +inline TVMArgValue::operator Halide::Type() const { |
| 187 | + return TVMType2Type(operator TVMType()); |
| 188 | +} |
| 189 | + |
| 190 | +inline void TVMArgsSetter::operator()( |
| 191 | + size_t i, const Halide::Type& t) const { |
| 192 | + this->operator()(i, Type2TVMType(t)); |
| 193 | +} |
| 194 | +} // namespace runtime |
| 195 | +} // namespace tvm |
| 196 | +#endif // TVM_PACKED_FUNC_EXT_H_ |
0 commit comments