Skip to content

Commit

Permalink
Fix build on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
hcho3 committed May 27, 2020
1 parent 9d69166 commit 2e704ee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
49 changes: 29 additions & 20 deletions src/common/probability_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
#ifndef XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_
#define XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_

#ifndef XGBOOST_USE_CUDA
#include <cmath>
namespace {

using namespace std; // access math functions in host code without std:: prefix

} // anonymous namespace
#endif // XGBOOST_USE_CUDA

namespace xgboost {
namespace common {

namespace probability_constant {

/*! \brief Constant PI */
const double kPI = 3.14159265358979323846;
constexpr double kPI = 3.14159265358979323846;
/*! \brief The Euler-Mascheroni_constant */
const double kEulerMascheroni = 0.57721566490153286060651209008240243104215933593992;
constexpr double kEulerMascheroni = 0.57721566490153286060651209008240243104215933593992;

} // namespace probability_constant

Expand All @@ -28,12 +37,12 @@ enum class ProbabilityDistributionType : int {
struct NormalDistribution {
XGBOOST_DEVICE inline static
double PDF(double z) {
return std::exp(-z * z / 2.0) / std::sqrt(2.0 * probability_constant::kPI);
return exp(-z * z / 2.0) / sqrt(2.0 * probability_constant::kPI);
}

XGBOOST_DEVICE inline static
double CDF(double z) {
return 0.5 * (1 + std::erf(z / std::sqrt(2.0)));
return 0.5 * (1 + erf(z / sqrt(2.0)));
}

XGBOOST_DEVICE inline static
Expand All @@ -55,9 +64,9 @@ struct NormalDistribution {
struct LogisticDistribution {
XGBOOST_DEVICE inline static
double PDF(double z) {
const double w = std::exp(z);
const double w = exp(z);
const double sqrt_denominator = 1 + w;
if (std::isinf(w) || std::isinf(w * w)) {
if (isinf(w) || isinf(w * w)) {
return 0.0;
} else {
return w / (sqrt_denominator * sqrt_denominator);
Expand All @@ -66,20 +75,20 @@ struct LogisticDistribution {

XGBOOST_DEVICE inline static
double CDF(double z) {
const double w = std::exp(z);
return std::isinf(w) ? 1.0 : (w / (1 + w));
const double w = exp(z);
return isinf(w) ? 1.0 : (w / (1 + w));
}

XGBOOST_DEVICE inline static
double GradPDF(double z) {
const double w = std::exp(z);
return std::isinf(w) ? 0.0 : (PDF(z) * (1 - w) / (1 + w));
const double w = exp(z);
return isinf(w) ? 0.0 : (PDF(z) * (1 - w) / (1 + w));
}

XGBOOST_DEVICE inline static
double HessPDF(double z) {
const double w = std::exp(z);
if (std::isinf(w) || std::isinf(w * w)) {
const double w = exp(z);
if (isinf(w) || isinf(w * w)) {
return 0.0;
} else {
return PDF(z) * (w * w - 4 * w + 1) / ((1 + w) * (1 + w));
Expand All @@ -95,26 +104,26 @@ struct LogisticDistribution {
struct ExtremeDistribution {
XGBOOST_DEVICE inline static
double PDF(double z) {
const double w = std::exp(z);
return std::isinf(w) ? 0.0 : (w * std::exp(-w));
const double w = exp(z);
return isinf(w) ? 0.0 : (w * exp(-w));
}

XGBOOST_DEVICE inline static
double CDF(double z) {
const double w = std::exp(z);
return 1 - std::exp(-w);
const double w = exp(z);
return 1 - exp(-w);
}

XGBOOST_DEVICE inline static
double GradPDF(double z) {
const double w = std::exp(z);
return std::isinf(w) ? 0.0 : ((1 - w) * PDF(z));
const double w = exp(z);
return isinf(w) ? 0.0 : ((1 - w) * PDF(z));
}

XGBOOST_DEVICE inline static
double HessPDF(double z) {
const double w = std::exp(z);
if (std::isinf(w) || std::isinf(w * w)) {
const double w = exp(z);
if (isinf(w) || isinf(w * w)) {
return 0.0;
} else {
return (w * w - 3 * w + 1) * PDF(z);
Expand Down
56 changes: 28 additions & 28 deletions src/common/survival_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
#ifndef XGBOOST_COMMON_SURVIVAL_UTIL_H_
#define XGBOOST_COMMON_SURVIVAL_UTIL_H_

#ifndef XGBOOST_USE_CUDA
#include <cmath>
namespace {

using namespace std; // access math functions in host code without std:: prefix

} // anonymous namespace
#endif // XGBOOST_USE_CUDA

/*
- Formulas are motivated from document -
http://members.cbio.mines-paristech.fr/~thocking/survival.pdf
Expand Down Expand Up @@ -53,15 +62,6 @@ inline double Clip(double x, double x_min, double x_max) {
return x;
}

XGBOOST_DEVICE
inline double Max(double a, double b) {
#if defined (__CUDA__) || defined(__NVCC__)
return max(a, b);
#else
return std::max(a, b);
#endif
}

template<typename Distribution>
XGBOOST_DEVICE inline double
GetLimitGradAtInfPred(CensoringType censor_type, bool sign, double sigma);
Expand Down Expand Up @@ -98,19 +98,19 @@ template<typename Distribution>
struct AFTLoss {
XGBOOST_DEVICE inline static
double Loss(double y_lower, double y_upper, double y_pred, double sigma) {
const double log_y_lower = std::log(y_lower);
const double log_y_upper = std::log(y_upper);
const double log_y_lower = log(y_lower);
const double log_y_upper = log(y_upper);

double cost;

if (y_lower == y_upper) { // uncensored
const double z = (log_y_lower - y_pred) / sigma;
const double pdf = Distribution::PDF(z);
// Regularize the denominator with eps, to avoid INF or NAN
cost = -std::log(aft::Max(pdf / (sigma * y_lower), aft::kEps));
cost = -log(fmax(pdf / (sigma * y_lower), aft::kEps));
} else { // censored; now check what type of censorship we have
double z_u, z_l, cdf_u, cdf_l;
if (std::isinf(y_upper)) { // right-censored
if (isinf(y_upper)) { // right-censored
cdf_u = 1;
} else { // left-censored or interval-censored
z_u = (log_y_upper - y_pred) / sigma;
Expand All @@ -123,16 +123,16 @@ struct AFTLoss {
cdf_l = Distribution::CDF(z_l);
}
// Regularize the denominator with eps, to avoid INF or NAN
cost = -std::log(aft::Max(cdf_u - cdf_l, aft::kEps));
cost = -log(fmax(cdf_u - cdf_l, aft::kEps));
}

return cost;
}

XGBOOST_DEVICE inline static
double Gradient(double y_lower, double y_upper, double y_pred, double sigma) {
const double log_y_lower = std::log(y_lower);
const double log_y_upper = std::log(y_upper);
const double log_y_lower = log(y_lower);
const double log_y_upper = log(y_upper);
double numerator, denominator, gradient; // numerator and denominator of gradient
CensoringType censor_type;
bool z_sign; // sign of z-score
Expand All @@ -148,7 +148,7 @@ struct AFTLoss {
} else { // censored; now check what type of censorship we have
double z_u = 0.0, z_l = 0.0, pdf_u, pdf_l, cdf_u, cdf_l;
censor_type = CensoringType::kIntervalCensored;
if (std::isinf(y_upper)) { // right-censored
if (isinf(y_upper)) { // right-censored
pdf_u = 0;
cdf_u = 1;
censor_type = CensoringType::kRightCensored;
Expand All @@ -171,7 +171,7 @@ struct AFTLoss {
denominator = sigma * (cdf_u - cdf_l);
}
gradient = numerator / denominator;
if (denominator < aft::kEps && (std::isnan(gradient) || std::isinf(gradient))) {
if (denominator < aft::kEps && (isnan(gradient) || isinf(gradient))) {
gradient = aft::GetLimitGradAtInfPred<Distribution>(censor_type, z_sign, sigma);
}

Expand All @@ -180,8 +180,8 @@ struct AFTLoss {

XGBOOST_DEVICE inline static
double Hessian(double y_lower, double y_upper, double y_pred, double sigma) {
const double log_y_lower = std::log(y_lower);
const double log_y_upper = std::log(y_upper);
const double log_y_lower = log(y_lower);
const double log_y_upper = log(y_upper);
double numerator, denominator, hessian; // numerator and denominator of hessian
CensoringType censor_type;
bool z_sign; // sign of z-score
Expand All @@ -198,7 +198,7 @@ struct AFTLoss {
} else { // censored; now check what type of censorship we have
double z_u = 0.0, z_l = 0.0, grad_pdf_u, grad_pdf_l, pdf_u, pdf_l, cdf_u, cdf_l;
censor_type = CensoringType::kIntervalCensored;
if (std::isinf(y_upper)) { // right-censored
if (isinf(y_upper)) { // right-censored
pdf_u = 0;
cdf_u = 1;
grad_pdf_u = 0;
Expand Down Expand Up @@ -229,7 +229,7 @@ struct AFTLoss {
denominator = sqrt_denominator * sqrt_denominator;
}
hessian = numerator / denominator;
if (denominator < aft::kEps && (std::isnan(hessian) || std::isinf(hessian))) {
if (denominator < aft::kEps && (isnan(hessian) || isinf(hessian))) {
hessian = aft::GetLimitHessAtInfPred<Distribution>(censor_type, z_sign, sigma);
}

Expand All @@ -252,7 +252,7 @@ GetLimitGradAtInfPred<NormalDistribution>(CensoringType censor_type, bool sign,
case CensoringType::kIntervalCensored:
return sign ? kMinGradient : kMaxGradient;
}
return std::nan("");
return nan("");
}

template <>
Expand All @@ -268,7 +268,7 @@ GetLimitHessAtInfPred<NormalDistribution>(CensoringType censor_type, bool sign,
case CensoringType::kIntervalCensored:
return 1.0 / (sigma * sigma);
}
return std::nan("");
return nan("");
}

template <>
Expand All @@ -284,7 +284,7 @@ GetLimitGradAtInfPred<LogisticDistribution>(CensoringType censor_type, bool sign
case CensoringType::kIntervalCensored:
return sign ? (-1.0 / sigma) : (1.0 / sigma);
}
return std::nan("");
return nan("");
}

template <>
Expand All @@ -297,7 +297,7 @@ GetLimitHessAtInfPred<LogisticDistribution>(CensoringType censor_type, bool sign
case CensoringType::kIntervalCensored:
return kMinHessian;
}
return std::nan("");
return nan("");
}

template <>
Expand All @@ -313,7 +313,7 @@ GetLimitGradAtInfPred<ExtremeDistribution>(CensoringType censor_type, bool sign,
case CensoringType::kIntervalCensored:
return sign ? kMinGradient : (1.0 / sigma);
}
return std::nan("");
return nan("");
}

template <>
Expand All @@ -328,7 +328,7 @@ GetLimitHessAtInfPred<ExtremeDistribution>(CensoringType censor_type, bool sign,
case CensoringType::kIntervalCensored:
return sign ? kMaxHessian : kMinHessian;
}
return std::nan("");
return nan("");
}

} // namespace aft
Expand Down

0 comments on commit 2e704ee

Please sign in to comment.