Add float32 histogram (#5624)
* new single_precision_histogram param was added. Co-authored-by: SHVETS, KIRILL <kirill.shvets@intel.com> Co-authored-by: fis <jm.yuan@outlook.com>
This commit is contained in:
@@ -391,46 +391,52 @@ class GHistIndexBlockMatrix {
|
||||
std::vector<Block> blocks_;
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief histogram of gradient statistics for a single node.
|
||||
* Consists of multiple GradStats, each entry showing total gradient statistics
|
||||
* for that particular bin
|
||||
* Uses global bin id so as to represent all features simultaneously
|
||||
*/
|
||||
using GHistRow = Span<tree::GradStats>;
|
||||
template<typename GradientSumT>
|
||||
using GHistRow = Span<xgboost::detail::GradientPairInternal<GradientSumT> >;
|
||||
|
||||
/*!
|
||||
* \brief fill a histogram by zeros
|
||||
*/
|
||||
void InitilizeHistByZeroes(GHistRow hist, size_t begin, size_t end);
|
||||
template<typename GradientSumT>
|
||||
void InitilizeHistByZeroes(GHistRow<GradientSumT> hist, size_t begin, size_t end);
|
||||
|
||||
/*!
|
||||
* \brief Increment hist as dst += add in range [begin, end)
|
||||
*/
|
||||
void IncrementHist(GHistRow dst, const GHistRow add, size_t begin, size_t end);
|
||||
template<typename GradientSumT>
|
||||
void IncrementHist(GHistRow<GradientSumT> dst, const GHistRow<GradientSumT> add,
|
||||
size_t begin, size_t end);
|
||||
|
||||
/*!
|
||||
* \brief Copy hist from src to dst in range [begin, end)
|
||||
*/
|
||||
void CopyHist(GHistRow dst, const GHistRow src, size_t begin, size_t end);
|
||||
template<typename GradientSumT>
|
||||
void CopyHist(GHistRow<GradientSumT> dst, const GHistRow<GradientSumT> src,
|
||||
size_t begin, size_t end);
|
||||
|
||||
/*!
|
||||
* \brief Compute Subtraction: dst = src1 - src2 in range [begin, end)
|
||||
*/
|
||||
void SubtractionHist(GHistRow dst, const GHistRow src1, const GHistRow src2,
|
||||
template<typename GradientSumT>
|
||||
void SubtractionHist(GHistRow<GradientSumT> dst, const GHistRow<GradientSumT> src1,
|
||||
const GHistRow<GradientSumT> src2,
|
||||
size_t begin, size_t end);
|
||||
|
||||
/*!
|
||||
* \brief histogram of gradient statistics for multiple nodes
|
||||
*/
|
||||
template<typename GradientSumT>
|
||||
class HistCollection {
|
||||
public:
|
||||
using GHistRowT = GHistRow<GradientSumT>;
|
||||
using GradientPairT = xgboost::detail::GradientPairInternal<GradientSumT>;
|
||||
|
||||
// access histogram for i-th node
|
||||
GHistRow operator[](bst_uint nid) const {
|
||||
GHistRowT operator[](bst_uint nid) const {
|
||||
constexpr uint32_t kMax = std::numeric_limits<uint32_t>::max();
|
||||
CHECK_NE(row_ptr_[nid], kMax);
|
||||
tree::GradStats* ptr =
|
||||
const_cast<tree::GradStats*>(dmlc::BeginPtr(data_) + row_ptr_[nid]);
|
||||
GradientPairT* ptr =
|
||||
const_cast<GradientPairT*>(dmlc::BeginPtr(data_) + row_ptr_[nid]);
|
||||
return {ptr, nbins_};
|
||||
}
|
||||
|
||||
@@ -473,7 +479,7 @@ class HistCollection {
|
||||
/*! \brief amount of active nodes in hist collection */
|
||||
uint32_t n_nodes_added_ = 0;
|
||||
|
||||
std::vector<tree::GradStats> data_;
|
||||
std::vector<GradientPairT> data_;
|
||||
|
||||
/*! \brief row_ptr_[nid] locates bin for histogram of node nid */
|
||||
std::vector<size_t> row_ptr_;
|
||||
@@ -484,8 +490,11 @@ class HistCollection {
|
||||
* Supports processing multiple tree-nodes for nested parallelism
|
||||
* Able to reduce histograms across threads in efficient way
|
||||
*/
|
||||
template<typename GradientSumT>
|
||||
class ParallelGHistBuilder {
|
||||
public:
|
||||
using GHistRowT = GHistRow<GradientSumT>;
|
||||
|
||||
void Init(size_t nbins) {
|
||||
if (nbins != nbins_) {
|
||||
hist_buffer_.Init(nbins);
|
||||
@@ -496,7 +505,7 @@ class ParallelGHistBuilder {
|
||||
// Add new elements if needed, mark all hists as unused
|
||||
// targeted_hists - already allocated hists which should contain final results after Reduce() call
|
||||
void Reset(size_t nthreads, size_t nodes, const BlockedSpace2d& space,
|
||||
const std::vector<GHistRow>& targeted_hists) {
|
||||
const std::vector<GHistRowT>& targeted_hists) {
|
||||
hist_buffer_.Init(nbins_);
|
||||
tid_nid_to_hist_.clear();
|
||||
hist_memory_.clear();
|
||||
@@ -518,12 +527,12 @@ class ParallelGHistBuilder {
|
||||
}
|
||||
|
||||
// Get specified hist, initialize hist by zeros if it wasn't used before
|
||||
GHistRow GetInitializedHist(size_t tid, size_t nid) {
|
||||
GHistRowT GetInitializedHist(size_t tid, size_t nid) {
|
||||
CHECK_LT(nid, nodes_);
|
||||
CHECK_LT(tid, nthreads_);
|
||||
|
||||
size_t idx = tid_nid_to_hist_.at({tid, nid});
|
||||
GHistRow hist = hist_memory_[idx];
|
||||
GHistRowT hist = hist_memory_[idx];
|
||||
|
||||
if (!hist_was_used_[tid * nodes_ + nid]) {
|
||||
InitilizeHistByZeroes(hist, 0, hist.size());
|
||||
@@ -538,14 +547,14 @@ class ParallelGHistBuilder {
|
||||
CHECK_GT(end, begin);
|
||||
CHECK_LT(nid, nodes_);
|
||||
|
||||
GHistRow dst = targeted_hists_[nid];
|
||||
GHistRowT dst = targeted_hists_[nid];
|
||||
|
||||
bool is_updated = false;
|
||||
for (size_t tid = 0; tid < nthreads_; ++tid) {
|
||||
if (hist_was_used_[tid * nodes_ + nid]) {
|
||||
is_updated = true;
|
||||
const size_t idx = tid_nid_to_hist_.at({tid, nid});
|
||||
GHistRow src = hist_memory_[idx];
|
||||
GHistRowT src = hist_memory_[idx];
|
||||
|
||||
if (dst.data() != src.data()) {
|
||||
IncrementHist(dst, src, begin, end);
|
||||
@@ -636,7 +645,7 @@ class ParallelGHistBuilder {
|
||||
/*! \brief number of nodes which will be processed in parallel */
|
||||
size_t nodes_ = 0;
|
||||
/*! \brief Buffer for additional histograms for Parallel processing */
|
||||
HistCollection hist_buffer_;
|
||||
HistCollection<GradientSumT> hist_buffer_;
|
||||
/*!
|
||||
* \brief Marks which hists were used, it means that they should be merged.
|
||||
* Contains only {true or false} values
|
||||
@@ -647,9 +656,9 @@ class ParallelGHistBuilder {
|
||||
/*! \brief Buffer for additional histograms for Parallel processing */
|
||||
std::vector<bool> threads_to_nids_map_;
|
||||
/*! \brief Contains histograms for final results */
|
||||
std::vector<GHistRow> targeted_hists_;
|
||||
std::vector<GHistRowT> targeted_hists_;
|
||||
/*! \brief Allocated memory for histograms used for construction */
|
||||
std::vector<GHistRow> hist_memory_;
|
||||
std::vector<GHistRowT> hist_memory_;
|
||||
/*! \brief map pair {tid, nid} to index of allocated histogram from hist_memory_ */
|
||||
std::map<std::pair<size_t, size_t>, size_t> tid_nid_to_hist_;
|
||||
};
|
||||
@@ -657,8 +666,11 @@ class ParallelGHistBuilder {
|
||||
/*!
|
||||
* \brief builder for histograms of gradient statistics
|
||||
*/
|
||||
template<typename GradientSumT>
|
||||
class GHistBuilder {
|
||||
public:
|
||||
using GHistRowT = GHistRow<GradientSumT>;
|
||||
|
||||
GHistBuilder() = default;
|
||||
GHistBuilder(size_t nthread, uint32_t nbins) : nthread_{nthread}, nbins_{nbins} {}
|
||||
|
||||
@@ -666,15 +678,17 @@ class GHistBuilder {
|
||||
void BuildHist(const std::vector<GradientPair>& gpair,
|
||||
const RowSetCollection::Elem row_indices,
|
||||
const GHistIndexMatrix& gmat,
|
||||
GHistRow hist,
|
||||
GHistRowT hist,
|
||||
bool isDense);
|
||||
// same, with feature grouping
|
||||
void BuildBlockHist(const std::vector<GradientPair>& gpair,
|
||||
const RowSetCollection::Elem row_indices,
|
||||
const GHistIndexBlockMatrix& gmatb,
|
||||
GHistRow hist);
|
||||
GHistRowT hist);
|
||||
// construct a histogram via subtraction trick
|
||||
void SubtractionTrick(GHistRow self, GHistRow sibling, GHistRow parent);
|
||||
void SubtractionTrick(GHistRowT self,
|
||||
GHistRowT sibling,
|
||||
GHistRowT parent);
|
||||
|
||||
uint32_t GetNumBins() const {
|
||||
return nbins_;
|
||||
|
||||
Reference in New Issue
Block a user