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 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
40 #define XGBOOST_ALIGNAS(X) alignas(X)
41 #else
42 #define XGBOOST_ALIGNAS(X)
43 #endif // defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
44 
45 #if defined(__GNUC__)
46 #define XGBOOST_EXPECT(cond, ret) __builtin_expect((cond), (ret))
47 #else
48 #define XGBOOST_EXPECT(cond, ret) (cond)
49 #endif // defined(__GNUC__)
50 
54 #if defined(__CUDA__) || defined(__NVCC__)
55 #define XGBOOST_DEVICE __host__ __device__
56 #else
57 #define XGBOOST_DEVICE
58 #endif // defined (__CUDA__) || defined(__NVCC__)
59 
60 #if defined(__CUDA__) || defined(__CUDACC__)
61 #define XGBOOST_HOST_DEV_INLINE XGBOOST_DEVICE __forceinline__
62 #define XGBOOST_DEV_INLINE __device__ __forceinline__
63 #else
64 #define XGBOOST_HOST_DEV_INLINE
65 #define XGBOOST_DEV_INLINE
66 #endif // defined(__CUDA__) || defined(__CUDACC__)
67 
68 // restrict
69 #if defined(_MSC_VER)
70 #define XGBOOST_RESTRICT __restrict
71 #else
72 #define XGBOOST_RESTRICT __restrict__
73 #endif
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  GradientPairInternal() = default;
156 
158  SetGrad(grad);
159  SetHess(hess);
160  }
161 
162  // Copy constructor if of same value type, marked as default to be trivially_copyable
167 
168  // Copy constructor if different value type - use getters and setters to
169  // perform conversion
170  template <typename T2>
172  SetGrad(g.GetGrad());
173  SetHess(g.GetHess());
174  }
175 
176  XGBOOST_DEVICE T GetGrad() const { return grad_; }
177  XGBOOST_DEVICE T GetHess() const { return hess_; }
178 
180  grad_ += rhs.grad_;
181  hess_ += rhs.hess_;
182  return *this;
183  }
184 
187  g.grad_ = grad_ + rhs.grad_;
188  g.hess_ = hess_ + rhs.hess_;
189  return g;
190  }
191 
193  grad_ -= rhs.grad_;
194  hess_ -= rhs.hess_;
195  return *this;
196  }
197 
200  g.grad_ = grad_ - rhs.grad_;
201  g.hess_ = hess_ - rhs.hess_;
202  return g;
203  }
204 
206  grad_ *= multiplier;
207  hess_ *= multiplier;
208  return *this;
209  }
210 
213  g.grad_ = grad_ * multiplier;
214  g.hess_ = hess_ * multiplier;
215  return g;
216  }
217 
219  grad_ /= divisor;
220  hess_ /= divisor;
221  return *this;
222  }
223 
226  g.grad_ = grad_ / divisor;
227  g.hess_ = hess_ / divisor;
228  return g;
229  }
230 
232  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
233  }
234 
235  XGBOOST_DEVICE explicit GradientPairInternal(int value) {
236  *this = GradientPairInternal<T>(static_cast<float>(value), static_cast<float>(value));
237  }
238 
239  friend std::ostream &operator<<(std::ostream &os, const GradientPairInternal<T> &g) {
240  os << g.GetGrad() << "/" << g.GetHess();
241  return os;
242  }
243 };
244 } // namespace detail
245 
250 
254  using T = int64_t;
255  T grad_ = 0;
256  T hess_ = 0;
257 
258  public:
259  using ValueT = T;
260 
261  XGBOOST_DEVICE GradientPairInt64(T grad, T hess) : grad_(grad), hess_(hess) {}
262  GradientPairInt64() = default;
263 
264  // Copy constructor if of same value type, marked as default to be trivially_copyable
267 
268  [[nodiscard]] XGBOOST_DEVICE T GetQuantisedGrad() const { return grad_; }
269  [[nodiscard]] XGBOOST_DEVICE T GetQuantisedHess() const { return hess_; }
270 
272  grad_ += rhs.grad_;
273  hess_ += rhs.hess_;
274  return *this;
275  }
276 
279  g.grad_ = grad_ + rhs.grad_;
280  g.hess_ = hess_ + rhs.hess_;
281  return g;
282  }
283 
285  grad_ -= rhs.grad_;
286  hess_ -= rhs.hess_;
287  return *this;
288  }
289 
292  g.grad_ = grad_ - rhs.grad_;
293  g.hess_ = hess_ - rhs.hess_;
294  return g;
295  }
296 
297  XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const {
298  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
299  }
300  friend std::ostream &operator<<(std::ostream &os, const GradientPairInt64 &g) {
301  os << g.GetQuantisedGrad() << "/" << g.GetQuantisedHess();
302  return os;
303  }
304 };
305 
306 using Args = std::vector<std::pair<std::string, std::string> >;
307 
309 constexpr inline float kRtEps = 1e-6f;
310 
312 using omp_ulong = dmlc::omp_ulong; // NOLINT
314 using bst_omp_uint = dmlc::omp_uint; // NOLINT
316 using XGBoostVersionT = std::int32_t;
317 } // namespace xgboost
318 
319 #endif // XGBOOST_BASE_H_
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:57
Fixed point representation for high precision gradient pair. Has a different interface so we don't ac...
Definition: base.h:253
T ValueT
Definition: base.h:259
XGBOOST_DEVICE GradientPairInt64 operator+(const GradientPairInt64 &rhs) const
Definition: base.h:277
XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const
Definition: base.h:297
XGBOOST_DEVICE T GetQuantisedHess() const
Definition: base.h:269
GradientPairInt64(GradientPairInt64 const &g)=default
GradientPairInt64 & operator=(GradientPairInt64 const &g)=default
XGBOOST_DEVICE GradientPairInt64 & operator-=(const GradientPairInt64 &rhs)
Definition: base.h:284
friend std::ostream & operator<<(std::ostream &os, const GradientPairInt64 &g)
Definition: base.h:300
XGBOOST_DEVICE GradientPairInt64 & operator+=(const GradientPairInt64 &rhs)
Definition: base.h:271
XGBOOST_DEVICE T GetQuantisedGrad() const
Definition: base.h:268
XGBOOST_DEVICE GradientPairInt64(T grad, T hess)
Definition: base.h:261
XGBOOST_DEVICE GradientPairInt64 operator-(const GradientPairInt64 &rhs) const
Definition: base.h:290
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:211
XGBOOST_DEVICE GradientPairInternal< T > & operator+=(const GradientPairInternal< T > &rhs)
Definition: base.h:179
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:239
XGBOOST_DEVICE GradientPairInternal(T grad, T hess)
Definition: base.h:157
XGBOOST_DEVICE GradientPairInternal< T > & operator*=(float multiplier)
Definition: base.h:205
XGBOOST_DEVICE GradientPairInternal(int value)
Definition: base.h:235
XGBOOST_DEVICE GradientPairInternal< T > operator+(const GradientPairInternal< T > &rhs) const
Definition: base.h:185
XGBOOST_DEVICE GradientPairInternal< T > & operator/=(float divisor)
Definition: base.h:218
XGBOOST_DEVICE GradientPairInternal< T > operator/(float divisor) const
Definition: base.h:224
XGBOOST_DEVICE T GetHess() const
Definition: base.h:177
XGBOOST_DEVICE GradientPairInternal< T > & operator-=(const GradientPairInternal< T > &rhs)
Definition: base.h:192
GradientPairInternal & operator=(GradientPairInternal &&that)=default
XGBOOST_DEVICE GradientPairInternal< T > operator-(const GradientPairInternal< T > &rhs) const
Definition: base.h:198
GradientPairInternal(GradientPairInternal const &g)=default
XGBOOST_DEVICE T GetGrad() const
Definition: base.h:176
GradientPairInternal & operator=(GradientPairInternal const &that)=default
XGBOOST_DEVICE GradientPairInternal(const GradientPairInternal< T2 > &g)
Definition: base.h:171
XGBOOST_DEVICE bool operator==(const GradientPairInternal< T > &rhs) const
Definition: base.h:231
Learner interface that integrates objective, gbm and evaluation together. This is the user facing XGB...
Definition: base.h:89
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:306
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 and tree depth.
Definition: base.h:111
dmlc::omp_ulong omp_ulong
define unsigned long for openmp loop
Definition: base.h:312
dmlc::omp_uint bst_omp_uint
define unsigned int for openmp loop
Definition: base.h:314
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:316
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
constexpr float kRtEps
small eps gap for minimum split decision.
Definition: base.h:309
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