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/base.h>
10 #include <dmlc/omp.h>
11 
12 #include <cmath>
13 #include <cstdint>
14 #include <iostream>
15 #include <string>
16 #include <utility>
17 #include <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 XGBOOST_STRICT_R_MODE
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 
90 namespace xgboost {
91 
93 using bst_uint = uint32_t; // NOLINT
95 using bst_ulong = uint64_t; // NOLINT
97 using bst_float = float; // NOLINT
99 using bst_cat_t = int32_t; // NOLINT
101 using bst_feature_t = uint32_t; // NOLINT
103 using bst_bin_t = int32_t; // NOLINT
110 using bst_row_t = std::size_t; // NOLINT
112 using bst_node_t = std::int32_t; // NOLINT
114 using bst_group_t = std::uint32_t; // NOLINT
118 using bst_target_t = std::uint32_t; // NOLINT
122 using bst_layer_t = std::int32_t; // NOLINT
126 using bst_tree_t = std::int32_t; // NOLINT
130 using bst_d_ordinal_t = std::int16_t; // NOLINT
131 
132 namespace detail {
136 template <typename T>
139  T grad_{0};
141  T hess_{0};
142 
143  XGBOOST_DEVICE void SetGrad(T g) { grad_ = g; }
144  XGBOOST_DEVICE void SetHess(T h) { hess_ = h; }
145 
146  public:
147  using ValueT = T;
148 
149  inline void Add(const ValueT& grad, const ValueT& hess) {
150  grad_ += grad;
151  hess_ += hess;
152  }
153 
154  inline static void Reduce(GradientPairInternal<T>& a, const GradientPairInternal<T>& b) { // NOLINT(*)
155  a += b;
156  }
157 
158  GradientPairInternal() = default;
159 
161  SetGrad(grad);
162  SetHess(hess);
163  }
164 
165  // Copy constructor if of same value type, marked as default to be trivially_copyable
170 
171  // Copy constructor if different value type - use getters and setters to
172  // perform conversion
173  template <typename T2>
175  SetGrad(g.GetGrad());
176  SetHess(g.GetHess());
177  }
178 
179  XGBOOST_DEVICE T GetGrad() const { return grad_; }
180  XGBOOST_DEVICE T GetHess() const { return hess_; }
181 
183  const GradientPairInternal<T> &rhs) {
184  grad_ += rhs.grad_;
185  hess_ += rhs.hess_;
186  return *this;
187  }
188 
190  const GradientPairInternal<T> &rhs) const {
192  g.grad_ = grad_ + rhs.grad_;
193  g.hess_ = hess_ + rhs.hess_;
194  return g;
195  }
196 
198  const GradientPairInternal<T> &rhs) {
199  grad_ -= rhs.grad_;
200  hess_ -= rhs.hess_;
201  return *this;
202  }
203 
205  const GradientPairInternal<T> &rhs) const {
207  g.grad_ = grad_ - rhs.grad_;
208  g.hess_ = hess_ - rhs.hess_;
209  return g;
210  }
211 
213  grad_ *= multiplier;
214  hess_ *= multiplier;
215  return *this;
216  }
217 
220  g.grad_ = grad_ * multiplier;
221  g.hess_ = hess_ * multiplier;
222  return g;
223  }
224 
226  grad_ /= divisor;
227  hess_ /= divisor;
228  return *this;
229  }
230 
233  g.grad_ = grad_ / divisor;
234  g.hess_ = hess_ / divisor;
235  return g;
236  }
237 
239  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
240  }
241 
242  XGBOOST_DEVICE explicit GradientPairInternal(int value) {
243  *this = GradientPairInternal<T>(static_cast<float>(value),
244  static_cast<float>(value));
245  }
246 
247  friend std::ostream &operator<<(std::ostream &os,
248  const GradientPairInternal<T> &g) {
249  os << g.GetGrad() << "/" << g.GetHess();
250  return os;
251  }
252 };
253 } // namespace detail
254 
259 
263  using T = int64_t;
264  T grad_ = 0;
265  T hess_ = 0;
266 
267  public:
268  using ValueT = T;
269 
270  XGBOOST_DEVICE GradientPairInt64(T grad, T hess) : grad_(grad), hess_(hess) {}
271  GradientPairInt64() = default;
272 
273  // Copy constructor if of same value type, marked as default to be trivially_copyable
276 
277  XGBOOST_DEVICE [[nodiscard]] T GetQuantisedGrad() const { return grad_; }
278  XGBOOST_DEVICE [[nodiscard]] T GetQuantisedHess() const { return hess_; }
279 
281  grad_ += rhs.grad_;
282  hess_ += rhs.hess_;
283  return *this;
284  }
285 
288  g.grad_ = grad_ + rhs.grad_;
289  g.hess_ = hess_ + rhs.hess_;
290  return g;
291  }
292 
294  grad_ -= rhs.grad_;
295  hess_ -= rhs.hess_;
296  return *this;
297  }
298 
301  g.grad_ = grad_ - rhs.grad_;
302  g.hess_ = hess_ - rhs.hess_;
303  return g;
304  }
305 
306  XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const {
307  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
308  }
309  friend std::ostream &operator<<(std::ostream &os,
310  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 = 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:262
T ValueT
Definition: base.h:268
XGBOOST_DEVICE GradientPairInt64 operator+(const GradientPairInt64 &rhs) const
Definition: base.h:286
XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const
Definition: base.h:306
XGBOOST_DEVICE T GetQuantisedHess() const
Definition: base.h:278
GradientPairInt64(GradientPairInt64 const &g)=default
GradientPairInt64 & operator=(GradientPairInt64 const &g)=default
XGBOOST_DEVICE GradientPairInt64 & operator-=(const GradientPairInt64 &rhs)
Definition: base.h:293
friend std::ostream & operator<<(std::ostream &os, const GradientPairInt64 &g)
Definition: base.h:309
XGBOOST_DEVICE GradientPairInt64 & operator+=(const GradientPairInt64 &rhs)
Definition: base.h:280
XGBOOST_DEVICE T GetQuantisedGrad() const
Definition: base.h:277
XGBOOST_DEVICE GradientPairInt64(T grad, T hess)
Definition: base.h:270
XGBOOST_DEVICE GradientPairInt64 operator-(const GradientPairInt64 &rhs) const
Definition: base.h:299
Implementation of gradient statistics pair. Template specialisation may be used to overload different...
Definition: base.h:137
GradientPairInternal(GradientPairInternal &&g)=default
XGBOOST_DEVICE GradientPairInternal< T > operator*(float multiplier) const
Definition: base.h:218
XGBOOST_DEVICE GradientPairInternal< T > & operator+=(const GradientPairInternal< T > &rhs)
Definition: base.h:182
void Add(const ValueT &grad, const ValueT &hess)
Definition: base.h:149
T ValueT
Definition: base.h:147
friend std::ostream & operator<<(std::ostream &os, const GradientPairInternal< T > &g)
Definition: base.h:247
XGBOOST_DEVICE GradientPairInternal(T grad, T hess)
Definition: base.h:160
XGBOOST_DEVICE GradientPairInternal< T > & operator*=(float multiplier)
Definition: base.h:212
XGBOOST_DEVICE GradientPairInternal(int value)
Definition: base.h:242
XGBOOST_DEVICE GradientPairInternal< T > operator+(const GradientPairInternal< T > &rhs) const
Definition: base.h:189
XGBOOST_DEVICE GradientPairInternal< T > & operator/=(float divisor)
Definition: base.h:225
XGBOOST_DEVICE GradientPairInternal< T > operator/(float divisor) const
Definition: base.h:231
XGBOOST_DEVICE T GetHess() const
Definition: base.h:180
XGBOOST_DEVICE GradientPairInternal< T > & operator-=(const GradientPairInternal< T > &rhs)
Definition: base.h:197
GradientPairInternal & operator=(GradientPairInternal &&that)=default
XGBOOST_DEVICE GradientPairInternal< T > operator-(const GradientPairInternal< T > &rhs) const
Definition: base.h:204
static void Reduce(GradientPairInternal< T > &a, const GradientPairInternal< T > &b)
Definition: base.h:154
GradientPairInternal(GradientPairInternal const &g)=default
XGBOOST_DEVICE T GetGrad() const
Definition: base.h:179
GradientPairInternal & operator=(GradientPairInternal const &that)=default
XGBOOST_DEVICE GradientPairInternal(const GradientPairInternal< T2 > &g)
Definition: base.h:174
XGBOOST_DEVICE bool operator==(const GradientPairInternal< T > &rhs) const
Definition: base.h:238
namespace of xgboost
Definition: base.h:90
int32_t XGBoostVersionT
Type used for representing version number in binary form.
Definition: base.h:326
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:316
uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:101
std::uint32_t bst_group_t
Type for ranking group index.
Definition: base.h:114
std::int32_t bst_node_t
Type for tree node index.
Definition: base.h:112
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_tree_t
Type for indexing trees.
Definition: base.h:126
std::uint32_t bst_target_t
Type for indexing into output targets.
Definition: base.h:118
std::int16_t bst_d_ordinal_t
Ordinal of a CUDA device.
Definition: base.h:130
uint64_t bst_ulong
unsigned long integers
Definition: base.h:95
std::size_t bst_row_t
Type for data row index.
Definition: base.h:110
uint32_t bst_uint
unsigned integer type used for feature index.
Definition: base.h:93
int32_t bst_cat_t
Categorical value type.
Definition: base.h:99
std::int32_t bst_layer_t
Type for indexing boosted layers.
Definition: base.h:122
int32_t bst_bin_t
Type for histogram bin index.
Definition: base.h:103
float bst_float
float type, used for storing statistics
Definition: base.h:97
constexpr bst_float kRtEps
small eps gap for minimum split decision.
Definition: base.h:319