/*! * Copyright 2017-2022 by XGBoost Contributors * \file updater_quantile_hist.h * \brief use quantized feature values to construct a tree * \author Philip Cho, Tianqi Chen, Egor Smirnov */ #ifndef XGBOOST_TREE_UPDATER_QUANTILE_HIST_H_ #define XGBOOST_TREE_UPDATER_QUANTILE_HIST_H_ #include #include #include #include #include #include #include #include "xgboost/base.h" #include "xgboost/data.h" #include "xgboost/json.h" #include "hist/evaluate_splits.h" #include "hist/histogram.h" #include "hist/expand_entry.h" #include "common_row_partitioner.h" #include "constraints.h" #include "./param.h" #include "./driver.h" #include "../common/random.h" #include "../common/timer.h" #include "../common/hist_util.h" #include "../common/row_set.h" #include "../common/partition_builder.h" #include "../common/column_matrix.h" namespace xgboost::tree { inline BatchParam HistBatch(TrainParam const* param) { return {param->max_bin, param->sparse_threshold}; } /*! \brief construct a tree using quantized feature values */ class QuantileHistMaker: public TreeUpdater { public: explicit QuantileHistMaker(Context const* ctx, ObjInfo const* task) : TreeUpdater(ctx), task_{task} {} void Configure(const Args&) override {} void Update(TrainParam const* param, HostDeviceVector* gpair, DMatrix* dmat, common::Span> out_position, const std::vector& trees) override; bool UpdatePredictionCache(const DMatrix *data, linalg::VectorView out_preds) override; void LoadConfig(Json const&) override {} void SaveConfig(Json*) const override {} [[nodiscard]] char const* Name() const override { return "grow_quantile_histmaker"; } [[nodiscard]] bool HasNodePosition() const override { return true; } protected: // actual builder that runs the algorithm struct Builder { public: // constructor explicit Builder(const size_t n_trees, TrainParam const* param, DMatrix const* fmat, ObjInfo task, Context const* ctx) : n_trees_(n_trees), param_(param), p_last_fmat_(fmat), histogram_builder_{new HistogramBuilder}, task_{task}, ctx_{ctx}, monitor_{std::make_unique()} { monitor_->Init("Quantile::Builder"); } // update one tree, growing void UpdateTree(HostDeviceVector* gpair, DMatrix* p_fmat, RegTree* p_tree, HostDeviceVector* p_out_position); bool UpdatePredictionCache(DMatrix const* data, linalg::VectorView out_preds) const; private: // initialize temp data structure void InitData(DMatrix* fmat, const RegTree& tree, std::vector* gpair); size_t GetNumberOfTrees(); CPUExpandEntry InitRoot(DMatrix* p_fmat, RegTree* p_tree, const std::vector& gpair_h); void BuildHistogram(DMatrix* p_fmat, RegTree* p_tree, std::vector const& valid_candidates, std::vector const& gpair); void LeafPartition(RegTree const& tree, common::Span gpair, std::vector* p_out_position); void ExpandTree(DMatrix* p_fmat, RegTree* p_tree, const std::vector& gpair_h, HostDeviceVector* p_out_position); private: const size_t n_trees_; TrainParam const* param_; std::shared_ptr column_sampler_{ std::make_shared()}; std::vector gpair_local_; std::unique_ptr> evaluator_; std::vector partitioner_; // back pointers to tree and data matrix const RegTree* p_last_tree_{nullptr}; DMatrix const* const p_last_fmat_; std::unique_ptr> histogram_builder_; ObjInfo task_; // Context for number of threads Context const* ctx_; std::unique_ptr monitor_; }; protected: std::unique_ptr pimpl_; ObjInfo const* task_; }; } // namespace xgboost::tree #endif // XGBOOST_TREE_UPDATER_QUANTILE_HIST_H_