| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef XGBOOST_TREE_MODEL_H_ |
| 8 |
#define XGBOOST_TREE_MODEL_H_ |
| 9 |
|
| 10 |
#include <xgboost/base.h> |
| 11 |
#include <xgboost/data.h> |
| 12 |
#include <xgboost/feature_map.h> |
| 13 |
#include <xgboost/host_device_vector.h> |
| 14 |
#include <xgboost/linalg.h> |
| 15 |
#include <xgboost/logging.h> |
| 16 |
#include <xgboost/model.h> |
| 17 |
#include <xgboost/multi_target_tree_model.h> |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <cstring> |
| 21 |
#include <limits> |
| 22 |
#include <memory> |
| 23 |
#include <string> |
| 24 |
#include <type_traits> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
namespace xgboost { |
| 28 |
|
| 29 |
namespace tree { |
| 30 |
struct ScalarTreeView; |
| 31 |
struct MultiTargetTreeView; |
| 32 |
} |
| 33 |
|
| 34 |
class Json; |
| 35 |
|
| 36 |
|
| 37 |
struct TreeParam { |
| 38 |
|
| 39 |
bst_node_t num_nodes{1}; |
| 40 |
|
| 41 |
bst_node_t num_deleted{0}; |
| 42 |
|
| 43 |
bst_feature_t num_feature{0}; |
| 44 |
|
| 45 |
bst_target_t size_leaf_vector{1}; |
| 46 |
|
| 47 |
bool operator==(const TreeParam& b) const { |
| 48 |
return num_nodes == b.num_nodes && num_deleted == b.num_deleted && |
| 49 |
num_feature == b.num_feature && size_leaf_vector == b.size_leaf_vector; |
| 50 |
} |
| 51 |
|
| 52 |
void FromJson(Json const& in); |
| 53 |
void ToJson(Json* p_out) const; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
struct RTreeNodeStat { |
| 58 |
|
| 59 |
float loss_chg; |
| 60 |
|
| 61 |
float sum_hess; |
| 62 |
|
| 63 |
float base_weight; |
| 64 |
|
| 65 |
int leaf_child_cnt{0}; |
| 66 |
|
| 67 |
RTreeNodeStat() = default; |
| 68 |
RTreeNodeStat(float loss_chg, float sum_hess, float weight) |
| 69 |
: loss_chg{loss_chg}, sum_hess{sum_hess}, base_weight{weight} {} |
| 70 |
bool operator==(const RTreeNodeStat& b) const { |
| 71 |
return loss_chg == b.loss_chg && sum_hess == b.sum_hess && base_weight == b.base_weight && |
| 72 |
leaf_child_cnt == b.leaf_child_cnt; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
class RegTree : public Model { |
| 82 |
public: |
| 83 |
using SplitCondT = float; |
| 84 |
static constexpr bst_node_t kInvalidNodeId{MultiTargetTree::InvalidNodeId()}; |
| 85 |
static constexpr uint32_t kDeletedNodeMarker = std::numeric_limits<uint32_t>::max(); |
| 86 |
static constexpr bst_node_t kRoot{0}; |
| 87 |
|
| 88 |
|
| 89 |
class Node { |
| 90 |
public: |
| 91 |
XGBOOST_DEVICE Node() { |
| 92 |
|
| 93 |
static_assert(sizeof(Node) == 4 * sizeof(int) + sizeof(Info), "Node: 64 bit align"); |
| 94 |
} |
| 95 |
Node(int32_t cleft, int32_t cright, int32_t parent, uint32_t split_ind, float split_cond, |
| 96 |
bool default_left) |
| 97 |
: parent_{parent}, cleft_{cleft}, cright_{cright} { |
| 98 |
this->SetParent(parent_); |
| 99 |
this->SetSplit(split_ind, split_cond, default_left); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
[[nodiscard]] XGBOOST_DEVICE int LeftChild() const { return this->cleft_; } |
| 104 |
|
| 105 |
[[nodiscard]] XGBOOST_DEVICE int RightChild() const { return this->cright_; } |
| 106 |
|
| 107 |
[[nodiscard]] XGBOOST_DEVICE int DefaultChild() const { |
| 108 |
return this->DefaultLeft() ? this->LeftChild() : this->RightChild(); |
| 109 |
} |
| 110 |
|
| 111 |
[[nodiscard]] XGBOOST_DEVICE bst_feature_t SplitIndex() const { |
| 112 |
static_assert(!std::is_signed_v<bst_feature_t>); |
| 113 |
return sindex_ & ((1U << 31) - 1U); |
| 114 |
} |
| 115 |
|
| 116 |
[[nodiscard]] XGBOOST_DEVICE bool DefaultLeft() const { return (sindex_ >> 31) != 0; } |
| 117 |
|
| 118 |
[[nodiscard]] XGBOOST_DEVICE bool IsLeaf() const { return cleft_ == kInvalidNodeId; } |
| 119 |
|
| 120 |
[[nodiscard]] XGBOOST_DEVICE float LeafValue() const { return (this->info_).leaf_value; } |
| 121 |
|
| 122 |
[[nodiscard]] XGBOOST_DEVICE SplitCondT SplitCond() const { return (this->info_).split_cond; } |
| 123 |
|
| 124 |
[[nodiscard]] XGBOOST_DEVICE int Parent() const { return parent_ & ((1U << 31) - 1); } |
| 125 |
|
| 126 |
[[nodiscard]] XGBOOST_DEVICE bool IsLeftChild() const { return (parent_ & (1U << 31)) != 0; } |
| 127 |
|
| 128 |
[[nodiscard]] XGBOOST_DEVICE bool IsDeleted() const { return sindex_ == kDeletedNodeMarker; } |
| 129 |
|
| 130 |
[[nodiscard]] XGBOOST_DEVICE bool IsRoot() const { return parent_ == kInvalidNodeId; } |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
XGBOOST_DEVICE void SetLeftChild(int nid) { |
| 136 |
this->cleft_ = nid; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
XGBOOST_DEVICE void SetRightChild(int nid) { |
| 143 |
this->cright_ = nid; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
XGBOOST_DEVICE void SetSplit(unsigned split_index, SplitCondT split_cond, |
| 152 |
bool default_left = false) { |
| 153 |
if (default_left) split_index |= (1U << 31); |
| 154 |
this->sindex_ = split_index; |
| 155 |
(this->info_).split_cond = split_cond; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
XGBOOST_DEVICE void SetLeaf(bst_float value, int right = kInvalidNodeId) { |
| 164 |
(this->info_).leaf_value = value; |
| 165 |
this->cleft_ = kInvalidNodeId; |
| 166 |
this->cright_ = right; |
| 167 |
} |
| 168 |
|
| 169 |
XGBOOST_DEVICE void MarkDelete() { |
| 170 |
this->sindex_ = kDeletedNodeMarker; |
| 171 |
} |
| 172 |
|
| 173 |
XGBOOST_DEVICE void Reuse() { |
| 174 |
this->sindex_ = 0; |
| 175 |
} |
| 176 |
|
| 177 |
XGBOOST_DEVICE void SetParent(int pidx, bool is_left_child = true) { |
| 178 |
if (is_left_child) pidx |= (1U << 31); |
| 179 |
this->parent_ = pidx; |
| 180 |
} |
| 181 |
bool operator==(const Node& b) const { |
| 182 |
return parent_ == b.parent_ && cleft_ == b.cleft_ && |
| 183 |
cright_ == b.cright_ && sindex_ == b.sindex_ && |
| 184 |
info_.leaf_value == b.info_.leaf_value; |
| 185 |
} |
| 186 |
|
| 187 |
private: |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
union Info{ |
| 193 |
bst_float leaf_value; |
| 194 |
SplitCondT split_cond; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
int32_t parent_{kInvalidNodeId}; |
| 199 |
|
| 200 |
int32_t cleft_{kInvalidNodeId}, cright_{kInvalidNodeId}; |
| 201 |
|
| 202 |
uint32_t sindex_{0}; |
| 203 |
|
| 204 |
Info info_; |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
void ChangeToLeaf(bst_node_t nidx, float value) { |
| 214 |
auto& h_nodes = nodes_.HostVector(); |
| 215 |
CHECK(h_nodes[h_nodes[nidx].LeftChild()].IsLeaf()); |
| 216 |
CHECK(h_nodes[h_nodes[nidx].RightChild()].IsLeaf()); |
| 217 |
this->DeleteNode(h_nodes[nidx].LeftChild()); |
| 218 |
this->DeleteNode(h_nodes[nidx].RightChild()); |
| 219 |
h_nodes[nidx].SetLeaf(value); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
void CollapseToLeaf(bst_node_t nidx, float value) { |
| 228 |
auto& h_nodes = nodes_.HostVector(); |
| 229 |
if (h_nodes[nidx].IsLeaf()) return; |
| 230 |
if (!h_nodes[h_nodes[nidx].LeftChild()].IsLeaf()) { |
| 231 |
CollapseToLeaf(h_nodes[nidx].LeftChild(), 0.0f); |
| 232 |
} |
| 233 |
if (!h_nodes[h_nodes[nidx].RightChild()].IsLeaf()) { |
| 234 |
CollapseToLeaf(h_nodes[nidx].RightChild(), 0.0f); |
| 235 |
} |
| 236 |
this->ChangeToLeaf(nidx, value); |
| 237 |
} |
| 238 |
|
| 239 |
RegTree() { |
| 240 |
nodes_.HostVector().resize(param_.num_nodes); |
| 241 |
stats_.HostVector().resize(param_.num_nodes); |
| 242 |
split_types_.HostVector().resize(param_.num_nodes, FeatureType::kNumerical); |
| 243 |
split_categories_segments_.HostVector().resize(param_.num_nodes); |
| 244 |
auto& h_nodes = nodes_.HostVector(); |
| 245 |
for (int i = 0; i < param_.num_nodes; i++) { |
| 246 |
h_nodes[i].SetLeaf(0.0f); |
| 247 |
h_nodes[i].SetParent(kInvalidNodeId); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
explicit RegTree(bst_target_t n_targets, bst_feature_t n_features) : RegTree{} { |
| 254 |
param_.num_feature = n_features; |
| 255 |
param_.size_leaf_vector = n_targets; |
| 256 |
if (n_targets > 1) { |
| 257 |
this->p_mt_tree_.reset(new MultiTargetTree{¶m_}); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
Node& operator[](bst_node_t nidx) { return nodes_.HostVector()[nidx]; } |
| 263 |
|
| 264 |
public: |
| 265 |
|
| 266 |
[[nodiscard]] common::Span<Node const> GetNodes(DeviceOrd device) const { |
| 267 |
CHECK(!this->IsMultiTarget()); |
| 268 |
return device.IsCPU() ? nodes_.ConstHostSpan() |
| 269 |
: (nodes_.SetDevice(device), nodes_.ConstDeviceSpan()); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
[[nodiscard]] common::Span<RTreeNodeStat const> GetStats(DeviceOrd device) const { |
| 274 |
CHECK(!this->IsMultiTarget()); |
| 275 |
return device.IsCPU() ? stats_.ConstHostSpan() |
| 276 |
: (stats_.SetDevice(device), stats_.ConstDeviceSpan()); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
RTreeNodeStat& Stat(int nid) { |
| 281 |
return stats_.HostVector()[nid]; |
| 282 |
} |
| 283 |
|
| 284 |
void LoadModel(Json const& in) override; |
| 285 |
void SaveModel(Json* out) const override; |
| 286 |
|
| 287 |
bool operator==(const RegTree& b) const { |
| 288 |
return nodes_.ConstHostVector() == b.nodes_.ConstHostVector() && |
| 289 |
stats_.ConstHostVector() == b.stats_.ConstHostVector() && |
| 290 |
deleted_nodes_ == b.deleted_nodes_ && param_ == b.param_; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
[[nodiscard]] bool Equal(const RegTree& b) const; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
void ExpandNode(bst_node_t nid, unsigned split_index, bst_float split_value, |
| 318 |
bool default_left, bst_float base_weight, |
| 319 |
bst_float left_leaf_weight, bst_float right_leaf_weight, |
| 320 |
bst_float loss_change, float sum_hess, float left_sum, |
| 321 |
float right_sum, |
| 322 |
bst_node_t leaf_right_child = kInvalidNodeId); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
void ExpandNode(bst_node_t nidx, bst_feature_t split_index, float split_cond, bool default_left, |
| 332 |
linalg::VectorView<float const> base_weight, |
| 333 |
linalg::VectorView<float const> left_weight, |
| 334 |
linalg::VectorView<float const> right_weight, float loss_chg, float sum_hess, |
| 335 |
float left_sum, float right_sum); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
void SetLeaves(std::vector<bst_node_t> leaves, common::Span<float const> weights); |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
void ExpandCategorical(bst_node_t nid, bst_feature_t split_index, |
| 364 |
common::Span<const uint32_t> split_cat, bool default_left, |
| 365 |
bst_float base_weight, bst_float left_leaf_weight, |
| 366 |
bst_float right_leaf_weight, bst_float loss_change, float sum_hess, |
| 367 |
float left_sum, float right_sum); |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
[[nodiscard]] bool HasCategoricalSplit() const { return !split_categories_.Empty(); } |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
[[nodiscard]] bool IsMultiTarget() const { return static_cast<bool>(p_mt_tree_); } |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
[[nodiscard]] bst_target_t NumTargets() const { return param_.size_leaf_vector; } |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
[[nodiscard]] auto GetMultiTargetTree() const { |
| 384 |
CHECK(IsMultiTarget()); |
| 385 |
return p_mt_tree_.get(); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
[[nodiscard]] bst_feature_t NumFeatures() const noexcept { return param_.num_feature; } |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
[[nodiscard]] bst_node_t NumNodes() const noexcept { return param_.num_nodes; } |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
[[nodiscard]] bst_node_t NumValidNodes() const noexcept { |
| 399 |
return param_.num_nodes - param_.num_deleted; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
[[nodiscard]] bst_node_t NumExtraNodes() const noexcept { |
| 405 |
return param_.num_nodes - 1 - param_.num_deleted; |
| 406 |
} |
| 407 |
|
| 408 |
[[nodiscard]] bst_node_t GetNumLeaves() const; |
| 409 |
[[nodiscard]] bst_node_t GetNumSplitNodes() const; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
[[nodiscard]] bst_node_t GetDepth(bst_node_t nidx) const; |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
void SetRoot(linalg::VectorView<float const> weight, float sum_hess) { |
| 422 |
CHECK(IsMultiTarget()); |
| 423 |
return this->p_mt_tree_->SetRoot(weight, sum_hess); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
[[nodiscard]] bst_node_t MaxDepth() const; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
struct FVec { |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
void Init(size_t size); |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
void Fill(SparsePage::Inst const& inst); |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
void Drop(); |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
[[nodiscard]] size_t Size() const; |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
[[nodiscard]] bst_float GetFvalue(size_t i) const; |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
[[nodiscard]] bool IsMissing(size_t i) const; |
| 468 |
[[nodiscard]] bool HasMissing() const; |
| 469 |
void HasMissing(bool has_missing) { this->has_missing_ = has_missing; } |
| 470 |
|
| 471 |
[[nodiscard]] common::Span<float> Data() { return data_; } |
| 472 |
|
| 473 |
private: |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
std::vector<float> data_; |
| 480 |
bool has_missing_; |
| 481 |
}; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
[[nodiscard]] std::string DumpModel(const FeatureMap& fmap, bool with_stats, |
| 491 |
std::string format) const; |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
[[nodiscard]] common::Span<FeatureType const> GetSplitTypes(DeviceOrd device) const { |
| 496 |
return device.IsCPU() ? split_types_.ConstHostSpan() |
| 497 |
: (split_types_.SetDevice(device), split_types_.ConstDeviceSpan()); |
| 498 |
} |
| 499 |
[[nodiscard]] common::Span<uint32_t const> GetSplitCategories(DeviceOrd device) const { |
| 500 |
return device.IsCPU() |
| 501 |
? split_categories_.ConstHostSpan() |
| 502 |
: (split_categories_.SetDevice(device), split_categories_.ConstDeviceSpan()); |
| 503 |
} |
| 504 |
[[nodiscard]] auto const& GetSplitCategoriesPtr() const { |
| 505 |
return split_categories_segments_.ConstHostVector(); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
struct CategoricalSplitMatrix { |
| 516 |
struct Segment { |
| 517 |
std::size_t beg{0}; |
| 518 |
std::size_t size{0}; |
| 519 |
}; |
| 520 |
common::Span<FeatureType const> split_type; |
| 521 |
common::Span<uint32_t const> categories; |
| 522 |
common::Span<Segment const> node_ptr; |
| 523 |
}; |
| 524 |
|
| 525 |
[[nodiscard]] CategoricalSplitMatrix GetCategoriesMatrix(DeviceOrd device) const { |
| 526 |
CategoricalSplitMatrix view; |
| 527 |
view.split_type = this->GetSplitTypes(device); |
| 528 |
view.categories = this->GetSplitCategories(device); |
| 529 |
if (device.IsCPU()) { |
| 530 |
view.node_ptr = split_categories_segments_.ConstHostSpan(); |
| 531 |
} else { |
| 532 |
split_categories_segments_.SetDevice(device); |
| 533 |
view.node_ptr = split_categories_segments_.ConstDeviceSpan(); |
| 534 |
} |
| 535 |
return view; |
| 536 |
} |
| 537 |
|
| 538 |
[[nodiscard]] bst_node_t LeftChild(bst_node_t nidx) const { |
| 539 |
if (IsMultiTarget()) { |
| 540 |
return this->p_mt_tree_->LeftChild(nidx); |
| 541 |
} |
| 542 |
return nodes_.ConstHostVector()[nidx].LeftChild(); |
| 543 |
} |
| 544 |
[[nodiscard]] bst_node_t RightChild(bst_node_t nidx) const { |
| 545 |
if (IsMultiTarget()) { |
| 546 |
return this->p_mt_tree_->RightChild(nidx); |
| 547 |
} |
| 548 |
return nodes_.ConstHostVector()[nidx].RightChild(); |
| 549 |
} |
| 550 |
[[nodiscard]] bst_node_t Size() const { |
| 551 |
if (IsMultiTarget()) { |
| 552 |
return this->p_mt_tree_->Size(); |
| 553 |
} |
| 554 |
return this->nodes_.Size(); |
| 555 |
} |
| 556 |
|
| 557 |
[[nodiscard]] RegTree* Copy() const; |
| 558 |
tree::ScalarTreeView HostScView() const; |
| 559 |
tree::MultiTargetTreeView HostMtView() const; |
| 560 |
|
| 561 |
private: |
| 562 |
template <bool typed> |
| 563 |
void LoadCategoricalSplit(Json const& in); |
| 564 |
void SaveCategoricalSplit(Json* p_out) const; |
| 565 |
|
| 566 |
TreeParam param_; |
| 567 |
|
| 568 |
HostDeviceVector<Node> nodes_; |
| 569 |
|
| 570 |
std::vector<int> deleted_nodes_; |
| 571 |
|
| 572 |
HostDeviceVector<RTreeNodeStat> stats_; |
| 573 |
HostDeviceVector<FeatureType> split_types_; |
| 574 |
|
| 575 |
|
| 576 |
HostDeviceVector<uint32_t> split_categories_; |
| 577 |
|
| 578 |
HostDeviceVector<CategoricalSplitMatrix::Segment> split_categories_segments_; |
| 579 |
|
| 580 |
std::unique_ptr<MultiTargetTree> p_mt_tree_; |
| 581 |
|
| 582 |
|
| 583 |
bst_node_t AllocNode() { |
| 584 |
if (param_.num_deleted != 0) { |
| 585 |
int nid = deleted_nodes_.back(); |
| 586 |
deleted_nodes_.pop_back(); |
| 587 |
nodes_.HostVector()[nid].Reuse(); |
| 588 |
--param_.num_deleted; |
| 589 |
return nid; |
| 590 |
} |
| 591 |
int nd = param_.num_nodes++; |
| 592 |
CHECK_LT(param_.num_nodes, std::numeric_limits<int>::max()) |
| 593 |
<< "number of nodes in the tree exceed 2^31"; |
| 594 |
nodes_.HostVector().resize(param_.num_nodes); |
| 595 |
stats_.HostVector().resize(param_.num_nodes); |
| 596 |
split_types_.HostVector().resize(param_.num_nodes, FeatureType::kNumerical); |
| 597 |
split_categories_segments_.HostVector().resize(param_.num_nodes); |
| 598 |
return nd; |
| 599 |
} |
| 600 |
|
| 601 |
void DeleteNode(int nid) { |
| 602 |
CHECK_GE(nid, 1); |
| 603 |
auto pid = (*this)[nid].Parent(); |
| 604 |
if (nid == (*this)[pid].LeftChild()) { |
| 605 |
(*this)[pid].SetLeftChild(kInvalidNodeId); |
| 606 |
} else { |
| 607 |
(*this)[pid].SetRightChild(kInvalidNodeId); |
| 608 |
} |
| 609 |
|
| 610 |
deleted_nodes_.push_back(nid); |
| 611 |
nodes_.HostVector()[nid].MarkDelete(); |
| 612 |
++param_.num_deleted; |
| 613 |
} |
| 614 |
}; |
| 615 |
|
| 616 |
inline void RegTree::FVec::Init(size_t size) { |
| 617 |
data_.resize(size); |
| 618 |
std::fill(data_.begin(), data_.end(), std::numeric_limits<float>::quiet_NaN()); |
| 619 |
has_missing_ = true; |
| 620 |
} |
| 621 |
|
| 622 |
inline void RegTree::FVec::Fill(SparsePage::Inst const& inst) { |
| 623 |
auto p_data = inst.data(); |
| 624 |
auto p_out = data_.data(); |
| 625 |
|
| 626 |
for (std::size_t i = 0, n = inst.size(); i < n; ++i) { |
| 627 |
auto const& entry = p_data[i]; |
| 628 |
p_out[entry.index] = entry.fvalue; |
| 629 |
} |
| 630 |
has_missing_ = data_.size() != inst.size(); |
| 631 |
} |
| 632 |
|
| 633 |
inline void RegTree::FVec::Drop() { this->Init(this->Size()); } |
| 634 |
|
| 635 |
inline size_t RegTree::FVec::Size() const { |
| 636 |
return data_.size(); |
| 637 |
} |
| 638 |
|
| 639 |
inline float RegTree::FVec::GetFvalue(size_t i) const { |
| 640 |
return data_[i]; |
| 641 |
} |
| 642 |
|
| 643 |
inline bool RegTree::FVec::IsMissing(size_t i) const { return std::isnan(data_[i]); } |
| 644 |
|
| 645 |
inline bool RegTree::FVec::HasMissing() const { return has_missing_; } |
| 646 |
|
| 647 |
|
| 648 |
inline StringView MTNotImplemented() { |
| 649 |
return " support for multi-target tree is not yet implemented."; |
| 650 |
} |
| 651 |
} |
| 652 |
#endif |