8 #ifndef TREELITE_TYPEINFO_H_ 9 #define TREELITE_TYPEINFO_H_ 15 #include <unordered_map> 18 #include <type_traits> 29 static_assert(std::is_same<std::underlying_type<TypeInfo>::type, uint8_t>::value,
30 "TypeInfo must use uint8_t as underlying type");
42 case treelite::TypeInfo::kInvalid:
44 case treelite::TypeInfo::kUInt32:
46 case treelite::TypeInfo::kFloat32:
48 case treelite::TypeInfo::kFloat64:
51 throw Error(
"Unrecognized type");
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;
70 throw Error(std::string(
"Unrecognized Value type") +
typeid(T).name());
71 return TypeInfo::kInvalid;
87 template <
template<
class>
class Dispatcher,
typename ...Args>
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:
100 return Dispatcher<double>::Dispatch(std::forward<Args>(args)...);
117 template <
template<
class,
class>
class Dispatcher,
typename ...Args>
120 auto error_threshold_type = [threshold_type]() {
121 std::ostringstream oss;
125 auto error_leaf_output_type = [threshold_type, leaf_output_type]() {
126 std::ostringstream oss;
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)...);
139 throw Error(error_leaf_output_type());
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)...);
150 throw Error(error_leaf_output_type());
155 throw Error(error_threshold_type());
158 return Dispatcher<double, double>::Dispatch(std::forward<Args>(args)...);
164 #endif // TREELITE_TYPEINFO_H_ Exception class that will be thrown by Treelite.
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.
TypeInfo
Types used by thresholds and leaf outputs.
std::string TypeInfoToString(treelite::TypeInfo type)
Get string representation of type info.
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...
TypeInfo GetTypeInfoByName(const std::string &str)
conversion table from string to TypeInfo, defined in tables.cc
TypeInfo TypeToInfo()
Convert a template type into a type info.