8 #ifndef TREELITE_TYPEINFO_H_ 9 #define TREELITE_TYPEINFO_H_ 14 #include <unordered_map> 17 #include <type_traits> 28 static_assert(std::is_same<std::underlying_type<TypeInfo>::type, uint8_t>::value,
29 "TypeInfo must use uint8_t as underlying type");
41 case treelite::TypeInfo::kInvalid:
43 case treelite::TypeInfo::kUInt32:
45 case treelite::TypeInfo::kFloat32:
47 case treelite::TypeInfo::kFloat64:
50 throw std::runtime_error(
"Unrecognized type");
62 if (std::is_same<T, uint32_t>::value) {
63 return TypeInfo::kUInt32;
64 }
else if (std::is_same<T, float>::value) {
65 return TypeInfo::kFloat32;
66 }
else if (std::is_same<T, double>::value) {
67 return TypeInfo::kFloat64;
69 throw std::runtime_error(std::string(
"Unrecognized Value type") +
typeid(T).name());
70 return TypeInfo::kInvalid;
86 template <
template<
class>
class Dispatcher,
typename ...Args>
89 case TypeInfo::kUInt32:
90 return Dispatcher<uint32_t>::Dispatch(std::forward<Args>(args)...);
91 case TypeInfo::kFloat32:
92 return Dispatcher<float>::Dispatch(std::forward<Args>(args)...);
93 case TypeInfo::kFloat64:
94 return Dispatcher<double>::Dispatch(std::forward<Args>(args)...);
95 case TypeInfo::kInvalid:
97 throw std::runtime_error(std::string(
"Invalid type: ") +
TypeInfoToString(type));
99 return Dispatcher<double>::Dispatch(std::forward<Args>(args)...);
116 template <
template<
class,
class>
class Dispatcher,
typename ...Args>
119 auto error_threshold_type = [threshold_type]() {
120 std::ostringstream oss;
124 auto error_leaf_output_type = [threshold_type, leaf_output_type]() {
125 std::ostringstream oss;
130 switch (threshold_type) {
131 case treelite::TypeInfo::kFloat32:
132 switch (leaf_output_type) {
133 case treelite::TypeInfo::kUInt32:
134 return Dispatcher<float, uint32_t>::Dispatch(std::forward<Args>(args)...);
135 case treelite::TypeInfo::kFloat32:
136 return Dispatcher<float, float>::Dispatch(std::forward<Args>(args)...);
138 throw std::runtime_error(error_leaf_output_type());
142 case treelite::TypeInfo::kFloat64:
143 switch (leaf_output_type) {
144 case treelite::TypeInfo::kUInt32:
145 return Dispatcher<double, uint32_t>::Dispatch(std::forward<Args>(args)...);
146 case treelite::TypeInfo::kFloat64:
147 return Dispatcher<double, double>::Dispatch(std::forward<Args>(args)...);
149 throw std::runtime_error(error_leaf_output_type());
154 throw std::runtime_error(error_threshold_type());
157 return Dispatcher<double, double>::Dispatch(std::forward<Args>(args)...);
163 #endif // TREELITE_TYPEINFO_H_
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.