| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifndef XGBOOST_PARAMETER_H_ |
| 9 |
#define XGBOOST_PARAMETER_H_ |
| 10 |
|
| 11 |
#include <dmlc/parameter.h> |
| 12 |
#include <xgboost/base.h> |
| 13 |
#include <string> |
| 14 |
#include <type_traits> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#define DECLARE_FIELD_ENUM_CLASS(EnumClass) \ |
| 51 |
namespace dmlc { \ |
| 52 |
namespace parameter { \ |
| 53 |
template <> \ |
| 54 |
class FieldEntry<EnumClass> : public FieldEntry<int> { \ |
| 55 |
public: \ |
| 56 |
FieldEntry() { \ |
| 57 |
static_assert( \ |
| 58 |
std::is_same_v<int, typename std::underlying_type_t<EnumClass>>, \ |
| 59 |
"enum class must be backed by int"); \ |
| 60 |
is_enum_ = true; \ |
| 61 |
} \ |
| 62 |
using Super = FieldEntry<int>; \ |
| 63 |
void Set(void *head, const std::string &value) const override { \ |
| 64 |
Super::Set(head, value); \ |
| 65 |
} \ |
| 66 |
inline FieldEntry<EnumClass>& add_enum(const std::string &key, EnumClass value) { \ |
| 67 |
Super::add_enum(key, static_cast<int>(value)); \ |
| 68 |
return *this; \ |
| 69 |
} \ |
| 70 |
inline FieldEntry<EnumClass>& set_default(const EnumClass& default_value) { \ |
| 71 |
default_value_ = static_cast<int>(default_value); \ |
| 72 |
has_default_ = true; \ |
| 73 |
return *this; \ |
| 74 |
} \ |
| 75 |
inline void Init(const std::string &key, void *head, EnumClass& ref) { \ |
| 76 |
Super::Init(key, head, *reinterpret_cast<int*>(&ref)); \ |
| 77 |
} \ |
| 78 |
}; \ |
| 79 |
} \ |
| 80 |
} |
| 81 |
|
| 82 |
namespace xgboost { |
| 83 |
template <typename Type> |
| 84 |
struct XGBoostParameter : public dmlc::Parameter<Type> { |
| 85 |
protected: |
| 86 |
bool initialised_ {false}; |
| 87 |
|
| 88 |
public: |
| 89 |
template <typename Container> |
| 90 |
Args UpdateAllowUnknown(Container const& kwargs) { |
| 91 |
if (initialised_) { |
| 92 |
return dmlc::Parameter<Type>::UpdateAllowUnknown(kwargs); |
| 93 |
} else { |
| 94 |
auto unknown = dmlc::Parameter<Type>::InitAllowUnknown(kwargs); |
| 95 |
initialised_ = true; |
| 96 |
return unknown; |
| 97 |
} |
| 98 |
} |
| 99 |
bool GetInitialised() const { return static_cast<bool>(this->initialised_); } |
| 100 |
}; |
| 101 |
} |
| 102 |
|
| 103 |
#endif |