Small cleanup to gradient index and hist. (#7668)

* Code comments.
* Const accessor to index.
* Remove some weird variables in the `Index` class.
* Simplify the `MemStackAllocator`.
This commit is contained in:
Jiaming Yuan
2022-02-23 11:37:21 +08:00
committed by GitHub
parent 49c74a5369
commit 6762c45494
12 changed files with 149 additions and 148 deletions

View File

@@ -35,14 +35,12 @@ class ApproxRowPartitioner {
std::vector<uint32_t> const &cut_ptrs,
std::vector<float> const &cut_values) {
int32_t gidx = -1;
auto const &row_ptr = index.row_ptr;
auto get_rid = [&](size_t ridx) { return row_ptr[ridx - index.base_rowid]; };
if (index.IsDense()) {
gidx = index.index[get_rid(ridx) + fidx];
// RowIdx returns the starting pos of this row
gidx = index.index[index.RowIdx(ridx) + fidx];
} else {
auto begin = get_rid(ridx);
auto end = get_rid(ridx + 1);
auto begin = index.RowIdx(ridx);
auto end = index.RowIdx(ridx + 1);
auto f_begin = cut_ptrs[fidx];
auto f_end = cut_ptrs[fidx + 1];
gidx = common::BinarySearchBin(begin, end, index.index, f_begin, f_end);

View File

@@ -135,7 +135,7 @@ void QuantileHistMaker::Builder<GradientSumT>::InitRoot(
{
auto nid = RegTree::kRoot;
GHistRowT hist = this->histogram_builder_->Histogram()[nid];
auto hist = this->histogram_builder_->Histogram()[nid];
GradientPairT grad_stat;
if (data_layout_ == DataLayout::kDenseDataZeroBased ||
data_layout_ == DataLayout::kDenseDataOneBased) {
@@ -149,7 +149,7 @@ void QuantileHistMaker::Builder<GradientSumT>::InitRoot(
grad_stat.Add(et.GetGrad(), et.GetHess());
}
} else {
const RowSetCollection::Elem e = row_set_collection_[nid];
const common::RowSetCollection::Elem e = row_set_collection_[nid];
for (const size_t *it = e.begin; it < e.end; ++it) {
grad_stat.Add(gpair_h[*it].GetGrad(), gpair_h[*it].GetHess());
}
@@ -229,7 +229,7 @@ template<typename GradientSumT>
template <bool any_missing>
void QuantileHistMaker::Builder<GradientSumT>::ExpandTree(
const GHistIndexMatrix& gmat,
const ColumnMatrix& column_matrix,
const common::ColumnMatrix& column_matrix,
DMatrix* p_fmat,
RegTree* p_tree,
const std::vector<GradientPair>& gpair_h) {

View File

@@ -147,7 +147,7 @@ class QuantileHistMaker: public TreeUpdater {
// training parameter
TrainParam param_;
// column accessor
ColumnMatrix column_matrix_;
common::ColumnMatrix column_matrix_;
DMatrix const* p_last_dmat_ {nullptr};
bool is_gmat_initialized_ {false};
@@ -155,7 +155,6 @@ class QuantileHistMaker: public TreeUpdater {
template<typename GradientSumT>
struct Builder {
public:
using GHistRowT = GHistRow<GradientSumT>;
using GradientPairT = xgboost::detail::GradientPairInternal<GradientSumT>;
// constructor
explicit Builder(const size_t n_trees, const TrainParam& param,
@@ -164,7 +163,6 @@ class QuantileHistMaker: public TreeUpdater {
: n_trees_(n_trees),
param_(param),
pruner_(std::move(pruner)),
p_last_tree_(nullptr),
p_last_fmat_(fmat),
histogram_builder_{new HistogramBuilder<GradientSumT, CPUExpandEntry>},
task_{task},
@@ -172,7 +170,7 @@ class QuantileHistMaker: public TreeUpdater {
builder_monitor_.Init("Quantile::Builder");
}
// update one tree, growing
void Update(const GHistIndexMatrix& gmat, const ColumnMatrix& column_matrix,
void Update(const GHistIndexMatrix& gmat, const common::ColumnMatrix& column_matrix,
HostDeviceVector<GradientPair>* gpair, DMatrix* p_fmat, RegTree* p_tree);
bool UpdatePredictionCache(const DMatrix* data,