| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef DMLC_PARAMETER_H_ |
| 7 |
#define DMLC_PARAMETER_H_ |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <cerrno> |
| 11 |
#include <cmath> |
| 12 |
#include <cstddef> |
| 13 |
#include <cstdlib> |
| 14 |
#include <iomanip> |
| 15 |
#include <iostream> |
| 16 |
#include <limits> |
| 17 |
#include <map> |
| 18 |
#include <set> |
| 19 |
#include <sstream> |
| 20 |
#include <stdexcept> |
| 21 |
#include <string> |
| 22 |
#include <typeinfo> |
| 23 |
#include <utility> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include "./base.h" |
| 27 |
#include "./json.h" |
| 28 |
#include "./logging.h" |
| 29 |
#include "./optional.h" |
| 30 |
#include "./strtonum.h" |
| 31 |
#include "./type_traits.h" |
| 32 |
|
| 33 |
namespace dmlc { |
| 34 |
|
| 35 |
|
| 36 |
struct ParamError : public dmlc::Error { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
explicit ParamError(const std::string &msg) : dmlc::Error(msg) {} |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
template <typename ValueType> |
| 51 |
inline ValueType GetEnv(const char *key, ValueType default_value); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
template <typename ValueType> |
| 59 |
inline void SetEnv(const char *key, ValueType value); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
inline void UnsetEnv(const char *key); |
| 66 |
|
| 67 |
|
| 68 |
namespace parameter { |
| 69 |
|
| 70 |
class ParamManager; |
| 71 |
|
| 72 |
class FieldAccessEntry; |
| 73 |
|
| 74 |
template <typename DType> |
| 75 |
class FieldEntry; |
| 76 |
|
| 77 |
template <typename PType> |
| 78 |
struct ParamManagerSingleton; |
| 79 |
|
| 80 |
|
| 81 |
enum ParamInitOption { |
| 82 |
|
| 83 |
kAllowUnknown, |
| 84 |
|
| 85 |
kAllMatch, |
| 86 |
|
| 87 |
kAllowHidden |
| 88 |
}; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
struct ParamFieldInfo { |
| 94 |
|
| 95 |
std::string name; |
| 96 |
|
| 97 |
std::string type; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
std::string type_info_str; |
| 103 |
|
| 104 |
std::string description; |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
template <typename PType> |
| 132 |
struct Parameter { |
| 133 |
public: |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
template <typename Container> |
| 145 |
inline void Init( |
| 146 |
const Container &kwargs, parameter::ParamInitOption option = parameter::kAllowHidden) { |
| 147 |
PType::__MANAGER__()->RunInit( |
| 148 |
static_cast<PType *>(this), kwargs.begin(), kwargs.end(), NULL, option); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
template <typename Container> |
| 160 |
inline std::vector<std::pair<std::string, std::string>> InitAllowUnknown( |
| 161 |
const Container &kwargs) { |
| 162 |
std::vector<std::pair<std::string, std::string>> unknown; |
| 163 |
PType::__MANAGER__()->RunInit(static_cast<PType *>(this), kwargs.begin(), kwargs.end(), |
| 164 |
&unknown, parameter::kAllowUnknown); |
| 165 |
return unknown; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
template <typename Container> |
| 180 |
std::vector<std::pair<std::string, std::string>> UpdateAllowUnknown(const Container &kwargs) { |
| 181 |
std::vector<std::pair<std::string, std::string>> unknown; |
| 182 |
PType::__MANAGER__()->RunUpdate(static_cast<PType *>(this), kwargs.begin(), kwargs.end(), |
| 183 |
parameter::kAllowUnknown, &unknown, nullptr); |
| 184 |
return unknown; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
template <typename Container> |
| 194 |
inline void UpdateDict(Container *dict) const { |
| 195 |
PType::__MANAGER__()->UpdateDict(this->head(), dict); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
inline std::map<std::string, std::string> __DICT__() const { |
| 202 |
std::vector<std::pair<std::string, std::string>> vec |
| 203 |
= PType::__MANAGER__()->GetDict(this->head()); |
| 204 |
return std::map<std::string, std::string>(vec.begin(), vec.end()); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
inline void Save(dmlc::JSONWriter *writer) const { |
| 211 |
writer->Write(this->__DICT__()); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
inline void Load(dmlc::JSONReader *reader) { |
| 219 |
std::map<std::string, std::string> kwargs; |
| 220 |
reader->Read(&kwargs); |
| 221 |
this->Init(kwargs); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
inline static std::vector<ParamFieldInfo> __FIELDS__() { |
| 228 |
return PType::__MANAGER__()->GetFieldInfo(); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
inline static std::string __DOC__() { |
| 235 |
std::ostringstream os; |
| 236 |
PType::__MANAGER__()->PrintDocString(os); |
| 237 |
return os.str(); |
| 238 |
} |
| 239 |
|
| 240 |
protected: |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
template <typename DType> |
| 248 |
inline parameter::FieldEntry<DType> &DECLARE(parameter::ParamManagerSingleton<PType> *manager, |
| 249 |
const std::string &key, DType &ref) { |
| 250 |
parameter::FieldEntry<DType> *e = new parameter::FieldEntry<DType>(); |
| 251 |
e->Init(key, this->head(), ref); |
| 252 |
manager->manager.AddEntry(key, e); |
| 253 |
return *e; |
| 254 |
} |
| 255 |
|
| 256 |
private: |
| 257 |
|
| 258 |
inline PType *head() const { |
| 259 |
return static_cast<PType *>(const_cast<Parameter<PType> *>(this)); |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
#define DMLC_DECLARE_PARAMETER(PType) \ |
| 284 |
static ::dmlc::parameter::ParamManager *__MANAGER__(); \ |
| 285 |
inline void __DECLARE__(::dmlc::parameter::ParamManagerSingleton<PType> *manager) |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
#define DMLC_DECLARE_FIELD(FieldName) this->DECLARE(manager, #FieldName, FieldName) |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
#define DMLC_DECLARE_ALIAS(FieldName, AliasName) manager->manager.AddAlias(#FieldName, #AliasName) |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
#define DMLC_REGISTER_PARAMETER(PType) \ |
| 309 |
::dmlc::parameter::ParamManager *PType::__MANAGER__() { \ |
| 310 |
static ::dmlc::parameter::ParamManagerSingleton<PType> inst(#PType); \ |
| 311 |
return &inst.manager; \ |
| 312 |
} \ |
| 313 |
static DMLC_ATTRIBUTE_UNUSED ::dmlc::parameter::ParamManager &__make__##PType##ParamManager__ \ |
| 314 |
= (*PType::__MANAGER__()) |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
namespace parameter { |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
class FieldAccessEntry { |
| 329 |
public: |
| 330 |
FieldAccessEntry() : has_default_(false), index_(0) {} |
| 331 |
|
| 332 |
virtual ~FieldAccessEntry() {} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
virtual void SetDefault(void *head) const = 0; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
virtual void Set(void *head, const std::string &value) const = 0; |
| 345 |
|
| 346 |
virtual void Check(void * ) const {} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
virtual std::string GetStringValue(void *head) const = 0; |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
virtual ParamFieldInfo GetFieldInfo() const = 0; |
| 357 |
|
| 358 |
protected: |
| 359 |
|
| 360 |
bool has_default_; |
| 361 |
|
| 362 |
size_t index_; |
| 363 |
|
| 364 |
std::string key_; |
| 365 |
|
| 366 |
std::string type_; |
| 367 |
|
| 368 |
std::string description_; |
| 369 |
|
| 370 |
ptrdiff_t offset_; |
| 371 |
|
| 372 |
char *GetRawPtr(void *head) const { |
| 373 |
return reinterpret_cast<char *>(head) + offset_; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
virtual void PrintDefaultValueString(std::ostream &os) const = 0; |
| 380 |
|
| 381 |
friend class ParamManager; |
| 382 |
}; |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
class ParamManager { |
| 389 |
public: |
| 390 |
|
| 391 |
~ParamManager() { |
| 392 |
for (size_t i = 0; i < entry_.size(); ++i) { |
| 393 |
delete entry_[i]; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
inline FieldAccessEntry *Find(const std::string &key) const { |
| 402 |
std::map<std::string, FieldAccessEntry *>::const_iterator it = entry_map_.find(key); |
| 403 |
if (it == entry_map_.end()) { |
| 404 |
return NULL; |
| 405 |
} |
| 406 |
return it->second; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
template <typename RandomAccessIterator> |
| 421 |
inline void RunInit(void *head, RandomAccessIterator begin, RandomAccessIterator end, |
| 422 |
std::vector<std::pair<std::string, std::string>> *unknown_args, |
| 423 |
parameter::ParamInitOption option) const { |
| 424 |
std::set<FieldAccessEntry *> selected_args; |
| 425 |
RunUpdate(head, begin, end, option, unknown_args, &selected_args); |
| 426 |
for (const auto &kv : entry_map_) { |
| 427 |
if (selected_args.find(kv.second) == selected_args.cend()) { |
| 428 |
kv.second->SetDefault(head); |
| 429 |
} |
| 430 |
} |
| 431 |
for (std::map<std::string, FieldAccessEntry *>::const_iterator it = entry_map_.begin(); |
| 432 |
it != entry_map_.end(); ++it) { |
| 433 |
if (selected_args.count(it->second) == 0) { |
| 434 |
it->second->SetDefault(head); |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
template <typename RandomAccessIterator> |
| 453 |
void RunUpdate(void *head, RandomAccessIterator begin, RandomAccessIterator end, |
| 454 |
parameter::ParamInitOption option, |
| 455 |
std::vector<std::pair<std::string, std::string>> *unknown_args, |
| 456 |
std::set<FieldAccessEntry *> *selected_args = nullptr) const { |
| 457 |
for (RandomAccessIterator it = begin; it != end; ++it) { |
| 458 |
if (FieldAccessEntry *e = Find(it->first)) { |
| 459 |
e->Set(head, it->second); |
| 460 |
e->Check(head); |
| 461 |
if (selected_args) { |
| 462 |
selected_args->insert(e); |
| 463 |
} |
| 464 |
} else { |
| 465 |
if (unknown_args != NULL) { |
| 466 |
unknown_args->push_back(*it); |
| 467 |
} else { |
| 468 |
if (option != parameter::kAllowUnknown) { |
| 469 |
if (option == parameter::kAllowHidden && it->first.length() > 4 |
| 470 |
&& it->first.find("__") == 0 && it->first.rfind("__") == it->first.length() - 2) { |
| 471 |
continue; |
| 472 |
} |
| 473 |
std::ostringstream os; |
| 474 |
os << "Cannot find argument \'" << it->first << "\', Possible Arguments:\n"; |
| 475 |
os << "----------------\n"; |
| 476 |
PrintDocString(os); |
| 477 |
throw dmlc::ParamError(os.str()); |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
inline void AddEntry(const std::string &key, FieldAccessEntry *e) { |
| 490 |
e->index_ = entry_.size(); |
| 491 |
|
| 492 |
if (entry_map_.count(key) != 0) { |
| 493 |
LOG(FATAL) << "key " << key << " has already been registered in " << name_; |
| 494 |
} |
| 495 |
entry_.push_back(e); |
| 496 |
entry_map_[key] = e; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
inline void AddAlias(const std::string &field, const std::string &alias) { |
| 505 |
if (entry_map_.count(field) == 0) { |
| 506 |
LOG(FATAL) << "key " << field << " has not been registered in " << name_; |
| 507 |
} |
| 508 |
if (entry_map_.count(alias) != 0) { |
| 509 |
LOG(FATAL) << "Alias " << alias << " has already been registered in " << name_; |
| 510 |
} |
| 511 |
entry_map_[alias] = entry_map_[field]; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
inline void set_name(const std::string &name) { |
| 518 |
name_ = name; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
inline std::vector<ParamFieldInfo> GetFieldInfo() const { |
| 525 |
std::vector<ParamFieldInfo> ret(entry_.size()); |
| 526 |
for (size_t i = 0; i < entry_.size(); ++i) { |
| 527 |
ret[i] = entry_[i]->GetFieldInfo(); |
| 528 |
} |
| 529 |
return ret; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
inline void PrintDocString(std::ostream &os) const { |
| 536 |
for (size_t i = 0; i < entry_.size(); ++i) { |
| 537 |
ParamFieldInfo info = entry_[i]->GetFieldInfo(); |
| 538 |
os << info.name << " : " << info.type_info_str << '\n'; |
| 539 |
if (info.description.length() != 0) { |
| 540 |
os << " " << info.description << '\n'; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
inline std::vector<std::pair<std::string, std::string>> GetDict(void *head) const { |
| 551 |
std::vector<std::pair<std::string, std::string>> ret; |
| 552 |
for (std::map<std::string, FieldAccessEntry *>::const_iterator it = entry_map_.begin(); |
| 553 |
it != entry_map_.end(); ++it) { |
| 554 |
ret.push_back(std::make_pair(it->first, it->second->GetStringValue(head))); |
| 555 |
} |
| 556 |
return ret; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
template <typename Container> |
| 565 |
inline void UpdateDict(void *head, Container *dict) const { |
| 566 |
for (std::map<std::string, FieldAccessEntry *>::const_iterator it = entry_map_.begin(); |
| 567 |
it != entry_map_.end(); ++it) { |
| 568 |
(*dict)[it->first] = it->second->GetStringValue(head); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
private: |
| 573 |
|
| 574 |
std::string name_; |
| 575 |
|
| 576 |
std::vector<FieldAccessEntry *> entry_; |
| 577 |
|
| 578 |
std::map<std::string, FieldAccessEntry *> entry_map_; |
| 579 |
}; |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
template <typename PType> |
| 586 |
struct ParamManagerSingleton { |
| 587 |
ParamManager manager; |
| 588 |
explicit ParamManagerSingleton(const std::string ¶m_name) { |
| 589 |
PType param; |
| 590 |
manager.set_name(param_name); |
| 591 |
param.__DECLARE__(this); |
| 592 |
} |
| 593 |
}; |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
template <typename TEntry, typename DType> |
| 598 |
class FieldEntryBase : public FieldAccessEntry { |
| 599 |
public: |
| 600 |
|
| 601 |
typedef TEntry EntryType; |
| 602 |
|
| 603 |
void Set(void *head, const std::string &value) const override { |
| 604 |
std::istringstream is(value); |
| 605 |
is >> this->Get(head); |
| 606 |
if (!is.fail()) { |
| 607 |
while (!is.eof()) { |
| 608 |
int ch = is.get(); |
| 609 |
if (ch == EOF) { |
| 610 |
is.clear(); |
| 611 |
break; |
| 612 |
} |
| 613 |
if (!isspace(ch)) { |
| 614 |
is.setstate(std::ios::failbit); |
| 615 |
break; |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
if (is.fail()) { |
| 621 |
std::ostringstream os; |
| 622 |
os << "Invalid Parameter format for " << key_ << " expect " << type_ << " but value=\'" |
| 623 |
<< value << '\''; |
| 624 |
throw dmlc::ParamError(os.str()); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
std::string GetStringValue(void *head) const override { |
| 629 |
std::ostringstream os; |
| 630 |
PrintValue(os, this->Get(head)); |
| 631 |
return os.str(); |
| 632 |
} |
| 633 |
ParamFieldInfo GetFieldInfo() const override { |
| 634 |
ParamFieldInfo info; |
| 635 |
std::ostringstream os; |
| 636 |
info.name = key_; |
| 637 |
info.type = type_; |
| 638 |
os << type_; |
| 639 |
if (has_default_) { |
| 640 |
os << ',' << " optional, default="; |
| 641 |
PrintDefaultValueString(os); |
| 642 |
} else { |
| 643 |
os << ", required"; |
| 644 |
} |
| 645 |
info.type_info_str = os.str(); |
| 646 |
info.description = description_; |
| 647 |
return info; |
| 648 |
} |
| 649 |
|
| 650 |
void SetDefault(void *head) const override { |
| 651 |
if (!has_default_) { |
| 652 |
std::ostringstream os; |
| 653 |
os << "Required parameter " << key_ << " of " << type_ << " is not presented"; |
| 654 |
throw dmlc::ParamError(os.str()); |
| 655 |
} else { |
| 656 |
this->Get(head) = default_value_; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
inline TEntry &self() { |
| 661 |
return *(static_cast<TEntry *>(this)); |
| 662 |
} |
| 663 |
|
| 664 |
inline TEntry &set_default(const DType &default_value) { |
| 665 |
default_value_ = default_value; |
| 666 |
has_default_ = true; |
| 667 |
|
| 668 |
return this->self(); |
| 669 |
} |
| 670 |
|
| 671 |
inline TEntry &describe(const std::string &description) { |
| 672 |
description_ = description; |
| 673 |
|
| 674 |
return this->self(); |
| 675 |
} |
| 676 |
|
| 677 |
inline void Init(const std::string &key, void *head, DType &ref) { |
| 678 |
this->key_ = key; |
| 679 |
if (this->type_.length() == 0) { |
| 680 |
this->type_ = dmlc::type_name<DType>(); |
| 681 |
} |
| 682 |
this->offset_ = ((char *)&ref) - ((char *)head); |
| 683 |
} |
| 684 |
|
| 685 |
protected: |
| 686 |
|
| 687 |
virtual void PrintValue(std::ostream &os, DType value) const { |
| 688 |
os << value; |
| 689 |
} |
| 690 |
void PrintDefaultValueString(std::ostream &os) const override { |
| 691 |
PrintValue(os, default_value_); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
inline DType &Get(void *head) const { |
| 697 |
return *(DType *)this->GetRawPtr(head); |
| 698 |
} |
| 699 |
|
| 700 |
DType default_value_; |
| 701 |
}; |
| 702 |
|
| 703 |
|
| 704 |
template <typename TEntry, typename DType> |
| 705 |
class FieldEntryNumeric : public FieldEntryBase<TEntry, DType> { |
| 706 |
public: |
| 707 |
FieldEntryNumeric() : has_begin_(false), has_end_(false) {} |
| 708 |
|
| 709 |
virtual TEntry &set_range(DType begin, DType end) { |
| 710 |
begin_ = begin; |
| 711 |
end_ = end; |
| 712 |
has_begin_ = true; |
| 713 |
has_end_ = true; |
| 714 |
return this->self(); |
| 715 |
} |
| 716 |
|
| 717 |
virtual TEntry &set_lower_bound(DType begin) { |
| 718 |
begin_ = begin; |
| 719 |
has_begin_ = true; |
| 720 |
return this->self(); |
| 721 |
} |
| 722 |
|
| 723 |
virtual void Check(void *head) const { |
| 724 |
FieldEntryBase<TEntry, DType>::Check(head); |
| 725 |
DType v = this->Get(head); |
| 726 |
if (has_begin_ && has_end_) { |
| 727 |
if (v < begin_ || v > end_) { |
| 728 |
std::ostringstream os; |
| 729 |
os << "value " << v << " for Parameter " << this->key_ << " exceed bound [" << begin_ << ',' |
| 730 |
<< end_ << ']' << '\n'; |
| 731 |
os << this->key_ << ": " << this->description_; |
| 732 |
throw dmlc::ParamError(os.str()); |
| 733 |
} |
| 734 |
} else if (has_begin_ && v < begin_) { |
| 735 |
std::ostringstream os; |
| 736 |
os << "value " << v << " for Parameter " << this->key_ << " should be greater equal to " |
| 737 |
<< begin_ << '\n'; |
| 738 |
os << this->key_ << ": " << this->description_; |
| 739 |
throw dmlc::ParamError(os.str()); |
| 740 |
} else if (has_end_ && v > end_) { |
| 741 |
std::ostringstream os; |
| 742 |
os << "value " << v << " for Parameter " << this->key_ << " should be smaller equal to " |
| 743 |
<< end_ << '\n'; |
| 744 |
os << this->key_ << ": " << this->description_; |
| 745 |
throw dmlc::ParamError(os.str()); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
protected: |
| 750 |
|
| 751 |
bool has_begin_, has_end_; |
| 752 |
|
| 753 |
DType begin_, end_; |
| 754 |
}; |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
template <typename DType> |
| 762 |
class FieldEntry : |
| 763 |
public IfThenElseType<dmlc::is_arithmetic<DType>::value, |
| 764 |
FieldEntryNumeric<FieldEntry<DType>, DType>, |
| 765 |
FieldEntryBase<FieldEntry<DType>, DType>>::Type {}; |
| 766 |
|
| 767 |
|
| 768 |
template <> |
| 769 |
class FieldEntry<int> : public FieldEntryNumeric<FieldEntry<int>, int> { |
| 770 |
public: |
| 771 |
|
| 772 |
FieldEntry() : is_enum_(false) {} |
| 773 |
|
| 774 |
typedef FieldEntryNumeric<FieldEntry<int>, int> Parent; |
| 775 |
|
| 776 |
virtual void Set(void *head, const std::string &value) const { |
| 777 |
if (is_enum_) { |
| 778 |
std::map<std::string, int>::const_iterator it = enum_map_.find(value); |
| 779 |
std::ostringstream os; |
| 780 |
if (it == enum_map_.end()) { |
| 781 |
os << "Invalid Input: \'" << value; |
| 782 |
os << "\', valid values are: "; |
| 783 |
PrintEnums(os); |
| 784 |
throw dmlc::ParamError(os.str()); |
| 785 |
} else { |
| 786 |
os << it->second; |
| 787 |
Parent::Set(head, os.str()); |
| 788 |
} |
| 789 |
} else { |
| 790 |
Parent::Set(head, value); |
| 791 |
} |
| 792 |
} |
| 793 |
virtual ParamFieldInfo GetFieldInfo() const { |
| 794 |
if (is_enum_) { |
| 795 |
ParamFieldInfo info; |
| 796 |
std::ostringstream os; |
| 797 |
info.name = key_; |
| 798 |
info.type = type_; |
| 799 |
PrintEnums(os); |
| 800 |
if (has_default_) { |
| 801 |
os << ',' << "optional, default="; |
| 802 |
PrintDefaultValueString(os); |
| 803 |
} else { |
| 804 |
os << ", required"; |
| 805 |
} |
| 806 |
info.type_info_str = os.str(); |
| 807 |
info.description = description_; |
| 808 |
return info; |
| 809 |
} else { |
| 810 |
return Parent::GetFieldInfo(); |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
inline FieldEntry<int> &add_enum(const std::string &key, int value) { |
| 815 |
if ((enum_map_.size() != 0 && enum_map_.count(key) != 0) || enum_back_map_.count(value) != 0) { |
| 816 |
std::ostringstream os; |
| 817 |
os << "Enum " << "(" << key << ": " << value << " exisit!" << ")\n"; |
| 818 |
os << "Enums: "; |
| 819 |
for (std::map<std::string, int>::const_iterator it = enum_map_.begin(); it != enum_map_.end(); |
| 820 |
++it) { |
| 821 |
os << "(" << it->first << ": " << it->second << "), "; |
| 822 |
} |
| 823 |
throw dmlc::ParamError(os.str()); |
| 824 |
} |
| 825 |
enum_map_[key] = value; |
| 826 |
enum_back_map_[value] = key; |
| 827 |
is_enum_ = true; |
| 828 |
return this->self(); |
| 829 |
} |
| 830 |
|
| 831 |
protected: |
| 832 |
|
| 833 |
bool is_enum_; |
| 834 |
|
| 835 |
std::map<std::string, int> enum_map_; |
| 836 |
|
| 837 |
std::map<int, std::string> enum_back_map_; |
| 838 |
|
| 839 |
virtual void PrintDefaultValueString(std::ostream &os) const { |
| 840 |
os << '\''; |
| 841 |
PrintValue(os, default_value_); |
| 842 |
os << '\''; |
| 843 |
} |
| 844 |
|
| 845 |
virtual void PrintValue(std::ostream &os, int value) const { |
| 846 |
if (is_enum_) { |
| 847 |
CHECK_NE(enum_back_map_.count(value), 0U) << "Value not found in enum declared"; |
| 848 |
os << enum_back_map_.at(value); |
| 849 |
} else { |
| 850 |
os << value; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
private: |
| 855 |
inline void PrintEnums(std::ostream &os) const { |
| 856 |
os << '{'; |
| 857 |
for (std::map<std::string, int>::const_iterator it = enum_map_.begin(); it != enum_map_.end(); |
| 858 |
++it) { |
| 859 |
if (it != enum_map_.begin()) { |
| 860 |
os << ", "; |
| 861 |
} |
| 862 |
os << "\'" << it->first << '\''; |
| 863 |
} |
| 864 |
os << '}'; |
| 865 |
} |
| 866 |
}; |
| 867 |
|
| 868 |
|
| 869 |
template <> |
| 870 |
class FieldEntry<optional<int>> : public FieldEntryBase<FieldEntry<optional<int>>, optional<int>> { |
| 871 |
public: |
| 872 |
|
| 873 |
FieldEntry() : is_enum_(false) {} |
| 874 |
|
| 875 |
typedef FieldEntryBase<FieldEntry<optional<int>>, optional<int>> Parent; |
| 876 |
|
| 877 |
virtual void Set(void *head, const std::string &value) const { |
| 878 |
if (is_enum_ && value != "None") { |
| 879 |
std::map<std::string, int>::const_iterator it = enum_map_.find(value); |
| 880 |
std::ostringstream os; |
| 881 |
if (it == enum_map_.end()) { |
| 882 |
os << "Invalid Input: \'" << value; |
| 883 |
os << "\', valid values are: "; |
| 884 |
PrintEnums(os); |
| 885 |
throw dmlc::ParamError(os.str()); |
| 886 |
} else { |
| 887 |
os << it->second; |
| 888 |
Parent::Set(head, os.str()); |
| 889 |
} |
| 890 |
} else { |
| 891 |
Parent::Set(head, value); |
| 892 |
} |
| 893 |
} |
| 894 |
virtual ParamFieldInfo GetFieldInfo() const { |
| 895 |
if (is_enum_) { |
| 896 |
ParamFieldInfo info; |
| 897 |
std::ostringstream os; |
| 898 |
info.name = key_; |
| 899 |
info.type = type_; |
| 900 |
PrintEnums(os); |
| 901 |
if (has_default_) { |
| 902 |
os << ',' << "optional, default="; |
| 903 |
PrintDefaultValueString(os); |
| 904 |
} else { |
| 905 |
os << ", required"; |
| 906 |
} |
| 907 |
info.type_info_str = os.str(); |
| 908 |
info.description = description_; |
| 909 |
return info; |
| 910 |
} else { |
| 911 |
return Parent::GetFieldInfo(); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
inline FieldEntry<optional<int>> &add_enum(const std::string &key, int value) { |
| 916 |
CHECK_NE(key, "None") << "None is reserved for empty optional<int>"; |
| 917 |
if ((enum_map_.size() != 0 && enum_map_.count(key) != 0) || enum_back_map_.count(value) != 0) { |
| 918 |
std::ostringstream os; |
| 919 |
os << "Enum " << "(" << key << ": " << value << " exisit!" << ")\n"; |
| 920 |
os << "Enums: "; |
| 921 |
for (std::map<std::string, int>::const_iterator it = enum_map_.begin(); it != enum_map_.end(); |
| 922 |
++it) { |
| 923 |
os << "(" << it->first << ": " << it->second << "), "; |
| 924 |
} |
| 925 |
throw dmlc::ParamError(os.str()); |
| 926 |
} |
| 927 |
enum_map_[key] = value; |
| 928 |
enum_back_map_[value] = key; |
| 929 |
is_enum_ = true; |
| 930 |
return this->self(); |
| 931 |
} |
| 932 |
|
| 933 |
protected: |
| 934 |
|
| 935 |
bool is_enum_; |
| 936 |
|
| 937 |
std::map<std::string, int> enum_map_; |
| 938 |
|
| 939 |
std::map<int, std::string> enum_back_map_; |
| 940 |
|
| 941 |
virtual void PrintDefaultValueString(std::ostream &os) const { |
| 942 |
os << '\''; |
| 943 |
PrintValue(os, default_value_); |
| 944 |
os << '\''; |
| 945 |
} |
| 946 |
|
| 947 |
virtual void PrintValue(std::ostream &os, optional<int> value) const { |
| 948 |
if (is_enum_) { |
| 949 |
if (!value) { |
| 950 |
os << "None"; |
| 951 |
} else { |
| 952 |
CHECK_NE(enum_back_map_.count(value.value()), 0U) << "Value not found in enum declared"; |
| 953 |
os << enum_back_map_.at(value.value()); |
| 954 |
} |
| 955 |
} else { |
| 956 |
os << value; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
private: |
| 961 |
inline void PrintEnums(std::ostream &os) const { |
| 962 |
os << "{None"; |
| 963 |
for (std::map<std::string, int>::const_iterator it = enum_map_.begin(); it != enum_map_.end(); |
| 964 |
++it) { |
| 965 |
os << ", "; |
| 966 |
os << "\'" << it->first << '\''; |
| 967 |
} |
| 968 |
os << '}'; |
| 969 |
} |
| 970 |
}; |
| 971 |
|
| 972 |
|
| 973 |
template <> |
| 974 |
class FieldEntry<std::string> : public FieldEntryBase<FieldEntry<std::string>, std::string> { |
| 975 |
public: |
| 976 |
|
| 977 |
typedef FieldEntryBase<FieldEntry<std::string>, std::string> Parent; |
| 978 |
|
| 979 |
virtual void Set(void *head, const std::string &value) const { |
| 980 |
this->Get(head) = value; |
| 981 |
} |
| 982 |
|
| 983 |
virtual void PrintDefaultValueString(std::ostream &os) const { |
| 984 |
os << '\'' << default_value_ << '\''; |
| 985 |
} |
| 986 |
}; |
| 987 |
|
| 988 |
|
| 989 |
template <> |
| 990 |
class FieldEntry<bool> : public FieldEntryBase<FieldEntry<bool>, bool> { |
| 991 |
public: |
| 992 |
|
| 993 |
typedef FieldEntryBase<FieldEntry<bool>, bool> Parent; |
| 994 |
|
| 995 |
virtual void Set(void *head, const std::string &value) const { |
| 996 |
std::string lower_case; |
| 997 |
lower_case.resize(value.length()); |
| 998 |
std::transform(value.begin(), value.end(), lower_case.begin(), ::tolower); |
| 999 |
bool &ref = this->Get(head); |
| 1000 |
if (lower_case == "true") { |
| 1001 |
ref = true; |
| 1002 |
} else if (lower_case == "false") { |
| 1003 |
ref = false; |
| 1004 |
} else if (lower_case == "1") { |
| 1005 |
ref = true; |
| 1006 |
} else if (lower_case == "0") { |
| 1007 |
ref = false; |
| 1008 |
} else { |
| 1009 |
std::ostringstream os; |
| 1010 |
os << "Invalid Parameter format for " << key_ << " expect " << type_ << " but value=\'" |
| 1011 |
<< value << '\''; |
| 1012 |
throw dmlc::ParamError(os.str()); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
protected: |
| 1017 |
|
| 1018 |
virtual void PrintValue(std::ostream &os, bool value) const { |
| 1019 |
os << static_cast<int>(value); |
| 1020 |
} |
| 1021 |
}; |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
#if DMLC_USE_CXX11 |
| 1026 |
template <> |
| 1027 |
class FieldEntry<float> : public FieldEntryNumeric<FieldEntry<float>, float> { |
| 1028 |
public: |
| 1029 |
|
| 1030 |
typedef FieldEntryNumeric<FieldEntry<float>, float> Parent; |
| 1031 |
|
| 1032 |
virtual void Set(void *head, const std::string &value) const { |
| 1033 |
size_t pos = 0; |
| 1034 |
try { |
| 1035 |
this->Get(head) = dmlc::stof(value, &pos); |
| 1036 |
} catch (const std::invalid_argument &) { |
| 1037 |
std::ostringstream os; |
| 1038 |
os << "Invalid Parameter format for " << key_ << " expect " << type_ << " but value=\'" |
| 1039 |
<< value << '\''; |
| 1040 |
throw dmlc::ParamError(os.str()); |
| 1041 |
} catch (const std::out_of_range &) { |
| 1042 |
std::ostringstream os; |
| 1043 |
os << "Out of range value for " << key_ << ", value=\'" << value << '\''; |
| 1044 |
throw dmlc::ParamError(os.str()); |
| 1045 |
} |
| 1046 |
CHECK_LE(pos, value.length()); |
| 1047 |
if (pos < value.length()) { |
| 1048 |
std::ostringstream os; |
| 1049 |
os << "Some trailing characters could not be parsed: \'" << value.substr(pos) << "\'"; |
| 1050 |
throw dmlc::ParamError(os.str()); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
protected: |
| 1055 |
|
| 1056 |
virtual void PrintValue(std::ostream &os, float value) const { |
| 1057 |
os << std::setprecision(std::numeric_limits<float>::max_digits10) << value; |
| 1058 |
} |
| 1059 |
}; |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
template <> |
| 1064 |
class FieldEntry<double> : public FieldEntryNumeric<FieldEntry<double>, double> { |
| 1065 |
public: |
| 1066 |
|
| 1067 |
typedef FieldEntryNumeric<FieldEntry<double>, double> Parent; |
| 1068 |
|
| 1069 |
virtual void Set(void *head, const std::string &value) const { |
| 1070 |
size_t pos = 0; |
| 1071 |
try { |
| 1072 |
this->Get(head) = dmlc::stod(value, &pos); |
| 1073 |
} catch (const std::invalid_argument &) { |
| 1074 |
std::ostringstream os; |
| 1075 |
os << "Invalid Parameter format for " << key_ << " expect " << type_ << " but value=\'" |
| 1076 |
<< value << '\''; |
| 1077 |
throw dmlc::ParamError(os.str()); |
| 1078 |
} catch (const std::out_of_range &) { |
| 1079 |
std::ostringstream os; |
| 1080 |
os << "Out of range value for " << key_ << ", value=\'" << value << '\''; |
| 1081 |
throw dmlc::ParamError(os.str()); |
| 1082 |
} |
| 1083 |
CHECK_LE(pos, value.length()); |
| 1084 |
if (pos < value.length()) { |
| 1085 |
std::ostringstream os; |
| 1086 |
os << "Some trailing characters could not be parsed: \'" << value.substr(pos) << "\'"; |
| 1087 |
throw dmlc::ParamError(os.str()); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
protected: |
| 1092 |
|
| 1093 |
virtual void PrintValue(std::ostream &os, double value) const { |
| 1094 |
os << std::setprecision(std::numeric_limits<double>::max_digits10) << value; |
| 1095 |
} |
| 1096 |
}; |
| 1097 |
#endif |
| 1098 |
|
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
template <typename ValueType> |
| 1104 |
inline ValueType GetEnv(const char *key, ValueType default_value) { |
| 1105 |
const char *val = getenv(key); |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
if (val == nullptr || !*val) { |
| 1110 |
return default_value; |
| 1111 |
} |
| 1112 |
ValueType ret; |
| 1113 |
parameter::FieldEntry<ValueType> e; |
| 1114 |
e.Init(key, &ret, ret); |
| 1115 |
e.Set(&ret, val); |
| 1116 |
return ret; |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
template <typename ValueType> |
| 1121 |
inline void SetEnv(const char *key, ValueType value) { |
| 1122 |
parameter::FieldEntry<ValueType> e; |
| 1123 |
e.Init(key, &value, value); |
| 1124 |
#ifdef _WIN32 |
| 1125 |
_putenv_s(key, e.GetStringValue(&value).c_str()); |
| 1126 |
#else |
| 1127 |
setenv(key, e.GetStringValue(&value).c_str(), 1); |
| 1128 |
#endif |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
inline void UnsetEnv(const char *key) { |
| 1133 |
#ifdef _WIN32 |
| 1134 |
_putenv_s(key, ""); |
| 1135 |
#else |
| 1136 |
unsetenv(key); |
| 1137 |
#endif |
| 1138 |
} |
| 1139 |
} |
| 1140 |
#endif |