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