Medial Code Documentation
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1
7#ifndef XGBOOST_COMMON_MATH_H_
8#define XGBOOST_COMMON_MATH_H_
9
10#include <xgboost/base.h> // for XGBOOST_DEVICE
11
12#include <algorithm> // for max
13#include <cmath> // for exp, abs, log, lgamma
14#include <limits> // for numeric_limits
15#include <type_traits> // for is_floating_point, conditional, is_signed, is_same, declval, enable_if
16#include <utility> // for pair
17
18namespace xgboost {
19namespace common {
20
21template <typename T> XGBOOST_DEVICE T Sqr(T const &w) { return w * w; }
22
28XGBOOST_DEVICE inline float Sigmoid(float x) {
29 float constexpr kEps = 1e-16; // avoid 0 div
30 x = std::min(-x, 88.7f); // avoid exp overflow
31 auto denom = expf(x) + 1.0f + kEps;
32 auto y = 1.0f / denom;
33 return y;
34}
35
36XGBOOST_DEVICE inline double Sigmoid(double x) {
37 auto denom = std::exp(-x) + 1.0;
38 auto y = 1.0 / denom;
39 return y;
40}
44template <typename T, typename U>
45XGBOOST_DEVICE constexpr bool CloseTo(T a, U b) {
46 using Casted =
47 typename std::conditional<
48 std::is_floating_point<T>::value || std::is_floating_point<U>::value,
49 double,
50 typename std::conditional<
51 std::is_signed<T>::value || std::is_signed<U>::value,
52 int64_t,
53 uint64_t>::type>::type;
54 return std::is_floating_point<Casted>::value ?
55 std::abs(static_cast<Casted>(a) -static_cast<Casted>(b)) < 1e-6 : a == b;
56}
57
66template <typename Iterator>
67XGBOOST_DEVICE inline void Softmax(Iterator start, Iterator end) {
68 static_assert(std::is_same<bst_float,
69 typename std::remove_reference<
70 decltype(std::declval<Iterator>().operator*())>::type
71 >::value,
72 "Values should be of type bst_float");
73 bst_float wmax = *start;
74 for (Iterator i = start+1; i != end; ++i) {
75 wmax = fmaxf(*i, wmax);
76 }
77 double wsum = 0.0f;
78 for (Iterator i = start; i != end; ++i) {
79 *i = expf(*i - wmax);
80 wsum += *i;
81 }
82 for (Iterator i = start; i != end; ++i) {
83 *i /= static_cast<float>(wsum);
84 }
85}
86
94template<typename Iterator>
95XGBOOST_DEVICE inline Iterator FindMaxIndex(Iterator begin, Iterator end) {
96 Iterator maxit = begin;
97 for (Iterator it = begin; it != end; ++it) {
98 if (*it > *maxit) maxit = it;
99 }
100 return maxit;
101}
102
109inline float LogSum(float x, float y) {
110 if (x < y) {
111 return y + std::log(std::exp(x - y) + 1.0f);
112 } else {
113 return x + std::log(std::exp(y - x) + 1.0f);
114 }
115}
116
124template<typename Iterator>
125inline float LogSum(Iterator begin, Iterator end) {
126 float mx = *begin;
127 for (Iterator it = begin; it != end; ++it) {
128 mx = std::max(mx, *it);
129 }
130 float sum = 0.0f;
131 for (Iterator it = begin; it != end; ++it) {
132 sum += std::exp(*it - mx);
133 }
134 return mx + std::log(sum);
135}
136
137// Redefined here to workaround a VC bug that doesn't support overloading for integer
138// types.
139template <typename T>
140XGBOOST_DEVICE typename std::enable_if<
141 std::numeric_limits<T>::is_integer, bool>::type
142CheckNAN(T) {
143 return false;
144}
145
146#if XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__)
147
148bool CheckNAN(double v);
149
150#else
151
152XGBOOST_DEVICE bool inline CheckNAN(float x) {
153#if defined(__CUDA_ARCH__)
154 return isnan(x);
155#else
156 return std::isnan(x);
157#endif // defined(__CUDA_ARCH__)
158}
159
160XGBOOST_DEVICE bool inline CheckNAN(double x) {
161#if defined(__CUDA_ARCH__)
162 return isnan(x);
163#else
164 return std::isnan(x);
165#endif // defined(__CUDA_ARCH__)
166}
167
168#endif // XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__)
169// GPU version is not uploaded in CRAN anyway.
170// Specialize only when using R with CPU.
171#if XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
172double LogGamma(double v);
173
174#else // Not R or R with GPU.
175
176template<typename T>
177XGBOOST_DEVICE inline T LogGamma(T v) {
178#ifdef _MSC_VER
179
180#if _MSC_VER >= 1800
181 return lgamma(v);
182#else
183#pragma message("Warning: lgamma function was not available until VS2013"\
184 ", poisson regression will be disabled")
185 utils::Error("lgamma function was not available until VS2013");
186 return static_cast<T>(1.0);
187#endif // _MSC_VER >= 1800
188
189#else
190 return lgamma(v);
191#endif // _MSC_VER
192}
193
194#endif // XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
195
196} // namespace common
197} // namespace xgboost
198#endif // XGBOOST_COMMON_MATH_H_
Copyright 2015-2023 by XGBoost Contributors.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition base.h:64
XGBOOST_DEVICE float Sigmoid(float x)
calculate the sigmoid of the input.
Definition math.h:28
XGBOOST_DEVICE void Softmax(Iterator start, Iterator end)
Do inplace softmax transformaton on start to end.
Definition math.h:67
XGBOOST_DEVICE constexpr bool CloseTo(T a, U b)
Equality test for both integer and floating point.
Definition math.h:45
float LogSum(float x, float y)
perform numerically safe logsum
Definition math.h:109
XGBOOST_DEVICE Iterator FindMaxIndex(Iterator begin, Iterator end)
Find the maximum iterator within the iterators.
Definition math.h:95
namespace of xgboost
Definition base.h:90
float bst_float
float type, used for storing statistics
Definition base.h:97