Treelite
typeinfo.h
Go to the documentation of this file.
1 
8 #ifndef TREELITE_TYPEINFO_H_
9 #define TREELITE_TYPEINFO_H_
10 
11 #include <treelite/error.h>
12 #include <cstdint>
13 #include <typeinfo>
14 #include <string>
15 #include <unordered_map>
16 #include <sstream>
17 #include <utility>
18 #include <type_traits>
19 
20 namespace treelite {
21 
23 enum class TypeInfo : uint8_t {
24  kInvalid = 0,
25  kUInt32 = 1,
26  kFloat32 = 2,
27  kFloat64 = 3
28 };
29 static_assert(std::is_same<std::underlying_type<TypeInfo>::type, uint8_t>::value,
30  "TypeInfo must use uint8_t as underlying type");
31 
33 TypeInfo GetTypeInfoByName(const std::string& str);
34 
40 inline std::string TypeInfoToString(treelite::TypeInfo type) {
41  switch (type) {
42  case treelite::TypeInfo::kInvalid:
43  return "invalid";
44  case treelite::TypeInfo::kUInt32:
45  return "uint32";
46  case treelite::TypeInfo::kFloat32:
47  return "float32";
48  case treelite::TypeInfo::kFloat64:
49  return "float64";
50  default:
51  throw Error("Unrecognized type");
52  return "";
53  }
54 }
55 
61 template <typename T>
62 inline TypeInfo TypeToInfo() {
63  if (std::is_same<T, uint32_t>::value) {
64  return TypeInfo::kUInt32;
65  } else if (std::is_same<T, float>::value) {
66  return TypeInfo::kFloat32;
67  } else if (std::is_same<T, double>::value) {
68  return TypeInfo::kFloat64;
69  } else {
70  throw Error(std::string("Unrecognized Value type") + typeid(T).name());
71  return TypeInfo::kInvalid;
72  }
73 }
74 
87 template <template<class> class Dispatcher, typename ...Args>
88 inline auto DispatchWithTypeInfo(TypeInfo type, Args&& ...args) {
89  switch (type) {
90  case TypeInfo::kUInt32:
91  return Dispatcher<uint32_t>::Dispatch(std::forward<Args>(args)...);
92  case TypeInfo::kFloat32:
93  return Dispatcher<float>::Dispatch(std::forward<Args>(args)...);
94  case TypeInfo::kFloat64:
95  return Dispatcher<double>::Dispatch(std::forward<Args>(args)...);
96  case TypeInfo::kInvalid:
97  default:
98  throw Error(std::string("Invalid type: ") + TypeInfoToString(type));
99  }
100  return Dispatcher<double>::Dispatch(std::forward<Args>(args)...); // avoid missing return error
101 }
102 
117 template <template<class, class> class Dispatcher, typename ...Args>
119  TypeInfo threshold_type, TypeInfo leaf_output_type, Args&& ...args) {
120  auto error_threshold_type = [threshold_type]() {
121  std::ostringstream oss;
122  oss << "Invalid threshold type: " << treelite::TypeInfoToString(threshold_type);
123  return oss.str();
124  };
125  auto error_leaf_output_type = [threshold_type, leaf_output_type]() {
126  std::ostringstream oss;
127  oss << "Cannot use leaf output type " << treelite::TypeInfoToString(leaf_output_type)
128  << " with threshold type " << treelite::TypeInfoToString(threshold_type);
129  return oss.str();
130  };
131  switch (threshold_type) {
132  case treelite::TypeInfo::kFloat32:
133  switch (leaf_output_type) {
134  case treelite::TypeInfo::kUInt32:
135  return Dispatcher<float, uint32_t>::Dispatch(std::forward<Args>(args)...);
136  case treelite::TypeInfo::kFloat32:
137  return Dispatcher<float, float>::Dispatch(std::forward<Args>(args)...);
138  default:
139  throw Error(error_leaf_output_type());
140  break;
141  }
142  break;
143  case treelite::TypeInfo::kFloat64:
144  switch (leaf_output_type) {
145  case treelite::TypeInfo::kUInt32:
146  return Dispatcher<double, uint32_t>::Dispatch(std::forward<Args>(args)...);
147  case treelite::TypeInfo::kFloat64:
148  return Dispatcher<double, double>::Dispatch(std::forward<Args>(args)...);
149  default:
150  throw Error(error_leaf_output_type());
151  break;
152  }
153  break;
154  default:
155  throw Error(error_threshold_type());
156  break;
157  }
158  return Dispatcher<double, double>::Dispatch(std::forward<Args>(args)...);
159  // avoid missing return value warning
160 }
161 
162 } // namespace treelite
163 
164 #endif // TREELITE_TYPEINFO_H_
Exception class that will be thrown by Treelite.
Definition: error.h:18
Exception class used throughout the Treelite codebase.
auto DispatchWithTypeInfo(TypeInfo type, Args &&...args)
Given a TypeInfo, dispatch a function with the corresponding template arg. More precisely, we shall call Dispatcher<T>::Dispatch() where the template arg T corresponds to the type parameter.
Definition: typeinfo.h:88
TypeInfo
Types used by thresholds and leaf outputs.
Definition: typeinfo.h:23
std::string TypeInfoToString(treelite::TypeInfo type)
Get string representation of type info.
Definition: typeinfo.h:40
auto DispatchWithModelTypes(TypeInfo threshold_type, TypeInfo leaf_output_type, Args &&...args)
Given the types for thresholds and leaf outputs, validate that they consist of a valid combination fo...
Definition: typeinfo.h:118
TypeInfo GetTypeInfoByName(const std::string &str)
conversion table from string to TypeInfo, defined in tables.cc
Definition: typeinfo.cc:15
TypeInfo TypeToInfo()
Convert a template type into a type info.
Definition: typeinfo.h:62