xgboost
base.h
Go to the documentation of this file.
1 
6 #ifndef XGBOOST_BASE_H_
7 #define XGBOOST_BASE_H_
8 
9 #include <dmlc/omp.h> // for omp_uint, omp_ulong
10 // Put the windefs here to guard as many files as possible.
11 #include <xgboost/windefs.h>
12 
13 #include <cstdint> // for int32_t, uint64_t, int16_t
14 #include <ostream> // for ostream
15 #include <string> // for string
16 #include <utility> // for pair
17 #include <vector> // for vector
18 
22 #ifndef XGBOOST_STRICT_R_MODE
23 #define XGBOOST_STRICT_R_MODE 0
24 #endif // XGBOOST_STRICT_R_MODE
25 
32 #ifndef XGBOOST_LOG_WITH_TIME
33 #define XGBOOST_LOG_WITH_TIME 1
34 #endif // XGBOOST_LOG_WITH_TIME
35 
39 #ifndef XGBOOST_CUSTOMIZE_GLOBAL_PRNG
40 #define XGBOOST_CUSTOMIZE_GLOBAL_PRNG 0
41 #endif // XGBOOST_CUSTOMIZE_GLOBAL_PRNG
42 
46 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
47 #define XGBOOST_ALIGNAS(X) alignas(X)
48 #else
49 #define XGBOOST_ALIGNAS(X)
50 #endif // defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
51 
52 #if defined(__GNUC__)
53 #define XGBOOST_EXPECT(cond, ret) __builtin_expect((cond), (ret))
54 #else
55 #define XGBOOST_EXPECT(cond, ret) (cond)
56 #endif // defined(__GNUC__)
57 
61 #if defined (__CUDA__) || defined(__NVCC__)
62 #define XGBOOST_DEVICE __host__ __device__
63 #else
64 #define XGBOOST_DEVICE
65 #endif // defined (__CUDA__) || defined(__NVCC__)
66 
67 #if defined(__CUDA__) || defined(__CUDACC__)
68 #define XGBOOST_HOST_DEV_INLINE XGBOOST_DEVICE __forceinline__
69 #define XGBOOST_DEV_INLINE __device__ __forceinline__
70 #else
71 #define XGBOOST_HOST_DEV_INLINE
72 #define XGBOOST_DEV_INLINE
73 #endif // defined(__CUDA__) || defined(__CUDACC__)
74 
75 // These check are for Makefile.
76 #if !defined(XGBOOST_MM_PREFETCH_PRESENT) && !defined(XGBOOST_BUILTIN_PREFETCH_PRESENT)
77 /* default logic for software pre-fetching */
78 #if (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))) || defined(__INTEL_COMPILER)
79 // Enable _mm_prefetch for Intel compiler and MSVC+x86
80  #define XGBOOST_MM_PREFETCH_PRESENT
81  #define XGBOOST_BUILTIN_PREFETCH_PRESENT
82 #elif defined(__GNUC__)
83 // Enable __builtin_prefetch for GCC
84 #define XGBOOST_BUILTIN_PREFETCH_PRESENT
85 #endif // GUARDS
86 
87 #endif // !defined(XGBOOST_MM_PREFETCH_PRESENT) && !defined()
88 
89 namespace xgboost {
91 using bst_uint = std::uint32_t; // NOLINT
93 using bst_ulong = std::uint64_t; // NOLINT
95 using bst_float = float; // NOLINT
97 using bst_cat_t = std::int32_t; // NOLINT
99 using bst_feature_t = std::uint32_t; // NOLINT
103 using bst_bin_t = std::int32_t; // NOLINT
107 using bst_idx_t = std::uint64_t; // NOLINT
111 using bst_node_t = std::int32_t; // NOLINT
115 using bst_group_t = std::uint32_t; // NOLINT
119 using bst_target_t = std::uint32_t; // NOLINT
123 using bst_layer_t = std::int32_t; // NOLINT
127 using bst_tree_t = std::int32_t; // NOLINT
131 using bst_d_ordinal_t = std::int16_t; // NOLINT
132 
133 namespace detail {
137 template <typename T>
140  T grad_{0};
142  T hess_{0};
143 
144  XGBOOST_DEVICE void SetGrad(T g) { grad_ = g; }
145  XGBOOST_DEVICE void SetHess(T h) { hess_ = h; }
146 
147  public:
148  using ValueT = T;
149 
150  inline void Add(const ValueT& grad, const ValueT& hess) {
151  grad_ += grad;
152  hess_ += hess;
153  }
154 
155  inline static void Reduce(GradientPairInternal<T>& a, const GradientPairInternal<T>& b) { // NOLINT(*)
156  a += b;
157  }
158 
159  GradientPairInternal() = default;
160 
162  SetGrad(grad);
163  SetHess(hess);
164  }
165 
166  // Copy constructor if of same value type, marked as default to be trivially_copyable
171 
172  // Copy constructor if different value type - use getters and setters to
173  // perform conversion
174  template <typename T2>
176  SetGrad(g.GetGrad());
177  SetHess(g.GetHess());
178  }
179 
180  XGBOOST_DEVICE T GetGrad() const { return grad_; }
181  XGBOOST_DEVICE T GetHess() const { return hess_; }
182 
184  const GradientPairInternal<T> &rhs) {
185  grad_ += rhs.grad_;
186  hess_ += rhs.hess_;
187  return *this;
188  }
189 
191  const GradientPairInternal<T> &rhs) const {
193  g.grad_ = grad_ + rhs.grad_;
194  g.hess_ = hess_ + rhs.hess_;
195  return g;
196  }
197 
199  const GradientPairInternal<T> &rhs) {
200  grad_ -= rhs.grad_;
201  hess_ -= rhs.hess_;
202  return *this;
203  }
204 
206  const GradientPairInternal<T> &rhs) const {
208  g.grad_ = grad_ - rhs.grad_;
209  g.hess_ = hess_ - rhs.hess_;
210  return g;
211  }
212 
214  grad_ *= multiplier;
215  hess_ *= multiplier;
216  return *this;
217  }
218 
221  g.grad_ = grad_ * multiplier;
222  g.hess_ = hess_ * multiplier;
223  return g;
224  }
225 
227  grad_ /= divisor;
228  hess_ /= divisor;
229  return *this;
230  }
231 
234  g.grad_ = grad_ / divisor;
235  g.hess_ = hess_ / divisor;
236  return g;
237  }
238 
240  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
241  }
242 
243  XGBOOST_DEVICE explicit GradientPairInternal(int value) {
244  *this = GradientPairInternal<T>(static_cast<float>(value),
245  static_cast<float>(value));
246  }
247 
248  friend std::ostream &operator<<(std::ostream &os,
249  const GradientPairInternal<T> &g) {
250  os << g.GetGrad() << "/" << g.GetHess();
251  return os;
252  }
253 };
254 } // namespace detail
255 
260 
264  using T = int64_t;
265  T grad_ = 0;
266  T hess_ = 0;
267 
268  public:
269  using ValueT = T;
270 
271  XGBOOST_DEVICE GradientPairInt64(T grad, T hess) : grad_(grad), hess_(hess) {}
272  GradientPairInt64() = default;
273 
274  // Copy constructor if of same value type, marked as default to be trivially_copyable
277 
278  [[nodiscard]] XGBOOST_DEVICE T GetQuantisedGrad() const { return grad_; }
279  [[nodiscard]] XGBOOST_DEVICE T GetQuantisedHess() const { return hess_; }
280 
282  grad_ += rhs.grad_;
283  hess_ += rhs.hess_;
284  return *this;
285  }
286 
289  g.grad_ = grad_ + rhs.grad_;
290  g.hess_ = hess_ + rhs.hess_;
291  return g;
292  }
293 
295  grad_ -= rhs.grad_;
296  hess_ -= rhs.hess_;
297  return *this;
298  }
299 
302  g.grad_ = grad_ - rhs.grad_;
303  g.hess_ = hess_ - rhs.hess_;
304  return g;
305  }
306 
307  XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const {
308  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
309  }
310  friend std::ostream &operator<<(std::ostream &os, const GradientPairInt64 &g) {
311  os << g.GetQuantisedGrad() << "/" << g.GetQuantisedHess();
312  return os;
313  }
314 };
315 
316 using Args = std::vector<std::pair<std::string, std::string> >;
317 
319 constexpr bst_float kRtEps = 1e-6f;
320 
322 using omp_ulong = dmlc::omp_ulong; // NOLINT
324 using bst_omp_uint = dmlc::omp_uint; // NOLINT
326 using XGBoostVersionT = std::int32_t;
327 } // namespace xgboost
328 
329 #endif // XGBOOST_BASE_H_
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:64
Fixed point representation for high precision gradient pair. Has a different interface so we don't ac...
Definition: base.h:263
T ValueT
Definition: base.h:269
XGBOOST_DEVICE GradientPairInt64 operator+(const GradientPairInt64 &rhs) const
Definition: base.h:287
XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const
Definition: base.h:307
XGBOOST_DEVICE T GetQuantisedHess() const
Definition: base.h:279
GradientPairInt64(GradientPairInt64 const &g)=default
GradientPairInt64 & operator=(GradientPairInt64 const &g)=default
XGBOOST_DEVICE GradientPairInt64 & operator-=(const GradientPairInt64 &rhs)
Definition: base.h:294
friend std::ostream & operator<<(std::ostream &os, const GradientPairInt64 &g)
Definition: base.h:310
XGBOOST_DEVICE GradientPairInt64 & operator+=(const GradientPairInt64 &rhs)
Definition: base.h:281
XGBOOST_DEVICE T GetQuantisedGrad() const
Definition: base.h:278
XGBOOST_DEVICE GradientPairInt64(T grad, T hess)
Definition: base.h:271
XGBOOST_DEVICE GradientPairInt64 operator-(const GradientPairInt64 &rhs) const
Definition: base.h:300
Implementation of gradient statistics pair. Template specialisation may be used to overload different...
Definition: base.h:138
GradientPairInternal(GradientPairInternal &&g)=default
XGBOOST_DEVICE GradientPairInternal< T > operator*(float multiplier) const
Definition: base.h:219
XGBOOST_DEVICE GradientPairInternal< T > & operator+=(const GradientPairInternal< T > &rhs)
Definition: base.h:183
void Add(const ValueT &grad, const ValueT &hess)
Definition: base.h:150
T ValueT
Definition: base.h:148
friend std::ostream & operator<<(std::ostream &os, const GradientPairInternal< T > &g)
Definition: base.h:248
XGBOOST_DEVICE GradientPairInternal(T grad, T hess)
Definition: base.h:161
XGBOOST_DEVICE GradientPairInternal< T > & operator*=(float multiplier)
Definition: base.h:213
XGBOOST_DEVICE GradientPairInternal(int value)
Definition: base.h:243
XGBOOST_DEVICE GradientPairInternal< T > operator+(const GradientPairInternal< T > &rhs) const
Definition: base.h:190
XGBOOST_DEVICE GradientPairInternal< T > & operator/=(float divisor)
Definition: base.h:226
XGBOOST_DEVICE GradientPairInternal< T > operator/(float divisor) const
Definition: base.h:232
XGBOOST_DEVICE T GetHess() const
Definition: base.h:181
XGBOOST_DEVICE GradientPairInternal< T > & operator-=(const GradientPairInternal< T > &rhs)
Definition: base.h:198
GradientPairInternal & operator=(GradientPairInternal &&that)=default
XGBOOST_DEVICE GradientPairInternal< T > operator-(const GradientPairInternal< T > &rhs) const
Definition: base.h:205
static void Reduce(GradientPairInternal< T > &a, const GradientPairInternal< T > &b)
Definition: base.h:155
GradientPairInternal(GradientPairInternal const &g)=default
XGBOOST_DEVICE T GetGrad() const
Definition: base.h:180
GradientPairInternal & operator=(GradientPairInternal const &that)=default
XGBOOST_DEVICE GradientPairInternal(const GradientPairInternal< T2 > &g)
Definition: base.h:175
XGBOOST_DEVICE bool operator==(const GradientPairInternal< T > &rhs) const
Definition: base.h:239
Core data structure for multi-target trees.
Definition: base.h:89
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:316
std::uint32_t bst_group_t
Type for ranking group index.
Definition: base.h:115
std::int32_t bst_node_t
Type for tree node index.
Definition: base.h:111
dmlc::omp_ulong omp_ulong
define unsigned long for openmp loop
Definition: base.h:322
dmlc::omp_uint bst_omp_uint
define unsigned int for openmp loop
Definition: base.h:324
std::int32_t bst_bin_t
Type for histogram bin index. We sometimes use -1 to indicate invalid bin.
Definition: base.h:103
std::int32_t XGBoostVersionT
Type used for representing version number in binary form.
Definition: base.h:326
std::uint64_t bst_idx_t
Type for data row index (sample).
Definition: base.h:107
std::int32_t bst_tree_t
Type for indexing trees.
Definition: base.h:127
std::uint32_t bst_target_t
Type for indexing into output targets.
Definition: base.h:119
std::int16_t bst_d_ordinal_t
Ordinal of a CUDA device.
Definition: base.h:131
std::uint32_t bst_uint
unsigned integer type used for feature index.
Definition: base.h:91
std::int32_t bst_layer_t
Type for indexing boosted layers.
Definition: base.h:123
std::uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:99
std::uint64_t bst_ulong
unsigned long integers
Definition: base.h:93
float bst_float
float type, used for storing statistics
Definition: base.h:95
std::int32_t bst_cat_t
Categorical value type.
Definition: base.h:97
constexpr bst_float kRtEps
small eps gap for minimum split decision.
Definition: base.h:319