diff --git a/.gitattributes b/.gitattributes index 5c71e130ed17..a22b3664e535 100644 --- a/.gitattributes +++ b/.gitattributes @@ -15,4 +15,4 @@ *.rst text eol=lf *.md text eol=lf -*.csv text eol=lf \ No newline at end of file +*.csv text eol=lf diff --git a/.gitignore b/.gitignore index 6dfb1212e135..f6bf2b8935df 100644 --- a/.gitignore +++ b/.gitignore @@ -165,4 +165,4 @@ Rplots.pdf # nsys *.nsys-rep -rmm_log.dev* \ No newline at end of file +rmm_log.dev* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2311d0c1258d..0200b8366b9f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,9 +27,9 @@ repos: language: python types: [file] files: \.(cc|c|cpp|h|cu|hpp)$ - exclude: (dmlc-core|gputreeshap) + exclude: (dmlc-core|gputreeshap|demo\/c-api|jvm-packages\/xgboost4j\/src\/native\/xgboost4j\..*) additional_dependencies: - - cpplint==1.6.1 + - cpplint - id: lint-cmake name: CMake lint (cmakelint) entry: cmakelint --linelength=120 --filter=-convention/filename,-package/stdargs,-readability/wonkycase diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 926ad43fa6f0..b1f076cb3a8b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -2,9 +2,9 @@ Contributors of DMLC/XGBoost ============================ XGBoost has been developed and used by a group of active community. Everyone is more than welcomed to is a great way to make the project better and more accessible to more users. -Project Management Committee(PMC) +Project Management Committee(PMC) ---------- -The Project Management Committee(PMC) consists group of active committers that moderate the discussion, manage the project release, and proposes new committer/PMC members. +The Project Management Committee(PMC) consists group of active committers that moderate the discussion, manage the project release, and proposes new committer/PMC members. * [Tianqi Chen](https://github.com/tqchen), University of Washington - Tianqi is a Ph.D. student working on large-scale machine learning. He is the creator of the project. @@ -19,7 +19,7 @@ The Project Management Committee(PMC) consists group of active committers that m * [Hyunsu Cho](http://hyunsu-cho.io/), NVIDIA - Hyunsu is the maintainer of the XGBoost Python package. He also manages the Jenkins continuous integration system (https://xgboost-ci.net/). He is the initial author of the CPU 'hist' updater. * [Rory Mitchell](https://github.com/RAMitchell), University of Waikato - - Rory is a Ph.D. student at University of Waikato. He is the original creator of the GPU training algorithms. He improved the CMake build system and continuous integration. + - Rory is a Ph.D. student at University of Waikato. He is the original creator of the GPU training algorithms. He improved the CMake build system and continuous integration. * [Hongliang Liu](https://github.com/phunterlau) diff --git a/R-package/configure b/R-package/configure index b1d727c6a869..65165ba65cd6 100755 --- a/R-package/configure +++ b/R-package/configure @@ -4622,5 +4622,3 @@ if test -n "$ac_unrecognized_opts" && test "$enable_option_checking" != no; then { printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: unrecognized options: $ac_unrecognized_opts" >&5 printf "%s\n" "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;} fi - - diff --git a/R-package/src/init.c b/R-package/src/init.c index ecff3e71da1c..27463d27e657 100644 --- a/R-package/src/init.c +++ b/R-package/src/init.c @@ -5,10 +5,10 @@ * and edited to conform to xgboost C linter requirements. For details, see * https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-native-routines */ -#include -#include #include #include +#include +#include /* FIXME: Check these declarations against the C/Fortran source code. @@ -80,75 +80,74 @@ extern SEXP XGBoosterSlice_R(SEXP, SEXP, SEXP, SEXP); extern SEXP XGBoosterSliceAndReplace_R(SEXP, SEXP, SEXP, SEXP); static const R_CallMethodDef CallEntries[] = { - {"XGDuplicate_R", (DL_FUNC) &XGDuplicate_R, 1}, - {"XGPointerEqComparison_R", (DL_FUNC) &XGPointerEqComparison_R, 2}, - {"XGBoosterTrainOneIter_R", (DL_FUNC) &XGBoosterTrainOneIter_R, 5}, - {"XGBoosterCreate_R", (DL_FUNC) &XGBoosterCreate_R, 1}, - {"XGBoosterReset_R", (DL_FUNC) &XGBoosterReset_R, 1}, - {"XGBoosterCopyInfoFromDMatrix_R", (DL_FUNC) &XGBoosterCopyInfoFromDMatrix_R, 2}, - {"XGBoosterSetStrFeatureInfo_R",(DL_FUNC) &XGBoosterSetStrFeatureInfo_R,3}, // NOLINT - {"XGBoosterGetStrFeatureInfo_R",(DL_FUNC) &XGBoosterGetStrFeatureInfo_R,2}, // NOLINT - {"XGBoosterBoostedRounds_R", (DL_FUNC) &XGBoosterBoostedRounds_R, 1}, - {"XGBoosterGetNumFeature_R", (DL_FUNC) &XGBoosterGetNumFeature_R, 1}, - {"XGBoosterDumpModel_R", (DL_FUNC) &XGBoosterDumpModel_R, 4}, - {"XGBoosterEvalOneIter_R", (DL_FUNC) &XGBoosterEvalOneIter_R, 4}, - {"XGBoosterGetAttrNames_R", (DL_FUNC) &XGBoosterGetAttrNames_R, 1}, - {"XGBoosterGetAttr_R", (DL_FUNC) &XGBoosterGetAttr_R, 2}, - {"XGBoosterLoadModelFromRaw_R", (DL_FUNC) &XGBoosterLoadModelFromRaw_R, 2}, - {"XGBoosterSaveModelToRaw_R", (DL_FUNC) &XGBoosterSaveModelToRaw_R, 2}, - {"XGBoosterLoadModel_R", (DL_FUNC) &XGBoosterLoadModel_R, 2}, - {"XGBoosterSaveJsonConfig_R", (DL_FUNC) &XGBoosterSaveJsonConfig_R, 1}, - {"XGBoosterLoadJsonConfig_R", (DL_FUNC) &XGBoosterLoadJsonConfig_R, 2}, - {"XGBoosterSerializeToBuffer_R", (DL_FUNC) &XGBoosterSerializeToBuffer_R, 1}, - {"XGBoosterUnserializeFromBuffer_R", (DL_FUNC) &XGBoosterUnserializeFromBuffer_R, 2}, - {"XGBoosterPredictFromDMatrix_R", (DL_FUNC) &XGBoosterPredictFromDMatrix_R, 3}, - {"XGBoosterPredictFromDense_R", (DL_FUNC) &XGBoosterPredictFromDense_R, 5}, - {"XGBoosterPredictFromCSR_R", (DL_FUNC) &XGBoosterPredictFromCSR_R, 5}, - {"XGBoosterPredictFromColumnar_R", (DL_FUNC) &XGBoosterPredictFromColumnar_R, 5}, - {"XGBoosterSaveModel_R", (DL_FUNC) &XGBoosterSaveModel_R, 2}, - {"XGBoosterSetAttr_R", (DL_FUNC) &XGBoosterSetAttr_R, 3}, - {"XGBoosterSetParam_R", (DL_FUNC) &XGBoosterSetParam_R, 3}, - {"XGBoosterUpdateOneIter_R", (DL_FUNC) &XGBoosterUpdateOneIter_R, 3}, - {"XGCheckNullPtr_R", (DL_FUNC) &XGCheckNullPtr_R, 1}, - {"XGSetArrayDimNamesInplace_R", (DL_FUNC) &XGSetArrayDimNamesInplace_R, 2}, - {"XGSetVectorNamesInplace_R", (DL_FUNC) &XGSetVectorNamesInplace_R, 2}, - {"XGDMatrixCreateFromCSC_R", (DL_FUNC) &XGDMatrixCreateFromCSC_R, 6}, - {"XGDMatrixCreateFromCSR_R", (DL_FUNC) &XGDMatrixCreateFromCSR_R, 6}, - {"XGDMatrixCreateFromURI_R", (DL_FUNC) &XGDMatrixCreateFromURI_R, 3}, - {"XGDMatrixCreateFromMat_R", (DL_FUNC) &XGDMatrixCreateFromMat_R, 3}, - {"XGDMatrixGetFloatInfo_R", (DL_FUNC) &XGDMatrixGetFloatInfo_R, 2}, - {"XGDMatrixGetUIntInfo_R", (DL_FUNC) &XGDMatrixGetUIntInfo_R, 2}, - {"XGDMatrixCreateFromDF_R", (DL_FUNC) &XGDMatrixCreateFromDF_R, 3}, - {"XGDMatrixGetStrFeatureInfo_R", (DL_FUNC) &XGDMatrixGetStrFeatureInfo_R, 2}, - {"XGDMatrixNumCol_R", (DL_FUNC) &XGDMatrixNumCol_R, 1}, - {"XGDMatrixNumRow_R", (DL_FUNC) &XGDMatrixNumRow_R, 1}, - {"XGProxyDMatrixCreate_R", (DL_FUNC) &XGProxyDMatrixCreate_R, 0}, - {"XGProxyDMatrixSetDataDense_R", (DL_FUNC) &XGProxyDMatrixSetDataDense_R, 2}, - {"XGProxyDMatrixSetDataCSR_R", (DL_FUNC) &XGProxyDMatrixSetDataCSR_R, 2}, - {"XGProxyDMatrixSetDataColumnar_R", (DL_FUNC) &XGProxyDMatrixSetDataColumnar_R, 2}, - {"XGDMatrixCreateFromCallback_R", (DL_FUNC) &XGDMatrixCreateFromCallback_R, 7}, - {"XGQuantileDMatrixCreateFromCallback_R", (DL_FUNC) &XGQuantileDMatrixCreateFromCallback_R, 8}, - {"XGDMatrixFree_R", (DL_FUNC) &XGDMatrixFree_R, 1}, - {"XGGetRNAIntAsDouble", (DL_FUNC) &XGGetRNAIntAsDouble, 0}, - {"XGDMatrixGetQuantileCut_R", (DL_FUNC) &XGDMatrixGetQuantileCut_R, 1}, - {"XGDMatrixNumNonMissing_R", (DL_FUNC) &XGDMatrixNumNonMissing_R, 1}, - {"XGDMatrixGetDataAsCSR_R", (DL_FUNC) &XGDMatrixGetDataAsCSR_R, 1}, - {"XGDMatrixSaveBinary_R", (DL_FUNC) &XGDMatrixSaveBinary_R, 3}, - {"XGDMatrixSetInfo_R", (DL_FUNC) &XGDMatrixSetInfo_R, 3}, - {"XGDMatrixSetStrFeatureInfo_R", (DL_FUNC) &XGDMatrixSetStrFeatureInfo_R, 3}, - {"XGDMatrixSliceDMatrix_R", (DL_FUNC) &XGDMatrixSliceDMatrix_R, 3}, - {"XGBSetGlobalConfig_R", (DL_FUNC) &XGBSetGlobalConfig_R, 1}, - {"XGBGetGlobalConfig_R", (DL_FUNC) &XGBGetGlobalConfig_R, 0}, - {"XGBoosterFeatureScore_R", (DL_FUNC) &XGBoosterFeatureScore_R, 2}, - {"XGBoosterSlice_R", (DL_FUNC) &XGBoosterSlice_R, 4}, - {"XGBoosterSliceAndReplace_R", (DL_FUNC) &XGBoosterSliceAndReplace_R, 4}, - {NULL, NULL, 0} -}; + {"XGDuplicate_R", (DL_FUNC)&XGDuplicate_R, 1}, + {"XGPointerEqComparison_R", (DL_FUNC)&XGPointerEqComparison_R, 2}, + {"XGBoosterTrainOneIter_R", (DL_FUNC)&XGBoosterTrainOneIter_R, 5}, + {"XGBoosterCreate_R", (DL_FUNC)&XGBoosterCreate_R, 1}, + {"XGBoosterReset_R", (DL_FUNC)&XGBoosterReset_R, 1}, + {"XGBoosterCopyInfoFromDMatrix_R", (DL_FUNC)&XGBoosterCopyInfoFromDMatrix_R, 2}, + {"XGBoosterSetStrFeatureInfo_R", (DL_FUNC)&XGBoosterSetStrFeatureInfo_R, 3}, // NOLINT + {"XGBoosterGetStrFeatureInfo_R", (DL_FUNC)&XGBoosterGetStrFeatureInfo_R, 2}, // NOLINT + {"XGBoosterBoostedRounds_R", (DL_FUNC)&XGBoosterBoostedRounds_R, 1}, + {"XGBoosterGetNumFeature_R", (DL_FUNC)&XGBoosterGetNumFeature_R, 1}, + {"XGBoosterDumpModel_R", (DL_FUNC)&XGBoosterDumpModel_R, 4}, + {"XGBoosterEvalOneIter_R", (DL_FUNC)&XGBoosterEvalOneIter_R, 4}, + {"XGBoosterGetAttrNames_R", (DL_FUNC)&XGBoosterGetAttrNames_R, 1}, + {"XGBoosterGetAttr_R", (DL_FUNC)&XGBoosterGetAttr_R, 2}, + {"XGBoosterLoadModelFromRaw_R", (DL_FUNC)&XGBoosterLoadModelFromRaw_R, 2}, + {"XGBoosterSaveModelToRaw_R", (DL_FUNC)&XGBoosterSaveModelToRaw_R, 2}, + {"XGBoosterLoadModel_R", (DL_FUNC)&XGBoosterLoadModel_R, 2}, + {"XGBoosterSaveJsonConfig_R", (DL_FUNC)&XGBoosterSaveJsonConfig_R, 1}, + {"XGBoosterLoadJsonConfig_R", (DL_FUNC)&XGBoosterLoadJsonConfig_R, 2}, + {"XGBoosterSerializeToBuffer_R", (DL_FUNC)&XGBoosterSerializeToBuffer_R, 1}, + {"XGBoosterUnserializeFromBuffer_R", (DL_FUNC)&XGBoosterUnserializeFromBuffer_R, 2}, + {"XGBoosterPredictFromDMatrix_R", (DL_FUNC)&XGBoosterPredictFromDMatrix_R, 3}, + {"XGBoosterPredictFromDense_R", (DL_FUNC)&XGBoosterPredictFromDense_R, 5}, + {"XGBoosterPredictFromCSR_R", (DL_FUNC)&XGBoosterPredictFromCSR_R, 5}, + {"XGBoosterPredictFromColumnar_R", (DL_FUNC)&XGBoosterPredictFromColumnar_R, 5}, + {"XGBoosterSaveModel_R", (DL_FUNC)&XGBoosterSaveModel_R, 2}, + {"XGBoosterSetAttr_R", (DL_FUNC)&XGBoosterSetAttr_R, 3}, + {"XGBoosterSetParam_R", (DL_FUNC)&XGBoosterSetParam_R, 3}, + {"XGBoosterUpdateOneIter_R", (DL_FUNC)&XGBoosterUpdateOneIter_R, 3}, + {"XGCheckNullPtr_R", (DL_FUNC)&XGCheckNullPtr_R, 1}, + {"XGSetArrayDimNamesInplace_R", (DL_FUNC)&XGSetArrayDimNamesInplace_R, 2}, + {"XGSetVectorNamesInplace_R", (DL_FUNC)&XGSetVectorNamesInplace_R, 2}, + {"XGDMatrixCreateFromCSC_R", (DL_FUNC)&XGDMatrixCreateFromCSC_R, 6}, + {"XGDMatrixCreateFromCSR_R", (DL_FUNC)&XGDMatrixCreateFromCSR_R, 6}, + {"XGDMatrixCreateFromURI_R", (DL_FUNC)&XGDMatrixCreateFromURI_R, 3}, + {"XGDMatrixCreateFromMat_R", (DL_FUNC)&XGDMatrixCreateFromMat_R, 3}, + {"XGDMatrixGetFloatInfo_R", (DL_FUNC)&XGDMatrixGetFloatInfo_R, 2}, + {"XGDMatrixGetUIntInfo_R", (DL_FUNC)&XGDMatrixGetUIntInfo_R, 2}, + {"XGDMatrixCreateFromDF_R", (DL_FUNC)&XGDMatrixCreateFromDF_R, 3}, + {"XGDMatrixGetStrFeatureInfo_R", (DL_FUNC)&XGDMatrixGetStrFeatureInfo_R, 2}, + {"XGDMatrixNumCol_R", (DL_FUNC)&XGDMatrixNumCol_R, 1}, + {"XGDMatrixNumRow_R", (DL_FUNC)&XGDMatrixNumRow_R, 1}, + {"XGProxyDMatrixCreate_R", (DL_FUNC)&XGProxyDMatrixCreate_R, 0}, + {"XGProxyDMatrixSetDataDense_R", (DL_FUNC)&XGProxyDMatrixSetDataDense_R, 2}, + {"XGProxyDMatrixSetDataCSR_R", (DL_FUNC)&XGProxyDMatrixSetDataCSR_R, 2}, + {"XGProxyDMatrixSetDataColumnar_R", (DL_FUNC)&XGProxyDMatrixSetDataColumnar_R, 2}, + {"XGDMatrixCreateFromCallback_R", (DL_FUNC)&XGDMatrixCreateFromCallback_R, 7}, + {"XGQuantileDMatrixCreateFromCallback_R", (DL_FUNC)&XGQuantileDMatrixCreateFromCallback_R, 8}, + {"XGDMatrixFree_R", (DL_FUNC)&XGDMatrixFree_R, 1}, + {"XGGetRNAIntAsDouble", (DL_FUNC)&XGGetRNAIntAsDouble, 0}, + {"XGDMatrixGetQuantileCut_R", (DL_FUNC)&XGDMatrixGetQuantileCut_R, 1}, + {"XGDMatrixNumNonMissing_R", (DL_FUNC)&XGDMatrixNumNonMissing_R, 1}, + {"XGDMatrixGetDataAsCSR_R", (DL_FUNC)&XGDMatrixGetDataAsCSR_R, 1}, + {"XGDMatrixSaveBinary_R", (DL_FUNC)&XGDMatrixSaveBinary_R, 3}, + {"XGDMatrixSetInfo_R", (DL_FUNC)&XGDMatrixSetInfo_R, 3}, + {"XGDMatrixSetStrFeatureInfo_R", (DL_FUNC)&XGDMatrixSetStrFeatureInfo_R, 3}, + {"XGDMatrixSliceDMatrix_R", (DL_FUNC)&XGDMatrixSliceDMatrix_R, 3}, + {"XGBSetGlobalConfig_R", (DL_FUNC)&XGBSetGlobalConfig_R, 1}, + {"XGBGetGlobalConfig_R", (DL_FUNC)&XGBGetGlobalConfig_R, 0}, + {"XGBoosterFeatureScore_R", (DL_FUNC)&XGBoosterFeatureScore_R, 2}, + {"XGBoosterSlice_R", (DL_FUNC)&XGBoosterSlice_R, 4}, + {"XGBoosterSliceAndReplace_R", (DL_FUNC)&XGBoosterSliceAndReplace_R, 4}, + {NULL, NULL, 0}}; #if defined(_WIN32) __declspec(dllexport) #endif // defined(_WIN32) -void attribute_visible R_init_xgboost(DllInfo *dll) { + void attribute_visible R_init_xgboost(DllInfo *dll) { R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); R_useDynamicSymbols(dll, FALSE); XGBInitializeAltrepClass_R(dll); diff --git a/R-package/src/xgboost_R.cc b/R-package/src/xgboost_R.cc index 7d87d49f73f1..c42b6ad5f5e3 100644 --- a/R-package/src/xgboost_R.cc +++ b/R-package/src/xgboost_R.cc @@ -13,8 +13,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -25,7 +25,9 @@ #include "../../src/common/threading_utils.h" #include "../../src/data/array_interface.h" // for ArrayInterface +// clang-format off #include "./xgboost_R.h" // Must follow other includes. +// clang-format on #ifdef _MSC_VER #error "Compilation of R package with MSVC is not supported due to issues handling R headers" @@ -50,15 +52,13 @@ struct PtrToConstChar { }; SEXP WrappedMkChar(void *void_ptr) { - return Rf_mkChar(static_cast(void_ptr)->ptr); + return Rf_mkChar(static_cast(void_ptr)->ptr); } SEXP SafeMkChar(const char *c_str, SEXP continuation_token) { PtrToConstChar ptr_struct{c_str}; - return R_UnwindProtect( - WrappedMkChar, static_cast(&ptr_struct), - ThrowExceptionFromRError, nullptr, - continuation_token); + return R_UnwindProtect(WrappedMkChar, static_cast(&ptr_struct), ThrowExceptionFromRError, + nullptr, continuation_token); } struct RFunAndEnv { @@ -67,7 +67,7 @@ struct RFunAndEnv { }; SEXP WrappedExecFun(void *void_ptr) { - RFunAndEnv *r_fun_and_env = static_cast(void_ptr); + RFunAndEnv *r_fun_and_env = static_cast(void_ptr); SEXP f_expr = Rf_protect(Rf_lang1(r_fun_and_env->R_fun)); SEXP out = Rf_protect(Rf_eval(f_expr, r_fun_and_env->R_calling_env)); Rf_unprotect(2); @@ -76,34 +76,28 @@ SEXP WrappedExecFun(void *void_ptr) { SEXP SafeExecFun(SEXP R_fun, SEXP R_calling_env, SEXP continuation_token) { RFunAndEnv r_fun_and_env{R_fun, R_calling_env}; - return R_UnwindProtect( - WrappedExecFun, static_cast(&r_fun_and_env), - ThrowExceptionFromRError, nullptr, - continuation_token); + return R_UnwindProtect(WrappedExecFun, static_cast(&r_fun_and_env), + ThrowExceptionFromRError, nullptr, continuation_token); } SEXP WrappedAllocReal(void *void_ptr) { - size_t *size = static_cast(void_ptr); + size_t *size = static_cast(void_ptr); return Rf_allocVector(REALSXP, *size); } SEXP SafeAllocReal(size_t size, SEXP continuation_token) { - return R_UnwindProtect( - WrappedAllocReal, static_cast(&size), - ThrowExceptionFromRError, nullptr, - continuation_token); + return R_UnwindProtect(WrappedAllocReal, static_cast(&size), ThrowExceptionFromRError, + nullptr, continuation_token); } SEXP WrappedAllocInteger(void *void_ptr) { - size_t *size = static_cast(void_ptr); + size_t *size = static_cast(void_ptr); return Rf_allocVector(INTSXP, *size); } SEXP SafeAllocInteger(size_t size, SEXP continuation_token) { - return R_UnwindProtect( - WrappedAllocInteger, static_cast(&size), - ThrowExceptionFromRError, nullptr, - continuation_token); + return R_UnwindProtect(WrappedAllocInteger, static_cast(&size), ThrowExceptionFromRError, + nullptr, continuation_token); } [[nodiscard]] std::string MakeArrayInterfaceFromRMat(SEXP R_mat) { @@ -119,8 +113,8 @@ SEXP SafeAllocInteger(size_t size, SEXP continuation_token) { using T = std::remove_pointer_t; auto m = linalg::MatrixView{ - common::Span{ptr, - static_cast(ptr_mat_dims[0]) * static_cast(ptr_mat_dims[1])}, + common::Span{ptr, static_cast(ptr_mat_dims[0]) * + static_cast(ptr_mat_dims[1])}, {ptr_mat_dims[0], ptr_mat_dims[1]}, // Shape DeviceOrd::CPU(), linalg::Order::kF // R uses column-major @@ -286,13 +280,13 @@ void AddMissingToJson(xgboost::Json *jconfig, SEXP missing, SEXPTYPE arr_type) { /*! * \brief macro to annotate begin of api */ -#define R_API_BEGIN() \ - try { \ -/* Note: an R error triggers a long jump, hence all C++ objects that -allocated memory through non-R allocators, including the exception -object, need to be destructed before triggering the R error. -In order to preserve the error message, it gets copied to a temporary -buffer, and the R error section is reached through a 'goto' statement +#define R_API_BEGIN() \ + try { \ +/* Note: an R error triggers a long jump, hence all C++ objects that \ +allocated memory through non-R allocators, including the exception \ +object, need to be destructed before triggering the R error. \ +In order to preserve the error message, it gets copied to a temporary \ +buffer, and the R error section is reached through a 'goto' statement \ that bypasses usual function control flow. */ namespace { constexpr std::size_t MsgSize = 512; @@ -356,7 +350,7 @@ XGB_DLL SEXP XGBSetGlobalConfig_R(SEXP json_str) { } XGB_DLL SEXP XGBGetGlobalConfig_R() { - const char* json_str; + const char *json_str; R_API_BEGIN(); CHECK_CALL(XGBGetGlobalConfig(&json_str)); R_API_END(); @@ -520,15 +514,13 @@ XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset, SEXP allow_groups int res_code; { std::vector idxvec(len); - #ifndef _MSC_VER - #pragma omp simd - #endif +#ifndef _MSC_VER +#pragma omp simd +#endif for (R_xlen_t i = 0; i < len; ++i) { idxvec[i] = idxset_[i] - 1; } - res_code = XGDMatrixSliceDMatrixEx(R_ExternalPtrAddr(handle), - BeginPtr(idxvec), len, - &res, + res_code = XGDMatrixSliceDMatrixEx(R_ExternalPtrAddr(handle), BeginPtr(idxvec), len, &res, Rf_asLogical(allow_groups)); } CHECK_CALL(res_code); @@ -541,9 +533,8 @@ XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset, SEXP allow_groups XGB_DLL SEXP XGDMatrixSaveBinary_R(SEXP handle, SEXP fname, SEXP silent) { R_API_BEGIN(); - CHECK_CALL(XGDMatrixSaveBinary(R_ExternalPtrAddr(handle), - CHAR(Rf_asChar(fname)), - Rf_asInteger(silent))); + CHECK_CALL( + XGDMatrixSaveBinary(R_ExternalPtrAddr(handle), CHAR(Rf_asChar(fname)), Rf_asInteger(silent))); R_API_END(); return R_NilValue; } @@ -554,10 +545,10 @@ XGB_DLL SEXP XGDMatrixSetInfo_R(SEXP handle, SEXP field, SEXP array) { SEXP arr_dim = Rf_getAttrib(array, R_DimSymbol); int res_code; { - const std::string array_str = Rf_isNull(arr_dim)? - MakeArrayInterfaceFromRVector(array) : MakeArrayInterfaceFromRMat(array); - res_code = XGDMatrixSetInfoFromInterface( - R_ExternalPtrAddr(handle), CHAR(field_), array_str.c_str()); + const std::string array_str = Rf_isNull(arr_dim) ? MakeArrayInterfaceFromRVector(array) + : MakeArrayInterfaceFromRMat(array); + res_code = + XGDMatrixSetInfoFromInterface(R_ExternalPtrAddr(handle), CHAR(field_), array_str.c_str()); } CHECK_CALL(res_code); Rf_unprotect(1); @@ -592,7 +583,7 @@ XGB_DLL SEXP XGDMatrixSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP array) { for (size_t i = 0; i < len; ++i) { str_info.emplace_back(CHAR(VECTOR_ELT(str_info_holder, i))); } - std::vector vec(len); + std::vector vec(len); std::transform(str_info.cbegin(), str_info.cend(), vec.begin(), [](std::string const &str) { return str.c_str(); }); res_code = XGDMatrixSetStrFeatureInfo(R_ExternalPtrAddr(handle), name, vec.data(), len); @@ -700,11 +691,8 @@ XGB_DLL SEXP XGProxyDMatrixSetDataCSR_R(SEXP handle, SEXP lst) { std::string array_str_indices = MakeArrayInterfaceFromRVector(VECTOR_ELT(lst, 1)); std::string array_str_data = MakeArrayInterfaceFromRVector(VECTOR_ELT(lst, 2)); const int ncol = Rf_asInteger(VECTOR_ELT(lst, 3)); - res_code = XGProxyDMatrixSetDataCSR(proxy_dmat, - array_str_indptr.c_str(), - array_str_indices.c_str(), - array_str_data.c_str(), - ncol); + res_code = XGProxyDMatrixSetDataCSR(proxy_dmat, array_str_indptr.c_str(), + array_str_indices.c_str(), array_str_data.c_str(), ncol); } CHECK_CALL(res_code); R_API_END(); @@ -732,36 +720,30 @@ struct _RDataIterator { SEXP calling_env; SEXP continuation_token; - _RDataIterator( - SEXP f_next, SEXP f_reset, SEXP calling_env, SEXP continuation_token) : - f_next(f_next), f_reset(f_reset), calling_env(calling_env), - continuation_token(continuation_token) {} + _RDataIterator(SEXP f_next, SEXP f_reset, SEXP calling_env, SEXP continuation_token) + : f_next(f_next), + f_reset(f_reset), + calling_env(calling_env), + continuation_token(continuation_token) {} - void reset() { - SafeExecFun(this->f_reset, this->calling_env, this->continuation_token); - } + void reset() { SafeExecFun(this->f_reset, this->calling_env, this->continuation_token); } int next() { - SEXP R_res = Rf_protect( - SafeExecFun(this->f_next, this->calling_env, this->continuation_token)); + SEXP R_res = Rf_protect(SafeExecFun(this->f_next, this->calling_env, this->continuation_token)); int res = Rf_asInteger(R_res); Rf_unprotect(1); return res; } }; -void _reset_RDataIterator(DataIterHandle iter) { - static_cast<_RDataIterator*>(iter)->reset(); -} +void _reset_RDataIterator(DataIterHandle iter) { static_cast<_RDataIterator *>(iter)->reset(); } -int _next_RDataIterator(DataIterHandle iter) { - return static_cast<_RDataIterator*>(iter)->next(); -} +int _next_RDataIterator(DataIterHandle iter) { return static_cast<_RDataIterator *>(iter)->next(); } -SEXP XGDMatrixCreateFromCallbackGeneric_R( - SEXP f_next, SEXP f_reset, SEXP calling_env, SEXP proxy_dmat, - SEXP n_threads, SEXP missing, SEXP max_bin, SEXP ref_dmat, - SEXP cache_prefix, bool as_quantile_dmatrix) { +SEXP XGDMatrixCreateFromCallbackGeneric_R(SEXP f_next, SEXP f_reset, SEXP calling_env, + SEXP proxy_dmat, SEXP n_threads, SEXP missing, + SEXP max_bin, SEXP ref_dmat, SEXP cache_prefix, + bool as_quantile_dmatrix) { SEXP continuation_token = Rf_protect(R_MakeUnwindCont()); SEXP out = Rf_protect(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue)); R_API_BEGIN(); @@ -794,21 +776,12 @@ SEXP XGDMatrixCreateFromCallbackGeneric_R( if (as_quantile_dmatrix) { res_code = XGQuantileDMatrixCreateFromCallback( - &data_iterator, - R_ExternalPtrAddr(proxy_dmat), - ref_dmat_handle, - _reset_RDataIterator, - _next_RDataIterator, - json_str.c_str(), - &out_dmat); + &data_iterator, R_ExternalPtrAddr(proxy_dmat), ref_dmat_handle, _reset_RDataIterator, + _next_RDataIterator, json_str.c_str(), &out_dmat); } else { - res_code = XGDMatrixCreateFromCallback( - &data_iterator, - R_ExternalPtrAddr(proxy_dmat), - _reset_RDataIterator, - _next_RDataIterator, - json_str.c_str(), - &out_dmat); + res_code = XGDMatrixCreateFromCallback(&data_iterator, R_ExternalPtrAddr(proxy_dmat), + _reset_RDataIterator, _next_RDataIterator, + json_str.c_str(), &out_dmat); } } catch (ErrorWithUnwind &e) { R_ContinueUnwind(continuation_token); @@ -824,22 +797,18 @@ SEXP XGDMatrixCreateFromCallbackGeneric_R( } /* namespace */ -XGB_DLL SEXP XGQuantileDMatrixCreateFromCallback_R( - SEXP f_next, SEXP f_reset, SEXP calling_env, SEXP proxy_dmat, - SEXP n_threads, SEXP missing, SEXP max_bin, SEXP ref_dmat) { - return XGDMatrixCreateFromCallbackGeneric_R( - f_next, f_reset, calling_env, proxy_dmat, - n_threads, missing, max_bin, ref_dmat, - R_NilValue, true); +XGB_DLL SEXP XGQuantileDMatrixCreateFromCallback_R(SEXP f_next, SEXP f_reset, SEXP calling_env, + SEXP proxy_dmat, SEXP n_threads, SEXP missing, + SEXP max_bin, SEXP ref_dmat) { + return XGDMatrixCreateFromCallbackGeneric_R(f_next, f_reset, calling_env, proxy_dmat, n_threads, + missing, max_bin, ref_dmat, R_NilValue, true); } -XGB_DLL SEXP XGDMatrixCreateFromCallback_R( - SEXP f_next, SEXP f_reset, SEXP calling_env, SEXP proxy_dmat, - SEXP n_threads, SEXP missing, SEXP cache_prefix) { - return XGDMatrixCreateFromCallbackGeneric_R( - f_next, f_reset, calling_env, proxy_dmat, - n_threads, missing, R_NilValue, R_NilValue, - cache_prefix, false); +XGB_DLL SEXP XGDMatrixCreateFromCallback_R(SEXP f_next, SEXP f_reset, SEXP calling_env, + SEXP proxy_dmat, SEXP n_threads, SEXP missing, + SEXP cache_prefix) { + return XGDMatrixCreateFromCallbackGeneric_R(f_next, f_reset, calling_env, proxy_dmat, n_threads, + missing, R_NilValue, R_NilValue, cache_prefix, false); } XGB_DLL SEXP XGDMatrixFree_R(SEXP proxy_dmat) { @@ -852,9 +821,7 @@ XGB_DLL SEXP XGGetRNAIntAsDouble() { return Rf_ScalarReal(sentinel_as_double); } -XGB_DLL SEXP XGDuplicate_R(SEXP obj) { - return Rf_duplicate(obj); -} +XGB_DLL SEXP XGDuplicate_R(SEXP obj) { return Rf_duplicate(obj); } XGB_DLL SEXP XGPointerEqComparison_R(SEXP obj1, SEXP obj2) { return Rf_ScalarLogical(R_ExternalPtrAddr(obj1) == R_ExternalPtrAddr(obj2)); @@ -912,10 +879,7 @@ XGB_DLL SEXP XGDMatrixGetDataAsCSR_R(SEXP handle) { std::unique_ptr indices(new unsigned[nnz]); std::unique_ptr data(new float[nnz]); - CHECK_CALL(XGDMatrixGetDataAsCSR(R_ExternalPtrAddr(handle), - "{}", - indptr.get(), - indices.get(), + CHECK_CALL(XGDMatrixGetDataAsCSR(R_ExternalPtrAddr(handle), "{}", indptr.get(), indices.get(), data.get())); std::copy(indptr.get(), indptr.get() + nrows + 1, INTEGER(VECTOR_ELT(out, 0))); @@ -940,9 +904,7 @@ corresponds to an 'externalptr' holding the C object, forbidding modification by not implementing setters, and adding custom serialization. */ R_altrep_class_t XGBAltrepPointerClass; -R_xlen_t XGBAltrepPointerLength_R(SEXP R_altrepped_obj) { - return 1; -} +R_xlen_t XGBAltrepPointerLength_R(SEXP R_altrepped_obj) { return 1; } SEXP XGBAltrepPointerGetElt_R(SEXP R_altrepped_obj, R_xlen_t idx) { return R_altrep_data1(R_altrepped_obj); @@ -972,8 +934,7 @@ SEXP XGBAltrepSerializer_R(SEXP R_altrepped_obj) { BoosterHandle handle = R_ExternalPtrAddr(R_altrep_data1(R_altrepped_obj)); char const *serialized_bytes; bst_ulong serialized_length; - CHECK_CALL(XGBoosterSerializeToBuffer( - handle, &serialized_length, &serialized_bytes)); + CHECK_CALL(XGBoosterSerializeToBuffer(handle, &serialized_length, &serialized_bytes)); SEXP R_state = Rf_protect(Rf_allocVector(RAWSXP, serialized_length)); if (serialized_length != 0) { std::memcpy(RAW(R_state), serialized_bytes, serialized_length); @@ -989,9 +950,7 @@ SEXP XGBAltrepDeserializer_R(SEXP /*unused*/, SEXP R_state) { R_API_BEGIN(); BoosterHandle handle = nullptr; CHECK_CALL(XGBoosterCreate(nullptr, 0, &handle)); - int res_code = XGBoosterUnserializeFromBuffer(handle, - RAW(R_state), - Rf_xlength(R_state)); + int res_code = XGBoosterUnserializeFromBuffer(handle, RAW(R_state), Rf_xlength(R_state)); if (res_code != 0) { XGBoosterFree(handle); } @@ -1003,11 +962,9 @@ SEXP XGBAltrepDeserializer_R(SEXP /*unused*/, SEXP R_state) { } // https://purrple.cat/blog/2018/10/14/altrep-and-cpp/ -Rboolean XGBAltrepInspector_R( - SEXP x, int pre, int deep, int pvec, - void (*inspect_subtree)(SEXP, int, int, int)) { - Rprintf("Altrepped external pointer [address:%p]\n", - R_ExternalPtrAddr(R_altrep_data1(x))); +Rboolean XGBAltrepInspector_R(SEXP x, int pre, int deep, int pvec, + void (*inspect_subtree)(SEXP, int, int, int)) { + Rprintf("Altrepped external pointer [address:%p]\n", R_ExternalPtrAddr(R_altrep_data1(x))); return TRUE; } @@ -1022,14 +979,11 @@ SEXP XGBAltrepDuplicate_R(SEXP R_altrepped_obj, Rboolean deep) { SEXP out = Rf_protect(XGBMakeEmptyAltrep()); char const *serialized_bytes; bst_ulong serialized_length; - CHECK_CALL(XGBoosterSerializeToBuffer( - R_ExternalPtrAddr(R_altrep_data1(R_altrepped_obj)), - &serialized_length, &serialized_bytes)); + CHECK_CALL(XGBoosterSerializeToBuffer(R_ExternalPtrAddr(R_altrep_data1(R_altrepped_obj)), + &serialized_length, &serialized_bytes)); BoosterHandle new_handle = nullptr; CHECK_CALL(XGBoosterCreate(nullptr, 0, &new_handle)); - int res_code = XGBoosterUnserializeFromBuffer(new_handle, - serialized_bytes, - serialized_length); + int res_code = XGBoosterUnserializeFromBuffer(new_handle, serialized_bytes, serialized_length); if (res_code != 0) { XGBoosterFree(new_handle); } @@ -1062,7 +1016,7 @@ XGB_DLL SEXP XGBoosterCreate_R(SEXP dmats) { int res_code; { - std::vector dvec(len); + std::vector dvec(len); for (R_xlen_t i = 0; i < len; ++i) { dvec[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i)); } @@ -1086,27 +1040,19 @@ XGB_DLL SEXP XGBoosterCopyInfoFromDMatrix_R(SEXP booster, SEXP dmat) { R_API_BEGIN(); char const **feature_names; bst_ulong len_feature_names = 0; - CHECK_CALL(XGDMatrixGetStrFeatureInfo(R_ExternalPtrAddr(dmat), - "feature_name", - &len_feature_names, + CHECK_CALL(XGDMatrixGetStrFeatureInfo(R_ExternalPtrAddr(dmat), "feature_name", &len_feature_names, &feature_names)); if (len_feature_names) { - CHECK_CALL(XGBoosterSetStrFeatureInfo(R_ExternalPtrAddr(booster), - "feature_name", - feature_names, + CHECK_CALL(XGBoosterSetStrFeatureInfo(R_ExternalPtrAddr(booster), "feature_name", feature_names, len_feature_names)); } char const **feature_types; bst_ulong len_feature_types = 0; - CHECK_CALL(XGDMatrixGetStrFeatureInfo(R_ExternalPtrAddr(dmat), - "feature_type", - &len_feature_types, + CHECK_CALL(XGDMatrixGetStrFeatureInfo(R_ExternalPtrAddr(dmat), "feature_type", &len_feature_types, &feature_types)); if (len_feature_types) { - CHECK_CALL(XGBoosterSetStrFeatureInfo(R_ExternalPtrAddr(booster), - "feature_type", - feature_types, + CHECK_CALL(XGBoosterSetStrFeatureInfo(R_ExternalPtrAddr(booster), "feature_type", feature_types, len_feature_types)); } R_API_END(); @@ -1120,14 +1066,12 @@ XGB_DLL SEXP XGBoosterSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP features int res_code; { - std::vector str_arr(len_features); + std::vector str_arr(len_features); for (bst_ulong idx = 0; idx < len_features; idx++) { str_arr[idx] = CHAR(STRING_ELT(features, idx)); } - res_code = XGBoosterSetStrFeatureInfo(R_ExternalPtrAddr(handle), - CHAR(field_char), - str_arr.data(), - len_features); + res_code = XGBoosterSetStrFeatureInfo(R_ExternalPtrAddr(handle), CHAR(field_char), + str_arr.data(), len_features); } CHECK_CALL(res_code); Rf_unprotect(1); @@ -1140,8 +1084,8 @@ XGB_DLL SEXP XGBoosterGetStrFeatureInfo_R(SEXP handle, SEXP field) { bst_ulong len; const char **out_features; SEXP field_char = Rf_protect(Rf_asChar(field)); - CHECK_CALL(XGBoosterGetStrFeatureInfo(R_ExternalPtrAddr(handle), - CHAR(field_char), &len, &out_features)); + CHECK_CALL( + XGBoosterGetStrFeatureInfo(R_ExternalPtrAddr(handle), CHAR(field_char), &len, &out_features)); SEXP out = Rf_protect(Rf_allocVector(STRSXP, len)); for (bst_ulong idx = 0; idx < len; idx++) { SET_STRING_ELT(out, idx, Rf_mkChar(out_features[idx])); @@ -1178,9 +1122,7 @@ XGB_DLL SEXP XGBoosterSetParam_R(SEXP handle, SEXP name, SEXP val) { R_API_BEGIN(); SEXP name_ = Rf_protect(Rf_asChar(name)); SEXP val_ = Rf_protect(Rf_asChar(val)); - CHECK_CALL(XGBoosterSetParam(R_ExternalPtrAddr(handle), - CHAR(name_), - CHAR(val_))); + CHECK_CALL(XGBoosterSetParam(R_ExternalPtrAddr(handle), CHAR(name_), CHAR(val_))); Rf_unprotect(2); R_API_END(); return R_NilValue; @@ -1188,8 +1130,7 @@ XGB_DLL SEXP XGBoosterSetParam_R(SEXP handle, SEXP name, SEXP val) { XGB_DLL SEXP XGBoosterUpdateOneIter_R(SEXP handle, SEXP iter, SEXP dtrain) { R_API_BEGIN(); - CHECK_CALL(XGBoosterUpdateOneIter(R_ExternalPtrAddr(handle), - Rf_asInteger(iter), + CHECK_CALL(XGBoosterUpdateOneIter(R_ExternalPtrAddr(handle), Rf_asInteger(iter), R_ExternalPtrAddr(dtrain))); R_API_END(); return R_NilValue; @@ -1203,10 +1144,10 @@ XGB_DLL SEXP XGBoosterTrainOneIter_R(SEXP handle, SEXP dtrain, SEXP iter, SEXP g int res_code; { - const std::string s_grad = Rf_isNull(gdim)? - MakeArrayInterfaceFromRVector(grad) : MakeArrayInterfaceFromRMat(grad); - const std::string s_hess = Rf_isNull(hdim)? - MakeArrayInterfaceFromRVector(hess) : MakeArrayInterfaceFromRMat(hess); + const std::string s_grad = + Rf_isNull(gdim) ? MakeArrayInterfaceFromRVector(grad) : MakeArrayInterfaceFromRMat(grad); + const std::string s_hess = + Rf_isNull(hdim) ? MakeArrayInterfaceFromRVector(hess) : MakeArrayInterfaceFromRMat(hess); res_code = XGBoosterTrainOneIter(R_ExternalPtrAddr(handle), R_ExternalPtrAddr(dtrain), Rf_asInteger(iter), s_grad.c_str(), s_hess.c_str()); } @@ -1220,8 +1161,7 @@ XGB_DLL SEXP XGBoosterTrainOneIter_R(SEXP handle, SEXP dtrain, SEXP iter, SEXP g XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evnames) { const char *ret; R_API_BEGIN(); - CHECK_EQ(Rf_xlength(dmats), Rf_xlength(evnames)) - << "dmats and evnams must have same length"; + CHECK_EQ(Rf_xlength(dmats), Rf_xlength(evnames)) << "dmats and evnams must have same length"; R_xlen_t len = Rf_xlength(dmats); SEXP evnames_lst = Rf_protect(Rf_allocVector(VECSXP, len)); for (R_xlen_t i = 0; i < len; i++) { @@ -1230,10 +1170,10 @@ XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evn int res_code; { - std::vector vec_dmats(len); + std::vector vec_dmats(len); std::vector vec_names; vec_names.reserve(len); - std::vector vec_sptr(len); + std::vector vec_sptr(len); for (R_xlen_t i = 0; i < len; ++i) { vec_dmats[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i)); vec_names.emplace_back(CHAR(VECTOR_ELT(evnames_lst, i))); @@ -1241,11 +1181,8 @@ XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evn for (R_xlen_t i = 0; i < len; ++i) { vec_sptr[i] = vec_names[i].c_str(); } - res_code = XGBoosterEvalOneIter(R_ExternalPtrAddr(handle), - Rf_asInteger(iter), - BeginPtr(vec_dmats), - BeginPtr(vec_sptr), - len, &ret); + res_code = XGBoosterEvalOneIter(R_ExternalPtrAddr(handle), Rf_asInteger(iter), + BeginPtr(vec_dmats), BeginPtr(vec_sptr), len, &ret); } CHECK_CALL(res_code); Rf_unprotect(1); @@ -1274,9 +1211,7 @@ struct ProxyDmatrixWrapper { } } - DMatrixHandle get_handle() { - return this->proxy_dmat_handle; - } + DMatrixHandle get_handle() { return this->proxy_dmat_handle; } }; std::unique_ptr GetProxyDMatrixWithBaseMargin(SEXP base_margin) { @@ -1287,26 +1222,25 @@ std::unique_ptr GetProxyDMatrixWithBaseMargin(SEXP base_mar SEXP base_margin_dim = Rf_getAttrib(base_margin, R_DimSymbol); int res_code; try { - const std::string array_str = Rf_isNull(base_margin_dim)? - MakeArrayInterfaceFromRVector(base_margin) : MakeArrayInterfaceFromRMat(base_margin); + const std::string array_str = Rf_isNull(base_margin_dim) + ? MakeArrayInterfaceFromRVector(base_margin) + : MakeArrayInterfaceFromRMat(base_margin); std::unique_ptr proxy_dmat(new ProxyDmatrixWrapper()); - res_code = XGDMatrixSetInfoFromInterface(proxy_dmat->get_handle(), - "base_margin", - array_str.c_str()); + res_code = + XGDMatrixSetInfoFromInterface(proxy_dmat->get_handle(), "base_margin", array_str.c_str()); if (res_code != 0) { throw ProxyDmatrixError(); } return proxy_dmat; - } catch(ProxyDmatrixError &err) { + } catch (ProxyDmatrixError &err) { Rf_error("%s", XGBGetLastError()); } } -enum class PredictionInputType {DMatrix, DenseMatrix, CSRMatrix, DataFrame}; +enum class PredictionInputType { DMatrix, DenseMatrix, CSRMatrix, DataFrame }; SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config, - PredictionInputType input_type, SEXP missing, - SEXP base_margin) { + PredictionInputType input_type, SEXP missing, SEXP base_margin) { SEXP r_out_result = R_NilValue; R_API_BEGIN(); SEXP json_config_ = Rf_protect(Rf_asChar(json_config)); @@ -1320,16 +1254,16 @@ SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config, { switch (input_type) { case PredictionInputType::DMatrix: { - res_code = XGBoosterPredictFromDMatrix(R_ExternalPtrAddr(handle), - R_ExternalPtrAddr(input_data), c_json_config, - &out_shape, &out_dim, &out_result); + res_code = + XGBoosterPredictFromDMatrix(R_ExternalPtrAddr(handle), R_ExternalPtrAddr(input_data), + c_json_config, &out_shape, &out_dim, &out_result); break; } case PredictionInputType::CSRMatrix: { - std::unique_ptr proxy_dmat = GetProxyDMatrixWithBaseMargin( - base_margin); - DMatrixHandle proxy_dmat_handle = proxy_dmat.get()? proxy_dmat->get_handle() : nullptr; + std::unique_ptr proxy_dmat = + GetProxyDMatrixWithBaseMargin(base_margin); + DMatrixHandle proxy_dmat_handle = proxy_dmat.get() ? proxy_dmat->get_handle() : nullptr; SEXP indptr = VECTOR_ELT(input_data, 0); SEXP indices = VECTOR_ELT(input_data, 1); @@ -1346,15 +1280,15 @@ SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config, const std::string new_c_json = xgboost::Json::Dump(new_json); res_code = XGBoosterPredictFromCSR( - R_ExternalPtrAddr(handle), sindptr.c_str(), sindices.c_str(), sdata.c_str(), - ncol_csr, new_c_json.c_str(), proxy_dmat_handle, &out_shape, &out_dim, &out_result); + R_ExternalPtrAddr(handle), sindptr.c_str(), sindices.c_str(), sdata.c_str(), ncol_csr, + new_c_json.c_str(), proxy_dmat_handle, &out_shape, &out_dim, &out_result); break; } case PredictionInputType::DenseMatrix: { - std::unique_ptr proxy_dmat = GetProxyDMatrixWithBaseMargin( - base_margin); - DMatrixHandle proxy_dmat_handle = proxy_dmat.get()? proxy_dmat->get_handle() : nullptr; + std::unique_ptr proxy_dmat = + GetProxyDMatrixWithBaseMargin(base_margin); + DMatrixHandle proxy_dmat_handle = proxy_dmat.get() ? proxy_dmat->get_handle() : nullptr; const std::string array_str = MakeArrayInterfaceFromRMat(input_data); xgboost::StringView json_str(c_json_config); @@ -1362,16 +1296,16 @@ SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config, AddMissingToJson(&new_json, missing, TYPEOF(input_data)); const std::string new_c_json = xgboost::Json::Dump(new_json); - res_code = XGBoosterPredictFromDense( - R_ExternalPtrAddr(handle), array_str.c_str(), new_c_json.c_str(), - proxy_dmat_handle, &out_shape, &out_dim, &out_result); + res_code = XGBoosterPredictFromDense(R_ExternalPtrAddr(handle), array_str.c_str(), + new_c_json.c_str(), proxy_dmat_handle, &out_shape, + &out_dim, &out_result); break; } case PredictionInputType::DataFrame: { - std::unique_ptr proxy_dmat = GetProxyDMatrixWithBaseMargin( - base_margin); - DMatrixHandle proxy_dmat_handle = proxy_dmat.get()? proxy_dmat->get_handle() : nullptr; + std::unique_ptr proxy_dmat = + GetProxyDMatrixWithBaseMargin(base_margin); + DMatrixHandle proxy_dmat_handle = proxy_dmat.get() ? proxy_dmat->get_handle() : nullptr; const std::string df_str = MakeArrayInterfaceFromRDataFrame(input_data); @@ -1380,9 +1314,9 @@ SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config, AddMissingToJson(&new_json, missing, REALSXP); const std::string new_c_json = xgboost::Json::Dump(new_json); - res_code = XGBoosterPredictFromColumnar( - R_ExternalPtrAddr(handle), df_str.c_str(), new_c_json.c_str(), - proxy_dmat_handle, &out_shape, &out_dim, &out_result); + res_code = XGBoosterPredictFromColumnar(R_ExternalPtrAddr(handle), df_str.c_str(), + new_c_json.c_str(), proxy_dmat_handle, &out_shape, + &out_dim, &out_result); break; } } @@ -1411,27 +1345,27 @@ SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config, } // namespace -XGB_DLL SEXP XGBoosterPredictFromDMatrix_R(SEXP handle, SEXP dmat, SEXP json_config) { - return XGBoosterPredictGeneric(handle, dmat, json_config, - PredictionInputType::DMatrix, R_NilValue, R_NilValue); +XGB_DLL SEXP XGBoosterPredictFromDMatrix_R(SEXP handle, SEXP dmat, SEXP json_config) { + return XGBoosterPredictGeneric(handle, dmat, json_config, PredictionInputType::DMatrix, + R_NilValue, R_NilValue); } -XGB_DLL SEXP XGBoosterPredictFromDense_R(SEXP handle, SEXP R_mat, SEXP missing, - SEXP json_config, SEXP base_margin) { - return XGBoosterPredictGeneric(handle, R_mat, json_config, - PredictionInputType::DenseMatrix, missing, base_margin); +XGB_DLL SEXP XGBoosterPredictFromDense_R(SEXP handle, SEXP R_mat, SEXP missing, SEXP json_config, + SEXP base_margin) { + return XGBoosterPredictGeneric(handle, R_mat, json_config, PredictionInputType::DenseMatrix, + missing, base_margin); } -XGB_DLL SEXP XGBoosterPredictFromCSR_R(SEXP handle, SEXP lst, SEXP missing, - SEXP json_config, SEXP base_margin) { - return XGBoosterPredictGeneric(handle, lst, json_config, - PredictionInputType::CSRMatrix, missing, base_margin); +XGB_DLL SEXP XGBoosterPredictFromCSR_R(SEXP handle, SEXP lst, SEXP missing, SEXP json_config, + SEXP base_margin) { + return XGBoosterPredictGeneric(handle, lst, json_config, PredictionInputType::CSRMatrix, missing, + base_margin); } -XGB_DLL SEXP XGBoosterPredictFromColumnar_R(SEXP handle, SEXP R_df, SEXP missing, - SEXP json_config, SEXP base_margin) { - return XGBoosterPredictGeneric(handle, R_df, json_config, - PredictionInputType::DataFrame, missing, base_margin); +XGB_DLL SEXP XGBoosterPredictFromColumnar_R(SEXP handle, SEXP R_df, SEXP missing, SEXP json_config, + SEXP base_margin) { + return XGBoosterPredictGeneric(handle, R_df, json_config, PredictionInputType::DataFrame, missing, + base_margin); } XGB_DLL SEXP XGBoosterLoadModel_R(SEXP handle, SEXP fname) { @@ -1450,9 +1384,7 @@ XGB_DLL SEXP XGBoosterSaveModel_R(SEXP handle, SEXP fname) { XGB_DLL SEXP XGBoosterLoadModelFromRaw_R(SEXP handle, SEXP raw) { R_API_BEGIN(); - CHECK_CALL(XGBoosterLoadModelFromBuffer(R_ExternalPtrAddr(handle), - RAW(raw), - Rf_xlength(raw))); + CHECK_CALL(XGBoosterLoadModelFromBuffer(R_ExternalPtrAddr(handle), RAW(raw), Rf_xlength(raw))); R_API_END(); return R_NilValue; } @@ -1474,12 +1406,10 @@ XGB_DLL SEXP XGBoosterSaveModelToRaw_R(SEXP handle, SEXP json_config) { } XGB_DLL SEXP XGBoosterSaveJsonConfig_R(SEXP handle) { - const char* ret; + const char *ret; R_API_BEGIN(); - bst_ulong len {0}; - CHECK_CALL(XGBoosterSaveJsonConfig(R_ExternalPtrAddr(handle), - &len, - &ret)); + bst_ulong len{0}; + CHECK_CALL(XGBoosterSaveJsonConfig(R_ExternalPtrAddr(handle), &len, &ret)); R_API_END(); return Rf_mkString(ret); } @@ -1508,9 +1438,7 @@ XGB_DLL SEXP XGBoosterSerializeToBuffer_R(SEXP handle) { XGB_DLL SEXP XGBoosterUnserializeFromBuffer_R(SEXP handle, SEXP raw) { R_API_BEGIN(); - CHECK_CALL(XGBoosterUnserializeFromBuffer(R_ExternalPtrAddr(handle), - RAW(raw), - Rf_xlength(raw))); + CHECK_CALL(XGBoosterUnserializeFromBuffer(R_ExternalPtrAddr(handle), RAW(raw), Rf_xlength(raw))); R_API_END(); return R_NilValue; } @@ -1524,16 +1452,13 @@ XGB_DLL SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats, SEXP bst_ulong olen; const char **res; const char *fmt = CHAR(dump_format_); - CHECK_CALL(XGBoosterDumpModelEx(R_ExternalPtrAddr(handle), - CHAR(fmap_), - Rf_asInteger(with_stats), - fmt, - &olen, &res)); + CHECK_CALL(XGBoosterDumpModelEx(R_ExternalPtrAddr(handle), CHAR(fmap_), Rf_asInteger(with_stats), + fmt, &olen, &res)); out = Rf_protect(Rf_allocVector(STRSXP, olen)); try { if (!strcmp("json", fmt)) { std::stringstream stream; - stream << "[\n"; + stream << "[\n"; for (size_t i = 0; i < olen; ++i) { stream << res[i]; if (i < olen - 1) { @@ -1542,13 +1467,13 @@ XGB_DLL SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats, SEXP stream << "\n"; } } - stream << "]"; + stream << "]"; const std::string temp_str = stream.str(); SET_STRING_ELT(out, 0, SafeMkChar(temp_str.c_str(), continuation_token)); } else { for (size_t i = 0; i < olen; ++i) { std::stringstream stream; - stream << "booster[" << i <<"]\n" << res[i]; + stream << "booster[" << i << "]\n" << res[i]; const std::string temp_str = stream.str(); SET_STRING_ELT(out, i, SafeMkChar(temp_str.c_str(), continuation_token)); } @@ -1566,10 +1491,7 @@ XGB_DLL SEXP XGBoosterGetAttr_R(SEXP handle, SEXP name) { R_API_BEGIN(); int success; const char *val; - CHECK_CALL(XGBoosterGetAttr(R_ExternalPtrAddr(handle), - CHAR(Rf_asChar(name)), - &val, - &success)); + CHECK_CALL(XGBoosterGetAttr(R_ExternalPtrAddr(handle), CHAR(Rf_asChar(name)), &val, &success)); if (success) { out = Rf_protect(Rf_allocVector(STRSXP, 1)); SET_STRING_ELT(out, 0, Rf_mkChar(val)); @@ -1593,8 +1515,7 @@ XGB_DLL SEXP XGBoosterSetAttr_R(SEXP handle, SEXP name, SEXP val) { v = CHAR(val_); } - CHECK_CALL(XGBoosterSetAttr(R_ExternalPtrAddr(handle), - CHAR(name_), v)); + CHECK_CALL(XGBoosterSetAttr(R_ExternalPtrAddr(handle), CHAR(name_), v)); Rf_unprotect(n_protected); R_API_END(); return R_NilValue; @@ -1605,8 +1526,7 @@ XGB_DLL SEXP XGBoosterGetAttrNames_R(SEXP handle) { R_API_BEGIN(); bst_ulong len; const char **res; - CHECK_CALL(XGBoosterGetAttrNames(R_ExternalPtrAddr(handle), - &len, &res)); + CHECK_CALL(XGBoosterGetAttrNames(R_ExternalPtrAddr(handle), &len, &res)); if (len > 0) { out = Rf_protect(Rf_allocVector(STRSXP, len)); for (size_t i = 0; i < len; ++i) { @@ -1635,9 +1555,8 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config) { bst_ulong const *out_shape; float const *out_scores; - CHECK_CALL(XGBoosterFeatureScore(R_ExternalPtrAddr(handle), c_json_config, - &out_n_features, &out_features, - &out_dim, &out_shape, &out_scores)); + CHECK_CALL(XGBoosterFeatureScore(R_ExternalPtrAddr(handle), c_json_config, &out_n_features, + &out_features, &out_dim, &out_shape, &out_scores)); out_shape_sexp = Rf_protect(Rf_allocVector(INTSXP, out_dim)); size_t len = 1; int *out_shape_sexp_ = INTEGER(out_shape_sexp); @@ -1668,11 +1587,8 @@ XGB_DLL SEXP XGBoosterSlice_R(SEXP handle, SEXP begin_layer, SEXP end_layer, SEX SEXP out = Rf_protect(XGBMakeEmptyAltrep()); R_API_BEGIN(); BoosterHandle handle_out = nullptr; - CHECK_CALL(XGBoosterSlice(R_ExternalPtrAddr(handle), - Rf_asInteger(begin_layer), - Rf_asInteger(end_layer), - Rf_asInteger(step), - &handle_out)); + CHECK_CALL(XGBoosterSlice(R_ExternalPtrAddr(handle), Rf_asInteger(begin_layer), + Rf_asInteger(end_layer), Rf_asInteger(step), &handle_out)); XGBAltrepSetPointer(out, handle_out); R_API_END(); Rf_unprotect(1); @@ -1683,11 +1599,8 @@ XGB_DLL SEXP XGBoosterSliceAndReplace_R(SEXP handle, SEXP begin_layer, SEXP end_ R_API_BEGIN(); BoosterHandle old_handle = R_ExternalPtrAddr(handle); BoosterHandle new_handle = nullptr; - CHECK_CALL(XGBoosterSlice(old_handle, - Rf_asInteger(begin_layer), - Rf_asInteger(end_layer), - Rf_asInteger(step), - &new_handle)); + CHECK_CALL(XGBoosterSlice(old_handle, Rf_asInteger(begin_layer), Rf_asInteger(end_layer), + Rf_asInteger(step), &new_handle)); R_SetExternalPtrAddr(handle, new_handle); CHECK_CALL(XGBoosterFree(old_handle)); R_API_END(); diff --git a/R-package/src/xgboost_R.h b/R-package/src/xgboost_R.h index f25d4e030cdd..1b97b3fcd291 100644 --- a/R-package/src/xgboost_R.h +++ b/R-package/src/xgboost_R.h @@ -4,19 +4,19 @@ * \author Tianqi Chen * \brief R wrapper of xgboost */ -#ifndef XGBOOST_R_H_ // NOLINT(*) -#define XGBOOST_R_H_ // NOLINT(*) - +#ifndef XGBOOST_R_H_ // NOLINT(*) +#define XGBOOST_R_H_ // NOLINT(*) #ifndef R_NO_REMAP -# define R_NO_REMAP +#define R_NO_REMAP #endif +// clang-format off #include #include #include #include - #include +// clang-format on /*! * \brief check whether a handle is NULL @@ -71,9 +71,7 @@ XGB_DLL SEXP XGDMatrixCreateFromURI_R(SEXP uri, SEXP silent, SEXP data_split_mod * \param n_threads Number of threads used to construct DMatrix from dense matrix. * \return created dmatrix */ -XGB_DLL SEXP XGDMatrixCreateFromMat_R(SEXP mat, - SEXP missing, - SEXP n_threads); +XGB_DLL SEXP XGDMatrixCreateFromMat_R(SEXP mat, SEXP missing, SEXP n_threads); /** * @brief Create matrix content from a data frame. @@ -209,9 +207,9 @@ XGB_DLL SEXP XGProxyDMatrixSetDataColumnar_R(SEXP handle, SEXP lst); * \param cache_prefix path of cache file * \return handle R externalptr holding the resulting DMatrix. */ -XGB_DLL SEXP XGDMatrixCreateFromCallback_R( - SEXP expr_f_next, SEXP expr_f_reset, SEXP calling_env, SEXP proxy_dmat, - SEXP n_threads, SEXP missing, SEXP cache_prefix); +XGB_DLL SEXP XGDMatrixCreateFromCallback_R(SEXP expr_f_next, SEXP expr_f_reset, SEXP calling_env, + SEXP proxy_dmat, SEXP n_threads, SEXP missing, + SEXP cache_prefix); /*! * \brief Create a QuantileDMatrix from a DataIter with callbacks @@ -227,9 +225,10 @@ XGB_DLL SEXP XGDMatrixCreateFromCallback_R( * \param ref_dmat an optional reference DMatrix from which to get the bin boundaries. * \return handle R externalptr holding the resulting QuantileDMatrix. */ -XGB_DLL SEXP XGQuantileDMatrixCreateFromCallback_R( - SEXP expr_f_next, SEXP expr_f_reset, SEXP calling_env, SEXP proxy_dmat, - SEXP n_threads, SEXP missing, SEXP max_bin, SEXP ref_dmat); +XGB_DLL SEXP XGQuantileDMatrixCreateFromCallback_R(SEXP expr_f_next, SEXP expr_f_reset, + SEXP calling_env, SEXP proxy_dmat, + SEXP n_threads, SEXP missing, SEXP max_bin, + SEXP ref_dmat); /*! * \brief Frees a ProxyDMatrix and empties out the R externalptr object that holds it @@ -385,8 +384,8 @@ XGB_DLL SEXP XGBoosterPredictFromDMatrix_R(SEXP handle, SEXP dmat, SEXP json_con * * \return A list containing 2 vectors, first one for shape while second one for prediction result. */ -XGB_DLL SEXP XGBoosterPredictFromDense_R(SEXP handle, SEXP R_mat, SEXP missing, - SEXP json_config, SEXP base_margin); +XGB_DLL SEXP XGBoosterPredictFromDense_R(SEXP handle, SEXP R_mat, SEXP missing, SEXP json_config, + SEXP base_margin); /*! * \brief Run prediction on R CSR matrix @@ -402,8 +401,8 @@ XGB_DLL SEXP XGBoosterPredictFromDense_R(SEXP handle, SEXP R_mat, SEXP missing, * * \return A list containing 2 vectors, first one for shape while second one for prediction result. */ -XGB_DLL SEXP XGBoosterPredictFromCSR_R(SEXP handle, SEXP lst, SEXP missing, - SEXP json_config, SEXP base_margin); +XGB_DLL SEXP XGBoosterPredictFromCSR_R(SEXP handle, SEXP lst, SEXP missing, SEXP json_config, + SEXP base_margin); /*! * \brief Run prediction on R data.frame @@ -415,8 +414,8 @@ XGB_DLL SEXP XGBoosterPredictFromCSR_R(SEXP handle, SEXP lst, SEXP missing, * * \return A list containing 2 vectors, first one for shape while second one for prediction result. */ -XGB_DLL SEXP XGBoosterPredictFromColumnar_R(SEXP handle, SEXP R_df, SEXP missing, - SEXP json_config, SEXP base_margin); +XGB_DLL SEXP XGBoosterPredictFromColumnar_R(SEXP handle, SEXP R_df, SEXP missing, SEXP json_config, + SEXP base_margin); /*! * \brief load model from existing file @@ -474,10 +473,10 @@ XGB_DLL SEXP XGBoosterSaveJsonConfig_R(SEXP handle); XGB_DLL SEXP XGBoosterLoadJsonConfig_R(SEXP handle, SEXP value); /*! - * \brief Memory snapshot based serialization method. Saves everything states - * into buffer. - * \param handle handle to booster - */ + * \brief Memory snapshot based serialization method. Saves everything states + * into buffer. + * \param handle handle to booster + */ XGB_DLL SEXP XGBoosterSerializeToBuffer_R(SEXP handle); /*! diff --git a/R-package/src/xgboost_custom.cc b/R-package/src/xgboost_custom.cc index fdd444e5d5fe..1a4f9eee0969 100644 --- a/R-package/src/xgboost_custom.cc +++ b/R-package/src/xgboost_custom.cc @@ -3,20 +3,20 @@ // to change behavior of libxgboost #include + +#include + #include "../../src/common/random.h" #include "./xgboost_R.h" // redirect the messages to R's console. namespace dmlc { -void CustomLogMessage::Log(const std::string& msg) { - Rprintf("%s\n", msg.c_str()); -} +void CustomLogMessage::Log(const std::string& msg) { Rprintf("%s\n", msg.c_str()); } } // namespace dmlc namespace xgboost { ConsoleLogger::~ConsoleLogger() { - if (cur_verbosity_ == LogVerbosity::kIgnore || - cur_verbosity_ <= GlobalVerbosity()) { + if (cur_verbosity_ == LogVerbosity::kIgnore || cur_verbosity_ <= GlobalVerbosity()) { if (cur_verbosity_ == LogVerbosity::kWarning) { REprintf("%s\n", log_stream_.str().c_str()); } else { @@ -24,22 +24,16 @@ ConsoleLogger::~ConsoleLogger() { } } } -TrackerLogger::~TrackerLogger() { - dmlc::CustomLogMessage::Log(log_stream_.str()); -} +TrackerLogger::~TrackerLogger() { dmlc::CustomLogMessage::Log(log_stream_.str()); } } // namespace xgboost namespace xgboost { namespace common { // redirect the nath functions. -bool CheckNAN(double v) { - return ISNAN(v); -} +bool CheckNAN(double v) { return ISNAN(v); } #if !defined(XGBOOST_USE_CUDA) -double LogGamma(double v) { - return lgammafn(v); -} +double LogGamma(double v) { return lgammafn(v); } #endif // !defined(XGBOOST_USE_CUDA) } // namespace common diff --git a/amalgamation/dmlc-minimum0.cc b/amalgamation/dmlc-minimum0.cc index 1474cb1bc447..9d154933d4d2 100644 --- a/amalgamation/dmlc-minimum0.cc +++ b/amalgamation/dmlc-minimum0.cc @@ -3,12 +3,12 @@ * \brief Mininum DMLC library Amalgamation, used for easy plugin of dmlc lib. * Normally this is not needed. */ -#include "../dmlc-core/src/io/line_split.cc" -#include "../dmlc-core/src/io/recordio_split.cc" -#include "../dmlc-core/src/io/input_split_base.cc" -#include "../dmlc-core/src/io/local_filesys.cc" -#include "../dmlc-core/src/io/filesys.cc" -#include "../dmlc-core/src/io/indexed_recordio_split.cc" #include "../dmlc-core/src/data.cc" #include "../dmlc-core/src/io.cc" +#include "../dmlc-core/src/io/filesys.cc" +#include "../dmlc-core/src/io/indexed_recordio_split.cc" +#include "../dmlc-core/src/io/input_split_base.cc" +#include "../dmlc-core/src/io/line_split.cc" +#include "../dmlc-core/src/io/local_filesys.cc" +#include "../dmlc-core/src/io/recordio_split.cc" #include "../dmlc-core/src/recordio.cc" diff --git a/cmake/RPackageInstall.cmake.in b/cmake/RPackageInstall.cmake.in index bde4c75c726a..cf0c39b4a9ae 100644 --- a/cmake/RPackageInstall.cmake.in +++ b/cmake/RPackageInstall.cmake.in @@ -31,4 +31,4 @@ set(XGB_DEPS_SCRIPT check_call(COMMAND "${LIBR_EXECUTABLE}" -q -e "${XGB_DEPS_SCRIPT}") # Install the XGBoost R package -check_call(COMMAND "${LIBR_EXECUTABLE}" CMD INSTALL --no-multiarch --build "${build_dir}/R-package") \ No newline at end of file +check_call(COMMAND "${LIBR_EXECUTABLE}" CMD INSTALL --no-multiarch --build "${build_dir}/R-package") diff --git a/demo/c-api/basic/c-api-demo.c b/demo/c-api/basic/c-api-demo.c index e7dfa23b9058..9fd1dca0159d 100644 --- a/demo/c-api/basic/c-api-demo.c +++ b/demo/c-api/basic/c-api-demo.c @@ -6,6 +6,7 @@ */ #include +#include #include #include /* uint32_t,uint64_t */ #include @@ -13,13 +14,14 @@ #include #include -#define safe_xgboost(call) { \ -int err = (call); \ -if (err != 0) { \ - fprintf(stderr, "%s:%d: error in %s: %s\n", __FILE__, __LINE__, #call, XGBGetLastError()); \ - exit(1); \ -} \ -} +#define safe_xgboost(call) \ + { \ + int err = (call); \ + if (err != 0) { \ + fprintf(stderr, "%s:%d: error in %s: %s\n", __FILE__, __LINE__, #call, XGBGetLastError()); \ + exit(1); \ + } \ + } /* Make Json encoded array interface. */ static void MakeArrayInterface(size_t data, size_t n, char const* typestr, size_t length, @@ -27,13 +29,13 @@ static void MakeArrayInterface(size_t data, size_t n, char const* typestr, size_ static char const kTemplate[] = "{\"data\": [%lu, true], \"shape\": [%lu, %lu], \"typestr\": \"%s\", \"version\": 3}"; memset(out, '\0', length); - sprintf(out, kTemplate, data, n, 1ul, typestr); + snprintf(out, length, kTemplate, data, n, 1ul, typestr); } /* Make Json encoded DMatrix configuration. */ static void MakeConfig(int n_threads, size_t length, char* out) { static char const kTemplate[] = "{\"missing\": NaN, \"nthread\": %d}"; memset(out, '\0', length); - sprintf(out, kTemplate, n_threads); + snprintf(out, length, kTemplate, n_threads); } int main() { @@ -42,8 +44,10 @@ int main() { // load the data DMatrixHandle dtrain, dtest; - safe_xgboost(XGDMatrixCreateFromFile("../../data/agaricus.txt.train?format=libsvm", silent, &dtrain)); - safe_xgboost(XGDMatrixCreateFromFile("../../data/agaricus.txt.test?format=libsvm", silent, &dtest)); + safe_xgboost( + XGDMatrixCreateFromFile("../../data/agaricus.txt.train?format=libsvm", silent, &dtrain)); + safe_xgboost( + XGDMatrixCreateFromFile("../../data/agaricus.txt.test?format=libsvm", silent, &dtest)); // create the booster BoosterHandle booster; @@ -73,7 +77,7 @@ int main() { bst_ulong num_feature = 0; safe_xgboost(XGBoosterGetNumFeature(booster, &num_feature)); - printf("num_feature: %lu\n", (unsigned long)(num_feature)); + printf("num_feature: %" PRIu64 "\n", (uint64_t)(num_feature)); // predict bst_ulong out_len = 0; @@ -109,12 +113,12 @@ int main() { { printf("Dense Matrix Example (XGDMatrixCreateFromMat): "); - const float values[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, - 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, - 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}; + const float values[] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}; DMatrixHandle dmat; safe_xgboost(XGDMatrixCreateFromMat(values, 1, 127, 0.0, &dmat)); diff --git a/demo/c-api/external-memory/README.md b/demo/c-api/external-memory/README.md index e578b535ba21..47cb272d8faa 100644 --- a/demo/c-api/external-memory/README.md +++ b/demo/c-api/external-memory/README.md @@ -13,4 +13,4 @@ In the example, we define a custom data iterator with 2 methods: `reset` and `ne its end, and the `reset` method resets iterations. One important detail when using the C API for data iterator is users need to make sure that the data passed into `next` method must be kept in memory until the next iteration or `reset` is called. The external memory -DMatrix is not limited to training, but also valid for other features like prediction. \ No newline at end of file +DMatrix is not limited to training, but also valid for other features like prediction. diff --git a/demo/c-api/external-memory/external_memory.c b/demo/c-api/external-memory/external_memory.c index d9c6123c148d..9a72b3dc0823 100644 --- a/demo/c-api/external-memory/external_memory.c +++ b/demo/c-api/external-memory/external_memory.c @@ -5,15 +5,15 @@ */ #include +#include #include #include #include -#define safe_xgboost(err) \ - if ((err) != 0) { \ - fprintf(stderr, "%s:%d: error in %s: %s\n", __FILE__, __LINE__, #err, \ - XGBGetLastError()); \ - exit(1); \ +#define safe_xgboost(err) \ + if ((err) != 0) { \ + fprintf(stderr, "%s:%d: error in %s: %s\n", __FILE__, __LINE__, #err, XGBGetLastError()); \ + exit(1); \ } #define N_BATCHS 32 @@ -40,11 +40,10 @@ typedef struct _DataIter { char _array[128]; } DataIter; -#define safe_malloc(ptr) \ - if ((ptr) == NULL) { \ - fprintf(stderr, "%s:%d: Failed to allocate memory.\n", __FILE__, \ - __LINE__); \ - exit(1); \ +#define safe_malloc(ptr) \ + if ((ptr) == NULL) { \ + fprintf(stderr, "%s:%d: Failed to allocate memory.\n", __FILE__, __LINE__); \ + exit(1); \ } /** @@ -100,30 +99,30 @@ void DataIterator_Free(DataIter *self) { free(self->lengths); free(self->labels); safe_xgboost(XGDMatrixFree(self->_proxy)); -}; +} int DataIterator_Next(DataIterHandle handle) { DataIter *self = (DataIter *)(handle); if (self->cur_it == self->n) { self->cur_it = 0; - return 0; /* At end */ + return 0; /* At end */ } /* A JSON string encoding array interface (standard from numpy). */ - char array[] = "{\"data\": [%lu, false], \"shape\":[%lu, 1], \"typestr\": " - "\"_array, '\0', sizeof(self->_array)); - sprintf(self->_array, array, (size_t)self->data[self->cur_it], - self->lengths[self->cur_it]); + snprintf(self->_array, sizeof(self->_array), array, (size_t)self->data[self->cur_it], + self->lengths[self->cur_it]); safe_xgboost(XGProxyDMatrixSetDataDense(self->_proxy, self->_array)); /* The data passed in the iterator must remain valid (not being freed until the next * iteration or reset) */ - safe_xgboost(XGDMatrixSetDenseInfo(self->_proxy, "label", - self->labels[self->cur_it], + safe_xgboost(XGDMatrixSetDenseInfo(self->_proxy, "label", self->labels[self->cur_it], self->lengths[self->cur_it], 1)); self->cur_it++; - return 1; /* Continue. */ + return 1; /* Continue. */ } void DataIterator_Reset(DataIterHandle handle) { @@ -149,8 +148,7 @@ void TrainModel(DMatrix Xy) { size_t n_rounds = 10; for (size_t i = 0; i < n_rounds; ++i) { safe_xgboost(XGBoosterUpdateOneIter(booster, i, Xy)); - safe_xgboost(XGBoosterEvalOneIter(booster, i, cache, validation_names, 1, - &validation_result)); + safe_xgboost(XGBoosterEvalOneIter(booster, i, cache, validation_names, 1, &validation_result)); printf("%s\n", validation_result); } @@ -168,8 +166,8 @@ int main() { * prefix "cache-" will be generated in current directory */ char config[] = "{\"missing\": NaN, \"cache_prefix\": \"cache\"}"; DMatrix Xy; - safe_xgboost(XGDMatrixCreateFromCallback( - &iter, iter._proxy, DataIterator_Reset, DataIterator_Next, config, &Xy)); + safe_xgboost(XGDMatrixCreateFromCallback(&iter, iter._proxy, DataIterator_Reset, + DataIterator_Next, config, &Xy)); TrainModel(Xy); diff --git a/demo/c-api/inference/inference.c b/demo/c-api/inference/inference.c index b3542e4665f2..7ee481a4a875 100644 --- a/demo/c-api/inference/inference.c +++ b/demo/c-api/inference/inference.c @@ -6,23 +6,22 @@ * See more examples in test_c_api.cc on how to reuse a ProxyDMatrix object for reducing * the latency of DMatrix creation. */ +/* NOLINTBEGIN(*) */ #include #include #include #include -#define safe_xgboost(err) \ - if ((err) != 0) { \ - fprintf(stderr, "%s:%d: error in %s: %s\n", __FILE__, __LINE__, #err, \ - XGBGetLastError()); \ - exit(1); \ +#define safe_xgboost(err) \ + if ((err) != 0) { \ + fprintf(stderr, "%s:%d: error in %s: %s\n", __FILE__, __LINE__, #err, XGBGetLastError()); \ + exit(1); \ } -#define safe_malloc(ptr) \ - if ((ptr) == NULL) { \ - fprintf(stderr, "%s:%d: Failed to allocate memory.\n", __FILE__, \ - __LINE__); \ - exit(1); \ +#define safe_malloc(ptr) \ + if ((ptr) == NULL) { \ + fprintf(stderr, "%s:%d: Failed to allocate memory.\n", __FILE__, __LINE__); \ + exit(1); \ } #define N_SAMPLES 128 @@ -44,8 +43,7 @@ struct _Matrix { typedef struct _Matrix *Matrix; /* Initialize matrix, copy data from `data` if it's not NULL. */ -void Matrix_Create(Matrix *self, float const *data, size_t n_samples, - size_t n_features) { +void Matrix_Create(Matrix *self, float const *data, size_t n_samples, size_t n_features) { if (self == NULL) { fprintf(stderr, "Invalid pointer to %s\n", __func__); exit(-1); @@ -59,8 +57,7 @@ void Matrix_Create(Matrix *self, float const *data, size_t n_samples, (*self)->shape[1] = n_features; if (data != NULL) { - memcpy((*self)->data, data, - (*self)->shape[0] * (*self)->shape[1] * sizeof(float)); + memcpy((*self)->data, data, (*self)->shape[0] * (*self)->shape[1] * sizeof(float)); } } @@ -75,11 +72,12 @@ void Matrix_Random(Matrix *self, size_t n_samples, size_t n_features) { /* Array interface specified by numpy. */ char const *Matrix_ArrayInterface(Matrix self) { - char const template[] = "{\"data\": [%lu, true], \"shape\": [%lu, %lu], " - "\"typestr\": \"_array_intrerface, '\0', sizeof(self->_array_intrerface)); - sprintf(self->_array_intrerface, template, (size_t)self->data, self->shape[0], - self->shape[1]); + snprintf(self->_array_intrerface, sizeof(self->_array_intrerface), template, (size_t)self->data, + self->shape[0], self->shape[1]); return self->_array_intrerface; } @@ -87,9 +85,7 @@ size_t Matrix_NSamples(Matrix self) { return self->shape[0]; } size_t Matrix_NFeatures(Matrix self) { return self->shape[1]; } -float Matrix_At(Matrix self, size_t i, size_t j) { - return self->data[i * self->shape[1] + j]; -} +float Matrix_At(Matrix self, size_t i, size_t j) { return self->data[i * self->shape[1] + j]; } void Matrix_Print(Matrix self) { for (size_t i = 0; i < Matrix_NSamples(self); i++) { @@ -157,8 +153,8 @@ int main() { /* Pointer to a thread local contigious array, assigned in prediction function. */ float const *out_results; - safe_xgboost(XGBoosterPredictFromDMatrix(booster, Xy, config, &out_shape, - &out_dim, &out_results)); + safe_xgboost( + XGBoosterPredictFromDMatrix(booster, Xy, config, &out_shape, &out_dim, &out_results)); if (out_dim != 2 || out_shape[0] != N_SAMPLES || out_shape[1] != 1) { fprintf(stderr, "Regression model should output prediction as vector."); exit(-1); @@ -175,9 +171,10 @@ int main() { { /* Run inplace prediction, which is faster and more memory efficient, but supports * only basic inference types. */ - char const config[] = "{\"type\": 0, \"iteration_begin\": 0, " - "\"iteration_end\": 0, \"strict_shape\": true, " - "\"cache_id\": 0, \"missing\": NaN}"; + char const config[] = + "{\"type\": 0, \"iteration_begin\": 0, " + "\"iteration_end\": 0, \"strict_shape\": true, " + "\"cache_id\": 0, \"missing\": NaN}"; /* Shape of output prediction */ uint64_t const *out_shape; /* Dimension of output prediction */ @@ -186,13 +183,12 @@ int main() { float const *out_results; char const *X_interface = Matrix_ArrayInterface(X); - safe_xgboost(XGBoosterPredictFromDense(booster, X_interface, config, NULL, - &out_shape, &out_dim, &out_results)); + safe_xgboost(XGBoosterPredictFromDense(booster, X_interface, config, NULL, &out_shape, &out_dim, + &out_results)); if (out_dim != 2 || out_shape[0] != N_SAMPLES || out_shape[1] != 1) { - fprintf(stderr, - "Regression model should output prediction as vector, %lu, %lu", - out_dim, out_shape[0]); + fprintf(stderr, "Regression model should output prediction as vector, %lu, %lu", out_dim, + out_shape[0]); exit(-1); } @@ -211,3 +207,5 @@ int main() { Matrix_Free(y); return 0; } + +/* NOLINTEND(*) */ diff --git a/demo/data/regression/machine.names b/demo/data/regression/machine.names index f19a21827976..b087e58fd32d 100644 --- a/demo/data/regression/machine.names +++ b/demo/data/regression/machine.names @@ -1,17 +1,17 @@ -1. Title: Relative CPU Performance Data +1. Title: Relative CPU Performance Data 2. Source Information -- Creators: Phillip Ein-Dor and Jacob Feldmesser - -- Ein-Dor: Faculty of Management; Tel Aviv University; Ramat-Aviv; + -- Ein-Dor: Faculty of Management; Tel Aviv University; Ramat-Aviv; Tel Aviv, 69978; Israel - -- Donor: David W. Aha (aha@ics.uci.edu) (714) 856-8779 + -- Donor: David W. Aha (aha@ics.uci.edu) (714) 856-8779 -- Date: October, 1987 - + 3. Past Usage: 1. Ein-Dor and Feldmesser (CACM 4/87, pp 308-317) - -- Results: + -- Results: -- linear regression prediction of relative cpu performance - -- Recorded 34% average deviation from actual values + -- Recorded 34% average deviation from actual values 2. Kibler,D. & Aha,D. (1988). Instance-Based Prediction of Real-Valued Attributes. In Proceedings of the CSCSI (Canadian AI) Conference. @@ -25,16 +25,16 @@ using a linear regression method. See their article (pp 308-313) for more details on how the relative performance values were set. -5. Number of Instances: 209 +5. Number of Instances: 209 -6. Number of Attributes: 10 (6 predictive attributes, 2 non-predictive, +6. Number of Attributes: 10 (6 predictive attributes, 2 non-predictive, 1 goal field, and the linear regression's guess) 7. Attribute Information: - 1. vendor name: 30 - (adviser, amdahl,apollo, basf, bti, burroughs, c.r.d, cambex, cdc, dec, - dg, formation, four-phase, gould, honeywell, hp, ibm, ipl, magnuson, - microdata, nas, ncr, nixdorf, perkin-elmer, prime, siemens, sperry, + 1. vendor name: 30 + (adviser, amdahl,apollo, basf, bti, burroughs, c.r.d, cambex, cdc, dec, + dg, formation, four-phase, gould, honeywell, hp, ibm, ipl, magnuson, + microdata, nas, ncr, nixdorf, perkin-elmer, prime, siemens, sperry, sratus, wang) 2. Model Name: many unique symbols 3. MYCT: machine cycle time in nanoseconds (integer) @@ -69,4 +69,3 @@ Summary Statistics: CHMAX: 0 176 18.2 26.0 0.6052 PRP: 6 1150 105.6 160.8 1.0000 ERP: 15 1238 99.3 154.8 0.9665 - diff --git a/demo/rmm_plugin/README.rst b/demo/rmm_plugin/README.rst index ff15e815818a..942dc0093ddd 100644 --- a/demo/rmm_plugin/README.rst +++ b/demo/rmm_plugin/README.rst @@ -75,4 +75,4 @@ The newer NVIDIA platforms like `Grace-Hopper have a coherent memory model. Users can use the `SamHeadroomMemoryResource` in the latest RMM to utilize system memory for storing data. This can help XGBoost utilize memory from the host for GPU computation, but it may reduce performance due to slower CPU memory speed -and page migration overhead. \ No newline at end of file +and page migration overhead. diff --git a/doc/.gitignore b/doc/.gitignore index 5c373a0e4a20..9521f97ef71a 100644 --- a/doc/.gitignore +++ b/doc/.gitignore @@ -8,4 +8,4 @@ parser.py *.pyc web-data # generated by doxygen -tmp \ No newline at end of file +tmp diff --git a/doc/_static/js/auto_module_index.js b/doc/_static/js/auto_module_index.js index b918ecdc1635..8af0ad7bf266 100644 --- a/doc/_static/js/auto_module_index.js +++ b/doc/_static/js/auto_module_index.js @@ -22,4 +22,3 @@ function auto_index(module) { li_node.append(html); }); } - diff --git a/doc/changes/index.rst b/doc/changes/index.rst index d376c490853f..03714c8bc85c 100644 --- a/doc/changes/index.rst +++ b/doc/changes/index.rst @@ -10,4 +10,4 @@ For release notes prior to the 2.1 release, please see `news `_ for mo custom_metric_obj advanced_custom_obj intercept - privacy_preserving \ No newline at end of file + privacy_preserving diff --git a/doc/tutorials/privacy_preserving.rst b/doc/tutorials/privacy_preserving.rst index 132861f7c5b3..6cc4b14ac6cd 100644 --- a/doc/tutorials/privacy_preserving.rst +++ b/doc/tutorials/privacy_preserving.rst @@ -53,7 +53,7 @@ To verify model accuracy in encrypted computations, you can run an FHE simulatio predictions = classifier.predict(X_test, fhe="simulate") -This simulation can be used to evaluate the model. The resulting accuracy of this simulation step is representative of the actual FHE execution without having to pay the cost of an actual FHE execution. +This simulation can be used to evaluate the model. The resulting accuracy of this simulation step is representative of the actual FHE execution without having to pay the cost of an actual FHE execution. When the model is ready, actual Fully Homomorphic Encryption execution can be performed: @@ -94,4 +94,4 @@ Concrete ML provides a framework for executing privacy-preserving inferences by More information and examples are given in the `Concrete ML documentation`_. .. _Concrete ML: https://github.com/zama-ai/concrete-ml -.. _`Concrete ML documentation`: https://docs.zama.ai/concrete-ml \ No newline at end of file +.. _`Concrete ML documentation`: https://docs.zama.ai/concrete-ml diff --git a/doc/tutorials/saving_model.rst b/doc/tutorials/saving_model.rst index 20aeb210c465..4a398c2e0fa8 100644 --- a/doc/tutorials/saving_model.rst +++ b/doc/tutorials/saving_model.rst @@ -299,4 +299,4 @@ Brief History - UBJSON has been set to default in 2.1. - The old binary format was removed in 3.1. - The JSON schema file is no longer maintained and has been removed in 3.2. The underlying - schema of the model is not changed. \ No newline at end of file + schema of the model is not changed. diff --git a/include/xgboost/base.h b/include/xgboost/base.h index c01922c830a9..2f931f25fa11 100644 --- a/include/xgboost/base.h +++ b/include/xgboost/base.h @@ -43,7 +43,7 @@ #endif // defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4) #if defined(__GNUC__) -#define XGBOOST_EXPECT(cond, ret) __builtin_expect((cond), (ret)) +#define XGBOOST_EXPECT(cond, ret) __builtin_expect((cond), (ret)) #else #define XGBOOST_EXPECT(cond, ret) (cond) #endif // defined(__GNUC__) @@ -51,7 +51,7 @@ /*! * \brief Tag function as usable by device */ -#if defined (__CUDA__) || defined(__NVCC__) +#if defined(__CUDA__) || defined(__NVCC__) #define XGBOOST_DEVICE __host__ __device__ #else #define XGBOOST_DEVICE @@ -65,7 +65,6 @@ #define XGBOOST_DEV_INLINE #endif // defined(__CUDA__) || defined(__CUDACC__) - // restrict #if defined(_MSC_VER) #define XGBOOST_RESTRICT __restrict @@ -78,8 +77,8 @@ /* default logic for software pre-fetching */ #if (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))) || defined(__INTEL_COMPILER) // Enable _mm_prefetch for Intel compiler and MSVC+x86 - #define XGBOOST_MM_PREFETCH_PRESENT - #define XGBOOST_BUILTIN_PREFETCH_PRESENT +#define XGBOOST_MM_PREFETCH_PRESENT +#define XGBOOST_BUILTIN_PREFETCH_PRESENT #elif defined(__GNUC__) // Enable __builtin_prefetch for GCC #define XGBOOST_BUILTIN_PREFETCH_PRESENT @@ -109,7 +108,7 @@ using bst_idx_t = std::uint64_t; // NOLINT /** * \brief Type for tree node index and tree depth. */ -using bst_node_t = std::int32_t; // NOLINT +using bst_node_t = std::int32_t; // NOLINT /** * @brief Type for ranking group index. */ @@ -148,12 +147,13 @@ class GradientPairInternal { public: using ValueT = T; - inline void Add(const ValueT& grad, const ValueT& hess) { + inline void Add(const ValueT &grad, const ValueT &hess) { grad_ += grad; hess_ += hess; } - inline static void Reduce(GradientPairInternal& a, const GradientPairInternal& b) { // NOLINT(*) + inline static void Reduce(GradientPairInternal &a, + const GradientPairInternal &b) { // NOLINT(*) a += b; } @@ -181,30 +181,26 @@ class GradientPairInternal { XGBOOST_DEVICE T GetGrad() const { return grad_; } XGBOOST_DEVICE T GetHess() const { return hess_; } - XGBOOST_DEVICE GradientPairInternal &operator+=( - const GradientPairInternal &rhs) { + XGBOOST_DEVICE GradientPairInternal &operator+=(const GradientPairInternal &rhs) { grad_ += rhs.grad_; hess_ += rhs.hess_; return *this; } - XGBOOST_DEVICE GradientPairInternal operator+( - const GradientPairInternal &rhs) const { + XGBOOST_DEVICE GradientPairInternal operator+(const GradientPairInternal &rhs) const { GradientPairInternal g; g.grad_ = grad_ + rhs.grad_; g.hess_ = hess_ + rhs.hess_; return g; } - XGBOOST_DEVICE GradientPairInternal &operator-=( - const GradientPairInternal &rhs) { + XGBOOST_DEVICE GradientPairInternal &operator-=(const GradientPairInternal &rhs) { grad_ -= rhs.grad_; hess_ -= rhs.hess_; return *this; } - XGBOOST_DEVICE GradientPairInternal operator-( - const GradientPairInternal &rhs) const { + XGBOOST_DEVICE GradientPairInternal operator-(const GradientPairInternal &rhs) const { GradientPairInternal g; g.grad_ = grad_ - rhs.grad_; g.hess_ = hess_ - rhs.hess_; @@ -242,12 +238,10 @@ class GradientPairInternal { } XGBOOST_DEVICE explicit GradientPairInternal(int value) { - *this = GradientPairInternal(static_cast(value), - static_cast(value)); + *this = GradientPairInternal(static_cast(value), static_cast(value)); } - friend std::ostream &operator<<(std::ostream &os, - const GradientPairInternal &g) { + friend std::ostream &operator<<(std::ostream &os, const GradientPairInternal &g) { os << g.GetGrad() << "/" << g.GetHess(); return os; } diff --git a/include/xgboost/c_api.h b/include/xgboost/c_api.h index 6b7a879350c9..a0ec15957a16 100644 --- a/include/xgboost/c_api.h +++ b/include/xgboost/c_api.h @@ -9,19 +9,19 @@ #ifdef __cplusplus #define XGB_EXTERN_C extern "C" #include -#include #include +#include #else #define XGB_EXTERN_C #include -#include #include +#include #endif // __cplusplus #if defined(_MSC_VER) || defined(_WIN32) #define XGB_DLL XGB_EXTERN_C __declspec(dllexport) #else -#define XGB_DLL XGB_EXTERN_C __attribute__ ((visibility ("default"))) +#define XGB_DLL XGB_EXTERN_C __attribute__((visibility("default"))) #endif // defined(_MSC_VER) || defined(_WIN32) // manually define unsigned long @@ -65,7 +65,7 @@ typedef void *CategoriesHandle; // NOLINT(*) * @param minor Store the minor version number. * @param patch Store the patch (revision) number. */ -XGB_DLL void XGBoostVersion(int* major, int* minor, int* patch); +XGB_DLL void XGBoostVersion(int *major, int *minor, int *patch); /** * @brief Get compile information of the shared XGBoost library. @@ -98,7 +98,7 @@ XGB_DLL const char *XGBGetLastError(); * * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBRegisterLogCallback(void (*callback)(const char*)); +XGB_DLL int XGBRegisterLogCallback(void (*callback)(const char *)); /** * @brief Set global configuration (collection of parameters that apply globally). This function @@ -112,7 +112,8 @@ XGB_DLL int XGBSetGlobalConfig(char const *config); /** * @brief Get current global configuration (collection of parameters that apply globally). - * @param out_config pointer to received returned global configuration, represented as a JSON string. + * @param out_config pointer to received returned global configuration, represented as a JSON + * string. * @return 0 when success, -1 when failure happens */ XGB_DLL int XGBGetGlobalConfig(char const **out_config); @@ -149,12 +150,14 @@ XGB_DLL int XGDMatrixCreateFromFile(const char *fname, int silent, DMatrixHandle * @brief load a data matrix * * @param config JSON encoded parameters for DMatrix construction. Accepted fields are: - * - uri: The URI of the input file. The URI parameter `format` is required when loading text data. + * - uri: The URI of the input file. The URI parameter `format` is required when loading text + * data. * @verbatim embed:rst:leading-asterisk * See :doc:`/tutorials/input_format` for more info. * @endverbatim * - silent (optional): Whether to print message during loading. Default to true. - * - data_split_mode (optional): Whether the file was split by row or column beforehand for distributed computing. Default to row. + * - data_split_mode (optional): Whether the file was split by row or column beforehand for + * distributed computing. Default to row. * @param out a loaded data matrix * @return 0 when success, -1 when failure happens */ @@ -243,7 +246,8 @@ XGB_DLL int XGDMatrixCreateFromCSR(char const *indptr, char const *indices, char * @param config JSON encoded configuration. Required values are: * - missing: Which value to represent missing value. * - nthread (optional): Number of threads used for initializing DMatrix. - * - data_split_mode (optional): Whether the data was split by row or column beforehand. Default to row. + * - data_split_mode (optional): Whether the data was split by row or column beforehand. Default + * to row. * @param out The created DMatrix * * @return 0 when success, -1 when failure happens @@ -265,7 +269,6 @@ XGB_DLL int XGDMatrixCreateFromDense(char const *data, char const *config, DMatr XGB_DLL int XGDMatrixCreateFromCSC(char const *indptr, char const *indices, char const *data, bst_ulong nrow, char const *config, DMatrixHandle *out); - /** * @brief create matrix content from dense matrix * @param data pointer to the data space @@ -275,10 +278,7 @@ XGB_DLL int XGDMatrixCreateFromCSC(char const *indptr, char const *indices, char * @param out created dmatrix * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGDMatrixCreateFromMat(const float *data, - bst_ulong nrow, - bst_ulong ncol, - float missing, +XGB_DLL int XGDMatrixCreateFromMat(const float *data, bst_ulong nrow, bst_ulong ncol, float missing, DMatrixHandle *out); /** * @brief create matrix content from dense matrix @@ -291,9 +291,8 @@ XGB_DLL int XGDMatrixCreateFromMat(const float *data, * @return 0 when success, -1 when failure happens */ XGB_DLL int XGDMatrixCreateFromMat_omp(const float *data, // NOLINT - bst_ulong nrow, bst_ulong ncol, - float missing, DMatrixHandle *out, - int nthread); + bst_ulong nrow, bst_ulong ncol, float missing, + DMatrixHandle *out, int nthread); /** * @brief Create DMatrix from CUDA columnar format. (cuDF) @@ -315,7 +314,8 @@ XGB_DLL int XGDMatrixCreateFromCudaColumnar(char const *data, char const *config * @param config JSON encoded configuration. Required values are: * - missing: Which value to represent missing value. * - nthread (optional): Number of threads used for initializing DMatrix. - * - data_split_mode (optional): Whether the data was split by row or column beforehand. Default to row. + * - data_split_mode (optional): Whether the data was split by row or column beforehand. Default + * to row. * @param out created dmatrix * @return 0 when success, -1 when failure happens */ @@ -380,7 +380,6 @@ typedef void *DataIterHandle; // NOLINT(*) /** @brief handle to an internal data holder. */ typedef void *DataHolderHandle; // NOLINT(*) - /** @brief Mini batch used in XGBoost Data Iteration */ typedef struct { // NOLINT(*) /** @brief number of rows in the minibatch */ @@ -391,18 +390,18 @@ typedef struct { // NOLINT(*) #ifdef __APPLE__ /* Necessary as Java on MacOS defines jlong as long int * and gcc defines int64_t as long long int. */ - long* offset; // NOLINT(*) + long *offset; // NOLINT(*) #else - int64_t* offset; // NOLINT(*) + int64_t *offset; // NOLINT(*) #endif // __APPLE__ /** @brief labels of each instance */ - float* label; + float *label; /** @brief weight of each instance, can be NULL */ - float* weight; + float *weight; /** @brief feature index */ - int* index; + int *index; /** @brief feature values */ - float* value; + float *value; } XGBoostBatchCSR; /** @@ -437,12 +436,9 @@ XGB_EXTERN_C typedef int XGBCallbackDataIterNext( // NOLINT(*) * @param out The created DMatrix * @return 0 when success, -1 when failure happens. */ -XGB_DLL int XGDMatrixCreateFromDataIter( - DataIterHandle data_handle, - XGBCallbackDataIterNext* callback, - const char* cache_info, - float missing, - DMatrixHandle *out); +XGB_DLL int XGDMatrixCreateFromDataIter(DataIterHandle data_handle, + XGBCallbackDataIterNext *callback, const char *cache_info, + float missing, DMatrixHandle *out); /** * Second set of callback functions, used by constructing Quantile DMatrix or external @@ -469,7 +465,7 @@ XGB_DLL int XGDMatrixCreateFromDataIter( * * @return 0 when success, -1 when failure happens. */ -XGB_DLL int XGProxyDMatrixCreate(DMatrixHandle* out); +XGB_DLL int XGProxyDMatrixCreate(DMatrixHandle *out); /** * @brief Callback function prototype for getting next batch of data. @@ -483,8 +479,7 @@ XGB_EXTERN_C typedef int XGDMatrixCallbackNext(DataIterHandle iter); // NOLINT( /** * @brief Callback function prototype for resetting the external iterator. */ -XGB_EXTERN_C typedef void DataIterResetCallback(DataIterHandle handle); // NOLINT(*) - +XGB_EXTERN_C typedef void DataIterResetCallback(DataIterHandle handle); // NOLINT(*) /** * @brief Create an external memory DMatrix with data iterator. @@ -503,7 +498,8 @@ XGB_EXTERN_C typedef void DataIterResetCallback(DataIterHandle handle); // NOLIN * @param next Callback function yielding the next batch of data. * @param config JSON encoded parameters for DMatrix construction. Accepted fields are: * - missing: Which value to represent missing value - * - cache_prefix: The path of cache file, caller must initialize all the directories in this path. + * - cache_prefix: The path of cache file, caller must initialize all the directories in this + * path. * - nthread (optional): Number of threads used for initializing DMatrix. * @param[out] out The created external memory DMatrix * @@ -572,7 +568,8 @@ XGB_DLL int XGQuantileDMatrixCreateFromCallback(DataIterHandle iter, DMatrixHand * @param next Callback function yielding the next batch of data. * @param config JSON encoded parameters for DMatrix construction. Accepted fields are: * - missing: Which value to represent missing value - * - cache_prefix: The path of cache file, caller must initialize all the directories in this path. + * - cache_prefix: The path of cache file, caller must initialize all the directories in this + * path. * - nthread (optional): Number of threads used for initializing DMatrix. * - max_bin (optional): Maximum number of bins for building histogram. Must be consistent with * the corresponding booster training parameter. @@ -653,9 +650,8 @@ XGB_DLL int XGProxyDMatrixSetDataDense(DMatrixHandle handle, char const *data); * * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGProxyDMatrixSetDataCSR(DMatrixHandle handle, char const *indptr, - char const *indices, char const *data, - bst_ulong ncol); +XGB_DLL int XGProxyDMatrixSetDataCSR(DMatrixHandle handle, char const *indptr, char const *indices, + char const *data, bst_ulong ncol); /** @} */ // End of Streaming @@ -667,9 +663,7 @@ XGB_DLL int XGProxyDMatrixSetDataCSR(DMatrixHandle handle, char const *indptr, * @param out a sliced new matrix * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGDMatrixSliceDMatrix(DMatrixHandle handle, - const int *idxset, - bst_ulong len, +XGB_DLL int XGDMatrixSliceDMatrix(DMatrixHandle handle, const int *idxset, bst_ulong len, DMatrixHandle *out); /** * @brief create a new dmatrix from sliced content of existing matrix @@ -680,11 +674,8 @@ XGB_DLL int XGDMatrixSliceDMatrix(DMatrixHandle handle, * @param allow_groups allow slicing of an array with groups * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGDMatrixSliceDMatrixEx(DMatrixHandle handle, - const int *idxset, - bst_ulong len, - DMatrixHandle *out, - int allow_groups); +XGB_DLL int XGDMatrixSliceDMatrixEx(DMatrixHandle handle, const int *idxset, bst_ulong len, + DMatrixHandle *out, int allow_groups); /** * @brief Free a DMatrix object. * @@ -705,8 +696,7 @@ XGB_DLL int XGDMatrixFree(DMatrixHandle handle); * * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGDMatrixSaveBinary(DMatrixHandle handle, - const char *fname, int silent); +XGB_DLL int XGDMatrixSaveBinary(DMatrixHandle handle, const char *fname, int silent); /** * @brief Set content in array interface to a content in info. @@ -765,8 +755,7 @@ XGB_DLL int XGDMatrixSetUIntInfo(DMatrixHandle handle, const char *field, const * @endcode */ XGB_DLL int XGDMatrixSetStrFeatureInfo(DMatrixHandle handle, const char *field, - const char **features, - const bst_ulong size); + const char **features, const bst_ulong size); /** * @brief Get string encoded information of all features. @@ -803,8 +792,7 @@ XGB_DLL int XGDMatrixSetStrFeatureInfo(DMatrixHandle handle, const char *field, * * @endcode */ -XGB_DLL int XGDMatrixGetStrFeatureInfo(DMatrixHandle handle, const char *field, - bst_ulong *size, +XGB_DLL int XGDMatrixGetStrFeatureInfo(DMatrixHandle handle, const char *field, bst_ulong *size, const char ***out_features); /** @@ -901,9 +889,7 @@ XGB_DLL int XGDMatrixGetFloatInfo(const DMatrixHandle handle, const char *field, * @param out_dptr pointer to the result * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGDMatrixGetUIntInfo(const DMatrixHandle handle, - const char *field, - bst_ulong* out_len, +XGB_DLL int XGDMatrixGetUIntInfo(const DMatrixHandle handle, const char *field, bst_ulong *out_len, const unsigned **out_dptr); /** * @brief Get the number of rows from a DMatrix. @@ -981,7 +967,7 @@ XGB_DLL int XGDMatrixGetDataAsCSR(DMatrixHandle const handle, char const *config * __(cuda_)array_interface__. */ XGB_DLL int XGDMatrixGetQuantileCut(DMatrixHandle const handle, char const *config, - char const **out_indptr, char const **out_data); + char const **out_indptr, char const **out_data); /** @} */ // End of DMatrix @@ -1046,8 +1032,7 @@ XGB_DLL int XGBoosterReset(BoosterHandle handle); * * @return 0 when success, -1 when failure happens, -2 when index is out of bound. */ -XGB_DLL int XGBoosterSlice(BoosterHandle handle, int begin_layer, - int end_layer, int step, +XGB_DLL int XGBoosterSlice(BoosterHandle handle, int begin_layer, int end_layer, int step, BoosterHandle *out); /** @@ -1057,7 +1042,7 @@ XGB_DLL int XGBoosterSlice(BoosterHandle handle, int begin_layer, * @param out Pointer to output integer. * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterBoostedRounds(BoosterHandle handle, int* out); +XGB_DLL int XGBoosterBoostedRounds(BoosterHandle handle, int *out); /** * @brief set parameters @@ -1066,9 +1051,7 @@ XGB_DLL int XGBoosterBoostedRounds(BoosterHandle handle, int* out); * @param value value of parameter * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterSetParam(BoosterHandle handle, - const char *name, - const char *value); +XGB_DLL int XGBoosterSetParam(BoosterHandle handle, const char *name, const char *value); /** * @example c-api-demo.c */ @@ -1157,26 +1140,22 @@ XGB_DLL int XGBoosterEvalOneIter(BoosterHandle handle, int iter, DMatrixHandle d * 1:output margin instead of transformed value * 2:output leaf index of trees instead of leaf value, note leaf index is unique per tree * 4:output feature contributions to individual predictions - * @param ntree_limit limit number of trees used for prediction, this is only valid for boosted trees - * when the parameter is set to 0, we will use all the trees + * @param ntree_limit limit number of trees used for prediction, this is only valid for boosted + * trees when the parameter is set to 0, we will use all the trees * @param training Whether the prediction function is used as part of a training loop. * Prediction can be run in 2 scenarios: * 1. Given data matrix X, obtain prediction y_pred from the model. * 2. Obtain the prediction for computing gradients. For example, DART booster performs dropout - * during training, and the prediction result will be different from the one obtained by normal - * inference step due to dropped trees. - * Set training=false for the first scenario. Set training=true for the second scenario. - * The second scenario applies when you are defining a custom objective function. + * during training, and the prediction result will be different from the one obtained by + * normal inference step due to dropped trees. Set training=false for the first scenario. Set + * training=true for the second scenario. The second scenario applies when you are defining a custom + * objective function. * @param out_len used to store length of returning result * @param out_result used to set a pointer to array * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterPredict(BoosterHandle handle, - DMatrixHandle dmat, - int option_mask, - unsigned ntree_limit, - int training, - bst_ulong *out_len, +XGB_DLL int XGBoosterPredict(BoosterHandle handle, DMatrixHandle dmat, int option_mask, + unsigned ntree_limit, int training, bst_ulong *out_len, const float **out_result); /** @@ -1201,21 +1180,17 @@ XGB_DLL int XGBoosterPredict(BoosterHandle handle, * * Prediction can be run in 2 scenarios: * 1. Given data matrix X, obtain prediction y_pred from the model. - * 2. Obtain the prediction for computing gradients. For example, DART booster performs dropout - * during training, and the prediction result will be different from the one obtained by normal - * inference step due to dropped trees. - * Set training=false for the first scenario. Set training=true for the second - * scenario. The second scenario applies when you are defining a custom objective - * function. - * "iteration_begin": int - * Beginning iteration of prediction. + * 2. Obtain the prediction for computing gradients. For example, DART booster performs + * dropout during training, and the prediction result will be different from the one obtained by + * normal inference step due to dropped trees. Set training=false for the first scenario. Set + * training=true for the second scenario. The second scenario applies when you are defining a + * custom objective function. "iteration_begin": int Beginning iteration of prediction. * "iteration_end": int - * End iteration of prediction. Set to 0 this will become the size of tree model (all the trees). - * "strict_shape": bool - * Whether should we reshape the output with stricter rules. If set to true, - * normal/margin/contrib/interaction predict will output consistent shape - * disregarding the use of multi-class model, and leaf prediction will output 4-dim - * array representing: (n_samples, n_iterations, n_classes, n_trees_in_forest) + * End iteration of prediction. Set to 0 this will become the size of tree model (all the + * trees). "strict_shape": bool Whether should we reshape the output with stricter rules. If set to + * true, normal/margin/contrib/interaction predict will output consistent shape disregarding the use + * of multi-class model, and leaf prediction will output 4-dim array representing: (n_samples, + * n_iterations, n_classes, n_trees_in_forest) * * Example JSON input for running a normal prediction with strict output shape, 2 dim * for softprob , 1 dim for others. @@ -1235,7 +1210,8 @@ XGB_DLL int XGBoosterPredict(BoosterHandle handle, * * @return 0 when success, -1 when failure happens * - * @see XGBoosterPredictFromDense XGBoosterPredictFromCSR XGBoosterPredictFromCudaArray XGBoosterPredictFromCudaColumnar + * @see XGBoosterPredictFromDense XGBoosterPredictFromCSR XGBoosterPredictFromCudaArray + * XGBoosterPredictFromCudaColumnar */ XGB_DLL int XGBoosterPredictFromDMatrix(BoosterHandle handle, DMatrixHandle dmat, char const *config, bst_ulong const **out_shape, @@ -1374,7 +1350,6 @@ XGB_DLL int XGBoosterPredictFromCudaColumnar(BoosterHandle handle, char const *d /**@}*/ // End of Prediction - /** * @defgroup Serialization Serialization * @ingroup Booster @@ -1409,8 +1384,7 @@ XGB_DLL int XGBoosterPredictFromCudaColumnar(BoosterHandle handle, char const *d * * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterLoadModel(BoosterHandle handle, - const char *fname); +XGB_DLL int XGBoosterLoadModel(BoosterHandle handle, const char *fname); /** * @brief Save the model into an existing file * @@ -1419,8 +1393,7 @@ XGB_DLL int XGBoosterLoadModel(BoosterHandle handle, * * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterSaveModel(BoosterHandle handle, - const char *fname); +XGB_DLL int XGBoosterSaveModel(BoosterHandle handle, const char *fname); /** * @brief load model from in memory buffer * @@ -1429,9 +1402,7 @@ XGB_DLL int XGBoosterSaveModel(BoosterHandle handle, * @param len the length of the buffer * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterLoadModelFromBuffer(BoosterHandle handle, - const void *buf, - bst_ulong len); +XGB_DLL int XGBoosterLoadModelFromBuffer(BoosterHandle handle, const void *buf, bst_ulong len); /** * @brief Save model into raw bytes, return header of the array. User must copy the @@ -1472,8 +1443,7 @@ XGB_DLL int XGBoosterSerializeToBuffer(BoosterHandle handle, bst_ulong *out_len, * @param len the length of the buffer * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterUnserializeFromBuffer(BoosterHandle handle, - const void *buf, bst_ulong len); +XGB_DLL int XGBoosterUnserializeFromBuffer(BoosterHandle handle, const void *buf, bst_ulong len); /** * @brief Save XGBoost's internal configuration into a JSON document. Currently the @@ -1487,8 +1457,7 @@ XGB_DLL int XGBoosterUnserializeFromBuffer(BoosterHandle handle, * be managed by caller. * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterSaveJsonConfig(BoosterHandle handle, bst_ulong *out_len, - char const **out_str); +XGB_DLL int XGBoosterSaveJsonConfig(BoosterHandle handle, bst_ulong *out_len, char const **out_str); /** * @brief Load XGBoost's internal configuration from a JSON document. Currently the * support is experimental, function signature may change in the future without @@ -1510,11 +1479,8 @@ XGB_DLL int XGBoosterLoadJsonConfig(BoosterHandle handle, char const *config); * @param out_dump_array pointer to hold representing dump of each model * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterDumpModel(BoosterHandle handle, - const char *fmap, - int with_stats, - bst_ulong *out_len, - const char ***out_dump_array); +XGB_DLL int XGBoosterDumpModel(BoosterHandle handle, const char *fmap, int with_stats, + bst_ulong *out_len, const char ***out_dump_array); /** * @brief dump model, return array of strings representing model dump @@ -1526,11 +1492,8 @@ XGB_DLL int XGBoosterDumpModel(BoosterHandle handle, * @param out_dump_array pointer to hold representing dump of each model * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterDumpModelEx(BoosterHandle handle, - const char *fmap, - int with_stats, - const char *format, - bst_ulong *out_len, +XGB_DLL int XGBoosterDumpModelEx(BoosterHandle handle, const char *fmap, int with_stats, + const char *format, bst_ulong *out_len, const char ***out_dump_array); /** @@ -1544,12 +1507,8 @@ XGB_DLL int XGBoosterDumpModelEx(BoosterHandle handle, * @param out_models pointer to hold representing dump of each model * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterDumpModelWithFeatures(BoosterHandle handle, - int fnum, - const char **fname, - const char **ftype, - int with_stats, - bst_ulong *out_len, +XGB_DLL int XGBoosterDumpModelWithFeatures(BoosterHandle handle, int fnum, const char **fname, + const char **ftype, int with_stats, bst_ulong *out_len, const char ***out_models); /** @@ -1564,14 +1523,9 @@ XGB_DLL int XGBoosterDumpModelWithFeatures(BoosterHandle handle, * @param out_models pointer to hold representing dump of each model * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterDumpModelExWithFeatures(BoosterHandle handle, - int fnum, - const char **fname, - const char **ftype, - int with_stats, - const char *format, - bst_ulong *out_len, - const char ***out_models); +XGB_DLL int XGBoosterDumpModelExWithFeatures(BoosterHandle handle, int fnum, const char **fname, + const char **ftype, int with_stats, const char *format, + bst_ulong *out_len, const char ***out_models); /** * See @ref XGDMatrixGetCategories @@ -1600,10 +1554,7 @@ XGB_DLL int XGBoosterGetCategoriesExportToArrow(BoosterHandle handle, char const * @param success Whether the result is contained in out. * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterGetAttr(BoosterHandle handle, - const char* key, - const char** out, - int *success); +XGB_DLL int XGBoosterGetAttr(BoosterHandle handle, const char *key, const char **out, int *success); /** * @brief Set or delete string attribute. * @@ -1613,9 +1564,7 @@ XGB_DLL int XGBoosterGetAttr(BoosterHandle handle, * If nullptr, the attribute would be deleted. * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterSetAttr(BoosterHandle handle, - const char* key, - const char* value); +XGB_DLL int XGBoosterSetAttr(BoosterHandle handle, const char *key, const char *value); /** * @brief Get the names of all attribute from Booster. * @param handle handle @@ -1623,9 +1572,7 @@ XGB_DLL int XGBoosterSetAttr(BoosterHandle handle, * @param out pointer to hold the output attribute stings * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterGetAttrNames(BoosterHandle handle, - bst_ulong* out_len, - const char*** out); +XGB_DLL int XGBoosterGetAttrNames(BoosterHandle handle, bst_ulong *out_len, const char ***out); /** * @brief Set string encoded feature info in Booster, similar to the feature @@ -1643,8 +1590,7 @@ XGB_DLL int XGBoosterGetAttrNames(BoosterHandle handle, * @return 0 when success, -1 when failure happens */ XGB_DLL int XGBoosterSetStrFeatureInfo(BoosterHandle handle, const char *field, - const char **features, - const bst_ulong size); + const char **features, const bst_ulong size); /** * @brief Get string encoded feature info from Booster, similar to the feature info @@ -1665,8 +1611,7 @@ XGB_DLL int XGBoosterSetStrFeatureInfo(BoosterHandle handle, const char *field, * * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGBoosterGetStrFeatureInfo(BoosterHandle handle, const char *field, - bst_ulong *len, +XGB_DLL int XGBoosterGetStrFeatureInfo(BoosterHandle handle, const char *field, bst_ulong *len, const char ***out_features); /** @@ -1850,7 +1795,7 @@ XGB_DLL int XGTrackerFree(TrackerHandle handle); * * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGCommunicatorInit(char const* config); +XGB_DLL int XGCommunicatorInit(char const *config); /** * @brief Finalize the collective communicator. @@ -1899,7 +1844,7 @@ XGB_DLL int XGCommunicatorPrint(char const *message); * @param name_str Pointer to received returned processor name. * @return 0 when success, -1 when failure happens */ -XGB_DLL int XGCommunicatorGetProcessorName(const char** name_str); +XGB_DLL int XGCommunicatorGetProcessorName(const char **name_str); /** * @brief Broadcast a memory region to all others from root. This function is NOT @@ -1943,4 +1888,4 @@ XGB_DLL int XGCommunicatorBroadcast(void *send_receive_buffer, size_t size, int XGB_DLL int XGCommunicatorAllreduce(void *send_receive_buffer, size_t count, int data_type, int op); /**@}*/ // End of Collective -#endif // XGBOOST_C_API_H_ +#endif // XGBOOST_C_API_H_ diff --git a/include/xgboost/cache.h b/include/xgboost/cache.h index 32e1b21ac3f6..09a9978688f8 100644 --- a/include/xgboost/cache.h +++ b/include/xgboost/cache.h @@ -6,14 +6,14 @@ #include // for CHECK_EQ, CHECK -#include // for size_t -#include // for weak_ptr, shared_ptr, make_shared -#include // for mutex, lock_guard -#include // for queue -#include // for thread -#include // for unordered_map -#include // for move -#include // for vector +#include // for size_t +#include // for weak_ptr, shared_ptr, make_shared +#include // for mutex, lock_guard +#include // for queue +#include // for thread +#include // for unordered_map +#include // for move +#include // for vector namespace xgboost { class DMatrix; diff --git a/include/xgboost/collective/poll_utils.h b/include/xgboost/collective/poll_utils.h index 0e012d2b583d..ef2ef112bfed 100644 --- a/include/xgboost/collective/poll_utils.h +++ b/include/xgboost/collective/poll_utils.h @@ -39,7 +39,7 @@ using SOCKET = int; using sock_size_t = size_t; // NOLINT -#endif // !defined(_WIN32) +#endif // !defined(_WIN32) #define IS_MINGW() defined(__MINGW32__) @@ -61,15 +61,15 @@ using sock_size_t = size_t; // NOLINT #pragma message("Distributed training on mingw is not supported.") typedef struct pollfd { SOCKET fd; - short events; // NOLINT - short revents; // NOLINT + short events; // NOLINT + short revents; // NOLINT } WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD; // POLLRDNORM | POLLRDBAND -#define POLLIN (0x0100 | 0x0200) -#define POLLPRI 0x0400 +#define POLLIN (0x0100 | 0x0200) +#define POLLPRI 0x0400 // POLLWRNORM -#define POLLOUT 0x0010 +#define POLLOUT 0x0010 #endif // IS_MINGW() && !defined(POLLRDNORM) && !defined(POLLRDBAND) @@ -139,7 +139,7 @@ struct PollHelper { pfd.fd = fd; pfd.events |= POLLIN; } - void WatchRead(xgboost::collective::TCPSocket const &socket) { this->WatchRead(socket.Handle()); } + void WatchRead(xgboost::collective::TCPSocket const& socket) { this->WatchRead(socket.Handle()); } /*! * \brief add file descriptor to watch for write @@ -150,7 +150,7 @@ struct PollHelper { pfd.fd = fd; pfd.events |= POLLOUT; } - void WatchWrite(xgboost::collective::TCPSocket const &socket) { + void WatchWrite(xgboost::collective::TCPSocket const& socket) { this->WatchWrite(socket.Handle()); } @@ -163,7 +163,7 @@ struct PollHelper { pfd.fd = fd; pfd.events |= POLLPRI; } - void WatchException(xgboost::collective::TCPSocket const &socket) { + void WatchException(xgboost::collective::TCPSocket const& socket) { this->WatchException(socket.Handle()); } /*! diff --git a/include/xgboost/collective/socket.h b/include/xgboost/collective/socket.h index f0a21dd1bb06..85e695cfdd0f 100644 --- a/include/xgboost/collective/socket.h +++ b/include/xgboost/collective/socket.h @@ -45,9 +45,9 @@ using ssize_t = int; #if defined(__sun) || defined(sun) #include -#endif // defined(__sun) || defined(sun) +#endif // defined(__sun) || defined(sun) -#endif // defined(_WIN32) +#endif // defined(_WIN32) #include "xgboost/base.h" // XGBOOST_EXPECT #include "xgboost/collective/result.h" // for Result @@ -546,7 +546,7 @@ class TCPSocket { /** * @brief Bind socket to INADDR_ANY, return the port selected by the OS. */ - [[nodiscard]] Result BindHost(std::int32_t* p_out) { + [[nodiscard]] Result BindHost(std::int32_t *p_out) { // Use int32 instead of in_port_t for consistency. We take port as parameter from // users using other languages, the port is usually stored and passed around as int. if (Domain() == SockDomain::kV6) { diff --git a/include/xgboost/context.h b/include/xgboost/context.h index 5a1c58042b24..0cb874ebc65f 100644 --- a/include/xgboost/context.h +++ b/include/xgboost/context.h @@ -36,8 +36,13 @@ struct DeviceOrd { static bst_d_ordinal_t constexpr CPUOrdinal() { return -1; } static bst_d_ordinal_t constexpr InvalidOrdinal() { return -2; } - enum Type : std::int16_t { kCPU = 0, kCUDA = 1, - kSyclDefault = 2, kSyclCPU = 3, kSyclGPU = 4} device{kCPU}; + enum Type : std::int16_t { + kCPU = 0, + kCUDA = 1, + kSyclDefault = 2, + kSyclCPU = 3, + kSyclGPU = 4 + } device{kCPU}; // CUDA or Sycl device ordinal. bst_d_ordinal_t ordinal{CPUOrdinal()}; @@ -46,9 +51,7 @@ struct DeviceOrd { [[nodiscard]] bool IsSyclDefault() const { return device == kSyclDefault; } [[nodiscard]] bool IsSyclCPU() const { return device == kSyclCPU; } [[nodiscard]] bool IsSyclGPU() const { return device == kSyclGPU; } - [[nodiscard]] bool IsSycl() const { return (IsSyclDefault() || - IsSyclCPU() || - IsSyclGPU()); } + [[nodiscard]] bool IsSycl() const { return (IsSyclDefault() || IsSyclCPU() || IsSyclGPU()); } constexpr DeviceOrd() = default; constexpr DeviceOrd(Type type, bst_d_ordinal_t ord) : device{type}, ordinal{ord} {} @@ -190,19 +193,17 @@ struct Context : public XGBoostParameter { /** * @brief Is XGBoost running on any SYCL device? */ - [[nodiscard]] bool IsSycl() const { return IsSyclDefault() - || IsSyclCPU() - || IsSyclGPU(); } + [[nodiscard]] bool IsSycl() const { return IsSyclDefault() || IsSyclCPU() || IsSyclGPU(); } /** * @brief Get the current device and ordinal. */ [[nodiscard]] DeviceOrd Device() const { return device_; } - /** - * @brief Get the current device and ordinal, if it supports fp64, - otherwise returns default CPU - */ + /** + * @brief Get the current device and ordinal, if it supports fp64, + otherwise returns default CPU + */ [[nodiscard]] DeviceOrd DeviceFP64() const; /** diff --git a/include/xgboost/data.h b/include/xgboost/data.h index d3bc2074ad15..a1c985d2d1dd 100644 --- a/include/xgboost/data.h +++ b/include/xgboost/data.h @@ -30,13 +30,7 @@ class DMatrix; struct Context; /*! \brief data type accepted by xgboost interface */ -enum class DataType : uint8_t { - kFloat32 = 1, - kDouble = 2, - kUInt32 = 3, - kUInt64 = 4, - kStr = 5 -}; +enum class DataType : uint8_t { kFloat32 = 1, kDouble = 2, kUInt32 = 3, kUInt64 = 4, kStr = 5 }; enum class FeatureType : uint8_t { kNumerical = 0, kCategorical = 1 }; @@ -134,7 +128,7 @@ class MetaInfo { * \return The weight. */ inline bst_float GetWeight(size_t i) const { - return weights_.Size() != 0 ? weights_.HostVector()[i] : 1.0f; + return weights_.Size() != 0 ? weights_.HostVector()[i] : 1.0f; } /*! \brief get sorted indexes (argsort) of labels by absolute value (used by cox loss) */ const std::vector& LabelAbsSort(Context const* ctx) const; @@ -157,11 +151,10 @@ class MetaInfo { */ void SetInfo(Context const& ctx, StringView key, StringView interface_str); - void GetInfo(char const* key, bst_ulong* out_len, DataType dtype, - const void** out_dptr) const; + void GetInfo(char const* key, bst_ulong* out_len, DataType dtype, const void** out_dptr) const; - void SetFeatureInfo(const char *key, const char **info, const bst_ulong size); - void GetFeatureInfo(const char *field, std::vector* out_str_vecs) const; + void SetFeatureInfo(const char* key, const char** info, const bst_ulong size); + void GetFeatureInfo(const char* field, std::vector* out_str_vecs) const; /** * @brief Extend with other MetaInfo. @@ -213,7 +206,7 @@ class MetaInfo { */ [[nodiscard]] CatContainer const* Cats() const; [[nodiscard]] CatContainer* Cats(); - [[nodiscard]] std::shared_ptr CatsShared() const; + [[nodiscard]] std::shared_ptr CatsShared() const; /** * @brief Setter for categories. */ @@ -245,12 +238,8 @@ struct Entry { */ XGBOOST_DEVICE Entry(bst_feature_t index, bst_float fvalue) : index(index), fvalue(fvalue) {} /*! \brief reversely compare feature values */ - inline static bool CmpValue(const Entry& a, const Entry& b) { - return a.fvalue < b.fvalue; - } - static bool CmpIndex(Entry const& a, Entry const& b) { - return a.index < b.index; - } + inline static bool CmpValue(const Entry& a, const Entry& b) { return a.fvalue < b.fvalue; } + static bool CmpIndex(Entry const& a, Entry const& b) { return a.index < b.index; } inline bool operator==(const Entry& other) const { return (this->index == other.index && this->fvalue == other.fvalue); } @@ -358,7 +347,7 @@ class SparsePage { /*! \brief the data of the segments */ HostDeviceVector data; - size_t base_rowid {0}; + size_t base_rowid{0}; /*! \brief an instance of sparse vector in the batch */ using Inst = common::Span; @@ -368,9 +357,7 @@ class SparsePage { } /*! \brief constructor */ - SparsePage() { - this->Clear(); - } + SparsePage() { this->Clear(); } SparsePage(SparsePage const& that) = delete; SparsePage(SparsePage&& that) = default; @@ -379,9 +366,7 @@ class SparsePage { virtual ~SparsePage() = default; /*! \return Number of instances in the page. */ - [[nodiscard]] size_t Size() const { - return offset.Size() == 0 ? 0 : offset.Size() - 1; - } + [[nodiscard]] size_t Size() const { return offset.Size() == 0 ? 0 : offset.Size() - 1; } /*! \return estimation of memory cost of this page */ [[nodiscard]] size_t MemCostBytes() const { @@ -398,9 +383,7 @@ class SparsePage { } /*! \brief Set the base row id for this page. */ - inline void SetBaseRowId(size_t row_id) { - base_rowid = row_id; - } + inline void SetBaseRowId(size_t row_id) { base_rowid = row_id; } [[nodiscard]] SparsePage GetTranspose(int num_columns, int32_t n_threads) const; @@ -427,7 +410,8 @@ class SparsePage { * \param missing * \param nthread * - * \return The maximum number of columns encountered in this input batch. Useful when pushing many adapter batches to work out the total number of columns. + * \return The maximum number of columns encountered in this input batch. Useful when pushing + * many adapter batches to work out the total number of columns. */ template bst_idx_t Push(AdapterBatchT const& batch, float missing, std::int32_t nthread); @@ -436,7 +420,7 @@ class SparsePage { * \brief Push a sparse page * \param batch the row page */ - void Push(const SparsePage &batch); + void Push(const SparsePage& batch); /*! * \brief Push a SparsePage stored in CSC format * \param batch The row batch to be pushed @@ -444,7 +428,7 @@ class SparsePage { void PushCSC(const SparsePage& batch); }; -class CSCPage: public SparsePage { +class CSCPage : public SparsePage { public: CSCPage() : SparsePage() {} explicit CSCPage(SparsePage page) : SparsePage(std::move(page)) {} @@ -469,7 +453,7 @@ class SortedCSCPage : public SparsePage { class EllpackPage; class GHistIndexMatrix; -template +template class BatchIteratorImpl { public: using iterator_category = std::forward_iterator_tag; // NOLINT @@ -480,14 +464,14 @@ class BatchIteratorImpl { virtual std::shared_ptr Page() const = 0; }; -template +template class BatchIterator { public: using iterator_category = std::forward_iterator_tag; // NOLINT explicit BatchIterator(BatchIteratorImpl* impl) { impl_.reset(impl); } explicit BatchIterator(std::shared_ptr> impl) { impl_ = impl; } - BatchIterator &operator++() { + BatchIterator& operator++() { CHECK(impl_ != nullptr); ++(*impl_); return *this; @@ -505,19 +489,17 @@ class BatchIterator { return impl_->AtEnd(); } - [[nodiscard]] std::shared_ptr Page() const { - return impl_->Page(); - } + [[nodiscard]] std::shared_ptr Page() const { return impl_->Page(); } private: std::shared_ptr> impl_; }; -template +template class BatchSet { public: explicit BatchSet(BatchIterator begin_iter) : begin_iter_(std::move(begin_iter)) {} - BatchIterator begin() { return begin_iter_; } // NOLINT + BatchIterator begin() { return begin_iter_; } // NOLINT BatchIterator end() { return BatchIterator(nullptr); } // NOLINT private: @@ -573,7 +555,7 @@ struct ExtMemConfig { class DMatrix { public: /*! \brief default constructor */ - DMatrix() = default; + DMatrix() = default; /** @brief meta information of the dataset */ [[nodiscard]] virtual MetaInfo& Info() = 0; virtual void SetInfo(const char* key, std::string const& interface_str) { @@ -706,7 +688,7 @@ class DMatrix { bst_bin_t max_bin, std::int64_t max_quantile_blocks, ExtMemConfig const& config); - virtual DMatrix *Slice(common::Span ridxs) = 0; + virtual DMatrix* Slice(common::Span ridxs) = 0; /** * @brief Slice a DMatrix by columns. diff --git a/include/xgboost/feature_map.h b/include/xgboost/feature_map.h index 43c52ed227c8..8b786d7abf9d 100644 --- a/include/xgboost/feature_map.h +++ b/include/xgboost/feature_map.h @@ -9,10 +9,10 @@ #include -#include -#include #include #include +#include +#include namespace xgboost { /*! @@ -22,18 +22,12 @@ namespace xgboost { class FeatureMap { public: /*! \brief type of feature maps */ - enum Type { - kIndicator = 0, - kQuantitive = 1, - kInteger = 2, - kFloat = 3, - kCategorical = 4 - }; + enum Type { kIndicator = 0, kQuantitive = 1, kInteger = 2, kFloat = 3, kCategorical = 4 }; /*! * \brief load feature map from input stream * \param is Input text stream */ - inline void LoadText(std::istream& is) { // NOLINT(*) + inline void LoadText(std::istream& is) { // NOLINT(*) int fid; std::string fname, ftype; while (is >> fid >> fname >> ftype) { @@ -46,7 +40,7 @@ class FeatureMap { * \param fname The feature name. * \param ftype The feature type. */ - inline void PushBack(int fid, const char *fname, const char *ftype) { + inline void PushBack(int fid, const char* fname, const char* ftype) { CHECK_EQ(fid, static_cast(names_.size())); names_.emplace_back(fname); types_.push_back(GetType(ftype)); @@ -57,12 +51,10 @@ class FeatureMap { types_.clear(); } /*! \return number of known features */ - inline size_t Size() const { - return names_.size(); - } + inline size_t Size() const { return names_.size(); } /*! \return name of specific feature */ inline const char* Name(size_t idx) const { - CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound"; + CHECK_LT(idx, names_.size()) << "FeatureMap feature index exceed bound"; return names_[idx].c_str(); } /*! \return type of specific feature */ diff --git a/include/xgboost/gbm.h b/include/xgboost/gbm.h index 65940773ffee..f9472c761865 100644 --- a/include/xgboost/gbm.h +++ b/include/xgboost/gbm.h @@ -116,8 +116,7 @@ class GradientBooster : public Model, public Configurable { * \param layer_begin Beginning of boosted tree layer used for prediction. * \param layer_end End of booster layer. 0 means do not limit trees. */ - virtual void PredictLeaf(DMatrix *dmat, - HostDeviceVector *out_preds, + virtual void PredictLeaf(DMatrix* dmat, HostDeviceVector* out_preds, unsigned layer_begin, unsigned layer_end) = 0; /*! @@ -147,8 +146,7 @@ class GradientBooster : public Model, public Configurable { [[nodiscard]] virtual std::vector DumpModel(const FeatureMap& fmap, bool with_stats, std::string format) const = 0; - virtual void FeatureScore(std::string const& importance_type, - common::Span trees, + virtual void FeatureScore(std::string const& importance_type, common::Span trees, std::vector* features, std::vector* scores) const = 0; /** @@ -190,10 +188,10 @@ struct GradientBoosterReg * }); * \endcode */ -#define XGBOOST_REGISTER_GBM(UniqueId, Name) \ - static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg & \ - __make_ ## GradientBoosterReg ## _ ## UniqueId ## __ = \ - ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name) +#define XGBOOST_REGISTER_GBM(UniqueId, Name) \ + static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg& \ + __make_##GradientBoosterReg##_##UniqueId##__ = \ + ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name) } // namespace xgboost #endif // XGBOOST_GBM_H_ diff --git a/include/xgboost/host_device_vector.h b/include/xgboost/host_device_vector.h index 1a0da50fa07a..7e3293310bd7 100644 --- a/include/xgboost/host_device_vector.h +++ b/include/xgboost/host_device_vector.h @@ -64,7 +64,8 @@ namespace xgboost { void SetCudaSetDeviceHandler(void (*handler)(int)); #endif // __CUDACC__ -template struct HostDeviceVectorImpl; +template +struct HostDeviceVectorImpl; /*! * \brief Controls data access from the GPU. @@ -78,7 +79,8 @@ template struct HostDeviceVectorImpl; * - Data is being manipulated on the host. Host has write access, device doesn't have access. */ enum GPUAccess { - kNone, kRead, + kNone, + kRead, // write implies read kWrite }; @@ -126,7 +128,7 @@ class HostDeviceVector { std::vector& HostVector(); const std::vector& ConstHostVector() const; - const std::vector& HostVector() const {return ConstHostVector(); } + const std::vector& HostVector() const { return ConstHostVector(); } [[nodiscard]] bool HostCanRead() const; [[nodiscard]] bool HostCanWrite() const; diff --git a/include/xgboost/intrusive_ptr.h b/include/xgboost/intrusive_ptr.h index a0b860be55f8..da9ff51f8017 100644 --- a/include/xgboost/intrusive_ptr.h +++ b/include/xgboost/intrusive_ptr.h @@ -19,15 +19,12 @@ namespace xgboost { */ class IntrusivePtrCell { private: - std::atomic count_ {0}; - template friend class IntrusivePtr; + std::atomic count_{0}; + template + friend class IntrusivePtr; - std::int32_t IncRef() noexcept { - return count_.fetch_add(1, std::memory_order_relaxed); - } - std::int32_t DecRef() noexcept { - return count_.fetch_sub(1, std::memory_order_release); - } + std::int32_t IncRef() noexcept { return count_.fetch_add(1, std::memory_order_relaxed); } + std::int32_t DecRef() noexcept { return count_.fetch_sub(1, std::memory_order_release); } bool IsZero() const { return Count() == 0; } public: @@ -38,7 +35,8 @@ class IntrusivePtrCell { /*! * \brief User defined function for returning embedded reference count. */ -template IntrusivePtrCell &IntrusivePtrRefCount(T const *ptr) noexcept; +template +IntrusivePtrCell &IntrusivePtrRefCount(T const *ptr) noexcept; /*! * \brief Implementation of Intrusive Pointer. A smart pointer that points to an object @@ -70,7 +68,8 @@ template IntrusivePtrCell &IntrusivePtrRefCount(T const *ptr) noexc * * \endcode */ -template class IntrusivePtr { +template +class IntrusivePtr { private: void IncRef(T *ptr) { if (ptr) { @@ -178,12 +177,12 @@ bool operator!=(T *x, IntrusivePtr const &y) noexcept { template bool operator<(IntrusivePtr const &x, IntrusivePtr const &y) noexcept { - return std::less{}(x.get(), y.get()); + return std::less{}(x.get(), y.get()); } template bool operator<=(IntrusivePtr const &x, IntrusivePtr const &y) noexcept { - return std::less_equal{}(x.get(), y.get()); + return std::less_equal{}(x.get(), y.get()); } template @@ -197,8 +196,7 @@ bool operator>=(IntrusivePtr const &x, IntrusivePtr const &y) noexcept { } template -std::basic_ostream &operator<<(std::basic_ostream &os, - IntrusivePtr const &p) { +std::basic_ostream &operator<<(std::basic_ostream &os, IntrusivePtr const &p) { os << p.get(); return os; } @@ -213,5 +211,5 @@ void swap(xgboost::IntrusivePtr &x, // NOLINT template struct hash> : public xgboost::IntrusivePtr::Hash {}; -} // namespace std +} // namespace std #endif // XGBOOST_INTRUSIVE_PTR_H_ diff --git a/include/xgboost/json.h b/include/xgboost/json.h index 14db1ad13c20..f80766b7ef1f 100644 --- a/include/xgboost/json.h +++ b/include/xgboost/json.h @@ -95,26 +95,24 @@ class JsonString : public Value { public: JsonString() : Value(ValueKind::kString) {} - JsonString(std::string const& str) : // NOLINT - Value(ValueKind::kString), str_{str} {} - JsonString(std::string&& str) noexcept : // NOLINT - Value(ValueKind::kString), str_{std::forward(str)} {} + JsonString(std::string const& str) // NOLINT + : Value(ValueKind::kString), str_{str} {} + JsonString(std::string&& str) noexcept // NOLINT + : Value(ValueKind::kString), str_{std::forward(str)} {} JsonString(JsonString&& str) noexcept : Value(ValueKind::kString) { // NOLINT std::swap(str.str_, this->str_); } void Save(JsonWriter* writer) const override; - std::string const& GetString() && { return str_; } - std::string const& GetString() const & { return str_; } - std::string& GetString() & { return str_; } + std::string const& GetString() && { return str_; } + std::string const& GetString() const& { return str_; } + std::string& GetString() & { return str_; } bool operator==(Value const& rhs) const override; Value& operator=(Value const& rhs) override = delete; - static bool IsClassOf(Value const* value) { - return value->Type() == ValueKind::kString; - } + static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kString; } }; class JsonArray : public Value { @@ -124,10 +122,10 @@ class JsonArray : public Value { JsonArray() : Value(ValueKind::kArray) {} JsonArray(std::vector&& arr) noexcept // NOLINT : Value(ValueKind::kArray), vec_{std::forward>(arr)} {} - JsonArray(std::vector const& arr) : // NOLINT - Value(ValueKind::kArray), vec_{arr} {} + JsonArray(std::vector const& arr) // NOLINT + : Value(ValueKind::kArray), vec_{arr} {} JsonArray(JsonArray const& that) = delete; - JsonArray(JsonArray && that) noexcept; + JsonArray(JsonArray&& that) noexcept; void Save(JsonWriter* writer) const override; @@ -135,16 +133,14 @@ class JsonArray : public Value { // silent the partial oveeridden warning Json& operator[](std::string const& key) override { return Value::operator[](key); } - std::vector const& GetArray() && { return vec_; } - std::vector const& GetArray() const & { return vec_; } - std::vector& GetArray() & { return vec_; } + std::vector const& GetArray() && { return vec_; } + std::vector const& GetArray() const& { return vec_; } + std::vector& GetArray() & { return vec_; } bool operator==(Value const& rhs) const override; Value& operator=(Value const& rhs) override = delete; - static bool IsClassOf(Value const* value) { - return value->Type() == ValueKind::kArray; - } + static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kArray; } }; /** @@ -263,7 +259,7 @@ class JsonNumber : public Value { using Float = float; private: - Float number_ { 0 }; + Float number_{0}; public: JsonNumber() : Value(ValueKind::kNumber) {} @@ -277,16 +273,14 @@ class JsonNumber : public Value { void Save(JsonWriter* writer) const override; - Float const& GetNumber() && { return number_; } - Float const& GetNumber() const & { return number_; } - Float& GetNumber() & { return number_; } + Float const& GetNumber() && { return number_; } + Float const& GetNumber() const& { return number_; } + Float& GetNumber() & { return number_; } bool operator==(Value const& rhs) const override; Value& operator=(Value const& rhs) override = delete; - static bool IsClassOf(Value const* value) { - return value->Type() == ValueKind::kNumber; - } + static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kNumber; } }; namespace detail { @@ -295,13 +289,12 @@ using Not32SizeT = std::enable_if_t && !std::is_same_v>; } - class JsonInteger : public Value { public: using Int = int64_t; private: - Int integer_ {0}; + Int integer_{0}; public: JsonInteger() : Value(ValueKind::kInteger) {} // NOLINT @@ -318,20 +311,17 @@ class JsonInteger : public Value { JsonInteger(IntT value) // NOLINT : Value(ValueKind::kInteger), integer_{static_cast(value)} {} - JsonInteger(JsonInteger &&that) noexcept - : Value{ValueKind::kInteger}, integer_{that.integer_} {} + JsonInteger(JsonInteger&& that) noexcept : Value{ValueKind::kInteger}, integer_{that.integer_} {} bool operator==(Value const& rhs) const override; Value& operator=(Value const& rhs) override = delete; - Int const& GetInteger() && { return integer_; } - Int const& GetInteger() const & { return integer_; } - Int& GetInteger() & { return integer_; } + Int const& GetInteger() && { return integer_; } + Int const& GetInteger() const& { return integer_; } + Int& GetInteger() & { return integer_; } void Save(JsonWriter* writer) const override; - static bool IsClassOf(Value const* value) { - return value->Type() == ValueKind::kInteger; - } + static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kInteger; } }; class JsonNull : public Value { @@ -345,9 +335,7 @@ class JsonNull : public Value { bool operator==(Value const& rhs) const override; Value& operator=(Value const& rhs) override = delete; - static bool IsClassOf(Value const* value) { - return value->Type() == ValueKind::kNull; - } + static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kNull; } }; /*! \brief Describes both true and false. */ @@ -359,21 +347,21 @@ class JsonBoolean : public Value { // Ambigious with JsonNumber. template , bool>* = nullptr> JsonBoolean(Bool value) : Value(ValueKind::kBoolean), boolean_{value} {} // NOLINT - JsonBoolean(JsonBoolean&& value) noexcept: // NOLINT - Value(ValueKind::kBoolean), boolean_{value.boolean_} {} + JsonBoolean(JsonBoolean&& value) noexcept + : // NOLINT + Value(ValueKind::kBoolean), + boolean_{value.boolean_} {} void Save(JsonWriter* writer) const override; - bool const& GetBoolean() && { return boolean_; } - bool const& GetBoolean() const & { return boolean_; } - bool& GetBoolean() & { return boolean_; } + bool const& GetBoolean() && { return boolean_; } + bool const& GetBoolean() const& { return boolean_; } + bool& GetBoolean() & { return boolean_; } bool operator==(Value const& rhs) const override; Value& operator=(Value const& rhs) override = delete; - static bool IsClassOf(Value const* value) { - return value->Type() == ValueKind::kBoolean; - } + static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kBoolean; } }; /*! @@ -466,15 +454,13 @@ class Json { return *this; } // bool - explicit Json(JsonBoolean boolean) : - ptr_{new JsonBoolean(std::move(boolean))} {} + explicit Json(JsonBoolean boolean) : ptr_{new JsonBoolean(std::move(boolean))} {} Json& operator=(JsonBoolean boolean) { ptr_.reset(new JsonBoolean(std::move(boolean))); return *this; } // null - explicit Json(JsonNull null) : - ptr_{new JsonNull(std::move(null))} {} + explicit Json(JsonNull null) : ptr_{new JsonNull(std::move(null))} {} Json& operator=(JsonNull null) { ptr_.reset(new JsonNull(std::move(null))); return *this; @@ -484,25 +470,23 @@ class Json { Json(Json const& other) = default; Json& operator=(Json const& other) = default; // move - Json(Json &&other) noexcept { std::swap(this->ptr_, other.ptr_); } - Json &operator=(Json &&other) noexcept { + Json(Json&& other) noexcept { std::swap(this->ptr_, other.ptr_); } + Json& operator=(Json&& other) noexcept { std::swap(this->ptr_, other.ptr_); return *this; } /*! \brief Index Json object with a std::string, used for Json Object. */ - Json& operator[](std::string const & key) const { return (*ptr_)[key]; } + Json& operator[](std::string const& key) const { return (*ptr_)[key]; } /*! \brief Index Json object with int, used for Json Array. */ - Json& operator[](int ind) const { return (*ptr_)[ind]; } + Json& operator[](int ind) const { return (*ptr_)[ind]; } /*! \brief Return the reference to stored Json value. */ [[nodiscard]] Value const& GetValue() const& { return *ptr_; } Value const& GetValue() && { return *ptr_; } Value& GetValue() & { return *ptr_; } - bool operator==(Json const& rhs) const { - return *ptr_ == *(rhs.ptr_); - } + bool operator==(Json const& rhs) const { return *ptr_ == *(rhs.ptr_); } friend std::ostream& operator<<(std::ostream& os, Json const& j) { std::string str; @@ -568,8 +552,7 @@ template >* bool& GetImpl(T& val) { // NOLINT return val.GetBoolean(); } -template >* = nullptr> +template >* = nullptr> bool const& GetImpl(T& val) { // NOLINT return val.GetBoolean(); } @@ -614,18 +597,18 @@ JsonObject::Map const& GetImpl(T& val) { // NOLINT * \return Value contained in Json object of type T. */ template -auto get(U& json) -> decltype(detail::GetImpl(*Cast(&json.GetValue())))& { // NOLINT +auto get(U& json) -> decltype(detail::GetImpl(*Cast(&json.GetValue())))& { // NOLINT auto& value = *Cast(&json.GetValue()); return detail::GetImpl(value); } -using Object = JsonObject; -using Array = JsonArray; -using Number = JsonNumber; +using Object = JsonObject; +using Array = JsonArray; +using Number = JsonNumber; using Integer = JsonInteger; using Boolean = JsonBoolean; -using String = JsonString; -using Null = JsonNull; +using String = JsonString; +using Null = JsonNull; /** * \brief Convert XGBoost parameter to JSON object. diff --git a/include/xgboost/json_io.h b/include/xgboost/json_io.h index b58ce96a9670..9e66aaebd526 100644 --- a/include/xgboost/json_io.h +++ b/include/xgboost/json_io.h @@ -69,7 +69,9 @@ class JsonReader { */ Char GetConsecutiveChar(char expected_char) { Char result = GetNextChar(); - if (XGBOOST_EXPECT(result != expected_char, false)) { Expect(expected_char, result); } + if (XGBOOST_EXPECT(result != expected_char, false)) { + Expect(expected_char, result); + } return result; } @@ -100,8 +102,7 @@ class JsonReader { Json Parse(); public: - explicit JsonReader(StringView str) : - raw_str_{str} {} + explicit JsonReader(StringView str) : raw_str_{str} {} virtual ~JsonReader() = default; @@ -138,21 +139,21 @@ class JsonWriter { virtual void Save(Json json); - virtual void Visit(JsonArray const* arr); - virtual void Visit(F32Array const* arr); + virtual void Visit(JsonArray const* arr); + virtual void Visit(F32Array const* arr); virtual void Visit(F64Array const*) { LOG(FATAL) << "Only UBJSON format can handle f64 array."; } - virtual void Visit(I8Array const* arr); + virtual void Visit(I8Array const* arr); virtual void Visit(U8Array const* arr); virtual void Visit(I16Array const* arr); virtual void Visit(U16Array const* arr); - virtual void Visit(I32Array const* arr); - virtual void Visit(U32Array const* arr); - virtual void Visit(I64Array const* arr); - virtual void Visit(U64Array const* arr); + virtual void Visit(I32Array const* arr); + virtual void Visit(U32Array const* arr); + virtual void Visit(I64Array const* arr); + virtual void Visit(U64Array const* arr); virtual void Visit(JsonObject const* obj); virtual void Visit(JsonNumber const* num); virtual void Visit(JsonInteger const* num); - virtual void Visit(JsonNull const* null); + virtual void Visit(JsonNull const* null); virtual void Visit(JsonString const* str); virtual void Visit(JsonBoolean const* boolean); }; @@ -224,11 +225,11 @@ class UBJWriter : public JsonWriter { void Visit(JsonArray const* arr) override; void Visit(F32Array const* arr) override; void Visit(F64Array const* arr) override; - void Visit(I8Array const* arr) override; - void Visit(U8Array const* arr) override; - void Visit(I16Array const* arr) override; - void Visit(I32Array const* arr) override; - void Visit(I64Array const* arr) override; + void Visit(I8Array const* arr) override; + void Visit(U8Array const* arr) override; + void Visit(I16Array const* arr) override; + void Visit(I32Array const* arr) override; + void Visit(I64Array const* arr) override; void Visit(JsonObject const* obj) override; void Visit(JsonNumber const* num) override; void Visit(JsonInteger const* num) override; @@ -240,6 +241,6 @@ class UBJWriter : public JsonWriter { using JsonWriter::JsonWriter; void Save(Json json) override; }; -} // namespace xgboost +} // namespace xgboost #endif // XGBOOST_JSON_IO_H_ diff --git a/include/xgboost/learner.h b/include/xgboost/learner.h index ffaddfbe6442..5aa2dc9fb75d 100644 --- a/include/xgboost/learner.h +++ b/include/xgboost/learner.h @@ -96,8 +96,7 @@ class Learner : public Model, public Configurable, public dmlc::Serializable { * \param data_names name of each dataset * \return a string corresponding to the evaluation result */ - virtual std::string EvalOneIter(int iter, - const std::vector>& data_sets, + virtual std::string EvalOneIter(int iter, const std::vector>& data_sets, const std::vector& data_names) = 0; /*! * \brief get prediction given the model. @@ -107,10 +106,10 @@ class Learner : public Model, public Configurable, public dmlc::Serializable { * \param layer_begin Beginning of boosted tree layer used for prediction. * \param layer_end End of booster layer. 0 means do not limit trees. * \param training Whether the prediction result is used for training - * \param pred_leaf whether to only predict the leaf index of each tree in a boosted tree predictor - * \param pred_contribs whether to only predict the feature contributions - * \param approx_contribs whether to approximate the feature contributions for speed - * \param pred_interactions whether to compute the feature pair contributions + * \param pred_leaf whether to only predict the leaf index of each tree in a boosted tree + * predictor \param pred_contribs whether to only predict the feature contributions \param + * approx_contribs whether to approximate the feature contributions for speed \param + * pred_interactions whether to compute the feature pair contributions */ virtual void Predict(std::shared_ptr data, bool output_margin, HostDeviceVector* out_preds, bst_layer_t layer_begin, @@ -206,7 +205,7 @@ class Learner : public Model, public Configurable, public dmlc::Serializable { * \brief Set the feature names for current booster. * \param fn Input feature names */ - virtual void SetFeatureNames(std::vector const& fn) = 0; + virtual void SetFeatureNames(std::vector const& fn) = 0; /*! * \brief Get the feature names for current booster. * \param fn Output feature names @@ -245,8 +244,7 @@ class Learner : public Model, public Configurable, public dmlc::Serializable { * \param format the format to dump the model in * \return a vector of dump for boosters. */ - virtual std::vector DumpModel(const FeatureMap& fmap, - bool with_stats, + virtual std::vector DumpModel(const FeatureMap& fmap, bool with_stats, std::string format) = 0; virtual XGBAPIThreadLocalEntry& GetThreadLocal() const = 0; @@ -259,7 +257,7 @@ class Learner : public Model, public Configurable, public dmlc::Serializable { * \param cache_data The matrix to cache the prediction. * \return Created learner. */ - static Learner* Create(const std::vector >& cache_data); + static Learner* Create(const std::vector>& cache_data); /** * \brief Return the context object of this Booster. */ @@ -276,7 +274,7 @@ class Learner : public Model, public Configurable, public dmlc::Serializable { /*! \brief The gradient booster used by the model*/ std::unique_ptr gbm_; /*! \brief The evaluation metrics used to evaluate the model. */ - std::vector > metrics_; + std::vector> metrics_; /*! \brief Training parameter. */ Context ctx_; }; diff --git a/include/xgboost/linalg.h b/include/xgboost/linalg.h index aff3ae860a98..8a6c061e81e8 100644 --- a/include/xgboost/linalg.h +++ b/include/xgboost/linalg.h @@ -363,8 +363,10 @@ class TensorView { * \brief Slice dimension for Index tag. */ template - LINALG_HD std::enable_if_t, size_t> MakeSliceDim( - size_t new_shape[D], size_t new_stride[D], Index i, S &&...slices) const { + LINALG_HD std::enable_if_t, size_t> MakeSliceDim(size_t new_shape[D], + size_t new_stride[D], + Index i, + S &&...slices) const { static_assert(old_dim < kDim); auto offset = stride_[old_dim] * i; auto res = @@ -659,8 +661,8 @@ auto MakeVec(std::vector const &v) { template auto MakeVec(HostDeviceVector *data) { - return MakeVec(data->Device().IsCPU() ? data->HostPointer() : data->DevicePointer(), - data->Size(), data->Device()); + return MakeVec(data->Device().IsCPU() ? data->HostPointer() : data->DevicePointer(), data->Size(), + data->Device()); } template diff --git a/include/xgboost/linear_updater.h b/include/xgboost/linear_updater.h index bcc8dd890dcd..1451457395b9 100644 --- a/include/xgboost/linear_updater.h +++ b/include/xgboost/linear_updater.h @@ -14,7 +14,6 @@ #include #include - namespace xgboost { class Json; @@ -38,8 +37,7 @@ class LinearUpdater : public Configurable { * \brief Initialize the updater with given arguments. * \param args arguments to the objective function. */ - virtual void Configure( - const std::vector >& args) = 0; + virtual void Configure(const std::vector >& args) = 0; /** * \brief Updates linear model given gradients. @@ -63,16 +61,14 @@ class LinearUpdater : public Configurable { * \brief Registry entry for linear updater. */ struct LinearUpdaterReg - : public dmlc::FunctionRegEntryBase > {}; + : public dmlc::FunctionRegEntryBase > {}; /*! * \brief Macro to register linear updater. */ -#define XGBOOST_REGISTER_LINEAR_UPDATER(UniqueId, Name) \ - static DMLC_ATTRIBUTE_UNUSED ::xgboost::LinearUpdaterReg& \ - __make_##LinearUpdaterReg##_##UniqueId##__ = \ - ::dmlc::Registry< ::xgboost::LinearUpdaterReg>::Get()->__REGISTER__( \ - Name) +#define XGBOOST_REGISTER_LINEAR_UPDATER(UniqueId, Name) \ + static DMLC_ATTRIBUTE_UNUSED ::xgboost::LinearUpdaterReg& \ + __make_##LinearUpdaterReg##_##UniqueId##__ = \ + ::dmlc::Registry< ::xgboost::LinearUpdaterReg>::Get()->__REGISTER__(Name) } // namespace xgboost diff --git a/include/xgboost/logging.h b/include/xgboost/logging.h index eaed72f18bad..ba133f9d2a0f 100644 --- a/include/xgboost/logging.h +++ b/include/xgboost/logging.h @@ -10,13 +10,13 @@ #include #include - #include -#include #include +#include -#include +#include #include +#include #include #include #include @@ -73,14 +73,10 @@ class TrackerLogger : public BaseLogger { class LogCallbackRegistry { public: using Callback = void (*)(const char*); - LogCallbackRegistry() - : log_callback_([] (const char* msg) { std::cerr << msg << std::endl; }) {} - inline void Register(Callback log_callback) { - this->log_callback_ = log_callback; - } - inline Callback Get() const { - return log_callback_; - } + LogCallbackRegistry() : log_callback_([](const char* msg) { std::cerr << msg << std::endl; }) {} + inline void Register(Callback log_callback) { this->log_callback_ = log_callback; } + inline Callback Get() const { return log_callback_; } + private: Callback log_callback_; }; @@ -98,32 +94,26 @@ using LogCallbackRegistryStore = dmlc::ThreadLocalStore; // Redefines LOG_WARNING for controling verbosity #if defined(LOG_WARNING) -#undef LOG_WARNING +#undef LOG_WARNING #endif // defined(LOG_WARNING) -#define LOG_WARNING \ - if (::xgboost::ConsoleLogger::ShouldLog( \ - ::xgboost::ConsoleLogger::LV::kWarning)) \ - ::xgboost::ConsoleLogger(__FILE__, __LINE__, \ - ::xgboost::ConsoleLogger::LogVerbosity::kWarning) +#define LOG_WARNING \ + if (::xgboost::ConsoleLogger::ShouldLog(::xgboost::ConsoleLogger::LV::kWarning)) \ + ::xgboost::ConsoleLogger(__FILE__, __LINE__, ::xgboost::ConsoleLogger::LogVerbosity::kWarning) // Redefines LOG_INFO for controling verbosity #if defined(LOG_INFO) -#undef LOG_INFO +#undef LOG_INFO #endif // defined(LOG_INFO) -#define LOG_INFO \ - if (::xgboost::ConsoleLogger::ShouldLog( \ - ::xgboost::ConsoleLogger::LV::kInfo)) \ - ::xgboost::ConsoleLogger(__FILE__, __LINE__, \ - ::xgboost::ConsoleLogger::LogVerbosity::kInfo) +#define LOG_INFO \ + if (::xgboost::ConsoleLogger::ShouldLog(::xgboost::ConsoleLogger::LV::kInfo)) \ + ::xgboost::ConsoleLogger(__FILE__, __LINE__, ::xgboost::ConsoleLogger::LogVerbosity::kInfo) #if defined(LOG_DEBUG) #undef LOG_DEBUG #endif // defined(LOG_DEBUG) -#define LOG_DEBUG \ - if (::xgboost::ConsoleLogger::ShouldLog( \ - ::xgboost::ConsoleLogger::LV::kDebug)) \ - ::xgboost::ConsoleLogger(__FILE__, __LINE__, \ - ::xgboost::ConsoleLogger::LogVerbosity::kDebug) +#define LOG_DEBUG \ + if (::xgboost::ConsoleLogger::ShouldLog(::xgboost::ConsoleLogger::LV::kDebug)) \ + ::xgboost::ConsoleLogger(__FILE__, __LINE__, ::xgboost::ConsoleLogger::LogVerbosity::kDebug) // redefines the logging macro if not existed #ifndef LOG @@ -131,17 +121,15 @@ using LogCallbackRegistryStore = dmlc::ThreadLocalStore; #endif // LOG // Enable LOG(CONSOLE) for print messages to console. -#define LOG_CONSOLE ::xgboost::ConsoleLogger( \ - ::xgboost::ConsoleLogger::LogVerbosity::kIgnore) +#define LOG_CONSOLE ::xgboost::ConsoleLogger(::xgboost::ConsoleLogger::LogVerbosity::kIgnore) // Enable LOG(TRACKER) for print messages to tracker #define LOG_TRACKER ::xgboost::TrackerLogger() #if defined(CHECK) #undef CHECK -#define CHECK(cond) \ - if (XGBOOST_EXPECT(!(cond), false)) \ - dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ - << "Check failed: " #cond << ": " +#define CHECK(cond) \ + if (XGBOOST_EXPECT(!(cond), false)) \ + dmlc::LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #cond << ": " #endif // defined(CHECK) } // namespace xgboost. diff --git a/include/xgboost/metric.h b/include/xgboost/metric.h index 3e405cf587c2..b54b5cf24a3a 100644 --- a/include/xgboost/metric.h +++ b/include/xgboost/metric.h @@ -35,8 +35,7 @@ class Metric : public Configurable { * \brief Configure the Metric with the specified parameters. * \param args arguments to the objective function. */ - virtual void Configure( - const std::vector >&) {} + virtual void Configure(const std::vector >&) {} /*! * \brief Load configuration from JSON object * By default, metric has no internal configuration; @@ -85,9 +84,7 @@ class Metric : public Configurable { * For example, metric map@3, then: param == "3". */ struct MetricReg - : public dmlc::FunctionRegEntryBase > { -}; + : public dmlc::FunctionRegEntryBase > {}; /*! * \brief Macro to register metric. @@ -102,8 +99,8 @@ struct MetricReg * }); * \endcode */ -#define XGBOOST_REGISTER_METRIC(UniqueId, Name) \ - ::xgboost::MetricReg& __make_ ## MetricReg ## _ ## UniqueId ## __ = \ +#define XGBOOST_REGISTER_METRIC(UniqueId, Name) \ + ::xgboost::MetricReg& __make_##MetricReg##_##UniqueId##__ = \ ::dmlc::Registry< ::xgboost::MetricReg>::Get()->__REGISTER__(Name) } // namespace xgboost #endif // XGBOOST_METRIC_H_ diff --git a/include/xgboost/objective.h b/include/xgboost/objective.h index 497821590bc9..f94f727b96ca 100644 --- a/include/xgboost/objective.h +++ b/include/xgboost/objective.h @@ -143,9 +143,7 @@ class ObjFunction : public Configurable { * \brief Registry entry for objective factory functions. */ struct ObjFunctionReg - : public dmlc::FunctionRegEntryBase > { -}; + : public dmlc::FunctionRegEntryBase > {}; /*! * \brief Macro to register objective function. @@ -159,9 +157,9 @@ struct ObjFunctionReg * }); * \endcode */ -#define XGBOOST_REGISTER_OBJECTIVE(UniqueId, Name) \ - static DMLC_ATTRIBUTE_UNUSED ::xgboost::ObjFunctionReg & \ - __make_ ## ObjFunctionReg ## _ ## UniqueId ## __ = \ - ::dmlc::Registry< ::xgboost::ObjFunctionReg>::Get()->__REGISTER__(Name) +#define XGBOOST_REGISTER_OBJECTIVE(UniqueId, Name) \ + static DMLC_ATTRIBUTE_UNUSED ::xgboost::ObjFunctionReg& \ + __make_##ObjFunctionReg##_##UniqueId##__ = \ + ::dmlc::Registry< ::xgboost::ObjFunctionReg>::Get()->__REGISTER__(Name) } // namespace xgboost #endif // XGBOOST_OBJECTIVE_H_ diff --git a/include/xgboost/parameter.h b/include/xgboost/parameter.h index d8188c030360..8e4b94896b00 100644 --- a/include/xgboost/parameter.h +++ b/include/xgboost/parameter.h @@ -10,6 +10,7 @@ #include #include + #include #include @@ -47,43 +48,40 @@ * DMLC_REGISTER_PARAMETER(MyParam); * \endcode */ -#define DECLARE_FIELD_ENUM_CLASS(EnumClass) \ -namespace dmlc { \ -namespace parameter { \ -template <> \ -class FieldEntry : public FieldEntry { \ - public: \ - FieldEntry() { \ - static_assert( \ - std::is_same_v>, \ - "enum class must be backed by int"); \ - is_enum_ = true; \ - } \ - using Super = FieldEntry; \ - void Set(void *head, const std::string &value) const override { \ - Super::Set(head, value); \ - } \ - inline FieldEntry& add_enum(const std::string &key, EnumClass value) { \ - Super::add_enum(key, static_cast(value)); \ - return *this; \ - } \ - inline FieldEntry& set_default(const EnumClass& default_value) { \ - default_value_ = static_cast(default_value); \ - has_default_ = true; \ - return *this; \ - } \ - inline void Init(const std::string &key, void *head, EnumClass& ref) { /* NOLINT */ \ - Super::Init(key, head, *reinterpret_cast(&ref)); \ - } \ -}; \ -} /* namespace parameter */ \ -} /* namespace dmlc */ +#define DECLARE_FIELD_ENUM_CLASS(EnumClass) \ + namespace dmlc { \ + namespace parameter { \ + template <> \ + class FieldEntry : public FieldEntry { \ + public: \ + FieldEntry() { \ + static_assert(std::is_same_v>, \ + "enum class must be backed by int"); \ + is_enum_ = true; \ + } \ + using Super = FieldEntry; \ + void Set(void* head, const std::string& value) const override { Super::Set(head, value); } \ + inline FieldEntry& add_enum(const std::string& key, EnumClass value) { \ + Super::add_enum(key, static_cast(value)); \ + return *this; \ + } \ + inline FieldEntry& set_default(const EnumClass& default_value) { \ + default_value_ = static_cast(default_value); \ + has_default_ = true; \ + return *this; \ + } \ + inline void Init(const std::string& key, void* head, EnumClass& ref) { /* NOLINT */ \ + Super::Init(key, head, *reinterpret_cast(&ref)); \ + } \ + }; \ + } /* namespace parameter */ \ + } /* namespace dmlc */ namespace xgboost { template struct XGBoostParameter : public dmlc::Parameter { protected: - bool initialised_ {false}; + bool initialised_{false}; public: template diff --git a/include/xgboost/predictor.h b/include/xgboost/predictor.h index 020e0a59d1e8..0b8c63260f59 100644 --- a/include/xgboost/predictor.h +++ b/include/xgboost/predictor.h @@ -150,8 +150,9 @@ class Predictor { * \param tree_end The tree end index. * \param tree_weights (Optional) Weights to multiply each tree by. * \param approximate Use fast approximate algorithm. - * \param condition Condition on the condition_feature (0=no, -1=cond off, 1=cond on). - * \param condition_feature Feature to condition on (i.e. fix) during calculations. + * \param condition Condition on the condition_feature (0=no, -1=cond off, + * 1=cond on). \param condition_feature Feature to condition on (i.e. fix) during + * calculations. */ virtual void PredictContribution(DMatrix* dmat, HostDeviceVector* out_contribs, @@ -181,8 +182,7 @@ class Predictor { struct PredictorReg : public dmlc::FunctionRegEntryBase> {}; -#define XGBOOST_REGISTER_PREDICTOR(UniqueId, Name) \ - static DMLC_ATTRIBUTE_UNUSED ::xgboost::PredictorReg& \ - __make_##PredictorReg##_##UniqueId##__ = \ - ::dmlc::Registry<::xgboost::PredictorReg>::Get()->__REGISTER__(Name) +#define XGBOOST_REGISTER_PREDICTOR(UniqueId, Name) \ + static DMLC_ATTRIBUTE_UNUSED ::xgboost::PredictorReg& __make_##PredictorReg##_##UniqueId##__ = \ + ::dmlc::Registry<::xgboost::PredictorReg>::Get()->__REGISTER__(Name) } // namespace xgboost diff --git a/include/xgboost/span.h b/include/xgboost/span.h index 29b1cf9326bc..45d303bb3c5b 100644 --- a/include/xgboost/span.h +++ b/include/xgboost/span.h @@ -144,8 +144,7 @@ using ptrdiff_t = typename std::conditional_t< // NOLINT } // namespace detail #if defined(_MSC_VER) && _MSC_VER < 1910 -constexpr const std::size_t -dynamic_extent = std::numeric_limits::max(); // NOLINT +constexpr const std::size_t dynamic_extent = std::numeric_limits::max(); // NOLINT #else constexpr std::size_t dynamic_extent = std::numeric_limits::max(); // NOLINT #endif // defined(_MSC_VER) && _MSC_VER < 1910 @@ -162,20 +161,20 @@ class SpanIterator { using ElementType = typename SpanType::element_type; public: - using iterator_category = std::random_access_iterator_tag; // NOLINT - using value_type = typename SpanType::value_type; // NOLINT - using difference_type = detail::ptrdiff_t; // NOLINT + using iterator_category = std::random_access_iterator_tag; // NOLINT + using value_type = typename SpanType::value_type; // NOLINT + using difference_type = detail::ptrdiff_t; // NOLINT - using reference = typename std::conditional< // NOLINT - IsConst, const ElementType, ElementType>::type&; - using pointer = typename std::add_pointer::type; // NOLINT + using reference = typename std::conditional< // NOLINT + IsConst, const ElementType, ElementType>::type&; + using pointer = typename std::add_pointer::type; // NOLINT constexpr SpanIterator() = default; - XGBOOST_DEVICE constexpr SpanIterator( - const SpanType* _span, - typename SpanType::index_type _idx) __span_noexcept : - span_(_span), index_(_idx) {} + XGBOOST_DEVICE constexpr SpanIterator(const SpanType* _span, + typename SpanType::index_type _idx) __span_noexcept + : span_(_span), + index_(_idx) {} friend SpanIterator; template * = nullptr> @@ -187,9 +186,7 @@ class SpanIterator { SPAN_CHECK(index_ < span_->size()); return *(span_->data() + index_); } - XGBOOST_DEVICE reference operator[](difference_type n) const { - return *(*this + n); - } + XGBOOST_DEVICE reference operator[](difference_type n) const { return *(*this + n); } XGBOOST_DEVICE pointer operator->() const { SPAN_CHECK(index_ != span_->size()); @@ -241,47 +238,44 @@ class SpanIterator { return ret -= n; } - XGBOOST_DEVICE SpanIterator& operator-=(difference_type n) { - return *this += -n; - } + XGBOOST_DEVICE SpanIterator& operator-=(difference_type n) { return *this += -n; } // friends - XGBOOST_DEVICE constexpr friend bool operator==( - SpanIterator _lhs, SpanIterator _rhs) __span_noexcept { + XGBOOST_DEVICE constexpr friend bool operator==(SpanIterator _lhs, + SpanIterator _rhs) __span_noexcept { return _lhs.span_ == _rhs.span_ && _lhs.index_ == _rhs.index_; } - XGBOOST_DEVICE constexpr friend bool operator!=( - SpanIterator _lhs, SpanIterator _rhs) __span_noexcept { + XGBOOST_DEVICE constexpr friend bool operator!=(SpanIterator _lhs, + SpanIterator _rhs) __span_noexcept { return !(_lhs == _rhs); } - XGBOOST_DEVICE constexpr friend bool operator<( - SpanIterator _lhs, SpanIterator _rhs) __span_noexcept { + XGBOOST_DEVICE constexpr friend bool operator<(SpanIterator _lhs, + SpanIterator _rhs) __span_noexcept { return _lhs.index_ < _rhs.index_; } - XGBOOST_DEVICE constexpr friend bool operator<=( - SpanIterator _lhs, SpanIterator _rhs) __span_noexcept { + XGBOOST_DEVICE constexpr friend bool operator<=(SpanIterator _lhs, + SpanIterator _rhs) __span_noexcept { return !(_rhs < _lhs); } - XGBOOST_DEVICE constexpr friend bool operator>( - SpanIterator _lhs, SpanIterator _rhs) __span_noexcept { + XGBOOST_DEVICE constexpr friend bool operator>(SpanIterator _lhs, + SpanIterator _rhs) __span_noexcept { return _rhs < _lhs; } - XGBOOST_DEVICE constexpr friend bool operator>=( - SpanIterator _lhs, SpanIterator _rhs) __span_noexcept { + XGBOOST_DEVICE constexpr friend bool operator>=(SpanIterator _lhs, + SpanIterator _rhs) __span_noexcept { return !(_rhs > _lhs); } protected: - const SpanType *span_ { nullptr }; - typename SpanType::index_type index_ { 0 }; + const SpanType* span_{nullptr}; + typename SpanType::index_type index_{0}; }; - // It's tempting to use constexpr instead of structs to do the following meta // programming. But remember that we are supporting MSVC 2013 here. @@ -293,27 +287,30 @@ class SpanIterator { * - Otherwise, dynamic_extent. */ template -struct ExtentValue : public std::integral_constant< - std::size_t, Count != dynamic_extent ? - Count : (Extent != dynamic_extent ? Extent - Offset : Extent)> {}; +struct ExtentValue + : public std::integral_constant< + std::size_t, + Count != dynamic_extent ? Count : (Extent != dynamic_extent ? Extent - Offset : Extent)> { +}; /*! * If N is dynamic_extent, the extent of the returned span E is also * dynamic_extent; otherwise it is std::size_t(sizeof(T)) * N. */ template -struct ExtentAsBytesValue : public std::integral_constant< - std::size_t, - Extent == dynamic_extent ? - Extent : sizeof(T) * Extent> {}; +struct ExtentAsBytesValue + : public std::integral_constant {}; template -struct IsAllowedExtentConversion : public std::integral_constant< - bool, From == To || From == dynamic_extent || To == dynamic_extent> {}; +struct IsAllowedExtentConversion + : public std::integral_constant {}; template struct IsAllowedElementTypeConversion - : public std::integral_constant> {}; // NOLINT + : public std::integral_constant> { +}; // NOLINT template struct IsSpanOracle : std::false_type {}; @@ -327,23 +324,18 @@ struct IsSpan : public IsSpanOracle> {}; // Re-implement std algorithms here to adopt CUDA. template struct Less { - XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const { - return _x < _y; - } + XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const { return _x < _y; } }; template struct Greater { - XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const { - return _x > _y; - } + XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const { return _x > _y; } }; template ().operator*())>> -XGBOOST_DEVICE bool LexicographicalCompare(InputIt1 first1, InputIt1 last1, - InputIt2 first2, InputIt2 last2) { + class Compare = detail::Less().operator*())>> +XGBOOST_DEVICE bool LexicographicalCompare(InputIt1 first1, InputIt1 last1, InputIt2 first2, + InputIt2 last2) { Compare comp; for (; first1 != last1 && first2 != last2; ++first1, ++first2) { if (comp(*first1, *first2)) { @@ -430,8 +422,7 @@ XGBOOST_DEVICE std::enable_if_t, std::size_t> SizeBytes( * compromise. Since we have subspan, it's acceptable not to support * passing iterator. */ -template +template class Span { public: using element_type = T; // NOLINT @@ -449,20 +440,19 @@ class Span { // constructors constexpr Span() = default; - XGBOOST_DEVICE Span(pointer _ptr, index_type _count) : - size_(_count), data_(_ptr) { + XGBOOST_DEVICE Span(pointer _ptr, index_type _count) : size_(_count), data_(_ptr) { SPAN_CHECK(!(Extent != dynamic_extent && _count != Extent)); SPAN_CHECK(_ptr || _count == 0); } - XGBOOST_DEVICE Span(pointer _first, pointer _last) : - size_(_last - _first), data_(_first) { + XGBOOST_DEVICE Span(pointer _first, pointer _last) : size_(_last - _first), data_(_first) { SPAN_CHECK(data_ || size_ == 0); } template XGBOOST_DEVICE constexpr Span(element_type (&arr)[N]) // NOLINT - __span_noexcept : size_(N), data_(&arr[0]) {} + __span_noexcept : size_(N), + data_(&arr[0]) {} template operator[](_idx); - } + XGBOOST_DEVICE reference operator()(index_type _idx) const { return this->operator[](_idx); } - XGBOOST_DEVICE constexpr pointer data() const __span_noexcept { // NOLINT + XGBOOST_DEVICE constexpr pointer data() const __span_noexcept { // NOLINT return data_; } @@ -598,26 +586,21 @@ class Span { */ template - XGBOOST_DEVICE auto subspan() const -> // NOLINT - Span::value> { - SPAN_CHECK((Count == dynamic_extent) ? - (Offset <= size()) : (Offset + Count <= size())); + XGBOOST_DEVICE auto subspan() const -> // NOLINT + Span::value> { + SPAN_CHECK((Count == dynamic_extent) ? (Offset <= size()) : (Offset + Count <= size())); return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count}; } XGBOOST_DEVICE Span subspan( // NOLINT - index_type _offset, - index_type _count = dynamic_extent) const { - SPAN_CHECK((_count == dynamic_extent) ? - (_offset <= size()) : (_offset + _count <= size())); - return {data() + _offset, _count == - dynamic_extent ? size() - _offset : _count}; + index_type _offset, index_type _count = dynamic_extent) const { + SPAN_CHECK((_count == dynamic_extent) ? (_offset <= size()) : (_offset + _count <= size())); + return {data() + _offset, _count == dynamic_extent ? size() - _offset : _count}; } private: - index_type size_ { 0 }; - pointer data_ { nullptr }; + index_type size_{0}; + pointer data_{nullptr}; }; template @@ -625,8 +608,7 @@ XGBOOST_DEVICE bool operator==(Span l, Span r) { if (l.size() != r.size()) { return false; } - for (auto l_beg = l.cbegin(), r_beg = r.cbegin(); l_beg != l.cend(); - ++l_beg, ++r_beg) { + for (auto l_beg = l.cbegin(), r_beg = r.cbegin(); l_beg != l.cend(); ++l_beg, ++r_beg) { if (*l_beg != *r_beg) { return false; } @@ -641,8 +623,7 @@ XGBOOST_DEVICE constexpr bool operator!=(Span l, Span r) { template XGBOOST_DEVICE constexpr bool operator<(Span l, Span r) { - return detail::LexicographicalCompare(l.begin(), l.end(), - r.begin(), r.end()); + return detail::LexicographicalCompare(l.begin(), l.end(), r.begin(), r.end()); } template @@ -652,10 +633,10 @@ XGBOOST_DEVICE constexpr bool operator<=(Span l, Span r) { template XGBOOST_DEVICE constexpr bool operator>(Span l, Span r) { - return detail::LexicographicalCompare< - typename Span::iterator, typename Span::iterator, - detail::Greater::element_type>>(l.begin(), l.end(), - r.begin(), r.end()); + return detail::LexicographicalCompare::iterator, + typename Span::iterator, + detail::Greater::element_type>>( + l.begin(), l.end(), r.begin(), r.end()); } template @@ -664,13 +645,13 @@ XGBOOST_DEVICE constexpr bool operator>=(Span l, Span r) { } template -XGBOOST_DEVICE auto as_bytes(Span s) __span_noexcept -> // NOLINT +XGBOOST_DEVICE auto as_bytes(Span s) __span_noexcept-> // NOLINT Span::value> { return {reinterpret_cast(s.data()), s.size_bytes()}; } template -XGBOOST_DEVICE auto as_writable_bytes(Span s) __span_noexcept -> // NOLINT +XGBOOST_DEVICE auto as_writable_bytes(Span s) __span_noexcept-> // NOLINT Span::value> { return {reinterpret_cast(s.data()), s.size_bytes()}; } @@ -720,8 +701,7 @@ template Span(std::vector&) -> Span; } // namespace xgboost::common - -#if defined(_MSC_VER) &&_MSC_VER < 1910 +#if defined(_MSC_VER) && _MSC_VER < 1910 #undef constexpr #pragma pop_macro("constexpr") #undef __span_noexcept diff --git a/include/xgboost/tree_model.h b/include/xgboost/tree_model.h index 116e14c426f8..7fba5ed4cd8c 100644 --- a/include/xgboost/tree_model.h +++ b/include/xgboost/tree_model.h @@ -29,7 +29,7 @@ namespace xgboost { namespace tree { struct ScalarTreeView; struct MultiTargetTreeView; -} +} // namespace tree class Json; @@ -88,7 +88,7 @@ class RegTree : public Model { /** @brief tree node */ class Node { public: - XGBOOST_DEVICE Node() { + XGBOOST_DEVICE Node() { // assert compact alignment static_assert(sizeof(Node) == 4 * sizeof(int) + sizeof(Info), "Node: 64 bit align"); } @@ -132,16 +132,12 @@ class RegTree : public Model { * \brief set the left child * \param nid node id to right child */ - XGBOOST_DEVICE void SetLeftChild(int nid) { - this->cleft_ = nid; - } + XGBOOST_DEVICE void SetLeftChild(int nid) { this->cleft_ = nid; } /*! * \brief set the right child * \param nid node id to right child */ - XGBOOST_DEVICE void SetRightChild(int nid) { - this->cright_ = nid; - } + XGBOOST_DEVICE void SetRightChild(int nid) { this->cright_ = nid; } /*! * \brief set split condition of current node * \param split_index feature index to split @@ -166,22 +162,17 @@ class RegTree : public Model { this->cright_ = right; } /*! \brief mark that this node is deleted */ - XGBOOST_DEVICE void MarkDelete() { - this->sindex_ = kDeletedNodeMarker; - } + XGBOOST_DEVICE void MarkDelete() { this->sindex_ = kDeletedNodeMarker; } /*! \brief Reuse this deleted node. */ - XGBOOST_DEVICE void Reuse() { - this->sindex_ = 0; - } + XGBOOST_DEVICE void Reuse() { this->sindex_ = 0; } // set parent XGBOOST_DEVICE void SetParent(int pidx, bool is_left_child = true) { if (is_left_child) pidx |= (1U << 31); this->parent_ = pidx; } bool operator==(const Node& b) const { - return parent_ == b.parent_ && cleft_ == b.cleft_ && - cright_ == b.cright_ && sindex_ == b.sindex_ && - info_.leaf_value == b.info_.leaf_value; + return parent_ == b.parent_ && cleft_ == b.cleft_ && cright_ == b.cright_ && + sindex_ == b.sindex_ && info_.leaf_value == b.info_.leaf_value; } private: @@ -189,7 +180,7 @@ class RegTree : public Model { * \brief in leaf node, we have weights, in non-leaf nodes, * we have split condition */ - union Info{ + union Info { bst_float leaf_value; SplitCondT split_cond; }; @@ -277,9 +268,7 @@ class RegTree : public Model { } /*! \brief get node statistics given nid */ - RTreeNodeStat& Stat(int nid) { - return stats_.HostVector()[nid]; - } + RTreeNodeStat& Stat(int nid) { return stats_.HostVector()[nid]; } void LoadModel(Json const& in) override; void SaveModel(Json* out) const override; @@ -314,11 +303,9 @@ class RegTree : public Model { * \param leaf_right_child The right child index of leaf, by default kInvalidNodeId, * some updaters use the right child index of leaf as a marker */ - void ExpandNode(bst_node_t nid, unsigned split_index, bst_float split_value, - bool default_left, bst_float base_weight, - bst_float left_leaf_weight, bst_float right_leaf_weight, - bst_float loss_change, float sum_hess, float left_sum, - float right_sum, + void ExpandNode(bst_node_t nid, unsigned split_index, bst_float split_value, bool default_left, + bst_float base_weight, bst_float left_leaf_weight, bst_float right_leaf_weight, + bst_float loss_change, float sum_hess, float left_sum, float right_sum, bst_node_t leaf_right_child = kInvalidNodeId); /** * @brief Expands a leaf node into two additional leaf nodes for a multi-target tree. @@ -567,7 +554,7 @@ class RegTree : public Model { // vector of nodes HostDeviceVector nodes_; // free node space, used during training process - std::vector deleted_nodes_; + std::vector deleted_nodes_; // stats of nodes HostDeviceVector stats_; HostDeviceVector split_types_; @@ -632,13 +619,9 @@ inline void RegTree::FVec::Fill(SparsePage::Inst const& inst) { inline void RegTree::FVec::Drop() { this->Init(this->Size()); } -inline size_t RegTree::FVec::Size() const { - return data_.size(); -} +inline size_t RegTree::FVec::Size() const { return data_.size(); } -inline float RegTree::FVec::GetFvalue(size_t i) const { - return data_[i]; -} +inline float RegTree::FVec::GetFvalue(size_t i) const { return data_[i]; } inline bool RegTree::FVec::IsMissing(size_t i) const { return std::isnan(data_[i]); } diff --git a/include/xgboost/tree_updater.h b/include/xgboost/tree_updater.h index d322022c47bf..e67fa3fc57f0 100644 --- a/include/xgboost/tree_updater.h +++ b/include/xgboost/tree_updater.h @@ -126,10 +126,10 @@ struct TreeUpdaterReg * }); * \endcode */ -#define XGBOOST_REGISTER_TREE_UPDATER(UniqueId, Name) \ - static DMLC_ATTRIBUTE_UNUSED ::xgboost::TreeUpdaterReg& \ - __make_ ## TreeUpdaterReg ## _ ## UniqueId ## __ = \ - ::dmlc::Registry< ::xgboost::TreeUpdaterReg>::Get()->__REGISTER__(Name) +#define XGBOOST_REGISTER_TREE_UPDATER(UniqueId, Name) \ + static DMLC_ATTRIBUTE_UNUSED ::xgboost::TreeUpdaterReg& \ + __make_##TreeUpdaterReg##_##UniqueId##__ = \ + ::dmlc::Registry<::xgboost::TreeUpdaterReg>::Get()->__REGISTER__(Name) } // namespace xgboost #endif // XGBOOST_TREE_UPDATER_H_ diff --git a/include/xgboost/version_config.h b/include/xgboost/version_config.h index 53ae594c2590..9d590236c9fe 100644 --- a/include/xgboost/version_config.h +++ b/include/xgboost/version_config.h @@ -4,8 +4,8 @@ #ifndef XGBOOST_VERSION_CONFIG_H_ #define XGBOOST_VERSION_CONFIG_H_ -#define XGBOOST_VER_MAJOR 3 /* NOLINT */ -#define XGBOOST_VER_MINOR 2 /* NOLINT */ -#define XGBOOST_VER_PATCH 0 /* NOLINT */ +#define XGBOOST_VER_MAJOR 3 /* NOLINT */ +#define XGBOOST_VER_MINOR 2 /* NOLINT */ +#define XGBOOST_VER_PATCH 0 /* NOLINT */ #endif // XGBOOST_VERSION_CONFIG_H_ diff --git a/jvm-packages/README.md b/jvm-packages/README.md index 78f9a5e0f9a1..41b74b72f10e 100644 --- a/jvm-packages/README.md +++ b/jvm-packages/README.md @@ -14,4 +14,4 @@ into JVM ecosystem. - Run distributed xgboost natively on jvm frameworks such as Apache Flink and Apache Spark. -You can find more about XGBoost on [Documentation](https://xgboost.readthedocs.org/en/stable/jvm/index.html) and [Resource Page](../demo/README.md). \ No newline at end of file +You can find more about XGBoost on [Documentation](https://xgboost.readthedocs.org/en/stable/jvm/index.html) and [Resource Page](../demo/README.md). diff --git a/jvm-packages/checkstyle.xml b/jvm-packages/checkstyle.xml index ebfd7cd88531..19cb35c4785a 100644 --- a/jvm-packages/checkstyle.xml +++ b/jvm-packages/checkstyle.xml @@ -77,7 +77,7 @@ - + diff --git a/jvm-packages/xgboost4j-example/LICENSE b/jvm-packages/xgboost4j-example/LICENSE index 9a1673be2ed6..f02ba610d0a3 100644 --- a/jvm-packages/xgboost4j-example/LICENSE +++ b/jvm-packages/xgboost4j-example/LICENSE @@ -1,10 +1,10 @@ /* -Copyright (c) 2014 by Contributors +Copyright (c) 2014 by Contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software @@ -12,4 +12,4 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/ \ No newline at end of file +*/ diff --git a/jvm-packages/xgboost4j-spark/src/test/scala/ml/dmlc/xgboost4j/scala/spark/TmpFolderPerSuite.scala b/jvm-packages/xgboost4j-spark/src/test/scala/ml/dmlc/xgboost4j/scala/spark/TmpFolderPerSuite.scala old mode 100755 new mode 100644 diff --git a/jvm-packages/xgboost4j/LICENSE b/jvm-packages/xgboost4j/LICENSE index 9a1673be2ed6..f02ba610d0a3 100644 --- a/jvm-packages/xgboost4j/LICENSE +++ b/jvm-packages/xgboost4j/LICENSE @@ -1,10 +1,10 @@ /* -Copyright (c) 2014 by Contributors +Copyright (c) 2014 by Contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software @@ -12,4 +12,4 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/ \ No newline at end of file +*/ diff --git a/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/XGBoostJNI.java b/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/XGBoostJNI.java index 8250162b37fa..eda4f4e56565 100644 --- a/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/XGBoostJNI.java +++ b/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/XGBoostJNI.java @@ -89,7 +89,7 @@ public final static native int XGDMatrixCreateFromMatRef(long dataRef, int nrow, /** * Set the feature information - * + * * @param handle the DMatrix native address * @param field "feature_names" or "feature_types" * @param values an array of string diff --git a/jvm-packages/xgboost4j/src/main/resources/xgboost4j-version.properties b/jvm-packages/xgboost4j/src/main/resources/xgboost4j-version.properties index e5683df88cb1..defbd48204e4 100644 --- a/jvm-packages/xgboost4j/src/main/resources/xgboost4j-version.properties +++ b/jvm-packages/xgboost4j/src/main/resources/xgboost4j-version.properties @@ -1 +1 @@ -version=${project.version} \ No newline at end of file +version=${project.version} diff --git a/jvm-packages/xgboost4j/src/native/jvm_utils.h b/jvm-packages/xgboost4j/src/native/jvm_utils.h index 3a940ddf7378..8c7c21f2da71 100644 --- a/jvm-packages/xgboost4j/src/native/jvm_utils.h +++ b/jvm-packages/xgboost4j/src/native/jvm_utils.h @@ -1,8 +1,8 @@ /** * Copyright 2014-2025, XGBoost Contributors */ -#ifndef JVM_UTILS_H_ -#define JVM_UTILS_H_ +#ifndef XGBOOST_NATIVE_JVM_UTILS_H_ +#define XGBOOST_NATIVE_JVM_UTILS_H_ #include @@ -28,4 +28,4 @@ T CheckJvmCall(T const &v, JNIEnv *jenv) { return v; } -#endif // JVM_UTILS_H_ +#endif // XGBOOST_NATIVE_JVM_UTILS_H_ diff --git a/jvm-packages/xgboost4j/src/native/xgboost4j-gpu.cu b/jvm-packages/xgboost4j/src/native/xgboost4j-gpu.cu index 6adffb39447a..5af31dbbdf9c 100644 --- a/jvm-packages/xgboost4j/src/native/xgboost4j-gpu.cu +++ b/jvm-packages/xgboost4j/src/native/xgboost4j-gpu.cu @@ -4,6 +4,11 @@ #include #include +#include +#include +#include +#include + #include "../../../../src/common/common.h" #include "../../../../src/common/cuda_pinned_allocator.h" #include "../../../../src/common/device_vector.cuh" // for device_vector diff --git a/jvm-packages/xgboost4j/src/native/xgboost4j.cpp b/jvm-packages/xgboost4j/src/native/xgboost4j.cpp index 6d258c4a3693..d4e0912278ef 100644 --- a/jvm-packages/xgboost4j/src/native/xgboost4j.cpp +++ b/jvm-packages/xgboost4j/src/native/xgboost4j.cpp @@ -12,7 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -*/ + */ #include "./xgboost4j.h" @@ -31,10 +31,10 @@ #include #include -#include "jvm_utils.h" // for JVM_CHECK_CALL #include "../../../../src/c_api/c_api_error.h" #include "../../../../src/c_api/c_api_utils.h" #include "../../../../src/data/array_interface.h" // for ArrayInterface +#include "jvm_utils.h" // for JVM_CHECK_CALL // helper functions // set handle @@ -47,8 +47,8 @@ void setHandle(JNIEnv *jenv, jlongArray jhandle, void *handle) { jenv->SetLongArrayRegion(jhandle, 0, 1, &out); } -JavaVM*& GlobalJvm() { - static JavaVM* vm; +JavaVM *&GlobalJvm() { + static JavaVM *vm; return vm; } @@ -63,12 +63,11 @@ template using Deleter = std::function; } // anonymous namespace -XGB_EXTERN_C int XGBoost4jCallbackDataIterNext( - DataIterHandle data_handle, - XGBCallbackSetData* set_function, - DataHolderHandle set_function_handle) { +XGB_EXTERN_C int XGBoost4jCallbackDataIterNext(DataIterHandle data_handle, + XGBCallbackSetData *set_function, + DataHolderHandle set_function_handle) { jobject jiter = static_cast(data_handle); - JNIEnv* jenv; + JNIEnv *jenv; int jni_status = GlobalJvm()->GetEnv((void **)&jenv, JNI_VERSION_1_6); if (jni_status == JNI_EDETACHED) { GlobalJvm()->AttachCurrentThread(reinterpret_cast(&jenv), nullptr); @@ -77,10 +76,8 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext( } try { jclass iterClass = jenv->FindClass("java/util/Iterator"); - jmethodID hasNext = jenv->GetMethodID(iterClass, - "hasNext", "()Z"); - jmethodID next = jenv->GetMethodID(iterClass, - "next", "()Ljava/lang/Object;"); + jmethodID hasNext = jenv->GetMethodID(iterClass, "hasNext", "()Z"); + jmethodID next = jenv->GetMethodID(iterClass, "next", "()Ljava/lang/Object;"); int ret_value; if (jenv->CallBooleanMethod(jiter, hasNext)) { ret_value = 1; @@ -92,18 +89,17 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext( } jclass batchClass = jenv->GetObjectClass(batch); - jlongArray joffset = (jlongArray)jenv->GetObjectField( - batch, jenv->GetFieldID(batchClass, "rowOffset", "[J")); - jfloatArray jlabel = (jfloatArray)jenv->GetObjectField( - batch, jenv->GetFieldID(batchClass, "label", "[F")); - jfloatArray jweight = (jfloatArray)jenv->GetObjectField( - batch, jenv->GetFieldID(batchClass, "weight", "[F")); + jlongArray joffset = + (jlongArray)jenv->GetObjectField(batch, jenv->GetFieldID(batchClass, "rowOffset", "[J")); + jfloatArray jlabel = + (jfloatArray)jenv->GetObjectField(batch, jenv->GetFieldID(batchClass, "label", "[F")); + jfloatArray jweight = + (jfloatArray)jenv->GetObjectField(batch, jenv->GetFieldID(batchClass, "weight", "[F")); jintArray jindex = (jintArray)jenv->GetObjectField( batch, jenv->GetFieldID(batchClass, "featureIndex", "[I")); jfloatArray jvalue = (jfloatArray)jenv->GetObjectField( batch, jenv->GetFieldID(batchClass, "featureValue", "[F")); - jint jcols = jenv->GetIntField( - batch, jenv->GetFieldID(batchClass, "featureCols", "I")); + jint jcols = jenv->GetIntField(batch, jenv->GetFieldID(batchClass, "featureCols", "I")); std::unique_ptr> cbatch{ [&] { @@ -178,7 +174,7 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext( GlobalJvm()->DetachCurrentThread(); } return ret_value; - } catch(dmlc::Error const& e) { + } catch (dmlc::Error const &e) { // only detach if it is a async call. if (jni_status == JNI_EDETACHED) { GlobalJvm()->DetachCurrentThread(); @@ -193,10 +189,10 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext( * Method: XGBGetLastError * Signature: ()Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBGetLastError - (JNIEnv *jenv, jclass jcls) { +JNIEXPORT jstring JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBGetLastError(JNIEnv *jenv, + jclass jcls) { jstring jresult = 0; - const char* result = XGBGetLastError(); + const char *result = XGBGetLastError(); if (result) { jresult = jenv->NewStringUTF(result); } @@ -208,8 +204,9 @@ JNIEXPORT jstring JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBGetLastError * Method: XGDMatrixCreateFromDataIter * Signature: (Ljava/util/Iterator;Ljava/lang/String;[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromDataIter - (JNIEnv *jenv, jclass jcls, jobject jiter, jstring jcache_info, jfloat jmissing, jlongArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromDataIter( + JNIEnv *jenv, jclass jcls, jobject jiter, jstring jcache_info, jfloat jmissing, + jlongArray jout) { DMatrixHandle result; std::unique_ptr> cache_info; if (jcache_info != nullptr) { @@ -218,9 +215,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFro }}; } auto missing = static_cast(jmissing); - int ret = - XGDMatrixCreateFromDataIter(jiter, XGBoost4jCallbackDataIterNext, cache_info.get(), - missing,&result); + int ret = XGDMatrixCreateFromDataIter(jiter, XGBoost4jCallbackDataIterNext, cache_info.get(), + missing, &result); JVM_CHECK_CALL(ret); setHandle(jenv, jout, result); return ret; @@ -339,29 +335,31 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFro * Method: XGDMatrixCreateFromMatRef * Signature: (JIIF)J */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMatRef - (JNIEnv *jenv, jclass jcls, jlong jdataRef, jint jnrow, jint jncol, jfloat jmiss, jlongArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMatRef( + JNIEnv *jenv, jclass jcls, jlong jdataRef, jint jnrow, jint jncol, jfloat jmiss, + jlongArray jout) { DMatrixHandle result; bst_ulong nrow = (bst_ulong)jnrow; bst_ulong ncol = (bst_ulong)jncol; - jint ret = (jint) XGDMatrixCreateFromMat((float const *)jdataRef, nrow, ncol, jmiss, &result); + jint ret = (jint)XGDMatrixCreateFromMat((float const *)jdataRef, nrow, ncol, jmiss, &result); JVM_CHECK_CALL(ret); setHandle(jenv, jout, result); return ret; } - /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromMat * Signature: ([FIIF)J */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMat - (JNIEnv *jenv, jclass jcls, jfloatArray jdata, jint jnrow, jint jncol, jfloat jmiss, jlongArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMat( + JNIEnv *jenv, jclass jcls, jfloatArray jdata, jint jnrow, jint jncol, jfloat jmiss, + jlongArray jout) { DMatrixHandle result; - std::unique_ptr> data{jenv->GetFloatArrayElements(jdata, 0), [&](jfloat* ptr) { - jenv->ReleaseFloatArrayElements(jdata, ptr, 0); - }}; + std::unique_ptr> data{jenv->GetFloatArrayElements(jdata, 0), + [&](jfloat *ptr) { + jenv->ReleaseFloatArrayElements(jdata, ptr, 0); + }}; bst_ulong nrow = (bst_ulong)jnrow; bst_ulong ncol = (bst_ulong)jncol; @@ -382,7 +380,8 @@ auto SliceDMatrixWinWar(DMatrixHandle handle, T *ptr, std::size_t len, DMatrixHa } template <> -auto SliceDMatrixWinWar(DMatrixHandle handle, long *ptr, std::size_t len, DMatrixHandle *result) { +auto SliceDMatrixWinWar(DMatrixHandle handle, long *ptr, std::size_t len, + DMatrixHandle *result) { std::vector copy(len); std::copy_n(ptr, len, copy.begin()); // default to not allowing slicing with group ID specified -- feel free to add if necessary @@ -416,9 +415,10 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSliceDMat * Method: XGDMatrixFree * Signature: (J)V */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixFree - (JNIEnv *jenv, jclass jcls, jlong jhandle) { - DMatrixHandle handle = (DMatrixHandle) jhandle; +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixFree(JNIEnv *jenv, + jclass jcls, + jlong jhandle) { + DMatrixHandle handle = (DMatrixHandle)jhandle; int ret = XGDMatrixFree(handle); return ret; } @@ -471,8 +471,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetFloatI * Method: XGDMatrixSetUIntInfo * Signature: (JLjava/lang/String;[I)V */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetUIntInfo - (JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jintArray jarray) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetUIntInfo( + JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jintArray jarray) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> field{ jenv->GetStringUTFChars(jfield, nullptr), [&](char const *ptr) { @@ -494,8 +494,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetUIntIn * Method: XGDMatrixGetFloatInfo * Signature: (JLjava/lang/String;)[F */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetFloatInfo - (JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetFloatInfo( + JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> field{ jenv->GetStringUTFChars(jfield, nullptr), [&](char const *ptr) { @@ -505,13 +505,13 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetFloatI }}; bst_ulong len; float *result; - int ret = XGDMatrixGetFloatInfo(handle, field.get(), &len, (const float**) &result); + int ret = XGDMatrixGetFloatInfo(handle, field.get(), &len, (const float **)&result); JVM_CHECK_CALL(ret); - jsize jlen = (jsize) len; + jsize jlen = (jsize)len; jfloatArray jarray = jenv->NewFloatArray(jlen); - jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *) result); - jenv->SetObjectArrayElement(jout, 0, (jobject) jarray); + jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *)result); + jenv->SetObjectArrayElement(jout, 0, (jobject)jarray); return ret; } @@ -521,8 +521,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetFloatI * Method: XGDMatrixGetUIntInfo * Signature: (JLjava/lang/String;)[I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetUIntInfo - (JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetUIntInfo( + JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> field{ jenv->GetStringUTFChars(jfield, nullptr), [&](char const *ptr) { @@ -535,9 +535,9 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetUIntIn int ret = (jint)XGDMatrixGetUIntInfo(handle, field.get(), &len, (const unsigned int **)&result); JVM_CHECK_CALL(ret); - jsize jlen = (jsize) len; + jsize jlen = (jsize)len; jintArray jarray = jenv->NewIntArray(jlen); - jenv->SetIntArrayRegion(jarray, 0, jlen, (jint *) result); + jenv->SetIntArrayRegion(jarray, 0, jlen, (jint *)result); jenv->SetObjectArrayElement(jout, 0, jarray); return ret; } @@ -547,13 +547,15 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetUIntIn * Method: XGDMatrixNumRow * Signature: (J)J */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixNumRow - (JNIEnv *jenv, jclass jcls, jlong jhandle, jlongArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixNumRow(JNIEnv *jenv, + jclass jcls, + jlong jhandle, + jlongArray jout) { auto handle = reinterpret_cast(jhandle); bst_ulong result[1]; - int ret = (jint) XGDMatrixNumRow(handle, result); + int ret = (jint)XGDMatrixNumRow(handle, result); JVM_CHECK_CALL(ret); - jenv->SetLongArrayRegion(jout, 0, 1, (const jlong *) result); + jenv->SetLongArrayRegion(jout, 0, 1, (const jlong *)result); return ret; } @@ -579,8 +581,10 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixNumNonMis * Method: XGBoosterCreate * Signature: ([J)J */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterCreate - (JNIEnv *jenv, jclass jcls, jlongArray jhandles, jlongArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterCreate(JNIEnv *jenv, + jclass jcls, + jlongArray jhandles, + jlongArray jout) { std::vector handles; if (jhandles != nullptr) { size_t len = jenv->GetArrayLength(jhandles); @@ -641,8 +645,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetParam( * Method: XGBoosterUpdateOneIter * Signature: (JIJ)V */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterUpdateOneIter - (JNIEnv *jenv, jclass jcls, jlong jhandle, jint jiter, jlong jdtrain) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterUpdateOneIter( + JNIEnv *jenv, jclass jcls, jlong jhandle, jint jiter, jlong jdtrain) { auto handle = reinterpret_cast(jhandle); auto dtrain = reinterpret_cast(jdtrain); return XGBoosterUpdateOneIter(handle, jiter, dtrain); @@ -695,19 +699,20 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterTrainOneI * Method: XGBoosterEvalOneIter * Signature: (JI[J[Ljava/lang/String;)Ljava/lang/String; */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterEvalOneIter - (JNIEnv *jenv, jclass jcls, jlong jhandle, jint jiter, jlongArray jdmats, jobjectArray jevnames, jobjectArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterEvalOneIter( + JNIEnv *jenv, jclass jcls, jlong jhandle, jint jiter, jlongArray jdmats, jobjectArray jevnames, + jobjectArray jout) { auto handle = reinterpret_cast(jhandle); std::vector dmats; std::vector evnames; - std::vector evchars; + std::vector evchars; - size_t len = static_cast(jenv->GetArrayLength(jdmats)); + size_t len = static_cast(jenv->GetArrayLength(jdmats)); // put handle from jhandles to chandles - std::unique_ptr> cjdmats{ - jenv->GetLongArrayElements(jdmats, nullptr), [&](jlong *ptr) { - jenv->ReleaseLongArrayElements(jdmats, ptr, 0); - }}; + std::unique_ptr> cjdmats{jenv->GetLongArrayElements(jdmats, nullptr), + [&](jlong *ptr) { + jenv->ReleaseLongArrayElements(jdmats, ptr, 0); + }}; for (size_t i = 0; i < len; ++i) { dmats.push_back(reinterpret_cast(cjdmats.get()[i])); jstring jevname = (jstring)jenv->GetObjectArrayElement(jevnames, i); @@ -738,20 +743,22 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterEvalOneIt * Method: XGBoosterPredict * Signature: (JJIJ)[F */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterPredict - (JNIEnv *jenv, jclass jcls, jlong jhandle, jlong jdmat, jint joption_mask, jint jntree_limit, jobjectArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterPredict( + JNIEnv *jenv, jclass jcls, jlong jhandle, jlong jdmat, jint joption_mask, jint jntree_limit, + jobjectArray jout) { auto handle = reinterpret_cast(jhandle); auto dmat = reinterpret_cast(jdmat); bst_ulong len; float *result; - int ret = XGBoosterPredict(handle, dmat, joption_mask, (unsigned int) jntree_limit, - /* training = */ 0, // Currently this parameter is not supported by JVM - &len, (const float **) &result); + int ret = + XGBoosterPredict(handle, dmat, joption_mask, (unsigned int)jntree_limit, + /* training = */ 0, // Currently this parameter is not supported by JVM + &len, (const float **)&result); JVM_CHECK_CALL(ret); if (len) { - jsize jlen = (jsize) len; + jsize jlen = (jsize)len; jfloatArray jarray = jenv->NewFloatArray(jlen); - jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *) result); + jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *)result); jenv->SetObjectArrayElement(jout, 0, jarray); } return ret; @@ -776,9 +783,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterPredictFr jfloat *data = jenv->GetFloatArrayElements(jdata, nullptr); xgboost::Context ctx; auto t_data = linalg::MakeTensorView( - ctx.Device(), - xgboost::common::Span{data, static_cast(num_rows * num_features)}, num_rows, - num_features); + ctx.Device(), xgboost::common::Span{data, static_cast(num_rows * num_features)}, + num_rows, num_features); auto s_array = linalg::ArrayInterfaceStr(t_data); /** @@ -892,8 +898,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterLoadModel * Method: XGBoosterSaveModelToBuffer * Signature: (JLjava/lang/String;[[B)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSaveModelToBuffer - (JNIEnv * jenv, jclass jcls, jlong jhandle, jstring jformat, jobjectArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSaveModelToBuffer( + JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jformat, jobjectArray jout) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> format{ jenv->GetStringUTFChars(jformat, nullptr), [&](char const *ptr) { @@ -923,8 +929,9 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSaveModel * Method: XGBoosterDumpModelEx * Signature: (JLjava/lang/String;ILjava/lang/String;[[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelEx - (JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfmap, jint jwith_stats, jstring jformat, jobjectArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelEx( + JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfmap, jint jwith_stats, jstring jformat, + jobjectArray jout) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> fmap{jenv->GetStringUTFChars(jfmap, nullptr), [&](char const *ptr) { @@ -944,10 +951,11 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModel int ret = XGBoosterDumpModelEx(handle, fmap.get(), jwith_stats, format.get(), &len, &result); JVM_CHECK_CALL(ret); - jsize jlen = (jsize) len; - jobjectArray jinfos = jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF("")); - for(int i=0 ; iSetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char*) result[i])); + jsize jlen = (jsize)len; + jobjectArray jinfos = + jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF("")); + for (int i = 0; i < jlen; i++) { + jenv->SetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char *)result[i])); } jenv->SetObjectArrayElement(jout, 0, jinfos); @@ -959,14 +967,14 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModel * Method: XGBoosterDumpModelExWithFeatures * Signature: (J[Ljava/lang/String;ILjava/lang/String;[[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelExWithFeatures - (JNIEnv *jenv, jclass jcls, jlong jhandle, jobjectArray jfeature_names, jint jwith_stats, +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelExWithFeatures( + JNIEnv *jenv, jclass jcls, jlong jhandle, jobjectArray jfeature_names, jint jwith_stats, jstring jformat, jobjectArray jout) { auto handle = reinterpret_cast(jhandle); bst_ulong feature_num = (bst_ulong)jenv->GetArrayLength(jfeature_names); std::vector feature_names; - std::vector feature_names_char; + std::vector feature_names_char; std::string feature_type_q = "q"; std::vector feature_types_char; @@ -1006,10 +1014,11 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModel (const char ***)&result); JVM_CHECK_CALL(ret); - jsize jlen = (jsize) len; - jobjectArray jinfos = jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF("")); - for(int i=0 ; iSetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char*) result[i])); + jsize jlen = (jsize)len; + jobjectArray jinfos = + jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF("")); + for (int i = 0; i < jlen; i++) { + jenv->SetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char *)result[i])); } jenv->SetObjectArrayElement(jout, 0, jinfos); @@ -1021,18 +1030,19 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModel * Method: XGBoosterGetAttrNames * Signature: (J[[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetAttrNames - (JNIEnv *jenv, jclass jcls, jlong jhandle, jobjectArray jout) { - BoosterHandle handle = (BoosterHandle) jhandle; +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetAttrNames( + JNIEnv *jenv, jclass jcls, jlong jhandle, jobjectArray jout) { + BoosterHandle handle = (BoosterHandle)jhandle; bst_ulong len = 0; char **result; - int ret = XGBoosterGetAttrNames(handle, &len, (const char ***) &result); + int ret = XGBoosterGetAttrNames(handle, &len, (const char ***)&result); JVM_CHECK_CALL(ret); - jsize jlen = (jsize) len; - jobjectArray jinfos = jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF("")); - for(int i=0 ; iSetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char*) result[i])); + jsize jlen = (jsize)len; + jobjectArray jinfos = + jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF("")); + for (int i = 0; i < jlen; i++) { + jenv->SetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char *)result[i])); } jenv->SetObjectArrayElement(jout, 0, jinfos); @@ -1262,8 +1272,9 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorPrint( * Method: CommunicatorGetRank * Signature: ([I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetRank - (JNIEnv *jenv, jclass jcls, jintArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetRank(JNIEnv *jenv, + jclass jcls, + jintArray jout) { jint rank = XGCommunicatorGetRank(); jenv->SetIntArrayRegion(jout, 0, 1, &rank); return 0; @@ -1274,8 +1285,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetRan * Method: CommunicatorGetWorldSize * Signature: ([I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetWorldSize - (JNIEnv *jenv, jclass jcls, jintArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetWorldSize( + JNIEnv *jenv, jclass jcls, jintArray jout) { jint out = XGCommunicatorGetWorldSize(); jenv->SetIntArrayRegion(jout, 0, 1, &out); return 0; @@ -1286,10 +1297,10 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetWor * Method: CommunicatorAllreduce * Signature: (Ljava/nio/ByteBuffer;III)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorAllreduce - (JNIEnv *jenv, jclass jcls, jobject jsendrecvbuf, jint jcount, jint jenum_dtype, jint jenum_op) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorAllreduce( + JNIEnv *jenv, jclass jcls, jobject jsendrecvbuf, jint jcount, jint jenum_dtype, jint jenum_op) { void *ptr_sendrecvbuf = jenv->GetDirectBufferAddress(jsendrecvbuf); - JVM_CHECK_CALL(XGCommunicatorAllreduce(ptr_sendrecvbuf, (size_t) jcount, jenum_dtype, jenum_op)); + JVM_CHECK_CALL(XGCommunicatorAllreduce(ptr_sendrecvbuf, (size_t)jcount, jenum_dtype, jenum_op)); return 0; } @@ -1304,8 +1315,7 @@ int QdmFromCallback(JNIEnv *jenv, jobject jdata_iter, jlongArray jref, char cons * Signature: (Ljava/util/Iterator;[JLjava/lang/String;[J)I */ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGQuantileDMatrixCreateFromCallback( - JNIEnv *jenv, jclass, jobject jdata_iter, jlongArray jref, jstring jconf, - jlongArray jout) { + JNIEnv *jenv, jclass, jobject jdata_iter, jlongArray jref, jstring jconf, jlongArray jout) { std::unique_ptr> conf{jenv->GetStringUTFChars(jconf, nullptr), [&](char const *ptr) { jenv->ReleaseStringUTFChars(jconf, ptr); @@ -1354,8 +1364,10 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetInfoFr * Method: XGDMatrixCreateFromArrayInterfaceColumns * Signature: (Ljava/lang/String;FI[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromArrayInterfaceColumns - (JNIEnv *jenv, jclass jcls, jstring jjson_columns, jfloat jmissing, jint jnthread, jlongArray jout) { +JNIEXPORT jint JNICALL +Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromArrayInterfaceColumns( + JNIEnv *jenv, jclass jcls, jstring jjson_columns, jfloat jmissing, jint jnthread, + jlongArray jout) { DMatrixHandle result; std::unique_ptr> cjson_columns{ jenv->GetStringUTFChars(jjson_columns, nullptr), [&](char const *ptr) { @@ -1374,8 +1386,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFro return ret; } -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetStrFeatureInfo - (JNIEnv *jenv, jclass jclz, jlong jhandle, jstring jfield, jobjectArray jvalues) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetStrFeatureInfo( + JNIEnv *jenv, jclass jclz, jlong jhandle, jstring jfield, jobjectArray jvalues) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> field{jenv->GetStringUTFChars(jfield, nullptr), [&](char const *ptr) { @@ -1438,10 +1450,8 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetStrFea * Method: XGBoosterSetStrFeatureInfo * Signature: (JLjava/lang/String;[Ljava/lang/String;])I */ -JNIEXPORT jint JNICALL -Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetStrFeatureInfo( - JNIEnv *jenv, jclass jclz, jlong jhandle, jstring jfield, - jobjectArray jfeatures) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetStrFeatureInfo( + JNIEnv *jenv, jclass jclz, jlong jhandle, jstring jfield, jobjectArray jfeatures) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> field{jenv->GetStringUTFChars(jfield, nullptr), @@ -1451,7 +1461,7 @@ Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetStrFeatureInfo( bst_ulong feature_num = (bst_ulong)jenv->GetArrayLength(jfeatures); std::vector features; - std::vector features_char; + std::vector features_char; for (bst_ulong i = 0; i < feature_num; ++i) { jstring jfeature = (jstring)jenv->GetObjectArrayElement(jfeatures, i); @@ -1477,10 +1487,8 @@ Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetStrFeatureInfo( * Method: XGBoosterSetGtrFeatureInfo * Signature: (JLjava/lang/String;[Ljava/lang/String;])I */ -JNIEXPORT jint JNICALL -Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetStrFeatureInfo( - JNIEnv *jenv, jclass jclz, jlong jhandle, jstring jfield, - jobjectArray jout) { +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetStrFeatureInfo( + JNIEnv *jenv, jclass jclz, jlong jhandle, jstring jfield, jobjectArray jout) { auto handle = reinterpret_cast(jhandle); std::unique_ptr> field{jenv->GetStringUTFChars(jfield, nullptr), [&](char const *ptr) { diff --git a/jvm-packages/xgboost4j/src/native/xgboost4j.h b/jvm-packages/xgboost4j/src/native/xgboost4j.h index f5ff559a1b79..696efeb655fb 100644 --- a/jvm-packages/xgboost4j/src/native/xgboost4j.h +++ b/jvm-packages/xgboost4j/src/native/xgboost4j.h @@ -12,456 +12,487 @@ extern "C" { * Method: XGBGetLastError * Signature: ()Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBGetLastError - (JNIEnv *, jclass); +JNIEXPORT jstring JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBGetLastError(JNIEnv *, jclass); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromFile * Signature: (Ljava/lang/String;I[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromFile - (JNIEnv *, jclass, jstring, jint, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromFile(JNIEnv *, + jclass, + jstring, jint, + jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromDataIter * Signature: (Ljava/util/Iterator;Ljava/lang/String;F[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromDataIter - (JNIEnv *, jclass, jobject, jstring, jfloat, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromDataIter( + JNIEnv *, jclass, jobject, jstring, jfloat, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromCSR * Signature: ([J[I[FIFI[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromCSR - (JNIEnv *, jclass, jlongArray, jintArray, jfloatArray, jint, jfloat, jint, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromCSR( + JNIEnv *, jclass, jlongArray, jintArray, jfloatArray, jint, jfloat, jint, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromCSC * Signature: ([J[I[FIFI[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromCSC - (JNIEnv *, jclass, jlongArray, jintArray, jfloatArray, jint, jfloat, jint, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromCSC( + JNIEnv *, jclass, jlongArray, jintArray, jfloatArray, jint, jfloat, jint, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromMat * Signature: ([FIIF[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMat - (JNIEnv *, jclass, jfloatArray, jint, jint, jfloat, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMat( + JNIEnv *, jclass, jfloatArray, jint, jint, jfloat, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromMatRef * Signature: (JIIF[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMatRef - (JNIEnv *, jclass, jlong, jint, jint, jfloat, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromMatRef( + JNIEnv *, jclass, jlong, jint, jint, jfloat, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixSliceDMatrix * Signature: (J[I[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSliceDMatrix - (JNIEnv *, jclass, jlong, jintArray, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSliceDMatrix(JNIEnv *, + jclass, jlong, + jintArray, + jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixFree * Signature: (J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixFree - (JNIEnv *, jclass, jlong); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixFree(JNIEnv *, jclass, + jlong); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixSaveBinary * Signature: (JLjava/lang/String;I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSaveBinary - (JNIEnv *, jclass, jlong, jstring, jint); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSaveBinary(JNIEnv *, jclass, + jlong, jstring, + jint); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixSetFloatInfo * Signature: (JLjava/lang/String;[F)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetFloatInfo - (JNIEnv *, jclass, jlong, jstring, jfloatArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetFloatInfo(JNIEnv *, + jclass, jlong, + jstring, + jfloatArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixSetUIntInfo * Signature: (JLjava/lang/String;[I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetUIntInfo - (JNIEnv *, jclass, jlong, jstring, jintArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetUIntInfo(JNIEnv *, jclass, + jlong, jstring, + jintArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixGetFloatInfo * Signature: (JLjava/lang/String;[[F)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetFloatInfo - (JNIEnv *, jclass, jlong, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetFloatInfo(JNIEnv *, + jclass, jlong, + jstring, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixGetUIntInfo * Signature: (JLjava/lang/String;[[I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetUIntInfo - (JNIEnv *, jclass, jlong, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetUIntInfo(JNIEnv *, jclass, + jlong, jstring, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixSetStrFeatureInfo * Signature: (JLjava/lang/String;[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetStrFeatureInfo - (JNIEnv *, jclass, jlong, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetStrFeatureInfo( + JNIEnv *, jclass, jlong, jstring, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixGetStrFeatureInfo * Signature: (JLjava/lang/String;[J[[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetStrFeatureInfo - (JNIEnv *, jclass, jlong, jstring, jlongArray, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetStrFeatureInfo( + JNIEnv *, jclass, jlong, jstring, jlongArray, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixNumRow * Signature: (J[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixNumRow - (JNIEnv *, jclass, jlong, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixNumRow(JNIEnv *, jclass, + jlong, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixNumNonMissing * Signature: (J[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixNumNonMissing - (JNIEnv *, jclass, jlong, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixNumNonMissing(JNIEnv *, + jclass, jlong, + jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterCreate * Signature: ([J[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterCreate - (JNIEnv *, jclass, jlongArray, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterCreate(JNIEnv *, jclass, + jlongArray, + jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterFree * Signature: (J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterFree - (JNIEnv *, jclass, jlong); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterFree(JNIEnv *, jclass, + jlong); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterSetParam * Signature: (JLjava/lang/String;Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetParam - (JNIEnv *, jclass, jlong, jstring, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetParam(JNIEnv *, jclass, + jlong, jstring, + jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterUpdateOneIter * Signature: (JIJ)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterUpdateOneIter - (JNIEnv *, jclass, jlong, jint, jlong); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterUpdateOneIter(JNIEnv *, + jclass, jlong, + jint, jlong); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterTrainOneIter * Signature: (JJI[F[F)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterTrainOneIter - (JNIEnv *, jclass, jlong, jlong, jint, jfloatArray, jfloatArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterTrainOneIter( + JNIEnv *, jclass, jlong, jlong, jint, jfloatArray, jfloatArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterEvalOneIter * Signature: (JI[J[Ljava/lang/String;[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterEvalOneIter - (JNIEnv *, jclass, jlong, jint, jlongArray, jobjectArray, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterEvalOneIter( + JNIEnv *, jclass, jlong, jint, jlongArray, jobjectArray, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterPredict * Signature: (JJII[[F)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterPredict - (JNIEnv *, jclass, jlong, jlong, jint, jint, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterPredict(JNIEnv *, jclass, + jlong, jlong, jint, + jint, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterPredictFromDense * Signature: (J[FJJFIII[F[[F)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterPredictFromDense - (JNIEnv *, jclass, jlong, jfloatArray, jlong, jlong, jfloat, jint, jint, jint, jfloatArray, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterPredictFromDense( + JNIEnv *, jclass, jlong, jfloatArray, jlong, jlong, jfloat, jint, jint, jint, jfloatArray, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterLoadModel * Signature: (JLjava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterLoadModel - (JNIEnv *, jclass, jlong, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterLoadModel(JNIEnv *, jclass, + jlong, jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterSaveModel * Signature: (JLjava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSaveModel - (JNIEnv *, jclass, jlong, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSaveModel(JNIEnv *, jclass, + jlong, jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterLoadModelFromBuffer * Signature: (J[B)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterLoadModelFromBuffer - (JNIEnv *, jclass, jlong, jbyteArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterLoadModelFromBuffer( + JNIEnv *, jclass, jlong, jbyteArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterSaveModelToBuffer * Signature: (JLjava/lang/String;[[B)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSaveModelToBuffer - (JNIEnv *, jclass, jlong, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSaveModelToBuffer( + JNIEnv *, jclass, jlong, jstring, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterDumpModelEx * Signature: (JLjava/lang/String;ILjava/lang/String;[[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelEx - (JNIEnv *, jclass, jlong, jstring, jint, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelEx(JNIEnv *, jclass, + jlong, jstring, + jint, jstring, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterDumpModelExWithFeatures * Signature: (J[Ljava/lang/String;ILjava/lang/String;[[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelExWithFeatures - (JNIEnv *, jclass, jlong, jobjectArray, jint, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterDumpModelExWithFeatures( + JNIEnv *, jclass, jlong, jobjectArray, jint, jstring, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterGetAttrNames * Signature: (J[[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetAttrNames - (JNIEnv *, jclass, jlong, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetAttrNames(JNIEnv *, + jclass, jlong, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterGetAttr * Signature: (JLjava/lang/String;[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetAttr - (JNIEnv *, jclass, jlong, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetAttr(JNIEnv *, jclass, + jlong, jstring, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterSetAttr * Signature: (JLjava/lang/String;Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetAttr - (JNIEnv *, jclass, jlong, jstring, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetAttr(JNIEnv *, jclass, + jlong, jstring, + jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterGetNumFeature * Signature: (J[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetNumFeature - (JNIEnv *, jclass, jlong, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetNumFeature(JNIEnv *, + jclass, jlong, + jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterGetNumBoostedRound * Signature: (J[I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetNumBoostedRound - (JNIEnv *, jclass, jlong, jintArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetNumBoostedRound( + JNIEnv *, jclass, jlong, jintArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: CommunicatorInit * Signature: (Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorInit - (JNIEnv *, jclass, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorInit(JNIEnv *, jclass, + jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: CommunicatorFinalize * Signature: ()I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorFinalize - (JNIEnv *, jclass); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorFinalize(JNIEnv *, + jclass); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: CommunicatorPrint * Signature: (Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorPrint - (JNIEnv *, jclass, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorPrint(JNIEnv *, jclass, + jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: CommunicatorGetRank * Signature: ([I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetRank - (JNIEnv *, jclass, jintArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetRank(JNIEnv *, jclass, + jintArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: CommunicatorGetWorldSize * Signature: ([I)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetWorldSize - (JNIEnv *, jclass, jintArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorGetWorldSize(JNIEnv *, + jclass, + jintArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: TrackerCreate * Signature: (Ljava/lang/String;IIIJ[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerCreate - (JNIEnv *, jclass, jstring, jint, jint, jint, jlong, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerCreate(JNIEnv *, jclass, + jstring, jint, jint, + jint, jlong, + jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: TrackerRun * Signature: (J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerRun - (JNIEnv *, jclass, jlong); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerRun(JNIEnv *, jclass, jlong); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: TrackerWaitFor * Signature: (JJ)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerWaitFor - (JNIEnv *, jclass, jlong, jlong); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerWaitFor(JNIEnv *, jclass, + jlong, jlong); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: TrackerWorkerArgs * Signature: (JJ[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerWorkerArgs - (JNIEnv *, jclass, jlong, jlong, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerWorkerArgs(JNIEnv *, jclass, + jlong, jlong, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: TrackerFree * Signature: (J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerFree - (JNIEnv *, jclass, jlong); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_TrackerFree(JNIEnv *, jclass, jlong); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: CommunicatorAllreduce * Signature: (Ljava/nio/ByteBuffer;III)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorAllreduce - (JNIEnv *, jclass, jobject, jint, jint, jint); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_CommunicatorAllreduce(JNIEnv *, + jclass, jobject, + jint, jint, + jint); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixSetInfoFromInterface * Signature: (JLjava/lang/String;Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetInfoFromInterface - (JNIEnv *, jclass, jlong, jstring, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixSetInfoFromInterface( + JNIEnv *, jclass, jlong, jstring, jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGQuantileDMatrixCreateFromCallback * Signature: (Ljava/util/Iterator;[JLjava/lang/String;[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGQuantileDMatrixCreateFromCallback - (JNIEnv *, jclass, jobject, jlongArray, jstring, jlongArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGQuantileDMatrixCreateFromCallback( + JNIEnv *, jclass, jobject, jlongArray, jstring, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGExtMemQuantileDMatrixCreateFromCallback * Signature: (Ljava/util/Iterator;[JLjava/lang/String;[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGExtMemQuantileDMatrixCreateFromCallback - (JNIEnv *, jclass, jobject, jlongArray, jstring, jlongArray); +JNIEXPORT jint JNICALL +Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGExtMemQuantileDMatrixCreateFromCallback( + JNIEnv *, jclass, jobject, jlongArray, jstring, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixCreateFromArrayInterfaceColumns * Signature: (Ljava/lang/String;FI[J)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromArrayInterfaceColumns - (JNIEnv *, jclass, jstring, jfloat, jint, jlongArray); +JNIEXPORT jint JNICALL +Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixCreateFromArrayInterfaceColumns(JNIEnv *, jclass, + jstring, jfloat, + jint, jlongArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterSetStrFeatureInfo * Signature: (JLjava/lang/String;[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetStrFeatureInfo - (JNIEnv *, jclass, jlong, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterSetStrFeatureInfo( + JNIEnv *, jclass, jlong, jstring, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBoosterGetStrFeatureInfo * Signature: (JLjava/lang/String;[Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetStrFeatureInfo - (JNIEnv *, jclass, jlong, jstring, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBoosterGetStrFeatureInfo( + JNIEnv *, jclass, jlong, jstring, jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGDMatrixGetQuantileCut * Signature: (J[[J[[F)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetQuantileCut - (JNIEnv *, jclass, jlong, jobjectArray, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGDMatrixGetQuantileCut(JNIEnv *, + jclass, jlong, + jobjectArray, + jobjectArray); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBSetGlobalConfig * Signature: (Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBSetGlobalConfig - (JNIEnv *, jclass, jstring); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBSetGlobalConfig(JNIEnv *, jclass, + jstring); /* * Class: ml_dmlc_xgboost4j_java_XGBoostJNI * Method: XGBGetGlobalConfig * Signature: ([Ljava/lang/String;)I */ -JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBGetGlobalConfig - (JNIEnv *, jclass, jobjectArray); +JNIEXPORT jint JNICALL Java_ml_dmlc_xgboost4j_java_XGBoostJNI_XGBGetGlobalConfig(JNIEnv *, jclass, + jobjectArray); #ifdef __cplusplus } diff --git a/ops/pipeline/build-python-wheels-cpu.sh b/ops/pipeline/build-python-wheels-cpu.sh old mode 100644 new mode 100755 diff --git a/ops/pipeline/get-docker-registry-details.sh b/ops/pipeline/get-docker-registry-details.sh index 000db9a2655a..f55eaade16a4 100755 --- a/ops/pipeline/get-docker-registry-details.sh +++ b/ops/pipeline/get-docker-registry-details.sh @@ -1,3 +1,4 @@ +#!/bin/bash ## Get details for AWS ECR (Elastic Container Registry) in environment variables ECR_AWS_ACCOUNT_ID="492475357299" diff --git a/ops/pipeline/login-docker-registry.sh b/ops/pipeline/login-docker-registry.sh index a03987f484b8..ea5db9f46726 100755 --- a/ops/pipeline/login-docker-registry.sh +++ b/ops/pipeline/login-docker-registry.sh @@ -1,3 +1,4 @@ +#!/bin/bash ## Log into AWS ECR (Elastic Container Registry) to be able to pull containers from it ## Note. Requires valid AWS credentials diff --git a/ops/script/lint_cpp.py b/ops/script/lint_cpp.py index b89f29c6edbe..6c9dcb5526d9 100644 --- a/ops/script/lint_cpp.py +++ b/ops/script/lint_cpp.py @@ -68,7 +68,16 @@ def __init__(self) -> None: ",".join( [ "-build/c++11", - "-build/include,", + "-build/c++17", + "-build/include", + "-runtime/references", + "-readability/nolint", + "-readability/braces", + # In general, trust clang-format to handle whitespace formatting + "-whitespace/braces", + "-whitespace/parens", + "-whitespace/indent_namespace", + "-whitespace/newline", "+build/namespaces", "+build/include_what_you_use", ] diff --git a/ops/script/test_tidy.cc b/ops/script/test_tidy.cc index 7f59b4f3afbe..2b91006ff785 100644 --- a/ops/script/test_tidy.cc +++ b/ops/script/test_tidy.cc @@ -1,3 +1,6 @@ +/** + * Copyright 2026, XGBoost contributors + */ #include #include diff --git a/ops/script/verify_link.sh b/ops/script/verify_link.sh index 8d856adffd86..6225002acf8c 100755 --- a/ops/script/verify_link.sh +++ b/ops/script/verify_link.sh @@ -1,3 +1,4 @@ +#!/bin/bash # Make sure the dependencies of XGBoost don't appear in directly downstream project. # Pass the executable as argument for this script diff --git a/plugin/example/custom_obj.cc b/plugin/example/custom_obj.cc index 86f941945518..c48185784b4f 100644 --- a/plugin/example/custom_obj.cc +++ b/plugin/example/custom_obj.cc @@ -10,6 +10,8 @@ #include #include +#include + namespace xgboost::obj { // This is a helpful data structure to define parameters // You do not have to use it. @@ -19,7 +21,9 @@ struct MyLogisticParam : public XGBoostParameter { float scale_neg_weight; // declare parameters DMLC_DECLARE_PARAMETER(MyLogisticParam) { - DMLC_DECLARE_FIELD(scale_neg_weight).set_default(1.0f).set_lower_bound(0.0f) + DMLC_DECLARE_FIELD(scale_neg_weight) + .set_default(1.0f) + .set_lower_bound(0.0f) .describe("Scale the weight of negative examples by this factor"); } }; @@ -53,12 +57,10 @@ class MyLogistic : public ObjFunction { out_gpair_h(i) = GradientPair(grad, hess); } } - [[nodiscard]] const char* DefaultEvalMetric() const override { - return "logloss"; - } - void PredTransform(HostDeviceVector *io_preds) const override { + [[nodiscard]] const char* DefaultEvalMetric() const override { return "logloss"; } + void PredTransform(HostDeviceVector* io_preds) const override { // transform margin value to probability. - std::vector &preds = io_preds->HostVector(); + std::vector& preds = io_preds->HostVector(); for (auto& pred : preds) { pred = 1.0f / (1.0f + std::exp(-pred)); } @@ -77,9 +79,7 @@ class MyLogistic : public ObjFunction { out["my_logistic_param"] = ToJson(param_); } - void LoadConfig(Json const& in) override { - FromJson(in["my_logistic_param"], ¶m_); - } + void LoadConfig(Json const& in) override { FromJson(in["my_logistic_param"], ¶m_); } private: MyLogisticParam param_; @@ -88,7 +88,7 @@ class MyLogistic : public ObjFunction { // Finally register the objective function. // After it succeeds you can try use xgboost with objective=mylogistic XGBOOST_REGISTER_OBJECTIVE(MyLogistic, "mylogistic") -.describe("User defined logistic regression plugin") -.set_body([]() { return new MyLogistic(); }); + .describe("User defined logistic regression plugin") + .set_body([]() { return new MyLogistic(); }); } // namespace xgboost::obj diff --git a/plugin/federated/federated_coll.cc b/plugin/federated/federated_coll.cc index b62abdada5a5..30adf58c3a8b 100644 --- a/plugin/federated/federated_coll.cc +++ b/plugin/federated/federated_coll.cc @@ -7,6 +7,7 @@ #include #include // for copy_n +#include #include "../../src/collective/allgather.h" #include "../../src/common/common.h" // for AssertGPUSupport diff --git a/plugin/federated/federated_coll.cu b/plugin/federated/federated_coll.cu index 3f604c50d2d2..609eb645115a 100644 --- a/plugin/federated/federated_coll.cu +++ b/plugin/federated/federated_coll.cu @@ -53,7 +53,8 @@ Coll *FederatedColl::MakeCUDAVar() { }; } -[[nodiscard]] Result CUDAFederatedColl::Allgather(Comm const &comm, common::Span data) { +[[nodiscard]] Result CUDAFederatedColl::Allgather(Comm const &comm, + common::Span data) { auto cufed = dynamic_cast(&comm); CHECK(cufed); std::vector h_data(data.size()); diff --git a/plugin/federated/federated_coll.h b/plugin/federated/federated_coll.h index 12443a3e1b5a..d0b4ae669abf 100644 --- a/plugin/federated/federated_coll.h +++ b/plugin/federated/federated_coll.h @@ -2,8 +2,8 @@ * Copyright 2023-2024, XGBoost contributors */ #pragma once -#include "../../src/collective/coll.h" // for Coll -#include "../../src/collective/comm.h" // for Comm +#include "../../src/collective/coll.h" // for Coll +#include "../../src/collective/comm.h" // for Comm namespace xgboost::collective { class FederatedColl : public Coll { diff --git a/plugin/federated/federated_comm.cc b/plugin/federated/federated_comm.cc index 6fa19112878a..439434e0a6dd 100644 --- a/plugin/federated/federated_comm.cc +++ b/plugin/federated/federated_comm.cc @@ -8,7 +8,8 @@ #include // for int32_t #include // for getenv #include // for numeric_limits -#include // for string, stoi +#include +#include // for string, stoi #include "../../src/common/common.h" // for Split #include "../../src/common/io.h" // for ReadAll @@ -32,7 +33,9 @@ void FederatedComm::Init(std::string const& host, std::int32_t port, std::int32_ CHECK_LT(rank, world) << "Invalid worker rank."; auto certs = {server_cert, client_cert, client_cert}; - auto is_empty = [](auto const& s) { return s.empty(); }; + auto is_empty = [](auto const& s) { + return s.empty(); + }; bool valid = std::all_of(certs.begin(), certs.end(), is_empty) || std::none_of(certs.begin(), certs.end(), is_empty); CHECK(valid) << "Invalid arguments for certificates."; diff --git a/plugin/federated/federated_comm.h b/plugin/federated/federated_comm.h index 0909509e07bc..0a99104c8380 100644 --- a/plugin/federated/federated_comm.h +++ b/plugin/federated/federated_comm.h @@ -11,7 +11,7 @@ #include // for shared_ptr #include // for string -#include "../../src/collective/comm.h" // for HostComm +#include "../../src/collective/comm.h" // for HostComm #include "xgboost/json.h" namespace xgboost::collective { diff --git a/plugin/sycl/README.md b/plugin/sycl/README.md old mode 100755 new mode 100644 diff --git a/plugin/sycl/common/hist_util.cc b/plugin/sycl/common/hist_util.cc index 37567b8e75b3..67d7a73b3809 100644 --- a/plugin/sycl/common/hist_util.cc +++ b/plugin/sycl/common/hist_util.cc @@ -2,15 +2,15 @@ * Copyright 2017-2023 by Contributors * \file hist_util.cc */ -#include -#include +#include "hist_util.h" + #include +#include +#include +#include #include "../data/gradient_index.h" #include "../tree/hist_dispatcher.h" -#include "hist_util.h" - -#include namespace xgboost { namespace sycl { @@ -19,55 +19,46 @@ namespace common { /*! * \brief Fill histogram with zeroes */ -template -void InitHist(::sycl::queue* qu, GHistRow* hist, - size_t size, ::sycl::event* event) { - *event = qu->fill(hist->Begin(), - xgboost::detail::GradientPairInternal(), size, *event); +template +void InitHist(::sycl::queue* qu, GHistRow* hist, size_t size, + ::sycl::event* event) { + *event = + qu->fill(hist->Begin(), xgboost::detail::GradientPairInternal(), size, *event); } -template void InitHist(::sycl::queue* qu, - GHistRow* hist, - size_t size, ::sycl::event* event); -template void InitHist(::sycl::queue* qu, - GHistRow* hist, +template void InitHist(::sycl::queue* qu, GHistRow* hist, size_t size, + ::sycl::event* event); +template void InitHist(::sycl::queue* qu, GHistRow* hist, size_t size, ::sycl::event* event); /*! * \brief Copy histogram from src to dst */ -template -void CopyHist(::sycl::queue* qu, - GHistRow* dst, - const GHistRow& src, - size_t size) { +template +void CopyHist(::sycl::queue* qu, GHistRow* dst, + const GHistRow& src, size_t size) { GradientSumT* pdst = reinterpret_cast(dst->Data()); const GradientSumT* psrc = reinterpret_cast(src.DataConst()); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(2 * size), [=](::sycl::item<1> pid) { - const size_t i = pid.get_id(0); - pdst[i] = psrc[i]; - }); - }).wait(); + cgh.parallel_for<>(::sycl::range<1>(2 * size), [=](::sycl::item<1> pid) { + const size_t i = pid.get_id(0); + pdst[i] = psrc[i]; + }); + }).wait(); } -template void CopyHist(::sycl::queue* qu, - GHistRow* dst, - const GHistRow& src, - size_t size); -template void CopyHist(::sycl::queue* qu, - GHistRow* dst, - const GHistRow& src, - size_t size); +template void CopyHist(::sycl::queue* qu, GHistRow* dst, + const GHistRow& src, size_t size); +template void CopyHist(::sycl::queue* qu, GHistRow* dst, + const GHistRow& src, size_t size); /*! * \brief Compute Subtraction: dst = src1 - src2 */ -template -::sycl::event SubtractionHist(::sycl::queue* qu, - GHistRow* dst, - const GHistRow& src1, - const GHistRow& src2, - size_t size, ::sycl::event event_priv) { +template +::sycl::event SubtractionHist(::sycl::queue* qu, GHistRow* dst, + const GHistRow& src1, + const GHistRow& src2, + size_t size, ::sycl::event event_priv) { GradientSumT* pdst = reinterpret_cast(dst->Data()); const GradientSumT* psrc1 = reinterpret_cast(src1.DataConst()); const GradientSumT* psrc2 = reinterpret_cast(src2.DataConst()); @@ -82,20 +73,19 @@ ::sycl::event SubtractionHist(::sycl::queue* qu, return event_final; } template ::sycl::event SubtractionHist(::sycl::queue* qu, - GHistRow* dst, - const GHistRow& src1, - const GHistRow& src2, - size_t size, ::sycl::event event_priv); + GHistRow* dst, + const GHistRow& src1, + const GHistRow& src2, + size_t size, ::sycl::event event_priv); template ::sycl::event SubtractionHist(::sycl::queue* qu, - GHistRow* dst, - const GHistRow& src1, - const GHistRow& src2, - size_t size, ::sycl::event event_priv); + GHistRow* dst, + const GHistRow& src1, + const GHistRow& src2, + size_t size, ::sycl::event event_priv); template ::sycl::event ReduceHist(::sycl::queue* qu, GradientPairT* hist_data, - GradientPairT* hist_buffer_data, - size_t nblocks, size_t nbins, + GradientPairT* hist_buffer_data, size_t nblocks, size_t nbins, const ::sycl::event& event_main) { auto event_save = qu->submit([&](::sycl::handler& cgh) { cgh.depends_on(event_main); @@ -116,15 +106,14 @@ ::sycl::event ReduceHist(::sycl::queue* qu, GradientPairT* hist_data, } // Kernel with buffer using -template -::sycl::event BuildHistKernel(::sycl::queue* qu, - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, - GHistRow* hist, - GHistRow* hist_buffer, - const tree::HistDispatcher& dispatcher, - ::sycl::event event_priv) { +template +::sycl::event BuildHistKernel(::sycl::queue* qu, const HostDeviceVector& gpair, + const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, + GHistRow* hist, + GHistRow* hist_buffer, + const tree::HistDispatcher& dispatcher, + ::sycl::event event_priv) { using GradientPairT = xgboost::detail::GradientPairInternal; const size_t size = row_indices.Size(); const size_t* rid = row_indices.begin; @@ -139,58 +128,56 @@ ::sycl::event BuildHistKernel(::sycl::queue* qu, const size_t nblocks = dispatcher.block.nblocks; GradientPairT* hist_buffer_data = hist_buffer->Data(); - auto event_fill = qu->fill(hist_buffer_data, GradientPairT(0, 0), - nblocks * nbins, event_priv); + auto event_fill = qu->fill(hist_buffer_data, GradientPairT(0, 0), nblocks * nbins, event_priv); auto event_main = qu->submit([&](::sycl::handler& cgh) { cgh.depends_on(event_fill); - cgh.parallel_for<>(::sycl::nd_range<2>(::sycl::range<2>(nblocks, work_group_size), - ::sycl::range<2>(1, work_group_size)), - [=](::sycl::nd_item<2> pid) { - size_t block = pid.get_global_id(0); - size_t feat = pid.get_global_id(1); - - GradientPairT* hist_local = hist_buffer_data + block * nbins; - for (size_t idx = 0; idx < block_size; ++idx) { - size_t i = block * block_size + idx; - if (i < size) { - const size_t icol_start = n_columns * rid[i]; - const size_t idx_gh = rid[i]; - - const GradientPairT pgh_row = {pgh[idx_gh].GetGrad(), pgh[idx_gh].GetHess()}; - pid.barrier(::sycl::access::fence_space::local_space); - const BinIdxType* gr_index_local = gradient_index + icol_start; - - for (size_t j = feat; j < n_columns; j += work_group_size) { - uint32_t idx_bin = static_cast(gr_index_local[j]); - if constexpr (isDense) { - idx_bin += offsets[j]; - } - if (idx_bin < nbins) { - hist_local[idx_bin] += pgh_row; + cgh.parallel_for<>( + ::sycl::nd_range<2>(::sycl::range<2>(nblocks, work_group_size), + ::sycl::range<2>(1, work_group_size)), + [=](::sycl::nd_item<2> pid) { + size_t block = pid.get_global_id(0); + size_t feat = pid.get_global_id(1); + + GradientPairT* hist_local = hist_buffer_data + block * nbins; + for (size_t idx = 0; idx < block_size; ++idx) { + size_t i = block * block_size + idx; + if (i < size) { + const size_t icol_start = n_columns * rid[i]; + const size_t idx_gh = rid[i]; + + const GradientPairT pgh_row = {pgh[idx_gh].GetGrad(), pgh[idx_gh].GetHess()}; + pid.barrier(::sycl::access::fence_space::local_space); + const BinIdxType* gr_index_local = gradient_index + icol_start; + + for (size_t j = feat; j < n_columns; j += work_group_size) { + uint32_t idx_bin = static_cast(gr_index_local[j]); + if constexpr (isDense) { + idx_bin += offsets[j]; + } + if (idx_bin < nbins) { + hist_local[idx_bin] += pgh_row; + } + } } } - } - } - }); + }); }); GradientPairT* hist_data = hist->Data(); - auto event_save = ReduceHist(qu, hist_data, hist_buffer_data, nblocks, - nbins, event_main); + auto event_save = ReduceHist(qu, hist_data, hist_buffer_data, nblocks, nbins, event_main); return event_save; } // Kernel with buffer and local hist using -template -::sycl::event BuildHistKernelLocal(::sycl::queue* qu, - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, - GHistRow* hist, - GHistRow* hist_buffer, - const tree::HistDispatcher& dispatcher, - ::sycl::event event_priv) { +template +::sycl::event BuildHistKernelLocal(::sycl::queue* qu, const HostDeviceVector& gpair, + const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, + GHistRow* hist, + GHistRow* hist_buffer, + const tree::HistDispatcher& dispatcher, + ::sycl::event event_priv) { constexpr int kMaxNumBins = tree::HistDispatcher::KMaxNumBins; using GradientPairT = xgboost::detail::GradientPairInternal; const size_t size = row_indices.Size(); @@ -212,64 +199,63 @@ ::sycl::event BuildHistKernelLocal(::sycl::queue* qu, cgh.parallel_for<>(::sycl::nd_range<2>(::sycl::range<2>(nblocks, work_group_size), ::sycl::range<2>(1, work_group_size)), [=](::sycl::nd_item<2> pid) { - size_t block = pid.get_global_id(0); - size_t feat = pid.get_global_id(1); - - // This buffer will be keeped in L1/registers - GradientPairT hist_fast[kMaxNumBins]; - - GradientPairT* hist_local = hist_buffer_data + block * nbins; - for (size_t fid = feat; fid < n_columns; fid += work_group_size) { - size_t n_bins_feature = offsets[fid+1] - offsets[fid]; - - // Not all elements of hist_fast are actually used: n_bins_feature <= kMaxNumBins - // We initililize only the requared elements to prevent the unused go to cache. - for (int bin = 0; bin < n_bins_feature; ++bin) { - hist_fast[bin] = {0, 0}; - } - - for (size_t idx = 0; idx < block_size; ++idx) { - size_t i = block * block_size + idx; - if (i < size) { - size_t row_id = rid[i]; - - const size_t icol_start = n_columns * row_id; - const GradientPairT pgh_row(pgh[row_id].GetGrad(), - pgh[row_id].GetHess()); - - const BinIdxType* gr_index_local = gradient_index + icol_start; - uint32_t idx_bin = gr_index_local[fid]; - - hist_fast[idx_bin] += pgh_row; - } - } - for (int bin = 0 ; bin < n_bins_feature; ++bin) { - hist_local[bin + offsets[fid]] = hist_fast[bin]; - } - } - }); + size_t block = pid.get_global_id(0); + size_t feat = pid.get_global_id(1); + + // This buffer will be keeped in L1/registers + GradientPairT hist_fast[kMaxNumBins]; + + GradientPairT* hist_local = hist_buffer_data + block * nbins; + for (size_t fid = feat; fid < n_columns; fid += work_group_size) { + size_t n_bins_feature = offsets[fid + 1] - offsets[fid]; + + // Not all elements of hist_fast are actually used: n_bins_feature <= + // kMaxNumBins We initililize only the requared elements to prevent the + // unused go to cache. + for (int bin = 0; bin < n_bins_feature; ++bin) { + hist_fast[bin] = {0, 0}; + } + + for (size_t idx = 0; idx < block_size; ++idx) { + size_t i = block * block_size + idx; + if (i < size) { + size_t row_id = rid[i]; + + const size_t icol_start = n_columns * row_id; + const GradientPairT pgh_row(pgh[row_id].GetGrad(), + pgh[row_id].GetHess()); + + const BinIdxType* gr_index_local = gradient_index + icol_start; + uint32_t idx_bin = gr_index_local[fid]; + + hist_fast[idx_bin] += pgh_row; + } + } + for (int bin = 0; bin < n_bins_feature; ++bin) { + hist_local[bin + offsets[fid]] = hist_fast[bin]; + } + } + }); }); GradientPairT* hist_data = hist->Data(); - auto event_save = ReduceHist(qu, hist_data, hist_buffer_data, nblocks, - nbins, event_main); + auto event_save = ReduceHist(qu, hist_data, hist_buffer_data, nblocks, nbins, event_main); return event_save; } // Kernel with atomic using -template -::sycl::event BuildHistKernel(::sycl::queue* qu, - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, - GHistRow* hist, - const tree::HistDispatcher& dispatcher, - ::sycl::event event_priv) { +template +::sycl::event BuildHistKernel(::sycl::queue* qu, const HostDeviceVector& gpair, + const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, + GHistRow* hist, + const tree::HistDispatcher& dispatcher, + ::sycl::event event_priv) { const size_t size = row_indices.Size(); const size_t* rid = row_indices.begin; const size_t n_columns = isDense ? gmat.nfeatures : gmat.row_stride; const GradientPair::ValueT* pgh = - reinterpret_cast(gpair.ConstDevicePointer()); + reinterpret_cast(gpair.ConstDevicePointer()); const BinIdxType* gradient_index = gmat.index.data(); const uint32_t* offsets = gmat.cut.cut_ptrs_.ConstDevicePointer(); FPType* hist_data = reinterpret_cast(hist->Data()); @@ -284,46 +270,43 @@ ::sycl::event BuildHistKernel(::sycl::queue* qu, cgh.parallel_for<>(::sycl::nd_range<2>(::sycl::range<2>(size, n_work_groups * work_group_size), ::sycl::range<2>(1, work_group_size)), [=](::sycl::nd_item<2> pid) { - const int i = pid.get_global_id(0); - auto group = pid.get_group(); - - const size_t icol_start = n_columns * rid[i]; - const size_t idx_gh = rid[i]; - const FPType pgh_row[2] = {pgh[2 * idx_gh], pgh[2 * idx_gh + 1]}; - const BinIdxType* gr_index_local = gradient_index + icol_start; - - const size_t group_id = group.get_group_id()[1]; - const size_t local_id = group.get_local_id()[1]; - const size_t j = group_id * work_group_size + local_id; - if (j < n_columns) { - uint32_t idx_bin = static_cast(gr_index_local[j]); - if constexpr (isDense) { - idx_bin += offsets[j]; - } - if (idx_bin < nbins) { - AtomicRef gsum(hist_data[2 * idx_bin]); - AtomicRef hsum(hist_data[2 * idx_bin + 1]); - gsum += pgh_row[0]; - hsum += pgh_row[1]; - } - } - }); + const int i = pid.get_global_id(0); + auto group = pid.get_group(); + + const size_t icol_start = n_columns * rid[i]; + const size_t idx_gh = rid[i]; + const FPType pgh_row[2] = {pgh[2 * idx_gh], pgh[2 * idx_gh + 1]}; + const BinIdxType* gr_index_local = gradient_index + icol_start; + + const size_t group_id = group.get_group_id()[1]; + const size_t local_id = group.get_local_id()[1]; + const size_t j = group_id * work_group_size + local_id; + if (j < n_columns) { + uint32_t idx_bin = static_cast(gr_index_local[j]); + if constexpr (isDense) { + idx_bin += offsets[j]; + } + if (idx_bin < nbins) { + AtomicRef gsum(hist_data[2 * idx_bin]); + AtomicRef hsum(hist_data[2 * idx_bin + 1]); + gsum += pgh_row[0]; + hsum += pgh_row[1]; + } + } + }); }); return event_main; } -template -::sycl::event BuildHistDispatchKernel( - ::sycl::queue* qu, - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, - GHistRow* hist, - bool isDense, - GHistRow* hist_buffer, - const DeviceProperties& device_prop, - ::sycl::event events_priv, - bool force_atomic_use) { +template +::sycl::event BuildHistDispatchKernel(::sycl::queue* qu, + const HostDeviceVector& gpair, + const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, + GHistRow* hist, bool isDense, + GHistRow* hist_buffer, + const DeviceProperties& device_prop, + ::sycl::event events_priv, bool force_atomic_use) { const size_t size = row_indices.Size(); const size_t n_columns = isDense ? gmat.nfeatures : gmat.row_stride; const size_t nbins = gmat.nbins; @@ -331,70 +314,59 @@ ::sycl::event BuildHistDispatchKernel( const size_t min_num_bins = gmat.min_num_bins; size_t max_n_blocks = hist_buffer->Size() / nbins; - auto dispatcher = tree::HistDispatcher - (device_prop, isDense, size, max_n_blocks, nbins, - n_columns, max_num_bins, min_num_bins); + auto dispatcher = tree::HistDispatcher(device_prop, isDense, size, max_n_blocks, nbins, + n_columns, max_num_bins, min_num_bins); // force_atomic_use flag is used only for testing bool use_atomic = dispatcher.use_atomics || force_atomic_use; if (!use_atomic) { if (isDense) { if (dispatcher.use_local_hist) { - return BuildHistKernelLocal(qu, gpair, row_indices, - gmat, hist, hist_buffer, - dispatcher, events_priv); + return BuildHistKernelLocal(qu, gpair, row_indices, gmat, hist, + hist_buffer, dispatcher, events_priv); } else { - return BuildHistKernel(qu, gpair, row_indices, - gmat, hist, hist_buffer, - dispatcher, events_priv); + return BuildHistKernel(qu, gpair, row_indices, gmat, hist, + hist_buffer, dispatcher, events_priv); } } else { - return BuildHistKernel(qu, gpair, row_indices, - gmat, hist, hist_buffer, - dispatcher, events_priv); + return BuildHistKernel(qu, gpair, row_indices, gmat, hist, + hist_buffer, dispatcher, events_priv); } } else { if (isDense) { - return BuildHistKernel(qu, gpair, row_indices, - gmat, hist, + return BuildHistKernel(qu, gpair, row_indices, gmat, hist, dispatcher, events_priv); } else { - return BuildHistKernel(qu, gpair, row_indices, - gmat, hist, + return BuildHistKernel(qu, gpair, row_indices, gmat, hist, dispatcher, events_priv); } } } -template -::sycl::event BuildHistKernel(::sycl::queue* qu, - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, const bool isDense, - GHistRow* hist, - GHistRow* hist_buffer, - const DeviceProperties& device_prop, - ::sycl::event event_priv, - bool force_atomic_use) { +template +::sycl::event BuildHistKernel(::sycl::queue* qu, const HostDeviceVector& gpair, + const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, const bool isDense, + GHistRow* hist, + GHistRow* hist_buffer, + const DeviceProperties& device_prop, ::sycl::event event_priv, + bool force_atomic_use) { const bool is_dense = isDense; switch (gmat.index.GetBinTypeSize()) { case BinTypeSize::kUint8BinsTypeSize: - return BuildHistDispatchKernel(qu, gpair, row_indices, - gmat, hist, is_dense, hist_buffer, - device_prop, - event_priv, force_atomic_use); + return BuildHistDispatchKernel(qu, gpair, row_indices, gmat, hist, is_dense, + hist_buffer, device_prop, event_priv, + force_atomic_use); break; case BinTypeSize::kUint16BinsTypeSize: - return BuildHistDispatchKernel(qu, gpair, row_indices, - gmat, hist, is_dense, hist_buffer, - device_prop, - event_priv, force_atomic_use); + return BuildHistDispatchKernel(qu, gpair, row_indices, gmat, hist, is_dense, + hist_buffer, device_prop, event_priv, + force_atomic_use); break; case BinTypeSize::kUint32BinsTypeSize: - return BuildHistDispatchKernel(qu, gpair, row_indices, - gmat, hist, is_dense, hist_buffer, - device_prop, - event_priv, force_atomic_use); + return BuildHistDispatchKernel(qu, gpair, row_indices, gmat, hist, is_dense, + hist_buffer, device_prop, event_priv, + force_atomic_use); break; default: CHECK(false); // no default behavior @@ -403,45 +375,26 @@ ::sycl::event BuildHistKernel(::sycl::queue* qu, template ::sycl::event GHistBuilder::BuildHist( - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix &gmat, - GHistRowT* hist, - bool isDense, - GHistRowT* hist_buffer, - const DeviceProperties& device_prop, - ::sycl::event event_priv, - bool force_atomic_use) { - return BuildHistKernel(qu_, gpair, row_indices, gmat, - isDense, hist, hist_buffer, - device_prop, event_priv, - force_atomic_use); + const HostDeviceVector& gpair, const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, GHistRowT* hist, bool isDense, + GHistRowT* hist_buffer, const DeviceProperties& device_prop, + ::sycl::event event_priv, bool force_atomic_use) { + return BuildHistKernel(qu_, gpair, row_indices, gmat, isDense, hist, hist_buffer, + device_prop, event_priv, force_atomic_use); } -template -::sycl::event GHistBuilder::BuildHist( - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, - GHistRow* hist, - bool isDense, - GHistRow* hist_buffer, - const DeviceProperties& device_prop, - ::sycl::event event_priv, - bool force_atomic_use); -template -::sycl::event GHistBuilder::BuildHist( - const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, - GHistRow* hist, - bool isDense, - GHistRow* hist_buffer, - const DeviceProperties& device_prop, - ::sycl::event event_priv, - bool force_atomic_use); - -template +template ::sycl::event GHistBuilder::BuildHist( + const HostDeviceVector& gpair, const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, GHistRow* hist, bool isDense, + GHistRow* hist_buffer, const DeviceProperties& device_prop, + ::sycl::event event_priv, bool force_atomic_use); +template ::sycl::event GHistBuilder::BuildHist( + const HostDeviceVector& gpair, const RowSetCollection::Elem& row_indices, + const GHistIndexMatrix& gmat, GHistRow* hist, bool isDense, + GHistRow* hist_buffer, const DeviceProperties& device_prop, + ::sycl::event event_priv, bool force_atomic_use); + +template void GHistBuilder::SubtractionTrick(GHistRowT* self, const GHistRowT& sibling, const GHistRowT& parent) { @@ -451,14 +404,14 @@ void GHistBuilder::SubtractionTrick(GHistRowT::SubtractionTrick(GHistRow* self, - const GHistRow& sibling, - const GHistRow& parent); -template -void GHistBuilder::SubtractionTrick(GHistRow* self, - const GHistRow& sibling, - const GHistRow& parent); +template void GHistBuilder::SubtractionTrick( + GHistRow* self, + const GHistRow& sibling, + const GHistRow& parent); +template void GHistBuilder::SubtractionTrick( + GHistRow* self, + const GHistRow& sibling, + const GHistRow& parent); } // namespace common } // namespace sycl } // namespace xgboost diff --git a/plugin/sycl/common/hist_util.h b/plugin/sycl/common/hist_util.h index 3c71a7be20d9..9aabfc4b410e 100644 --- a/plugin/sycl/common/hist_util.h +++ b/plugin/sycl/common/hist_util.h @@ -5,24 +5,22 @@ #ifndef PLUGIN_SYCL_COMMON_HIST_UTIL_H_ #define PLUGIN_SYCL_COMMON_HIST_UTIL_H_ -#include -#include #include - -#include "../data.h" -#include "row_set.h" +#include +#include +#include #include "../../src/common/hist_util.h" +#include "../data.h" #include "../data/gradient_index.h" #include "../tree/hist_dispatcher.h" - -#include +#include "row_set.h" namespace xgboost { namespace sycl { namespace common { -template +template using GHistRow = USMVector, memory_type>; using BinTypeSize = ::xgboost::common::BinTypeSize; @@ -32,26 +30,22 @@ class ColumnMatrix; /*! * \brief Fill histogram with zeroes */ -template -void InitHist(::sycl::queue* qu, - GHistRow* hist, - size_t size, ::sycl::event* event); +template +void InitHist(::sycl::queue* qu, GHistRow* hist, size_t size, + ::sycl::event* event); /*! * \brief Copy histogram from src to dst */ -template -void CopyHist(::sycl::queue* qu, - GHistRow* dst, - const GHistRow& src, - size_t size); +template +void CopyHist(::sycl::queue* qu, GHistRow* dst, + const GHistRow& src, size_t size); /*! * \brief Compute subtraction: dst = src1 - src2 */ -template -::sycl::event SubtractionHist(::sycl::queue* qu, - GHistRow* dst, +template +::sycl::event SubtractionHist(::sycl::queue* qu, GHistRow* dst, const GHistRow& src1, const GHistRow& src2, size_t size, ::sycl::event event_priv); @@ -59,19 +53,15 @@ ::sycl::event SubtractionHist(::sycl::queue* qu, /*! * \brief Histograms of gradient statistics for multiple nodes */ -template +template class HistCollection { public: using GHistRowT = GHistRow; // Access histogram for i-th node - GHistRowT& operator[](bst_uint nid) { - return *(data_.at(nid)); - } + GHistRowT& operator[](bst_uint nid) { return *(data_.at(nid)); } - const GHistRowT& operator[](bst_uint nid) const { - return *(data_.at(nid)); - } + const GHistRowT& operator[](bst_uint nid) const { return *(data_.at(nid)); } // Initialize histogram collection void Init(::sycl::queue* qu, uint32_t nbins) { @@ -86,13 +76,10 @@ class HistCollection { ::sycl::event AddHistRow(bst_uint nid) { ::sycl::event event; if (data_.count(nid) == 0) { - data_[nid] = - std::make_shared(qu_, nbins_, - xgboost::detail::GradientPairInternal(0, 0), - &event); + data_[nid] = std::make_shared( + qu_, nbins_, xgboost::detail::GradientPairInternal(0, 0), &event); } else { - data_[nid]->Resize(qu_, nbins_, - xgboost::detail::GradientPairInternal(0, 0), + data_[nid]->Resize(qu_, nbins_, xgboost::detail::GradientPairInternal(0, 0), &event); } return event; @@ -110,7 +97,7 @@ class HistCollection { /*! * \brief Stores temporary histograms to compute them in parallel */ -template +template class ParallelGHistBuilder { public: using GHistRowT = GHistRow; @@ -123,13 +110,9 @@ class ParallelGHistBuilder { } } - void Reset(size_t nblocks) { - hist_device_buffer_.Resize(qu_, nblocks * nbins_); - } + void Reset(size_t nblocks) { hist_device_buffer_.Resize(qu_, nblocks * nbins_); } - GHistRowT& GetDeviceBuffer() { - return hist_device_buffer_; - } + GHistRowT& GetDeviceBuffer() { return hist_device_buffer_; } protected: /*! \brief Number of bins in each histogram */ @@ -146,10 +129,10 @@ class ParallelGHistBuilder { /*! * \brief Builder for histograms of gradient statistics */ -template +template class GHistBuilder { public: - template + template using GHistRowT = GHistRow; GHistBuilder() = default; @@ -157,13 +140,10 @@ class GHistBuilder { // Construct a histogram via histogram aggregation ::sycl::event BuildHist(const HostDeviceVector& gpair, - const RowSetCollection::Elem& row_indices, - const GHistIndexMatrix& gmat, - GHistRowT* HistCollection, - bool isDense, + const RowSetCollection::Elem& row_indices, const GHistIndexMatrix& gmat, + GHistRowT* HistCollection, bool isDense, GHistRowT* hist_buffer, - const DeviceProperties& device_prop, - ::sycl::event event, + const DeviceProperties& device_prop, ::sycl::event event, bool force_atomic_use = false); // Construct a histogram via subtraction trick @@ -171,13 +151,11 @@ class GHistBuilder { const GHistRowT& sibling, const GHistRowT& parent); - uint32_t GetNumBins() const { - return nbins_; - } + uint32_t GetNumBins() const { return nbins_; } private: /*! \brief Number of all bins over all features */ - uint32_t nbins_ { 0 }; + uint32_t nbins_{0}; ::sycl::queue* qu_; }; diff --git a/plugin/sycl/common/host_device_vector.cc b/plugin/sycl/common/host_device_vector.cc index 0a32fae40279..cb7d586ec088 100644 --- a/plugin/sycl/common/host_device_vector.cc +++ b/plugin/sycl/common/host_device_vector.cc @@ -6,16 +6,18 @@ // implementation of HostDeviceVector with sycl support +#include #include #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-W#pragma-messages" #pragma GCC diagnostic ignored "-Wtautological-constant-compare" #include "xgboost/host_device_vector.h" #pragma GCC diagnostic pop -#include "../device_manager.h" #include "../data.h" +#include "../device_manager.h" #include "../predictor/node.h" namespace xgboost { @@ -46,10 +48,11 @@ class HostDeviceVectorImpl { } } - HostDeviceVectorImpl(HostDeviceVectorImpl&& that) : device_{that.device_}, - data_h_{std::move(that.data_h_)}, - data_d_{std::move(that.data_d_)}, - device_access_{that.device_access_} {} + HostDeviceVectorImpl(HostDeviceVectorImpl&& that) + : device_{that.device_}, + data_h_{std::move(that.data_h_)}, + data_d_{std::move(that.data_d_)}, + device_access_{that.device_access_} {} std::vector& HostVector() { SyncHost(GPUAccess::kNone); @@ -62,14 +65,15 @@ class HostDeviceVectorImpl { } void SetDevice(DeviceOrd device) { - if (device_ == device) { return; } + if (device_ == device) { + return; + } if (device_.IsSycl()) { SyncHost(GPUAccess::kNone); } if (device_.IsSycl() && device.IsSycl()) { - CHECK_EQ(device_, device) - << "New device is different from previous one."; + CHECK_EQ(device_, device) << "New device is different from previous one."; } device_ = device; if (device_.IsSycl()) { @@ -97,20 +101,26 @@ class HostDeviceVectorImpl { } void SyncHost(GPUAccess access) { - if (HostCanAccess(access)) { return; } + if (HostCanAccess(access)) { + return; + } if (HostCanRead()) { // data is present, just need to deny access to the device device_access_ = access; return; } device_access_ = access; - if (data_h_.size() != data_d_->Size()) { data_h_.resize(data_d_->Size()); } + if (data_h_.size() != data_d_->Size()) { + data_h_.resize(data_d_->Size()); + } SetDevice(); qu_->memcpy(data_h_.data(), data_d_->Data(), data_d_->Size() * sizeof(T)).wait(); } void SyncDevice(GPUAccess access) { - if (DeviceCanAccess(access)) { return; } + if (DeviceCanAccess(access)) { + return; + } if (DeviceCanRead()) { device_access_ = access; return; @@ -130,9 +140,7 @@ class HostDeviceVectorImpl { bool DeviceCanWrite() const { return DeviceCanAccess(GPUAccess::kWrite); } GPUAccess Access() const { return device_access_; } - size_t Size() const { - return HostCanRead() ? data_h_.size() : data_d_ ? data_d_->Size() : 0; - } + size_t Size() const { return HostCanRead() ? data_h_.size() : data_d_ ? data_d_->Size() : 0; } DeviceOrd Device() const { return device_; } @@ -214,7 +222,9 @@ class HostDeviceVectorImpl { private: void ResizeDevice(size_t new_size) { - if (data_d_ && new_size == data_d_->Size()) { return; } + if (data_d_ && new_size == data_d_->Size()) { + return; + } SetDevice(); data_d_->Resize(qu_, new_size); } @@ -254,20 +264,19 @@ class HostDeviceVectorImpl { }; template -HostDeviceVector::HostDeviceVector(size_t size, T v, DeviceOrd device) - : impl_(nullptr) { +HostDeviceVector::HostDeviceVector(size_t size, T v, DeviceOrd device) : impl_(nullptr) { impl_ = new HostDeviceVectorImpl(size, v, device); } template HostDeviceVector::HostDeviceVector(std::initializer_list init, DeviceOrd device) - : impl_(nullptr) { + : impl_(nullptr) { impl_ = new HostDeviceVectorImpl(init, device); } template HostDeviceVector::HostDeviceVector(const std::vector& init, DeviceOrd device) - : impl_(nullptr) { + : impl_(nullptr) { impl_ = new HostDeviceVectorImpl(init, device); } @@ -278,7 +287,9 @@ HostDeviceVector::HostDeviceVector(HostDeviceVector&& that) { template HostDeviceVector& HostDeviceVector::operator=(HostDeviceVector&& that) { - if (this == &that) { return *this; } + if (this == &that) { + return *this; + } std::unique_ptr> new_impl( new HostDeviceVectorImpl(std::move(*that.impl_))); @@ -294,7 +305,9 @@ HostDeviceVector::~HostDeviceVector() { } template -size_t HostDeviceVector::Size() const { return impl_->Size(); } +size_t HostDeviceVector::Size() const { + return impl_->Size(); +} template DeviceOrd HostDeviceVector::Device() const { @@ -322,7 +335,9 @@ common::Span HostDeviceVector::ConstDeviceSpan() const { } template -std::vector& HostDeviceVector::HostVector() { return impl_->HostVector(); } +std::vector& HostDeviceVector::HostVector() { + return impl_->HostVector(); +} template const std::vector& HostDeviceVector::ConstHostVector() const { @@ -399,7 +414,7 @@ template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; -template class HostDeviceVector; // bst_node_t +template class HostDeviceVector; // bst_node_t template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; diff --git a/plugin/sycl/common/linalg_op.cc b/plugin/sycl/common/linalg_op.cc index 387b01baa5c9..f43c5dd67a04 100644 --- a/plugin/sycl/common/linalg_op.cc +++ b/plugin/sycl/common/linalg_op.cc @@ -3,14 +3,13 @@ * \file linalg_op.h */ -#include "../data.h" -#include "../device_manager.h" +#include #include "../../../src/common/optional_weight.h" // for OptionalWeights +#include "../data.h" +#include "../device_manager.h" #include "xgboost/context.h" // for Context -#include - namespace xgboost::sycl::linalg { void SmallHistogram(Context const* ctx, xgboost::linalg::MatrixView indices, xgboost::common::OptionalWeights const& weights, @@ -19,14 +18,13 @@ void SmallHistogram(Context const* ctx, xgboost::linalg::MatrixView auto* qu = device_manager.GetQueue(ctx->Device()); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(indices.Size()), - [=](::sycl::id<1> pid) { - const size_t i = pid[0]; - auto y = indices(i); - auto w = weights[i]; - AtomicRef bin_val(const_cast(bins(static_cast(y)))); - bin_val += w; - }); - }).wait(); + cgh.parallel_for<>(::sycl::range<1>(indices.Size()), [=](::sycl::id<1> pid) { + const size_t i = pid[0]; + auto y = indices(i); + auto w = weights[i]; + AtomicRef bin_val(const_cast(bins(static_cast(y)))); + bin_val += w; + }); + }).wait(); } } // namespace xgboost::sycl::linalg diff --git a/plugin/sycl/common/linalg_op.h b/plugin/sycl/common/linalg_op.h index e246b73265d5..b010d860c7c1 100644 --- a/plugin/sycl/common/linalg_op.h +++ b/plugin/sycl/common/linalg_op.h @@ -5,19 +5,18 @@ #ifndef PLUGIN_SYCL_COMMON_LINALG_OP_H_ #define PLUGIN_SYCL_COMMON_LINALG_OP_H_ -#include +#include #include +#include #include "../data.h" #include "../device_manager.h" -#include - namespace xgboost { namespace sycl { namespace linalg { -template +template using TensorView = xgboost::linalg::TensorView; struct WorkGroupsParams { @@ -27,33 +26,32 @@ struct WorkGroupsParams { template ::sycl::event GroupWiseKernel(::sycl::queue* qu, int* flag_ptr, - const std::vector<::sycl::event>& events, - const WorkGroupsParams& wg, Fn &&fn) { + const std::vector<::sycl::event>& events, const WorkGroupsParams& wg, + Fn&& fn) { ::sycl::buffer flag_buf(flag_ptr, 1); auto event = qu->submit([&](::sycl::handler& cgh) { cgh.depends_on(events); - auto flag = flag_buf.get_access<::sycl::access::mode::write>(cgh); + auto flag = flag_buf.get_access<::sycl::access::mode::write>(cgh); cgh.parallel_for_work_group<>(::sycl::range<1>(wg.n_workgroups), - ::sycl::range<1>(wg.workgroup_size), - [=](::sycl::group<1> group) { - group.parallel_for_work_item([&](::sycl::h_item<1> item) { - const size_t idx = item.get_global_id()[0]; - fn(idx, flag); - }); - }); + ::sycl::range<1>(wg.workgroup_size), [=](::sycl::group<1> group) { + group.parallel_for_work_item([&](::sycl::h_item<1> item) { + const size_t idx = item.get_global_id()[0]; + fn(idx, flag); + }); + }); }); return event; } -template -auto call(Fn&& fn, TupleType t, std::index_sequence) { - return fn(std::get(t) ...); +template +auto call(Fn&& fn, TupleType t, std::index_sequence) { + return fn(std::get(t)...); } -template +template auto call(Fn&& fn, TupleType t) { - static constexpr auto size = std::tuple_size::value; - return call(fn, t, std::make_index_sequence{}); + static constexpr auto size = std::tuple_size::value; + return call(fn, t, std::make_index_sequence{}); } template @@ -61,12 +59,11 @@ void ElementWiseKernel(TensorView t, Fn&& fn) { sycl::DeviceManager device_manager; auto* qu = device_manager.GetQueue(t.Device()); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(t.Size()), - [=](::sycl::id<1> pid) { - const size_t idx = pid[0]; - call(const_cast(fn), xgboost::linalg::UnravelIndex(idx, t.Shape())); - }); - }).wait_and_throw(); + cgh.parallel_for<>(::sycl::range<1>(t.Size()), [=](::sycl::id<1> pid) { + const size_t idx = pid[0]; + call(const_cast(fn), xgboost::linalg::UnravelIndex(idx, t.Shape())); + }); + }).wait_and_throw(); } template @@ -78,9 +75,8 @@ bool Validate(DeviceOrd device, TensorView t, Fn&& fn) { { ::sycl::buffer flag_buf(&flag, 1); qu->submit([&](::sycl::handler& cgh) { - auto flag_acc = flag_buf.get_access<::sycl::access::mode::write>(cgh); - cgh.parallel_for<>(::sycl::range<1>(t.Size()), - [=](::sycl::id<1> pid) { + auto flag_acc = flag_buf.get_access<::sycl::access::mode::write>(cgh); + cgh.parallel_for<>(::sycl::range<1>(t.Size()), [=](::sycl::id<1> pid) { const size_t idx = pid[0]; const T& value = call(t, xgboost::linalg::UnravelIndex(idx, t.Shape())); bool is_valid = const_cast(fn)(value); diff --git a/plugin/sycl/common/optional_weight.cc b/plugin/sycl/common/optional_weight.cc index aa984a152dc3..8b31f5ebe2b0 100644 --- a/plugin/sycl/common/optional_weight.cc +++ b/plugin/sycl/common/optional_weight.cc @@ -1,10 +1,10 @@ /*! * Copyright by Contributors 2017-2025 */ -#include - #include "../../../src/common/optional_weight.h" +#include + #include "../device_manager.h" namespace xgboost::common::sycl_impl { @@ -17,13 +17,13 @@ double SumOptionalWeights(Context const* ctx, OptionalWeights const& weights) { { ::sycl::buffer buff(&result, 1); qu->submit([&](::sycl::handler& cgh) { - auto reduction = ::sycl::reduction(buff, cgh, ::sycl::plus<>()); - cgh.parallel_for<>(::sycl::range<1>(weights.Size()), reduction, - [=](::sycl::id<1> pid, auto& sum) { - size_t i = pid[0]; - sum += data[i]; - }); - }).wait_and_throw(); + auto reduction = ::sycl::reduction(buff, cgh, ::sycl::plus<>()); + cgh.parallel_for<>(::sycl::range<1>(weights.Size()), reduction, + [=](::sycl::id<1> pid, auto& sum) { + size_t i = pid[0]; + sum += data[i]; + }); + }).wait_and_throw(); } return result; diff --git a/plugin/sycl/common/partition_builder.h b/plugin/sycl/common/partition_builder.h index 82c5afe11cbc..382c800bee96 100644 --- a/plugin/sycl/common/partition_builder.h +++ b/plugin/sycl/common/partition_builder.h @@ -12,20 +12,20 @@ #include #include -#include #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" #include "../../../src/common/column_matrix.h" #pragma GCC diagnostic pop +#include + #include "../data.h" -#include "row_set.h" #include "../data/gradient_index.h" #include "../tree/expand_entry.h" - -#include +#include "row_set.h" namespace xgboost { namespace sycl { @@ -35,15 +35,11 @@ namespace common { // on comparison of indexes values (idx_span) and split point (split_cond) // Handle dense columns template -inline ::sycl::event PartitionDenseKernel( - ::sycl::queue* qu, - const GHistIndexMatrix& gmat, - const RowSetCollection::Elem& rid_span, - const size_t fid, - const int32_t split_cond, - xgboost::common::Span* rid_buf, - size_t* parts_size, - ::sycl::event event) { +inline ::sycl::event PartitionDenseKernel(::sycl::queue* qu, const GHistIndexMatrix& gmat, + const RowSetCollection::Elem& rid_span, const size_t fid, + const int32_t split_cond, + xgboost::common::Span* rid_buf, + size_t* parts_size, ::sycl::event event) { const size_t row_stride = gmat.row_stride; const BinIdxType* gradient_index = gmat.index.data(); const size_t* rid = rid_span.begin; @@ -73,14 +69,11 @@ inline ::sycl::event PartitionDenseKernel( // on comparison of indexes values (idx_span) and split point (split_cond) // Handle sparce columns template -inline ::sycl::event PartitionSparseKernel(::sycl::queue* qu, - const GHistIndexMatrix& gmat, - const RowSetCollection::Elem& rid_span, - const size_t fid, - const int32_t split_cond, - xgboost::common::Span* rid_buf, - size_t* parts_size, - ::sycl::event event) { +inline ::sycl::event PartitionSparseKernel(::sycl::queue* qu, const GHistIndexMatrix& gmat, + const RowSetCollection::Elem& rid_span, const size_t fid, + const int32_t split_cond, + xgboost::common::Span* rid_buf, + size_t* parts_size, ::sycl::event event) { const size_t row_stride = gmat.row_stride; const BinIdxType* gradient_index = gmat.index.data(); const size_t* rid = rid_span.begin; @@ -94,13 +87,13 @@ inline ::sycl::event PartitionSparseKernel(::sycl::queue* qu, const size_t id = rid[nid.get_id(0)]; const BinIdxType* gr_index_local = gradient_index + row_stride * id; - const int32_t fid_local = std::lower_bound(gr_index_local, - gr_index_local + row_stride, - cut_ptrs[fid]) - gr_index_local; - const bool is_left = (fid_local >= row_stride || - gr_index_local[fid_local] >= cut_ptrs[fid + 1]) ? - default_left : - gr_index_local[fid_local] <= split_cond; + const int32_t fid_local = + std::lower_bound(gr_index_local, gr_index_local + row_stride, cut_ptrs[fid]) - + gr_index_local; + const bool is_left = + (fid_local >= row_stride || gr_index_local[fid_local] >= cut_ptrs[fid + 1]) + ? default_left + : gr_index_local[fid_local] <= split_cond; if (is_left) { AtomicRef n_left(parts_size[0]); p_rid_buf[n_left.fetch_add(1)] = id; @@ -115,17 +108,16 @@ inline ::sycl::event PartitionSparseKernel(::sycl::queue* qu, // The builder is required for samples partition to left and rights children for set of nodes class PartitionBuilder { public: - template + template void Init(::sycl::queue* qu, size_t n_nodes, Func funcNTaks) { qu_ = qu; - nodes_offsets_.resize(n_nodes+1); + nodes_offsets_.resize(n_nodes + 1); result_rows_.resize(2 * n_nodes); n_nodes_ = n_nodes; - nodes_offsets_[0] = 0; - for (size_t i = 1; i < n_nodes+1; ++i) { - nodes_offsets_[i] = nodes_offsets_[i-1] + funcNTaks(i-1); + for (size_t i = 1; i < n_nodes + 1; ++i) { + nodes_offsets_[i] = nodes_offsets_[i - 1] + funcNTaks(i - 1); } if (data_.Size() < nodes_offsets_[n_nodes]) { @@ -133,64 +125,52 @@ class PartitionBuilder { } } - size_t GetNLeftElems(int nid) const { - return result_rows_[2 * nid]; - } + size_t GetNLeftElems(int nid) const { return result_rows_[2 * nid]; } - size_t GetNRightElems(int nid) const { - return result_rows_[2 * nid + 1]; - } + size_t GetNRightElems(int nid) const { return result_rows_[2 * nid + 1]; } // For test purposes only - void SetNLeftElems(int nid, size_t val) { - result_rows_[2 * nid] = val; - } + void SetNLeftElems(int nid, size_t val) { result_rows_[2 * nid] = val; } // For test purposes only - void SetNRightElems(int nid, size_t val) { - result_rows_[2 * nid + 1] = val; - } + void SetNRightElems(int nid, size_t val) { result_rows_[2 * nid + 1] = val; } xgboost::common::Span GetData(int nid) { - return { data_.Data() + nodes_offsets_[nid], nodes_offsets_[nid + 1] - nodes_offsets_[nid] }; + return {data_.Data() + nodes_offsets_[nid], nodes_offsets_[nid + 1] - nodes_offsets_[nid]}; } template - ::sycl::event Partition(const int32_t split_cond, - const GHistIndexMatrix& gmat, - const RowSetCollection::Elem& rid_span, - const xgboost::RegTree::Node& node, - xgboost::common::Span* rid_buf, - size_t* parts_size, - ::sycl::event event) { + ::sycl::event Partition(const int32_t split_cond, const GHistIndexMatrix& gmat, + const RowSetCollection::Elem& rid_span, + const xgboost::RegTree::Node& node, + xgboost::common::Span* rid_buf, size_t* parts_size, + ::sycl::event event) { const bst_uint fid = node.SplitIndex(); const bool default_left = node.DefaultLeft(); if (gmat.IsDense()) { if (default_left) { - return PartitionDenseKernel(qu_, gmat, rid_span, fid, - split_cond, rid_buf, parts_size, event); + return PartitionDenseKernel(qu_, gmat, rid_span, fid, split_cond, rid_buf, + parts_size, event); } else { - return PartitionDenseKernel(qu_, gmat, rid_span, fid, - split_cond, rid_buf, parts_size, event); + return PartitionDenseKernel(qu_, gmat, rid_span, fid, split_cond, + rid_buf, parts_size, event); } } else { if (default_left) { - return PartitionSparseKernel(qu_, gmat, rid_span, fid, - split_cond, rid_buf, parts_size, event); + return PartitionSparseKernel(qu_, gmat, rid_span, fid, split_cond, + rid_buf, parts_size, event); } else { - return PartitionSparseKernel(qu_, gmat, rid_span, fid, - split_cond, rid_buf, parts_size, event); + return PartitionSparseKernel(qu_, gmat, rid_span, fid, split_cond, + rid_buf, parts_size, event); } } } // Entry point for Partition - void Partition(const GHistIndexMatrix& gmat, - const std::vector nodes, + void Partition(const GHistIndexMatrix& gmat, const std::vector nodes, const RowSetCollection& row_set_collection, - const std::vector& split_conditions, - RegTree* p_tree, + const std::vector& split_conditions, RegTree* p_tree, ::sycl::event* general_event) { nodes_events_.resize(n_nodes_); @@ -207,16 +187,16 @@ class PartitionBuilder { int32_t split_condition = split_conditions[node_in_set]; switch (gmat.index.GetBinTypeSize()) { case common::BinTypeSize::kUint8BinsTypeSize: - node_event = Partition(split_condition, gmat, rid_span, node, - &rid_buf, part_size, *general_event); + node_event = Partition(split_condition, gmat, rid_span, node, &rid_buf, + part_size, *general_event); break; case common::BinTypeSize::kUint16BinsTypeSize: - node_event = Partition(split_condition, gmat, rid_span, node, - &rid_buf, part_size, *general_event); + node_event = Partition(split_condition, gmat, rid_span, node, &rid_buf, + part_size, *general_event); break; case common::BinTypeSize::kUint32BinsTypeSize: - node_event = Partition(split_condition, gmat, rid_span, node, - &rid_buf, part_size, *general_event); + node_event = Partition(split_condition, gmat, rid_span, node, &rid_buf, + part_size, *general_event); break; default: CHECK(false); // no default behavior @@ -226,15 +206,11 @@ class PartitionBuilder { } } - *general_event = qu_->memcpy(result_rows_.data(), - parts_size_.DataConst(), - sizeof(size_t) * 2 * n_nodes_, - nodes_events_); + *general_event = qu_->memcpy(result_rows_.data(), parts_size_.DataConst(), + sizeof(size_t) * 2 * n_nodes_, nodes_events_); } - void MergeToArray(size_t nid, - size_t* data_result, - ::sycl::event* event) { + void MergeToArray(size_t nid, size_t* data_result, ::sycl::event* event) { size_t n_nodes_total = GetNLeftElems(nid) + GetNRightElems(nid); if (n_nodes_total > 0) { const size_t* data = data_.Data() + nodes_offsets_[nid]; @@ -258,5 +234,4 @@ class PartitionBuilder { } // namespace sycl } // namespace xgboost - #endif // PLUGIN_SYCL_COMMON_PARTITION_BUILDER_H_ diff --git a/plugin/sycl/common/row_set.h b/plugin/sycl/common/row_set.h index 67734f321f7d..433676d90787 100644 --- a/plugin/sycl/common/row_set.h +++ b/plugin/sycl/common/row_set.h @@ -10,18 +10,16 @@ #include #pragma GCC diagnostic pop #include -#include +#include #include +#include #include "../data.h" -#include - namespace xgboost { namespace sycl { namespace common { - /*! \brief Collection of rowsets stored on device in USM memory */ class RowSetCollection { public: @@ -32,28 +30,19 @@ class RowSetCollection { const size_t* begin{nullptr}; const size_t* end{nullptr}; bst_node_t node_id{-1}; // id of node associated with this instance set; -1 means uninitialized - Elem() - = default; - Elem(const size_t* begin, - const size_t* end, - bst_node_t node_id = -1) + Elem() = default; + Elem(const size_t* begin, const size_t* end, bst_node_t node_id = -1) : begin(begin), end(end), node_id(node_id) {} - - inline size_t Size() const { - return end - begin; - } + inline size_t Size() const { return end - begin; } }; - inline size_t Size() const { - return elem_of_each_node_.size(); - } + inline size_t Size() const { return elem_of_each_node_.size(); } /*! \brief return corresponding element set given the node_id */ inline const Elem& operator[](unsigned node_id) const { const Elem& e = elem_of_each_node_[node_id]; - CHECK(e.begin != nullptr) - << "access element that is not in the set"; + CHECK(e.begin != nullptr) << "access element that is not in the set"; return e; } @@ -64,9 +53,7 @@ class RowSetCollection { } // clear up things - inline void Clear() { - elem_of_each_node_.clear(); - } + inline void Clear() { elem_of_each_node_.clear(); } // initialize node id 0->everything inline void Init() { CHECK_EQ(elem_of_each_node_.size(), 0U); @@ -79,22 +66,17 @@ class RowSetCollection { auto& Data() { return row_indices_; } // split rowset into two - inline void AddSplit(unsigned node_id, - unsigned left_node_id, - unsigned right_node_id, - size_t n_left, - size_t n_right) { + inline void AddSplit(unsigned node_id, unsigned left_node_id, unsigned right_node_id, + size_t n_left, size_t n_right) { const Elem e = elem_of_each_node_[node_id]; CHECK(e.begin != nullptr); size_t* all_begin = row_indices_.Begin(); size_t* begin = all_begin + (e.begin - all_begin); - CHECK_EQ(n_left + n_right, e.Size()); CHECK_LE(begin + n_left, e.end); CHECK_EQ(begin + n_left + n_right, e.end); - if (left_node_id >= elem_of_each_node_.size()) { elem_of_each_node_.resize(left_node_id + 1, Elem(nullptr, nullptr, -1)); } @@ -102,7 +84,6 @@ class RowSetCollection { elem_of_each_node_.resize(right_node_id + 1, Elem(nullptr, nullptr, -1)); } - elem_of_each_node_[left_node_id] = Elem(begin, begin + n_left, left_node_id); elem_of_each_node_[right_node_id] = Elem(begin + n_left, e.end, right_node_id); elem_of_each_node_[node_id] = Elem(nullptr, nullptr, -1); @@ -119,5 +100,4 @@ class RowSetCollection { } // namespace sycl } // namespace xgboost - #endif // PLUGIN_SYCL_COMMON_ROW_SET_H_ diff --git a/plugin/sycl/common/transform.h b/plugin/sycl/common/transform.h index 261d71f2330d..619958faba3e 100644 --- a/plugin/sycl/common/transform.h +++ b/plugin/sycl/common/transform.h @@ -5,10 +5,10 @@ #ifndef PLUGIN_SYCL_COMMON_TRANSFORM_H_ #define PLUGIN_SYCL_COMMON_TRANSFORM_H_ -#include "../device_manager.h" - #include +#include "../device_manager.h" + namespace xgboost { namespace sycl { namespace common { @@ -21,12 +21,11 @@ void LaunchSyclKernel(DeviceOrd device, Functor&& _func, xgboost::common::Range size_t size = *(_range.end()); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(size), - [=](::sycl::id<1> pid) { - const size_t idx = pid[0]; - const_cast(_func)(idx, _spans...); - }); - }).wait(); + cgh.parallel_for<>(::sycl::range<1>(size), [=](::sycl::id<1> pid) { + const size_t idx = pid[0]; + const_cast(_func)(idx, _spans...); + }); + }).wait(); } } // namespace common diff --git a/plugin/sycl/context_helper.cc b/plugin/sycl/context_helper.cc index d5ced146187c..2b141c6be6e1 100644 --- a/plugin/sycl/context_helper.cc +++ b/plugin/sycl/context_helper.cc @@ -3,11 +3,11 @@ * \file context_helper.cc */ -#include +#include "context_helper.h" +#include #include "device_manager.h" -#include "context_helper.h" namespace xgboost { namespace sycl { diff --git a/plugin/sycl/data.h b/plugin/sycl/data.h index c7f8d75e1404..e29235774ad0 100644 --- a/plugin/sycl/data.h +++ b/plugin/sycl/data.h @@ -4,12 +4,12 @@ #ifndef PLUGIN_SYCL_DATA_H_ #define PLUGIN_SYCL_DATA_H_ +#include #include #include +#include #include #include -#include -#include #include "xgboost/base.h" #pragma GCC diagnostic push @@ -17,31 +17,26 @@ #pragma GCC diagnostic ignored "-W#pragma-messages" #include "xgboost/data.h" #pragma GCC diagnostic pop -#include "xgboost/logging.h" -#include "xgboost/host_device_vector.h" +#include #include "../../src/common/threading_utils.h" - -#include +#include "xgboost/host_device_vector.h" +#include "xgboost/logging.h" namespace xgboost { namespace sycl { template -using AtomicRef = ::sycl::atomic_ref; +using AtomicRef = ::sycl::atomic_ref; -enum class MemoryType { shared, on_device}; +enum class MemoryType { shared, on_device }; template class USMDeleter { public: explicit USMDeleter(::sycl::queue* qu) : qu_(qu) {} - void operator()(T* data) const { - ::sycl::free(data, *qu_); - } + void operator()(T* data) const { ::sycl::free(data, *qu_); } private: ::sycl::queue* qu_; @@ -59,7 +54,7 @@ class USMVector { } } - void copy_vector_to_memory_(::sycl::queue* qu, const std::vector &vec) { + void copy_vector_to_memory_(::sycl::queue* qu, const std::vector& vec) { if constexpr (memory_type == MemoryType::shared) { std::copy(vec.begin(), vec.end(), data_.get()); } else { @@ -67,7 +62,6 @@ class USMVector { } } - public: USMVector() : size_(0), capacity_(0), data_(nullptr) {} @@ -80,21 +74,20 @@ class USMVector { qu->fill(data_.get(), v, size_).wait(); } - USMVector(::sycl::queue* qu, size_t size, T v, - ::sycl::event* event) : size_(size), capacity_(size) { + USMVector(::sycl::queue* qu, size_t size, T v, ::sycl::event* event) + : size_(size), capacity_(size) { data_ = allocate_memory_(qu, size_); *event = qu->fill(data_.get(), v, size_, *event); } - USMVector(::sycl::queue* qu, const std::vector &vec) { + USMVector(::sycl::queue* qu, const std::vector& vec) { size_ = vec.size(); capacity_ = size_; data_ = allocate_memory_(qu, size_); copy_vector_to_memory_(qu, vec); } - ~USMVector() { - } + ~USMVector() {} USMVector& operator=(const USMVector& other) { size_ = other.size_; @@ -110,11 +103,11 @@ class USMVector { size_t Capacity() const { return capacity_; } - T& operator[] (size_t i) { return data_.get()[i]; } - const T& operator[] (size_t i) const { return data_.get()[i]; } + T& operator[](size_t i) { return data_.get()[i]; } + const T& operator[](size_t i) const { return data_.get()[i]; } - T* Begin () const { return data_.get(); } - T* End () const { return data_.get() + size_; } + T* Begin() const { return data_.get(); } + T* End() const { return data_.get() + size_; } bool Empty() const { return (size_ == 0); } @@ -132,7 +125,7 @@ class USMVector { auto data_old = data_; size_ = size_new; capacity_ = size_new; - data_ = allocate_memory_(qu, size_);; + data_ = allocate_memory_(qu, size_); if (size_old > 0) { qu->memcpy(data_.get(), data_old.get(), sizeof(T) * size_old).wait(); } @@ -205,11 +198,9 @@ class USMVector { } } - ::sycl::event Fill(::sycl::queue* qu, T v) { - return qu->fill(data_.get(), v, size_); - } + ::sycl::event Fill(::sycl::queue* qu, T v) { return qu->fill(data_.get(), v, size_); } - void Init(::sycl::queue* qu, const std::vector &vec) { + void Init(::sycl::queue* qu, const std::vector& vec) { size_ = vec.size(); capacity_ = size_; data_ = allocate_memory_(qu, size_); diff --git a/plugin/sycl/data/gradient_index.cc b/plugin/sycl/data/gradient_index.cc index 4f29f2d28fc6..b6dd012a38de 100644 --- a/plugin/sycl/data/gradient_index.cc +++ b/plugin/sycl/data/gradient_index.cc @@ -2,13 +2,12 @@ * Copyright 2017-2024 by Contributors * \file gradient_index.cc */ -#include -#include -#include - #include "gradient_index.h" +#include +#include #include +#include namespace xgboost { namespace sycl { @@ -49,10 +48,8 @@ void mergeSort(BinIdxType* begin, BinIdxType* end, BinIdxType* buf) { } template -void GHistIndexMatrix::SetIndexData(::sycl::queue* qu, - Context const * ctx, - BinIdxType* index_data, - DMatrix *dmat) { +void GHistIndexMatrix::SetIndexData(::sycl::queue* qu, Context const* ctx, BinIdxType* index_data, + DMatrix* dmat) { if (nbins == 0) return; const bst_float* cut_values = cut.cut_values_.ConstDevicePointer(); const uint32_t* cut_ptrs = cut.cut_ptrs_.ConstDevicePointer(); @@ -60,11 +57,11 @@ void GHistIndexMatrix::SetIndexData(::sycl::queue* qu, BinIdxType* sort_data = reinterpret_cast(sort_buff.Data()); - for (auto &batch : dmat->GetBatches()) { + for (auto& batch : dmat->GetBatches()) { batch.data.SetDevice(ctx->Device()); batch.offset.SetDevice(ctx->Device()); - const xgboost::Entry *data_ptr = batch.data.ConstDevicePointer(); - const bst_idx_t *offset_vec = batch.offset.ConstDevicePointer(); + const xgboost::Entry* data_ptr = batch.data.ConstDevicePointer(); + const bst_idx_t* offset_vec = batch.offset.ConstDevicePointer(); size_t batch_size = batch.Size(); if (batch_size > 0) { const auto base_rowid = batch.base_rowid; @@ -101,8 +98,9 @@ void GHistIndexMatrix::ResizeIndex(::sycl::queue* qu, size_t n_index) { if ((max_num_bins - 1 <= static_cast(std::numeric_limits::max())) && isDense_) { index.SetBinTypeSize(BinTypeSize::kUint8BinsTypeSize); index.Resize(qu, (sizeof(uint8_t)) * n_index); - } else if ((max_num_bins - 1 > static_cast(std::numeric_limits::max()) && - max_num_bins - 1 <= static_cast(std::numeric_limits::max())) && isDense_) { + } else if ((max_num_bins - 1 > static_cast(std::numeric_limits::max()) && + max_num_bins - 1 <= static_cast(std::numeric_limits::max())) && + isDense_) { index.SetBinTypeSize(BinTypeSize::kUint16BinsTypeSize); index.Resize(qu, (sizeof(uint16_t)) * n_index); } else { @@ -111,10 +109,7 @@ void GHistIndexMatrix::ResizeIndex(::sycl::queue* qu, size_t n_index) { } } -void GHistIndexMatrix::Init(::sycl::queue* qu, - Context const * ctx, - DMatrix *dmat, - int max_bins) { +void GHistIndexMatrix::Init(::sycl::queue* qu, Context const* ctx, DMatrix* dmat, int max_bins) { nfeatures = dmat->Info().num_col_; cut = xgboost::common::SketchOnDMatrix(ctx, dmat, max_bins); @@ -168,8 +163,8 @@ void GHistIndexMatrix::Init(::sycl::queue* qu, CHECK_EQ(curent_bin_size, BinTypeSize::kUint32BinsTypeSize); SetIndexData(qu, ctx, index.data(), dmat); } - /* For sparse DMatrix we have to store index of feature for each bin - in index field to chose right offset. So offset is nullptr and index is not reduced */ + /* For sparse DMatrix we have to store index of feature for each bin + in index field to chose right offset. So offset is nullptr and index is not reduced */ } else { sort_buff.Resize(qu, n_rows * row_stride * sizeof(uint32_t)); SetIndexData(qu, ctx, index.data(), dmat); diff --git a/plugin/sycl/data/gradient_index.h b/plugin/sycl/data/gradient_index.h index 967ac9a87f9e..716c42a2f2e7 100644 --- a/plugin/sycl/data/gradient_index.h +++ b/plugin/sycl/data/gradient_index.h @@ -5,12 +5,11 @@ #ifndef PLUGIN_SYCL_DATA_GRADIENT_INDEX_H_ #define PLUGIN_SYCL_DATA_GRADIENT_INDEX_H_ +#include #include -#include "../data.h" #include "../../src/common/hist_util.h" - -#include +#include "../data.h" namespace xgboost { namespace sycl { @@ -22,52 +21,40 @@ using BinTypeSize = ::xgboost::common::BinTypeSize; * \brief Index data and offsets stored in USM buffers to provide access from device kernels */ struct Index { - Index() { - SetBinTypeSize(binTypeSize_); - } + Index() { SetBinTypeSize(binTypeSize_); } Index(const Index& i) = delete; Index& operator=(Index i) = delete; Index(Index&& i) = delete; Index& operator=(Index&& i) = delete; void SetBinTypeSize(BinTypeSize binTypeSize) { binTypeSize_ = binTypeSize; - CHECK(binTypeSize == BinTypeSize::kUint8BinsTypeSize || + CHECK(binTypeSize == BinTypeSize::kUint8BinsTypeSize || binTypeSize == BinTypeSize::kUint16BinsTypeSize || binTypeSize == BinTypeSize::kUint32BinsTypeSize); } - BinTypeSize GetBinTypeSize() const { - return binTypeSize_; - } + BinTypeSize GetBinTypeSize() const { return binTypeSize_; } - template + template T* data() { return reinterpret_cast(data_.Data()); } - template + template const T* data() const { return reinterpret_cast(data_.DataConst()); } - size_t Size() const { - return data_.Size() / (binTypeSize_); - } + size_t Size() const { return data_.Size() / (binTypeSize_); } - void Resize(::sycl::queue* qu, const size_t nBytesData) { - data_.Resize(qu, nBytesData); - } + void Resize(::sycl::queue* qu, const size_t nBytesData) { data_.Resize(qu, nBytesData); } - uint8_t* begin() const { - return data_.Begin(); - } + uint8_t* begin() const { return data_.Begin(); } - uint8_t* end() const { - return data_.End(); - } + uint8_t* end() const { return data_.End(); } private: USMVector data_; - BinTypeSize binTypeSize_ {BinTypeSize::kUint8BinsTypeSize}; + BinTypeSize binTypeSize_{BinTypeSize::kUint8BinsTypeSize}; }; /*! @@ -92,12 +79,10 @@ struct GHistIndexMatrix { size_t row_stride; // Create a global histogram matrix based on a given DMatrix device wrapper - void Init(::sycl::queue* qu, Context const * ctx, - DMatrix *dmat, int max_num_bins); + void Init(::sycl::queue* qu, Context const* ctx, DMatrix* dmat, int max_num_bins); template - void SetIndexData(::sycl::queue* qu, Context const * ctx, BinIdxType* index_data, - DMatrix *dmat); + void SetIndexData(::sycl::queue* qu, Context const* ctx, BinIdxType* index_data, DMatrix* dmat); void ResizeIndex(::sycl::queue* qu, size_t n_index); @@ -111,9 +96,7 @@ struct GHistIndexMatrix { } } } - inline bool IsDense() const { - return isDense_; - } + inline bool IsDense() const { return isDense_; } private: bool isDense_; diff --git a/plugin/sycl/device_manager.cc b/plugin/sycl/device_manager.cc index dc3939934e31..9005e83133e6 100644 --- a/plugin/sycl/device_manager.cc +++ b/plugin/sycl/device_manager.cc @@ -4,79 +4,80 @@ */ #include "../sycl/device_manager.h" +#include + #include "../../src/collective/communicator-inl.h" namespace xgboost { namespace sycl { ::sycl::queue* DeviceManager::GetQueue(const DeviceOrd& device_spec) const { - if (!device_spec.IsSycl()) { - LOG(WARNING) << "Sycl kernel is executed with non-sycl context: " - << device_spec.Name() << ". " - << "Default sycl device_selector will be used."; - } + if (!device_spec.IsSycl()) { + LOG(WARNING) << "Sycl kernel is executed with non-sycl context: " << device_spec.Name() << ". " + << "Default sycl device_selector will be used."; + } - size_t queue_idx; - bool not_use_default_selector = (device_spec.ordinal != kDefaultOrdinal) || - (collective::IsDistributed()); - DeviceRegister& device_register = GetDevicesRegister(); - if (not_use_default_selector) { - const int device_idx = - collective::IsDistributed() ? collective::GetRank() : device_spec.ordinal; - if (device_spec.IsSyclDefault()) { - auto& devices = device_register.devices; - CHECK_LT(device_idx, devices.size()); - queue_idx = device_idx; - } else if (device_spec.IsSyclCPU()) { - auto& cpu_devices_idxes = device_register.cpu_devices_idxes; - CHECK_LT(device_idx, cpu_devices_idxes.size()); - queue_idx = cpu_devices_idxes[device_idx]; - } else if (device_spec.IsSyclGPU()) { - auto& gpu_devices_idxes = device_register.gpu_devices_idxes; - CHECK_LT(device_idx, gpu_devices_idxes.size()); - queue_idx = gpu_devices_idxes[device_idx]; - } else { - LOG(WARNING) << device_spec << " is not sycl, sycl:cpu or sycl:gpu"; - auto device = ::sycl::queue(::sycl::default_selector_v).get_device(); - queue_idx = device_register.devices.at(device); - } + size_t queue_idx; + bool not_use_default_selector = + (device_spec.ordinal != kDefaultOrdinal) || (collective::IsDistributed()); + DeviceRegister& device_register = GetDevicesRegister(); + if (not_use_default_selector) { + const int device_idx = + collective::IsDistributed() ? collective::GetRank() : device_spec.ordinal; + if (device_spec.IsSyclDefault()) { + auto& devices = device_register.devices; + CHECK_LT(device_idx, devices.size()); + queue_idx = device_idx; + } else if (device_spec.IsSyclCPU()) { + auto& cpu_devices_idxes = device_register.cpu_devices_idxes; + CHECK_LT(device_idx, cpu_devices_idxes.size()); + queue_idx = cpu_devices_idxes[device_idx]; + } else if (device_spec.IsSyclGPU()) { + auto& gpu_devices_idxes = device_register.gpu_devices_idxes; + CHECK_LT(device_idx, gpu_devices_idxes.size()); + queue_idx = gpu_devices_idxes[device_idx]; } else { - if (device_spec.IsSyclCPU()) { - auto device = ::sycl::queue(::sycl::cpu_selector_v).get_device(); - queue_idx = device_register.devices.at(device); - } else if (device_spec.IsSyclGPU()) { - auto device = ::sycl::queue(::sycl::gpu_selector_v).get_device(); - queue_idx = device_register.devices.at(device); - } else { - auto device = ::sycl::queue(::sycl::default_selector_v).get_device(); - queue_idx = device_register.devices.at(device); - } + LOG(WARNING) << device_spec << " is not sycl, sycl:cpu or sycl:gpu"; + auto device = ::sycl::queue(::sycl::default_selector_v).get_device(); + queue_idx = device_register.devices.at(device); } - return &(device_register.queues[queue_idx]); + } else { + if (device_spec.IsSyclCPU()) { + auto device = ::sycl::queue(::sycl::cpu_selector_v).get_device(); + queue_idx = device_register.devices.at(device); + } else if (device_spec.IsSyclGPU()) { + auto device = ::sycl::queue(::sycl::gpu_selector_v).get_device(); + queue_idx = device_register.devices.at(device); + } else { + auto device = ::sycl::queue(::sycl::default_selector_v).get_device(); + queue_idx = device_register.devices.at(device); + } + } + return &(device_register.queues[queue_idx]); } DeviceManager::DeviceRegister& DeviceManager::GetDevicesRegister() const { - static DeviceRegister device_register; + static DeviceRegister device_register; - if (device_register.devices.size() == 0) { - std::lock_guard guard(device_registering_mutex); - std::vector<::sycl::device> devices = ::sycl::device::get_devices(); - for (size_t i = 0; i < devices.size(); i++) { - LOG(INFO) << "device_index = " << i << ", name = " - << devices[i].get_info<::sycl::info::device::name>(); - } + if (device_register.devices.size() == 0) { + std::lock_guard guard(device_registering_mutex); + std::vector<::sycl::device> devices = ::sycl::device::get_devices(); + for (size_t i = 0; i < devices.size(); i++) { + LOG(INFO) << "device_index = " << i + << ", name = " << devices[i].get_info<::sycl::info::device::name>(); + } - for (size_t i = 0; i < devices.size(); i++) { - device_register.devices[devices[i]] = i; - device_register.queues.push_back(::sycl::queue(devices[i])); - if (devices[i].is_cpu()) { - device_register.cpu_devices_idxes.push_back(i); - } else if (devices[i].is_gpu()) { - device_register.gpu_devices_idxes.push_back(i); - } - } + for (size_t i = 0; i < devices.size(); i++) { + device_register.devices[devices[i]] = i; + device_register.queues.push_back(::sycl::queue(devices[i])); + if (devices[i].is_cpu()) { + device_register.cpu_devices_idxes.push_back(i); + } else if (devices[i].is_gpu()) { + device_register.gpu_devices_idxes.push_back(i); + } } - return device_register; + } + return device_register; } } // namespace sycl diff --git a/plugin/sycl/device_manager.h b/plugin/sycl/device_manager.h index 701647237241..e838c1abc0cc 100644 --- a/plugin/sycl/device_manager.h +++ b/plugin/sycl/device_manager.h @@ -5,12 +5,11 @@ #ifndef PLUGIN_SYCL_DEVICE_MANAGER_H_ #define PLUGIN_SYCL_DEVICE_MANAGER_H_ -#include #include #include -#include - #include +#include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" diff --git a/plugin/sycl/device_properties.h b/plugin/sycl/device_properties.h index 96f258737c2b..7951aa0d3941 100644 --- a/plugin/sycl/device_properties.h +++ b/plugin/sycl/device_properties.h @@ -5,9 +5,10 @@ #ifndef PLUGIN_SYCL_DEVICE_PROPERTIES_H_ #define PLUGIN_SYCL_DEVICE_PROPERTIES_H_ -#include #include -#include "../../src/common/common.h" // for HumanMemUnit +#include + +#include "../../src/common/common.h" // for HumanMemUnit namespace xgboost { namespace sycl { @@ -20,16 +21,15 @@ class DeviceProperties { } void GetSRAMSize(const ::sycl::device& device) { - auto arch = - device.get_info<::sycl::ext::oneapi::experimental::info::device::architecture>(); + auto arch = device.get_info<::sycl::ext::oneapi::experimental::info::device::architecture>(); size_t eu_per_core = - device.get_info<::sycl::ext::intel::info::device::gpu_eu_count_per_subslice>(); + device.get_info<::sycl::ext::intel::info::device::gpu_eu_count_per_subslice>(); switch (arch) { case ::sycl::ext::oneapi::experimental::architecture::intel_gpu_pvc: { LOG(INFO) << "Xe-HPC (Ponte Vecchio) Architecture. L1 friendly optimization enabled."; size_t l1_size = 512 * 1024; size_t registers_size = 64 * 1024; - sram_size_per_eu = l1_size / eu_per_core + registers_size; + sram_size_per_eu = l1_size / eu_per_core + registers_size; break; } default: @@ -47,20 +47,19 @@ class DeviceProperties { size_t l2_size = 0; float l2_size_per_eu = 0; - DeviceProperties(): - is_gpu(false) {} + DeviceProperties() : is_gpu(false) {} - explicit DeviceProperties(const ::sycl::device& device): - is_gpu(device.is_gpu()), - usm_host_allocations(device.has(::sycl::aspect::usm_host_allocations)), - max_compute_units(device.get_info<::sycl::info::device::max_compute_units>()), - max_work_group_size(device.get_info<::sycl::info::device::max_work_group_size>()), - sub_group_size(device.get_info<::sycl::info::device::sub_group_sizes>().back()) { - GetL2Size(device); - if (is_gpu) { - GetSRAMSize(device); - } + explicit DeviceProperties(const ::sycl::device& device) + : is_gpu(device.is_gpu()), + usm_host_allocations(device.has(::sycl::aspect::usm_host_allocations)), + max_compute_units(device.get_info<::sycl::info::device::max_compute_units>()), + max_work_group_size(device.get_info<::sycl::info::device::max_work_group_size>()), + sub_group_size(device.get_info<::sycl::info::device::sub_group_sizes>().back()) { + GetL2Size(device); + if (is_gpu) { + GetSRAMSize(device); } + } }; } // namespace sycl diff --git a/plugin/sycl/predictor/node.h b/plugin/sycl/predictor/node.h index feed8b3123dd..44f145733dff 100644 --- a/plugin/sycl/predictor/node.h +++ b/plugin/sycl/predictor/node.h @@ -40,9 +40,9 @@ class Node { } } - int LeftChildIdx() const {return left_child_idx; } + int LeftChildIdx() const { return left_child_idx; } - int RightChildIdx() const {return right_child_idx; } + int RightChildIdx() const { return right_child_idx; } bool IsLeaf() const { return left_child_idx == -1; } diff --git a/plugin/sycl/predictor/predictor.cc b/plugin/sycl/predictor/predictor.cc old mode 100755 new mode 100644 index 1b4f8f9ee2d6..8c8d2f871d66 --- a/plugin/sycl/predictor/predictor.cc +++ b/plugin/sycl/predictor/predictor.cc @@ -6,20 +6,21 @@ #pragma GCC diagnostic ignored "-W#pragma-messages" #pragma GCC diagnostic pop +#include "xgboost/predictor.h" + +#include #include #include +#include #include - #include +#include +#include "../../../src/common/timer.h" #include "../data.h" - #include "dmlc/registry.h" - #include "xgboost/tree_model.h" -#include "xgboost/predictor.h" #include "xgboost/tree_updater.h" -#include "../../../src/common/timer.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" @@ -27,7 +28,6 @@ #pragma GCC diagnostic pop #include "../../src/common/math.h" #include "../../src/gbm/gbtree_model.h" - #include "../device_manager.h" #include "../device_properties.h" #include "node.h" @@ -38,13 +38,12 @@ void InitOutPredictions(Context const* ctx, linalg::VectorView base sycl::DeviceManager device_manager; auto* qu = device_manager.GetQueue(predt.Device()); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(predt.Size()), - [=](::sycl::id<1> pid) { - size_t k = pid[0]; - auto [i, j] = xgboost::linalg::UnravelIndex(k, predt.Shape()); - const_cast(predt(i, j)) = base_score(j); - }); - }).wait_and_throw(); + cgh.parallel_for<>(::sycl::range<1>(predt.Size()), [=](::sycl::id<1> pid) { + size_t k = pid[0]; + auto [i, j] = xgboost::linalg::UnravelIndex(k, predt.Shape()); + const_cast(predt(i, j)) = base_score(j); + }); + }).wait_and_throw(); } } // namespace xgboost::sycl_impl @@ -100,8 +99,8 @@ class DeviceModel { }; // Binary search -float BinarySearch(const Entry* begin_ptr, const Entry* end_ptr, - size_t col_idx, size_t num_features) { +float BinarySearch(const Entry* begin_ptr, const Entry* end_ptr, size_t col_idx, + size_t num_features) { const size_t n_elems = end_ptr - begin_ptr; if (n_elems == num_features) { return (begin_ptr + col_idx)->fvalue; @@ -146,15 +145,15 @@ size_t NextNodeIdx(float fvalue, const Node& node) { } } -float GetLeafWeight(const Node* nodes, const Entry* first_entry, - const Entry* last_entry, size_t num_features) { +float GetLeafWeight(const Node* nodes, const Entry* first_entry, const Entry* last_entry, + size_t num_features) { size_t is_dense = (last_entry - first_entry == num_features); const Node* node = nodes; while (!node->IsLeaf()) { - const float fvalue = is_dense ? - (first_entry + node->GetFidx())->fvalue : - BinarySearch(first_entry, last_entry, node->GetFidx(), num_features); + const float fvalue = is_dense + ? (first_entry + node->GetFidx())->fvalue + : BinarySearch(first_entry, last_entry, node->GetFidx(), num_features); node = nodes + NextNodeIdx(fvalue, *node); } return node->GetWeight(); @@ -171,13 +170,12 @@ float GetLeafWeight(const Node* nodes, const float* fval_buff) { class Predictor : public xgboost::Predictor { public: - explicit Predictor(Context const* context) : - xgboost::Predictor::Predictor{context}, - cpu_predictor(xgboost::Predictor::Create("cpu_predictor", context)) {} + explicit Predictor(Context const* context) + : xgboost::Predictor::Predictor{context}, + cpu_predictor(xgboost::Predictor::Create("cpu_predictor", context)) {} - void PredictBatch(DMatrix *dmat, PredictionCacheEntry *predts, - const gbm::GBTreeModel &model, bst_tree_t tree_begin, - bst_tree_t tree_end = 0) const override { + void PredictBatch(DMatrix* dmat, PredictionCacheEntry* predts, const gbm::GBTreeModel& model, + bst_tree_t tree_begin, bst_tree_t tree_end = 0) const override { auto* out_preds = &predts->predictions; device_model.SetDevice(ctx_->Device()); qu_ = device_manager.GetQueue(ctx_->Device()); @@ -201,9 +199,8 @@ class Predictor : public xgboost::Predictor { } } - bool InplacePredict(std::shared_ptr p_m, - const gbm::GBTreeModel &model, float missing, - PredictionCacheEntry *out_preds, bst_tree_t tree_begin, + bool InplacePredict(std::shared_ptr p_m, const gbm::GBTreeModel& model, float missing, + PredictionCacheEntry* out_preds, bst_tree_t tree_begin, bst_tree_t tree_end) const override { LOG(WARNING) << "InplacePredict is not yet implemented for SYCL. CPU Predictor is used."; return cpu_predictor->InplacePredict(p_m, model, missing, out_preds, tree_begin, tree_end); @@ -217,9 +214,8 @@ class Predictor : public xgboost::Predictor { void PredictContribution(DMatrix* p_fmat, HostDeviceVector* out_contribs, const gbm::GBTreeModel& model, bst_tree_t ntree_limit, - const std::vector* tree_weights, - bool approximate, int condition, - unsigned condition_feature) const override { + const std::vector* tree_weights, bool approximate, + int condition, unsigned condition_feature) const override { LOG(WARNING) << "PredictContribution is not yet implemented for SYCL. CPU Predictor is used."; cpu_predictor->PredictContribution(p_fmat, out_contribs, model, ntree_limit, tree_weights, approximate, condition, condition_feature); @@ -244,27 +240,21 @@ class Predictor : public xgboost::Predictor { static constexpr float kCostCalibrationDescrete = 4; template - void PredictKernelBufferDispatch(::sycl::event* event, - const Entry* data, - float* out_predictions, - const size_t* row_ptr, - size_t num_rows, - size_t num_features, - size_t num_group, - size_t tree_begin, - size_t tree_end, + void PredictKernelBufferDispatch(::sycl::event* event, const Entry* data, float* out_predictions, + const size_t* row_ptr, size_t num_rows, size_t num_features, + size_t num_group, size_t tree_begin, size_t tree_end, float sparsity) const { if constexpr (kFeatureBufferSize > kMaxFeatureBufferSize) { LOG(FATAL) << "Unreachable"; } else { if (num_features > kFeatureBufferSize) { - PredictKernelBufferDispatch - (event, data, out_predictions, row_ptr, num_rows, - num_features, num_group, tree_begin, tree_end, sparsity); + PredictKernelBufferDispatch( + event, data, out_predictions, row_ptr, num_rows, num_features, num_group, tree_begin, + tree_end, sparsity); } else { - PredictKernelBuffer - (event, data, out_predictions, row_ptr, num_rows, - num_features, num_group, tree_begin, tree_end, sparsity); + PredictKernelBuffer(event, data, out_predictions, row_ptr, + num_rows, num_features, num_group, + tree_begin, tree_end, sparsity); } } } @@ -275,11 +265,12 @@ class Predictor : public xgboost::Predictor { size_t sub_group_size = device_prop_.sub_group_size; size_t nodes_bytes = n_nodes * sizeof(Node); bool nodes_fit_l2 = l2_size > 2 * nodes_bytes; - size_t block_size = nodes_fit_l2 - // nodes and data fit L2 - ? 0.8 * (l2_size - nodes_bytes) / (sparsity * num_features * sizeof(Entry)) - // only data fit L2 - : 0.8 * (l2_size) / (sparsity * num_features * sizeof(Entry)); + size_t block_size = + nodes_fit_l2 + // nodes and data fit L2 + ? 0.8 * (l2_size - nodes_bytes) / (sparsity * num_features * sizeof(Entry)) + // only data fit L2 + : 0.8 * (l2_size) / (sparsity * num_features * sizeof(Entry)); block_size = (block_size / sub_group_size) * sub_group_size; if (block_size < max_compute_units * sub_group_size) { block_size = max_compute_units * sub_group_size; @@ -290,22 +281,15 @@ class Predictor : public xgboost::Predictor { } template - void PredictKernelBuffer(::sycl::event* event, - const Entry* data, - float* out_predictions, - const size_t* row_ptr, - size_t num_rows, - size_t num_features, - size_t num_group, - size_t tree_begin, - size_t tree_end, + void PredictKernelBuffer(::sycl::event* event, const Entry* data, float* out_predictions, + const size_t* row_ptr, size_t num_rows, size_t num_features, + size_t num_group, size_t tree_begin, size_t tree_end, float sparsity) const { const Node* nodes = device_model.nodes.ConstDevicePointer(); const size_t* first_node_position = device_model.first_node_position.ConstDevicePointer(); const int* tree_group = device_model.tree_group.ConstDevicePointer(); - size_t block_size = GetBlockSize(device_model.nodes.Size(), - num_features, num_rows, sparsity); + size_t block_size = GetBlockSize(device_model.nodes.Size(), num_features, num_rows, sparsity); size_t n_blocks = num_rows / block_size + (num_rows % block_size > 0); for (size_t block = 0; block < n_blocks; ++block) { @@ -337,8 +321,7 @@ class Predictor : public xgboost::Predictor { for (int tree_idx = tree_begin; tree_idx < tree_end; tree_idx++) { const Node* first_node = nodes + first_node_position[tree_idx - tree_begin]; int out_prediction_idx = row_idx * num_group + tree_group[tree_idx]; - out_predictions[out_prediction_idx] += - GetLeafWeight(first_node, fvalues); + out_predictions[out_prediction_idx] += GetLeafWeight(first_node, fvalues); } } } @@ -347,22 +330,14 @@ class Predictor : public xgboost::Predictor { } } - void PredictKernel(::sycl::event* event, - const Entry* data, - float* out_predictions, - const size_t* row_ptr, - size_t num_rows, - size_t num_features, - size_t num_group, - size_t tree_begin, - size_t tree_end, - float sparsity) const { + void PredictKernel(::sycl::event* event, const Entry* data, float* out_predictions, + const size_t* row_ptr, size_t num_rows, size_t num_features, size_t num_group, + size_t tree_begin, size_t tree_end, float sparsity) const { const Node* nodes = device_model.nodes.ConstDevicePointer(); const size_t* first_node_position = device_model.first_node_position.ConstDevicePointer(); const int* tree_group = device_model.tree_group.ConstDevicePointer(); - size_t block_size = GetBlockSize(device_model.nodes.Size(), - num_features, num_rows, sparsity); + size_t block_size = GetBlockSize(device_model.nodes.Size(), num_features, num_rows, sparsity); size_t n_blocks = num_rows / block_size + (num_rows % block_size > 0); for (size_t block = 0; block < n_blocks; ++block) { @@ -395,9 +370,7 @@ class Predictor : public xgboost::Predictor { } template - bool UseFvalueBuffer(size_t tree_begin, - size_t tree_end, - int num_features) const { + bool UseFvalueBuffer(size_t tree_begin, size_t tree_end, int num_features) const { size_t n_nodes = device_model.nodes.Size(); size_t n_trees = tree_end - tree_begin; float av_depth = std::log2(static_cast(n_nodes) / n_trees); @@ -410,22 +383,19 @@ class Predictor : public xgboost::Predictor { n_reads *= std::log2(static_cast(num_features)); } - float cost_callibration = device_prop_.usm_host_allocations - ? kCostCalibrationIntegrated - : kCostCalibrationDescrete; + float cost_callibration = + device_prop_.usm_host_allocations ? kCostCalibrationIntegrated : kCostCalibrationDescrete; // number of writes in local memory. float n_writes = num_features; - bool use_fvalue_buffer = (num_features <= kMaxFeatureBufferSize) && - (n_reads > cost_callibration * n_writes); + bool use_fvalue_buffer = + (num_features <= kMaxFeatureBufferSize) && (n_reads > cost_callibration * n_writes); return use_fvalue_buffer; } template - void DevicePredictInternal(DMatrix *dmat, - HostDeviceVector* out_preds, - const gbm::GBTreeModel& model, - size_t tree_begin, + void DevicePredictInternal(DMatrix* dmat, HostDeviceVector* out_preds, + const gbm::GBTreeModel& model, size_t tree_begin, size_t tree_end) const { if (tree_end - tree_begin == 0) return; if (out_preds->Size() == 0) return; @@ -437,7 +407,7 @@ class Predictor : public xgboost::Predictor { float* out_predictions = out_preds->DevicePointer(); ::sycl::event event; - for (auto &batch : dmat->GetBatches()) { + for (auto& batch : dmat->GetBatches()) { batch.data.SetDevice(ctx_->Device()); batch.offset.SetDevice(ctx_->Device()); const Entry* data = batch.data.ConstDevicePointer(); @@ -449,15 +419,12 @@ class Predictor : public xgboost::Predictor { float sparsity = static_cast(batch.data.Size()) / (batch_size * num_features); if (UseFvalueBuffer(tree_begin, tree_end, num_features)) { - PredictKernelBufferDispatch(&event, data, - out_predictions + base_rowid * num_group, - row_ptr, batch_size, num_features, - num_group, tree_begin, tree_end, sparsity); + PredictKernelBufferDispatch( + &event, data, out_predictions + base_rowid * num_group, row_ptr, batch_size, + num_features, num_group, tree_begin, tree_end, sparsity); } else { - PredictKernel(&event, data, - out_predictions + base_rowid * num_group, - row_ptr, batch_size, num_features, - num_group, tree_begin, tree_end, sparsity); + PredictKernel(&event, data, out_predictions + base_rowid * num_group, row_ptr, batch_size, + num_features, num_group, tree_begin, tree_end, sparsity); } } } @@ -475,8 +442,8 @@ class Predictor : public xgboost::Predictor { }; XGBOOST_REGISTER_PREDICTOR(Predictor, "sycl_predictor") -.describe("Make predictions using SYCL.") -.set_body([](Context const* ctx) { return new Predictor(ctx); }); + .describe("Make predictions using SYCL.") + .set_body([](Context const* ctx) { return new Predictor(ctx); }); } // namespace predictor } // namespace sycl diff --git a/plugin/sycl/tree/expand_entry.h b/plugin/sycl/tree/expand_entry.h index 807b27d44275..ddc55ca02ed9 100644 --- a/plugin/sycl/tree/expand_entry.h +++ b/plugin/sycl/tree/expand_entry.h @@ -16,7 +16,7 @@ namespace sycl { namespace tree { /* tree growing policies */ struct ExpandEntry : public xgboost::tree::ExpandEntryImpl { - static constexpr bst_node_t kRootNid = 0; + static constexpr bst_node_t kRootNid = 0; xgboost::tree::SplitEntry split; @@ -32,7 +32,7 @@ struct ExpandEntry : public xgboost::tree::ExpandEntryImpl { return tree.IsLeftChild(nid) ? tree.RightChild(parent_id) : tree.LeftChild(parent_id); } - bool IsValidImpl(xgboost::tree::TrainParam const ¶m, int32_t num_leaves) const { + bool IsValidImpl(xgboost::tree::TrainParam const& param, int32_t num_leaves) const { if (split.loss_chg <= kRtEps) return false; if (split.loss_chg < param.min_split_loss) return false; if (param.max_depth > 0 && depth == param.max_depth) return false; diff --git a/plugin/sycl/tree/hist_dispatcher.h b/plugin/sycl/tree/hist_dispatcher.h index fe3874a90656..fe99060402db 100644 --- a/plugin/sycl/tree/hist_dispatcher.h +++ b/plugin/sycl/tree/hist_dispatcher.h @@ -14,7 +14,9 @@ namespace xgboost { namespace sycl { namespace tree { -struct BlockParams { size_t size, nblocks; }; +struct BlockParams { + size_t size, nblocks; +}; template class HistDispatcher { @@ -72,9 +74,8 @@ class HistDispatcher { return {block_size, nblocks}; } - HistDispatcher(const DeviceProperties& device_prop, bool isDense, size_t size, - size_t max_nblocks, size_t nbins, size_t ncolumns, - size_t max_num_bins, size_t min_num_bins) { + HistDispatcher(const DeviceProperties& device_prop, bool isDense, size_t size, size_t max_nblocks, + size_t nbins, size_t ncolumns, size_t max_num_bins, size_t min_num_bins) { block = GetBlocksParameters(size, max_nblocks, device_prop.max_compute_units); work_group_size = std::min(ncolumns, device_prop.max_work_group_size); if (!device_prop.is_gpu) return; @@ -83,33 +84,31 @@ class HistDispatcher { /* If local histogram is possible and beneficial */ const int buff_size = nbins * sizeof(GradientPairT); /* block_size writes into array of size max_num_bins are made, - * if (block_size < max_num_bins) - * most part of buffer isn't used and perf suffers. - */ + * if (block_size < max_num_bins) + * most part of buffer isn't used and perf suffers. + */ const size_t th_block_size = max_num_bins; - use_local_hist = (buff_size < device_prop.sram_size_per_eu - KLocalHistSRAM) - && isDense - && (max_num_bins <= KMaxNumBins) - && (block.size >= th_block_size); + use_local_hist = (buff_size < device_prop.sram_size_per_eu - KLocalHistSRAM) && isDense && + (max_num_bins <= KMaxNumBins) && (block.size >= th_block_size); /* Predict penalty from atomic usage and compare with one from block-based build with buffer */ // EUs processing different columns do not trigger conflicts. float wg_per_columns = std::max(1.0f, static_cast(ncolumns) / kMaxWorkGroupSizeAtomic); /* Rows are processed per execution unit. - * Some EUs process different columns, and don't triiger conflicts. - * We use a worse case scenario, i.e. use the minimal number of bins per feature - */ + * Some EUs process different columns, and don't triiger conflicts. + * We use a worse case scenario, i.e. use the minimal number of bins per feature + */ float conflicts_per_bin = (device_prop.max_compute_units / wg_per_columns) / min_num_bins; // Atomics resolve conflicts between EUs, so L2 size can be a proxy for atomic efficiency. float atomic_efficency = device_prop.l2_size_per_eu / kAtomicEfficiencyNormalization; // We use simple quadratic model to predict atomic penalty - float atomic_penalty = conflicts_per_bin - + kAtomicQuadraticWeight * (conflicts_per_bin * conflicts_per_bin); + float atomic_penalty = + conflicts_per_bin + kAtomicQuadraticWeight * (conflicts_per_bin * conflicts_per_bin); // Block-based builder operates with buffer of type FPType, placed in L2. - float base_block_penalty = kBlockPenaltyNormalization / - device_prop.l2_size_per_eu * (sizeof(FPType) / 4); + float base_block_penalty = + kBlockPenaltyNormalization / device_prop.l2_size_per_eu * (sizeof(FPType) / 4); if (block.nblocks >= device_prop.max_compute_units) { // if GPU is fully loaded, we can simply compare penaltys. @@ -117,48 +116,45 @@ class HistDispatcher { } else { float blocks_per_eu = static_cast(block.nblocks) / device_prop.max_compute_units; /* The GPU is not 100% loaded. We need to take this into account in our model: - * block_penalty = base_block_penalty + base_time * (1 - blocks_per_eu); - * - * atomics should be used, if: - * block_penalty > atomic_penalty - * - * The normalization is chosen so that: base_time = 1 - * base_block_penalty + 1 - blocks_per_eu > atomic_penalty / atomic_efficency - * - * blocks_per_eu < 1 + base_block_penalty - atomic_penalty / atomic_efficency - */ + * block_penalty = base_block_penalty + base_time * (1 - blocks_per_eu); + * + * atomics should be used, if: + * block_penalty > atomic_penalty + * + * The normalization is chosen so that: base_time = 1 + * base_block_penalty + 1 - blocks_per_eu > atomic_penalty / atomic_efficency + * + * blocks_per_eu < 1 + base_block_penalty - atomic_penalty / atomic_efficency + */ float th_block_per_eu = 1 + base_block_penalty - atomic_penalty / atomic_efficency; /* We can't trust the decision of the approximate performance model - * if penalties are close to each other - * i.e. (1 + base_block_penalty) ~ (atomic_penalty / atomic_efficency) - * We manually limit the minimal value of th_block_per_eu, - * to determine the behaviour in this region. - */ + * if penalties are close to each other + * i.e. (1 + base_block_penalty) ~ (atomic_penalty / atomic_efficency) + * We manually limit the minimal value of th_block_per_eu, + * to determine the behaviour in this region. + */ th_block_per_eu = std::max(kMinTh, th_block_per_eu); use_atomics = (blocks_per_eu < th_block_per_eu); } if (use_atomics) { - work_group_size = std::min(kMaxWorkGroupSizeAtomic, - work_group_size); + work_group_size = std::min(kMaxWorkGroupSizeAtomic, work_group_size); } else if (use_local_hist) { - work_group_size = std::min(kMaxWorkGroupSizeLocal, - work_group_size); + work_group_size = std::min(kMaxWorkGroupSizeLocal, work_group_size); } } }; // For some datasets buffer is not used, we estimate if it is the case. -template +template size_t GetRequiredBufferSize(const DeviceProperties& device_prop, size_t max_n_rows, size_t nbins, size_t ncolumns, size_t max_num_bins, size_t min_num_bins) { size_t max_nblocks = HistDispatcher::kMaxGPUUtilisation * device_prop.max_compute_units; // Buffer size doesn't depend on isDense flag. - auto build_params = HistDispatcher - (device_prop, true, max_n_rows, max_nblocks, nbins, - ncolumns, max_num_bins, min_num_bins); + auto build_params = HistDispatcher(device_prop, true, max_n_rows, max_nblocks, nbins, + ncolumns, max_num_bins, min_num_bins); return build_params.use_atomics ? 0 : build_params.block.nblocks; } diff --git a/plugin/sycl/tree/hist_row_adder.h b/plugin/sycl/tree/hist_row_adder.h index 93650d5d0746..08190ca494fa 100644 --- a/plugin/sycl/tree/hist_row_adder.h +++ b/plugin/sycl/tree/hist_row_adder.h @@ -5,8 +5,8 @@ #ifndef PLUGIN_SYCL_TREE_HIST_ROW_ADDER_H_ #define PLUGIN_SYCL_TREE_HIST_ROW_ADDER_H_ -#include #include +#include namespace xgboost { namespace sycl { @@ -15,16 +15,16 @@ namespace tree { template class HistRowsAdder { public: - virtual void AddHistRows(HistUpdater* builder, - std::vector* sync_ids, RegTree *p_tree) = 0; + virtual void AddHistRows(HistUpdater* builder, std::vector* sync_ids, + RegTree* p_tree) = 0; virtual ~HistRowsAdder() = default; }; template -class BatchHistRowsAdder: public HistRowsAdder { +class BatchHistRowsAdder : public HistRowsAdder { public: - void AddHistRows(HistUpdater* builder, - std::vector* sync_ids, RegTree *p_tree) override { + void AddHistRows(HistUpdater* builder, std::vector* sync_ids, + RegTree* p_tree) override { builder->builder_monitor_.Start("AddHistRows"); for (auto const& entry : builder->nodes_for_explicit_hist_build_) { @@ -39,12 +39,11 @@ class BatchHistRowsAdder: public HistRowsAdder { } }; - template -class DistributedHistRowsAdder: public HistRowsAdder { +class DistributedHistRowsAdder : public HistRowsAdder { public: - void AddHistRows(HistUpdater* builder, - std::vector* sync_ids, RegTree *p_tree) override { + void AddHistRows(HistUpdater* builder, std::vector* sync_ids, + RegTree* p_tree) override { builder->builder_monitor_.Start("AddHistRows"); const size_t explicit_size = builder->nodes_for_explicit_hist_build_.size(); const size_t subtaction_size = builder->nodes_for_subtraction_trick_.size(); @@ -53,8 +52,7 @@ class DistributedHistRowsAdder: public HistRowsAdder { merged_node_ids[i] = builder->nodes_for_explicit_hist_build_[i].nid; } for (size_t i = 0; i < subtaction_size; ++i) { - merged_node_ids[explicit_size + i] = - builder->nodes_for_subtraction_trick_[i].nid; + merged_node_ids[explicit_size + i] = builder->nodes_for_subtraction_trick_[i].nid; } std::sort(merged_node_ids.begin(), merged_node_ids.end()); sync_ids->clear(); diff --git a/plugin/sycl/tree/hist_synchronizer.h b/plugin/sycl/tree/hist_synchronizer.h index 31d84413c41e..9a337b9ad6d5 100644 --- a/plugin/sycl/tree/hist_synchronizer.h +++ b/plugin/sycl/tree/hist_synchronizer.h @@ -26,7 +26,7 @@ class HistSynchronizer { }; template -class BatchHistSynchronizer: public HistSynchronizer { +class BatchHistSynchronizer : public HistSynchronizer { public: void SyncHistograms(HistUpdater* builder, const std::vector& sync_ids, RegTree const* p_tree) override { @@ -52,16 +52,14 @@ class BatchHistSynchronizer: public HistSynchronizer { builder->builder_monitor_.Stop("SyncHistograms"); } - std::vector<::sycl::event> GetEvents() const { - return hist_sync_events_; - } + std::vector<::sycl::event> GetEvents() const { return hist_sync_events_; } private: std::vector<::sycl::event> hist_sync_events_; }; template -class DistributedHistSynchronizer: public HistSynchronizer { +class DistributedHistSynchronizer : public HistSynchronizer { public: void SyncHistograms(HistUpdater* builder, const std::vector& sync_ids, RegTree const* p_tree) override { @@ -81,8 +79,8 @@ class DistributedHistSynchronizer: public HistSynchronizer { auto& parent_hist = builder->hist_local_worker_[parent_id]; auto& sibling_hist = builder->hist_[sibling_nid]; - common::SubtractionHist(builder->qu_, &sibling_hist, parent_hist, - this_hist, nbins, ::sycl::event()); + common::SubtractionHist(builder->qu_, &sibling_hist, parent_hist, this_hist, nbins, + ::sycl::event()); builder->qu_->wait_and_throw(); // Store posible parent node auto& sibling_local = builder->hist_local_worker_[sibling_nid]; @@ -98,8 +96,7 @@ class DistributedHistSynchronizer: public HistSynchronizer { } void ParallelSubtractionHist(HistUpdater* builder, - const std::vector& nodes, - const RegTree * p_tree) { + const std::vector& nodes, const RegTree* p_tree) { const size_t nbins = builder->hist_builder_.GetNumBins(); auto tree = p_tree->HostScView(); for (int node = 0; node < nodes.size(); node++) { @@ -111,8 +108,8 @@ class DistributedHistSynchronizer: public HistSynchronizer { const size_t parent_id = tree.Parent(entry.nid); auto& parent_hist = builder->hist_[parent_id]; auto& sibling_hist = builder->hist_[entry.GetSiblingId(tree, parent_id)]; - common::SubtractionHist(builder->qu_, &this_hist, parent_hist, - sibling_hist, nbins, ::sycl::event()); + common::SubtractionHist(builder->qu_, &this_hist, parent_hist, sibling_hist, nbins, + ::sycl::event()); builder->qu_->wait_and_throw(); } } diff --git a/plugin/sycl/tree/hist_updater.cc b/plugin/sycl/tree/hist_updater.cc index f8f0e5f74001..1a929e9eb852 100644 --- a/plugin/sycl/tree/hist_updater.cc +++ b/plugin/sycl/tree/hist_updater.cc @@ -5,34 +5,33 @@ #include "hist_updater.h" -#include - #include +#include +#include +#include +#include "../../src/collective/allreduce.h" #include "../../src/tree/common_row_partitioner.h" - #include "../common/hist_util.h" #include "xgboost/linalg.h" -#include "../../src/collective/allreduce.h" namespace xgboost { namespace sycl { namespace tree { -using ::sycl::ext::oneapi::plus; -using ::sycl::ext::oneapi::minimum; using ::sycl::ext::oneapi::maximum; +using ::sycl::ext::oneapi::minimum; +using ::sycl::ext::oneapi::plus; template -void HistUpdater::ReduceHists(const std::vector& sync_ids, - size_t nbins) { +void HistUpdater::ReduceHists(const std::vector& sync_ids, size_t nbins) { if (reduce_buffer_.size() < sync_ids.size() * nbins) { reduce_buffer_.resize(sync_ids.size() * nbins); } for (size_t i = 0; i < sync_ids.size(); i++) { auto& this_hist = hist_[sync_ids[i]]; const GradientPairT* psrc = reinterpret_cast(this_hist.DataConst()); - qu_->memcpy(reduce_buffer_.data() + i * nbins, psrc, nbins*sizeof(GradientPairT)).wait(); + qu_->memcpy(reduce_buffer_.data() + i * nbins, psrc, nbins * sizeof(GradientPairT)).wait(); } auto buffer_vec = ::xgboost::linalg::MakeVec( @@ -43,27 +42,23 @@ void HistUpdater::ReduceHists(const std::vector& sync_ids, for (size_t i = 0; i < sync_ids.size(); i++) { auto& this_hist = hist_[sync_ids[i]]; GradientPairT* psrc = reinterpret_cast(this_hist.Data()); - qu_->memcpy(psrc, reduce_buffer_.data() + i * nbins, nbins*sizeof(GradientPairT)).wait(); + qu_->memcpy(psrc, reduce_buffer_.data() + i * nbins, nbins * sizeof(GradientPairT)).wait(); } } template -void HistUpdater::SetHistSynchronizer( - HistSynchronizer *sync) { +void HistUpdater::SetHistSynchronizer(HistSynchronizer* sync) { hist_synchronizer_.reset(sync); } template -void HistUpdater::SetHistRowsAdder( - HistRowsAdder *adder) { +void HistUpdater::SetHistRowsAdder(HistRowsAdder* adder) { hist_rows_adder_.reset(adder); } template void HistUpdater::BuildHistogramsLossGuide( - ExpandEntry entry, - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, + ExpandEntry entry, const common::GHistIndexMatrix& gmat, RegTree* p_tree, const HostDeviceVector& gpair) { nodes_for_explicit_hist_build_.clear(); nodes_for_subtraction_trick_.clear(); @@ -82,11 +77,10 @@ void HistUpdater::BuildHistogramsLossGuide( hist_synchronizer_->SyncHistograms(this, sync_ids, p_tree); } -template -void HistUpdater::BuildLocalHistograms( - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - const HostDeviceVector& gpair) { +template +void HistUpdater::BuildLocalHistograms(const common::GHistIndexMatrix& gmat, + RegTree* p_tree, + const HostDeviceVector& gpair) { builder_monitor_.Start("BuildLocalHistograms"); const size_t n_nodes = nodes_for_explicit_hist_build_.size(); ::sycl::event event; @@ -105,11 +99,10 @@ void HistUpdater::BuildLocalHistograms( builder_monitor_.Stop("BuildLocalHistograms"); } -template -void HistUpdater::BuildNodeStats( - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - const HostDeviceVector& gpair) { +template +void HistUpdater::BuildNodeStats(const common::GHistIndexMatrix& gmat, + RegTree* p_tree, + const HostDeviceVector& gpair) { builder_monitor_.Start("BuildNodeStats"); for (auto const& entry : qexpand_depth_wise_) { int nid = entry.nid; @@ -120,24 +113,19 @@ void HistUpdater::BuildNodeStats( auto parent_id = (*p_tree)[nid].Parent(); auto left_sibling_id = (*p_tree)[parent_id].LeftChild(); auto parent_split_feature_id = snode_host_[parent_id].best.SplitIndex(); - tree_evaluator_.AddSplit( - parent_id, left_sibling_id, nid, parent_split_feature_id, - snode_host_[left_sibling_id].weight, snode_host_[nid].weight); - interaction_constraints_.Split(parent_id, parent_split_feature_id, - left_sibling_id, nid); + tree_evaluator_.AddSplit(parent_id, left_sibling_id, nid, parent_split_feature_id, + snode_host_[left_sibling_id].weight, snode_host_[nid].weight); + interaction_constraints_.Split(parent_id, parent_split_feature_id, left_sibling_id, nid); } } builder_monitor_.Stop("BuildNodeStats"); } -template -void HistUpdater::AddSplitsToTree( - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - int *num_leaves, - int depth, - std::vector* nodes_for_apply_split, - std::vector* temp_qexpand_depth) { +template +void HistUpdater::AddSplitsToTree(const common::GHistIndexMatrix& gmat, + RegTree* p_tree, int* num_leaves, int depth, + std::vector* nodes_for_apply_split, + std::vector* temp_qexpand_depth) { builder_monitor_.Start("AddSplitsToTree"); auto evaluator = tree_evaluator_.GetEvaluator(); for (auto const& entry : qexpand_depth_wise_) { @@ -156,14 +144,13 @@ void HistUpdater::AddSplitsToTree( evaluator.CalcWeight(nid, GradStats{e.best.left_sum}) * lr; bst_float right_leaf_weight = evaluator.CalcWeight(nid, GradStats{e.best.right_sum}) * lr; - p_tree->ExpandNode(nid, e.best.SplitIndex(), e.best.split_value, - e.best.DefaultLeft(), e.weight, left_leaf_weight, - right_leaf_weight, e.best.loss_chg, e.stats.GetHess(), - e.best.left_sum.GetHess(), e.best.right_sum.GetHess()); + p_tree->ExpandNode(nid, e.best.SplitIndex(), e.best.split_value, e.best.DefaultLeft(), + e.weight, left_leaf_weight, right_leaf_weight, e.best.loss_chg, + e.stats.GetHess(), e.best.left_sum.GetHess(), e.best.right_sum.GetHess()); int left_id = (*p_tree)[nid].LeftChild(); int right_id = (*p_tree)[nid].RightChild(); - temp_qexpand_depth->push_back(ExpandEntry(left_id, p_tree->GetDepth(left_id))); + temp_qexpand_depth->push_back(ExpandEntry(left_id, p_tree->GetDepth(left_id))); temp_qexpand_depth->push_back(ExpandEntry(right_id, p_tree->GetDepth(right_id))); // - 1 parent + 2 new children (*num_leaves)++; @@ -172,19 +159,14 @@ void HistUpdater::AddSplitsToTree( builder_monitor_.Stop("AddSplitsToTree"); } - -template +template void HistUpdater::EvaluateAndApplySplits( - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - int *num_leaves, - int depth, - std::vector *temp_qexpand_depth) { + const common::GHistIndexMatrix& gmat, RegTree* p_tree, int* num_leaves, int depth, + std::vector* temp_qexpand_depth) { EvaluateSplits(qexpand_depth_wise_, gmat, *p_tree); std::vector nodes_for_apply_split; - AddSplitsToTree(gmat, p_tree, num_leaves, depth, - &nodes_for_apply_split, temp_qexpand_depth); + AddSplitsToTree(gmat, p_tree, num_leaves, depth, &nodes_for_apply_split, temp_qexpand_depth); ApplySplit(nodes_for_apply_split, gmat, p_tree); } @@ -195,26 +177,25 @@ void HistUpdater::EvaluateAndApplySplits( // and use 'Subtraction Trick' to built the histogram for the right child node. // This ensures that the workers operate on the same set of tree nodes. template -void HistUpdater::SplitSiblings( - const std::vector &nodes, - std::vector *small_siblings, - std::vector *big_siblings, - RegTree *p_tree) { +void HistUpdater::SplitSiblings(const std::vector& nodes, + std::vector* small_siblings, + std::vector* big_siblings, + RegTree* p_tree) { builder_monitor_.Start("SplitSiblings"); for (auto const& entry : nodes) { int nid = entry.nid; - RegTree::Node &node = (*p_tree)[nid]; + RegTree::Node& node = (*p_tree)[nid]; if (node.IsRoot()) { small_siblings->push_back(entry); } else { const int32_t left_id = (*p_tree)[node.Parent()].LeftChild(); const int32_t right_id = (*p_tree)[node.Parent()].RightChild(); - if (nid == left_id && row_set_collection_[left_id ].Size() < - row_set_collection_[right_id].Size()) { + if (nid == left_id && + row_set_collection_[left_id].Size() < row_set_collection_[right_id].Size()) { small_siblings->push_back(entry); - } else if (nid == right_id && row_set_collection_[right_id].Size() <= - row_set_collection_[left_id ].Size()) { + } else if (nid == right_id && + row_set_collection_[right_id].Size() <= row_set_collection_[left_id].Size()) { small_siblings->push_back(entry); } else { big_siblings->push_back(entry); @@ -224,16 +205,14 @@ void HistUpdater::SplitSiblings( builder_monitor_.Stop("SplitSiblings"); } -template -void HistUpdater::ExpandWithDepthWise( - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - const HostDeviceVector& gpair) { +template +void HistUpdater::ExpandWithDepthWise(const common::GHistIndexMatrix& gmat, + RegTree* p_tree, + const HostDeviceVector& gpair) { int num_leaves = 0; // in depth_wise growing, we feed loss_chg with 0.0 since it is not used anyway - qexpand_depth_wise_.emplace_back(ExpandEntry::kRootNid, - p_tree->GetDepth(ExpandEntry::kRootNid)); + qexpand_depth_wise_.emplace_back(ExpandEntry::kRootNid, p_tree->GetDepth(ExpandEntry::kRootNid)); ++num_leaves; for (int depth = 0; depth < param_.max_depth + 1; depth++) { std::vector sync_ids; @@ -245,8 +224,7 @@ void HistUpdater::ExpandWithDepthWise( hist_synchronizer_->SyncHistograms(this, sync_ids, p_tree); BuildNodeStats(gmat, p_tree, gpair); - EvaluateAndApplySplits(gmat, p_tree, &num_leaves, depth, - &temp_qexpand_depth); + EvaluateAndApplySplits(gmat, p_tree, &num_leaves, depth, &temp_qexpand_depth); // clean up qexpand_depth_wise_.clear(); @@ -261,11 +239,10 @@ void HistUpdater::ExpandWithDepthWise( } } -template -void HistUpdater::ExpandWithLossGuide( - const common::GHistIndexMatrix& gmat, - RegTree* p_tree, - const HostDeviceVector& gpair) { +template +void HistUpdater::ExpandWithLossGuide(const common::GHistIndexMatrix& gmat, + RegTree* p_tree, + const HostDeviceVector& gpair) { builder_monitor_.Start("ExpandWithLossGuide"); int num_leaves = 0; const auto lr = param_.learning_rate; @@ -294,10 +271,9 @@ void HistUpdater::ExpandWithLossGuide( evaluator.CalcWeight(nid, GradStats{e.best.left_sum}) * lr; bst_float right_leaf_weight = evaluator.CalcWeight(nid, GradStats{e.best.right_sum}) * lr; - p_tree->ExpandNode(nid, e.best.SplitIndex(), e.best.split_value, - e.best.DefaultLeft(), e.weight, left_leaf_weight, - right_leaf_weight, e.best.loss_chg, e.stats.GetHess(), - e.best.left_sum.GetHess(), e.best.right_sum.GetHess()); + p_tree->ExpandNode(nid, e.best.SplitIndex(), e.best.split_value, e.best.DefaultLeft(), + e.weight, left_leaf_weight, right_leaf_weight, e.best.loss_chg, + e.stats.GetHess(), e.best.left_sum.GetHess(), e.best.right_sum.GetHess()); this->ApplySplit({candidate}, gmat, p_tree); @@ -316,8 +292,8 @@ void HistUpdater::ExpandWithLossGuide( this->InitNewNode(cleft, gmat, gpair, *p_tree); this->InitNewNode(cright, gmat, gpair, *p_tree); bst_uint featureid = snode_host_[nid].best.SplitIndex(); - tree_evaluator_.AddSplit(nid, cleft, cright, featureid, - snode_host_[cleft].weight, snode_host_[cright].weight); + tree_evaluator_.AddSplit(nid, cleft, cright, featureid, snode_host_[cleft].weight, + snode_host_[cright].weight); interaction_constraints_.Split(nid, featureid, cleft, cright); this->EvaluateSplits({left_node, right_node}, gmat, *p_tree); @@ -335,12 +311,9 @@ void HistUpdater::ExpandWithLossGuide( template void HistUpdater::Update( - xgboost::tree::TrainParam const *param, - const common::GHistIndexMatrix &gmat, - const HostDeviceVector& gpair, - DMatrix *p_fmat, - xgboost::common::Span> out_position, - RegTree *p_tree) { + xgboost::tree::TrainParam const* param, const common::GHistIndexMatrix& gmat, + const HostDeviceVector& gpair, DMatrix* p_fmat, + xgboost::common::Span> out_position, RegTree* p_tree) { builder_monitor_.Start("Update"); tree_evaluator_.Reset(qu_, param_, p_fmat->Info().num_col_); @@ -408,10 +381,10 @@ bool HistUpdater::UpdatePredictionCache( return true; } -template +template void HistUpdater::InitSampling( - const HostDeviceVector& gpair, - USMVector* row_indices) { + const HostDeviceVector& gpair, + USMVector* row_indices) { const size_t num_rows = row_indices->Size(); auto* row_idx = row_indices->Data(); const auto* gpair_ptr = gpair.ConstDevicePointer(); @@ -424,17 +397,16 @@ void HistUpdater::InitSampling( uint64_t seed = seed_; seed_ += num_rows; - /* - * oneDLP bernoulli_distribution implicitly uses double. - * In this case the device doesn't have fp64 support, - * we generate bernoulli distributed random values from uniform distribution - */ + /* + * oneDLP bernoulli_distribution implicitly uses double. + * In this case the device doesn't have fp64 support, + * we generate bernoulli distributed random values from uniform distribution + */ if (has_fp64_support_) { // Use oneDPL bernoulli_distribution for better perf event = qu_->submit([&](::sycl::handler& cgh) { - auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); - cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(num_rows)), - [=](::sycl::item<1> pid) { + auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); + cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(num_rows)), [=](::sycl::item<1> pid) { uint64_t i = pid.get_id(0); // Create minstd_rand engine oneapi::dpl::minstd_rand engine(seed, i); @@ -450,9 +422,8 @@ void HistUpdater::InitSampling( } else { // Use oneDPL uniform, as far as bernoulli_distribution uses fp64 event = qu_->submit([&](::sycl::handler& cgh) { - auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); - cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(num_rows)), - [=](::sycl::item<1> pid) { + auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); + cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(num_rows)), [=](::sycl::item<1> pid) { uint64_t i = pid.get_id(0); oneapi::dpl::minstd_rand engine(seed, i); oneapi::dpl::uniform_real_distribution distr; @@ -473,12 +444,10 @@ void HistUpdater::InitSampling( qu_->wait(); } -template -void HistUpdater::InitData( - const common::GHistIndexMatrix& gmat, - const HostDeviceVector& gpair, - const DMatrix& fmat, - const RegTree& tree) { +template +void HistUpdater::InitData(const common::GHistIndexMatrix& gmat, + const HostDeviceVector& gpair, + const DMatrix& fmat, const RegTree& tree) { CHECK((param_.max_depth > 0 || param_.max_leaves > 0)) << "max_depth or max_leaves cannot be both 0 (unlimited); " << "at least one should be a positive quantity."; @@ -511,8 +480,8 @@ void HistUpdater::InitData( // mark subsample and build list of member rows if (param_.subsample < 1.0f) { CHECK_EQ(param_.sampling_method, xgboost::tree::TrainParam::kUniform) - << "Only uniform sampling is supported, " - << "gradient-based sampling is only support by GPU Hist."; + << "Only uniform sampling is supported, " + << "gradient-based sampling is only support by GPU Hist."; InitSampling(gpair, row_indices); } else { int has_neg_hess = 0; @@ -521,16 +490,16 @@ void HistUpdater::InitData( { ::sycl::buffer flag_buf(&has_neg_hess, 1); event = qu_->submit([&](::sycl::handler& cgh) { - auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); + auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(info.num_row_)), - [=](::sycl::item<1> pid) { - const size_t idx = pid.get_id(0); - p_row_indices[idx] = idx; - if (gpair_ptr[idx].GetHess() < 0.0f) { - AtomicRef has_neg_hess_ref(flag_buf_acc[0]); - has_neg_hess_ref.fetch_max(1); - } - }); + [=](::sycl::item<1> pid) { + const size_t idx = pid.get_id(0); + p_row_indices[idx] = idx; + if (gpair_ptr[idx].GetHess() < 0.0f) { + AtomicRef has_neg_hess_ref(flag_buf_acc[0]); + has_neg_hess_ref.fetch_max(1); + } + }); }); } @@ -540,15 +509,15 @@ void HistUpdater::InitData( ::sycl::buffer flag_buf(&max_idx, 1); event = qu_->submit([&](::sycl::handler& cgh) { cgh.depends_on(event); - auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); + auto flag_buf_acc = flag_buf.get_access<::sycl::access::mode::read_write>(cgh); cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(info.num_row_)), - [=](::sycl::item<1> pid) { - const size_t idx = pid.get_id(0); - if (gpair_ptr[idx].GetHess() >= 0.0f) { - AtomicRef max_idx_ref(flag_buf_acc[0]); - p_row_indices[max_idx_ref++] = idx; - } - }); + [=](::sycl::item<1> pid) { + const size_t idx = pid.get_id(0); + if (gpair_ptr[idx].GetHess() >= 0.0f) { + AtomicRef max_idx_ref(flag_buf_acc[0]); + p_row_indices[max_idx_ref++] = idx; + } + }); }); } row_indices->Resize(qu_, max_idx, 0, &event); @@ -579,9 +548,8 @@ void HistUpdater::InitData( // store a pointer to the tree p_last_tree_ = &tree; - column_sampler_->Init(ctx_, info.num_col_, info.feature_weights, - param_.colsample_bynode, param_.colsample_bylevel, - param_.colsample_bytree); + column_sampler_->Init(ctx_, info.num_col_, info.feature_weights, param_.colsample_bynode, + param_.colsample_bylevel, param_.colsample_bytree); if (data_layout_ == kDenseDataZeroBased || data_layout_ == kDenseDataOneBased) { /* specialized code for dense data: choose the column that has a least positive number of discrete bins. @@ -602,7 +570,7 @@ void HistUpdater::InitData( CHECK_GT(min_nbins_per_feature, 0U); } - std::fill(snode_host_.begin(), snode_host_.end(), NodeEntry(param_)); + std::fill(snode_host_.begin(), snode_host_.end(), NodeEntry(param_)); { if (param_.grow_policy == xgboost::tree::TrainParam::kLossGuide) { @@ -617,9 +585,8 @@ void HistUpdater::InitData( hist_buffer_.Init(qu_, nbins); bool isDense = data_layout_ != kSparseData; const size_t ncolumns = isDense ? gmat.nfeatures : gmat.row_stride; - size_t buffer_size = GetRequiredBufferSize - (device_properties_, info.num_row_, nbins, ncolumns, - gmat.max_num_bins, gmat.min_num_bins); + size_t buffer_size = GetRequiredBufferSize( + device_properties_, info.num_row_, nbins, ncolumns, gmat.max_num_bins, gmat.min_num_bins); hist_buffer_.Reset(buffer_size); } @@ -627,25 +594,22 @@ void HistUpdater::InitData( } template -void HistUpdater::AddSplitsToRowSet( - const std::vector& nodes, - RegTree* p_tree) { +void HistUpdater::AddSplitsToRowSet(const std::vector& nodes, + RegTree* p_tree) { const size_t n_nodes = nodes.size(); for (size_t i = 0; i < n_nodes; ++i) { const int32_t nid = nodes[i].nid; const size_t n_left = partition_builder_.GetNLeftElems(i); const size_t n_right = partition_builder_.GetNRightElems(i); - row_set_collection_.AddSplit(nid, (*p_tree)[nid].LeftChild(), - (*p_tree)[nid].RightChild(), n_left, n_right); + row_set_collection_.AddSplit(nid, (*p_tree)[nid].LeftChild(), (*p_tree)[nid].RightChild(), + n_left, n_right); } } template -void HistUpdater::ApplySplit( - const std::vector nodes, - const common::GHistIndexMatrix& gmat, - RegTree* p_tree) { +void HistUpdater::ApplySplit(const std::vector nodes, + const common::GHistIndexMatrix& gmat, RegTree* p_tree) { using CommonRowPartitioner = xgboost::tree::CommonRowPartitioner; builder_monitor_.Start("ApplySplit"); @@ -660,8 +624,7 @@ void HistUpdater::ApplySplit( }); ::sycl::event event; - partition_builder_.Partition(gmat, nodes, row_set_collection_, - split_conditions, p_tree, &event); + partition_builder_.Partition(gmat, nodes, row_set_collection_, split_conditions, p_tree, &event); qu_->wait_and_throw(); for (size_t node_in_set = 0; node_in_set < n_nodes; node_in_set++) { @@ -677,8 +640,7 @@ void HistUpdater::ApplySplit( } template -void HistUpdater::InitNewNode(int nid, - const common::GHistIndexMatrix& gmat, +void HistUpdater::InitNewNode(int nid, const common::GHistIndexMatrix& gmat, const HostDeviceVector& gpair, const RegTree& tree) { builder_monitor_.Start("InitNewNode"); @@ -695,8 +657,8 @@ void HistUpdater::InitNewNode(int nid, const auto* hist = reinterpret_cast*>(hist_[nid].Data()); std::vector> ets(iend - ibegin); - qu_->memcpy(ets.data(), hist + ibegin, - (iend - ibegin) * sizeof(GradStats)).wait_and_throw(); + qu_->memcpy(ets.data(), hist + ibegin, (iend - ibegin) * sizeof(GradStats)) + .wait_and_throw(); for (const auto& et : ets) { grad_stat += et; } @@ -708,19 +670,20 @@ void HistUpdater::InitNewNode(int nid, ::sycl::buffer> buff(&grad_stat, 1); qu_->submit([&](::sycl::handler& cgh) { - auto reduction = ::sycl::reduction(buff, cgh, ::sycl::plus<>()); - cgh.parallel_for<>(::sycl::range<1>(size), reduction, - [=](::sycl::item<1> pid, auto& sum) { - size_t i = pid.get_id(0); - size_t row_idx = row_idxs[i]; - if constexpr (std::is_same::value) { - sum += gpair_ptr[row_idx]; - } else { - sum += GradStats(gpair_ptr[row_idx].GetGrad(), - gpair_ptr[row_idx].GetHess()); - } - }); - }).wait_and_throw(); + auto reduction = ::sycl::reduction(buff, cgh, ::sycl::plus<>()); + cgh.parallel_for<>( + ::sycl::range<1>(size), reduction, [=](::sycl::item<1> pid, auto& sum) { + size_t i = pid.get_id(0); + size_t row_idx = row_idxs[i]; + if constexpr (std::is_same::value) { + sum += gpair_ptr[row_idx]; + } else { + sum += GradStats(gpair_ptr[row_idx].GetGrad(), + gpair_ptr[row_idx].GetHess()); + } + }); + }) + .wait_and_throw(); } auto rc = collective::Allreduce( ctx_, ::xgboost::linalg::MakeVec(reinterpret_cast(&grad_stat), 2), @@ -748,11 +711,10 @@ void HistUpdater::InitNewNode(int nid, } // nodes_set - set of nodes to be processed in parallel -template -void HistUpdater::EvaluateSplits( - const std::vector& nodes_set, - const common::GHistIndexMatrix& gmat, - const RegTree& tree) { +template +void HistUpdater::EvaluateSplits(const std::vector& nodes_set, + const common::GHistIndexMatrix& gmat, + const RegTree& tree) { builder_monitor_.Start("EvaluateSplits"); const size_t n_nodes_in_set = nodes_set.size(); @@ -781,7 +743,7 @@ void HistUpdater::EvaluateSplits( split_queries_device_.Resize(qu_, total_features); auto event = qu_->memcpy(split_queries_device_.Data(), split_queries_host_.data(), - total_features * sizeof(SplitQuery)); + total_features * sizeof(SplitQuery)); auto evaluator = tree_evaluator_.GetEvaluator(); SplitQuery* split_queries_device = split_queries_device_.Data(); @@ -790,7 +752,7 @@ void HistUpdater::EvaluateSplits( snode_device_.ResizeNoCopy(qu_, snode_host_.size()); event = qu_->memcpy(snode_device_.Data(), snode_host_.data(), - snode_host_.size() * sizeof(NodeEntry), event); + snode_host_.size() * sizeof(NodeEntry), event); const NodeEntry* snode = snode_device_.Data(); const float min_child_weight = param_.min_child_weight; @@ -804,19 +766,19 @@ void HistUpdater::EvaluateSplits( cgh.parallel_for<>(::sycl::nd_range<2>(::sycl::range<2>(total_features, sub_group_size_), ::sycl::range<2>(1, sub_group_size_)), [=](::sycl::nd_item<2> pid) { - int i = pid.get_global_id(0); - auto sg = pid.get_sub_group(); - int nid = split_queries_device[i].nid; - int fid = split_queries_device[i].fid; - const GradientPairT* hist_data = split_queries_device[i].hist; - - best_splits[i] = snode[nid].best; - EnumerateSplit(sg, cut_ptr, cut_val, hist_data, snode[nid], - &(best_splits[i]), fid, nid, evaluator, min_child_weight); - }); + int i = pid.get_global_id(0); + auto sg = pid.get_sub_group(); + int nid = split_queries_device[i].nid; + int fid = split_queries_device[i].fid; + const GradientPairT* hist_data = split_queries_device[i].hist; + + best_splits[i] = snode[nid].best; + EnumerateSplit(sg, cut_ptr, cut_val, hist_data, snode[nid], + &(best_splits[i]), fid, nid, evaluator, min_child_weight); + }); }); event = qu_->memcpy(best_splits_host_.data(), best_splits, - total_features * sizeof(SplitEntry), event); + total_features * sizeof(SplitEntry), event); qu_->wait(); for (size_t i = 0; i < total_features; i++) { @@ -832,16 +794,10 @@ void HistUpdater::EvaluateSplits( // for the particular feature fid. template void HistUpdater::EnumerateSplit( - const ::sycl::sub_group& sg, - const uint32_t* cut_ptr, - const bst_float* cut_val, - const GradientPairT* hist_data, - const NodeEntry& snode, - SplitEntry* p_best, - bst_uint fid, - bst_uint nodeID, - typename TreeEvaluator::SplitEvaluator const &evaluator, - float min_child_weight) { + const ::sycl::sub_group& sg, const uint32_t* cut_ptr, const bst_float* cut_val, + const GradientPairT* hist_data, const NodeEntry& snode, + SplitEntry* p_best, bst_uint fid, bst_uint nodeID, + typename TreeEvaluator::SplitEvaluator const& evaluator, float min_child_weight) { SplitEntry best; int32_t ibegin = static_cast(cut_ptr[fid]); @@ -880,12 +836,10 @@ void HistUpdater::EnumerateSplit( } bst_float total_loss_chg = ::sycl::reduce_over_group(sg, best.loss_chg, maximum<>()); - bst_feature_t total_split_index = ::sycl::reduce_over_group(sg, - best.loss_chg == total_loss_chg ? - best.SplitIndex() : - (1U << 31) - 1U, minimum<>()); - if (best.loss_chg == total_loss_chg && - best.SplitIndex() == total_split_index) p_best->Update(best); + bst_feature_t total_split_index = ::sycl::reduce_over_group( + sg, best.loss_chg == total_loss_chg ? best.SplitIndex() : (1U << 31) - 1U, minimum<>()); + if (best.loss_chg == total_loss_chg && best.SplitIndex() == total_split_index) + p_best->Update(best); } template class HistUpdater; diff --git a/plugin/sycl/tree/hist_updater.h b/plugin/sycl/tree/hist_updater.h index 37ff6a8b3e9b..52d7576d1143 100644 --- a/plugin/sycl/tree/hist_updater.h +++ b/plugin/sycl/tree/hist_updater.h @@ -12,26 +12,25 @@ #include #pragma GCC diagnostic pop -#include #include #include #include - -#include "../common/partition_builder.h" -#include "split_evaluator.h" -#include "hist_synchronizer.h" -#include "hist_row_adder.h" -#include "hist_dispatcher.h" +#include #include "../../src/common/random.h" +#include "../common/partition_builder.h" #include "../data.h" +#include "hist_dispatcher.h" +#include "hist_row_adder.h" +#include "hist_synchronizer.h" +#include "split_evaluator.h" namespace xgboost { namespace sycl { namespace tree { // data structure -template +template struct NodeEntry { /*! \brief statics for node entry */ GradStats stats; @@ -42,26 +41,27 @@ struct NodeEntry { /*! \brief current best solution */ SplitEntry best; // constructor - explicit NodeEntry(const xgboost::tree::TrainParam& param) - : root_gain(0.0f), weight(0.0f) {} + explicit NodeEntry(const xgboost::tree::TrainParam& param) : root_gain(0.0f), weight(0.0f) {} }; -template +template class HistUpdater { public: template using GHistRowT = common::GHistRow; using GradientPairT = xgboost::detail::GradientPairInternal; - explicit HistUpdater(const Context* ctx, - ::sycl::queue* qu, + explicit HistUpdater(const Context* ctx, ::sycl::queue* qu, const xgboost::tree::TrainParam& param, - FeatureInteractionConstraintHost int_constraints_, - DMatrix const* fmat) - : ctx_(ctx), qu_(qu), device_properties_(qu->get_device()), param_(param), - tree_evaluator_(qu, param, fmat->Info().num_col_), - interaction_constraints_{std::move(int_constraints_)}, - p_last_tree_(nullptr), p_last_fmat_(fmat) { + FeatureInteractionConstraintHost int_constraints_, DMatrix const* fmat) + : ctx_(ctx), + qu_(qu), + device_properties_(qu->get_device()), + param_(param), + tree_evaluator_(qu, param, fmat->Info().num_col_), + interaction_constraints_{std::move(int_constraints_)}, + p_last_tree_(nullptr), + p_last_fmat_(fmat) { builder_monitor_.Init("SYCL::Quantile::HistUpdater"); kernel_monitor_.Init("SYCL::Quantile::HistUpdater"); if (param.max_depth > 0) { @@ -69,17 +69,14 @@ class HistUpdater { } has_fp64_support_ = qu_->get_device().has(::sycl::aspect::fp64); const auto sub_group_sizes = - qu_->get_device().get_info<::sycl::info::device::sub_group_sizes>(); + qu_->get_device().get_info<::sycl::info::device::sub_group_sizes>(); sub_group_size_ = sub_group_sizes.back(); } // update one tree, growing - void Update(xgboost::tree::TrainParam const *param, - const common::GHistIndexMatrix &gmat, - const HostDeviceVector& gpair, - DMatrix *p_fmat, - xgboost::common::Span> out_position, - RegTree *p_tree); + void Update(xgboost::tree::TrainParam const* param, const common::GHistIndexMatrix& gmat, + const HostDeviceVector& gpair, DMatrix* p_fmat, + xgboost::common::Span> out_position, RegTree* p_tree); bool UpdatePredictionCache(const DMatrix* data, ::xgboost::linalg::MatrixView p_out_preds); @@ -103,89 +100,67 @@ class HistUpdater { USMVector* row_indices); void EvaluateSplits(const std::vector& nodes_set, - const common::GHistIndexMatrix& gmat, - const RegTree& tree); + const common::GHistIndexMatrix& gmat, const RegTree& tree); // Enumerate the split values of specific feature // Returns the sum of gradients corresponding to the data points that contains a non-missing // value for the particular feature fid. - static void EnumerateSplit(const ::sycl::sub_group& sg, - const uint32_t* cut_ptr, const bst_float* cut_val, const GradientPairT* hist_data, - const NodeEntry &snode, SplitEntry* p_best, bst_uint fid, - bst_uint nodeID, - typename TreeEvaluator::SplitEvaluator const &evaluator, - float min_child_weight); + static void EnumerateSplit(const ::sycl::sub_group& sg, const uint32_t* cut_ptr, + const bst_float* cut_val, const GradientPairT* hist_data, + const NodeEntry& snode, SplitEntry* p_best, + bst_uint fid, bst_uint nodeID, + typename TreeEvaluator::SplitEvaluator const& evaluator, + float min_child_weight); - void ApplySplit(std::vector nodes, - const common::GHistIndexMatrix& gmat, - RegTree* p_tree); + void ApplySplit(std::vector nodes, const common::GHistIndexMatrix& gmat, + RegTree* p_tree); void AddSplitsToRowSet(const std::vector& nodes, RegTree* p_tree); - void InitData(const common::GHistIndexMatrix& gmat, - const HostDeviceVector& gpair, - const DMatrix& fmat, - const RegTree& tree); - - inline ::sycl::event BuildHist( - const HostDeviceVector& gpair, - const common::RowSetCollection::Elem row_indices, - const common::GHistIndexMatrix& gmat, - GHistRowT* hist, - GHistRowT* hist_buffer, - ::sycl::event event_priv) { - return hist_builder_.BuildHist(gpair, row_indices, gmat, hist, - data_layout_ != kSparseData, hist_buffer, - device_properties_, event_priv); + void InitData(const common::GHistIndexMatrix& gmat, const HostDeviceVector& gpair, + const DMatrix& fmat, const RegTree& tree); + + inline ::sycl::event BuildHist(const HostDeviceVector& gpair, + const common::RowSetCollection::Elem row_indices, + const common::GHistIndexMatrix& gmat, + GHistRowT* hist, + GHistRowT* hist_buffer, + ::sycl::event event_priv) { + return hist_builder_.BuildHist(gpair, row_indices, gmat, hist, data_layout_ != kSparseData, + hist_buffer, device_properties_, event_priv); } - void InitNewNode(int nid, - const common::GHistIndexMatrix& gmat, - const HostDeviceVector& gpair, - const RegTree& tree); + void InitNewNode(int nid, const common::GHistIndexMatrix& gmat, + const HostDeviceVector& gpair, const RegTree& tree); // Split nodes to 2 sets depending on amount of rows in each node // Histograms for small nodes will be built explicitly // Histograms for big nodes will be built by 'Subtraction Trick' void SplitSiblings(const std::vector& nodes, - std::vector* small_siblings, - std::vector* big_siblings, - RegTree *p_tree); + std::vector* small_siblings, + std::vector* big_siblings, RegTree* p_tree); - void BuildNodeStats(const common::GHistIndexMatrix &gmat, - RegTree *p_tree, + void BuildNodeStats(const common::GHistIndexMatrix& gmat, RegTree* p_tree, const HostDeviceVector& gpair); - void EvaluateAndApplySplits(const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - int *num_leaves, - int depth, - std::vector *temp_qexpand_depth); - - void AddSplitsToTree( - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - int *num_leaves, - int depth, - std::vector* nodes_for_apply_split, - std::vector* temp_qexpand_depth); - - void ExpandWithDepthWise(const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - const HostDeviceVector& gpair); + void EvaluateAndApplySplits(const common::GHistIndexMatrix& gmat, RegTree* p_tree, + int* num_leaves, int depth, + std::vector* temp_qexpand_depth); - void BuildLocalHistograms(const common::GHistIndexMatrix &gmat, - RegTree *p_tree, + void AddSplitsToTree(const common::GHistIndexMatrix& gmat, RegTree* p_tree, int* num_leaves, + int depth, std::vector* nodes_for_apply_split, + std::vector* temp_qexpand_depth); + + void ExpandWithDepthWise(const common::GHistIndexMatrix& gmat, RegTree* p_tree, + const HostDeviceVector& gpair); + + void BuildLocalHistograms(const common::GHistIndexMatrix& gmat, RegTree* p_tree, const HostDeviceVector& gpair); - void BuildHistogramsLossGuide( - ExpandEntry entry, - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - const HostDeviceVector& gpair); + void BuildHistogramsLossGuide(ExpandEntry entry, const common::GHistIndexMatrix& gmat, + RegTree* p_tree, const HostDeviceVector& gpair); - void ExpandWithLossGuide(const common::GHistIndexMatrix& gmat, - RegTree* p_tree, + void ExpandWithLossGuide(const common::GHistIndexMatrix& gmat, RegTree* p_tree, const HostDeviceVector& gpair); void ReduceHists(const std::vector& sync_ids, size_t nbins); @@ -225,9 +200,8 @@ class HistUpdater { const RegTree* p_last_tree_; DMatrix const* const p_last_fmat_; - using ExpandQueue = - std::priority_queue, - std::function>; + using ExpandQueue = std::priority_queue, + std::function>; std::unique_ptr qexpand_loss_guided_; std::vector qexpand_depth_wise_; diff --git a/plugin/sycl/tree/param.h b/plugin/sycl/tree/param.h index 3c30c4e5c822..163a71c7b559 100644 --- a/plugin/sycl/tree/param.h +++ b/plugin/sycl/tree/param.h @@ -4,16 +4,14 @@ #ifndef PLUGIN_SYCL_TREE_PARAM_H_ #define PLUGIN_SYCL_TREE_PARAM_H_ - #include #include #include #include #include - -#include "xgboost/parameter.h" #include "xgboost/data.h" +#include "xgboost/parameter.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" #include "../src/tree/param.h" @@ -25,7 +23,6 @@ namespace xgboost { namespace sycl { namespace tree { - /*! \brief Wrapper for necessary training parameters for regression tree to access on device */ /* The original structure xgboost::tree::TrainParam can't be used, * since std::vector are not copyable on sycl-devices. @@ -38,7 +35,7 @@ struct TrainParam { TrainParam() {} - explicit TrainParam(const xgboost::tree::TrainParam& param) { + explicit TrainParam(const xgboost::tree::TrainParam ¶m) { reg_lambda = param.reg_lambda; reg_alpha = param.reg_alpha; min_child_weight = param.min_child_weight; @@ -54,23 +51,20 @@ using GradStats = xgboost::detail::GradientPairInternal; * Original structure cannot be used due 'cat_bits' field of type std::vector, * which is not device-copyable */ -template +template struct SplitEntryContainer { /*! \brief loss change after split this node */ - bst_float loss_chg {0.0f}; + bst_float loss_chg{0.0f}; /*! \brief split index */ bst_feature_t sindex{0}; bst_float split_value{0.0f}; - GradientT left_sum; GradientT right_sum; - SplitEntryContainer() = default; - - friend std::ostream& operator<<(std::ostream& os, SplitEntryContainer const& s) { + friend std::ostream &operator<<(std::ostream &os, SplitEntryContainer const &s) { os << "loss_chg: " << s.loss_chg << ", " << "split index: " << s.SplitIndex() << ", " << "split value: " << s.split_value << ", " @@ -94,8 +88,8 @@ struct SplitEntryContainer { */ inline bool NeedReplace(bst_float new_loss_chg, unsigned split_index) const { if (::sycl::isinf(new_loss_chg)) { // in some cases new_loss_chg can be NaN or Inf, - // for example when lambda = 0 & min_child_weight = 0 - // skip value in this case + // for example when lambda = 0 & min_child_weight = 0 + // skip value in this case return false; } else if (this->SplitIndex() <= split_index) { return new_loss_chg > this->loss_chg; @@ -128,10 +122,8 @@ struct SplitEntryContainer { * \param default_left whether the missing value goes to left * \return whether the proposed split is better and can replace current split */ - bool Update(bst_float new_loss_chg, unsigned split_index, - bst_float new_split_value, bool default_left, - const GradientT &left_sum, - const GradientT &right_sum) { + bool Update(bst_float new_loss_chg, unsigned split_index, bst_float new_split_value, + bool default_left, const GradientT &left_sum, const GradientT &right_sum) { if (this->NeedReplace(new_loss_chg, split_index)) { this->loss_chg = new_loss_chg; if (default_left) { @@ -147,15 +139,14 @@ struct SplitEntryContainer { } } - /*! \brief same as update, used by AllReduce*/ - inline static void Reduce(SplitEntryContainer &dst, // NOLINT(*) - const SplitEntryContainer &src) { // NOLINT(*) + inline static void Reduce(SplitEntryContainer &dst, // NOLINT(*) + const SplitEntryContainer &src) { // NOLINT(*) dst.Update(src); } }; -template +template using SplitEntry = SplitEntryContainer>; } // namespace tree diff --git a/plugin/sycl/tree/split_evaluator.h b/plugin/sycl/tree/split_evaluator.h index 40acfa69db28..7740e8edae80 100644 --- a/plugin/sycl/tree/split_evaluator.h +++ b/plugin/sycl/tree/split_evaluator.h @@ -7,36 +7,35 @@ #include #include + +#include +#include #include #include -#include - -#include "param.h" -#include "../data.h" -#include "xgboost/tree_model.h" -#include "xgboost/host_device_vector.h" -#include "xgboost/context.h" -#include "../../src/common/transform.h" #include "../../src/common/math.h" +#include "../../src/common/transform.h" #include "../../src/tree/param.h" - -#include +#include "../data.h" +#include "param.h" +#include "xgboost/context.h" +#include "xgboost/host_device_vector.h" +#include "xgboost/tree_model.h" namespace xgboost { namespace sycl { namespace tree { -/*! \brief SYCL implementation of TreeEvaluator, with USM memory for temporary buffer to access on device. - * It also contains own implementation of SplitEvaluator for device compilation, because some of the - functions from the original SplitEvaluator are currently not supported +/*! \brief SYCL implementation of TreeEvaluator, with USM memory for temporary buffer to access on + device. + * It also contains own implementation of SplitEvaluator for device compilation, because + some of the functions from the original SplitEvaluator are currently not supported */ -template +template class TreeEvaluator { // hist and exact use parent id to calculate constraints. - static constexpr bst_node_t kRootParentId = - (-1 & static_cast((1U << 31) - 1)); + static constexpr bst_node_t kRootParentId = (-1 & static_cast((1U << 31) - 1)); USMVector lower_bounds_; USMVector upper_bounds_; @@ -60,7 +59,7 @@ class TreeEvaluator { if (has_constraint_) { monotone_.Resize(qu_, n_features, 0); qu_->memcpy(monotone_.Data(), p.monotone_constraints.data(), - sizeof(int) * p.monotone_constraints.size()); + sizeof(int) * p.monotone_constraints.size()); qu_->wait(); lower_bounds_.Resize(qu_, p.MaxNodes(), std::numeric_limits::lowest()); @@ -69,9 +68,7 @@ class TreeEvaluator { param_ = TrainParam(p); } - bool HasConstraint() const { - return has_constraint_; - } + bool HasConstraint() const { return has_constraint_; } TreeEvaluator(::sycl::queue* qu, xgboost::tree::TrainParam const& p, bst_feature_t n_features) { Reset(qu, p, n_features); @@ -84,15 +81,13 @@ class TreeEvaluator { bool has_constraint; TrainParam param; - GradType CalcSplitGain(bst_node_t nidx, - bst_feature_t fidx, - const GradStats& left, - const GradStats& right) const { + GradType CalcSplitGain(bst_node_t nidx, bst_feature_t fidx, const GradStats& left, + const GradStats& right) const { const GradType negative_infinity = -std::numeric_limits::infinity(); GradType wleft = this->CalcWeight(nidx, left); GradType wright = this->CalcWeight(nidx, right); - GradType gain = this->CalcGainGivenWeight(nidx, left, wleft) + + GradType gain = this->CalcGainGivenWeight(nidx, left, wleft) + this->CalcGainGivenWeight(nidx, right, wright); if (!has_constraint) { return gain; @@ -109,10 +104,10 @@ class TreeEvaluator { } inline static GradType ThresholdL1(GradType w, float alpha) { - if (w > + alpha) { + if (w > +alpha) { return w - alpha; } - if (w < - alpha) { + if (w < -alpha) { return w + alpha; } return 0.0; @@ -171,15 +166,12 @@ class TreeEvaluator { public: /* Get a view to the evaluator that can be passed down to device. */ auto GetEvaluator() const { - return SplitEvaluator{monotone_.DataConst(), - lower_bounds_.DataConst(), - upper_bounds_.DataConst(), - has_constraint_, - param_}; + return SplitEvaluator{monotone_.DataConst(), lower_bounds_.DataConst(), + upper_bounds_.DataConst(), has_constraint_, param_}; } - void AddSplit(bst_node_t nodeid, bst_node_t leftid, bst_node_t rightid, - bst_feature_t f, GradType left_weight, GradType right_weight) { + void AddSplit(bst_node_t nodeid, bst_node_t leftid, bst_node_t rightid, bst_feature_t f, + GradType left_weight, GradType right_weight) { if (!has_constraint_) { return; } diff --git a/plugin/sycl/tree/updater_quantile_hist.cc b/plugin/sycl/tree/updater_quantile_hist.cc index 14fa701e62fa..397e2d715516 100644 --- a/plugin/sycl/tree/updater_quantile_hist.cc +++ b/plugin/sycl/tree/updater_quantile_hist.cc @@ -2,8 +2,8 @@ * Copyright 2017-2024 by Contributors * \file updater_quantile_hist.cc */ -#include #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" @@ -25,7 +25,7 @@ DMLC_REGISTRY_FILE_TAG(updater_quantile_hist_sycl); DMLC_REGISTER_PARAMETER(HistMakerTrainParam); -void QuantileHistMaker::Configure(const Args& args) { +void QuantileHistMaker::Configure(const Args &args) { const DeviceOrd device_spec = ctx_->Device(); qu_ = device_manager.GetQueue(device_spec); @@ -43,14 +43,9 @@ void QuantileHistMaker::Configure(const Args& args) { } } -template -void QuantileHistMaker::SetPimpl(std::unique_ptr>* pimpl, - DMatrix *dmat) { - pimpl->reset(new HistUpdater( - ctx_, - qu_, - param_, - int_constraint_, dmat)); +template +void QuantileHistMaker::SetPimpl(std::unique_ptr> *pimpl, DMatrix *dmat) { + pimpl->reset(new HistUpdater(ctx_, qu_, param_, int_constraint_, dmat)); if (collective::IsDistributed()) { (*pimpl)->SetHistSynchronizer(new DistributedHistSynchronizer()); (*pimpl)->SetHistRowsAdder(new DistributedHistRowsAdder()); @@ -126,9 +121,8 @@ bool QuantileHistMaker::UpdatePredictionCache(const DMatrix *data, } XGBOOST_REGISTER_TREE_UPDATER(QuantileHistMaker, "grow_quantile_histmaker_sycl") -.describe("Grow tree using quantized histogram with SYCL.") -.set_body( - [](Context const* ctx, ObjInfo const * task) { + .describe("Grow tree using quantized histogram with SYCL.") + .set_body([](Context const *ctx, ObjInfo const *task) { return new QuantileHistMaker(ctx, task); }); } // namespace tree diff --git a/plugin/sycl/tree/updater_quantile_hist.h b/plugin/sycl/tree/updater_quantile_hist.h index 5419c6ae825f..469f5ad0a47b 100644 --- a/plugin/sycl/tree/updater_quantile_hist.h +++ b/plugin/sycl/tree/updater_quantile_hist.h @@ -29,21 +29,20 @@ namespace sycl { namespace tree { // training parameters specific to this algorithm -struct HistMakerTrainParam - : public XGBoostParameter { +struct HistMakerTrainParam : public XGBoostParameter { bool single_precision_histogram = false; // declare parameters DMLC_DECLARE_PARAMETER(HistMakerTrainParam) { - DMLC_DECLARE_FIELD(single_precision_histogram).set_default(false).describe( - "Use single precision to build histograms."); + DMLC_DECLARE_FIELD(single_precision_histogram) + .set_default(false) + .describe("Use single precision to build histograms."); } }; /*! \brief construct a tree using quantized feature values with SYCL backend*/ -class QuantileHistMaker: public TreeUpdater { +class QuantileHistMaker : public TreeUpdater { public: - QuantileHistMaker(Context const* ctx, ObjInfo const * task) : - TreeUpdater(ctx), task_{task} { + QuantileHistMaker(Context const* ctx, ObjInfo const* task) : TreeUpdater(ctx), task_{task} { updater_monitor_.Init("SYCLQuantileHistMaker"); } void Configure(const Args& args) override; @@ -68,9 +67,7 @@ class QuantileHistMaker: public TreeUpdater { out["sycl_hist_train_param"] = ToJson(hist_maker_param_); } - char const* Name() const override { - return "grow_quantile_histmaker_sycl"; - } + char const* Name() const override { return "grow_quantile_histmaker_sycl"; } protected: HistMakerTrainParam hist_maker_param_; @@ -80,23 +77,22 @@ class QuantileHistMaker: public TreeUpdater { common::GHistIndexMatrix gmat_; // (optional) data matrix with feature grouping // column accessor - DMatrix const* p_last_dmat_ {nullptr}; - bool is_gmat_initialized_ {false}; + DMatrix const* p_last_dmat_{nullptr}; + bool is_gmat_initialized_{false}; xgboost::common::Monitor updater_monitor_; - template - void SetPimpl(std::unique_ptr>*, DMatrix *dmat); + template + void SetPimpl(std::unique_ptr>*, DMatrix* dmat); - template + template void CallUpdate(const std::unique_ptr>& builder, - xgboost::tree::TrainParam const *param, - ::xgboost::linalg::Matrix *gpair, - DMatrix *dmat, + xgboost::tree::TrainParam const* param, + ::xgboost::linalg::Matrix* gpair, DMatrix* dmat, xgboost::common::Span> out_position, - const std::vector &trees); + const std::vector& trees); - enum class HistPrecision {fp32, fp64}; + enum class HistPrecision { fp32, fp64 }; HistPrecision hist_precision_; std::unique_ptr> pimpl_fp32; @@ -106,10 +102,9 @@ class QuantileHistMaker: public TreeUpdater { ::sycl::queue* qu_; DeviceManager device_manager; - ObjInfo const *task_{nullptr}; + ObjInfo const* task_{nullptr}; }; - } // namespace tree } // namespace sycl } // namespace xgboost diff --git a/python-package/.gitignore b/python-package/.gitignore index d765c67c773e..2ebc5b00be03 100644 --- a/python-package/.gitignore +++ b/python-package/.gitignore @@ -1,3 +1,3 @@ build dist -*.egg* \ No newline at end of file +*.egg* diff --git a/python-package/hatch_build.py b/python-package/hatch_build.py index d81a21cd55d6..0beb9c15e0ec 100644 --- a/python-package/hatch_build.py +++ b/python-package/hatch_build.py @@ -18,6 +18,7 @@ def get_tag() -> str: class CustomBuildHook(BuildHookInterface): """A custom build hook""" + # pylint: disable-next=unused-argument def initialize(self, version: str, build_data: Dict[str, Any]) -> None: """This step ccurs immediately before each build.""" build_data["tag"] = get_tag() diff --git a/src/c_api/c_api.cc b/src/c_api/c_api.cc index 313175a2d584..d2a0efc3aa49 100644 --- a/src/c_api/c_api.cc +++ b/src/c_api/c_api.cc @@ -50,9 +50,9 @@ #include "xgboost/version_config.h" // for XGBOOST_VER_MAJOR, XGBOOST_VER_MINOR, XGBOOS... #include "xgboost/windefs.h" // for xgboost_IS_WIN -using namespace xgboost; // NOLINT(*); +using namespace xgboost; // NOLINT(*); -XGB_DLL void XGBoostVersion(int* major, int* minor, int* patch) { +XGB_DLL void XGBoostVersion(int *major, int *minor, int *patch) { if (major) { *major = XGBOOST_VER_MAJOR; } @@ -143,9 +143,9 @@ XGB_DLL int XGBuildInfo(char const **out) { API_END(); } -XGB_DLL int XGBRegisterLogCallback(void (*callback)(const char*)) { +XGB_DLL int XGBRegisterLogCallback(void (*callback)(const char *)) { API_BEGIN_UNGUARD() - LogCallbackRegistry* registry = LogCallbackRegistryStore::Get(); + LogCallbackRegistry *registry = LogCallbackRegistryStore::Get(); registry->Register(callback); API_END(); } @@ -224,22 +224,22 @@ XGB_DLL int XGBSetGlobalConfig(const char *json_str) { API_END(); } -XGB_DLL int XGBGetGlobalConfig(const char** json_str) { +XGB_DLL int XGBGetGlobalConfig(const char **json_str) { API_BEGIN_UNGUARD() - auto const& global_config = *GlobalConfigThreadLocalStore::Get(); - Json config {ToJson(global_config)}; - auto const* mgr = global_config.__MANAGER__(); + auto const &global_config = *GlobalConfigThreadLocalStore::Get(); + Json config{ToJson(global_config)}; + auto const *mgr = global_config.__MANAGER__(); - for (auto& item : get(config)) { + for (auto &item : get(config)) { auto const &str = get(item.second); auto const &name = item.first; auto e = mgr->Find(name); CHECK(e); - if (dynamic_cast const*>(e) || - dynamic_cast const*>(e) || - dynamic_cast const*>(e) || - dynamic_cast const*>(e)) { + if (dynamic_cast const *>(e) || + dynamic_cast const *>(e) || + dynamic_cast const *>(e) || + dynamic_cast const *>(e)) { auto i = std::strtoimax(str.data(), nullptr, 10); CHECK_LE(i, static_cast(std::numeric_limits::max())); item.second = Integer(static_cast(i)); @@ -255,7 +255,7 @@ XGB_DLL int XGBGetGlobalConfig(const char** json_str) { } config["nthread"] = GlobalConfigThreadLocalStore::Get()->nthread; - auto& local = *GlobalConfigAPIThreadLocalStore::Get(); + auto &local = *GlobalConfigAPIThreadLocalStore::Get(); Json::Dump(config, &local.ret_str); xgboost_CHECK_C_ARG_PTR(json_str); @@ -295,9 +295,7 @@ XGB_DLL int XGDMatrixCreateFromURI(const char *config, DMatrixHandle *out) { XGB_DLL int XGDMatrixCreateFromDataIter( void *data_handle, // a Java iterator XGBCallbackDataIterNext *callback, // C++ callback defined in xgboost4j.cpp - const char *cache_info, - float missing, - DMatrixHandle *out) { + const char *cache_info, float missing, DMatrixHandle *out) { API_BEGIN(); std::string scache; @@ -307,9 +305,7 @@ XGB_DLL int XGDMatrixCreateFromDataIter( xgboost::data::IteratorAdapter adapter( data_handle, callback); xgboost_CHECK_C_ARG_PTR(out); - *out = new std::shared_ptr { - DMatrix::Create(&adapter, missing, 1, scache) - }; + *out = new std::shared_ptr{DMatrix::Create(&adapter, missing, 1, scache)}; API_END(); } @@ -530,8 +526,7 @@ XGB_DLL int XGDMatrixCreateFromCSR(char const *indptr, char const *indices, char API_END(); } -XGB_DLL int XGDMatrixCreateFromDense(char const *data, - char const *c_json_config, +XGB_DLL int XGDMatrixCreateFromDense(char const *data, char const *c_json_config, DMatrixHandle *out) { API_BEGIN(); xgboost_CHECK_C_ARG_PTR(data); @@ -570,10 +565,8 @@ XGB_DLL int XGDMatrixCreateFromCSC(char const *indptr, char const *indices, char API_END(); } -XGB_DLL int XGDMatrixCreateFromMat(const bst_float* data, - xgboost::bst_ulong nrow, - xgboost::bst_ulong ncol, bst_float missing, - DMatrixHandle* out) { +XGB_DLL int XGDMatrixCreateFromMat(const bst_float *data, xgboost::bst_ulong nrow, + xgboost::bst_ulong ncol, bst_float missing, DMatrixHandle *out) { API_BEGIN(); data::DenseAdapter adapter(data, nrow, ncol); xgboost_CHECK_C_ARG_PTR(out); @@ -581,11 +574,9 @@ XGB_DLL int XGDMatrixCreateFromMat(const bst_float* data, API_END(); } -XGB_DLL int XGDMatrixCreateFromMat_omp(const bst_float* data, // NOLINT - xgboost::bst_ulong nrow, - xgboost::bst_ulong ncol, - bst_float missing, DMatrixHandle* out, - int nthread) { +XGB_DLL int XGDMatrixCreateFromMat_omp(const bst_float *data, // NOLINT + xgboost::bst_ulong nrow, xgboost::bst_ulong ncol, + bst_float missing, DMatrixHandle *out, int nthread) { API_BEGIN(); data::DenseAdapter adapter(data, nrow, ncol); xgboost_CHECK_C_ARG_PTR(out); @@ -599,41 +590,32 @@ XGB_DLL int XGDMatrixSliceDMatrix(DMatrixHandle handle, const int *idxset, xgboo return XGDMatrixSliceDMatrixEx(handle, idxset, len, out, 0); } -XGB_DLL int XGDMatrixSliceDMatrixEx(DMatrixHandle handle, - const int* idxset, - xgboost::bst_ulong len, - DMatrixHandle* out, - int allow_groups) { +XGB_DLL int XGDMatrixSliceDMatrixEx(DMatrixHandle handle, const int *idxset, xgboost::bst_ulong len, + DMatrixHandle *out, int allow_groups) { API_BEGIN(); CHECK_HANDLE(); if (!allow_groups) { - CHECK_EQ(static_cast*>(handle) - ->get() - ->Info() - .group_ptr_.size(), - 0U) + CHECK_EQ(static_cast *>(handle)->get()->Info().group_ptr_.size(), 0U) << "slice does not support group structure"; } - DMatrix* dmat = static_cast*>(handle)->get(); - *out = new std::shared_ptr( - dmat->Slice({idxset, static_cast(len)})); + DMatrix *dmat = static_cast *>(handle)->get(); + *out = new std::shared_ptr(dmat->Slice({idxset, static_cast(len)})); API_END(); } XGB_DLL int XGDMatrixFree(DMatrixHandle handle) { API_BEGIN(); CHECK_HANDLE(); - delete static_cast*>(handle); + delete static_cast *>(handle); API_END(); } -XGB_DLL int XGDMatrixSaveBinary(DMatrixHandle handle, const char* fname, - int) { +XGB_DLL int XGDMatrixSaveBinary(DMatrixHandle handle, const char *fname, int) { API_BEGIN(); CHECK_HANDLE(); - auto dmat = static_cast*>(handle)->get(); + auto dmat = static_cast *>(handle)->get(); xgboost_CHECK_C_ARG_PTR(fname); - if (data::SimpleDMatrix* derived = dynamic_cast(dmat)) { + if (data::SimpleDMatrix *derived = dynamic_cast(dmat)) { derived->SaveToLocalFile(fname); } else { LOG(FATAL) << "binary saving only supported by SimpleDMatrix"; @@ -672,8 +654,7 @@ XGB_DLL int XGDMatrixSetUIntInfo(DMatrixHandle handle, const char *field, const API_END(); } -XGB_DLL int XGDMatrixSetStrFeatureInfo(DMatrixHandle handle, const char *field, - const char **c_info, +XGB_DLL int XGDMatrixSetStrFeatureInfo(DMatrixHandle handle, const char *field, const char **c_info, const xgboost::bst_ulong size) { API_BEGIN(); CHECK_HANDLE(); @@ -684,11 +665,10 @@ XGB_DLL int XGDMatrixSetStrFeatureInfo(DMatrixHandle handle, const char *field, } XGB_DLL int XGDMatrixGetStrFeatureInfo(DMatrixHandle handle, const char *field, - xgboost::bst_ulong *len, - const char ***out_features) { + xgboost::bst_ulong *len, const char ***out_features) { API_BEGIN(); CHECK_HANDLE(); - auto m = *static_cast*>(handle); + auto m = *static_cast *>(handle); auto &info = static_cast *>(handle)->get()->Info(); std::vector &charp_vecs = m->GetThreadLocal().ret_vec_charp; @@ -878,31 +858,27 @@ XGB_DLL int XGDMatrixSetDenseInfo(DMatrixHandle handle, const char *field, void API_END(); } -XGB_DLL int XGDMatrixGetFloatInfo(const DMatrixHandle handle, - const char* field, - xgboost::bst_ulong* out_len, - const bst_float** out_dptr) { +XGB_DLL int XGDMatrixGetFloatInfo(const DMatrixHandle handle, const char *field, + xgboost::bst_ulong *out_len, const bst_float **out_dptr) { API_BEGIN(); CHECK_HANDLE(); xgboost_CHECK_C_ARG_PTR(field); - const MetaInfo& info = static_cast*>(handle)->get()->Info(); + const MetaInfo &info = static_cast *>(handle)->get()->Info(); xgboost_CHECK_C_ARG_PTR(out_len); xgboost_CHECK_C_ARG_PTR(out_dptr); - info.GetInfo(field, out_len, DataType::kFloat32, reinterpret_cast(out_dptr)); + info.GetInfo(field, out_len, DataType::kFloat32, reinterpret_cast(out_dptr)); API_END(); } -XGB_DLL int XGDMatrixGetUIntInfo(const DMatrixHandle handle, - const char *field, - xgboost::bst_ulong *out_len, - const unsigned **out_dptr) { +XGB_DLL int XGDMatrixGetUIntInfo(const DMatrixHandle handle, const char *field, + xgboost::bst_ulong *out_len, const unsigned **out_dptr) { API_BEGIN(); CHECK_HANDLE(); xgboost_CHECK_C_ARG_PTR(field); - const MetaInfo& info = static_cast*>(handle)->get()->Info(); + const MetaInfo &info = static_cast *>(handle)->get()->Info(); xgboost_CHECK_C_ARG_PTR(out_len); xgboost_CHECK_C_ARG_PTR(out_dptr); - info.GetInfo(field, out_len, DataType::kUInt32, reinterpret_cast(out_dptr)); + info.GetInfo(field, out_len, DataType::kUInt32, reinterpret_cast(out_dptr)); API_END(); } @@ -1077,14 +1053,13 @@ XGB_DLL int XGDMatrixGetQuantileCut(DMatrixHandle const handle, char const *conf } // xgboost implementation -XGB_DLL int XGBoosterCreate(const DMatrixHandle dmats[], - xgboost::bst_ulong len, +XGB_DLL int XGBoosterCreate(const DMatrixHandle dmats[], xgboost::bst_ulong len, BoosterHandle *out) { API_BEGIN(); - std::vector > mats; + std::vector> mats; for (xgboost::bst_ulong i = 0; i < len; ++i) { xgboost_CHECK_C_ARG_PTR(dmats); - mats.push_back(*static_cast*>(dmats[i])); + mats.push_back(*static_cast *>(dmats[i])); } xgboost_CHECK_C_ARG_PTR(out); *out = Learner::Create(mats); @@ -1094,7 +1069,7 @@ XGB_DLL int XGBoosterCreate(const DMatrixHandle dmats[], XGB_DLL int XGBoosterFree(BoosterHandle handle) { API_BEGIN(); CHECK_HANDLE(); - delete static_cast(handle); + delete static_cast(handle); API_END(); } @@ -1105,53 +1080,49 @@ XGB_DLL int XGBoosterReset(BoosterHandle handle) { API_END(); } -XGB_DLL int XGBoosterSetParam(BoosterHandle handle, - const char *name, - const char *value) { +XGB_DLL int XGBoosterSetParam(BoosterHandle handle, const char *name, const char *value) { API_BEGIN(); CHECK_HANDLE(); - static_cast(handle)->SetParam(name, value); + static_cast(handle)->SetParam(name, value); API_END(); } -XGB_DLL int XGBoosterGetNumFeature(BoosterHandle handle, - xgboost::bst_ulong *out) { +XGB_DLL int XGBoosterGetNumFeature(BoosterHandle handle, xgboost::bst_ulong *out) { API_BEGIN(); CHECK_HANDLE(); - static_cast(handle)->Configure(); + static_cast(handle)->Configure(); xgboost_CHECK_C_ARG_PTR(out); - *out = static_cast(handle)->GetNumFeature(); + *out = static_cast(handle)->GetNumFeature(); API_END(); } -XGB_DLL int XGBoosterBoostedRounds(BoosterHandle handle, int* out) { +XGB_DLL int XGBoosterBoostedRounds(BoosterHandle handle, int *out) { API_BEGIN(); CHECK_HANDLE(); - static_cast(handle)->Configure(); + static_cast(handle)->Configure(); xgboost_CHECK_C_ARG_PTR(out); - *out = static_cast(handle)->BoostedRounds(); + *out = static_cast(handle)->BoostedRounds(); API_END(); } -XGB_DLL int XGBoosterLoadJsonConfig(BoosterHandle handle, char const* json_parameters) { +XGB_DLL int XGBoosterLoadJsonConfig(BoosterHandle handle, char const *json_parameters) { API_BEGIN(); CHECK_HANDLE(); xgboost_CHECK_C_ARG_PTR(json_parameters); - Json config { Json::Load(StringView{json_parameters}) }; - static_cast(handle)->LoadConfig(config); + Json config{Json::Load(StringView{json_parameters})}; + static_cast(handle)->LoadConfig(config); API_END(); } -XGB_DLL int XGBoosterSaveJsonConfig(BoosterHandle handle, - xgboost::bst_ulong *out_len, - char const** out_str) { +XGB_DLL int XGBoosterSaveJsonConfig(BoosterHandle handle, xgboost::bst_ulong *out_len, + char const **out_str) { API_BEGIN(); CHECK_HANDLE(); - Json config { Object() }; - auto* learner = static_cast(handle); + Json config{Object()}; + auto *learner = static_cast(handle); learner->Configure(); learner->SaveConfig(&config); - std::string& raw_str = learner->GetThreadLocal().ret_str; + std::string &raw_str = learner->GetThreadLocal().ret_str; Json::Dump(config, &raw_str); xgboost_CHECK_C_ARG_PTR(out_str); @@ -1162,12 +1133,10 @@ XGB_DLL int XGBoosterSaveJsonConfig(BoosterHandle handle, API_END(); } -XGB_DLL int XGBoosterUpdateOneIter(BoosterHandle handle, - int iter, - DMatrixHandle dtrain) { +XGB_DLL int XGBoosterUpdateOneIter(BoosterHandle handle, int iter, DMatrixHandle dtrain) { API_BEGIN(); CHECK_HANDLE(); - auto* bst = static_cast(handle); + auto *bst = static_cast(handle); xgboost_CHECK_C_ARG_PTR(dtrain); auto *dtr = static_cast *>(dtrain); CHECK(dtr); @@ -1202,7 +1171,7 @@ void CopyGradientFromCudaArrays(Context const *, ArrayInterface<2, false> const common::AssertGPUSupport(); } #else -; // NOLINT + ; // NOLINT #endif // Helper function to copy gradient from array interface to linalg::Matrix @@ -1292,23 +1261,20 @@ XGB_DLL int XGBoosterTrainOneIterWithSplitGrad(BoosterHandle handle, DMatrixHand API_END(); } -XGB_DLL int XGBoosterEvalOneIter(BoosterHandle handle, - int iter, - DMatrixHandle dmats[], - const char* evnames[], - xgboost::bst_ulong len, - const char** out_str) { +XGB_DLL int XGBoosterEvalOneIter(BoosterHandle handle, int iter, DMatrixHandle dmats[], + const char *evnames[], xgboost::bst_ulong len, + const char **out_str) { API_BEGIN(); CHECK_HANDLE(); - auto* bst = static_cast(handle); - std::string& eval_str = bst->GetThreadLocal().ret_str; + auto *bst = static_cast(handle); + std::string &eval_str = bst->GetThreadLocal().ret_str; std::vector> data_sets; std::vector data_names; for (xgboost::bst_ulong i = 0; i < len; ++i) { xgboost_CHECK_C_ARG_PTR(dmats); - data_sets.push_back(*static_cast*>(dmats[i])); + data_sets.push_back(*static_cast *>(dmats[i])); xgboost_CHECK_C_ARG_PTR(evnames); data_names.emplace_back(evnames[i]); } @@ -1319,22 +1285,17 @@ XGB_DLL int XGBoosterEvalOneIter(BoosterHandle handle, API_END(); } -XGB_DLL int XGBoosterPredict(BoosterHandle handle, - DMatrixHandle dmat, - int option_mask, - unsigned ntree_limit, - int training, - xgboost::bst_ulong *len, +XGB_DLL int XGBoosterPredict(BoosterHandle handle, DMatrixHandle dmat, int option_mask, + unsigned ntree_limit, int training, xgboost::bst_ulong *len, const bst_float **out_result) { API_BEGIN(); CHECK_HANDLE(); - auto *learner = static_cast(handle); - auto& entry = learner->GetThreadLocal().prediction_entry; + auto *learner = static_cast(handle); + auto &entry = learner->GetThreadLocal().prediction_entry; auto iteration_end = GetIterationFromTreeLimit(ntree_limit, learner); - learner->Predict(*static_cast *>(dmat), - (option_mask & 1) != 0, &entry.predictions, 0, iteration_end, - static_cast(training), (option_mask & 2) != 0, - (option_mask & 4) != 0, (option_mask & 8) != 0, + learner->Predict(*static_cast *>(dmat), (option_mask & 1) != 0, + &entry.predictions, 0, iteration_end, static_cast(training), + (option_mask & 2) != 0, (option_mask & 4) != 0, (option_mask & 8) != 0, (option_mask & 16) != 0); xgboost_CHECK_C_ARG_PTR(len); @@ -1345,12 +1306,10 @@ XGB_DLL int XGBoosterPredict(BoosterHandle handle, API_END(); } -XGB_DLL int XGBoosterPredictFromDMatrix(BoosterHandle handle, - DMatrixHandle dmat, - char const* c_json_config, +XGB_DLL int XGBoosterPredictFromDMatrix(BoosterHandle handle, DMatrixHandle dmat, + char const *c_json_config, xgboost::bst_ulong const **out_shape, - xgboost::bst_ulong *out_dim, - bst_float const **out_result) { + xgboost::bst_ulong *out_dim, bst_float const **out_result) { API_BEGIN(); if (handle == nullptr) { LOG(FATAL) << "Booster has not been initialized or has already been disposed."; @@ -1361,34 +1320,33 @@ XGB_DLL int XGBoosterPredictFromDMatrix(BoosterHandle handle, xgboost_CHECK_C_ARG_PTR(c_json_config); auto config = Json::Load(StringView{c_json_config}); - auto *learner = static_cast(handle); - auto& entry = learner->GetThreadLocal().prediction_entry; + auto *learner = static_cast(handle); + auto &entry = learner->GetThreadLocal().prediction_entry; auto p_m = *static_cast *>(dmat); auto type = PredictionType(RequiredArg(config, "type", __func__)); auto iteration_begin = RequiredArg(config, "iteration_begin", __func__); auto iteration_end = RequiredArg(config, "iteration_end", __func__); - auto const& j_config = get(config); + auto const &j_config = get(config); auto ntree_limit_it = j_config.find("ntree_limit"); if (ntree_limit_it != j_config.cend() && !IsA(ntree_limit_it->second) && get(ntree_limit_it->second) != 0) { - CHECK(iteration_end == 0) << - "Only one of the `ntree_limit` or `iteration_range` can be specified."; + CHECK(iteration_end == 0) + << "Only one of the `ntree_limit` or `iteration_range` can be specified."; LOG(WARNING) << "`ntree_limit` is deprecated, use `iteration_range` instead."; iteration_end = GetIterationFromTreeLimit(get(ntree_limit_it->second), learner); } - bool approximate = type == PredictionType::kApproxContribution || - type == PredictionType::kApproxInteraction; - bool contribs = type == PredictionType::kContribution || - type == PredictionType::kApproxContribution; - bool interactions = type == PredictionType::kInteraction || - type == PredictionType::kApproxInteraction; + bool approximate = + type == PredictionType::kApproxContribution || type == PredictionType::kApproxInteraction; + bool contribs = + type == PredictionType::kContribution || type == PredictionType::kApproxContribution; + bool interactions = + type == PredictionType::kInteraction || type == PredictionType::kApproxInteraction; bool training = RequiredArg(config, "training", __func__); - learner->Predict(p_m, type == PredictionType::kMargin, &entry.predictions, - iteration_begin, iteration_end, training, - type == PredictionType::kLeaf, contribs, approximate, + learner->Predict(p_m, type == PredictionType::kMargin, &entry.predictions, iteration_begin, + iteration_end, training, type == PredictionType::kLeaf, contribs, approximate, interactions); xgboost_CHECK_C_ARG_PTR(out_result); @@ -1404,9 +1362,8 @@ XGB_DLL int XGBoosterPredictFromDMatrix(BoosterHandle handle, xgboost_CHECK_C_ARG_PTR(out_dim); xgboost_CHECK_C_ARG_PTR(out_shape); - CalcPredictShape(strict_shape, type, p_m->Info().num_row_, - p_m->Info().num_col_, chunksize, learner->Groups(), rounds, - &shape, out_dim); + CalcPredictShape(strict_shape, type, p_m->Info().num_row_, p_m->Info().num_col_, chunksize, + learner->Groups(), rounds, &shape, out_dim); *out_shape = dmlc::BeginPtr(shape); API_END(); } @@ -1693,15 +1650,14 @@ XGB_DLL int XGBoosterSerializeToBuffer(BoosterHandle handle, xgboost::bst_ulong API_END(); } -XGB_DLL int XGBoosterUnserializeFromBuffer(BoosterHandle handle, - const void *buf, +XGB_DLL int XGBoosterUnserializeFromBuffer(BoosterHandle handle, const void *buf, xgboost::bst_ulong len) { API_BEGIN(); CHECK_HANDLE(); xgboost_CHECK_C_ARG_PTR(buf); - common::MemoryFixSizeBuffer fs((void*)buf, len); // NOLINT(*) - static_cast(handle)->Load(&fs); + common::MemoryFixSizeBuffer fs((void *)buf, len); // NOLINT(*) + static_cast(handle)->Load(&fs); API_END(); } @@ -1722,16 +1678,15 @@ XGB_DLL int XGBoosterSlice(BoosterHandle handle, int begin_layer, int end_layer, API_END(); } -inline void XGBoostDumpModelImpl(BoosterHandle handle, FeatureMap* fmap, - int with_stats, const char *format, - xgboost::bst_ulong *len, +inline void XGBoostDumpModelImpl(BoosterHandle handle, FeatureMap *fmap, int with_stats, + const char *format, xgboost::bst_ulong *len, const char ***out_models) { - auto *bst = static_cast(handle); + auto *bst = static_cast(handle); bst->Configure(); GenerateFeatureMap(bst, {}, bst->GetNumFeature(), fmap); - std::vector& str_vecs = bst->GetThreadLocal().ret_vec_str; - std::vector& charp_vecs = bst->GetThreadLocal().ret_vec_charp; + std::vector &str_vecs = bst->GetThreadLocal().ret_vec_str; + std::vector &charp_vecs = bst->GetThreadLocal().ret_vec_charp; str_vecs = bst->DumpModel(*fmap, with_stats != 0, format); charp_vecs.resize(str_vecs.size()); for (size_t i = 0; i < str_vecs.size(); ++i) { @@ -1745,23 +1700,17 @@ inline void XGBoostDumpModelImpl(BoosterHandle handle, FeatureMap* fmap, *len = static_cast(charp_vecs.size()); } -XGB_DLL int XGBoosterDumpModel(BoosterHandle handle, - const char* fmap, - int with_stats, - xgboost::bst_ulong* len, - const char*** out_models) { +XGB_DLL int XGBoosterDumpModel(BoosterHandle handle, const char *fmap, int with_stats, + xgboost::bst_ulong *len, const char ***out_models) { API_BEGIN(); CHECK_HANDLE(); return XGBoosterDumpModelEx(handle, fmap, with_stats, "text", len, out_models); API_END(); } -XGB_DLL int XGBoosterDumpModelEx(BoosterHandle handle, - const char* fmap, - int with_stats, - const char *format, - xgboost::bst_ulong* len, - const char*** out_models) { +XGB_DLL int XGBoosterDumpModelEx(BoosterHandle handle, const char *fmap, int with_stats, + const char *format, xgboost::bst_ulong *len, + const char ***out_models) { API_BEGIN(); CHECK_HANDLE(); @@ -1772,25 +1721,16 @@ XGB_DLL int XGBoosterDumpModelEx(BoosterHandle handle, API_END(); } -XGB_DLL int XGBoosterDumpModelWithFeatures(BoosterHandle handle, - int fnum, - const char** fname, - const char** ftype, - int with_stats, - xgboost::bst_ulong* len, - const char*** out_models) { - return XGBoosterDumpModelExWithFeatures(handle, fnum, fname, ftype, - with_stats, "text", len, out_models); +XGB_DLL int XGBoosterDumpModelWithFeatures(BoosterHandle handle, int fnum, const char **fname, + const char **ftype, int with_stats, + xgboost::bst_ulong *len, const char ***out_models) { + return XGBoosterDumpModelExWithFeatures(handle, fnum, fname, ftype, with_stats, "text", len, + out_models); } -XGB_DLL int XGBoosterDumpModelExWithFeatures(BoosterHandle handle, - int fnum, - const char** fname, - const char** ftype, - int with_stats, - const char *format, - xgboost::bst_ulong* len, - const char*** out_models) { +XGB_DLL int XGBoosterDumpModelExWithFeatures(BoosterHandle handle, int fnum, const char **fname, + const char **ftype, int with_stats, const char *format, + xgboost::bst_ulong *len, const char ***out_models) { API_BEGIN(); CHECK_HANDLE(); FeatureMap featmap; @@ -1850,8 +1790,8 @@ XGB_DLL int XGBoosterGetCategoriesExportToArrow(BoosterHandle handle, char const XGB_DLL int XGBoosterGetAttr(BoosterHandle handle, const char *key, const char **out, int *success) { - auto* bst = static_cast(handle); - std::string& ret_str = bst->GetThreadLocal().ret_str; + auto *bst = static_cast(handle); + std::string &ret_str = bst->GetThreadLocal().ret_str; API_BEGIN(); CHECK_HANDLE(); @@ -1868,12 +1808,10 @@ XGB_DLL int XGBoosterGetAttr(BoosterHandle handle, const char *key, const char * API_END(); } -XGB_DLL int XGBoosterSetAttr(BoosterHandle handle, - const char* key, - const char* value) { +XGB_DLL int XGBoosterSetAttr(BoosterHandle handle, const char *key, const char *value) { API_BEGIN(); CHECK_HANDLE(); - auto* bst = static_cast(handle); + auto *bst = static_cast(handle); xgboost_CHECK_C_ARG_PTR(key); if (value == nullptr) { bst->DelAttr(key); @@ -1884,16 +1822,14 @@ XGB_DLL int XGBoosterSetAttr(BoosterHandle handle, API_END(); } -XGB_DLL int XGBoosterGetAttrNames(BoosterHandle handle, - xgboost::bst_ulong* out_len, - const char*** out) { +XGB_DLL int XGBoosterGetAttrNames(BoosterHandle handle, xgboost::bst_ulong *out_len, + const char ***out) { API_BEGIN(); CHECK_HANDLE(); auto *learner = static_cast(handle); std::vector &str_vecs = learner->GetThreadLocal().ret_vec_str; - std::vector &charp_vecs = - learner->GetThreadLocal().ret_vec_charp; + std::vector &charp_vecs = learner->GetThreadLocal().ret_vec_charp; str_vecs = learner->GetAttrNames(); charp_vecs.resize(str_vecs.size()); for (size_t i = 0; i < str_vecs.size(); ++i) { @@ -1909,8 +1845,7 @@ XGB_DLL int XGBoosterGetAttrNames(BoosterHandle handle, } XGB_DLL int XGBoosterSetStrFeatureInfo(BoosterHandle handle, const char *field, - const char **features, - const xgboost::bst_ulong size) { + const char **features, const xgboost::bst_ulong size) { API_BEGIN(); CHECK_HANDLE(); auto *learner = static_cast(handle); @@ -1934,13 +1869,11 @@ XGB_DLL int XGBoosterSetStrFeatureInfo(BoosterHandle handle, const char *field, } XGB_DLL int XGBoosterGetStrFeatureInfo(BoosterHandle handle, const char *field, - xgboost::bst_ulong *len, - const char ***out_features) { + xgboost::bst_ulong *len, const char ***out_features) { API_BEGIN(); CHECK_HANDLE(); auto const *learner = static_cast(handle); - std::vector &charp_vecs = - learner->GetThreadLocal().ret_vec_charp; + std::vector &charp_vecs = learner->GetThreadLocal().ret_vec_charp; std::vector &str_vecs = learner->GetThreadLocal().ret_vec_str; if (!std::strcmp(field, "feature_name")) { learner->GetFeatureNames(&str_vecs); @@ -1998,9 +1931,9 @@ XGB_DLL int XGBoosterFeatureScore(BoosterHandle handle, char const *config, auto n_features = learner->GetNumFeature(); GenerateFeatureMap(learner, custom_feature_names, n_features, &feature_map); - auto& feature_names = learner->GetThreadLocal().ret_vec_str; + auto &feature_names = learner->GetThreadLocal().ret_vec_str; feature_names.resize(features.size()); - auto& feature_names_c = learner->GetThreadLocal().ret_vec_charp; + auto &feature_names_c = learner->GetThreadLocal().ret_vec_charp; feature_names_c.resize(features.size()); for (bst_feature_t i = 0; i < features.size(); ++i) { diff --git a/src/c_api/c_api.cu b/src/c_api/c_api.cu index 999d3dfb36d5..9eeeb65bf1e8 100644 --- a/src/c_api/c_api.cu +++ b/src/c_api/c_api.cu @@ -3,6 +3,10 @@ */ #include // for transform +#include +#include +#include + #include "../common/api_entry.h" // for XGBAPIThreadLocalEntry #include "../common/cuda_context.cuh" // for CUDAContext #include "../data/array_interface.h" // for DispatchDType, ArrayInterface @@ -104,12 +108,11 @@ void CopyGradientFromCudaArrays(Context const *ctx, ArrayInterface<2, false> con }); }); } -} // namespace xgboost +} // namespace xgboost using namespace xgboost; // NOLINT -XGB_DLL int XGDMatrixCreateFromCudaColumnar(char const *data, - char const* c_json_config, +XGB_DLL int XGDMatrixCreateFromCudaColumnar(char const *data, char const *c_json_config, DMatrixHandle *out) { API_BEGIN(); @@ -122,13 +125,11 @@ XGB_DLL int XGDMatrixCreateFromCudaColumnar(char const *data, float missing = GetMissing(config); auto n_threads = OptionalArg(config, "nthread", 0); data::CudfAdapter adapter(json_str); - *out = - new std::shared_ptr(DMatrix::Create(&adapter, missing, n_threads)); + *out = new std::shared_ptr(DMatrix::Create(&adapter, missing, n_threads)); API_END(); } -XGB_DLL int XGDMatrixCreateFromCudaArrayInterface(char const *data, - char const* c_json_config, +XGB_DLL int XGDMatrixCreateFromCudaArrayInterface(char const *data, char const *c_json_config, DMatrixHandle *out) { API_BEGIN(); std::string json_str{data}; @@ -136,8 +137,7 @@ XGB_DLL int XGDMatrixCreateFromCudaArrayInterface(char const *data, float missing = GetMissing(config); auto n_threads = OptionalArg(config, "nthread", 0); data::CupyAdapter adapter(json_str); - *out = - new std::shared_ptr(DMatrix::Create(&adapter, missing, n_threads)); + *out = new std::shared_ptr(DMatrix::Create(&adapter, missing, n_threads)); API_END(); } diff --git a/src/c_api/c_api_error.cc b/src/c_api/c_api_error.cc index 59dfb8854dc3..80eabae04c25 100644 --- a/src/c_api/c_api_error.cc +++ b/src/c_api/c_api_error.cc @@ -7,9 +7,11 @@ #include -#include "xgboost/c_api.h" +#include + #include "../collective/comm.h" #include "../collective/comm_group.h" +#include "xgboost/c_api.h" struct XGBAPIErrorEntry { std::string last_error; diff --git a/src/c_api/c_api_error.h b/src/c_api/c_api_error.h index c92aa7bd39d6..c9deb6ed11f2 100644 --- a/src/c_api/c_api_error.h +++ b/src/c_api/c_api_error.h @@ -13,9 +13,9 @@ /*! \brief macro to guard beginning and end section of all functions */ #ifdef LOG_CAPI_INVOCATION -#define API_BEGIN() \ - LOG(CONSOLE) << "[XGBoost C API invocation] " << __PRETTY_FUNCTION__; \ - try { \ +#define API_BEGIN() \ + LOG(CONSOLE) << "[XGBoost C API invocation] " << __PRETTY_FUNCTION__; \ + try { \ auto __guard = ::xgboost::XGBoostAPIGuard(); #define API_BEGIN_UNGUARD() \ @@ -23,8 +23,8 @@ try { #else // LOG_CAPI_INVOCATION -#define API_BEGIN() \ - try { \ +#define API_BEGIN() \ + try { \ auto __guard = ::xgboost::XGBoostAPIGuard(); #define API_BEGIN_UNGUARD() try { @@ -32,13 +32,15 @@ /*! \brief every function starts with API_BEGIN(); and finishes with API_END() */ -#define API_END() \ - } catch (dmlc::Error & _except_) { \ - return XGBAPIHandleException(_except_); \ - } catch (std::exception const& _except_) { \ - return XGBAPIHandleException(dmlc::Error(_except_.what())); \ - } \ - return 0; // NOLINT(*) +#define API_END() \ + } \ + catch (dmlc::Error & _except_) { \ + return XGBAPIHandleException(_except_); \ + } \ + catch (std::exception const& _except_) { \ + return XGBAPIHandleException(dmlc::Error(_except_.what())); \ + } \ + return 0; // NOLINT(*) #define CHECK_HANDLE() \ if (handle == nullptr) ::xgboost::detail::EmptyHandle(); diff --git a/src/c_api/c_api_utils.h b/src/c_api/c_api_utils.h index 0138ca808c61..80a3908b8d67 100644 --- a/src/c_api/c_api_utils.h +++ b/src/c_api/c_api_utils.h @@ -40,8 +40,7 @@ namespace xgboost { */ inline void CalcPredictShape(bool strict_shape, PredictionType type, size_t rows, size_t cols, size_t chunksize, size_t groups, size_t rounds, - std::vector *out_shape, - xgboost::bst_ulong *out_dim) { + std::vector *out_shape, xgboost::bst_ulong *out_dim) { auto &shape = *out_shape; if (type == PredictionType::kMargin && rows != 0) { // When kValue is used, softmax can change the chunksize. @@ -49,80 +48,80 @@ inline void CalcPredictShape(bool strict_shape, PredictionType type, size_t rows } switch (type) { - case PredictionType::kValue: - case PredictionType::kMargin: { - if (chunksize == 1 && !strict_shape) { - *out_dim = 1; - shape.resize(*out_dim); - shape.front() = rows; - } else { - *out_dim = 2; - shape.resize(*out_dim); - shape.front() = rows; - // chunksize can be 1 if it's softmax - shape.back() = std::min(groups, chunksize); + case PredictionType::kValue: + case PredictionType::kMargin: { + if (chunksize == 1 && !strict_shape) { + *out_dim = 1; + shape.resize(*out_dim); + shape.front() = rows; + } else { + *out_dim = 2; + shape.resize(*out_dim); + shape.front() = rows; + // chunksize can be 1 if it's softmax + shape.back() = std::min(groups, chunksize); + } + break; } - break; - } - case PredictionType::kApproxContribution: - case PredictionType::kContribution: { - if (groups == 1 && !strict_shape) { - *out_dim = 2; - shape.resize(*out_dim); - shape.front() = rows; - shape.back() = cols + 1; - } else { - *out_dim = 3; - shape.resize(*out_dim); - shape[0] = rows; - shape[1] = groups; - shape[2] = cols + 1; + case PredictionType::kApproxContribution: + case PredictionType::kContribution: { + if (groups == 1 && !strict_shape) { + *out_dim = 2; + shape.resize(*out_dim); + shape.front() = rows; + shape.back() = cols + 1; + } else { + *out_dim = 3; + shape.resize(*out_dim); + shape[0] = rows; + shape[1] = groups; + shape[2] = cols + 1; + } + break; } - break; - } - case PredictionType::kApproxInteraction: - case PredictionType::kInteraction: { - if (groups == 1 && !strict_shape) { - *out_dim = 3; - shape.resize(*out_dim); - shape[0] = rows; - shape[1] = cols + 1; - shape[2] = cols + 1; - } else { - *out_dim = 4; - shape.resize(*out_dim); - shape[0] = rows; - shape[1] = groups; - shape[2] = cols + 1; - shape[3] = cols + 1; + case PredictionType::kApproxInteraction: + case PredictionType::kInteraction: { + if (groups == 1 && !strict_shape) { + *out_dim = 3; + shape.resize(*out_dim); + shape[0] = rows; + shape[1] = cols + 1; + shape[2] = cols + 1; + } else { + *out_dim = 4; + shape.resize(*out_dim); + shape[0] = rows; + shape[1] = groups; + shape[2] = cols + 1; + shape[3] = cols + 1; + } + break; } - break; - } - case PredictionType::kLeaf: { - if (strict_shape) { - shape.resize(4); - shape[0] = rows; - shape[1] = rounds; - shape[2] = groups; - auto forest = chunksize / (shape[1] * shape[2]); - forest = std::max(static_cast(1), forest); - shape[3] = forest; - *out_dim = shape.size(); - } else if (chunksize == 1) { - *out_dim = 1; - shape.resize(*out_dim); - shape.front() = rows; - } else { - *out_dim = 2; - shape.resize(*out_dim); - shape.front() = rows; - shape.back() = chunksize; + case PredictionType::kLeaf: { + if (strict_shape) { + shape.resize(4); + shape[0] = rows; + shape[1] = rounds; + shape[2] = groups; + auto forest = chunksize / (shape[1] * shape[2]); + forest = std::max(static_cast(1), forest); + shape[3] = forest; + *out_dim = shape.size(); + } else if (chunksize == 1) { + *out_dim = 1; + shape.resize(*out_dim); + shape.front() = rows; + } else { + *out_dim = 2; + shape.resize(*out_dim); + shape.front() = rows; + shape.back() = chunksize; + } + break; + } + default: { + LOG(FATAL) << "Unknown prediction type:" << static_cast(type); } - break; - } - default: { - LOG(FATAL) << "Unknown prediction type:" << static_cast(type); - } } CHECK_EQ( std::accumulate(shape.cbegin(), shape.cend(), static_cast(1), std::multiplies<>{}), @@ -180,7 +179,7 @@ inline float GetMissing(Json const &config) { // Safe guard some global variables from being changed by XGBoost. class XGBoostAPIGuard { #if defined(XGBOOST_USE_CUDA) - std::int32_t device_id_ {0}; + std::int32_t device_id_{0}; void SetGPUAttribute(); void RestoreGPUAttribute(); @@ -190,15 +189,11 @@ class XGBoostAPIGuard { #endif public: - XGBoostAPIGuard() { - SetGPUAttribute(); - } - ~XGBoostAPIGuard() { - RestoreGPUAttribute(); - } + XGBoostAPIGuard() { SetGPUAttribute(); } + ~XGBoostAPIGuard() { RestoreGPUAttribute(); } }; -inline FeatureMap LoadFeatureMap(std::string const& uri) { +inline FeatureMap LoadFeatureMap(std::string const &uri) { FeatureMap feat; if (uri.size() != 0) { std::unique_ptr fs(dmlc::Stream::Create(uri.c_str(), "r")); @@ -209,11 +204,10 @@ inline FeatureMap LoadFeatureMap(std::string const& uri) { } inline void GenerateFeatureMap(Learner const *learner, - std::vector const &custom_feature_names, - size_t n_features, FeatureMap *out_feature_map) { + std::vector const &custom_feature_names, size_t n_features, + FeatureMap *out_feature_map) { auto &feature_map = *out_feature_map; - auto maybe = [&](std::vector const &values, size_t i, - std::string const &dft) { + auto maybe = [&](std::vector const &values, size_t i, std::string const &dft) { return values.empty() ? dft : values[i]; }; if (feature_map.Size() == 0) { @@ -225,8 +219,7 @@ inline void GenerateFeatureMap(Learner const *learner, // 3. from booster // 4. default feature name. if (!custom_feature_names.empty()) { - CHECK_EQ(custom_feature_names.size(), n_features) - << "Incorrect number of feature names."; + CHECK_EQ(custom_feature_names.size(), n_features) << "Incorrect number of feature names."; feature_names.resize(custom_feature_names.size()); std::transform(custom_feature_names.begin(), custom_feature_names.end(), feature_names.begin(), @@ -245,16 +238,14 @@ inline void GenerateFeatureMap(Learner const *learner, } for (size_t i = 0; i < n_features; ++i) { - feature_map.PushBack( - i, - maybe(feature_names, i, "f" + std::to_string(i)).data(), - maybe(feature_types, i, "q").data()); + feature_map.PushBack(i, maybe(feature_names, i, "f" + std::to_string(i)).data(), + maybe(feature_types, i, "q").data()); } } CHECK_EQ(feature_map.Size(), n_features); } -void XGBBuildInfoDevice(Json* p_info); +void XGBBuildInfoDevice(Json *p_info); /** * \brief Get shared ptr from DMatrix C handle with additional checks. diff --git a/src/c_api/coll_c_api.cc b/src/c_api/coll_c_api.cc index 0b52db9ee1ce..7b1fd3f9c30f 100644 --- a/src/c_api/coll_c_api.cc +++ b/src/c_api/coll_c_api.cc @@ -1,6 +1,7 @@ /** * Copyright 2023-2026, XGBoost Contributors */ +#include // for min #include // for seconds #include // for future #include // for unique_ptr diff --git a/src/collective/allgather.cc b/src/collective/allgather.cc index c2c7a500f0f3..778b5fb6712c 100644 --- a/src/collective/allgather.cc +++ b/src/collective/allgather.cc @@ -8,6 +8,7 @@ #include // for int8_t, int32_t, int64_t #include // for shared_ptr #include // for move +#include // for vector #include "broadcast.h" #include "comm.h" // for Comm, Channel diff --git a/src/collective/allgather.h b/src/collective/allgather.h index ca44c3916cc3..79208517f494 100644 --- a/src/collective/allgather.h +++ b/src/collective/allgather.h @@ -142,8 +142,8 @@ template std::vector sizes(comm.World(), 0); sizes[comm.Rank()] = data.Values().size_bytes(); auto erased_sizes = common::EraseType(common::Span{sizes.data(), sizes.size()}); - auto rc = comm.Backend(DeviceOrd::CPU()) - ->Allgather(comm.Ctx(ctx, DeviceOrd::CPU()), erased_sizes); + auto rc = + comm.Backend(DeviceOrd::CPU())->Allgather(comm.Ctx(ctx, DeviceOrd::CPU()), erased_sizes); if (!rc.OK()) { return rc; } diff --git a/src/collective/broadcast.cc b/src/collective/broadcast.cc index e1ef60f86847..171461b3b104 100644 --- a/src/collective/broadcast.cc +++ b/src/collective/broadcast.cc @@ -62,8 +62,11 @@ Result Broadcast(Comm const& comm, common::Span data, std::int32_t if (shifted_rank != 0) { // not root auto parent = ShiftRight(ShiftedParentRank(shifted_rank, depth), world, root); - auto rc = Success() << [&] { return comm.Chan(parent)->RecvAll(data); } - << [&] { return comm.Chan(parent)->Block(); }; + auto rc = Success() << [&] { + return comm.Chan(parent)->RecvAll(data); + } << [&] { + return comm.Chan(parent)->Block(); + }; if (!rc.OK()) { return Fail("broadcast failed.", std::move(rc)); } diff --git a/src/collective/coll.cc b/src/collective/coll.cc index b720d09b7eb9..0894d66a858a 100644 --- a/src/collective/coll.cc +++ b/src/collective/coll.cc @@ -100,7 +100,9 @@ bool constexpr IsFloatingPointV() { return Fail("Invalid op."); }); - return std::move(rc) << [&] { return comm.Block(); }; + return std::move(rc) << [&] { + return comm.Block(); + }; } [[nodiscard]] Result Coll::Broadcast(Comm const& comm, common::Span data, diff --git a/src/collective/coll.cu b/src/collective/coll.cu index d327e03ba29b..315cd3c4248e 100644 --- a/src/collective/coll.cu +++ b/src/collective/coll.cu @@ -2,17 +2,17 @@ * Copyright 2023-2025, XGBoost Contributors */ #if defined(XGBOOST_USE_NCCL) -#include // for chrono, chrono_literals -#include // for size_t -#include // for int8_t, int64_t -#include // for bit_and, bit_or, bit_xor -#include // for future, future_status -#include // for shared_ptr -#include // for mutex, unique_lock -#include // for string -#include // for this_thread -#include // for invoke_result_t, is_same_v, enable_if_t -#include // for move +#include // for chrono, chrono_literals +#include // for size_t +#include // for int8_t, int64_t +#include // for bit_and, bit_or, bit_xor +#include // for future, future_status +#include // for shared_ptr +#include // for mutex, unique_lock +#include // for string +#include // for this_thread +#include // for invoke_result_t, is_same_v, enable_if_t +#include // for move #include "../common/cuda_stream.h" // for StreamRef, Event #include "../common/device_helpers.cuh" // for device_vector @@ -264,11 +264,10 @@ ncclRedOp_t GetNCCLRedOp(Op const& op) { return DispatchDType(type, [&](auto t) { using T = decltype(t); auto rdata = common::RestoreType(data); - return AsyncLaunch( - &this->pool_, nccl, stub, this->stream_.View(), [&](curt::StreamRef s) { - return stub->Allreduce(data.data(), data.data(), rdata.size(), GetNCCLType(type), - GetNCCLRedOp(op), nccl->Handle(), s); - }); + return AsyncLaunch(&this->pool_, nccl, stub, this->stream_.View(), [&](curt::StreamRef s) { + return stub->Allreduce(data.data(), data.data(), rdata.size(), GetNCCLType(type), + GetNCCLRedOp(op), nccl->Handle(), s); + }); }); } } << [&] { diff --git a/src/collective/comm.cc b/src/collective/comm.cc index 60662a96f851..e9afb5439eee 100644 --- a/src/collective/comm.cc +++ b/src/collective/comm.cc @@ -11,6 +11,7 @@ #include // for string #include // for thread #include // for move, forward +#include // for vector #if !defined(XGBOOST_USE_NCCL) #include "../common/common.h" // for AssertNCCLSupport #endif // !defined(XGBOOST_USE_NCCL) @@ -108,7 +109,9 @@ Result ConnectTrackerImpl(proto::PeerInfo info, std::chrono::seconds timeout, st rc = std::move(rc) << [&] { return cpu_impl::RingAllgather(comm, s_buffer, HOST_NAME_MAX, 0, prev_ch, next_ch); - } << [&] { return block(); }; + } << [&] { + return block(); + }; if (!rc.OK()) { return Fail("Failed to get host names from peers.", std::move(rc)); } @@ -119,7 +122,9 @@ Result ConnectTrackerImpl(proto::PeerInfo info, std::chrono::seconds timeout, st auto s_ports = common::Span{reinterpret_cast(peers_port.data()), peers_port.size() * sizeof(ninfo.port)}; return cpu_impl::RingAllgather(comm, s_ports, sizeof(ninfo.port), 0, prev_ch, next_ch); - } << [&] { return block(); }; + } << [&] { + return block(); + }; if (!rc.OK()) { return Fail("Failed to get the port from peers.", std::move(rc)); } @@ -143,9 +148,11 @@ Result ConnectTrackerImpl(proto::PeerInfo info, std::chrono::seconds timeout, st for (std::int32_t r = (comm.Rank() + 1); r < comm.World(); ++r) { auto const& peer = peers[r]; auto worker = std::make_shared(); - rc = std::move(rc) - << [&] { return Connect(peer.host, peer.port, retry, timeout, worker.get()); } - << [&] { return worker->RecvTimeout(timeout); }; + rc = std::move(rc) << [&] { + return Connect(peer.host, peer.port, retry, timeout, worker.get()); + } << [&] { + return worker->RecvTimeout(timeout); + }; if (!rc.OK()) { return rc; } @@ -306,8 +313,11 @@ Comm* RabitComm::MakeCUDAVar(Context const*, std::shared_ptr) const { error_worker_.detach(); proto::Start start; - rc = std::move(rc) << [&] { return start.WorkerSend(lport, &tracker, eport); } - << [&] { return start.WorkerRecv(&tracker, &world); }; + rc = std::move(rc) << [&] { + return start.WorkerSend(lport, &tracker, eport); + } << [&] { + return start.WorkerRecv(&tracker, &world); + }; if (!rc.OK()) { return rc; } @@ -418,8 +428,11 @@ RabitComm::~RabitComm() noexcept(false) { } TCPSocket out; proto::Print print; - return Success() << [&] { return this->ConnectTracker(&out); } - << [&] { return print.WorkerSend(&out, msg); }; + return Success() << [&] { + return this->ConnectTracker(&out); + } << [&] { + return print.WorkerSend(&out, msg); + }; } [[nodiscard]] Result RabitComm::SignalError(Result const& res) { diff --git a/src/collective/comm.cu b/src/collective/comm.cu index 7894daed1a35..23ce54ceaad2 100644 --- a/src/collective/comm.cu +++ b/src/collective/comm.cu @@ -8,6 +8,8 @@ #include // for memcpy #include // for shared_ptr #include // for stringstream +#include // for string +#include // for move #include // for vector #include "../common/cuda_context.cuh" // for CUDAContext diff --git a/src/collective/comm_group.cc b/src/collective/comm_group.cc index 6b4c03686a32..6342771f34a3 100644 --- a/src/collective/comm_group.cc +++ b/src/collective/comm_group.cc @@ -10,6 +10,7 @@ #include // for back_inserter #include // for shared_ptr, unique_ptr #include // for string +#include // for move #include "../common/json_utils.h" // for OptionalArg #include "coll.h" // for Coll @@ -85,7 +86,8 @@ CommGroup::CommGroup() auto tracker_port = get_param("dmlc_tracker_port", static_cast(0), Integer{}); auto nccl = get_param("dmlc_nccl_path", std::string{DefaultNcclName()}, String{}); auto ptr = new CommGroup{ - std::shared_ptr{new RabitComm{ // NOLINT + std::shared_ptr{new RabitComm{ + // NOLINT tracker_host, static_cast(tracker_port), std::chrono::seconds{timeout}, static_cast(retry), task_id, nccl}}, std::shared_ptr(new Coll{})}; // NOLINT diff --git a/src/collective/in_memory_communicator.h b/src/collective/in_memory_communicator.h index bd89be6e32dc..8f02a975b0a9 100644 --- a/src/collective/in_memory_communicator.h +++ b/src/collective/in_memory_communicator.h @@ -51,9 +51,7 @@ class InMemoryCommunicator { return new InMemoryCommunicator(world_size, rank); } - InMemoryCommunicator(int world_size, int rank) { - handler_.Init(world_size, rank); - } + InMemoryCommunicator(int world_size, int rank) { handler_.Init(world_size, rank); } ~InMemoryCommunicator() override { handler_.Shutdown(sequence_number_++, GetRank()); } diff --git a/src/collective/in_memory_handler.cc b/src/collective/in_memory_handler.cc index 37be3f9c7127..c26109f60c96 100644 --- a/src/collective/in_memory_handler.cc +++ b/src/collective/in_memory_handler.cc @@ -5,6 +5,9 @@ #include #include +#include +#include + #include "comm.h" namespace xgboost::collective { diff --git a/src/collective/in_memory_handler.h b/src/collective/in_memory_handler.h index 7c3465d08b8b..0abcbc9c8768 100644 --- a/src/collective/in_memory_handler.h +++ b/src/collective/in_memory_handler.h @@ -117,9 +117,9 @@ class InMemoryHandler { std::int32_t rank, HandlerFunctor const& functor); std::int32_t world_size_{}; /// Number of workers. - std::int64_t received_{}; /// Number of calls received with the current sequence. + std::int64_t received_{}; /// Number of calls received with the current sequence. std::int64_t sent_{}; /// Number of calls completed with the current sequence. - std::string buffer_{}; /// A shared common buffer. + std::string buffer_{}; /// A shared common buffer. std::map aux_{}; /// A shared auxiliary map. uint64_t sequence_number_{}; /// Call sequence number. mutable std::mutex mutex_; /// Lock. diff --git a/src/collective/result.cc b/src/collective/result.cc index fd0914e4c11f..4095d54cd64c 100644 --- a/src/collective/result.cc +++ b/src/collective/result.cc @@ -6,6 +6,7 @@ #include // for path #include // for stringstream #include // for stack +#include // for string #include "xgboost/logging.h" diff --git a/src/collective/socket.cc b/src/collective/socket.cc index 7ae39cbc2365..5c33dfc7b688 100644 --- a/src/collective/socket.cc +++ b/src/collective/socket.cc @@ -8,8 +8,11 @@ #include // for size_t #include // for int32_t #include // for memcpy, memset +#include // for numeric_limits +#include // for string #include // for error_code, system_category #include // for sleep_for +#include // for move #include "xgboost/collective/poll_utils.h" // for PollHelper #include "xgboost/collective/result.h" // for Result diff --git a/src/collective/tracker.cc b/src/collective/tracker.cc index ad909f5e722b..c50aa3bbc73d 100644 --- a/src/collective/tracker.cc +++ b/src/collective/tracker.cc @@ -21,6 +21,7 @@ #include // for unique_ptr #include // for string #include // for move, forward +#include // for vector #include "../common/json_utils.h" #include "../common/threading_utils.h" // for NameThread @@ -265,103 +266,103 @@ Result RabitTracker::Bootstrap(std::vector* p_workers) { return Success(); }; - return std::async(std::launch::async, [this, handle_error, - init = TrackerInitThread{ - *GlobalConfigThreadLocalStore::Get()}] { - init(); - State state{this->n_workers_}; - - auto select_accept = [&](TCPSocket* sock, auto* addr) { - // accept with poll so that we can enable timeout and interruption. - rabit::utils::PollHelper poll; - auto rc = Success() << [&] { - std::lock_guard lock{listener_mu_}; - return listener_.NonBlocking(true); - } << [&] { - { - std::lock_guard lock{listener_mu_}; - poll.WatchRead(listener_); - } - if (state.running) { - // Don't timeout if the communicator group is up and running. - return poll.Poll(std::chrono::seconds{-1}); - } else { - // Have timeout for workers to bootstrap. - return poll.Poll(timeout_); - } - } << [&] { - // this->Stop() closes the socket with a lock. Therefore, when the accept returns - // due to shutdown, the state is still valid (closed). - return listener_.Accept(sock, addr); - }; - return rc; - }; - - while (state.ShouldContinue()) { - TCPSocket sock; - SockAddress addr; - this->ready_ = true; - auto rc = select_accept(&sock, &addr); - if (!rc.OK()) { - return Fail("Failed to accept connection.", this->Stop() + std::move(rc)); - } - - auto worker = WorkerProxy{n_workers_, std::move(sock), std::move(addr)}; - if (!worker.Status().OK()) { - LOG(WARNING) << "Failed to initialize worker proxy." << worker.Status().Report(); - continue; - } - switch (worker.Command()) { - case proto::CMD::kStart: { - if (state.running) { - // Something went wrong with one of the workers. It got disconnected without - // notice. - state.Error(); - rc = handle_error(worker); - if (!rc.OK()) { - return Fail("Failed to handle abort.", this->Stop() + std::move(rc)); + return std::async( + std::launch::async, + [this, handle_error, init = TrackerInitThread{*GlobalConfigThreadLocalStore::Get()}] { + init(); + State state{this->n_workers_}; + + auto select_accept = [&](TCPSocket* sock, auto* addr) { + // accept with poll so that we can enable timeout and interruption. + rabit::utils::PollHelper poll; + auto rc = Success() << [&] { + std::lock_guard lock{listener_mu_}; + return listener_.NonBlocking(true); + } << [&] { + { + std::lock_guard lock{listener_mu_}; + poll.WatchRead(listener_); } - } - - state.Start(std::move(worker)); - if (state.Ready()) { - rc = this->Bootstrap(&state.pending); - state.Bootstrap(); - } + if (state.running) { + // Don't timeout if the communicator group is up and running. + return poll.Poll(std::chrono::seconds{-1}); + } else { + // Have timeout for workers to bootstrap. + return poll.Poll(timeout_); + } + } << [&] { + // this->Stop() closes the socket with a lock. Therefore, when the accept returns + // due to shutdown, the state is still valid (closed). + return listener_.Accept(sock, addr); + }; + return rc; + }; + + while (state.ShouldContinue()) { + TCPSocket sock; + SockAddress addr; + this->ready_ = true; + auto rc = select_accept(&sock, &addr); if (!rc.OK()) { - return this->Stop() + std::move(rc); + return Fail("Failed to accept connection.", this->Stop() + std::move(rc)); } - continue; - } - case proto::CMD::kShutdown: { - if (state.during_restart) { - // The worker can still send shutdown after call to `std::exit`. + + auto worker = WorkerProxy{n_workers_, std::move(sock), std::move(addr)}; + if (!worker.Status().OK()) { + LOG(WARNING) << "Failed to initialize worker proxy." << worker.Status().Report(); continue; } - state.Shutdown(); - continue; - } - case proto::CMD::kError: { - if (state.during_restart) { - // Ignore further errors. - continue; + switch (worker.Command()) { + case proto::CMD::kStart: { + if (state.running) { + // Something went wrong with one of the workers. It got disconnected without + // notice. + state.Error(); + rc = handle_error(worker); + if (!rc.OK()) { + return Fail("Failed to handle abort.", this->Stop() + std::move(rc)); + } + } + + state.Start(std::move(worker)); + if (state.Ready()) { + rc = this->Bootstrap(&state.pending); + state.Bootstrap(); + } + if (!rc.OK()) { + return this->Stop() + std::move(rc); + } + continue; + } + case proto::CMD::kShutdown: { + if (state.during_restart) { + // The worker can still send shutdown after call to `std::exit`. + continue; + } + state.Shutdown(); + continue; + } + case proto::CMD::kError: { + if (state.during_restart) { + // Ignore further errors. + continue; + } + state.Error(); + rc = handle_error(worker); + continue; + } + case proto::CMD::kPrint: { + LOG(CONSOLE) << worker.Msg(); + continue; + } + case proto::CMD::kInvalid: + default: { + return Fail("Invalid command received.", this->Stop()); + } } - state.Error(); - rc = handle_error(worker); - continue; - } - case proto::CMD::kPrint: { - LOG(CONSOLE) << worker.Msg(); - continue; } - case proto::CMD::kInvalid: - default: { - return Fail("Invalid command received.", this->Stop()); - } - } - } - return this->Stop(); - }); + return this->Stop(); + }); } [[nodiscard]] Json RabitTracker::WorkerArgs() const { diff --git a/src/common/algorithm.h b/src/common/algorithm.h index 10d23d05cc08..4c3ffe62a7b7 100644 --- a/src/common/algorithm.h +++ b/src/common/algorithm.h @@ -3,11 +3,11 @@ */ #ifndef XGBOOST_COMMON_ALGORITHM_H_ #define XGBOOST_COMMON_ALGORITHM_H_ -#include // upper_bound, stable_sort, sort, max -#include // size_t -#include // less -#include // iterator_traits, distance -#include // vector +#include // upper_bound, stable_sort, sort, max +#include // size_t +#include // less +#include // iterator_traits, distance +#include // vector #include "numeric.h" // Iota #include "xgboost/context.h" // Context @@ -81,7 +81,9 @@ std::vector ArgSort(Context const *ctx, Iter begin, Iter end, Comp comp = s auto n = std::distance(begin, end); std::vector result(n); Iota(ctx, result.begin(), result.end(), 0); - auto op = [&](Idx const &l, Idx const &r) { return comp(begin[l], begin[r]); }; + auto op = [&](Idx const &l, Idx const &r) { + return comp(begin[l], begin[r]); + }; StableSort(ctx, result.begin(), result.end(), op); return result; } diff --git a/src/common/api_entry.h b/src/common/api_entry.h index df1fcd70467f..378125eb7c5f 100644 --- a/src/common/api_entry.h +++ b/src/common/api_entry.h @@ -3,8 +3,8 @@ */ #ifndef XGBOOST_COMMON_API_ENTRY_H_ #define XGBOOST_COMMON_API_ENTRY_H_ -#include // std::string -#include // std::vector +#include // std::string +#include // std::vector #include "xgboost/base.h" // GradientPair,bst_ulong #include "xgboost/predictor.h" // PredictionCacheEntry diff --git a/src/common/base64.h b/src/common/base64.h index 4c876b5f8ddd..907233d344c2 100644 --- a/src/common/base64.h +++ b/src/common/base64.h @@ -9,9 +9,11 @@ #define XGBOOST_COMMON_BASE64_H_ #include + #include #include #include + #include "./io.h" namespace xgboost { @@ -19,9 +21,7 @@ namespace common { /*! \brief buffer reader of the stream that allows you to get */ class StreamBufferReader { public: - explicit StreamBufferReader(size_t buffer_size) - :stream_(NULL), - read_len_(1), read_ptr_(1) { + explicit StreamBufferReader(size_t buffer_size) : stream_(NULL), read_len_(1), read_ptr_(1) { buffer_.resize(buffer_size); } /*! @@ -46,9 +46,7 @@ class StreamBufferReader { } } /*! \brief whether we are reaching the end of file */ - inline bool AtEnd(void) const { - return read_len_ == 0; - } + inline bool AtEnd(void) const { return read_len_ == 0; } private: /*! \brief the underlying stream */ @@ -64,28 +62,27 @@ class StreamBufferReader { /*! \brief namespace of base64 decoding and encoding table */ namespace base64 { const char DecodeTable[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 62, // '+' - 0, 0, 0, - 63, // '/' - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9' - 0, 0, 0, 0, 0, 0, 0, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, - 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z' - 0, 0, 0, 0, 0, 0, - 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, - 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z' + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 62, // '+' + 0, 0, 0, + 63, // '/' + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9' + 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z' + 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z' }; static const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; } // namespace base64 /*! \brief the stream that reads from base64, note we take from file pointers */ -class Base64InStream: public dmlc::Stream { +class Base64InStream : public dmlc::Stream { public: explicit Base64InStream(dmlc::Stream *fs) : reader_(256) { reader_.set_stream(fs); - num_prev = 0; tmp_ch = 0; + num_prev = 0; + tmp_ch = 0; } /*! * \brief initialize the stream position to beginning of next base64 stream @@ -98,15 +95,13 @@ class Base64InStream: public dmlc::Stream { } while (isspace(tmp_ch)); } /*! \brief whether current position is end of a base64 stream */ - inline bool IsEOF(void) const { - return num_prev == 0 && (tmp_ch == EOF || isspace(tmp_ch)); - } + inline bool IsEOF(void) const { return num_prev == 0 && (tmp_ch == EOF || isspace(tmp_ch)); } virtual size_t Read(void *ptr, size_t size) { using base64::DecodeTable; if (size == 0) return 0; // use tlen to record left size size_t tlen = size; - unsigned char *cptr = static_cast(ptr); + unsigned char *cptr = static_cast(ptr); // if anything left, load from previous buffered result if (num_prev != 0) { if (num_prev == 2) { @@ -117,13 +112,16 @@ class Base64InStream: public dmlc::Stream { num_prev = 0; } else { // assert tlen == 1 - *cptr++ = buf_prev[0]; --tlen; + *cptr++ = buf_prev[0]; + --tlen; buf_prev[0] = buf_prev[1]; num_prev = 1; } } else { // assert num_prev == 1 - *cptr++ = buf_prev[0]; --tlen; num_prev = 0; + *cptr++ = buf_prev[0]; + --tlen; + num_prev = 0; } } if (tlen == 0) return size; @@ -138,8 +136,9 @@ class Base64InStream: public dmlc::Stream { tmp_ch = reader_.GetChar(); CHECK(tmp_ch != EOF && !isspace(tmp_ch)) << "invalid base64 format"; nvalue |= DecodeTable[tmp_ch] << 12; - *cptr++ = (nvalue >> 16) & 0xFF; --tlen; - } + *cptr++ = (nvalue >> 16) & 0xFF; + --tlen; + } { // third byte tmp_ch = reader_.GetChar(); @@ -149,13 +148,13 @@ class Base64InStream: public dmlc::Stream { tmp_ch = reader_.GetChar(); CHECK(tmp_ch == '=') << "invalid base64 format"; tmp_ch = reader_.GetChar(); - CHECK(tmp_ch == EOF || isspace(tmp_ch)) - << "invalid base64 format"; + CHECK(tmp_ch == EOF || isspace(tmp_ch)) << "invalid base64 format"; break; } nvalue |= DecodeTable[tmp_ch] << 6; if (tlen) { - *cptr++ = (nvalue >> 8) & 0xFF; --tlen; + *cptr++ = (nvalue >> 8) & 0xFF; + --tlen; } else { buf_prev[num_prev++] = (nvalue >> 8) & 0xFF; } @@ -163,19 +162,18 @@ class Base64InStream: public dmlc::Stream { { // fourth byte tmp_ch = reader_.GetChar(); - CHECK(tmp_ch != EOF && !isspace(tmp_ch)) - << "invalid base64 format"; + CHECK(tmp_ch != EOF && !isspace(tmp_ch)) << "invalid base64 format"; if (tmp_ch == '=') { tmp_ch = reader_.GetChar(); - CHECK(tmp_ch == EOF || isspace(tmp_ch)) - << "invalid base64 format"; + CHECK(tmp_ch == EOF || isspace(tmp_ch)) << "invalid base64 format"; break; } nvalue |= DecodeTable[tmp_ch]; if (tlen) { - *cptr++ = nvalue & 0xFF; --tlen; + *cptr++ = nvalue & 0xFF; + --tlen; } else { - buf_prev[num_prev ++] = nvalue & 0xFF; + buf_prev[num_prev++] = nvalue & 0xFF; } } // get next char @@ -199,18 +197,17 @@ class Base64InStream: public dmlc::Stream { static const bool kStrictCheck = false; }; /*! \brief the stream that write to base64, note we take from file pointers */ -class Base64OutStream: public dmlc::Stream { +class Base64OutStream : public dmlc::Stream { public: - explicit Base64OutStream(dmlc::Stream *fp) : fp(fp) { - buf_top = 0; - } + explicit Base64OutStream(dmlc::Stream *fp) : fp(fp) { buf_top = 0; } virtual void Write(const void *ptr, size_t size) { using base64::EncodeTable; size_t tlen = size; - const unsigned char *cptr = static_cast(ptr); + const unsigned char *cptr = static_cast(ptr); while (tlen) { - while (buf_top < 3 && tlen != 0) { - buf[++buf_top] = *cptr++; --tlen; + while (buf_top < 3 && tlen != 0) { + buf[++buf_top] = *cptr++; + --tlen; } if (buf_top == 3) { // flush 4 bytes out diff --git a/src/common/bitfield.h b/src/common/bitfield.h index 08a385a2cf00..30c865de2bb3 100644 --- a/src/common/bitfield.h +++ b/src/common/bitfield.h @@ -93,8 +93,8 @@ struct BitFieldContainer { : bits_{bits.data()}, n_values_{bits.size()} {} BitFieldContainer(BitFieldContainer const& other) = default; BitFieldContainer(BitFieldContainer&& other) = default; - BitFieldContainer &operator=(BitFieldContainer const &that) = default; - BitFieldContainer &operator=(BitFieldContainer &&that) = default; + BitFieldContainer& operator=(BitFieldContainer const& that) = default; + BitFieldContainer& operator=(BitFieldContainer&& that) = default; XGBOOST_DEVICE auto Bits() { return common::Span{bits_, NumValues()}; } XGBOOST_DEVICE auto Bits() const { return common::Span{bits_, NumValues()}; } @@ -155,14 +155,14 @@ struct BitFieldContainer { value_type& value = Data()[pos_v.int_pos]; value_type set_bit = kOne << pos_v.bit_pos; using Type = typename dh::detail::AtomicDispatcher::Type; - atomicOr(reinterpret_cast(&value), set_bit); + atomicOr(reinterpret_cast(&value), set_bit); } __device__ void Clear(index_type pos) noexcept(true) { Pos pos_v = Direction::Shift(ToBitPos(pos)); value_type& value = Data()[pos_v.int_pos]; value_type clear_bit = ~(kOne << pos_v.bit_pos); using Type = typename dh::detail::AtomicDispatcher::Type; - atomicAnd(reinterpret_cast(&value), clear_bit); + atomicAnd(reinterpret_cast(&value), clear_bit); } #else void Set(index_type pos) noexcept(true) { @@ -238,9 +238,7 @@ struct RBitsPolicy : public BitFieldContainer> { using Pos = typename Container::Pos; using value_type = typename Container::value_type; // NOLINT - XGBOOST_DEVICE static Pos Shift(Pos pos) { - return pos; - } + XGBOOST_DEVICE static Pos Shift(Pos pos) { return pos; } }; // Format: BitField, underlying type @@ -278,6 +276,6 @@ inline std::uint32_t TrailingZeroBits(std::uint32_t value) { return detail::TrailingZeroBitsImpl(value); #endif // __GNUC__ } -} // namespace xgboost +} // namespace xgboost #endif // XGBOOST_COMMON_BITFIELD_H_ diff --git a/src/common/cache_manager.cc b/src/common/cache_manager.cc index dfd98c453604..85106296d662 100644 --- a/src/common/cache_manager.cc +++ b/src/common/cache_manager.cc @@ -7,16 +7,16 @@ #if defined(__x86_64__) -void RunCpuid(uint32_t eax, uint32_t ecx, uint32_t (& abcd)[4]) { +void RunCpuid(uint32_t eax, uint32_t ecx, uint32_t (&abcd)[4]) { #if defined(_MSC_VER) - __cpuidex(reinterpret_cast(abcd), eax, ecx); + __cpuidex(reinterpret_cast(abcd), eax, ecx); #else - uint32_t ebx = 0, edx = 0; - __asm__("cpuid" : "+b"(ebx), "+a"(eax), "+c"(ecx), "=d"(edx)); - abcd[0] = eax; - abcd[1] = ebx; - abcd[2] = ecx; - abcd[3] = edx; + uint32_t ebx = 0, edx = 0; + __asm__("cpuid" : "+b"(ebx), "+a"(eax), "+c"(ecx), "=d"(edx)); + abcd[0] = eax; + abcd[1] = ebx; + abcd[2] = ecx; + abcd[3] = edx; #endif } @@ -41,9 +41,9 @@ void RunCpuid(uint32_t eax, uint32_t ecx, uint32_t (& abcd)[4]) { #define _CPUID_VENDOR_ID_AMD 0x68747541 // Run CPUID and collect raw output. -void GetCacheInfo(int cache_num, int* type, int* level, int64_t* sets, - int* line_size, int* partitions, int* ways) { -// Leaf 0x0 returns Vendor ID in EBX, EDX, ECX +void GetCacheInfo(int cache_num, int* type, int* level, int64_t* sets, int* line_size, + int* partitions, int* ways) { + // Leaf 0x0 returns Vendor ID in EBX, EDX, ECX uint32_t vendor_reg[4]; RunCpuid(0, 0, vendor_reg); bool is_amd = (vendor_reg[1] == _CPUID_VENDOR_ID_AMD); @@ -56,12 +56,12 @@ void GetCacheInfo(int cache_num, int* type, int* level, int64_t* sets, const uint32_t ebx = abcd[1]; const uint32_t ecx = abcd[2]; // const uint32_t edx = abcd[3]; // Not used - *type = _CPUID_GET_TYPE(eax); - *level = _CPUID_GET_LEVEL(eax); - *sets = _CPUID_GET_SETS(ecx); - *line_size = _CPUID_GET_LINE_SIZE(ebx); - *partitions = _CPUID_GET_PARTITIONS(ebx); - *ways = _CPUID_GET_WAYS(ebx); + *type = _CPUID_GET_TYPE(eax); + *level = _CPUID_GET_LEVEL(eax); + *sets = _CPUID_GET_SETS(ecx); + *line_size = _CPUID_GET_LINE_SIZE(ebx); + *partitions = _CPUID_GET_PARTITIONS(ebx); + *ways = _CPUID_GET_WAYS(ebx); } constexpr int kCpuidTypeNull = 0; diff --git a/src/common/cache_manager.h b/src/common/cache_manager.h index a4eb0c196f0b..409596014039 100644 --- a/src/common/cache_manager.h +++ b/src/common/cache_manager.h @@ -4,8 +4,8 @@ #ifndef XGBOOST_COMMON_CACHE_MANAGER_H_ #define XGBOOST_COMMON_CACHE_MANAGER_H_ -#include // for int64_t #include +#include // for int64_t namespace xgboost::common { @@ -16,8 +16,8 @@ class CacheManager { private: constexpr static int64_t kUninitCache = -1; constexpr static int kMaxCacheSize = 4; - std::array cache_size_ = {kUninitCache, kUninitCache, - kUninitCache, kUninitCache}; + std::array cache_size_ = {kUninitCache, kUninitCache, kUninitCache, + kUninitCache}; constexpr static int64_t kDefaultL1Size = 32 * 1024; // 32KB constexpr static int64_t kDefaultL2Size = 1024 * 1024; // 1MB diff --git a/src/common/charconv.cc b/src/common/charconv.cc index 3114a90e33db..3c3342de5d1e 100644 --- a/src/common/charconv.cc +++ b/src/common/charconv.cc @@ -26,14 +26,15 @@ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. */ +#include "charconv.h" + #include #include #include -#include #include +#include #include "xgboost/logging.h" -#include "charconv.h" #if defined(_MSC_VER) #include @@ -57,20 +58,17 @@ namespace xgboost { namespace detail { static constexpr char kItoaLut[200] = { - '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', - '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', - '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0', '2', '1', '2', - '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', - '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', - '7', '3', '8', '3', '9', '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', - '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', '5', '0', '5', '1', '5', - '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', - '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', - '7', '6', '8', '6', '9', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', - '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', - '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', - '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', - '7', '9', '8', '9', '9'}; + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', '7', '0', '8', '0', + '9', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', + '1', '9', '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', + '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', + '3', '8', '3', '9', '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', + '7', '4', '8', '4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', + '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', + '6', '6', '7', '6', '8', '6', '9', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', + '7', '6', '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', + '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', + '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'}; constexpr uint32_t Tens(uint32_t n) { return n == 1 ? 10 : (Tens(n - 1) * 10); } @@ -84,7 +82,7 @@ struct UnsignedFloatBase10 { }; template -To BitCast(From&& from) { +To BitCast(From &&from) { static_assert(sizeof(From) == sizeof(To), "Bit cast doesn't change output size."); To t; std::memcpy(&t, &from, sizeof(To)); @@ -96,14 +94,13 @@ struct IEEE754 { static constexpr uint32_t kFloatBias = 127; static constexpr uint32_t kFloatExponentBits = 8; - static void Decode(float f, UnsignedFloatBase2* uf, bool* signbit); - static float Encode(UnsignedFloatBase2 const& uf, bool signbit); + static void Decode(float f, UnsignedFloatBase2 *uf, bool *signbit); + static float Encode(UnsignedFloatBase2 const &uf, bool signbit); static float Infinity(bool sign) { - uint32_t f = - ((static_cast(sign)) - << (IEEE754::kFloatExponentBits + IEEE754::kFloatMantissaBits)) | - (0xffu << IEEE754::kFloatMantissaBits); + uint32_t f = ((static_cast(sign)) + << (IEEE754::kFloatExponentBits + IEEE754::kFloatMantissaBits)) | + (0xffu << IEEE754::kFloatMantissaBits); float result = BitCast(f); return result; } @@ -115,12 +112,8 @@ struct UnsignedFloatBase2 { // inclusive, and can fit in a short if needed. uint32_t exponent; - bool Infinite() const { - return exponent == ((1u << IEEE754::kFloatExponentBits) - 1u); - } - bool Zero() const { - return mantissa == 0 && exponent == 0; - } + bool Infinite() const { return exponent == ((1u << IEEE754::kFloatExponentBits) - 1u); } + bool Zero() const { return mantissa == 0 && exponent == 0; } }; inline void IEEE754::Decode(float f, UnsignedFloatBase2 *uf, bool *signbit) { @@ -133,11 +126,10 @@ inline void IEEE754::Decode(float f, UnsignedFloatBase2 *uf, bool *signbit) { } inline float IEEE754::Encode(UnsignedFloatBase2 const &uf, bool signbit) { - uint32_t f = - ((((static_cast(signbit)) << IEEE754::kFloatExponentBits) | - static_cast(uf.exponent)) - << IEEE754::kFloatMantissaBits) | - uf.mantissa; + uint32_t f = ((((static_cast(signbit)) << IEEE754::kFloatExponentBits) | + static_cast(uf.exponent)) + << IEEE754::kFloatMantissaBits) | + uf.mantissa; return BitCast(f); } @@ -158,44 +150,35 @@ struct RyuPowLogUtils { // f2s_full_table.h uint32_t constexpr static kFloatPow5InvBitcount = 59; static constexpr uint64_t kFloatPow5InvSplit[55] = { - 576460752303423489u, 461168601842738791u, 368934881474191033u, - 295147905179352826u, 472236648286964522u, 377789318629571618u, - 302231454903657294u, 483570327845851670u, 386856262276681336u, - 309485009821345069u, 495176015714152110u, 396140812571321688u, - 316912650057057351u, 507060240091291761u, 405648192073033409u, - 324518553658426727u, 519229685853482763u, 415383748682786211u, - 332306998946228969u, 531691198313966350u, 425352958651173080u, - 340282366920938464u, 544451787073501542u, 435561429658801234u, - 348449143727040987u, 557518629963265579u, 446014903970612463u, - 356811923176489971u, 570899077082383953u, 456719261665907162u, - 365375409332725730u, 292300327466180584u, 467680523945888934u, - 374144419156711148u, 299315535325368918u, 478904856520590269u, - 383123885216472215u, 306499108173177772u, 490398573077084435u, - 392318858461667548u, 313855086769334039u, 502168138830934462u, - 401734511064747569u, 321387608851798056u, 514220174162876889u, - 411376139330301511u, 329100911464241209u, 526561458342785934u, - 421249166674228747u, 336999333339382998u, 539198933343012796u, - 431359146674410237u, 345087317339528190u, 552139707743245103u, - 441711766194596083u}; + 576460752303423489u, 461168601842738791u, 368934881474191033u, 295147905179352826u, + 472236648286964522u, 377789318629571618u, 302231454903657294u, 483570327845851670u, + 386856262276681336u, 309485009821345069u, 495176015714152110u, 396140812571321688u, + 316912650057057351u, 507060240091291761u, 405648192073033409u, 324518553658426727u, + 519229685853482763u, 415383748682786211u, 332306998946228969u, 531691198313966350u, + 425352958651173080u, 340282366920938464u, 544451787073501542u, 435561429658801234u, + 348449143727040987u, 557518629963265579u, 446014903970612463u, 356811923176489971u, + 570899077082383953u, 456719261665907162u, 365375409332725730u, 292300327466180584u, + 467680523945888934u, 374144419156711148u, 299315535325368918u, 478904856520590269u, + 383123885216472215u, 306499108173177772u, 490398573077084435u, 392318858461667548u, + 313855086769334039u, 502168138830934462u, 401734511064747569u, 321387608851798056u, + 514220174162876889u, 411376139330301511u, 329100911464241209u, 526561458342785934u, + 421249166674228747u, 336999333339382998u, 539198933343012796u, 431359146674410237u, + 345087317339528190u, 552139707743245103u, 441711766194596083u}; uint32_t constexpr static kFloatPow5Bitcount = 61; static constexpr uint64_t kFloatPow5Split[47] = { - 1152921504606846976u, 1441151880758558720u, 1801439850948198400u, - 2251799813685248000u, 1407374883553280000u, 1759218604441600000u, - 2199023255552000000u, 1374389534720000000u, 1717986918400000000u, - 2147483648000000000u, 1342177280000000000u, 1677721600000000000u, - 2097152000000000000u, 1310720000000000000u, 1638400000000000000u, - 2048000000000000000u, 1280000000000000000u, 1600000000000000000u, - 2000000000000000000u, 1250000000000000000u, 1562500000000000000u, - 1953125000000000000u, 1220703125000000000u, 1525878906250000000u, - 1907348632812500000u, 1192092895507812500u, 1490116119384765625u, - 1862645149230957031u, 1164153218269348144u, 1455191522836685180u, - 1818989403545856475u, 2273736754432320594u, 1421085471520200371u, - 1776356839400250464u, 2220446049250313080u, 1387778780781445675u, - 1734723475976807094u, 2168404344971008868u, 1355252715606880542u, - 1694065894508600678u, 2117582368135750847u, 1323488980084844279u, - 1654361225106055349u, 2067951531382569187u, 1292469707114105741u, - 1615587133892632177u, 2019483917365790221u}; + 1152921504606846976u, 1441151880758558720u, 1801439850948198400u, 2251799813685248000u, + 1407374883553280000u, 1759218604441600000u, 2199023255552000000u, 1374389534720000000u, + 1717986918400000000u, 2147483648000000000u, 1342177280000000000u, 1677721600000000000u, + 2097152000000000000u, 1310720000000000000u, 1638400000000000000u, 2048000000000000000u, + 1280000000000000000u, 1600000000000000000u, 2000000000000000000u, 1250000000000000000u, + 1562500000000000000u, 1953125000000000000u, 1220703125000000000u, 1525878906250000000u, + 1907348632812500000u, 1192092895507812500u, 1490116119384765625u, 1862645149230957031u, + 1164153218269348144u, 1455191522836685180u, 1818989403545856475u, 2273736754432320594u, + 1421085471520200371u, 1776356839400250464u, 2220446049250313080u, 1387778780781445675u, + 1734723475976807094u, 2168404344971008868u, 1355252715606880542u, 1694065894508600678u, + 2117582368135750847u, 1323488980084844279u, 1654361225106055349u, 2067951531382569187u, + 1292469707114105741u, 1615587133892632177u, 2019483917365790221u}; static uint32_t Pow5Factor(uint32_t value) noexcept(true) { uint32_t count = 0; @@ -239,15 +222,12 @@ struct RyuPowLogUtils { return static_cast(((static_cast(e)) * 1217359) >> 19); } - static int32_t CeilLog2Pow5(const int32_t e) { - return RyuPowLogUtils::Log2Pow5(e) + 1; - } + static int32_t CeilLog2Pow5(const int32_t e) { return RyuPowLogUtils::Log2Pow5(e) + 1; } /* * \brief Multiply 32-bit and 64-bit -> 128 bit, then access the higher bits. */ - static uint32_t MulShift(const uint32_t x, const uint64_t y, - const int32_t shift) noexcept(true) { + static uint32_t MulShift(const uint32_t x, const uint64_t y, const int32_t shift) noexcept(true) { // For 32-bit * 64-bit: x * y, it can be decomposed into: // // x * (y_high + y_low) = (x * y_high) + (x * y_low) @@ -311,8 +291,7 @@ struct RyuPowLogUtils { // greater than 10^1832. assert(expoent >= 0); assert(expoent <= 1 << 15); - return static_cast( - ((static_cast(expoent)) * 196742565691928ull) >> 48); + return static_cast(((static_cast(expoent)) * 196742565691928ull) >> 48); } }; @@ -321,11 +300,10 @@ constexpr uint64_t RyuPowLogUtils::kFloatPow5Split[47]; class PowerBaseComputer { private: - static uint8_t - ToDecimalBase(bool const accept_bounds, uint32_t const mantissa_low_shift, - MantissaInteval const base2, MantissaInteval *base10, - bool *mantissa_low_is_trailing_zeros, - bool *mantissa_out_is_trailing_zeros) noexcept(true) { + static uint8_t ToDecimalBase(bool const accept_bounds, uint32_t const mantissa_low_shift, + MantissaInteval const base2, MantissaInteval *base10, + bool *mantissa_low_is_trailing_zeros, + bool *mantissa_out_is_trailing_zeros) noexcept(true) { uint8_t last_removed_digit = 0; if (base2.exponent >= 0) { const uint32_t q = RyuPowLogUtils::Log10Pow2(base2.exponent); @@ -333,26 +311,20 @@ class PowerBaseComputer { const int32_t k = RyuPowLogUtils::kFloatPow5InvBitcount + RyuPowLogUtils::Pow5Bits(static_cast(q)) - 1; const int32_t i = -base2.exponent + static_cast(q) + k; - base10->mantissa_low = - RyuPowLogUtils::MulPow5InvDivPow2(base2.mantissa_low, q, i); - base10->mantissa_correct = - RyuPowLogUtils::MulPow5InvDivPow2(base2.mantissa_correct, q, i); - base10->mantissa_high = - RyuPowLogUtils::MulPow5InvDivPow2(base2.mantissa_high, q, i); + base10->mantissa_low = RyuPowLogUtils::MulPow5InvDivPow2(base2.mantissa_low, q, i); + base10->mantissa_correct = RyuPowLogUtils::MulPow5InvDivPow2(base2.mantissa_correct, q, i); + base10->mantissa_high = RyuPowLogUtils::MulPow5InvDivPow2(base2.mantissa_high, q, i); - if (q != 0 && - (base10->mantissa_high - 1) / 10 <= base10->mantissa_low / 10) { + if (q != 0 && (base10->mantissa_high - 1) / 10 <= base10->mantissa_low / 10) { // We need to know one removed digit even if we are not going to loop // below. We could use q = X - 1 above, except that would require 33 // bits for the result, and we've found that 32-bit arithmetic is // faster even on 64-bit machines. - const int32_t l = - RyuPowLogUtils::kFloatPow5InvBitcount + - RyuPowLogUtils::Pow5Bits(static_cast(q - 1)) - 1; + const int32_t l = RyuPowLogUtils::kFloatPow5InvBitcount + + RyuPowLogUtils::Pow5Bits(static_cast(q - 1)) - 1; last_removed_digit = static_cast( - RyuPowLogUtils::MulPow5InvDivPow2( - base2.mantissa_correct, q - 1, - -base2.exponent + static_cast(q) - 1 + l) % + RyuPowLogUtils::MulPow5InvDivPow2(base2.mantissa_correct, q - 1, + -base2.exponent + static_cast(q) - 1 + l) % 10); } if (q <= 9) { @@ -366,33 +338,29 @@ class PowerBaseComputer { *mantissa_low_is_trailing_zeros = RyuPowLogUtils::MultipleOfPowerOf5(base2.mantissa_low, q); } else { - base10->mantissa_high -= - RyuPowLogUtils::MultipleOfPowerOf5(base2.mantissa_high, q); + base10->mantissa_high -= RyuPowLogUtils::MultipleOfPowerOf5(base2.mantissa_high, q); } } } else { const uint32_t q = RyuPowLogUtils::Log10Pow5(-base2.exponent); base10->exponent = static_cast(q) + base2.exponent; const int32_t i = -base2.exponent - static_cast(q); - const int32_t k = - RyuPowLogUtils::Pow5Bits(i) - RyuPowLogUtils::kFloatPow5Bitcount; + const int32_t k = RyuPowLogUtils::Pow5Bits(i) - RyuPowLogUtils::kFloatPow5Bitcount; int32_t j = static_cast(q) - k; - base10->mantissa_correct = RyuPowLogUtils::MulPow5divPow2( - base2.mantissa_correct, static_cast(i), j); - base10->mantissa_high = RyuPowLogUtils::MulPow5divPow2( - base2.mantissa_high, static_cast(i), j); - base10->mantissa_low = RyuPowLogUtils::MulPow5divPow2( - base2.mantissa_low, static_cast(i), j); - - if (q != 0 && - (base10->mantissa_high - 1) / 10 <= base10->mantissa_low / 10) { + base10->mantissa_correct = + RyuPowLogUtils::MulPow5divPow2(base2.mantissa_correct, static_cast(i), j); + base10->mantissa_high = + RyuPowLogUtils::MulPow5divPow2(base2.mantissa_high, static_cast(i), j); + base10->mantissa_low = + RyuPowLogUtils::MulPow5divPow2(base2.mantissa_low, static_cast(i), j); + + if (q != 0 && (base10->mantissa_high - 1) / 10 <= base10->mantissa_low / 10) { j = static_cast(q) - 1 - - (RyuPowLogUtils::Pow5Bits(i + 1) - - RyuPowLogUtils::kFloatPow5Bitcount); - last_removed_digit = static_cast( - RyuPowLogUtils::MulPow5divPow2(base2.mantissa_correct, - static_cast(i + 1), j) % - 10); + (RyuPowLogUtils::Pow5Bits(i + 1) - RyuPowLogUtils::kFloatPow5Bitcount); + last_removed_digit = + static_cast(RyuPowLogUtils::MulPow5divPow2(base2.mantissa_correct, + static_cast(i + 1), j) % + 10); } if (q <= 1) { // {mantissa_out, mantissa_out_high, mantissa_out_low} is trailing zeros if @@ -419,13 +387,13 @@ class PowerBaseComputer { /* * \brief A varient of extended euclidean GCD algorithm. */ - static UnsignedFloatBase10 - ShortestRepresentation(bool mantissa_low_is_trailing_zeros, - bool mantissa_out_is_trailing_zeros, - uint8_t last_removed_digit, bool const accept_bounds, - MantissaInteval base10) noexcept(true) { - int32_t removed {0}; - uint32_t output {0}; + static UnsignedFloatBase10 ShortestRepresentation(bool mantissa_low_is_trailing_zeros, + bool mantissa_out_is_trailing_zeros, + uint8_t last_removed_digit, + bool const accept_bounds, + MantissaInteval base10) noexcept(true) { + int32_t removed{0}; + uint32_t output{0}; if (mantissa_low_is_trailing_zeros || mantissa_out_is_trailing_zeros) { // General case, which happens rarely (~4.0%). @@ -457,10 +425,9 @@ class PowerBaseComputer { } // We need to take mantissa_out + 1 if mantissa_out is outside bounds or we need to // round up. - output = base10.mantissa_correct + - ((base10.mantissa_correct == base10.mantissa_low && - (!accept_bounds || !mantissa_low_is_trailing_zeros)) || - last_removed_digit >= 5); + output = base10.mantissa_correct + ((base10.mantissa_correct == base10.mantissa_low && + (!accept_bounds || !mantissa_low_is_trailing_zeros)) || + last_removed_digit >= 5); } else { // Specialized for the common case (~96.0%). Percentages below are // relative to this. Loop iterations below (approximately): 0: 13.6%, @@ -476,8 +443,7 @@ class PowerBaseComputer { // We need to take mantissa_out + 1 if mantissa_out is outside bounds or we need to // round up. output = base10.mantissa_correct + - (base10.mantissa_correct == base10.mantissa_low || - last_removed_digit >= 5); + (base10.mantissa_correct == base10.mantissa_low || last_removed_digit >= 5); } const int32_t exp = base10.exponent + removed; @@ -493,8 +459,7 @@ class PowerBaseComputer { uint32_t mantissa_base2; if (f.exponent == 0) { // We subtract 2 so that the bounds computation has 2 additional bits. - base2_range.exponent = static_cast(1) - - static_cast(IEEE754::kFloatBias) - + base2_range.exponent = static_cast(1) - static_cast(IEEE754::kFloatBias) - static_cast(IEEE754::kFloatMantissaBits) - static_cast(2); static_assert(static_cast(1) - static_cast(IEEE754::kFloatBias) - @@ -503,8 +468,8 @@ class PowerBaseComputer { -151); mantissa_base2 = f.mantissa; } else { - base2_range.exponent = static_cast(f.exponent) - IEEE754::kFloatBias - - IEEE754::kFloatMantissaBits - 2; + base2_range.exponent = + static_cast(f.exponent) - IEEE754::kFloatBias - IEEE754::kFloatMantissaBits - 2; mantissa_base2 = (1u << IEEE754::kFloatMantissaBits) | f.mantissa; } const bool even = (mantissa_base2 & 1) == 0; @@ -527,10 +492,9 @@ class PowerBaseComputer { // Step 4: Find the shortest decimal representation in the interval of valid // representations. - auto out = ShortestRepresentation(mantissa_low_is_trailing_zeros, - mantissa_out_is_trailing_zeros, - last_removed_digit, - accept_bounds, base10_range); + auto out = + ShortestRepresentation(mantissa_low_is_trailing_zeros, mantissa_out_is_trailing_zeros, + last_removed_digit, accept_bounds, base10_range); return out; } }; @@ -664,7 +628,7 @@ class RyuPrinter { } }; -int32_t ToCharsFloatImpl(float f, char * const result) { +int32_t ToCharsFloatImpl(float f, char *const result) { // Step 1: Decode the floating-point number, and unify normalized and // subnormal cases. UnsignedFloatBase2 uf32; @@ -681,7 +645,6 @@ int32_t ToCharsFloatImpl(float f, char * const result) { return index; } - // ====================== Integer ================== // This is an implementation for base 10 inspired by the one in libstdc++v3. The general @@ -704,25 +667,25 @@ void ItoaUnsignedImpl(char *first, uint32_t length, uint64_t value) { first[0] = kItoaLut[num]; first[1] = kItoaLut[num + 1]; } else { - first[0]= '0' + value; + first[0] = '0' + value; } } constexpr uint32_t ShortestDigit10Impl(uint64_t value, uint32_t n) { // Should trigger tail recursion optimization. - return value < 10 ? n : - (value < Tens(2) ? n + 1 : - (value < Tens(3) ? n + 2 : - (value < Tens(4) ? n + 3 : - ShortestDigit10Impl(value / Tens(4), n + 4)))); + return value < 10 + ? n + : (value < Tens(2) + ? n + 1 + : (value < Tens(3) + ? n + 2 + : (value < Tens(4) ? n + 3 + : ShortestDigit10Impl(value / Tens(4), n + 4)))); } -constexpr uint32_t ShortestDigit10(uint64_t value) { - return ShortestDigit10Impl(value, 1); -} +constexpr uint32_t ShortestDigit10(uint64_t value) { return ShortestDigit10Impl(value, 1); } -to_chars_result ToCharsUnsignedImpl(char *first, char *last, - uint64_t const value) { +to_chars_result ToCharsUnsignedImpl(char *first, char *last, uint64_t const value) { const uint32_t output_len = ShortestDigit10(value); to_chars_result ret; if (XGBOOST_EXPECT(std::distance(first, last) == 0, false)) { @@ -742,8 +705,7 @@ to_chars_result ToCharsUnsignedImpl(char *first, char *last, * double table. But here we optimize the table size with float table instead. The * result is exactly the same. */ -from_chars_result FromCharFloatImpl(const char *buffer, const int len, - float *result) { +from_chars_result FromCharFloatImpl(const char *buffer, const int len, float *result) { if (len == 0) { return {buffer, std::errc::invalid_argument}; } @@ -817,9 +779,8 @@ from_chars_result FromCharFloatImpl(const char *buffer, const int len, if ((m10digits + exp_b10 <= -46) || (mantissa_b10 == 0)) { // Number is less than 1e-46, which should be rounded down to 0; return // +/-0.0. - uint32_t ieee = - (static_cast(signed_mantissa)) - << (IEEE754::kFloatExponentBits + IEEE754::kFloatMantissaBits); + uint32_t ieee = (static_cast(signed_mantissa)) + << (IEEE754::kFloatExponentBits + IEEE754::kFloatMantissaBits); *result = BitCast(ieee); return {}; } @@ -846,8 +807,7 @@ from_chars_result FromCharFloatImpl(const char *buffer, const int len, // // We use floor(log2(5^e10)) so that we get at least this many bits; better // to have an additional bit than to not have enough bits. - exp_b2 = RyuPowLogUtils::FloorLog2(mantissa_b10) + exp_b10 + - RyuPowLogUtils::Log2Pow5(exp_b10) - + exp_b2 = RyuPowLogUtils::FloorLog2(mantissa_b10) + exp_b10 + RyuPowLogUtils::Log2Pow5(exp_b10) - (IEEE754::kFloatMantissaBits + 1); // We now compute [m10 * 10^e10 / 2^e2] = [m10 * 5^e10 / 2^(e2-e10)]. @@ -864,13 +824,11 @@ from_chars_result FromCharFloatImpl(const char *buffer, const int len, // than e2. If e2 is less than e10, then the result must be exact. Otherwise // we use the existing multipleOfPowerOf2 function. trailing_zeros = - exp_b2 < exp_b10 || - (exp_b2 - exp_b10 < 32 && - RyuPowLogUtils::MultipleOfPowerOf2(mantissa_b10, exp_b2 - exp_b10)); + exp_b2 < exp_b10 || (exp_b2 - exp_b10 < 32 && + RyuPowLogUtils::MultipleOfPowerOf2(mantissa_b10, exp_b2 - exp_b10)); } else { exp_b2 = RyuPowLogUtils::FloorLog2(mantissa_b10) + exp_b10 - - RyuPowLogUtils::CeilLog2Pow5(-exp_b10) - - (IEEE754::kFloatMantissaBits + 1); + RyuPowLogUtils::CeilLog2Pow5(-exp_b10) - (IEEE754::kFloatMantissaBits + 1); // We now compute [m10 * 10^e10 / 2^e2] = [m10 / (5^(-e10) 2^(e2-e10))]. int j = exp_b2 - exp_b10 + RyuPowLogUtils::CeilLog2Pow5(-exp_b10) - 1 + @@ -887,17 +845,15 @@ from_chars_result FromCharFloatImpl(const char *buffer, const int len, // above, and we need to check whether 5^(-e10) divides (m10 * 2^(e10-e2)), // which is the case iff pow5(m10 * 2^(e10-e2)) = pow5(m10) >= -e10. trailing_zeros = - (exp_b2 < exp_b10 || - (exp_b2 - exp_b10 < 32 && RyuPowLogUtils::MultipleOfPowerOf2( - mantissa_b10, exp_b2 - exp_b10))) && + (exp_b2 < exp_b10 || (exp_b2 - exp_b10 < 32 && RyuPowLogUtils::MultipleOfPowerOf2( + mantissa_b10, exp_b2 - exp_b10))) && RyuPowLogUtils::MultipleOfPowerOf5(mantissa_b10, -exp_b10); } // Compute the final IEEE exponent. - uint32_t f_e2 = - std::max(static_cast(0), - static_cast(exp_b2 + IEEE754::kFloatBias + - RyuPowLogUtils::FloorLog2(mantissa_b2))); + uint32_t f_e2 = std::max( + static_cast(0), + static_cast(exp_b2 + IEEE754::kFloatBias + RyuPowLogUtils::FloorLog2(mantissa_b2))); if (f_e2 > 0xfe) { // Final IEEE exponent is larger than the maximum representable; return @@ -909,8 +865,8 @@ from_chars_result FromCharFloatImpl(const char *buffer, const int len, // We need to figure out how much we need to shift m2. The tricky part is that // we need to take the final IEEE exponent into account, so we need to reverse // the bias and also special-case the value 0. - int32_t shift = (f_e2 == 0 ? 1 : f_e2) - exp_b2 - IEEE754::kFloatBias - - IEEE754::kFloatMantissaBits; + int32_t shift = + (f_e2 == 0 ? 1 : f_e2) - exp_b2 - IEEE754::kFloatBias - IEEE754::kFloatMantissaBits; assert(shift >= 1); // We need to round up if the exact value is more than 0.5 above the value we @@ -922,8 +878,7 @@ from_chars_result FromCharFloatImpl(const char *buffer, const int len, // exponent ieee_e2 now. trailing_zeros &= (mantissa_b2 & ((1u << (shift - 1)) - 1)) == 0; // NOLINT uint32_t lastRemovedBit = (mantissa_b2 >> (shift - 1)) & 1; - bool roundup = (lastRemovedBit != 0) && - (!trailing_zeros || (((mantissa_b2 >> shift) & 1) != 0)); + bool roundup = (lastRemovedBit != 0) && (!trailing_zeros || (((mantissa_b2 >> shift) & 1) != 0)); uint32_t f_m2 = (mantissa_b2 >> shift) + roundup; assert(f_m2 <= (1u << (IEEE754::kFloatMantissaBits + 1))); diff --git a/src/common/charconv.h b/src/common/charconv.h index c37b0bd966a0..03d909a08a2d 100644 --- a/src/common/charconv.h +++ b/src/common/charconv.h @@ -9,16 +9,16 @@ #define XGBOOST_COMMON_CHARCONV_H_ #include -#include #include #include +#include #include "xgboost/logging.h" namespace xgboost { struct to_chars_result { // NOLINT - char* ptr; + char *ptr; std::errc ec; }; @@ -28,17 +28,16 @@ struct from_chars_result { // NOLINT }; namespace detail { -int32_t ToCharsFloatImpl(float f, char * const result); -to_chars_result ToCharsUnsignedImpl(char *first, char *last, - uint64_t const value); -from_chars_result FromCharFloatImpl(const char *buffer, const int len, - float *result); +int32_t ToCharsFloatImpl(float f, char *const result); +to_chars_result ToCharsUnsignedImpl(char *first, char *last, uint64_t const value); +from_chars_result FromCharFloatImpl(const char *buffer, const int len, float *result); } // namespace detail template struct NumericLimits; -template <> struct NumericLimits { +template <> +struct NumericLimits { // Unlike std::numeric_limit::max_digits10, which represents the **minimum** // length of base10 digits that are necessary to uniquely represent all distinct values. // This value is used to represent the maximum length. As sign bit occupies 1 character: @@ -46,7 +45,8 @@ template <> struct NumericLimits { static constexpr size_t kToCharsSize = 16; }; -template <> struct NumericLimits { +template <> +struct NumericLimits { // From llvm libcxx: numeric_limits::digits10 returns value less on 1 than desired for // unsigned numbers. For example, for 1-byte unsigned value digits10 is 2 (999 can not // be represented), so we need +1 here. @@ -55,9 +55,8 @@ template <> struct NumericLimits { 3; // +1 for minus, +1 for digits10, +1 for '\0' just to be safe. }; -inline to_chars_result to_chars(char *first, char *last, float value) { // NOLINT - if (XGBOOST_EXPECT(!(static_cast(last - first) >= - NumericLimits::kToCharsSize), +inline to_chars_result to_chars(char *first, char *last, float value) { // NOLINT + if (XGBOOST_EXPECT(!(static_cast(last - first) >= NumericLimits::kToCharsSize), false)) { return {first, std::errc::value_too_large}; } @@ -68,13 +67,13 @@ inline to_chars_result to_chars(char *first, char *last, float value) { // NOL if (XGBOOST_EXPECT(ret.ptr < last, true)) { ret.ec = std::errc(); } else { - ret.ec = std::errc::value_too_large; + ret.ec = std::errc::value_too_large; ret.ptr = last; } return ret; } -inline to_chars_result to_chars(char *first, char *last, int64_t value) { // NOLINT +inline to_chars_result to_chars(char *first, char *last, int64_t value) { // NOLINT if (XGBOOST_EXPECT(first == last, false)) { return {first, std::errc::value_too_large}; } @@ -92,12 +91,11 @@ inline to_chars_result to_chars(char *first, char *last, int64_t value) { // NOL return detail::ToCharsUnsignedImpl(first, last, unsigned_value); } -inline from_chars_result from_chars(const char *buffer, const char *end, // NOLINT - float &value) { // NOLINT - from_chars_result res = - detail::FromCharFloatImpl(buffer, std::distance(buffer, end), &value); +inline from_chars_result from_chars(const char *buffer, const char *end, // NOLINT + float &value) { // NOLINT + from_chars_result res = detail::FromCharFloatImpl(buffer, std::distance(buffer, end), &value); return res; } } // namespace xgboost -#endif // XGBOOST_COMMON_CHARCONV_H_ +#endif // XGBOOST_COMMON_CHARCONV_H_ diff --git a/src/common/common.cu b/src/common/common.cu index 958f93779308..9fb4bd2e1069 100644 --- a/src/common/common.cu +++ b/src/common/common.cu @@ -4,6 +4,8 @@ #include #include +#include + #include "common.h" namespace dh { diff --git a/src/common/common.h b/src/common/common.h index f4e6a697e742..2b59bd600601 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -138,24 +138,20 @@ class Range { return *this; } XGBOOST_DEVICE Iterator operator++(int) { - Iterator res {*this}; + Iterator res{*this}; i_ += step_; return res; } - XGBOOST_DEVICE bool operator==(const Iterator &other) const { - return i_ >= other.i_; - } - XGBOOST_DEVICE bool operator!=(const Iterator &other) const { - return i_ < other.i_; - } + XGBOOST_DEVICE bool operator==(const Iterator &other) const { return i_ >= other.i_; } + XGBOOST_DEVICE bool operator!=(const Iterator &other) const { return i_ < other.i_; } XGBOOST_DEVICE void Step(DifferenceType s) { step_ = s; } protected: XGBOOST_DEVICE explicit Iterator(DifferenceType start) : i_(start) {} - XGBOOST_DEVICE explicit Iterator(DifferenceType start, DifferenceType step) : - i_{start}, step_{step} {} + XGBOOST_DEVICE explicit Iterator(DifferenceType start, DifferenceType step) + : i_{start}, step_{step} {} private: int64_t i_; @@ -165,18 +161,14 @@ class Range { XGBOOST_DEVICE Iterator begin() const { return begin_; } // NOLINT XGBOOST_DEVICE Iterator end() const { return end_; } // NOLINT - XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end) - : begin_(begin), end_(end) {} - XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end, - DifferenceType step) + XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end) : begin_(begin), end_(end) {} + XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end, DifferenceType step) : begin_(begin, step), end_(end) {} - XGBOOST_DEVICE bool operator==(const Range& other) const { + XGBOOST_DEVICE bool operator==(const Range &other) const { return *begin_ == *other.begin_ && *end_ == *other.end_; } - XGBOOST_DEVICE bool operator!=(const Range& other) const { - return !(*this == other); - } + XGBOOST_DEVICE bool operator!=(const Range &other) const { return !(*this == other); } XGBOOST_DEVICE void Step(DifferenceType s) { begin_.Step(s); } @@ -205,7 +197,7 @@ inline void AssertNCCLSupport() { inline void AssertSYCLSupport() { #ifndef XGBOOST_USE_SYCL - LOG(FATAL) << "XGBoost version not compiled with SYCL support."; + LOG(FATAL) << "XGBoost version not compiled with SYCL support."; #endif // XGBOOST_USE_SYCL } diff --git a/src/common/compressed_iterator.h b/src/common/compressed_iterator.h index 766f8c01cfce..0df692ec72fc 100644 --- a/src/common/compressed_iterator.h +++ b/src/common/compressed_iterator.h @@ -20,16 +20,12 @@ namespace xgboost::common { using CompressedByteT = unsigned char; namespace detail { -inline void SetBit(CompressedByteT *byte, int bit_idx) { - *byte |= 1 << bit_idx; -} +inline void SetBit(CompressedByteT *byte, int bit_idx) { *byte |= 1 << bit_idx; } template inline T CheckBit(const T &byte, int bit_idx) { return byte & (1 << bit_idx); } -inline void ClearBit(CompressedByteT *byte, int bit_idx) { - *byte &= ~(1 << bit_idx); -} +inline void ClearBit(CompressedByteT *byte, int bit_idx) { *byte &= ~(1 << bit_idx); } inline constexpr int kPadding = 8; // Assign padding so we can read slightly off // the beginning of the array @@ -137,16 +133,15 @@ class CompressedBufferWriter { } #ifdef __CUDACC__ - __device__ void AtomicWriteSymbol - (CompressedByteT* buffer, uint64_t symbol, size_t offset) { + __device__ void AtomicWriteSymbol(CompressedByteT *buffer, uint64_t symbol, size_t offset) { size_t ibit_start = offset * symbol_bits_; size_t ibit_end = (offset + 1) * symbol_bits_ - 1; size_t ibyte_start = ibit_start / 8, ibyte_end = ibit_end / 8; symbol <<= 7 - ibit_end % 8; for (ptrdiff_t ibyte = ibyte_end; ibyte >= static_cast(ibyte_start); --ibyte) { - dh::AtomicOrByte(reinterpret_cast(buffer + detail::kPadding), - ibyte, symbol & 0xff); + dh::AtomicOrByte(reinterpret_cast(buffer + detail::kPadding), ibyte, + symbol & 0xff); symbol >>= 8; } } @@ -165,8 +160,8 @@ class CompressedBufferWriter { // Eject only full bytes size_t tmp_bytes = stored_bits / 8; for (size_t j = 0; j < tmp_bytes; j++) { - buffer[buffer_position] = static_cast( - tmp >> (stored_bits - (j + 1) * 8)); + buffer[buffer_position] = + static_cast(tmp >> (stored_bits - (j + 1) * 8)); buffer_position++; } stored_bits -= tmp_bytes * 8; @@ -179,16 +174,13 @@ class CompressedBufferWriter { } // Eject all bytes - int tmp_bytes = - static_cast(std::ceil(static_cast(stored_bits) / 8)); + int tmp_bytes = static_cast(std::ceil(static_cast(stored_bits) / 8)); for (int j = 0; j < tmp_bytes; j++) { int shift_bits = static_cast(stored_bits) - (j + 1) * 8; if (shift_bits >= 0) { - buffer[buffer_position] = - static_cast(tmp >> shift_bits); + buffer[buffer_position] = static_cast(tmp >> shift_bits); } else { - buffer[buffer_position] = - static_cast(tmp << std::abs(shift_bits)); + buffer[buffer_position] = static_cast(tmp << std::abs(shift_bits)); } buffer_position++; } @@ -205,9 +197,9 @@ class CompressedBufferWriter { template class CompressedIterator { public: - typedef T value_type; // NOLINT - typedef value_type *pointer; // NOLINT - typedef value_type reference; // NOLINT + typedef T value_type; // NOLINT + typedef value_type *pointer; // NOLINT + typedef value_type reference; // NOLINT private: CompressedByteT const *XGBOOST_RESTRICT buffer_{nullptr}; @@ -281,9 +273,9 @@ class CompressedIterator { template class DoubleCompressedIter { public: - using value_type = OutT; // NOLINT - using pointer = value_type *; // NOLINT - using reference = value_type; // NOLINT + using value_type = OutT; // NOLINT + using pointer = value_type *; // NOLINT + using reference = value_type; // NOLINT private: using BufT = CompressedByteT const *; diff --git a/src/common/cuda_stream_pool.h b/src/common/cuda_stream_pool.h index a3943ce00941..08b63c19e066 100644 --- a/src/common/cuda_stream_pool.h +++ b/src/common/cuda_stream_pool.h @@ -6,7 +6,7 @@ #include // for size_t #include // for vector -#include "cuda_stream.h" // for StreamRef, Stream +#include "cuda_stream.h" // for StreamRef, Stream namespace xgboost::curt { // rmm cuda_stream_pool diff --git a/src/common/device_compression.cu b/src/common/device_compression.cu index 287a7695f80a..575fe11f7866 100644 --- a/src/common/device_compression.cu +++ b/src/common/device_compression.cu @@ -10,8 +10,8 @@ #include // for uint8_t, uint32_t, int32_t #include // for shared_ptr +#include "cuda_stream.h" // for StreamRef #include "device_compression.cuh" -#include "cuda_stream.h" // for StreamRef #include "device_helpers.cuh" // for MemcpyBatchAsync #include "xgboost/span.h" // for Span @@ -410,9 +410,8 @@ void DecompressSnappy(curt::StreamRef stream, SnappyDecomprMgr const& mgr, } [[nodiscard]] common::RefResourceView CoalesceCompressedBuffersToHost( - curt::StreamRef stream, std::shared_ptr pool, - CuMemParams const& in_params, dh::DeviceUVector const& in_buf, - CuMemParams* p_out) { + curt::StreamRef stream, std::shared_ptr pool, CuMemParams const& in_params, + dh::DeviceUVector const& in_buf, CuMemParams* p_out) { std::size_t n_total_act_bytes = in_params.TotalSrcActBytes(); std::size_t n_total_bytes = in_params.TotalSrcBytes(); if (n_total_bytes == 0) { diff --git a/src/common/device_vector.cu b/src/common/device_vector.cu index c82c6c15890e..c48b97b43a90 100644 --- a/src/common/device_vector.cu +++ b/src/common/device_vector.cu @@ -1,7 +1,10 @@ /** * Copyright 2017-2024, XGBoost contributors */ +#include // for make_unique #include // for accumulate +#include // for string +#include // for move #include "../collective/communicator-inl.h" // for GetRank #include "common.h" // for HumanMemUnit diff --git a/src/common/error_msg.cc b/src/common/error_msg.cc index 0eb215f95f40..5f4919156181 100644 --- a/src/common/error_msg.cc +++ b/src/common/error_msg.cc @@ -5,6 +5,7 @@ #include // for call_once, once_flag #include // for stringstream +#include // for string #include // for error_code, system_category #include "../collective/communicator-inl.h" // for GetRank @@ -75,7 +76,8 @@ This warning will only be shown once. void CheckOldNccl(std::int32_t major, std::int32_t minor, std::int32_t patch) { auto msg = [&] { std::stringstream ss; - ss << "NCCL version too old: " << "(" << major << "." << minor << "." << patch << ")" + ss << "NCCL version too old: " + << "(" << major << "." << minor << "." << patch << ")" << ". Install NCCL >= 2.23.4 ."; return ss.str(); }; diff --git a/src/common/group_data.h b/src/common/group_data.h index 3a51c6547565..27b2816ff5c8 100644 --- a/src/common/group_data.h +++ b/src/common/group_data.h @@ -14,10 +14,10 @@ #ifndef XGBOOST_COMMON_GROUP_DATA_H_ #define XGBOOST_COMMON_GROUP_DATA_H_ -#include -#include #include +#include #include +#include #include "xgboost/base.h" @@ -29,7 +29,7 @@ namespace common { * \tparam SizeType type of the index range holder * \tparam is_row_major bool value helps to reduce memory for row major */ -template +template class ParallelGroupBuilder { public: /** @@ -42,12 +42,9 @@ class ParallelGroupBuilder { * starting from. This saves considerable amounts of time/memory when * incrementaly building. */ - ParallelGroupBuilder(std::vector *p_rptr, - std::vector *p_data, + ParallelGroupBuilder(std::vector *p_rptr, std::vector *p_data, size_t base_row_offset = 0) - : rptr_(*p_rptr), - data_(*p_data), - base_row_offset_(base_row_offset) {} + : rptr_(*p_rptr), data_(*p_data), base_row_offset_(base_row_offset) {} /*! * \brief step 1: initialize the helper, with hint of number keys @@ -64,8 +61,8 @@ class ParallelGroupBuilder { const size_t thread_size = is_row_major ? thread_displacement_ : full_size; thread_rptr_[i].resize(thread_size, 0); } - const size_t last_thread_size = is_row_major ? (full_size - (nthread - 1)*thread_displacement_) - : full_size; + const size_t last_thread_size = + is_row_major ? (full_size - (nthread - 1) * thread_displacement_) : full_size; thread_rptr_[nthread - 1].resize(last_thread_size, 0); } @@ -77,7 +74,7 @@ class ParallelGroupBuilder { */ void AddBudget(std::size_t key, int threadid, SizeType nelem = 1) { std::vector &trptr = thread_rptr_[threadid]; - size_t offset_key = is_row_major ? (key - base_row_offset_ - threadid*thread_displacement_) + size_t offset_key = is_row_major ? (key - base_row_offset_ - threadid * thread_displacement_) : (key - base_row_offset_); if (trptr.size() < offset_key + 1) { trptr.resize(offset_key + 1, 0); @@ -124,10 +121,8 @@ class ParallelGroupBuilder { for (std::size_t i = base_row_offset_; i + 1 < rptr_.size(); ++i) { for (std::size_t tid = 0; tid < thread_rptr_.size(); ++tid) { std::vector &trptr = thread_rptr_[tid]; - if (i < trptr.size() + - base_row_offset_) { // i^th row is assigned for this thread - std::size_t thread_count = - trptr[i - base_row_offset_]; // how many entries in this row + if (i < trptr.size() + base_row_offset_) { // i^th row is assigned for this thread + std::size_t thread_count = trptr[i - base_row_offset_]; // how many entries in this row trptr[i - base_row_offset_] = count + rptr_.back(); count += thread_count; } @@ -146,7 +141,7 @@ class ParallelGroupBuilder { * \param value The value to be pushed to the group. * \param threadid the id of thread that calls this function */ - void Push(std::size_t key, ValueType&& value, int threadid) { + void Push(std::size_t key, ValueType &&value, int threadid) { size_t offset_key = is_row_major ? (key - base_row_offset_ - threadid * thread_displacement_) : (key - base_row_offset_); SizeType &rp = thread_rptr_[threadid][offset_key]; diff --git a/src/common/hist_util.cc b/src/common/hist_util.cc index 3a9cd4bdf61d..0e28d1af52f3 100644 --- a/src/common/hist_util.cc +++ b/src/common/hist_util.cc @@ -6,6 +6,8 @@ #include +#include +#include #include #include "../data/adapter.h" // for SparsePageAdapterBatch @@ -17,18 +19,18 @@ #include "xgboost/data.h" // for SparsePage, SortedCSCPage #if defined(XGBOOST_MM_PREFETCH_PRESENT) - #include - #define PREFETCH_READ_T0(addr) _mm_prefetch(reinterpret_cast(addr), _MM_HINT_T0) +#include +#define PREFETCH_READ_T0(addr) _mm_prefetch(reinterpret_cast(addr), _MM_HINT_T0) #elif defined(XGBOOST_BUILTIN_PREFETCH_PRESENT) - #define PREFETCH_READ_T0(addr) __builtin_prefetch(reinterpret_cast(addr), 0, 3) +#define PREFETCH_READ_T0(addr) __builtin_prefetch(reinterpret_cast(addr), 0, 3) #else // no SW pre-fetching available; PREFETCH_READ_T0 is no-op - #define PREFETCH_READ_T0(addr) do {} while (0) +#define PREFETCH_READ_T0(addr) \ + do { \ + } while (0) #endif // defined(XGBOOST_MM_PREFETCH_PRESENT) namespace xgboost::common { -HistogramCuts::HistogramCuts() { - cut_ptrs_.HostVector().emplace_back(0); -} +HistogramCuts::HistogramCuts() { cut_ptrs_.HostVector().emplace_back(0); } void HistogramCuts::Save(common::AlignedFileWriteStream *fo) const { auto const &ptrs = this->Ptrs(); @@ -75,10 +77,7 @@ HistogramCuts SketchOnDMatrix(Context const *ctx, DMatrix *m, bst_bin_t max_bins } container.MakeCuts(ctx, m->Info(), &out); } else { - SortedSketchContainer container{ctx, - max_bins, - m->Info().feature_types.ConstHostSpan(), - reduced, + SortedSketchContainer container{ctx, max_bins, m->Info().feature_types.ConstHostSpan(), reduced, HostSketchContainer::UseGroup(info)}; for (auto const &page : m->GetBatches(ctx)) { container.PushColPage(page, info, hessian); @@ -118,9 +117,9 @@ void CopyHist(GHistRow dst, const GHistRow src, size_t begin, size_t end) { */ void SubtractionHist(GHistRow dst, const GHistRow src1, const GHistRow src2, size_t begin, size_t end) { - double* pdst = reinterpret_cast(dst.data()); - const double* psrc1 = reinterpret_cast(src1.data()); - const double* psrc2 = reinterpret_cast(src2.data()); + double *pdst = reinterpret_cast(dst.data()); + const double *psrc1 = reinterpret_cast(src1.data()); + const double *psrc2 = reinterpret_cast(src2.data()); for (size_t i = 2 * begin; i < 2 * end; ++i) { pdst[i] = psrc1[i] - psrc2[i]; @@ -134,13 +133,10 @@ struct Prefetch { private: static constexpr size_t kNoPrefetchSize = - kPrefetchOffset + kCacheLineSize / - sizeof(decltype(GHistIndexMatrix::row_ptr)::value_type); + kPrefetchOffset + kCacheLineSize / sizeof(decltype(GHistIndexMatrix::row_ptr)::value_type); public: - static size_t NoPrefetchSize(size_t rows) { - return std::min(rows, kNoPrefetchSize); - } + static size_t NoPrefetchSize(size_t rows) { return std::min(rows, kNoPrefetchSize); } template static constexpr size_t GetPrefetchStep() { @@ -156,9 +152,7 @@ struct RuntimeFlags { const BinTypeSize bin_type_size; }; -template class GHistBuildingManager { public: @@ -192,7 +186,7 @@ class GHistBuildingManager { * and forward the call there. */ template - static void DispatchAndExecute(const RuntimeFlags& flags, Fn&& fn) { + static void DispatchAndExecute(const RuntimeFlags &flags, Fn &&fn) { if (flags.first_page != kFirstPage) { SetFirstPage::Type::DispatchAndExecute(flags, std::forward(fn)); } else if (flags.read_by_column != kReadByColumn) { @@ -247,22 +241,19 @@ void RowsWiseBuildHistKernel(Span gpair, Span gpair, Span gpair, Span gpair, Span gpair, Span row template void BuildHist(Span gpair, Span row_indices, const GHistIndexMatrix &gmat, GHistRow hist, bool read_by_column) { - bool first_page = gmat.base_rowid == 0;; + bool first_page = gmat.base_rowid == 0; auto bin_type_size = gmat.index.GetBinTypeSize(); GHistBuildingManager::DispatchAndExecute( @@ -380,10 +371,8 @@ void BuildHist(Span gpair, Span row_indices } template void BuildHist(Span gpair, Span row_indices, - const GHistIndexMatrix &gmat, GHistRow hist, - bool read_by_column); + const GHistIndexMatrix &gmat, GHistRow hist, bool read_by_column); template void BuildHist(Span gpair, Span row_indices, - const GHistIndexMatrix &gmat, GHistRow hist, - bool read_by_column); + const GHistIndexMatrix &gmat, GHistRow hist, bool read_by_column); } // namespace xgboost::common diff --git a/src/common/hist_util.cu b/src/common/hist_util.cu index 395bc3b0d13c..360b28815e1e 100644 --- a/src/common/hist_util.cu +++ b/src/common/hist_util.cu @@ -12,6 +12,7 @@ #include #include +#include #include // for size_t #include #include @@ -44,8 +45,8 @@ size_t RequiredSampleCuts(bst_idx_t num_rows, bst_feature_t num_columns, size_t return result; } -size_t RequiredMemory(bst_idx_t num_rows, bst_feature_t num_columns, size_t nnz, - size_t num_bins, bool with_weights) { +size_t RequiredMemory(bst_idx_t num_rows, bst_feature_t num_columns, size_t nnz, size_t num_bins, + bool with_weights) { size_t peak = 0; // 0. Allocate cut pointer in quantile container by increasing: n_columns + 1 size_t total = (num_columns + 1) * sizeof(SketchContainer::OffsetT); @@ -71,12 +72,13 @@ size_t RequiredMemory(bst_idx_t num_rows, bst_feature_t num_columns, size_t nnz, // 9. Allocate final cut values, min values, cut ptrs: std::min(rows, bins + 1) * // n_columns + n_columns + n_columns + 1 total += std::min(num_rows, num_bins) * num_columns * sizeof(float); - total += num_columns * - sizeof(std::remove_reference_t().MinValues())>::value_type); - total += (num_columns + 1) * - sizeof(std::remove_reference_t().Ptrs())>::value_type); + total += + num_columns * + sizeof( + std::remove_reference_t().MinValues())>::value_type); + total += + (num_columns + 1) * + sizeof(std::remove_reference_t().Ptrs())>::value_type); peak = std::max(peak, total); return peak; diff --git a/src/common/hist_util.h b/src/common/hist_util.h index 58129309ab18..5efdd13d2b2a 100644 --- a/src/common/hist_util.h +++ b/src/common/hist_util.h @@ -88,9 +88,9 @@ class HistogramCuts { } [[nodiscard]] bst_feature_t NumFeatures() const { return this->cut_ptrs_.Size() - 1; } - std::vector const& Ptrs() const { return cut_ptrs_.ConstHostVector(); } - std::vector const& Values() const { return cut_values_.ConstHostVector(); } - std::vector const& MinValues() const { return min_vals_.ConstHostVector(); } + std::vector const& Ptrs() const { return cut_ptrs_.ConstHostVector(); } + std::vector const& Values() const { return cut_values_.ConstHostVector(); } + std::vector const& MinValues() const { return min_vals_.ConstHostVector(); } [[nodiscard]] bool HasCategorical() const { return has_categorical_; } [[nodiscard]] float MaxCategory() const { return max_cat_; } @@ -491,7 +491,7 @@ class ParallelGHistBuilder { CHECK_EQ(nodes, targeted_hists.size()); - nodes_ = nodes; + nodes_ = nodes; nthreads_ = nthreads; MatchThreadsToNodes(space); @@ -556,11 +556,11 @@ class ParallelGHistBuilder { for (size_t tid = 0; tid < nthreads_; ++tid) { size_t begin = chunck_size * tid; - size_t end = std::min(begin + chunck_size, space_size); + size_t end = std::min(begin + chunck_size, space_size); if (begin < space_size) { size_t nid_begin = space.GetFirstDimension(begin); - size_t nid_end = space.GetFirstDimension(end-1); + size_t nid_end = space.GetFirstDimension(end - 1); for (size_t nid = nid_begin; nid <= nid_end; ++nid) { // true - means thread 'tid' will work to compute partial hist for node 'nid' @@ -635,8 +635,8 @@ class ParallelGHistBuilder { /*! \brief Contains histograms for final results */ std::vector targeted_hists_; /*! - * \brief map pair {tid, nid} to index of allocated histogram from hist_buffer_ and targeted_hists_, - * -1 is reserved for targeted_hists_ + * \brief map pair {tid, nid} to index of allocated histogram from hist_buffer_ and + * targeted_hists_, -1 is reserved for targeted_hists_ */ std::map, int> tid_nid_to_hist_; }; diff --git a/src/common/host_device_vector.cc b/src/common/host_device_vector.cc index a1201258da49..99dc33b7a501 100644 --- a/src/common/host_device_vector.cc +++ b/src/common/host_device_vector.cc @@ -6,13 +6,18 @@ // dummy implementation of HostDeviceVector in case CUDA is not used +#include "xgboost/host_device_vector.h" + #include #include + +#include #include #include #include +#include + #include "xgboost/tree_model.h" -#include "xgboost/host_device_vector.h" namespace xgboost { @@ -20,12 +25,10 @@ template struct HostDeviceVectorImpl { explicit HostDeviceVectorImpl(size_t size, T v) : data_h_(size, v) {} HostDeviceVectorImpl(std::initializer_list init) : data_h_(init) {} - explicit HostDeviceVectorImpl(std::vector init) : data_h_(std::move(init)) {} + explicit HostDeviceVectorImpl(std::vector init) : data_h_(std::move(init)) {} HostDeviceVectorImpl(HostDeviceVectorImpl&& that) : data_h_(std::move(that.data_h_)) {} - void Swap(HostDeviceVectorImpl &other) { - data_h_.swap(other.data_h_); - } + void Swap(HostDeviceVectorImpl& other) { data_h_.swap(other.data_h_); } std::vector& Vec() { return data_h_; } @@ -34,20 +37,17 @@ struct HostDeviceVectorImpl { }; template -HostDeviceVector::HostDeviceVector(size_t size, T v, DeviceOrd) - : impl_(nullptr) { +HostDeviceVector::HostDeviceVector(size_t size, T v, DeviceOrd) : impl_(nullptr) { impl_ = new HostDeviceVectorImpl(size, v); } template -HostDeviceVector::HostDeviceVector(std::initializer_list init, DeviceOrd) - : impl_(nullptr) { +HostDeviceVector::HostDeviceVector(std::initializer_list init, DeviceOrd) : impl_(nullptr) { impl_ = new HostDeviceVectorImpl(init); } template -HostDeviceVector::HostDeviceVector(const std::vector& init, DeviceOrd) - : impl_(nullptr) { +HostDeviceVector::HostDeviceVector(const std::vector& init, DeviceOrd) : impl_(nullptr) { impl_ = new HostDeviceVectorImpl(init); } @@ -58,7 +58,9 @@ HostDeviceVector::HostDeviceVector(HostDeviceVector&& that) { template HostDeviceVector& HostDeviceVector::operator=(HostDeviceVector&& that) { - if (this == &that) { return *this; } + if (this == &that) { + return *this; + } std::unique_ptr> new_impl( new HostDeviceVectorImpl(std::move(*that.impl_))); @@ -79,13 +81,19 @@ GPUAccess HostDeviceVector::DeviceAccess() const { } template -size_t HostDeviceVector::Size() const { return impl_->Vec().size(); } +size_t HostDeviceVector::Size() const { + return impl_->Vec().size(); +} template -DeviceOrd HostDeviceVector::Device() const { return DeviceOrd::CPU(); } +DeviceOrd HostDeviceVector::Device() const { + return DeviceOrd::CPU(); +} template -T* HostDeviceVector::DevicePointer() { return nullptr; } +T* HostDeviceVector::DevicePointer() { + return nullptr; +} template const T* HostDeviceVector::ConstDevicePointer() const { @@ -103,7 +111,9 @@ common::Span HostDeviceVector::ConstDeviceSpan() const { } template -std::vector& HostDeviceVector::HostVector() { return impl_->Vec(); } +std::vector& HostDeviceVector::HostVector() { + return impl_->Vec(); +} template const std::vector& HostDeviceVector::ConstHostVector() const { @@ -179,7 +189,7 @@ template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; -template class HostDeviceVector; // bst_node_t +template class HostDeviceVector; // bst_node_t template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; diff --git a/src/common/host_device_vector.cu b/src/common/host_device_vector.cu index d492285cb01a..780e64d495b1 100644 --- a/src/common/host_device_vector.cu +++ b/src/common/host_device_vector.cu @@ -6,6 +6,9 @@ #include #include // for size_t #include +#include +#include +#include #include "cuda_stream.h" // for DefaultStream #include "device_helpers.cuh" @@ -19,9 +22,7 @@ namespace xgboost { // the handler to call instead of cudaSetDevice; only used for testing static void (*cudaSetDeviceHandler)(int) = nullptr; // NOLINT -void SetCudaSetDeviceHandler(void (*handler)(int)) { - cudaSetDeviceHandler = handler; -} +void SetCudaSetDeviceHandler(void (*handler)(int)) { cudaSetDeviceHandler = handler; } template class HostDeviceVectorImpl { @@ -48,11 +49,11 @@ class HostDeviceVectorImpl { } } - HostDeviceVectorImpl(HostDeviceVectorImpl&& that) : - device_{that.device_}, - data_h_{std::move(that.data_h_)}, - data_d_{std::move(that.data_d_)}, - gpu_access_{that.gpu_access_} {} + HostDeviceVectorImpl(HostDeviceVectorImpl&& that) + : device_{that.device_}, + data_h_{std::move(that.data_h_)}, + data_d_{std::move(that.data_d_)}, + gpu_access_{that.gpu_access_} {} ~HostDeviceVectorImpl() { if (device_.IsCUDA()) { @@ -157,7 +158,9 @@ class HostDeviceVectorImpl { } void SetDevice(DeviceOrd device) { - if (device_ == device) { return; } + if (device_ == device) { + return; + } if (device_.IsCUDA()) { LazySyncHost(GPUAccess::kNone); } @@ -192,21 +195,27 @@ class HostDeviceVectorImpl { } void LazySyncHost(GPUAccess access) { - if (HostCanAccess(access)) { return; } + if (HostCanAccess(access)) { + return; + } if (HostCanRead()) { // data is present, just need to deny access to the device gpu_access_ = access; return; } gpu_access_ = access; - if (data_h_.size() != data_d_->size()) { data_h_.resize(data_d_->size()); } + if (data_h_.size() != data_d_->size()) { + data_h_.resize(data_d_->size()); + } SetDevice(); dh::safe_cuda(cudaMemcpy(data_h_.data(), data_d_->data(), data_d_->size() * sizeof(T), cudaMemcpyDeviceToHost)); } void LazySyncDevice(GPUAccess access) { - if (DeviceCanAccess(access)) { return; } + if (DeviceCanAccess(access)) { + return; + } if (DeviceCanRead()) { // deny read to the host gpu_access_ = access; @@ -256,7 +265,9 @@ class HostDeviceVectorImpl { } void LazyResizeDevice(size_t new_size) { - if (data_d_ && new_size == data_d_->size()) { return; } + if (data_d_ && new_size == data_d_->size()) { + return; + } SetDevice(); data_d_->resize(new_size); } @@ -275,7 +286,7 @@ class HostDeviceVectorImpl { } }; -template +template HostDeviceVector::HostDeviceVector(size_t size, T v, DeviceOrd device) : impl_(new HostDeviceVectorImpl(size, v, device)) {} @@ -293,7 +304,9 @@ HostDeviceVector::HostDeviceVector(HostDeviceVector&& other) template HostDeviceVector& HostDeviceVector::operator=(HostDeviceVector&& other) { - if (this == &other) { return *this; } + if (this == &other) { + return *this; + } std::unique_ptr> new_impl( new HostDeviceVectorImpl(std::move(*other.impl_))); @@ -309,7 +322,9 @@ HostDeviceVector::~HostDeviceVector() { } template -size_t HostDeviceVector::Size() const { return impl_->Size(); } +size_t HostDeviceVector::Size() const { + return impl_->Size(); +} template DeviceOrd HostDeviceVector::Device() const { @@ -362,7 +377,9 @@ void HostDeviceVector::Extend(HostDeviceVector const& other) { } template -std::vector& HostDeviceVector::HostVector() { return impl_->HostVector(); } +std::vector& HostDeviceVector::HostVector() { + return impl_->HostVector(); +} template const std::vector& HostDeviceVector::ConstHostVector() const { @@ -415,7 +432,7 @@ template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; -template class HostDeviceVector; // bst_node_t +template class HostDeviceVector; // bst_node_t template class HostDeviceVector; template class HostDeviceVector; template class HostDeviceVector; diff --git a/src/common/io.cc b/src/common/io.cc index 0b2f34b44a30..fa5c5b371103 100644 --- a/src/common/io.cc +++ b/src/common/io.cc @@ -20,23 +20,23 @@ #endif // defined(__unix__) || defined(__APPLE__) -#include // for copy, transform -#include // for tolower -#include // for size_t -#include // for int32_t, uint32_t -#include // for fread, fseek -#include // for memcpy -#include // for filesystem, weakly_canonical -#include // for ifstream -#include // for distance -#include // for unique_ptr, make_unique -#include // for string -#include // for move -#include // for vector +#include // for copy, transform +#include // for tolower +#include // for size_t +#include // for int32_t, uint32_t +#include // for fread, fseek +#include // for memcpy +#include // for filesystem, weakly_canonical +#include // for ifstream +#include // for distance +#include // for unique_ptr, make_unique +#include // for string +#include // for move +#include // for vector #include "io.h" -#include "xgboost/logging.h" // for CHECK_LE -#include "xgboost/string_view.h" // for StringView +#include "xgboost/logging.h" // for CHECK_LE +#include "xgboost/string_view.h" // for StringView #if !defined(__linux__) && !defined(__GLIBC__) && !defined(xgboost_IS_WIN) #include // for numeric_limits diff --git a/src/common/io.h b/src/common/io.h index 51aed53945b6..277eed187068 100644 --- a/src/common/io.h +++ b/src/common/io.h @@ -38,17 +38,17 @@ struct MemoryFixSizeBuffer : public dmlc::SeekStream { * @param p_buffer Pointer to the source buffer with size `buffer_size`. * @param buffer_size Size of the source buffer */ - MemoryFixSizeBuffer(void *p_buffer, std::size_t buffer_size) - : p_buffer_(reinterpret_cast(p_buffer)), buffer_size_(buffer_size) {} + MemoryFixSizeBuffer(void* p_buffer, std::size_t buffer_size) + : p_buffer_(reinterpret_cast(p_buffer)), buffer_size_(buffer_size) {} ~MemoryFixSizeBuffer() override = default; - std::size_t Read(void *ptr, std::size_t size) override { + std::size_t Read(void* ptr, std::size_t size) override { std::size_t nread = std::min(buffer_size_ - curr_ptr_, size); if (nread != 0) std::memcpy(ptr, p_buffer_ + curr_ptr_, nread); curr_ptr_ += nread; return nread; } - std::size_t Write(const void *ptr, std::size_t size) override { + std::size_t Write(const void* ptr, std::size_t size) override { if (size == 0) return 0; CHECK_LE(curr_ptr_ + size, buffer_size_); std::memcpy(p_buffer_ + curr_ptr_, ptr, size); @@ -70,7 +70,7 @@ struct MemoryFixSizeBuffer : public dmlc::SeekStream { protected: /*! \brief in memory buffer */ - char *p_buffer_{nullptr}; + char* p_buffer_{nullptr}; /*! \brief current pointer */ std::size_t buffer_size_{0}; /*! \brief current pointer */ @@ -80,40 +80,31 @@ struct MemoryFixSizeBuffer : public dmlc::SeekStream { /*! \brief a in memory buffer that can be read and write as stream interface */ struct MemoryBufferStream : public dmlc::SeekStream { public: - explicit MemoryBufferStream(std::string *p_buffer) - : p_buffer_(p_buffer) { - curr_ptr_ = 0; - } + explicit MemoryBufferStream(std::string* p_buffer) : p_buffer_(p_buffer) { curr_ptr_ = 0; } ~MemoryBufferStream() override = default; - size_t Read(void *ptr, size_t size) override { + size_t Read(void* ptr, size_t size) override { CHECK_LE(curr_ptr_, p_buffer_->length()) << "read can not have position excceed buffer length"; size_t nread = std::min(p_buffer_->length() - curr_ptr_, size); if (nread != 0) std::memcpy(ptr, &(*p_buffer_)[0] + curr_ptr_, nread); curr_ptr_ += nread; return nread; } - std::size_t Write(const void *ptr, size_t size) override { + std::size_t Write(const void* ptr, size_t size) override { if (size == 0) return 0; if (curr_ptr_ + size > p_buffer_->length()) { - p_buffer_->resize(curr_ptr_+size); + p_buffer_->resize(curr_ptr_ + size); } std::memcpy(&(*p_buffer_)[0] + curr_ptr_, ptr, size); curr_ptr_ += size; return size; } - void Seek(size_t pos) override { - curr_ptr_ = static_cast(pos); - } - size_t Tell() override { - return curr_ptr_; - } - virtual bool AtEnd() const { - return curr_ptr_ == p_buffer_->length(); - } + void Seek(size_t pos) override { curr_ptr_ = static_cast(pos); } + size_t Tell() override { return curr_ptr_; } + virtual bool AtEnd() const { return curr_ptr_ == p_buffer_->length(); } private: /*! \brief in memory buffer */ - std::string *p_buffer_; + std::string* p_buffer_; /*! \brief current pointer */ size_t curr_ptr_; }; // class MemoryBufferStream @@ -136,7 +127,7 @@ class PeekableInStream : public dmlc::Stream { private: /*! \brief input stream */ - dmlc::Stream *strm_; + dmlc::Stream* strm_; /*! \brief current buffer pointer */ size_t buffer_ptr_{0}; /*! \brief internal buffer */ @@ -212,7 +203,7 @@ inline std::string ReadAll(dmlc::Stream* fi, PeekableInStream* fp) { /** * \brief Read the whole file content into a string. */ -inline std::string ReadAll(std::string const &path) { +inline std::string ReadAll(std::string const& path) { std::ifstream stream(path); if (!stream.is_open()) { LOG(FATAL) << "Could not open file " << path; @@ -514,8 +505,9 @@ class PrivateMmapConstStream : public AlignedResourceReadStream { * @param length See the `length` parameter of `mmap` for details. */ explicit PrivateMmapConstStream(StringView path, std::size_t offset, std::size_t length) - : AlignedResourceReadStream{std::shared_ptr{ // NOLINT - new MmapResource{path, offset, length}}} {} + : AlignedResourceReadStream{ + std::shared_ptr{// NOLINT + new MmapResource{path, offset, length}}} {} ~PrivateMmapConstStream() noexcept(false) override; }; diff --git a/src/common/json.cc b/src/common/json.cc index 59259e1309e1..a3546743b9c4 100644 --- a/src/common/json.cc +++ b/src/common/json.cc @@ -3,6 +3,7 @@ */ #include "xgboost/json.h" +#include // for copy, equal #include // for array #include // for isdigit #include // for isinf, isnan @@ -14,7 +15,10 @@ #include // for distance #include // for numeric_limits #include // for operator<<, basic_ostream, operator&, ios, stringstream +#include // for string #include // for errc +#include // for move +#include // for vector #include "./math.h" // for CheckNAN #include "charconv.h" // for to_chars, NumericLimits, from_chars, to_chars_result @@ -36,7 +40,9 @@ void JsonWriter::Visit(F32Array const* arr) { this->WriteArray(arr, [](float v) { return Json{v}; }); } namespace { -auto to_i64 = [](auto v) { return Json{static_cast(v)}; }; +auto to_i64 = [](auto v) { + return Json{static_cast(v)}; +}; } // anonymous namespace void JsonWriter::Visit(I8Array const* arr) { this->WriteArray(arr, to_i64); } void JsonWriter::Visit(U8Array const* arr) { this->WriteArray(arr, to_i64); } @@ -58,7 +64,7 @@ void JsonWriter::Visit(JsonObject const* obj) { stream_->emplace_back(':'); this->Save(value.second); - if (i != size-1) { + if (i != size - 1) { stream_->emplace_back(','); } i++; @@ -89,26 +95,26 @@ void JsonWriter::Visit(JsonInteger const* num) { std::memcpy(stream_->data() + ori_size, i2s_buffer_.data(), digits); } -void JsonWriter::Visit(JsonNull const* ) { - auto s = stream_->size(); - stream_->resize(s + 4); - auto& buf = (*stream_); - buf[s + 0] = 'n'; - buf[s + 1] = 'u'; - buf[s + 2] = 'l'; - buf[s + 3] = 'l'; +void JsonWriter::Visit(JsonNull const*) { + auto s = stream_->size(); + stream_->resize(s + 4); + auto& buf = (*stream_); + buf[s + 0] = 'n'; + buf[s + 1] = 'u'; + buf[s + 2] = 'l'; + buf[s + 3] = 'l'; } void JsonWriter::Visit(JsonString const* str) { - std::string buffer; - buffer += '"'; - auto const& string = str->GetString(); - common::EscapeU8(string, &buffer); - buffer += '"'; + std::string buffer; + buffer += '"'; + auto const& string = str->GetString(); + common::EscapeU8(string, &buffer); + buffer += '"'; - auto s = stream_->size(); - stream_->resize(s + buffer.size()); - std::memcpy(stream_->data() + s, buffer.data(), buffer.size()); + auto s = stream_->size(); + stream_->resize(s + buffer.size()); + std::memcpy(stream_->data() + s, buffer.data(), buffer.size()); } void JsonWriter::Visit(JsonBoolean const* boolean) { @@ -209,7 +215,9 @@ void JsonObject::Save(JsonWriter* writer) const { writer->Visit(this); } // Json String bool JsonString::operator==(Value const& rhs) const { - if (!IsA(&rhs)) { return false; } + if (!IsA(&rhs)) { + return false; + } return Cast(&rhs)->GetString() == str_; } @@ -293,7 +301,9 @@ template class JsonTypedArray; // Json Number bool JsonNumber::operator==(Value const& rhs) const { - if (!IsA(&rhs)) { return false; } + if (!IsA(&rhs)) { + return false; + } auto r_num = Cast(&rhs)->GetNumber(); if (std::isinf(number_)) { return std::isinf(r_num); @@ -308,7 +318,9 @@ void JsonNumber::Save(JsonWriter* writer) const { writer->Visit(this); } // Json Integer bool JsonInteger::operator==(Value const& rhs) const { - if (!IsA(&rhs)) { return false; } + if (!IsA(&rhs)) { + return false; + } return integer_ == Cast(&rhs)->GetInteger(); } @@ -316,7 +328,9 @@ void JsonInteger::Save(JsonWriter* writer) const { writer->Visit(this); } // Json Null bool JsonNull::operator==(Value const& rhs) const { - if (!IsA(&rhs)) { return false; } + if (!IsA(&rhs)) { + return false; + } return true; } @@ -324,7 +338,9 @@ void JsonNull::Save(JsonWriter* writer) const { writer->Visit(this); } // Json Boolean bool JsonBoolean::operator==(Value const& rhs) const { - if (!IsA(&rhs)) { return false; } + if (!IsA(&rhs)) { + return false; + } return boolean_ == Cast(&rhs)->GetBoolean(); } @@ -336,19 +352,20 @@ Json JsonReader::Parse() { while (true) { SkipSpaces(); auto c = PeekNextChar(); - if (c == -1) { break; } + if (c == -1) { + break; + } if (c == '{') { return ParseObject(); - } else if ( c == '[' ) { + } else if (c == '[') { return ParseArray(); - } else if ( c == '-' || std::isdigit(c) || - c == 'N' || c == 'I') { + } else if (c == '-' || std::isdigit(c) || c == 'N' || c == 'I') { // For now we only accept `NaN`, not `nan` as the later violates LR(1) with `null`. return ParseNumber(); - } else if ( c == '\"' ) { + } else if (c == '\"') { return ParseString(); - } else if ( c == 't' || c == 'f' ) { + } else if (c == 't' || c == 'f') { return ParseBoolean(); } else if (c == 'n') { return ParseNull(); @@ -377,10 +394,10 @@ void JsonReader::Error(std::string msg) const { } constexpr size_t kExtend = 8; - auto beg = static_cast(cursor_.Pos()) - - static_cast(kExtend) < 0 ? 0 : cursor_.Pos() - kExtend; - auto end = cursor_.Pos() + kExtend >= raw_str_.size() ? - raw_str_.size() : cursor_.Pos() + kExtend; + auto beg = static_cast(cursor_.Pos()) - static_cast(kExtend) < 0 + ? 0 + : cursor_.Pos() - kExtend; + auto end = cursor_.Pos() + kExtend >= raw_str_.size() ? raw_str_.size() : cursor_.Pos() + kExtend; auto raw_portion = raw_str_.substr(beg, end - beg); std::string portion; @@ -428,7 +445,7 @@ void JsonReader::SkipSpaces() { void ParseStr(std::string const& str) { size_t end = 0; for (size_t i = 0; i < str.size(); ++i) { - if (str[i] == '"' && i > 0 && str[i-1] != '\\') { + if (str[i] == '"' && i > 0 && str[i - 1] != '\\') { end = i; break; } @@ -438,23 +455,34 @@ void ParseStr(std::string const& str) { } Json JsonReader::ParseString() { - Char ch { GetConsecutiveChar('\"') }; // NOLINT + Char ch{GetConsecutiveChar('\"')}; // NOLINT std::string str; while (true) { ch = GetNextChar(); if (ch == '\\') { Char next{GetNextChar()}; switch (next) { - case 'r': str += u8"\r"; break; - case 'n': str += u8"\n"; break; - case '\\': str += u8"\\"; break; - case 't': str += u8"\t"; break; - case '\"': str += u8"\""; break; + case 'r': + str += u8"\r"; + break; + case 'n': + str += u8"\n"; + break; + case '\\': + str += u8"\\"; + break; + case 't': + str += u8"\t"; + break; + case '\"': + str += u8"\""; + break; case 'u': str += ch; str += 'u'; break; - default: Error("Unknown escape"); + default: + Error("Unknown escape"); } } else { if (ch == '\"') break; @@ -482,7 +510,7 @@ Json JsonReader::ParseNull() { Json JsonReader::ParseArray() { std::vector data; - Char ch { GetConsecutiveChar('[') }; // NOLINT + Char ch{GetConsecutiveChar('[')}; // NOLINT while (true) { if (PeekNextChar() == ']') { GetConsecutiveChar(']'); @@ -528,7 +556,7 @@ Json JsonReader::ParseObject() { Expect(':', ch); } - Json value { Parse() }; + Json value{Parse()}; data[get(key)] = std::move(value); @@ -558,19 +586,19 @@ Json JsonReader::ParseNumber() { bool negative = false; switch (*p) { - case '-': { - negative = true; - ++p; - break; - } - case '+': { - negative = false; - ++p; - break; - } - default: { - break; - } + case '-': { + negative = true; + ++p; + break; + } + case '+': { + negative = false; + ++p; + break; + } + default: { + break; + } } if (XGBOOST_EXPECT(*p == 'I', false)) { @@ -614,13 +642,13 @@ Json JsonReader::ParseNumber() { p++; switch (*p) { - case '-': - case '+': { - p++; - break; - } - default: - break; + case '-': + case '+': { + p++; + break; + } + default: + break; } if (XGBOOST_EXPECT(*p >= '0' && *p <= '9', true)) { @@ -708,9 +736,7 @@ void Json::Dump(Json json, std::vector* str, std::ios::openmode mode) { } } -void Json::Dump(Json json, JsonWriter* writer) { - writer->Save(json); -} +void Json::Dump(Json json, JsonWriter* writer) { writer->Save(json); } static_assert(std::is_nothrow_move_constructible_v); static_assert(std::is_nothrow_move_constructible_v); diff --git a/src/common/linalg_op.cc b/src/common/linalg_op.cc index 43a3af14ce15..85ae6d7b41ec 100644 --- a/src/common/linalg_op.cc +++ b/src/common/linalg_op.cc @@ -18,8 +18,7 @@ void SmallHistogram(Context const* ctx, xgboost::linalg::MatrixView xgboost::linalg::VectorView bins); #if !defined(XGBOOST_USE_SYCL) void SmallHistogram(Context const*, xgboost::linalg::MatrixView, - common::OptionalWeights const&, - xgboost::linalg::VectorView) { + common::OptionalWeights const&, xgboost::linalg::VectorView) { common::AssertSYCLSupport(); } #endif diff --git a/src/common/math.h b/src/common/math.h index 878870fbf2db..a23d5202df51 100644 --- a/src/common/math.h +++ b/src/common/math.h @@ -18,7 +18,10 @@ namespace xgboost { namespace common { -template XGBOOST_DEVICE T Sqr(T const &w) { return w * w; } +template +XGBOOST_DEVICE T Sqr(T const &w) { + return w * w; +} /*! * \brief calculate the sigmoid of the input. @@ -47,8 +50,9 @@ XGBOOST_DEVICE constexpr bool CloseTo(T a, U b) { std::is_floating_point_v || std::is_floating_point_v, double, typename std::conditional_t || std::is_signed_v, std::int64_t, std::uint64_t>>; - return std::is_floating_point_v ? - std::abs(static_cast(a) -static_cast(b)) < 1e-6 : a == b; + return std::is_floating_point_v + ? std::abs(static_cast(a) - static_cast(b)) < 1e-6 + : a == b; } /*! @@ -86,7 +90,7 @@ XGBOOST_DEVICE void Softmax(Iterator start, Iterator end) { * \return the iterator point to the maximum value. * \tparam Iterator The type of the iterator. */ -template +template XGBOOST_DEVICE inline Iterator FindMaxIndex(Iterator begin, Iterator end) { Iterator maxit = begin; for (Iterator it = begin; it != end; ++it) { @@ -116,7 +120,7 @@ inline float LogSum(float x, float y) { * \return the iterator point to the maximum value. * \tparam Iterator The type of the iterator. */ -template +template inline float LogSum(Iterator begin, Iterator end) { float mx = *begin; for (Iterator it = begin; it != end; ++it) { @@ -166,15 +170,16 @@ double LogGamma(double v); #else // Not R or R with GPU. -template +template XGBOOST_DEVICE inline T LogGamma(T v) { #ifdef _MSC_VER #if _MSC_VER >= 1800 return lgamma(v); #else -#pragma message("Warning: lgamma function was not available until VS2013"\ - ", poisson regression will be disabled") +#pragma message( \ + "Warning: lgamma function was not available until VS2013" \ + ", poisson regression will be disabled") utils::Error("lgamma function was not available until VS2013"); return static_cast(1.0); #endif // _MSC_VER >= 1800 diff --git a/src/common/numa_topo.cc b/src/common/numa_topo.cc index f4b8b0ebe54b..aca0702f6996 100644 --- a/src/common/numa_topo.cc +++ b/src/common/numa_topo.cc @@ -211,7 +211,7 @@ void GetNumaHasCpuNodes(std::vector *p_nodes) { #endif // defined(__linux__) } -[[nodiscard]] bool GetCpuNuma(unsigned int* cpu, unsigned int* numa) { +[[nodiscard]] bool GetCpuNuma(unsigned int *cpu, unsigned int *numa) { #ifdef SYS_getcpu return syscall(SYS_getcpu, cpu, numa, NULL) == 0; #else diff --git a/src/common/numa_topo.h b/src/common/numa_topo.h index 3aedbe42e57a..6e75153bb0ef 100644 --- a/src/common/numa_topo.h +++ b/src/common/numa_topo.h @@ -65,7 +65,7 @@ void GetNumaHasCpuNodes(std::vector *p_nodes); * @brief Get numa node on Linux. Other platforms are not supported. Returns false if the * call fails. */ -[[nodiscard]] bool GetCpuNuma(unsigned int* cpu, unsigned int* numa); +[[nodiscard]] bool GetCpuNuma(unsigned int *cpu, unsigned int *numa); /** * @brief Is it physically possible to access the wrong memory? diff --git a/src/common/numeric.cu b/src/common/numeric.cu index a326b935537f..6214bdeccc81 100644 --- a/src/common/numeric.cu +++ b/src/common/numeric.cu @@ -3,7 +3,7 @@ */ #include -#include "device_helpers.cuh" // dh::Reduce, dh::XGBCachingDeviceAllocator +#include "device_helpers.cuh" // dh::Reduce, dh::XGBCachingDeviceAllocator #include "numeric.h" #include "xgboost/context.h" // Context #include "xgboost/host_device_vector.h" // HostDeviceVector diff --git a/src/common/observer.h b/src/common/observer.h index 2abebc7a4470..6eeee09fc220 100644 --- a/src/common/observer.h +++ b/src/common/observer.h @@ -5,16 +5,16 @@ #ifndef XGBOOST_COMMON_OBSERVER_H_ #define XGBOOST_COMMON_OBSERVER_H_ -#include #include +#include #include #include #include +#include "xgboost/base.h" #include "xgboost/host_device_vector.h" -#include "xgboost/parameter.h" #include "xgboost/json.h" -#include "xgboost/base.h" +#include "xgboost/parameter.h" #include "xgboost/tree_model.h" #if defined(XGBOOST_STRICT_R_MODE) && XGBOOST_STRICT_R_MODE == 1 @@ -37,21 +37,25 @@ namespace xgboost { */ class TrainingObserver { #if defined(XGBOOST_USE_DEBUG_OUTPUT) - bool constexpr static kObserve {true}; + bool constexpr static kObserve{true}; #else - bool constexpr static kObserve {false}; + bool constexpr static kObserve{false}; #endif // defined(XGBOOST_USE_DEBUG_OUTPUT) public: void Update(int32_t iter) const { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } OBSERVER_PRINT << "Iter: " << iter << OBSERVER_ENDL; } /*\brief Observe tree. */ void Observe(RegTree const& tree) { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } OBSERVER_PRINT << "Tree:" << OBSERVER_ENDL; - Json j_tree {Object()}; + Json j_tree{Object()}; tree.SaveModel(&j_tree); std::string str; Json::Dump(j_tree, &str); @@ -59,7 +63,9 @@ class TrainingObserver { } /*\brief Observe tree. */ void Observe(RegTree const* p_tree) { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } auto const& tree = *p_tree; this->Observe(tree); } @@ -74,7 +80,9 @@ class TrainingObserver { template void Observe(std::vector const& h_vec, std::string name, size_t n = std::numeric_limits::max()) const { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } OBSERVER_PRINT << "Procedure: " << name << OBSERVER_ENDL; for (size_t i = 0; i < h_vec.size(); ++i) { @@ -92,14 +100,18 @@ class TrainingObserver { template void Observe(HostDeviceVector const& vec, std::string name, size_t n = std::numeric_limits::max()) const { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } auto const& h_vec = vec.HostVector(); this->Observe(h_vec, name, n); } template void Observe(HostDeviceVector* vec, std::string name, size_t n = std::numeric_limits::max()) const { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } this->Observe(*vec, name, n); } @@ -108,14 +120,18 @@ class TrainingObserver { typename std::enable_if_t, Parameter>>* = nullptr> void Observe(const Parameter& p, std::string name) const { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } - Json obj {toJson(p)}; + Json obj{toJson(p)}; OBSERVER_PRINT << "Parameter: " << name << ":\n" << obj << OBSERVER_ENDL; } /*\brief Observe parameters provided by users. */ void Observe(Args const& args) const { - if (XGBOOST_EXPECT(!kObserve, true)) { return; } + if (XGBOOST_EXPECT(!kObserve, true)) { + return; + } for (auto kv : args) { OBSERVER_PRINT << kv.first << ": " << kv.second << OBSERVER_NEWLINE; diff --git a/src/common/optional_weight.cc b/src/common/optional_weight.cc index a22de40c1a88..7ac8a2662930 100644 --- a/src/common/optional_weight.cc +++ b/src/common/optional_weight.cc @@ -5,11 +5,10 @@ #include // for accumulate +#include "common.h" // for AssertGPUSupport #include "xgboost/base.h" // for bst_idx_t #include "xgboost/context.h" // for Context -#include "common.h" // for AssertGPUSupport - namespace xgboost::common { #if defined(XGBOOST_USE_CUDA) namespace cuda_impl { diff --git a/src/common/optional_weight.cu b/src/common/optional_weight.cu index 7801eadea86e..d3cf100c08a2 100644 --- a/src/common/optional_weight.cu +++ b/src/common/optional_weight.cu @@ -1,9 +1,8 @@ /** * Copyright 2025, XGBoost Contributors */ -#include // for plus - #include // for size_t +#include // for plus #include "cuda_context.cuh" #include "device_helpers.cuh" diff --git a/src/common/partition_builder.h b/src/common/partition_builder.h index a996f7b89ed1..f344727a8773 100644 --- a/src/common/partition_builder.h +++ b/src/common/partition_builder.h @@ -28,19 +28,19 @@ namespace xgboost::common { // 1) Effective memory allocation for intermediate results for multi-thread work // 2) Merging partial results produced by threads into original row set (row_set_collection_) // BlockSize is template to enable memory alignment easily with C++11 'alignas()' feature -template +template class PartitionBuilder { using BitVector = RBitField8; public: - template + template void Init(const size_t n_tasks, size_t n_nodes, Func funcNTask) { left_right_nodes_sizes_.resize(n_nodes); - blocks_offsets_.resize(n_nodes+1); + blocks_offsets_.resize(n_nodes + 1); blocks_offsets_[0] = 0; - for (size_t i = 1; i < n_nodes+1; ++i) { - blocks_offsets_[i] = blocks_offsets_[i-1] + funcNTask(i-1); + for (size_t i = 1; i < n_nodes + 1; ++i) { + blocks_offsets_[i] = blocks_offsets_[i - 1] + funcNTask(i - 1); } if (n_tasks > max_n_tasks_) { @@ -177,7 +177,7 @@ class PartitionBuilder { } } - const size_t n_left = child_nodes_sizes.first; + const size_t n_left = child_nodes_sizes.first; const size_t n_right = child_nodes_sizes.second; SetNLeftElems(node_in_set, range.begin(), n_left); @@ -286,7 +286,7 @@ class PartitionBuilder { std::pair child_nodes_sizes; child_nodes_sizes = PartitionRangeKernel(rid_span, left, right, pred); - const size_t n_left = child_nodes_sizes.first; + const size_t n_left = child_nodes_sizes.first; const size_t n_right = child_nodes_sizes.second; SetNLeftElems(node_in_set, range.begin(), n_left); @@ -304,12 +304,12 @@ class PartitionBuilder { common::Span GetLeftBuffer(int nid, size_t begin, size_t end) { const size_t task_idx = GetTaskIdx(nid, begin); - return { mem_blocks_.at(task_idx)->Left(), end - begin }; + return {mem_blocks_.at(task_idx)->Left(), end - begin}; } common::Span GetRightBuffer(int nid, size_t begin, size_t end) { const size_t task_idx = GetTaskIdx(nid, begin); - return { mem_blocks_.at(task_idx)->Right(), end - begin }; + return {mem_blocks_.at(task_idx)->Right(), end - begin}; } void SetNLeftElems(int nid, size_t begin, size_t n_left) { @@ -322,7 +322,6 @@ class PartitionBuilder { mem_blocks_.at(task_idx)->n_right = n_right; } - [[nodiscard]] std::size_t GetNLeftElems(int nid) const { return left_right_nodes_sizes_[nid].first; } @@ -334,9 +333,9 @@ class PartitionBuilder { // Each thread has partial results for some set of tree-nodes // The function decides order of merging partial results into final row set void CalculateRowOffsets() { - for (size_t i = 0; i < blocks_offsets_.size()-1; ++i) { + for (size_t i = 0; i < blocks_offsets_.size() - 1; ++i) { size_t n_left = 0; - for (size_t j = blocks_offsets_[i]; j < blocks_offsets_[i+1]; ++j) { + for (size_t j = blocks_offsets_[i]; j < blocks_offsets_[i + 1]; ++j) { mem_blocks_[j]->n_offset_left = n_left; n_left += mem_blocks_[j]->n_left; } @@ -362,9 +361,7 @@ class PartitionBuilder { std::copy_n(right, mem_blocks_[task_idx]->n_right, right_result); } - size_t GetTaskIdx(int nid, size_t begin) { - return blocks_offsets_[nid] + begin / BlockSize; - } + size_t GetTaskIdx(int nid, size_t begin) { return blocks_offsets_[nid] + begin / BlockSize; } // Copy row partitions into global cache for reuse in objective template @@ -390,20 +387,17 @@ class PartitionBuilder { } protected: - struct BlockInfo{ + struct BlockInfo { size_t n_left; size_t n_right; size_t n_offset_left; size_t n_offset_right; - bst_idx_t* Left() { - return &left_data_[0]; - } + bst_idx_t* Left() { return &left_data_[0]; } + + bst_idx_t* Right() { return &right_data_[0]; } - bst_idx_t* Right() { - return &right_data_[0]; - } private: bst_idx_t left_data_[BlockSize]; bst_idx_t right_data_[BlockSize]; diff --git a/src/common/probability_distribution.h b/src/common/probability_distribution.h index b581df0cf31b..75c0b619539f 100644 --- a/src/common/probability_distribution.h +++ b/src/common/probability_distribution.h @@ -8,6 +8,8 @@ #ifndef XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_ #define XGBOOST_COMMON_PROBABILITY_DISTRIBUTION_H_ +#include + #include namespace xgboost { @@ -16,9 +18,9 @@ namespace common { #ifndef __CUDACC__ using std::exp; -using std::sqrt; using std::isinf; using std::isnan; +using std::sqrt; #endif // __CUDACC__ @@ -28,26 +30,16 @@ constexpr double kPI = 3.14159265358979323846; constexpr double kEulerMascheroni = 0.57721566490153286060651209008240243104215933593992; /*! \brief Enum encoding possible choices of probability distribution */ -enum class ProbabilityDistributionType : int { - kNormal = 0, kLogistic = 1, kExtreme = 2 -}; +enum class ProbabilityDistributionType : int { kNormal = 0, kLogistic = 1, kExtreme = 2 }; struct NormalDistribution { - XGBOOST_DEVICE static double PDF(double z) { - return exp(-z * z / 2.0) / sqrt(2.0 * kPI); - } + XGBOOST_DEVICE static double PDF(double z) { return exp(-z * z / 2.0) / sqrt(2.0 * kPI); } - XGBOOST_DEVICE static double CDF(double z) { - return 0.5 * (1 + erf(z / sqrt(2.0))); - } + XGBOOST_DEVICE static double CDF(double z) { return 0.5 * (1 + erf(z / sqrt(2.0))); } - XGBOOST_DEVICE static double GradPDF(double z) { - return -z * PDF(z); - } + XGBOOST_DEVICE static double GradPDF(double z) { return -z * PDF(z); } - XGBOOST_DEVICE static double HessPDF(double z) { - return (z * z - 1.0) * PDF(z); - } + XGBOOST_DEVICE static double HessPDF(double z) { return (z * z - 1.0) * PDF(z); } XGBOOST_DEVICE static ProbabilityDistributionType Type() { return ProbabilityDistributionType::kNormal; diff --git a/src/common/quantile.cc b/src/common/quantile.cc index 49df99d6261b..01846fe2d78b 100644 --- a/src/common/quantile.cc +++ b/src/common/quantile.cc @@ -3,9 +3,11 @@ */ #include "quantile.h" +#include #include #include // for partial_sum #include +#include #include "../collective/aggregator.h" #include "../common/error_msg.h" // for InvalidMaxBin @@ -41,8 +43,10 @@ std::vector MergeWeights(MetaInfo const &info, Span hessian, CHECK_EQ(hessian.size(), info.num_row_); std::vector results(hessian.size()); auto const &group_ptr = info.group_ptr_; - auto const& weights = info.weights_.HostVector(); - auto get_weight = [&](size_t i) { return weights.empty() ? 1.0f : weights[i]; }; + auto const &weights = info.weights_.HostVector(); + auto get_weight = [&](size_t i) { + return weights.empty() ? 1.0f : weights[i]; + }; if (use_group) { CHECK_GE(group_ptr.size(), 2); CHECK_EQ(group_ptr.back(), hessian.size()); @@ -220,7 +224,7 @@ void SketchContainerImpl::GatherSketchInfo( } template -void SketchContainerImpl::AllreduceCategories(Context const* ctx, MetaInfo const& info) { +void SketchContainerImpl::AllreduceCategories(Context const *ctx, MetaInfo const &info) { auto world_size = collective::GetWorldSize(); auto rank = collective::GetRank(); if (world_size == 1 || info.IsColumnSplit()) { @@ -302,7 +306,7 @@ void SketchContainerImpl::AllReduce( AllreduceCategories(ctx, info); - auto& num_cuts = *p_num_cuts; + auto &num_cuts = *p_num_cuts; CHECK_EQ(num_cuts.size(), 0); num_cuts.resize(sketches_.size()); diff --git a/src/common/quantile.cu b/src/common/quantile.cu index 535a2a700e89..c129eba56a92 100644 --- a/src/common/quantile.cu +++ b/src/common/quantile.cu @@ -8,10 +8,12 @@ #include #include +#include #include // for numeric_limits #include // for partial_sum #include // for is_same_v #include +#include #include "../collective/allgather.h" #include "../collective/allreduce.h" @@ -46,13 +48,10 @@ __device__ SketchEntry BinarySearchQuery(EntryIter beg, EntryIter end, float ran } auto search_begin = dh::MakeTransformIterator( - beg, [=] __device__(SketchEntry const &entry) { - return entry.rmin + entry.rmax; - }); + beg, [=] __device__(SketchEntry const &entry) { return entry.rmin + entry.rmax; }); auto search_end = search_begin + (end - beg); auto i = - thrust::upper_bound(thrust::seq, search_begin + 1, search_end - 1, rank) - - search_begin - 1; + thrust::upper_bound(thrust::seq, search_begin + 1, search_end - 1, rank) - search_begin - 1; if (rank < (*(beg + i)).RMinNext() + (*(beg + i + 1)).RMaxPrev()) { return *(beg + i); } else { @@ -68,11 +67,10 @@ void PruneImpl(common::Span cuts_ptr, ToSketchEntry to_sketch_entry) { dh::LaunchN(out_cuts.size(), [=] __device__(size_t idx) { size_t column_id = dh::SegmentId(cuts_ptr, idx); - auto out_column = out_cuts.subspan( - cuts_ptr[column_id], cuts_ptr[column_id + 1] - cuts_ptr[column_id]); + auto out_column = + out_cuts.subspan(cuts_ptr[column_id], cuts_ptr[column_id + 1] - cuts_ptr[column_id]); auto in_column = sorted_data.subspan(columns_ptr_in[column_id], - columns_ptr_in[column_id + 1] - - columns_ptr_in[column_id]); + columns_ptr_in[column_id + 1] - columns_ptr_in[column_id]); auto to = cuts_ptr[column_id + 1] - cuts_ptr[column_id]; idx -= cuts_ptr[column_id]; auto front = to_sketch_entry(0ul, in_column, column_id); @@ -112,27 +110,24 @@ template void CopyTo(Span out, Span src) { CHECK_EQ(out.size(), src.size()); static_assert(std::is_same_v, std::remove_cv_t>); - dh::safe_cuda(cudaMemcpyAsync(out.data(), src.data(), - out.size_bytes(), - cudaMemcpyDefault)); + dh::safe_cuda(cudaMemcpyAsync(out.data(), src.data(), out.size_bytes(), cudaMemcpyDefault)); } // Compute the merge path. common::Span> MergePath( - Context const* ctx, - Span const &d_x, Span const &x_ptr, - Span const &d_y, Span const &y_ptr, - Span out, Span out_ptr) { - auto x_merge_key_it = thrust::make_zip_iterator(thrust::make_tuple( - dh::MakeTransformIterator( - thrust::make_counting_iterator(0ul), - [=] __device__(size_t idx) { return dh::SegmentId(x_ptr, idx); }), - d_x.data())); - auto y_merge_key_it = thrust::make_zip_iterator(thrust::make_tuple( - dh::MakeTransformIterator( - thrust::make_counting_iterator(0ul), - [=] __device__(size_t idx) { return dh::SegmentId(y_ptr, idx); }), - d_y.data())); + Context const *ctx, Span const &d_x, Span const &x_ptr, + Span const &d_y, Span const &y_ptr, Span out, + Span out_ptr) { + auto x_merge_key_it = thrust::make_zip_iterator( + thrust::make_tuple(dh::MakeTransformIterator( + thrust::make_counting_iterator(0ul), + [=] __device__(size_t idx) { return dh::SegmentId(x_ptr, idx); }), + d_x.data())); + auto y_merge_key_it = thrust::make_zip_iterator( + thrust::make_tuple(dh::MakeTransformIterator( + thrust::make_counting_iterator(0ul), + [=] __device__(size_t idx) { return dh::SegmentId(y_ptr, idx); }), + d_y.data())); using Tuple = thrust::tuple; @@ -140,40 +135,42 @@ common::Span> MergePath( thrust::constant_iterator b_ind_iter(1ul); auto place_holder = thrust::make_constant_iterator(0u); - auto x_merge_val_it = - thrust::make_zip_iterator(thrust::make_tuple(a_ind_iter, place_holder)); - auto y_merge_val_it = - thrust::make_zip_iterator(thrust::make_tuple(b_ind_iter, place_holder)); + auto x_merge_val_it = thrust::make_zip_iterator(thrust::make_tuple(a_ind_iter, place_holder)); + auto y_merge_val_it = thrust::make_zip_iterator(thrust::make_tuple(b_ind_iter, place_holder)); static_assert(sizeof(Tuple) == sizeof(SketchEntry)); // We reuse the memory for storing merge path. common::Span merge_path{reinterpret_cast(out.data()), out.size()}; // Determine the merge path, 0 if element is from x, 1 if it's from y. - thrust::merge_by_key( - ctx->CUDACtx()->CTP(), x_merge_key_it, x_merge_key_it + d_x.size(), - y_merge_key_it, y_merge_key_it + d_y.size(), x_merge_val_it, - y_merge_val_it, thrust::make_discard_iterator(), merge_path.data(), - [=] __device__(auto const &l, auto const &r) -> bool { - auto l_column_id = thrust::get<0>(l); - auto r_column_id = thrust::get<0>(r); - if (l_column_id == r_column_id) { - return thrust::get<1>(l).value < thrust::get<1>(r).value; - } - return l_column_id < r_column_id; - }); + thrust::merge_by_key(ctx->CUDACtx()->CTP(), x_merge_key_it, x_merge_key_it + d_x.size(), + y_merge_key_it, y_merge_key_it + d_y.size(), x_merge_val_it, y_merge_val_it, + thrust::make_discard_iterator(), merge_path.data(), + [=] __device__(auto const &l, auto const &r) -> bool { + auto l_column_id = thrust::get<0>(l); + auto r_column_id = thrust::get<0>(r); + if (l_column_id == r_column_id) { + return thrust::get<1>(l).value < thrust::get<1>(r).value; + } + return l_column_id < r_column_id; + }); // Compute output ptr - auto transform_it = - thrust::make_zip_iterator(thrust::make_tuple(x_ptr.data(), y_ptr.data())); + auto transform_it = thrust::make_zip_iterator(thrust::make_tuple(x_ptr.data(), y_ptr.data())); thrust::transform(ctx->CUDACtx()->CTP(), transform_it, transform_it + x_ptr.size(), out_ptr.data(), [] __device__(auto const &t) { return thrust::get<0>(t) + thrust::get<1>(t); }); // 0^th is the indicator, 1^th is placeholder - auto get_ind = []XGBOOST_DEVICE(Tuple const& t) { return thrust::get<0>(t); }; + auto get_ind = [] XGBOOST_DEVICE(Tuple const &t) { + return thrust::get<0>(t); + }; // 0^th is the counter for x, 1^th for y. - auto get_x = []XGBOOST_DEVICE(Tuple const &t) { return thrust::get<0>(t); }; - auto get_y = []XGBOOST_DEVICE(Tuple const &t) { return thrust::get<1>(t); }; + auto get_x = [] XGBOOST_DEVICE(Tuple const &t) { + return thrust::get<0>(t); + }; + auto get_y = [] XGBOOST_DEVICE(Tuple const &t) { + return thrust::get<1>(t); + }; auto scan_key_it = dh::MakeTransformIterator( thrust::make_counting_iterator(0ul), @@ -195,11 +192,9 @@ common::Span> MergePath( // is landed into output as the first element in merge result. The scan result is the // subscript of x and y. thrust::exclusive_scan_by_key( - ctx->CUDACtx()->CTP(), scan_key_it, scan_key_it + merge_path.size(), - scan_val_it, merge_path.data(), - thrust::make_tuple(0ul, 0ul), - thrust::equal_to{}, - [=] __device__(Tuple const &l, Tuple const &r) -> Tuple { + ctx->CUDACtx()->CTP(), scan_key_it, scan_key_it + merge_path.size(), scan_val_it, + merge_path.data(), thrust::make_tuple(0ul, 0ul), + thrust::equal_to{}, [=] __device__(Tuple const &l, Tuple const &r) -> Tuple { return thrust::make_tuple(get_x(l) + get_x(r), get_y(l) + get_y(r)); }); @@ -224,14 +219,12 @@ void MergeImpl(Context const *ctx, Span const &d_x, auto column_id = dh::SegmentId(out_ptr, idx); idx -= out_ptr[column_id]; - auto d_x_column = - d_x.subspan(x_ptr[column_id], x_ptr[column_id + 1] - x_ptr[column_id]); - auto d_y_column = - d_y.subspan(y_ptr[column_id], y_ptr[column_id + 1] - y_ptr[column_id]); - auto d_out_column = d_out.subspan( - out_ptr[column_id], out_ptr[column_id + 1] - out_ptr[column_id]); - auto d_path_column = d_merge_path.subspan( - out_ptr[column_id], out_ptr[column_id + 1] - out_ptr[column_id]); + auto d_x_column = d_x.subspan(x_ptr[column_id], x_ptr[column_id + 1] - x_ptr[column_id]); + auto d_y_column = d_y.subspan(y_ptr[column_id], y_ptr[column_id + 1] - y_ptr[column_id]); + auto d_out_column = + d_out.subspan(out_ptr[column_id], out_ptr[column_id + 1] - out_ptr[column_id]); + auto d_path_column = + d_merge_path.subspan(out_ptr[column_id], out_ptr[column_id + 1] - out_ptr[column_id]); uint64_t a_ind, b_ind; thrust::tie(a_ind, b_ind) = d_path_column[idx]; @@ -253,17 +246,17 @@ void MergeImpl(Context const *ctx, Span const &d_x, if (a_ind == d_x_column.size()) { // Trailing elements are from y because there's no more x to land. auto y_elem = d_y_column[b_ind]; - d_out_column[idx] = SketchEntry(y_elem.rmin + d_x_column.back().RMinNext(), - y_elem.rmax + d_x_column.back().rmax, - y_elem.wmin, y_elem.value); + d_out_column[idx] = + SketchEntry(y_elem.rmin + d_x_column.back().RMinNext(), + y_elem.rmax + d_x_column.back().rmax, y_elem.wmin, y_elem.value); return; } auto x_elem = d_x_column[a_ind]; assert(b_ind <= d_y_column.size()); if (b_ind == d_y_column.size()) { - d_out_column[idx] = SketchEntry(x_elem.rmin + d_y_column.back().RMinNext(), - x_elem.rmax + d_y_column.back().rmax, - x_elem.wmin, x_elem.value); + d_out_column[idx] = + SketchEntry(x_elem.rmin + d_y_column.back().RMinNext(), + x_elem.rmax + d_y_column.back().rmax, x_elem.wmin, x_elem.value); return; } auto y_elem = d_y_column[b_ind]; @@ -284,9 +277,8 @@ void MergeImpl(Context const *ctx, Span const &d_x, */ assert(idx < d_out_column.size()); if (x_elem.value == y_elem.value) { - d_out_column[idx] = - SketchEntry{x_elem.rmin + y_elem.rmin, x_elem.rmax + y_elem.rmax, - x_elem.wmin + y_elem.wmin, x_elem.value}; + d_out_column[idx] = SketchEntry{x_elem.rmin + y_elem.rmin, x_elem.rmax + y_elem.rmax, + x_elem.wmin + y_elem.wmin, x_elem.value}; } else if (x_elem.value < y_elem.value) { // elem from x is landed. yprev_min is the element in D_2 that's 1 rank less than // x_elem if we put x_elem in D_2. @@ -294,15 +286,13 @@ void MergeImpl(Context const *ctx, Span const &d_x, // rmin should be equal to x_elem.rmin + x_elem.wmin + yprev_min. But for // implementation, the weight is stored in a separated field and we compute the // extended definition on the fly when needed. - d_out_column[idx] = - SketchEntry{x_elem.rmin + yprev_min, x_elem.rmax + y_elem.RMaxPrev(), - x_elem.wmin, x_elem.value}; + d_out_column[idx] = SketchEntry{x_elem.rmin + yprev_min, x_elem.rmax + y_elem.RMaxPrev(), + x_elem.wmin, x_elem.value}; } else { // elem from y is landed. float xprev_min = a_ind == 0 ? 0.0f : d_x_column[a_ind - 1].RMinNext(); - d_out_column[idx] = - SketchEntry{xprev_min + y_elem.rmin, x_elem.RMaxPrev() + y_elem.rmax, - y_elem.wmin, y_elem.value}; + d_out_column[idx] = SketchEntry{xprev_min + y_elem.rmin, x_elem.RMaxPrev() + y_elem.rmax, + y_elem.wmin, y_elem.value}; } }); } @@ -322,19 +312,17 @@ void SketchContainer::Push(Context const *ctx, Span entries, Spanfeature_types_.ConstDeviceSpan(); if (weights.empty()) { - auto to_sketch_entry = [] __device__(size_t sample_idx, - Span const &column, + auto to_sketch_entry = [] __device__(size_t sample_idx, Span const &column, size_t) { float rmin = sample_idx; float rmax = sample_idx + 1; return SketchEntry{rmin, rmax, 1, column[sample_idx].fvalue}; - }; // NOLINT + }; // NOLINT PruneImpl(cuts_ptr, entries, columns_ptr, ft, out, to_sketch_entry); } else { - auto to_sketch_entry = [weights, columns_ptr] __device__( - size_t sample_idx, - Span const &column, - size_t column_id) { + auto to_sketch_entry = [weights, columns_ptr] __device__(size_t sample_idx, + Span const &column, + size_t column_id) { Span column_weights_scan = weights.subspan(columns_ptr[column_id], column.size()); float rmin = sample_idx > 0 ? column_weights_scan[sample_idx - 1] : 0.0f; @@ -342,7 +330,7 @@ void SketchContainer::Push(Context const *ctx, Span entries, Span(cuts_ptr, entries, columns_ptr, ft, out, to_sketch_entry); } auto n_uniques = this->ScanInput(ctx, out, cuts_ptr); @@ -374,24 +362,20 @@ size_t SketchContainer::ScanInput(Context const *ctx, Span entries, auto key_it = dh::MakeTransformIterator( thrust::make_reverse_iterator(thrust::make_counting_iterator(entries.size())), - [=] __device__(size_t idx) { - return dh::SegmentId(d_columns_ptr_in, idx); - }); + [=] __device__(size_t idx) { return dh::SegmentId(d_columns_ptr_in, idx); }); // Reverse scan to accumulate weights into first duplicated element on left. auto val_it = thrust::make_reverse_iterator(dh::tend(entries)); - thrust::inclusive_scan_by_key( - ctx->CUDACtx()->CTP(), key_it, key_it + entries.size(), - val_it, val_it, - thrust::equal_to{}, - [] __device__(SketchEntry const &r, SketchEntry const &l) { - // Only accumulate for the first type of duplication. - if (l.value - r.value == 0 && l.rmin - r.rmin != 0) { - auto w = l.wmin + r.wmin; - SketchEntry v{l.rmin, l.rmin + w, w, l.value}; - return v; - } - return l; - }); + thrust::inclusive_scan_by_key(ctx->CUDACtx()->CTP(), key_it, key_it + entries.size(), val_it, + val_it, thrust::equal_to{}, + [] __device__(SketchEntry const &r, SketchEntry const &l) { + // Only accumulate for the first type of duplication. + if (l.value - r.value == 0 && l.rmin - r.rmin != 0) { + auto w = l.wmin + r.wmin; + SketchEntry v{l.rmin, l.rmin + w, w, l.value}; + return v; + } + return l; + }); auto d_columns_ptr_out = columns_ptr_b_.DeviceSpan(); // thrust unique_by_key preserves the first element. @@ -406,14 +390,14 @@ size_t SketchContainer::ScanInput(Context const *ctx, Span entries, return n_uniques; } -void SketchContainer::Prune(Context const* ctx, std::size_t to) { +void SketchContainer::Prune(Context const *ctx, std::size_t to) { timer_.Start(__func__); curt::SetDevice(ctx->Ordinal()); OffsetT to_total = 0; - auto& h_columns_ptr = columns_ptr_b_.HostVector(); + auto &h_columns_ptr = columns_ptr_b_.HostVector(); h_columns_ptr[0] = to_total; - auto const& h_feature_types = feature_types_.ConstHostSpan(); + auto const &h_feature_types = feature_types_.ConstHostSpan(); for (bst_feature_t i = 0; i < num_columns_; ++i) { size_t length = this->Column(i).size(); length = std::min(length, to); @@ -421,7 +405,7 @@ void SketchContainer::Prune(Context const* ctx, std::size_t to) { length = this->Column(i).size(); } to_total += length; - h_columns_ptr[i+1] = to_total; + h_columns_ptr[i + 1] = to_total; } this->Other().resize(to_total); @@ -429,9 +413,9 @@ void SketchContainer::Prune(Context const* ctx, std::size_t to) { auto d_columns_ptr_out = columns_ptr_b_.ConstDeviceSpan(); auto out = dh::ToSpan(this->Other()); auto in = dh::ToSpan(this->Current()); - auto no_op = [] __device__(size_t sample_idx, - Span const &entries, - size_t) { return entries[sample_idx]; }; // NOLINT + auto no_op = [] __device__(size_t sample_idx, Span const &entries, size_t) { + return entries[sample_idx]; + }; // NOLINT auto ft = this->feature_types_.ConstDeviceSpan(); PruneImpl(d_columns_ptr_out, in, d_columns_ptr_in, ft, out, no_op); this->columns_ptr_.Copy(columns_ptr_b_); @@ -497,14 +481,13 @@ void SketchContainer::FixError() { dh::LaunchN(in.size(), [=] __device__(size_t idx) { auto column_id = dh::SegmentId(d_columns_ptr, idx); auto in_column = in.subspan(d_columns_ptr[column_id], - d_columns_ptr[column_id + 1] - - d_columns_ptr[column_id]); + d_columns_ptr[column_id + 1] - d_columns_ptr[column_id]); idx -= d_columns_ptr[column_id]; - float prev_rmin = idx == 0 ? 0.0f : in_column[idx-1].rmin; + float prev_rmin = idx == 0 ? 0.0f : in_column[idx - 1].rmin; if (in_column[idx].rmin < prev_rmin) { in_column[idx].rmin = prev_rmin; } - float prev_rmax = idx == 0 ? 0.0f : in_column[idx-1].rmax; + float prev_rmax = idx == 0 ? 0.0f : in_column[idx - 1].rmax; if (in_column[idx].rmax < prev_rmax) { in_column[idx].rmax = prev_rmax; } @@ -515,7 +498,7 @@ void SketchContainer::FixError() { }); } -void SketchContainer::AllReduce(Context const* ctx, bool is_column_split) { +void SketchContainer::AllReduce(Context const *ctx, bool is_column_split) { curt::SetDevice(ctx->Ordinal()); auto world = collective::GetWorldSize(); if (world == 1 || is_column_split) { @@ -604,7 +587,7 @@ struct InvalidCatOp { }; } // anonymous namespace -void SketchContainer::MakeCuts(Context const* ctx, HistogramCuts* p_cuts, bool is_column_split) { +void SketchContainer::MakeCuts(Context const *ctx, HistogramCuts *p_cuts, bool is_column_split) { curt::SetDevice(ctx->Ordinal()); p_cuts->min_vals_.Resize(num_columns_); @@ -625,10 +608,10 @@ void SketchContainer::MakeCuts(Context const* ctx, HistogramCuts* p_cuts, bool i // Set up output ptr p_cuts->cut_ptrs_.SetDevice(ctx->Device()); - auto& h_out_columns_ptr = p_cuts->cut_ptrs_.HostVector(); + auto &h_out_columns_ptr = p_cuts->cut_ptrs_.HostVector(); h_out_columns_ptr.clear(); h_out_columns_ptr.push_back(0); - auto const& h_feature_types = this->feature_types_.ConstHostSpan(); + auto const &h_feature_types = this->feature_types_.ConstHostSpan(); auto d_ft = feature_types_.ConstDeviceSpan(); @@ -708,12 +691,11 @@ void SketchContainer::MakeCuts(Context const* ctx, HistogramCuts* p_cuts, bool i dh::LaunchN(total_bins, [=] __device__(size_t idx) { auto column_id = dh::SegmentId(d_out_columns_ptr, idx); - auto in_column = in_cut_values.subspan(d_in_columns_ptr[column_id], - d_in_columns_ptr[column_id + 1] - - d_in_columns_ptr[column_id]); - auto out_column = out_cut_values.subspan(d_out_columns_ptr[column_id], - d_out_columns_ptr[column_id + 1] - - d_out_columns_ptr[column_id]); + auto in_column = in_cut_values.subspan( + d_in_columns_ptr[column_id], d_in_columns_ptr[column_id + 1] - d_in_columns_ptr[column_id]); + auto out_column = + out_cut_values.subspan(d_out_columns_ptr[column_id], + d_out_columns_ptr[column_id + 1] - d_out_columns_ptr[column_id]); idx -= d_out_columns_ptr[column_id]; if (in_column.size() == 0) { // If the column is empty, we push a dummy value. It won't affect training as the @@ -745,8 +727,8 @@ void SketchContainer::MakeCuts(Context const* ctx, HistogramCuts* p_cuts, bool i out_column[idx] = last; return; } - assert(idx+1 < in_column.size()); - out_column[idx] = in_column[idx+1].value; + assert(idx + 1 < in_column.size()); + out_column[idx] = in_column[idx + 1].value; }); p_cuts->SetCategorical(this->has_categorical_, max_cat); diff --git a/src/common/quantile.h b/src/common/quantile.h index e189b259b159..8447194963fd 100644 --- a/src/common/quantile.h +++ b/src/common/quantile.h @@ -30,7 +30,7 @@ namespace xgboost::common { * \tparam DType type of data content * \tparam RType type of rank */ -template +template struct WQSummary { /*! \brief an entry in the sketch summary */ struct Entry { @@ -53,18 +53,14 @@ struct WQSummary { */ inline void CheckValid(RType eps = 0) const { CHECK(rmin >= 0 && rmax >= 0 && wmin >= 0) << "nonneg constraint"; - CHECK(rmax- rmin - wmin > -eps) << "relation constraint: min/max"; + CHECK(rmax - rmin - wmin > -eps) << "relation constraint: min/max"; } /*! \return rmin estimation for v strictly bigger than value */ - XGBOOST_DEVICE inline RType RMinNext() const { - return rmin + wmin; - } + XGBOOST_DEVICE inline RType RMinNext() const { return rmin + wmin; } /*! \return rmax estimation for v strictly smaller than value */ - XGBOOST_DEVICE inline RType RMaxPrev() const { - return rmax - wmin; - } + XGBOOST_DEVICE inline RType RMaxPrev() const { return rmax - wmin; } - friend std::ostream& operator<<(std::ostream& os, Entry const& e) { + friend std::ostream &operator<<(std::ostream &os, Entry const &e) { os << "rmin: " << e.rmin << ", " << "rmax: " << e.rmax << ", " << "wmin: " << e.wmin << ", " @@ -83,12 +79,9 @@ struct WQSummary { // default constructor QEntry() = default; // constructor - QEntry(DType value, RType weight) - : value(value), weight(weight) {} + QEntry(DType value, RType weight) : value(value), weight(weight) {} // comparator on value - inline bool operator<(const QEntry &b) const { - return value < b.value; - } + inline bool operator<(const QEntry &b) const { return value < b.value; } }; // the input queue std::vector queue; @@ -112,10 +105,12 @@ struct WQSummary { size_t j = i + 1; RType w = queue[i].weight; while (j < qtail && queue[j].value == queue[i].value) { - w += queue[j].weight; ++j; + w += queue[j].weight; + ++j; } out->data[out->size++] = Entry(wsum, wsum + w, w, queue[i].value); - wsum += w; i = j; + wsum += w; + i = j; } } }; @@ -124,8 +119,7 @@ struct WQSummary { /*! \brief number of elements in the summary */ size_t size; // constructor - WQSummary(Entry *data, size_t size) - : data(data), size(size) {} + WQSummary(Entry *data, size_t size) : data(data), size(size) {} /*! * \return the maximum error of the Summary */ @@ -142,7 +136,7 @@ struct WQSummary { * \param qvalue the value we query for * \param istart starting position */ - inline Entry Query(DType qvalue, size_t &istart) const { // NOLINT(*) + inline Entry Query(DType qvalue, size_t &istart) const { // NOLINT(*) while (istart < size && qvalue > data[istart].value) { ++istart; } @@ -156,16 +150,12 @@ struct WQSummary { if (istart == 0) { return Entry(0.0f, 0.0f, 0.0f, qvalue); } else { - return Entry(data[istart - 1].RMinNext(), - data[istart].RMaxPrev(), - 0.0f, qvalue); + return Entry(data[istart - 1].RMinNext(), data[istart].RMaxPrev(), 0.0f, qvalue); } } } /*! \return maximum rank in the summary */ - inline RType MaxRank() const { - return data[size - 1].rmax; - } + inline RType MaxRank() const { return data[size - 1].rmax; } /*! * \brief copy content from src * \param src source sketch @@ -184,14 +174,14 @@ struct WQSummary { size = src.size; std::memcpy(data, src.data, sizeof(Entry) * size); } - inline void MakeFromSorted(const Entry* entries, size_t n) { + inline void MakeFromSorted(const Entry *entries, size_t n) { size = 0; for (size_t i = 0; i < n;) { size_t j = i + 1; // ignore repeated values - for (; j < n && entries[j].value == entries[i].value; ++j) {} - data[size++] = Entry(entries[i].rmin, entries[i].rmax, entries[i].wmin, - entries[i].value); + for (; j < n && entries[j].value == entries[i].value; ++j) { + } + data[size++] = Entry(entries[i].rmin, entries[i].rmax, entries[i].wmin, entries[i].value); i = j; } } @@ -219,7 +209,8 @@ struct WQSummary { */ void SetPrune(const WQSummary &src, size_t maxsize) { if (src.size <= maxsize) { - this->CopyFrom(src); return; + this->CopyFrom(src); + return; } const RType begin = src.data[0].rmax; const RType range = src.data[src.size - 1].rmin - src.data[0].rmax; @@ -229,18 +220,19 @@ struct WQSummary { // lastidx is used to avoid duplicated records size_t i = 1, lastidx = 0; for (size_t k = 1; k < n; ++k) { - RType dx2 = 2 * ((k * range) / n + begin); + RType dx2 = 2 * ((k * range) / n + begin); // find first i such that d < (rmax[i+1] + rmin[i+1]) / 2 - while (i < src.size - 1 - && dx2 >= src.data[i + 1].rmax + src.data[i + 1].rmin) ++i; + while (i < src.size - 1 && dx2 >= src.data[i + 1].rmax + src.data[i + 1].rmin) ++i; if (i == src.size - 1) break; if (dx2 < src.data[i].RMinNext() + src.data[i + 1].RMaxPrev()) { if (i != lastidx) { - data[size++] = src.data[i]; lastidx = i; + data[size++] = src.data[i]; + lastidx = i; } } else { if (i + 1 != lastidx) { - data[size++] = src.data[i + 1]; lastidx = i + 1; + data[size++] = src.data[i + 1]; + lastidx = i + 1; } } } @@ -253,13 +245,14 @@ struct WQSummary { * \param sa first input summary to be merged * \param sb second input summary to be merged */ - inline void SetCombine(const WQSummary &sa, - const WQSummary &sb) { + inline void SetCombine(const WQSummary &sa, const WQSummary &sb) { if (sa.size == 0) { - this->CopyFrom(sb); return; + this->CopyFrom(sb); + return; } if (sb.size == 0) { - this->CopyFrom(sa); return; + this->CopyFrom(sa); + return; } CHECK(sa.size > 0 && sb.size > 0); const Entry *a = sa.data, *a_end = sa.data + sa.size; @@ -270,38 +263,38 @@ struct WQSummary { while (a != a_end && b != b_end) { // duplicated value entry if (a->value == b->value) { - *dst = Entry(a->rmin + b->rmin, - a->rmax + b->rmax, - a->wmin + b->wmin, a->value); + *dst = Entry(a->rmin + b->rmin, a->rmax + b->rmax, a->wmin + b->wmin, a->value); aprev_rmin = a->RMinNext(); bprev_rmin = b->RMinNext(); - ++dst; ++a; ++b; + ++dst; + ++a; + ++b; } else if (a->value < b->value) { - *dst = Entry(a->rmin + bprev_rmin, - a->rmax + b->RMaxPrev(), - a->wmin, a->value); + *dst = Entry(a->rmin + bprev_rmin, a->rmax + b->RMaxPrev(), a->wmin, a->value); aprev_rmin = a->RMinNext(); - ++dst; ++a; + ++dst; + ++a; } else { - *dst = Entry(b->rmin + aprev_rmin, - b->rmax + a->RMaxPrev(), - b->wmin, b->value); + *dst = Entry(b->rmin + aprev_rmin, b->rmax + a->RMaxPrev(), b->wmin, b->value); bprev_rmin = b->RMinNext(); - ++dst; ++b; + ++dst; + ++b; } } if (a != a_end) { RType brmax = (b_end - 1)->rmax; do { *dst = Entry(a->rmin + bprev_rmin, a->rmax + brmax, a->wmin, a->value); - ++dst; ++a; + ++dst; + ++a; } while (a != a_end); } if (b != b_end) { RType armax = (a_end - 1)->rmax; do { *dst = Entry(b->rmin + aprev_rmin, b->rmax + armax, b->wmin, b->value); - ++dst; ++b; + ++dst; + ++b; } while (b != b_end); } this->size = dst - data; @@ -309,26 +302,20 @@ struct WQSummary { RType err_mingap, err_maxgap, err_wgap; this->FixError(&err_mingap, &err_maxgap, &err_wgap); if (err_mingap > tol || err_maxgap > tol || err_wgap > tol) { - LOG(INFO) << "mingap=" << err_mingap - << ", maxgap=" << err_maxgap - << ", wgap=" << err_wgap; + LOG(INFO) << "mingap=" << err_mingap << ", maxgap=" << err_maxgap << ", wgap=" << err_wgap; } CHECK(size <= sa.size + sb.size) << "bug in combine"; } // helper function to print the current content of sketch inline void Print() const { for (size_t i = 0; i < this->size; ++i) { - LOG(CONSOLE) << "[" << i << "] rmin=" << data[i].rmin - << ", rmax=" << data[i].rmax - << ", wmin=" << data[i].wmin - << ", v=" << data[i].value; + LOG(CONSOLE) << "[" << i << "] rmin=" << data[i].rmin << ", rmax=" << data[i].rmax + << ", wmin=" << data[i].wmin << ", v=" << data[i].value; } } // try to fix rounding error // and re-establish invariance - inline void FixError(RType *err_mingap, - RType *err_maxgap, - RType *err_wgap) const { + inline void FixError(RType *err_mingap, RType *err_maxgap, RType *err_wgap) const { *err_mingap = 0; *err_maxgap = 0; *err_wgap = 0; @@ -355,21 +342,21 @@ struct WQSummary { }; /*! \brief try to do efficient pruning */ -template +template struct WXQSummary : public WQSummary { // redefine entry type using Entry = typename WQSummary::Entry; // constructor - WXQSummary(Entry *data, size_t size) - : WQSummary(data, size) {} + WXQSummary(Entry *data, size_t size) : WQSummary(data, size) {} // check if the block is large chunk inline static bool CheckLarge(const Entry &e, RType chunk) { - return e.RMinNext() > e.RMaxPrev() + chunk; + return e.RMinNext() > e.RMaxPrev() + chunk; } // set prune inline void SetPrune(const WQSummary &src, size_t maxsize) { if (src.size <= maxsize) { - this->CopyFrom(src); return; + this->CopyFrom(src); + return; } RType begin = src.data[0].rmax; // n is number of points exclude the min/max points @@ -403,19 +390,20 @@ struct WXQSummary : public WQSummary { // accumulate the range of the rest points mrange += src.data[i].RMaxPrev() - src.data[bid].RMinNext(); } - bid = i; ++nbig; + bid = i; + ++nbig; } } if (bid != src.size - 2) { - mrange += src.data[src.size-1].RMaxPrev() - src.data[bid].RMinNext(); + mrange += src.data[src.size - 1].RMaxPrev() - src.data[bid].RMinNext(); } } // assert: there cannot be more than n big data points if (nbig >= n) { // see what was the case LOG(INFO) << " check quantile stats, nbig=" << nbig << ", n=" << n; - LOG(INFO) << " srcsize=" << src.size << ", maxsize=" << maxsize - << ", range=" << range << ", chunk=" << chunk; + LOG(INFO) << " srcsize=" << src.size << ", maxsize=" << maxsize << ", range=" << range + << ", chunk=" << chunk; src.Print(); CHECK(nbig < n) << "quantile: too many large chunk"; } @@ -431,18 +419,19 @@ struct WXQSummary : public WQSummary { size_t i = bid; RType maxdx2 = src.data[end].RMaxPrev() * 2; for (; k < n; ++k) { - RType dx2 = 2 * ((k * mrange) / n + begin); + RType dx2 = 2 * ((k * mrange) / n + begin); if (dx2 >= maxdx2) break; - while (i < end && - dx2 >= src.data[i + 1].rmax + src.data[i + 1].rmin) ++i; + while (i < end && dx2 >= src.data[i + 1].rmax + src.data[i + 1].rmin) ++i; if (i == end) break; if (dx2 < src.data[i].RMinNext() + src.data[i + 1].RMaxPrev()) { if (i != lastidx) { - this->data[this->size++] = src.data[i]; lastidx = i; + this->data[this->size++] = src.data[i]; + lastidx = i; } } else { if (i + 1 != lastidx) { - this->data[this->size++] = src.data[i + 1]; lastidx = i + 1; + this->data[this->size++] = src.data[i + 1]; + lastidx = i + 1; } } } @@ -465,7 +454,7 @@ struct WXQSummary : public WQSummary { * \tparam RType type of rank * \tparam TSummary actual summary data structure it uses */ -template +template class QuantileSketchTemplate { public: static float constexpr kFactor = 8.0; @@ -482,8 +471,7 @@ class QuantileSketchTemplate { this->space = src.space; this->data = dmlc::BeginPtr(this->space); } - SummaryContainer() : Summary(nullptr, 0) { - } + SummaryContainer() : Summary(nullptr, 0) {} /*! \brief reserve space for summary */ inline void Reserve(size_t size) { if (size > space.size()) { @@ -509,7 +497,7 @@ class QuantileSketchTemplate { return sizeof(size_t) + sizeof(Entry) * nentry; } /*! \brief save the data structure into stream */ - template + template inline void Save(TStream &fo) const { // NOLINT(*) fo.Write(&(this->size), sizeof(this->size)); if (this->size != 0) { @@ -517,13 +505,12 @@ class QuantileSketchTemplate { } } /*! \brief load data structure from input stream */ - template + template inline void Load(TStream &fi) { // NOLINT(*) CHECK_EQ(fi.Read(&this->size, sizeof(this->size)), sizeof(this->size)); this->Reserve(this->size); if (this->size != 0) { - CHECK_EQ(fi.Read(this->data, this->size * sizeof(Entry)), - this->size * sizeof(Entry)); + CHECK_EQ(fi.Read(this->data, this->size * sizeof(Entry)), this->size * sizeof(Entry)); } } }; @@ -541,10 +528,10 @@ class QuantileSketchTemplate { level.clear(); } - inline static void LimitSizeLevel - (size_t maxn, double eps, size_t* out_nlevel, size_t* out_limit_size) { - size_t& nlevel = *out_nlevel; - size_t& limit_size = *out_limit_size; + inline static void LimitSizeLevel(size_t maxn, double eps, size_t *out_nlevel, + size_t *out_limit_size) { + size_t &nlevel = *out_nlevel; + size_t &limit_size = *out_limit_size; nlevel = 1; while (true) { limit_size = static_cast(ceil(nlevel / eps)) + 1; @@ -582,7 +569,7 @@ class QuantileSketchTemplate { inqueue.Push(x, w); } - inline void PushSummary(const Summary& summary) { + inline void PushSummary(const Summary &summary) { temp.Reserve(limit_size * 2); temp.SetPrune(summary, limit_size * 2); PushTemp(); @@ -606,7 +593,8 @@ class QuantileSketchTemplate { level[l].size = 0; } else { // if merged record is still smaller, no need to send to next level - level[l].CopyFrom(temp); break; + level[l].CopyFrom(temp); + break; } } } @@ -673,20 +661,16 @@ class QuantileSketchTemplate { * \tparam DType type of data content * \tparam RType type of rank */ -template -class WQuantileSketch : - public QuantileSketchTemplate > { -}; +template +class WQuantileSketch : public QuantileSketchTemplate> {}; /*! * \brief Quantile sketch use WXQSummary * \tparam DType type of data content * \tparam RType type of rank */ -template -class WXQuantileSketch : - public QuantileSketchTemplate > { -}; +template +class WXQuantileSketch : public QuantileSketchTemplate> {}; namespace detail { inline std::vector UnrollGroupWeights(MetaInfo const &info) { @@ -750,7 +734,7 @@ std::vector CalcColumnSize(Batch const &batch, bst_feature_t const n_ template std::vector LoadBalance(Batch const &batch, size_t nnz, bst_feature_t n_columns, - size_t const nthreads, IsValid&& is_valid) { + size_t const nthreads, IsValid &&is_valid) { /* Some sparse datasets have their mass concentrating on small number of features. To * avoid waiting for a few threads running forever, we here distribute different number * of columns to different threads according to number of entries. @@ -809,11 +793,9 @@ class SketchContainerImpl { common::Span feature_types, bool use_group); static bool UseGroup(MetaInfo const &info) { - size_t const num_groups = - info.group_ptr_.size() == 0 ? 0 : info.group_ptr_.size() - 1; + size_t const num_groups = info.group_ptr_.size() == 0 ? 0 : info.group_ptr_.size() - 1; // Use group index for weights? - bool const use_group_ind = - num_groups != 0 && (info.weights_.Size() != info.num_row_); + bool const use_group_ind = num_groups != 0 && (info.weights_.Size() != info.num_row_); return use_group_ind; } @@ -821,9 +803,8 @@ class SketchContainerImpl { size_t const base_rowid) { CHECK_LT(base_rowid, group_ptr.back()) << "Row: " << base_rowid << " is not found in any group."; - bst_group_t group_ind = - std::upper_bound(group_ptr.cbegin(), group_ptr.cend() - 1, base_rowid) - - group_ptr.cbegin() - 1; + bst_group_t group_ind = std::upper_bound(group_ptr.cbegin(), group_ptr.cend() - 1, base_rowid) - + group_ptr.cbegin() - 1; return group_ind; } // Gather sketches from all workers. @@ -892,7 +873,7 @@ class SketchContainerImpl { private: // Merge all categories from other workers. - void AllreduceCategories(Context const* ctx, MetaInfo const& info); + void AllreduceCategories(Context const *ctx, MetaInfo const &info); }; class HostSketchContainer : public SketchContainerImpl> { @@ -920,7 +901,7 @@ struct SortedQuantile { /*! \brief current size of sketch */ double next_goal; // pointer to the sketch to put things in - common::WXQuantileSketch* sketch; + common::WXQuantileSketch *sketch; // initialize the space inline void Init(unsigned max_size) { next_goal = -1.0f; diff --git a/src/common/random.cu b/src/common/random.cu index f5811d924e17..44ba65df89a5 100644 --- a/src/common/random.cu +++ b/src/common/random.cu @@ -3,7 +3,8 @@ */ #include // for shuffle -#include // for shared_ptr +#include // for max +#include // for shared_ptr #include "algorithm.cuh" // for ArgSort #include "cuda_context.cuh" // for CUDAContext diff --git a/src/common/random.h b/src/common/random.h index bea631c958fe..ac770324b86e 100644 --- a/src/common/random.h +++ b/src/common/random.h @@ -18,8 +18,8 @@ #include #include "../collective/broadcast.h" // for Broadcast -#include "algorithm.h" // ArgSort -#include "xgboost/context.h" // Context +#include "algorithm.h" // ArgSort +#include "xgboost/context.h" // Context #include "xgboost/host_device_vector.h" #include "xgboost/linalg.h" @@ -39,7 +39,7 @@ using GlobalRandomEngine = RandomEngine; * This random engine is thread-local and * only visible to current thread. */ -GlobalRandomEngine& GlobalRandom(); // NOLINT(*) +GlobalRandomEngine& GlobalRandom(); // NOLINT(*) /* * Original paper: @@ -47,7 +47,7 @@ GlobalRandomEngine& GlobalRandom(); // NOLINT(*) * * Blog: * https://timvieira.github.io/blog/post/2019/09/16/algorithms-for-sampling-without-replacement/ -*/ + */ template std::vector WeightedSamplingWithoutReplacement(Context const* ctx, std::vector const& array, std::vector const& weights, size_t n) { diff --git a/src/common/ranking_utils.cc b/src/common/ranking_utils.cc index d477225a4efe..f70ebc17f3ce 100644 --- a/src/common/ranking_utils.cc +++ b/src/common/ranking_utils.cc @@ -3,11 +3,11 @@ */ #include "ranking_utils.h" -#include // for copy_n, max, min, none_of, all_of -#include // for size_t -#include // for sscanf -#include // for greater -#include // for char_traits, string +#include // for copy_n, max, min, none_of, all_of +#include // for size_t +#include // for sscanf +#include // for greater +#include // for char_traits, string #include "algorithm.h" // for ArgSort #include "linalg_op.h" // for cbegin, cend diff --git a/src/common/ranking_utils.cu b/src/common/ranking_utils.cu index 590dd93a321b..cdb894606962 100644 --- a/src/common/ranking_utils.cu +++ b/src/common/ranking_utils.cu @@ -8,20 +8,20 @@ #include // for reduce #include // for inclusive_scan -#include // for size_t +#include // for size_t -#include "algorithm.cuh" // for SegmentedArgSort -#include "cuda_context.cuh" // for CUDAContext -#include "device_helpers.cuh" // for MakeTransformIterator, LaunchN -#include "optional_weight.h" // for MakeOptionalWeights, OptionalWeights -#include "ranking_utils.cuh" // for ThreadsForMean +#include "algorithm.cuh" // for SegmentedArgSort +#include "cuda_context.cuh" // for CUDAContext +#include "device_helpers.cuh" // for MakeTransformIterator, LaunchN +#include "optional_weight.h" // for MakeOptionalWeights, OptionalWeights +#include "ranking_utils.cuh" // for ThreadsForMean #include "ranking_utils.h" -#include "threading_utils.cuh" // for SegmentedTrapezoidThreads -#include "xgboost/base.h" // for XGBOOST_DEVICE, bst_group_t -#include "xgboost/context.h" // for Context -#include "xgboost/linalg.h" // for VectorView, All, Range -#include "xgboost/logging.h" // for CHECK -#include "xgboost/span.h" // for Span +#include "threading_utils.cuh" // for SegmentedTrapezoidThreads +#include "xgboost/base.h" // for XGBOOST_DEVICE, bst_group_t +#include "xgboost/context.h" // for Context +#include "xgboost/linalg.h" // for VectorView, All, Range +#include "xgboost/logging.h" // for CHECK +#include "xgboost/span.h" // for Span namespace xgboost::ltr { namespace cuda_impl { diff --git a/src/common/ranking_utils.h b/src/common/ranking_utils.h index 16a264fdc967..f6a8929d93ad 100644 --- a/src/common/ranking_utils.h +++ b/src/common/ranking_utils.h @@ -3,13 +3,13 @@ */ #ifndef XGBOOST_COMMON_RANKING_UTILS_H_ #define XGBOOST_COMMON_RANKING_UTILS_H_ -#include // for min -#include // for log2, fabs, floor -#include // for size_t -#include // for uint32_t, uint8_t, int32_t -#include // for numeric_limits -#include // for char_traits, string -#include // for vector +#include // for min +#include // for log2, fabs, floor +#include // for size_t +#include // for uint32_t, uint8_t, int32_t +#include // for numeric_limits +#include // for char_traits, string +#include // for vector #include "dmlc/parameter.h" // for FieldEntry, DMLC_DECLARE_FIELD #include "error_msg.h" // for GroupWeight, GroupSize, InvalidCUDAOrdinal @@ -330,8 +330,8 @@ class NDCGCache : public RankingCache { } linalg::VectorView InvIDCG(Context const* ctx) const { - // This function doesn't have sycl-specific implementation yet. - // For that reason we transfer data to host in case of sycl is used for propper execution. + // This function doesn't have sycl-specific implementation yet. + // For that reason we transfer data to host in case of sycl is used for propper execution. return inv_idcg_.View(ctx->Device().IsSycl() ? DeviceOrd::CPU() : ctx->Device()); } common::Span Discount(Context const* ctx) const { diff --git a/src/common/resource.cu b/src/common/resource.cu index f317c70de1f1..6a24737925c1 100644 --- a/src/common/resource.cu +++ b/src/common/resource.cu @@ -1,6 +1,8 @@ /** * Copyright 2024-2025, XGBoost Contributors */ +#include // for string + #include "cuda_stream.h" // for DefaultStream #include "device_helpers.cuh" // for CurrentDevice #include "resource.cuh" diff --git a/src/common/row_set.h b/src/common/row_set.h index 8df2a7a36839..84fd084ecbcc 100644 --- a/src/common/row_set.h +++ b/src/common/row_set.h @@ -77,9 +77,7 @@ class RowSetCollection { } // clear up things - void Clear() { - elem_of_each_node_.clear(); - } + void Clear() { elem_of_each_node_.clear(); } // initialize node id 0->everything void Init() { CHECK(elem_of_each_node_.empty()); diff --git a/src/common/stats.cc b/src/common/stats.cc index b1c65aaf1a50..9c65133f8721 100644 --- a/src/common/stats.cc +++ b/src/common/stats.cc @@ -3,8 +3,9 @@ */ #include "stats.h" -#include // std::size_t -#include // std::accumulate +#include // std::max +#include // std::size_t +#include // std::accumulate #include "../collective/aggregator.h" // for GlobalSum #include "linalg_op.h" // for Matrix diff --git a/src/common/survival_util.cc b/src/common/survival_util.cc index 2e4d81bf649a..c5274828bf38 100644 --- a/src/common/survival_util.cc +++ b/src/common/survival_util.cc @@ -6,9 +6,10 @@ * \author Avinash Barnwal, Hyunsu Cho and Toby Hocking */ -#include #include "survival_util.h" +#include + namespace xgboost { namespace common { diff --git a/src/common/survival_util.h b/src/common/survival_util.h index e891edb5428c..d0ded90cc102 100644 --- a/src/common/survival_util.h +++ b/src/common/survival_util.h @@ -15,9 +15,11 @@ */ #include -#include + #include #include +#include + #include "probability_distribution.h" DECLARE_FIELD_ENUM_CLASS(xgboost::common::ProbabilityDistributionType); @@ -27,13 +29,16 @@ namespace common { #ifndef __CUDACC__ -using std::log; using std::fmax; +using std::log; #endif // __CUDACC__ enum class CensoringType : uint8_t { - kUncensored, kRightCensored, kLeftCensored, kIntervalCensored + kUncensored, + kRightCensored, + kLeftCensored, + kIntervalCensored }; namespace aft { @@ -60,13 +65,13 @@ inline double Clip(double x, double x_min, double x_max) { return x; } -template -XGBOOST_DEVICE inline double -GetLimitGradAtInfPred(CensoringType censor_type, bool sign, double sigma); +template +XGBOOST_DEVICE inline double GetLimitGradAtInfPred(CensoringType censor_type, bool sign, + double sigma); -template -XGBOOST_DEVICE inline double -GetLimitHessAtInfPred(CensoringType censor_type, bool sign, double sigma); +template +XGBOOST_DEVICE inline double GetLimitHessAtInfPred(CensoringType censor_type, bool sign, + double sigma); } // namespace aft @@ -82,20 +87,22 @@ struct AFTParam : public XGBoostParameter { .add_enum("normal", ProbabilityDistributionType::kNormal) .add_enum("logistic", ProbabilityDistributionType::kLogistic) .add_enum("extreme", ProbabilityDistributionType::kExtreme) - .describe("Choice of distribution for the noise term in " - "Accelerated Failure Time model"); + .describe( + "Choice of distribution for the noise term in " + "Accelerated Failure Time model"); DMLC_DECLARE_FIELD(aft_loss_distribution_scale) .set_default(1.0f) - .describe("Scaling factor used to scale the distribution in " - "Accelerated Failure Time model"); + .describe( + "Scaling factor used to scale the distribution in " + "Accelerated Failure Time model"); } }; /*! \brief The AFT loss function */ -template +template struct AFTLoss { - XGBOOST_DEVICE inline static - double Loss(double y_lower, double y_upper, double y_pred, double sigma) { + XGBOOST_DEVICE inline static double Loss(double y_lower, double y_upper, double y_pred, + double sigma) { const double log_y_lower = log(y_lower); const double log_y_upper = log(y_upper); @@ -127,8 +134,8 @@ struct AFTLoss { return cost; } - XGBOOST_DEVICE inline static - double Gradient(double y_lower, double y_upper, double y_pred, double sigma) { + XGBOOST_DEVICE inline static double Gradient(double y_lower, double y_upper, double y_pred, + double sigma) { 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 @@ -176,8 +183,8 @@ struct AFTLoss { return aft::Clip(gradient, aft::kMinGradient, aft::kMaxGradient); } - XGBOOST_DEVICE inline static - double Hessian(double y_lower, double y_upper, double y_pred, double sigma) { + XGBOOST_DEVICE inline static double Hessian(double y_lower, double y_upper, double y_pred, + double sigma) { 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 @@ -238,103 +245,103 @@ struct AFTLoss { namespace aft { template <> -XGBOOST_DEVICE inline double -GetLimitGradAtInfPred(CensoringType censor_type, bool sign, double sigma) { +XGBOOST_DEVICE inline double GetLimitGradAtInfPred(CensoringType censor_type, + bool sign, double sigma) { // Remove unused parameter compiler warning. - (void) sigma; + (void)sigma; switch (censor_type) { - case CensoringType::kUncensored: - return sign ? kMinGradient : kMaxGradient; - case CensoringType::kRightCensored: - return sign ? kMinGradient : 0.0; - case CensoringType::kLeftCensored: - return sign ? 0.0 : kMaxGradient; - case CensoringType::kIntervalCensored: - return sign ? kMinGradient : kMaxGradient; + case CensoringType::kUncensored: + return sign ? kMinGradient : kMaxGradient; + case CensoringType::kRightCensored: + return sign ? kMinGradient : 0.0; + case CensoringType::kLeftCensored: + return sign ? 0.0 : kMaxGradient; + case CensoringType::kIntervalCensored: + return sign ? kMinGradient : kMaxGradient; } return std::numeric_limits::quiet_NaN(); } template <> -XGBOOST_DEVICE inline double -GetLimitHessAtInfPred(CensoringType censor_type, bool sign, double sigma) { +XGBOOST_DEVICE inline double GetLimitHessAtInfPred(CensoringType censor_type, + bool sign, double sigma) { switch (censor_type) { - case CensoringType::kUncensored: - return 1.0 / (sigma * sigma); - case CensoringType::kRightCensored: - return sign ? (1.0 / (sigma * sigma)) : kMinHessian; - case CensoringType::kLeftCensored: - return sign ? kMinHessian : (1.0 / (sigma * sigma)); - case CensoringType::kIntervalCensored: - return 1.0 / (sigma * sigma); + case CensoringType::kUncensored: + return 1.0 / (sigma * sigma); + case CensoringType::kRightCensored: + return sign ? (1.0 / (sigma * sigma)) : kMinHessian; + case CensoringType::kLeftCensored: + return sign ? kMinHessian : (1.0 / (sigma * sigma)); + case CensoringType::kIntervalCensored: + return 1.0 / (sigma * sigma); } return std::numeric_limits::quiet_NaN(); } template <> -XGBOOST_DEVICE inline double -GetLimitGradAtInfPred(CensoringType censor_type, bool sign, double sigma) { +XGBOOST_DEVICE inline double GetLimitGradAtInfPred(CensoringType censor_type, + bool sign, double sigma) { switch (censor_type) { - case CensoringType::kUncensored: - return sign ? (-1.0 / sigma) : (1.0 / sigma); - case CensoringType::kRightCensored: - return sign ? (-1.0 / sigma) : 0.0; - case CensoringType::kLeftCensored: - return sign ? 0.0 : (1.0 / sigma); - case CensoringType::kIntervalCensored: - return sign ? (-1.0 / sigma) : (1.0 / sigma); + case CensoringType::kUncensored: + return sign ? (-1.0 / sigma) : (1.0 / sigma); + case CensoringType::kRightCensored: + return sign ? (-1.0 / sigma) : 0.0; + case CensoringType::kLeftCensored: + return sign ? 0.0 : (1.0 / sigma); + case CensoringType::kIntervalCensored: + return sign ? (-1.0 / sigma) : (1.0 / sigma); } return std::numeric_limits::quiet_NaN(); } template <> -XGBOOST_DEVICE inline double -GetLimitHessAtInfPred(CensoringType censor_type, bool sign, double sigma) { +XGBOOST_DEVICE inline double GetLimitHessAtInfPred(CensoringType censor_type, + bool sign, double sigma) { // Remove unused parameter compiler warning. - (void) sign; - (void) sigma; + (void)sign; + (void)sigma; switch (censor_type) { - case CensoringType::kUncensored: - case CensoringType::kRightCensored: - case CensoringType::kLeftCensored: - case CensoringType::kIntervalCensored: - return kMinHessian; + case CensoringType::kUncensored: + case CensoringType::kRightCensored: + case CensoringType::kLeftCensored: + case CensoringType::kIntervalCensored: + return kMinHessian; } return std::numeric_limits::quiet_NaN(); } template <> -XGBOOST_DEVICE inline double -GetLimitGradAtInfPred(CensoringType censor_type, bool sign, double sigma) { +XGBOOST_DEVICE inline double GetLimitGradAtInfPred(CensoringType censor_type, + bool sign, double sigma) { switch (censor_type) { - case CensoringType::kUncensored: - return sign ? kMinGradient : (1.0 / sigma); - case CensoringType::kRightCensored: - return sign ? kMinGradient : 0.0; - case CensoringType::kLeftCensored: - return sign ? 0.0 : (1.0 / sigma); - case CensoringType::kIntervalCensored: - return sign ? kMinGradient : (1.0 / sigma); + case CensoringType::kUncensored: + return sign ? kMinGradient : (1.0 / sigma); + case CensoringType::kRightCensored: + return sign ? kMinGradient : 0.0; + case CensoringType::kLeftCensored: + return sign ? 0.0 : (1.0 / sigma); + case CensoringType::kIntervalCensored: + return sign ? kMinGradient : (1.0 / sigma); } return std::numeric_limits::quiet_NaN(); } template <> -XGBOOST_DEVICE inline double -GetLimitHessAtInfPred(CensoringType censor_type, bool sign, double sigma) { +XGBOOST_DEVICE inline double GetLimitHessAtInfPred(CensoringType censor_type, + bool sign, double sigma) { // Remove unused parameter compiler warning. - (void) sigma; + (void)sigma; switch (censor_type) { - case CensoringType::kUncensored: - case CensoringType::kRightCensored: - return sign ? kMaxHessian : kMinHessian; - case CensoringType::kLeftCensored: - return kMinHessian; - case CensoringType::kIntervalCensored: - return sign ? kMaxHessian : kMinHessian; + case CensoringType::kUncensored: + case CensoringType::kRightCensored: + return sign ? kMaxHessian : kMinHessian; + case CensoringType::kLeftCensored: + return kMinHessian; + case CensoringType::kIntervalCensored: + return sign ? kMaxHessian : kMinHessian; } return std::numeric_limits::quiet_NaN(); } diff --git a/src/common/threading_utils.cc b/src/common/threading_utils.cc index 16afe72a0898..df52338c4521 100644 --- a/src/common/threading_utils.cc +++ b/src/common/threading_utils.cc @@ -62,7 +62,9 @@ std::int32_t GetCGroupV2Count(std::filesystem::path const& bandwidth_path) noexc std::int32_t a{0}, b{0}; - auto warn = [] { LOG(WARNING) << "Invalid cgroupv2 file."; }; + auto warn = [] { + LOG(WARNING) << "Invalid cgroupv2 file."; + }; try { std::ifstream fin{bandwidth_path, std::ios::in}; fin >> a; diff --git a/src/common/threading_utils.h b/src/common/threading_utils.h index b20b1bc08069..aab2bd98f396 100644 --- a/src/common/threading_utils.h +++ b/src/common/threading_utils.h @@ -103,9 +103,7 @@ class BlockedSpace2d { } // Amount of blocks(tasks) in a space - [[nodiscard]] std::size_t Size() const { - return ranges_.size(); - } + [[nodiscard]] std::size_t Size() const { return ranges_.size(); } // get index of the first dimension of i-th block(task) [[nodiscard]] std::size_t GetFirstDimension(std::size_t i) const { @@ -136,7 +134,6 @@ class BlockedSpace2d { std::vector first_dimension_; }; - // Wrapper to implement nested parallelism with simple omp parallel for template void ParallelFor2d(const BlockedSpace2d& space, std::int32_t n_threads, Func&& func) { @@ -200,48 +197,48 @@ void ParallelFor(Index size, std::int32_t n_threads, Sched sched, Func&& fn) { dmlc::OMPException exc; switch (sched.sched) { - case Sched::kAuto: { + case Sched::kAuto: { #pragma omp parallel for num_threads(n_threads) - for (OmpInd i = 0; i < length; ++i) { - exc.Run(fn, i); - } - break; - } - case Sched::kDynamic: { - if (sched.chunk == 0) { -#pragma omp parallel for num_threads(n_threads) schedule(dynamic) for (OmpInd i = 0; i < length; ++i) { exc.Run(fn, i); } - } else { + break; + } + case Sched::kDynamic: { + if (sched.chunk == 0) { +#pragma omp parallel for num_threads(n_threads) schedule(dynamic) + for (OmpInd i = 0; i < length; ++i) { + exc.Run(fn, i); + } + } else { #pragma omp parallel for num_threads(n_threads) schedule(dynamic, sched.chunk) - for (OmpInd i = 0; i < length; ++i) { - exc.Run(fn, i); + for (OmpInd i = 0; i < length; ++i) { + exc.Run(fn, i); + } } + break; } - break; - } - case Sched::kStatic: { - if (sched.chunk == 0) { + case Sched::kStatic: { + if (sched.chunk == 0) { #pragma omp parallel for num_threads(n_threads) schedule(static) - for (OmpInd i = 0; i < length; ++i) { - exc.Run(fn, i); - } - } else { + for (OmpInd i = 0; i < length; ++i) { + exc.Run(fn, i); + } + } else { #pragma omp parallel for num_threads(n_threads) schedule(static, sched.chunk) - for (OmpInd i = 0; i < length; ++i) { - exc.Run(fn, i); + for (OmpInd i = 0; i < length; ++i) { + exc.Run(fn, i); + } } + break; } - break; - } - case Sched::kGuided: { + case Sched::kGuided: { #pragma omp parallel for num_threads(n_threads) schedule(guided) - for (OmpInd i = 0; i < length; ++i) { - exc.Run(fn, i); + for (OmpInd i = 0; i < length; ++i) { + exc.Run(fn, i); + } + break; } - break; - } } exc.Rethrow(); } diff --git a/src/common/timer.cc b/src/common/timer.cc index 35c779f12c82..0e9db9bc353d 100644 --- a/src/common/timer.cc +++ b/src/common/timer.cc @@ -3,6 +3,7 @@ */ #include "timer.h" +#include #include #include "../collective/communicator-inl.h" diff --git a/src/common/timer.h b/src/common/timer.h index 3308dfb653dd..098b7e67ddf0 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -29,8 +30,7 @@ struct Timer { SecondsT Duration() const { return ClockT::now() - start; } void PrintElapsed(std::string label) { char buffer[255]; - snprintf(buffer, sizeof(buffer), "%s:\t %fs", label.c_str(), - SecondsT(elapsed).count()); + snprintf(buffer, sizeof(buffer), "%s:\t %fs", label.c_str(), SecondsT(elapsed).count()); LOG(CONSOLE) << buffer; Reset(); } @@ -57,7 +57,7 @@ struct Monitor { std::map statistics_map_; Timer self_timer_; - void PrintStatistics(StatMap const& statistics) const; + void PrintStatistics(StatMap const &statistics) const; public: Monitor() { self_timer_.Start(); } diff --git a/src/common/transform.h b/src/common/transform.h index e23ffb5398c7..74f62de5cf07 100644 --- a/src/common/transform.h +++ b/src/common/transform.h @@ -17,11 +17,11 @@ #include "xgboost/host_device_vector.h" #include "xgboost/span.h" -#if defined (__CUDACC__) +#if defined(__CUDACC__) #include "device_helpers.cuh" #endif // defined (__CUDACC__) -#if defined (SYCL_LANGUAGE_VERSION) +#if defined(SYCL_LANGUAGE_VERSION) #include "../plugin/sycl/common/transform.h" #endif // defined (SYCL_LANGUAGE_VERSION) @@ -34,8 +34,7 @@ namespace detail { #if defined(__CUDACC__) template -__global__ void LaunchCUDAKernel(Functor _func, Range _range, - SpanType... _spans) { +__global__ void LaunchCUDAKernel(Functor _func, Range _range, SpanType... _spans) { for (auto i : dh::GridStrideRange(*_range.begin(), *_range.end())) { _func(i, _spans...); } @@ -87,25 +86,24 @@ class Transform { private: // CUDA UnpackHDV template - Span UnpackHDVOnDevice(HostDeviceVector* _vec) const { + Span UnpackHDVOnDevice(HostDeviceVector *_vec) const { auto span = _vec->DeviceSpan(); return span; } template - Span UnpackHDVOnDevice(const HostDeviceVector* _vec) const { + Span UnpackHDVOnDevice(const HostDeviceVector *_vec) const { auto span = _vec->ConstDeviceSpan(); return span; } // CPU UnpackHDV template - Span UnpackHDV(HostDeviceVector* _vec) const { - return Span {_vec->HostPointer(), - static_cast::index_type>(_vec->Size())}; + Span UnpackHDV(HostDeviceVector *_vec) const { + return Span{_vec->HostPointer(), static_cast::index_type>(_vec->Size())}; } template - Span UnpackHDV(const HostDeviceVector* _vec) const { - return Span {_vec->ConstHostPointer(), - static_cast::index_type>(_vec->Size())}; + Span UnpackHDV(const HostDeviceVector *_vec) const { + return Span{_vec->ConstHostPointer(), + static_cast::index_type>(_vec->Size())}; } // Recursive sync host template @@ -114,7 +112,7 @@ class Transform { } template void SyncHost(const HostDeviceVector *_vector, - const HostDeviceVector *... _vectors) const { + const HostDeviceVector *..._vectors) const { _vector->ConstHostPointer(); SyncHost(_vectors...); } @@ -124,17 +122,15 @@ class Transform { vector->SetDevice(device); } template - void UnpackShard(DeviceOrd device, - const HostDeviceVector *_vector, - const HostDeviceVector *... _vectors) const { + void UnpackShard(DeviceOrd device, const HostDeviceVector *_vector, + const HostDeviceVector *..._vectors) const { _vector->SetDevice(device); UnpackShard(device, _vectors...); } #if defined(__CUDACC__) - template * = nullptr, - typename... HDV> - void LaunchCUDA(Functor _func, HDV*... _vectors) const { + template * = nullptr, typename... HDV> + void LaunchCUDA(Functor _func, HDV *..._vectors) const { UnpackShard(device_, _vectors...); size_t range_size = *range_.end() - *range_.begin(); @@ -143,10 +139,9 @@ class Transform { // This deals with situation like multi-class setting where // granularity is used in data vector. size_t shard_size = range_size; - Range shard_range {0, static_cast(shard_size)}; + Range shard_range{0, static_cast(shard_size)}; dh::safe_cuda(cudaSetDevice(device_.ordinal)); - const int kGrids = - static_cast(DivRoundUp(*(range_.end()), kBlockThreads)); + const int kGrids = static_cast(DivRoundUp(*(range_.end()), kBlockThreads)); if (kGrids == 0) { return; } @@ -158,24 +153,24 @@ class Transform { template * = nullptr, typename... HDV> void LaunchCUDA(Functor _func, HDV *...) const { // Remove unused parameter compiler warning. - (void) _func; + (void)_func; LOG(FATAL) << "Not part of device code. WITH_CUDA: " << WITH_CUDA(); } #endif // defined(__CUDACC__) -#if defined (SYCL_LANGUAGE_VERSION) +#if defined(SYCL_LANGUAGE_VERSION) template - void LaunchSycl(Functor _func, HDV*... _vectors) const { + void LaunchSycl(Functor _func, HDV *..._vectors) const { UnpackShard(device_, _vectors...); size_t range_size = *range_.end() - *range_.begin(); - Range shard_range {0, static_cast(range_size)}; + Range shard_range{0, static_cast(range_size)}; sycl::common::LaunchSyclKernel(device_, _func, shard_range, UnpackHDVOnDevice(_vectors)...); } #else template - void LaunchSycl(Functor _func, HDV *... _vectors) const { + void LaunchSycl(Functor _func, HDV *..._vectors) const { LaunchCPU(_func, _vectors...); } #endif // defined(SYCL_LANGUAGE_VERSION) diff --git a/src/common/version.cc b/src/common/version.cc index d5407e04b1b4..7031e5a4677f 100644 --- a/src/common/version.cc +++ b/src/common/version.cc @@ -1,32 +1,32 @@ /*! * Copyright 2019 XGBoost contributors */ +#include "version.h" + #include #include #include #include -#include "xgboost/logging.h" #include "xgboost/json.h" +#include "xgboost/logging.h" #include "xgboost/version_config.h" -#include "version.h" namespace xgboost { -const Version::TripletT Version::kInvalid {-1, -1, -1}; +const Version::TripletT Version::kInvalid{-1, -1, -1}; Version::TripletT Version::Load(Json const& in) { if (get(in).find("version") == get(in).cend()) { return kInvalid; } - Integer::Int major {0}, minor {0}, patch {0}; + Integer::Int major{0}, minor{0}, patch{0}; try { auto const& j_version = get(in["version"]); - std::tie(major, minor, patch) = std::make_tuple( - get(j_version.at(0)), - get(j_version.at(1)), - get(j_version.at(2))); + std::tie(major, minor, patch) = + std::make_tuple(get(j_version.at(0)), get(j_version.at(1)), + get(j_version.at(2))); } catch (dmlc::Error const& e) { LOG(FATAL) << "Invaid version format in loaded JSON object: " << in; } @@ -37,10 +37,11 @@ Version::TripletT Version::Load(Json const& in) { Version::TripletT Version::Load(dmlc::Stream* fi) { XGBoostVersionT major{0}, minor{0}, patch{0}; // This is only used in DMatrix serialization, so doesn't break model compatibility. - std::string msg { "Incorrect version format found in binary file. " - "Binary file from XGBoost < 1.0.0 is no longer supported. " - "Please generate it again." }; - std::string verstr { u8"version:" }, read; + std::string msg{ + "Incorrect version format found in binary file. " + "Binary file from XGBoost < 1.0.0 is no longer supported. " + "Please generate it again."}; + std::string verstr{u8"version:"}, read; read.resize(verstr.size(), 0); CHECK_EQ(fi->Read(&read[0], verstr.size()), verstr.size()) << msg; @@ -58,16 +59,15 @@ Version::TripletT Version::Load(dmlc::Stream* fi) { void Version::Save(Json* out) { Integer::Int major, minor, patch; - std::tie(major, minor, patch)= Self(); - (*out)["version"] = std::vector{Json(Integer{major}), - Json(Integer{minor}), - Json(Integer{patch})}; + std::tie(major, minor, patch) = Self(); + (*out)["version"] = + std::vector{Json(Integer{major}), Json(Integer{minor}), Json(Integer{patch})}; } void Version::Save(dmlc::Stream* fo) { XGBoostVersionT major, minor, patch; std::tie(major, minor, patch) = Self(); - std::string verstr { u8"version:" }; + std::string verstr{u8"version:"}; fo->Write(&verstr[0], verstr.size()); fo->Write(major); fo->Write(minor); @@ -84,8 +84,6 @@ Version::TripletT Version::Self() { return std::make_tuple(XGBOOST_VER_MAJOR, XGBOOST_VER_MINOR, XGBOOST_VER_PATCH); } -bool Version::Same(TripletT const& triplet) { - return triplet == Self(); -} +bool Version::Same(TripletT const& triplet) { return triplet == Self(); } } // namespace xgboost diff --git a/src/common/version.h b/src/common/version.h index cf562abd98c5..2c933e4b3093 100644 --- a/src/common/version.h +++ b/src/common/version.h @@ -5,6 +5,7 @@ #define XGBOOST_COMMON_VERSION_H_ #include + #include #include @@ -31,5 +32,5 @@ struct Version { static bool Same(TripletT const& triplet); }; -} // namespace xgboost +} // namespace xgboost #endif // XGBOOST_COMMON_VERSION_H_ diff --git a/src/context.cc b/src/context.cc index ae1874b02e4a..9b68c2940b9c 100644 --- a/src/context.cc +++ b/src/context.cc @@ -8,8 +8,10 @@ #include // for find_if #include // for from_chars #include // for distance +#include // for numeric_limits #include // for optional #include // for regex_replace, regex_match +#include // for string #include "common/cuda_rt_utils.h" // for AllVisibleGPUs #include "common/error_msg.h" // WarnDeprecatedGPUId @@ -108,7 +110,9 @@ DeviceOrd CUDAOrdinal(DeviceOrd device, bool) { - gpu - gpu: # e.g. gpu:0 )"}; - auto fatal = [&] { LOG(FATAL) << msg << "Got: `" << input << "`."; }; + auto fatal = [&] { + LOG(FATAL) << msg << "Got: `" << input << "`."; + }; #if defined(__MINGW32__) // mingw hangs on regex using rtools 430. Basic checks only. @@ -140,9 +144,8 @@ DeviceOrd CUDAOrdinal(DeviceOrd device, bool) { auto split_it = std::find(s_device.cbegin(), s_device.cend(), ':'); // For these cases we need to move iterator to the end, not to look for a ordinal. - if ((s_device == "sycl:cpu") || - (s_device == "sycl:gpu")) { - split_it = s_device.cend(); + if ((s_device == "sycl:cpu") || (s_device == "sycl:gpu")) { + split_it = s_device.cend(); } // For s_device like "sycl:gpu:1" @@ -268,11 +271,11 @@ std::int32_t Context::Threads() const { } DeviceOrd Context::DeviceFP64() const { - #if defined(XGBOOST_USE_SYCL) - return sycl::DeviceFP64(device_); - #else - return device_; - #endif // defined(XGBOOST_USE_SYCL) +#if defined(XGBOOST_USE_SYCL) + return sycl::DeviceFP64(device_); +#else + return device_; +#endif // defined(XGBOOST_USE_SYCL) } #if !defined(XGBOOST_USE_CUDA) diff --git a/src/data/adapter.h b/src/data/adapter.h index 88b7650c75bc..37928ff7a504 100644 --- a/src/data/adapter.h +++ b/src/data/adapter.h @@ -68,7 +68,7 @@ namespace xgboost::data { /** \brief An adapter can return this value for number of rows or columns * indicating that this value is currently unknown and should be inferred while * passing over the data. */ -constexpr size_t kAdapterUnknownSize = std::numeric_limits::max(); +constexpr size_t kAdapterUnknownSize = std::numeric_limits::max(); namespace detail { @@ -114,9 +114,7 @@ class DenseAdapterBatch : public detail::NoMetaInfo { : row_idx_(row_idx), size_(size), values_(values) {} size_t Size() const { return size_; } - COOTuple GetElement(size_t idx) const { - return COOTuple{row_idx_, idx, values_[idx]}; - } + COOTuple GetElement(size_t idx) const { return COOTuple{row_idx_, idx, values_[idx]}; } private: size_t row_idx_; @@ -142,9 +140,7 @@ class DenseAdapterBatch : public detail::NoMetaInfo { class DenseAdapter : public detail::SingleBatchDataIter { public: DenseAdapter(const float* values, size_t num_rows, size_t num_features) - : batch_(values, num_rows, num_features), - num_rows_(num_rows), - num_columns_(num_features) {} + : batch_(values, num_rows, num_features), num_rows_(num_rows), num_columns_(num_features) {} const DenseAdapterBatch& Value() const override { return batch_; } [[nodiscard]] std::size_t NumRows() const { return num_rows_; } @@ -173,16 +169,12 @@ class ArrayAdapterBatch : public detail::NoMetaInfo { size_t Size() const { return array_interface_.Shape<1>(); } - COOTuple GetElement(size_t idx) const { - return {ridx_, idx, array_interface_(ridx_, idx)}; - } + COOTuple GetElement(size_t idx) const { return {ridx_, idx, array_interface_(ridx_, idx)}; } }; public: ArrayAdapterBatch() = default; - Line const GetLine(size_t idx) const { - return Line{array_interface_, idx}; - } + Line const GetLine(size_t idx) const { return Line{array_interface_, idx}; } [[nodiscard]] std::size_t NumRows() const { return array_interface_.Shape<0>(); } [[nodiscard]] std::size_t NumCols() const { return array_interface_.Shape<1>(); } @@ -226,18 +218,14 @@ class CSRArrayAdapterBatch : public detail::NoMetaInfo { size_t offset_; public: - Line(ArrayInterface<1> indices, ArrayInterface<1> values, size_t ridx, - size_t offset) - : indices_{std::move(indices)}, values_{std::move(values)}, ridx_{ridx}, - offset_{offset} {} + Line(ArrayInterface<1> indices, ArrayInterface<1> values, size_t ridx, size_t offset) + : indices_{std::move(indices)}, values_{std::move(values)}, ridx_{ridx}, offset_{offset} {} [[nodiscard]] COOTuple GetElement(std::size_t idx) const { return {ridx_, TypedIndex{indices_}(offset_ + idx), values_(offset_ + idx)}; } - [[nodiscard]] std::size_t Size() const { - return values_.Shape<0>(); - } + [[nodiscard]] std::size_t Size() const { return values_.Shape<0>(); } }; public: @@ -250,8 +238,7 @@ class CSRArrayAdapterBatch : public detail::NoMetaInfo { : indptr_{std::move(indptr)}, indices_{std::move(indices)}, values_{std::move(values)}, - n_features_{n_features} { - } + n_features_{n_features} {} [[nodiscard]] std::size_t NumRows() const { size_t size = indptr_.Shape<0>(); @@ -283,11 +270,10 @@ class CSRArrayAdapterBatch : public detail::NoMetaInfo { */ class CSRArrayAdapter : public detail::SingleBatchDataIter { public: - CSRArrayAdapter(StringView indptr, StringView indices, StringView values, - size_t num_cols) + CSRArrayAdapter(StringView indptr, StringView indices, StringView values, size_t num_cols) : indptr_{indptr}, indices_{indices}, values_{values}, num_cols_{num_cols} { - batch_ = CSRArrayAdapterBatch{indptr_, indices_, values_, - static_cast(num_cols_)}; + batch_ = + CSRArrayAdapterBatch{indptr_, indices_, values_, static_cast(num_cols_)}; } [[nodiscard]] CSRArrayAdapterBatch const& Value() const override { return batch_; } @@ -493,12 +479,8 @@ class FileAdapterBatch { public: class Line { public: - Line(size_t row_idx, const uint32_t *feature_idx, const float *value, - size_t size) - : row_idx_(row_idx), - feature_idx_(feature_idx), - value_(value), - size_(size) {} + Line(size_t row_idx, const uint32_t* feature_idx, const float* value, size_t size) + : row_idx_(row_idx), feature_idx_(feature_idx), value_(value), size_(size) {} size_t Size() { return size_; } COOTuple GetElement(size_t idx) { @@ -517,8 +499,7 @@ class FileAdapterBatch { Line GetLine(size_t idx) const { auto begin = block_->offset[idx]; auto end = block_->offset[idx + 1]; - return Line{idx + row_offset_, &block_->index[begin], &block_->value[begin], - end - begin}; + return Line{idx + row_offset_, &block_->index[begin], &block_->value[begin], end - begin}; } const float* Labels() const { return block_->label; } const float* Weights() const { return block_->weight; } @@ -574,15 +555,11 @@ class IteratorAdapter : public dmlc::DataIter { next_callback_(next_callback) {} // override functions - void BeforeFirst() override { - CHECK(at_first_) << "Cannot reset IteratorAdapter"; - } + void BeforeFirst() override { CHECK(at_first_) << "Cannot reset IteratorAdapter"; } [[nodiscard]] bool Next() override; - [[nodiscard]] FileAdapterBatch const& Value() const override { - return *batch_.get(); - } + [[nodiscard]] FileAdapterBatch const& Value() const override { return *batch_.get(); } // callback to set the data void SetData(const XGBoostBatchCSR& batch) { @@ -600,22 +577,19 @@ class IteratorAdapter : public dmlc::DataIter { weight_.insert(weight_.end(), batch.weight, batch.weight + batch.size); } if (batch.index != nullptr) { - index_.insert(index_.end(), batch.index + offset_[0], - batch.index + offset_.back()); + index_.insert(index_.end(), batch.index + offset_[0], batch.index + offset_.back()); } if (batch.value != nullptr) { - value_.insert(value_.end(), batch.value + offset_[0], - batch.value + offset_.back()); + value_.insert(value_.end(), batch.value + offset_[0], batch.value + offset_.back()); } if (offset_[0] != 0) { size_t base = offset_[0]; - for (size_t &item : offset_) { + for (size_t& item : offset_) { item -= base; } } CHECK(columns_ == data::kAdapterUnknownSize || columns_ == batch.columns) - << "Number of columns between batches changed from " << columns_ - << " to " << batch.columns; + << "Number of columns between batches changed from " << columns_ << " to " << batch.columns; columns_ = batch.columns; block_.size = batch.size; @@ -649,7 +623,7 @@ class IteratorAdapter : public dmlc::DataIter { // handle to the iterator, DataIterHandle data_handle_; // call back to get the data. - XGBCallbackDataIterNext *next_callback_; + XGBCallbackDataIterNext* next_callback_; // internal Rowblock dmlc::RowBlock block_; std::unique_ptr batch_; diff --git a/src/data/array_interface.cc b/src/data/array_interface.cc index 8240387256d9..cc4ec531f61b 100644 --- a/src/data/array_interface.cc +++ b/src/data/array_interface.cc @@ -3,6 +3,8 @@ */ #include "array_interface.h" +#include // for string + #if !defined(XGBOOST_USE_CUDA) #include "../common/common.h" // for AssertGPUSupport diff --git a/src/data/array_interface.h b/src/data/array_interface.h index ce393c0fffa4..136b0a3f95ba 100644 --- a/src/data/array_interface.h +++ b/src/data/array_interface.h @@ -253,8 +253,8 @@ class ArrayInterfaceHandler { * \brief Extracts the optiona `strides' field and returns whether the array is c-contiguous. */ template - static bool ExtractStride(Object::Map const &array, size_t itemsize, - size_t (&shape)[D], size_t (&stride)[D]) { + static bool ExtractStride(Object::Map const &array, size_t itemsize, size_t (&shape)[D], + size_t (&stride)[D]) { auto strides_it = array.find("strides"); // No stride is provided if (strides_it == array.cend() || IsA(strides_it->second)) { @@ -335,8 +335,7 @@ struct ToDType { static constexpr ArrayInterfaceHandler::Type kType = ArrayInterfaceHandler::kF8; }; template -struct ToDType && sizeof(long double) == 16>> { +struct ToDType && sizeof(long double) == 16>> { static constexpr ArrayInterfaceHandler::Type kType = ArrayInterfaceHandler::kF16; }; // uint diff --git a/src/data/cat_container.cu b/src/data/cat_container.cu index d957089b8ea1..a9eba3b13f88 100644 --- a/src/data/cat_container.cu +++ b/src/data/cat_container.cu @@ -4,6 +4,8 @@ #include // for copy #include // for make_unique +#include +#include #include // for vector #include "../common/cuda_context.cuh" // for CUDAContext diff --git a/src/data/columnar.h b/src/data/columnar.h index 28ffa372554a..7d9d96e17afd 100644 --- a/src/data/columnar.h +++ b/src/data/columnar.h @@ -54,7 +54,7 @@ auto GetArrowNames(Object::Map const& jnames, std::vector* p_c #if defined(__CUDACC__) #pragma nv_diagnostic push #pragma nv_diag_suppress 20208 // long double is treated as double in device code -#endif // defined(__CUDACC__) +#endif // defined(__CUDACC__) T back{0}; dh::safe_cuda(cudaMemcpy(&back, static_cast(offset.data) + offset_last_idx, sizeof(T), cudaMemcpyDeviceToHost)); diff --git a/src/data/data.cc b/src/data/data.cc index fa0545d09b08..9357eb745a44 100644 --- a/src/data/data.cc +++ b/src/data/data.cc @@ -12,10 +12,15 @@ #include // for uint64_t, int32_t, uint8_t, uint32_t #include // for size_t, strcmp, memcpy #include // for operator<<, basic_ostream, basic_ostream::op... +#include // for numeric_limits #include // for map, operator!= +#include // for shared_ptr, unique_ptr #include // for accumulate, partial_sum +#include // for string #include // for get, apply #include // for remove_pointer_t, remove_reference +#include // for move +#include // for vector #include "../collective/allgather.h" // for AllgatherStrings #include "../collective/allreduce.h" // for Allreduce @@ -69,8 +74,8 @@ DMLC_REGISTRY_ENABLE(::xgboost::data::SparsePageFormatReg<::xgboost::GHistIndexM namespace { template -void SaveScalarField(dmlc::Stream *strm, const std::string &name, - xgboost::DataType type, const T &field) { +void SaveScalarField(dmlc::Stream* strm, const std::string& name, xgboost::DataType type, + const T& field) { strm->Write(name); strm->Write(static_cast(type)); strm->Write(true); // is_scalar=True @@ -78,9 +83,8 @@ void SaveScalarField(dmlc::Stream *strm, const std::string &name, } template -void SaveVectorField(dmlc::Stream *strm, const std::string &name, - xgboost::DataType type, std::pair shape, - const std::vector& field) { +void SaveVectorField(dmlc::Stream* strm, const std::string& name, xgboost::DataType type, + std::pair shape, const std::vector& field) { strm->Write(name); strm->Write(static_cast(type)); strm->Write(false); // is_scalar=False @@ -90,8 +94,8 @@ void SaveVectorField(dmlc::Stream *strm, const std::string &name, } template -void SaveVectorField(dmlc::Stream* strm, const std::string& name, - xgboost::DataType type, std::pair shape, +void SaveVectorField(dmlc::Stream* strm, const std::string& name, xgboost::DataType type, + std::pair shape, const xgboost::HostDeviceVector& field) { SaveVectorField(strm, name, type, shape, field.ConstHostVector()); } @@ -116,17 +120,18 @@ void LoadScalarField(dmlc::Stream* strm, const std::string& expected_name, xgboost::DataType type; bool is_scalar; CHECK(strm->Read(&name)) << invalid; - CHECK_EQ(name, expected_name) - << invalid << " Expected field: " << expected_name << ", got: " << name; + CHECK_EQ(name, expected_name) << invalid << " Expected field: " << expected_name + << ", got: " << name; uint8_t type_val; CHECK(strm->Read(&type_val)) << invalid; type = static_cast(type_val); - CHECK(type == expected_type) - << invalid << "Expected field of type: " << static_cast(expected_type) << ", " - << "got field type: " << static_cast(type); + CHECK(type == expected_type) << invalid + << "Expected field of type: " << static_cast(expected_type) + << ", " + << "got field type: " << static_cast(type); CHECK(strm->Read(&is_scalar)) << invalid; - CHECK(is_scalar) - << invalid << "Expected field " << expected_name << " to be a scalar; got a vector"; + CHECK(is_scalar) << invalid << "Expected field " << expected_name + << " to be a scalar; got a vector"; CHECK(strm->Read(field)) << invalid; } @@ -138,17 +143,18 @@ void LoadVectorField(dmlc::Stream* strm, const std::string& expected_name, xgboost::DataType type; bool is_scalar; CHECK(strm->Read(&name)) << invalid; - CHECK_EQ(name, expected_name) - << invalid << " Expected field: " << expected_name << ", got: " << name; + CHECK_EQ(name, expected_name) << invalid << " Expected field: " << expected_name + << ", got: " << name; uint8_t type_val; CHECK(strm->Read(&type_val)) << invalid; type = static_cast(type_val); - CHECK(type == expected_type) - << invalid << "Expected field of type: " << static_cast(expected_type) << ", " - << "got field type: " << static_cast(type); + CHECK(type == expected_type) << invalid + << "Expected field of type: " << static_cast(expected_type) + << ", " + << "got field type: " << static_cast(type); CHECK(strm->Read(&is_scalar)) << invalid; - CHECK(!is_scalar) - << invalid << "Expected field " << expected_name << " to be a vector; got a scalar"; + CHECK(!is_scalar) << invalid << "Expected field " << expected_name + << " to be a vector; got a scalar"; std::pair shape; CHECK(strm->Read(&shape.first)); @@ -161,8 +167,7 @@ void LoadVectorField(dmlc::Stream* strm, const std::string& expected_name, template void LoadVectorField(dmlc::Stream* strm, const std::string& expected_name, - xgboost::DataType expected_type, - xgboost::HostDeviceVector* field) { + xgboost::DataType expected_type, xgboost::HostDeviceVector* field) { LoadVectorField(strm, expected_name, expected_type, &field->HostVector()); } @@ -214,45 +219,52 @@ void MetaInfo::Clear() { /* * Binary serialization format for MetaInfo: * - * | name | type | is_scalar | num_row | num_col | value | + * | name | type | is_scalar | num_row | num_col | value | * |--------------------+----------+-----------+-------------+-------------+------------------------| - * | num_row | kUInt64 | True | NA | NA | ${num_row_} | - * | num_col | kUInt64 | True | NA | NA | ${num_col_} | - * | num_nonzero | kUInt64 | True | NA | NA | ${num_nonzero_} | - * | labels | kFloat32 | False | ${size} | 1 | ${labels_} | - * | group_ptr | kUInt32 | False | ${size} | 1 | ${group_ptr_} | - * | weights | kFloat32 | False | ${size} | 1 | ${weights_} | - * | base_margin | kFloat32 | False | ${Shape(0)} | ${Shape(1)} | ${base_margin_} | - * | labels_lower_bound | kFloat32 | False | ${size} | 1 | ${labels_lower_bound_} | - * | labels_upper_bound | kFloat32 | False | ${size} | 1 | ${labels_upper_bound_} | - * | feature_names | kStr | False | ${size} | 1 | ${feature_names} | - * | feature_types | kStr | False | ${size} | 1 | ${feature_types} | - * | feature_weights | kFloat32 | False | ${size} | 1 | ${feature_weights} | - * | cats | kStr | False | ${size} | 1 | ${cats} | + * | num_row | kUInt64 | True | NA | NA | ${num_row_} | | num_col + * | kUInt64 | True | NA | NA | ${num_col_} | | num_nonzero | + * kUInt64 | True | NA | NA | ${num_nonzero_} | | labels | kFloat32 | + * False | ${size} | 1 | ${labels_} | | group_ptr | kUInt32 + * | False | ${size} | 1 | ${group_ptr_} | | weights | + * kFloat32 | False | ${size} | 1 | ${weights_} | | base_margin | + * kFloat32 | False | ${Shape(0)} | ${Shape(1)} | ${base_margin_} | | labels_lower_bound + * | kFloat32 | False | ${size} | 1 | ${labels_lower_bound_} | | + * labels_upper_bound | kFloat32 | False | ${size} | 1 | ${labels_upper_bound_} | + * | feature_names | kStr | False | ${size} | 1 | ${feature_names} | | + * feature_types | kStr | False | ${size} | 1 | ${feature_types} | + * | feature_weights | kFloat32 | False | ${size} | 1 | ${feature_weights} | | + * cats | kStr | False | ${size} | 1 | ${cats} | * * Note that the scalar fields (is_scalar=True) will have num_row and num_col missing. * Also notice the difference between the saved name and the name used in `SetInfo': * the former uses the plural form. */ -void MetaInfo::SaveBinary(dmlc::Stream *fo) const { +void MetaInfo::SaveBinary(dmlc::Stream* fo) const { Version::Save(fo); fo->Write(kNumField); int field_cnt = 0; // make sure we are actually writing kNumField fields - SaveScalarField(fo, u8"num_row", DataType::kUInt64, num_row_); ++field_cnt; - SaveScalarField(fo, u8"num_col", DataType::kUInt64, num_col_); ++field_cnt; - SaveScalarField(fo, u8"num_nonzero", DataType::kUInt64, num_nonzero_); ++field_cnt; - SaveTensorField(fo, u8"labels", DataType::kFloat32, labels); ++field_cnt; - SaveVectorField(fo, u8"group_ptr", DataType::kUInt32, - {group_ptr_.size(), 1}, group_ptr_); ++field_cnt; - SaveVectorField(fo, u8"weights", DataType::kFloat32, - {weights_.Size(), 1}, weights_); ++field_cnt; - SaveTensorField(fo, u8"base_margin", DataType::kFloat32, base_margin_); ++field_cnt; - SaveVectorField(fo, u8"labels_lower_bound", DataType::kFloat32, - {labels_lower_bound_.Size(), 1}, labels_lower_bound_); ++field_cnt; - SaveVectorField(fo, u8"labels_upper_bound", DataType::kFloat32, - {labels_upper_bound_.Size(), 1}, labels_upper_bound_); ++field_cnt; + SaveScalarField(fo, u8"num_row", DataType::kUInt64, num_row_); + ++field_cnt; + SaveScalarField(fo, u8"num_col", DataType::kUInt64, num_col_); + ++field_cnt; + SaveScalarField(fo, u8"num_nonzero", DataType::kUInt64, num_nonzero_); + ++field_cnt; + SaveTensorField(fo, u8"labels", DataType::kFloat32, labels); + ++field_cnt; + SaveVectorField(fo, u8"group_ptr", DataType::kUInt32, {group_ptr_.size(), 1}, group_ptr_); + ++field_cnt; + SaveVectorField(fo, u8"weights", DataType::kFloat32, {weights_.Size(), 1}, weights_); + ++field_cnt; + SaveTensorField(fo, u8"base_margin", DataType::kFloat32, base_margin_); + ++field_cnt; + SaveVectorField(fo, u8"labels_lower_bound", DataType::kFloat32, {labels_lower_bound_.Size(), 1}, + labels_lower_bound_); + ++field_cnt; + SaveVectorField(fo, u8"labels_upper_bound", DataType::kFloat32, {labels_upper_bound_.Size(), 1}, + labels_upper_bound_); + ++field_cnt; SaveVectorField(fo, u8"feature_names", DataType::kStr, {feature_names.size(), 1}, feature_names); ++field_cnt; @@ -312,7 +324,7 @@ const std::vector& MetaInfo::LabelAbsSort(Context const* ctx) const { return label_order_cache_; } -void MetaInfo::LoadBinary(dmlc::Stream *fi) { +void MetaInfo::LoadBinary(dmlc::Stream* fi) { auto version = Version::Load(fi); auto major = std::get<0>(version); auto minor = std::get<1>(version); @@ -327,7 +339,7 @@ void MetaInfo::LoadBinary(dmlc::Stream *fi) { CHECK_GE(minor, 1) << msg.str(); const uint64_t expected_num_field = kNumField; - uint64_t num_field { 0 }; + uint64_t num_field{0}; CHECK(fi->Read(&num_field)) << "MetaInfo: invalid format"; size_t expected = 0; if (major == 1 && std::get<1>(version) < 2) { @@ -336,10 +348,9 @@ void MetaInfo::LoadBinary(dmlc::Stream *fi) { } else { expected = expected_num_field; } - CHECK_GE(num_field, expected) - << "MetaInfo: insufficient number of fields (expected at least " - << expected << " fields, but the binary file only contains " << num_field - << "fields.)"; + CHECK_GE(num_field, expected) << "MetaInfo: insufficient number of fields (expected at least " + << expected << " fields, but the binary file only contains " + << num_field << "fields.)"; if (num_field > expected_num_field) { LOG(WARNING) << "MetaInfo: the given binary file contains extra fields " "which will be ignored."; @@ -636,10 +647,10 @@ void MetaInfo::GetInfo(char const* key, bst_ulong* out_len, DataType dtype, } else { LOG(FATAL) << "Unknown float field name: " << key; } - *out_len = static_cast(vec->size()); // NOLINT + *out_len = static_cast(vec->size()); // NOLINT *reinterpret_cast(out_dptr) = dmlc::BeginPtr(*vec); } else if (dtype == DataType::kUInt32) { - const std::vector *vec = nullptr; + const std::vector* vec = nullptr; if (!std::strcmp(key, "group_ptr")) { vec = &this->group_ptr_; } else { @@ -652,7 +663,7 @@ void MetaInfo::GetInfo(char const* key, bst_ulong* out_len, DataType dtype, } } -void MetaInfo::SetFeatureInfo(const char* key, const char **info, const bst_ulong size) { +void MetaInfo::SetFeatureInfo(const char* key, const char** info, const bst_ulong size) { bool is_col_split = this->IsColumnSplit(); if (size != 0 && this->num_col_ != 0 && !is_col_split) { @@ -751,8 +762,7 @@ void MetaInfo::Extend(MetaInfo const& that, bool accumulate_rows, bool check_col for (size_t i = 1; i < group_ptr.size(); ++i) { group_ptr[i] += this->group_ptr_.back(); } - this->group_ptr_.insert(this->group_ptr_.end(), group_ptr.begin() + 1, - group_ptr.end()); + this->group_ptr_.insert(this->group_ptr_.end(), group_ptr.begin() + 1, group_ptr.end()); } /** @@ -820,8 +830,7 @@ void MetaInfo::Validate(DeviceOrd device) const { } if (weights_.Size() != 0) { - CHECK_EQ(weights_.Size(), num_row_) - << "Size of weights must equal to number of rows."; + CHECK_EQ(weights_.Size(), num_row_) << "Size of weights must equal to number of rows."; CheckDevice(device, weights_); return; } @@ -859,9 +868,7 @@ void MetaInfo::Validate(DeviceOrd device) const { void MetaInfo::SetInfoFromCUDA(Context const*, StringView, Json) { common::AssertGPUSupport(); } #endif // !defined(XGBOOST_USE_CUDA) -bool MetaInfo::IsVerticalFederated() const { - return collective::IsFederated() && IsColumnSplit(); -} +bool MetaInfo::IsVerticalFederated() const { return collective::IsFederated() && IsColumnSplit(); } bool MetaInfo::ShouldHaveLabels() const { return !IsVerticalFederated() || collective::GetRank() == 0; @@ -880,8 +887,7 @@ void MetaInfo::Cats(std::shared_ptr cats) { static_castNumCatsTotal())>(std::numeric_limits::max())); } -using DMatrixThreadLocal = - dmlc::ThreadLocalStore>; +using DMatrixThreadLocal = dmlc::ThreadLocalStore>; XGBAPIThreadLocalEntry& DMatrix::GetThreadLocal() const { return (*DMatrixThreadLocal::Get())[this]; @@ -933,9 +939,8 @@ DMatrix* DMatrix::Load(const std::string& uri, bool silent, DataSplitMode data_s int partid = 0, npart = 1; static std::once_flag warning_flag; - std::call_once(warning_flag, []() { - LOG(WARNING) << "Text file input has been deprecated since 3.1"; - }); + std::call_once(warning_flag, + []() { LOG(WARNING) << "Text file input has been deprecated since 3.1"; }); fname = data::ValidateFileFormat(fname); std::unique_ptr> parser( @@ -1032,10 +1037,8 @@ SparsePage SparsePage::GetTranspose(int num_columns, int32_t n_threads) const { int tid = omp_get_thread_num(); auto inst = page[i]; for (const auto& entry : inst) { - builder.Push( - entry.index, - Entry(static_cast(this->base_rowid + i), entry.fvalue), - tid); + builder.Push(entry.index, Entry(static_cast(this->base_rowid + i), entry.fvalue), + tid); } }); @@ -1077,9 +1080,7 @@ void SparsePage::SortIndices(int32_t n_threads) { void SparsePage::Reindex(uint64_t feature_offset, int32_t n_threads) { auto& h_data = this->data.HostVector(); - common::ParallelFor(h_data.size(), n_threads, [&](auto i) { - h_data[i].index += feature_offset; - }); + common::ParallelFor(h_data.size(), n_threads, [&](auto i) { h_data[i].index += feature_offset; }); } void SparsePage::SortRows(int32_t n_threads) { @@ -1092,7 +1093,7 @@ void SparsePage::SortRows(int32_t n_threads) { }); } -void SparsePage::Push(const SparsePage &batch) { +void SparsePage::Push(const SparsePage& batch) { auto& data_vec = data.HostVector(); auto& offset_vec = offset.HostVector(); const auto& batch_offset_vec = batch.offset.HostVector(); @@ -1122,8 +1123,8 @@ bst_idx_t SparsePage::Push(AdapterBatchT const& batch, float missing, std::int32 auto& data_vec = data.HostVector(); size_t builder_base_row_offset = this->Size(); - common::ParallelGroupBuilder< - Entry, std::remove_reference::type::value_type, kIsRowMajor> + common::ParallelGroupBuilder::type::value_type, + kIsRowMajor> builder(&offset_vec, &data_vec, builder_base_row_offset); // Estimate expected number of rows by using last element in batch // This is not required to be exact but prevents unnecessary resizing @@ -1131,8 +1132,7 @@ bst_idx_t SparsePage::Push(AdapterBatchT const& batch, float missing, std::int32 if (batch.Size() > 0) { auto last_line = batch.GetLine(batch.Size() - 1); if (last_line.Size() > 0) { - expected_rows = - last_line.GetElement(last_line.Size() - 1).row_idx - base_rowid; + expected_rows = last_line.GetElement(last_line.Size() - 1).row_idx - base_rowid; } } size_t batch_size = batch.Size(); @@ -1152,8 +1152,8 @@ bst_idx_t SparsePage::Push(AdapterBatchT const& batch, float missing, std::int32 { exec.Run([&]() { int tid = omp_get_thread_num(); - size_t begin = tid*thread_size; - size_t end = tid != (nthread-1) ? (tid+1)*thread_size : batch_size; + size_t begin = tid * thread_size; + size_t end = tid != (nthread - 1) ? (tid + 1) * thread_size : batch_size; uint64_t& max_columns_local = max_columns_vector[tid][0]; for (size_t i = begin; i < end; ++i) { @@ -1164,7 +1164,7 @@ bst_idx_t SparsePage::Push(AdapterBatchT const& batch, float missing, std::int32 valid = false; } const size_t key = element.row_idx - base_rowid; - CHECK_GE(key, builder_base_row_offset); + CHECK_GE(key, builder_base_row_offset); max_columns_local = std::max(max_columns_local, static_cast(element.column_idx + 1)); @@ -1179,7 +1179,7 @@ bst_idx_t SparsePage::Push(AdapterBatchT const& batch, float missing, std::int32 } exec.Rethrow(); CHECK(valid) << error::InfInData(); - for (const auto & max : max_columns_vector) { + for (const auto& max : max_columns_vector) { max_columns = std::max(max_columns, max[0]); } @@ -1209,7 +1209,7 @@ bst_idx_t SparsePage::Push(AdapterBatchT const& batch, float missing, std::int32 return max_columns; } -void SparsePage::PushCSC(const SparsePage &batch) { +void SparsePage::PushCSC(const SparsePage& batch) { std::vector& self_data = data.HostVector(); std::vector& self_offset = offset.HostVector(); @@ -1241,22 +1241,20 @@ void SparsePage::PushCSC(const SparsePage &batch) { size_t ptr = 1; for (size_t i = 0; i < n_features; ++i) { size_t const self_beg = self_offset.at(i); - size_t const self_length = self_offset.at(i+1) - self_beg; + size_t const self_length = self_offset.at(i + 1) - self_beg; // It is possible that the current feature and further features aren't referenced // in any rows accumulated thus far. It is also possible for this to happen // in the current sparse page row batch as well. // Hence, the incremental number of rows may stay constant thus equaling the data size CHECK_LE(beg, data.size()); - std::memcpy(dmlc::BeginPtr(data)+beg, - dmlc::BeginPtr(self_data) + self_beg, + std::memcpy(dmlc::BeginPtr(data) + beg, dmlc::BeginPtr(self_data) + self_beg, sizeof(Entry) * self_length); beg += self_length; size_t const other_beg = other_offset.at(i); - size_t const other_length = other_offset.at(i+1) - other_beg; + size_t const other_length = other_offset.at(i + 1) - other_beg; CHECK_LE(beg, data.size()); - std::memcpy(dmlc::BeginPtr(data)+beg, - dmlc::BeginPtr(other_data) + other_beg, + std::memcpy(dmlc::BeginPtr(data) + beg, dmlc::BeginPtr(other_data) + other_beg, sizeof(Entry) * other_length); beg += other_length; diff --git a/src/data/data.cu b/src/data/data.cu index 6f605777e02b..15c763f5f5b5 100644 --- a/src/data/data.cu +++ b/src/data/data.cu @@ -7,6 +7,11 @@ #include // for gather #include // for none_of +#include // for copy +#include // for string +#include // for move +#include // for vector + #include "../common/algorithm.cuh" // for RunLengthEncode #include "../common/cuda_context.cuh" #include "../common/device_helpers.cuh" @@ -97,8 +102,7 @@ void CopyQidImpl(Context const* ctx, ArrayInterface<1> array_interface, } }); bool non_dec = true; - dh::safe_cuda(cudaMemcpy(&non_dec, flag.data().get(), sizeof(bool), - cudaMemcpyDeviceToHost)); + dh::safe_cuda(cudaMemcpy(&non_dec, flag.data().get(), sizeof(bool), cudaMemcpyDeviceToHost)); CHECK(non_dec) << "`qid` must be sorted in increasing order along with data."; dh::caching_device_vector out(array_interface.Shape<0>()); @@ -230,10 +234,10 @@ DMatrix* DMatrix::Create(AdapterT* adapter, float missing, int nthread, return new data::SimpleDMatrix(adapter, missing, nthread, data_split_mode); } -template DMatrix* DMatrix::Create( - data::CudfAdapter* adapter, float missing, int nthread, - const std::string& cache_prefix, DataSplitMode data_split_mode); -template DMatrix* DMatrix::Create( - data::CupyAdapter* adapter, float missing, int nthread, - const std::string& cache_prefix, DataSplitMode data_split_mode); +template DMatrix* DMatrix::Create(data::CudfAdapter* adapter, float missing, + int nthread, const std::string& cache_prefix, + DataSplitMode data_split_mode); +template DMatrix* DMatrix::Create(data::CupyAdapter* adapter, float missing, + int nthread, const std::string& cache_prefix, + DataSplitMode data_split_mode); } // namespace xgboost diff --git a/src/data/device_adapter.cu b/src/data/device_adapter.cu index 38a52ebfbb25..6643d112b9a5 100644 --- a/src/data/device_adapter.cu +++ b/src/data/device_adapter.cu @@ -1,6 +1,10 @@ /** * Copyright 2019-2025, XGBoost Contributors */ +#include // for max +#include // for move +#include // for vector + #include "../common/cuda_rt_utils.h" // for SetDevice, CurrentDevice #include "columnar.h" // for GetRefCats, GetArrowDictionary #include "device_adapter.cuh" diff --git a/src/data/ellpack_page.cu b/src/data/ellpack_page.cu index f4546add43d2..d5e6e0847642 100644 --- a/src/data/ellpack_page.cu +++ b/src/data/ellpack_page.cu @@ -9,6 +9,7 @@ #include // for copy #include // for distance #include // for numeric_limits +#include // for make_shared #include // for move #include // for vector @@ -360,7 +361,7 @@ void CopyDataToEllpack(Context const* ctx, const AdapterBatchT& batch, void WriteNullValues(Context const* ctx, EllpackPageImpl* dst, common::Span row_counts) { // Write the null values - auto null = dst->NullValue();; + auto null = dst->NullValue(); common::CompressedBufferWriter writer(dst->NumSymbols()); auto d_compressed_buffer = dst->gidx_buffer.data(); auto row_stride = dst->info.row_stride; diff --git a/src/data/ellpack_page_source.cu b/src/data/ellpack_page_source.cu index 5cddd94996da..b1cfd2d1cc25 100644 --- a/src/data/ellpack_page_source.cu +++ b/src/data/ellpack_page_source.cu @@ -7,6 +7,7 @@ #include // for shared_ptr, make_unique, make_shared #include // for accumulate #include // for move +#include // for vector #include "../common/common.h" // for HumanMemUnit, safe_cuda #include "../common/cuda_dr_utils.h" // for CUDA_HW_DECOM_AVAILABLE diff --git a/src/data/extmem_quantile_dmatrix.h b/src/data/extmem_quantile_dmatrix.h index 2907b18b2251..976b4e23d23f 100644 --- a/src/data/extmem_quantile_dmatrix.h +++ b/src/data/extmem_quantile_dmatrix.h @@ -44,8 +44,7 @@ class ExtMemQuantileDMatrix : public QuantileDMatrix { Context const *ctx, std::shared_ptr> iter, DMatrixHandle proxy_handle, BatchParam const &p, std::shared_ptr ref, - std::int64_t max_quantile_blocks, - ExtMemConfig const &config); + std::int64_t max_quantile_blocks, ExtMemConfig const &config); [[nodiscard]] BatchSet GetGradientIndexImpl(); BatchSet GetGradientIndex(Context const *ctx, BatchParam const ¶m) override; diff --git a/src/data/file_iterator.cc b/src/data/file_iterator.cc index 1e341447c35a..ce6134376fe9 100644 --- a/src/data/file_iterator.cc +++ b/src/data/file_iterator.cc @@ -8,6 +8,8 @@ #include // for weakly_canonical, path, u8path #include // for map, operator== #include // for operator<<, basic_ostream, istringstream +#include // for string +#include // for pair #include // for vector #include "../common/common.h" // for Split @@ -31,10 +33,10 @@ std::string ValidateFileFormat(std::string const& uri) { for (size_t i = 0; i < arg_list.size(); ++i) { std::istringstream is(arg_list[i]); std::pair kv; - CHECK(std::getline(is, kv.first, '=')) - << "Invalid uri argument format" << " for key in arg " << i + 1; - CHECK(std::getline(is, kv.second)) - << "Invalid uri argument format" << " for value in arg " << i + 1; + CHECK(std::getline(is, kv.first, '=')) << "Invalid uri argument format" + << " for key in arg " << i + 1; + CHECK(std::getline(is, kv.second)) << "Invalid uri argument format" + << " for value in arg " << i + 1; args.insert(kv); } if (args.find("format") == args.cend()) { diff --git a/src/data/file_iterator.h b/src/data/file_iterator.h index a4afbabe4077..029cbd7dd198 100644 --- a/src/data/file_iterator.h +++ b/src/data/file_iterator.h @@ -4,13 +4,13 @@ #ifndef XGBOOST_DATA_FILE_ITERATOR_H_ #define XGBOOST_DATA_FILE_ITERATOR_H_ -#include // for uint32_t -#include // for unique_ptr -#include // for string -#include // for move +#include // for uint32_t +#include // for unique_ptr +#include // for string +#include // for move -#include "dmlc/data.h" // for RowBlock, Parser -#include "xgboost/c_api.h" // for XGDMatrixFree, XGProxyDMatrixCreate +#include "dmlc/data.h" // for RowBlock, Parser +#include "xgboost/c_api.h" // for XGDMatrixFree, XGProxyDMatrixCreate namespace xgboost::data { [[nodiscard]] std::string ValidateFileFormat(std::string const& uri); @@ -45,9 +45,7 @@ class FileIterator { : uri_{ValidateFileFormat(std::move(uri))}, part_idx_{part_index}, n_parts_{num_parts} { XGProxyDMatrixCreate(&proxy_); } - ~FileIterator() { - XGDMatrixFree(proxy_); - } + ~FileIterator() { XGDMatrixFree(proxy_); } int Next(); @@ -59,13 +57,9 @@ class FileIterator { }; namespace fileiter { -inline void Reset(DataIterHandle self) { - static_cast(self)->Reset(); -} +inline void Reset(DataIterHandle self) { static_cast(self)->Reset(); } -inline int Next(DataIterHandle self) { - return static_cast(self)->Next(); -} +inline int Next(DataIterHandle self) { return static_cast(self)->Next(); } } // namespace fileiter } // namespace xgboost::data #endif // XGBOOST_DATA_FILE_ITERATOR_H_ diff --git a/src/data/gradient_index.cc b/src/data/gradient_index.cc index 88dc7304b2cb..35cd7189cc97 100644 --- a/src/data/gradient_index.cc +++ b/src/data/gradient_index.cc @@ -7,6 +7,7 @@ #include #include #include // for forward +#include // for vector #include "../common/column_matrix.h" #include "../common/hist_util.h" @@ -89,7 +90,9 @@ void GHistIndexMatrix::PushBatch(SparsePage const &batch, common::Span(0), row_ptr.begin()); data::SparsePageAdapterBatch adapter_batch{page}; - auto is_valid = [](auto) { return true; }; // SparsePage always contains valid entries + auto is_valid = [](auto) { + return true; + }; // SparsePage always contains valid entries PushBatchImpl(n_threads, adapter_batch, 0, is_valid, ft); } @@ -165,8 +168,8 @@ void GHistIndexMatrix::ResizeIndex(const size_t n_index, const bool isDense) { new_vec = {new_ptr, n_bytes / sizeof(std::uint8_t), malloc_resource}; } this->data = std::move(new_vec); - this->index = common::Index{common::Span{data.data(), static_cast(data.size())}, - t_size}; + this->index = + common::Index{common::Span{data.data(), static_cast(data.size())}, t_size}; }; if ((MaxNumBinPerFeat() - 1 <= static_cast(std::numeric_limits::max())) && @@ -195,7 +198,7 @@ bst_bin_t GHistIndexMatrix::GetGindex(size_t ridx, size_t fidx) const { return static_cast(this->index[begin + fidx]); } auto end = RowIdx(ridx + 1); - auto const& cut_ptrs = cut.Ptrs(); + auto const &cut_ptrs = cut.Ptrs(); auto f_begin = cut_ptrs[fidx]; auto f_end = cut_ptrs[fidx + 1]; return BinarySearchBin(begin, end, this->index, f_begin, f_end); diff --git a/src/data/gradient_index.h b/src/data/gradient_index.h index f1e1350ae648..e225cc2f51f7 100644 --- a/src/data/gradient_index.h +++ b/src/data/gradient_index.h @@ -174,8 +174,7 @@ class GHistIndexMatrix { * @brief Constructor for Quantile DMatrix. Initialize basic information and prepare * for push batch. */ - GHistIndexMatrix(MetaInfo const& info, common::HistogramCuts&& cuts, - bst_bin_t max_bin_per_feat); + GHistIndexMatrix(MetaInfo const& info, common::HistogramCuts&& cuts, bst_bin_t max_bin_per_feat); /** * @brief Constructor for the external memory Quantile DMatrix. Initialize basic diff --git a/src/data/iterative_dmatrix.cc b/src/data/iterative_dmatrix.cc index 6cf6e24e8286..470e17b0c770 100644 --- a/src/data/iterative_dmatrix.cc +++ b/src/data/iterative_dmatrix.cc @@ -29,8 +29,7 @@ IterativeDMatrix::IterativeDMatrix(DataIterHandle iter_handle, DMatrixHandle pro bst_bin_t max_bin, std::int64_t max_quantile_blocks) : proxy_{proxy} { // The external iterator, fetch the first batch - auto iter = - DataIterProxy{iter_handle, reset, next}; + auto iter = DataIterProxy{iter_handle, reset, next}; iter.Reset(); bool valid = iter.Next(); CHECK(valid) << "Iterative DMatrix must have at least 1 batch."; diff --git a/src/data/iterative_dmatrix.h b/src/data/iterative_dmatrix.h index 0a7e80ab59b9..f1405fe9f5ad 100644 --- a/src/data/iterative_dmatrix.h +++ b/src/data/iterative_dmatrix.h @@ -9,11 +9,11 @@ #include // for shared_ptr #include // for move -#include "quantile_dmatrix.h" // for QuantileDMatrix -#include "xgboost/base.h" // for bst_bin_t -#include "xgboost/c_api.h" // for DataIterHandle, DMatrixHandle -#include "xgboost/context.h" // for Context -#include "xgboost/data.h" // for BatchSet +#include "quantile_dmatrix.h" // for QuantileDMatrix +#include "xgboost/base.h" // for bst_bin_t +#include "xgboost/c_api.h" // for DataIterHandle, DMatrixHandle +#include "xgboost/context.h" // for Context +#include "xgboost/data.h" // for BatchSet namespace xgboost { namespace common { diff --git a/src/data/proxy_dmatrix.cu b/src/data/proxy_dmatrix.cu index 34d9221f2ece..39fe2db621a1 100644 --- a/src/data/proxy_dmatrix.cu +++ b/src/data/proxy_dmatrix.cu @@ -1,10 +1,12 @@ /** * Copyright 2020-2025, XGBoost contributors */ +#include // for shared_ptr + +#include "../common/type.h" // for GetValueT #include "../encoder/ordinal.h" // for DeviceColumnsView #include "device_adapter.cuh" #include "proxy_dmatrix.cuh" -#include "../common/type.h" // for GetValueT #include "proxy_dmatrix.h" namespace xgboost::data { diff --git a/src/data/proxy_dmatrix.h b/src/data/proxy_dmatrix.h index b2ea9a2d5afa..a69caa9576da 100644 --- a/src/data/proxy_dmatrix.h +++ b/src/data/proxy_dmatrix.h @@ -301,8 +301,8 @@ namespace cuda_impl { */ [[nodiscard]] inline bst_idx_t BatchSamples(DMatrixProxy const* proxy) { bool type_error = false; - auto n_samples = - cpu_impl::DispatchAny(proxy, [](auto const& value) { return value.NumRows(); }, &type_error); + auto n_samples = cpu_impl::DispatchAny( + proxy, [](auto const& value) { return value.NumRows(); }, &type_error); if (type_error) { n_samples = cuda_impl::BatchSamples(proxy); } @@ -314,8 +314,8 @@ namespace cuda_impl { */ [[nodiscard]] inline bst_feature_t BatchColumns(DMatrixProxy const* proxy) { bool type_error = false; - auto n_features = - cpu_impl::DispatchAny(proxy, [](auto const& value) { return value.NumCols(); }, &type_error); + auto n_features = cpu_impl::DispatchAny( + proxy, [](auto const& value) { return value.NumCols(); }, &type_error); if (type_error) { n_features = cuda_impl::BatchColumns(proxy); } diff --git a/src/data/quantile_dmatrix.cc b/src/data/quantile_dmatrix.cc index 2c30784719cf..0c9810bb6e40 100644 --- a/src/data/quantile_dmatrix.cc +++ b/src/data/quantile_dmatrix.cc @@ -3,15 +3,17 @@ */ #include "quantile_dmatrix.h" +#include // for make_unique, shared_ptr #include // for accumulate +#include // for vector #include "../collective/allreduce.h" // for Allreduce #include "../collective/communicator-inl.h" // for IsDistributed #include "../common/error_msg.h" // for InconsistentCategories #include "../common/threading_utils.h" // for ParallelFor -#include "proxy_dmatrix.h" // for DispatchAny #include "cat_container.h" // for CatContainer #include "gradient_index.h" // for GHistIndexMatrix +#include "proxy_dmatrix.h" // for DispatchAny #include "xgboost/collective/result.h" // for SafeColl #include "xgboost/linalg.h" // for Tensor diff --git a/src/data/quantile_dmatrix.cu b/src/data/quantile_dmatrix.cu index 9a9963f7cc3f..0859e07f5702 100644 --- a/src/data/quantile_dmatrix.cu +++ b/src/data/quantile_dmatrix.cu @@ -3,6 +3,7 @@ */ #include // for max #include // for numeric_limits +#include // for make_shared #include // for partial_sum #include // for pair #include // for vector diff --git a/src/data/simple_dmatrix.cc b/src/data/simple_dmatrix.cc index a249c99ac515..930823c52620 100644 --- a/src/data/simple_dmatrix.cc +++ b/src/data/simple_dmatrix.cc @@ -8,7 +8,9 @@ #include #include +#include #include // for accumulate +#include #include #include diff --git a/src/data/sparse_page_dmatrix.cc b/src/data/sparse_page_dmatrix.cc index d6fac5096d6c..2e8b5932c5f6 100644 --- a/src/data/sparse_page_dmatrix.cc +++ b/src/data/sparse_page_dmatrix.cc @@ -42,8 +42,7 @@ SparsePageDMatrix::SparsePageDMatrix(DataIterHandle iter_handle, DMatrixHandle p cache_prefix_ = MakeCachePrefix(cache_prefix_); DMatrixProxy *proxy = MakeProxy(proxy_); - auto iter = DataIterProxy{ - iter_, reset_, next_}; + auto iter = DataIterProxy{iter_, reset_, next_}; auto get_cats = [](DMatrixProxy const *proxy) { if (proxy->Ctx()->IsCPU()) { diff --git a/src/data/sparse_page_source.h b/src/data/sparse_page_source.h index 931da303316f..7e5918d3d6a9 100644 --- a/src/data/sparse_page_source.h +++ b/src/data/sparse_page_source.h @@ -58,9 +58,7 @@ struct Cache { return name + format; } - [[nodiscard]] std::string ShardName() const { - return ShardName(this->name, this->format); - } + [[nodiscard]] std::string ShardName() const { return ShardName(this->name, this->format); } [[nodiscard]] bool OnHost() const { return on_host; } /** * @brief Record a page with size of n_bytes. @@ -132,9 +130,7 @@ class TryLockGuard { explicit TryLockGuard(std::mutex& lock) : lock_{lock} { // NOLINT CHECK(lock_.try_lock()) << "Multiple threads attempting to use Sparse DMatrix."; } - ~TryLockGuard() { - lock_.unlock(); - } + ~TryLockGuard() { lock_.unlock(); } }; // Similar to `dmlc::OMPException`, but doesn't need the threads to be joined before rethrow @@ -262,7 +258,7 @@ class SparsePageSourceImpl : public BatchIteratorImpl, public FormatStreamPol // Workers for fetching data from external memory. common::ThreadPool workers_; - bool at_end_ {false}; + bool at_end_{false}; float missing_; std::int32_t nthreads_; bst_feature_t n_features_; @@ -382,7 +378,7 @@ class SparsePageSourceImpl : public BatchIteratorImpl, public FormatStreamPol monitor_.Init(typeid(S).name()); // not pretty, but works for basic profiling } - SparsePageSourceImpl(SparsePageSourceImpl const &that) = delete; + SparsePageSourceImpl(SparsePageSourceImpl const& that) = delete; ~SparsePageSourceImpl() override { // Don't orphan the threads. @@ -400,13 +396,9 @@ class SparsePageSourceImpl : public BatchIteratorImpl, public FormatStreamPol return *page_; } - [[nodiscard]] std::shared_ptr Page() const override { - return page_; - } + [[nodiscard]] std::shared_ptr Page() const override { return page_; } - [[nodiscard]] bool AtEnd() const override { - return at_end_; - } + [[nodiscard]] bool AtEnd() const override { return at_end_; } // Call this at the last iteration (it == n_batches). virtual void EndIter() { this->cache_info_->Commit(); @@ -588,7 +580,7 @@ class CSCPageSource : public PageSourceIncMixIn { protected: void Fetch() final { if (!this->ReadCache()) { - auto const &csr = source_->Page(); + auto const& csr = source_->Page(); this->page_.reset(new CSCPage{}); // we might be able to optimize this by merging transpose and pushcsc this->page_->PushCSC(csr->GetTranspose(n_features_, nthreads_)); @@ -610,7 +602,7 @@ class SortedCSCPageSource : public PageSourceIncMixIn { protected: void Fetch() final { if (!this->ReadCache()) { - auto const &csr = this->source_->Page(); + auto const& csr = this->source_->Page(); this->page_.reset(new SortedCSCPage{}); // we might be able to optimize this by merging transpose and pushcsc this->page_->PushCSC(csr->GetTranspose(n_features_, nthreads_)); @@ -623,9 +615,8 @@ class SortedCSCPageSource : public PageSourceIncMixIn { } public: - SortedCSCPageSource(float missing, int nthreads, bst_feature_t n_features, - uint32_t n_batches, std::shared_ptr cache, - std::shared_ptr source) + SortedCSCPageSource(float missing, int nthreads, bst_feature_t n_features, uint32_t n_batches, + std::shared_ptr cache, std::shared_ptr source) : PageSourceIncMixIn(missing, nthreads, n_features, n_batches, cache, true) { this->source_ = source; this->Fetch(); diff --git a/src/data/sparse_page_writer.h b/src/data/sparse_page_writer.h index 526126d296e1..e00bf3d6d8ac 100644 --- a/src/data/sparse_page_writer.h +++ b/src/data/sparse_page_writer.h @@ -13,7 +13,7 @@ #include "dmlc/registry.h" // for Registry, FunctionRegEntryBase namespace xgboost::data { -template +template struct SparsePageFormatReg; /** @@ -43,9 +43,9 @@ class SparsePageFormat { * \brief Create sparse page of format. * \return The created format functors. */ -template +template inline SparsePageFormat* CreatePageFormat(const std::string& name) { - auto *e = ::dmlc::Registry>::Get()->Find(name); + auto* e = ::dmlc::Registry>::Get()->Find(name); if (e == nullptr) { LOG(FATAL) << "Unknown format type " << name; return nullptr; @@ -56,10 +56,9 @@ inline SparsePageFormat* CreatePageFormat(const std::string& name) { /** * @brief Registry entry for sparse page format. */ -template +template struct SparsePageFormatReg : public dmlc::FunctionRegEntryBase, - std::function* ()>> { -}; + std::function*()>> {}; } // namespace xgboost::data #endif // XGBOOST_DATA_SPARSE_PAGE_WRITER_H_ diff --git a/src/encoder/ordinal.h b/src/encoder/ordinal.h index cb8cf8855a34..d99026824159 100644 --- a/src/encoder/ordinal.h +++ b/src/encoder/ordinal.h @@ -344,8 +344,8 @@ void Recode(ExecPolicy const &policy, HostColumnsView orig_enc, Span #include +#include #include #include #include -#include "../common/error_msg.h" // NoCategorical, DeprecatedFunc +#include "../common/error_msg.h" // NoCategorical, DeprecatedFunc #include "../common/threading_utils.h" #include "../common/timer.h" #include "gblinear_model.h" @@ -35,13 +36,10 @@ struct GBLinearTrainParam : public XGBoostParameter { size_t max_row_perbatch; DMLC_DECLARE_PARAMETER(GBLinearTrainParam) { - DMLC_DECLARE_FIELD(updater) - .set_default("shotgun") - .describe("Update algorithm for linear model. One of shotgun/coord_descent"); - DMLC_DECLARE_FIELD(tolerance) - .set_lower_bound(0.0f) - .set_default(0.0f) - .describe("Stop if largest weight update is smaller than this number."); + DMLC_DECLARE_FIELD(updater).set_default("shotgun").describe( + "Update algorithm for linear model. One of shotgun/coord_descent"); + DMLC_DECLARE_FIELD(tolerance).set_lower_bound(0.0f).set_default(0.0f).describe( + "Stop if largest weight update is smaller than this number."); DMLC_DECLARE_FIELD(max_row_perbatch) .set_default(std::numeric_limits::max()) .describe("Maximum rows per batch."); @@ -86,9 +84,7 @@ class GBLinear : public GradientBooster { updater_->Configure(cfg); } - int32_t BoostedRounds() const override { - return model_.num_boosted_rounds; - } + int32_t BoostedRounds() const override { return model_.num_boosted_rounds; } bool ModelFitted() const override { return BoostedRounds() != 0; } @@ -152,7 +148,7 @@ class GBLinear : public GradientBooster { monitor_.Stop("PredictBatch"); } - void PredictLeaf(DMatrix *, HostDeviceVector *, unsigned, unsigned) override { + void PredictLeaf(DMatrix*, HostDeviceVector*, unsigned, unsigned) override { LOG(FATAL) << "gblinear does not support prediction of leaf index"; } @@ -170,7 +166,7 @@ class GBLinear : public GradientBooster { std::fill(contribs.begin(), contribs.end(), 0); auto base_score = learner_model_param_->BaseScore(ctx_); // start collecting the contributions - for (const auto &batch : p_fmat->GetBatches()) { + for (const auto& batch : p_fmat->GetBatches()) { // parallel over local batch const auto nsize = static_cast(batch.Size()); auto page = batch.GetView(); @@ -179,7 +175,7 @@ class GBLinear : public GradientBooster { auto row_idx = static_cast(batch.base_rowid + i); // loop over output groups for (int gid = 0; gid < ngroup; ++gid) { - bst_float *p_contribs = &contribs[(row_idx * ngroup + gid) * ncolumns]; + bst_float* p_contribs = &contribs[(row_idx * ngroup + gid) * ncolumns]; // calculate linear terms' contributions for (auto& ins : inst) { if (ins.index >= model_.learner_model_param->num_feature) continue; @@ -201,8 +197,8 @@ class GBLinear : public GradientBooster { std::vector& contribs = out_contribs->HostVector(); // linear models have no interaction effects - const size_t nelements = model_.learner_model_param->num_feature * - model_.learner_model_param->num_feature; + const size_t nelements = + model_.learner_model_param->num_feature * model_.learner_model_param->num_feature; contribs.resize(p_fmat->Info().num_row_ * nelements * model_.learner_model_param->num_output_group); std::fill(contribs.begin(), contribs.end(), 0); @@ -213,10 +209,9 @@ class GBLinear : public GradientBooster { return model_.DumpModel(fmap, with_stats, format); } - void FeatureScore(std::string const &importance_type, - common::Span trees, - std::vector *out_features, - std::vector *out_scores) const override { + void FeatureScore(std::string const& importance_type, common::Span trees, + std::vector* out_features, + std::vector* out_scores) const override { CHECK(!model_.weight.empty()) << "Model is not initialized"; CHECK(trees.empty()) << "gblinear doesn't support number of trees for feature importance."; CHECK_EQ(importance_type, "weight") @@ -238,18 +233,17 @@ class GBLinear : public GradientBooster { } protected: - void PredictBatchInternal(DMatrix *p_fmat, - std::vector *out_preds) { + void PredictBatchInternal(DMatrix* p_fmat, std::vector* out_preds) { monitor_.Start("PredictBatchInternal"); model_.LazyInitModel(); - std::vector &preds = *out_preds; + std::vector& preds = *out_preds; auto base_margin = p_fmat->Info().base_margin_.View(DeviceOrd::CPU()); // start collecting the prediction const int ngroup = model_.learner_model_param->num_output_group; preds.resize(p_fmat->Info().num_row_ * ngroup); auto base_score = learner_model_param_->BaseScore(DeviceOrd::CPU()); - for (const auto &page : p_fmat->GetBatches()) { + for (const auto& page : p_fmat->GetBatches()) { auto const& batch = page.GetView(); // output convention: nrow * k, where nrow is number of rows // k is number of group @@ -279,8 +273,7 @@ class GBLinear : public GradientBooster { } float largest_dw = 0.0; for (size_t i = 0; i < model_.weight.size(); i++) { - largest_dw = std::max( - largest_dw, std::abs(model_.weight[i] - previous_model_.weight[i])); + largest_dw = std::max(largest_dw, std::abs(model_.weight[i] - previous_model_.weight[i])); } previous_model_ = model_; @@ -288,9 +281,9 @@ class GBLinear : public GradientBooster { return is_converged_; } - void LazySumWeights(DMatrix *p_fmat) { + void LazySumWeights(DMatrix* p_fmat) { if (!sum_weight_complete_) { - auto &info = p_fmat->Info(); + auto& info = p_fmat->Info(); for (size_t i = 0; i < info.num_row_; i++) { sum_instance_weight_ += info.GetWeight(i); } @@ -298,8 +291,7 @@ class GBLinear : public GradientBooster { } } - void Pred(const SparsePage::Inst &inst, bst_float *preds, int gid, - bst_float base) { + void Pred(const SparsePage::Inst& inst, bst_float* preds, int gid, bst_float base) { bst_float psum = model_.Bias()[gid] + base; for (const auto& ins : inst) { if (ins.index >= model_.learner_model_param->num_feature) continue; diff --git a/src/gbm/gblinear_model.cc b/src/gbm/gblinear_model.cc index 72853f544fdf..2e344bf51fea 100644 --- a/src/gbm/gblinear_model.cc +++ b/src/gbm/gblinear_model.cc @@ -1,10 +1,12 @@ /** * Copyright 2019-2025, XGBoost Contributors */ +#include "gblinear_model.h" + #include #include + #include "xgboost/json.h" -#include "gblinear_model.h" namespace xgboost::gbm { void GBLinearModel::SaveModel(Json* p_out) const { diff --git a/src/gbm/gblinear_model.h b/src/gbm/gblinear_model.h index 08ae56959461..725e9e63cedc 100644 --- a/src/gbm/gblinear_model.h +++ b/src/gbm/gblinear_model.h @@ -6,14 +6,14 @@ #include #include -#include -#include #include +#include +#include #include "xgboost/base.h" #include "xgboost/feature_map.h" -#include "xgboost/model.h" #include "xgboost/json.h" +#include "xgboost/model.h" namespace xgboost { class Json; @@ -22,12 +22,12 @@ namespace gbm { class GBLinearModel : public Model { public: std::int32_t num_boosted_rounds{0}; - LearnerModelParam const* learner_model_param; + LearnerModelParam const *learner_model_param; public: explicit GBLinearModel(LearnerModelParam const *learner_model_param) : learner_model_param{learner_model_param} {} - void Configure(Args const &) { } + void Configure(Args const &) {} // weight for each of feature, bias is the last one std::vector weight; @@ -37,8 +37,7 @@ class GBLinearModel : public Model { return; } // bias is the last weight - weight.resize((learner_model_param->num_feature + 1) * - learner_model_param->num_output_group); + weight.resize((learner_model_param->num_feature + 1) * learner_model_param->num_output_group); std::fill(weight.begin(), weight.end(), 0.0f); } @@ -47,12 +46,10 @@ class GBLinearModel : public Model { // model bias inline bst_float *Bias() { - return &weight[learner_model_param->num_feature * - learner_model_param->num_output_group]; + return &weight[learner_model_param->num_feature * learner_model_param->num_output_group]; } inline const bst_float *Bias() const { - return &weight[learner_model_param->num_feature * - learner_model_param->num_output_group]; + return &weight[learner_model_param->num_feature * learner_model_param->num_output_group]; } // get i-th weight inline bst_float *operator[](size_t i) { @@ -62,8 +59,7 @@ class GBLinearModel : public Model { return &weight[i * learner_model_param->num_output_group]; } - std::vector DumpModel(const FeatureMap &, bool, - std::string format) const { + std::vector DumpModel(const FeatureMap &, bool, std::string format) const { const int ngroup = learner_model_param->num_output_group; const unsigned nfeature = learner_model_param->num_feature; @@ -76,9 +72,7 @@ class GBLinearModel : public Model { } fo << " " << this->Bias()[gid]; } - fo << std::endl - << " ]," << std::endl - << " \"weight\": [" << std::endl; + fo << std::endl << " ]," << std::endl << " \"weight\": [" << std::endl; for (unsigned i = 0; i < nfeature; ++i) { for (int gid = 0; gid < ngroup; ++gid) { if (i != 0 || gid != 0) { diff --git a/src/gbm/gbm.cc b/src/gbm/gbm.cc index ecee708ecb8e..5f904844c4b1 100644 --- a/src/gbm/gbm.cc +++ b/src/gbm/gbm.cc @@ -19,11 +19,11 @@ DMLC_REGISTRY_ENABLE(::xgboost::GradientBoosterReg); namespace xgboost { GradientBooster* GradientBooster::Create(const std::string& name, Context const* ctx, LearnerModelParam const* learner_model_param) { - auto *e = ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->Find(name); + auto* e = ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->Find(name); if (e == nullptr) { LOG(FATAL) << "Unknown gbm type " << name; } - auto p_bst = (e->body)(learner_model_param, ctx); + auto p_bst = (e->body)(learner_model_param, ctx); return p_bst; } } // namespace xgboost diff --git a/src/gbm/gbtree.cc b/src/gbm/gbtree.cc index 06f89242b1d0..594458dbbb93 100644 --- a/src/gbm/gbtree.cc +++ b/src/gbm/gbtree.cc @@ -112,8 +112,7 @@ void GBTree::Configure(Args const& cfg) { #if defined(XGBOOST_USE_SYCL) if (!sycl_predictor_) { - sycl_predictor_ = - std::unique_ptr(Predictor::Create("sycl_predictor", this->ctx_)); + sycl_predictor_ = std::unique_ptr(Predictor::Create("sycl_predictor", this->ctx_)); } sycl_predictor_->Configure(cfg); #endif // defined(XGBOOST_USE_SYCL) @@ -639,9 +638,9 @@ void GBTree::InplacePredict(std::shared_ptr p_m, float missing, return gpu_predictor_; } else { #if defined(XGBOOST_USE_SYCL) - common::AssertSYCLSupport(); - CHECK(sycl_predictor_); - return sycl_predictor_; + common::AssertSYCLSupport(); + CHECK(sycl_predictor_); + return sycl_predictor_; #endif // defined(XGBOOST_USE_SYCL) } @@ -676,7 +675,6 @@ void GPUDartInplacePredictInc(common::Span /*out_predts*/, common::Spanmodel_.learner_model_param->IsVectorLeaf()) << "dart" << MTNotImplemented(); auto& predictor = this->GetPredictor(training, &p_out_preds->predictions, p_fmat); CHECK(predictor); - predictor->InitOutPredictions(p_fmat->Info(), &p_out_preds->predictions, - model_); + predictor->InitOutPredictions(p_fmat->Info(), &p_out_preds->predictions, model_); p_out_preds->version = 0; auto [tree_begin, tree_end] = detail::LayerToTree(model_, layer_begin, layer_end); auto n_groups = model_.learner_model_param->num_output_group; @@ -784,8 +781,8 @@ class Dart : public GBTree { GPUDartPredictInc(p_out_preds->predictions.DeviceSpan(), predts.predictions.DeviceSpan(), w, n_rows, n_groups, grp_idx); } else { - auto &h_out_predts = p_out_preds->predictions.HostVector(); - auto &h_predts = predts.predictions.ConstHostVector(); + auto& h_out_predts = p_out_preds->predictions.HostVector(); + auto& h_predts = predts.predictions.ConstHostVector(); common::ParallelFor(p_fmat->Info().num_row_, ctx_->Threads(), [&](auto ridx) { const size_t offset = ridx * n_groups + grp_idx; h_out_predts[offset] += (h_predts[offset] * w); @@ -942,10 +939,8 @@ class Dart : public GBTree { // size_t i = std::discrete_distribution(weight_drop.begin(), // weight_drop.end())(rnd); size_t i = std::discrete_distribution( - weight_drop_.size(), 0., static_cast(weight_drop_.size()), - [this](double x) -> double { - return weight_drop_[static_cast(x)]; - })(rnd); + weight_drop_.size(), 0., static_cast(weight_drop_.size()), + [this](double x) -> double { return weight_drop_[static_cast(x)]; })(rnd); idx_drop_.push_back(i); } } else { diff --git a/src/gbm/gbtree.cu b/src/gbm/gbtree.cu index 4e80a55de82d..72526215e9d7 100644 --- a/src/gbm/gbtree.cu +++ b/src/gbm/gbtree.cu @@ -21,9 +21,8 @@ void GPUCopyGradient(Context const *ctx, linalg::Matrix const *in_ thrust::copy(cuctx->CTP(), it, it + v_in.Size(), d_out.Values().data()); } -void GPUDartPredictInc(common::Span out_predts, - common::Span predts, float tree_w, size_t n_rows, - bst_group_t n_groups, bst_group_t group) { +void GPUDartPredictInc(common::Span out_predts, common::Span predts, float tree_w, + size_t n_rows, bst_group_t n_groups, bst_group_t group) { dh::LaunchN(n_rows, [=] XGBOOST_DEVICE(size_t ridx) { const size_t offset = ridx * n_groups + group; out_predts[offset] += (predts[offset] * tree_w); diff --git a/src/gbm/gbtree.h b/src/gbm/gbtree.h index 739d196769f9..7d5c425ad6f2 100644 --- a/src/gbm/gbtree.h +++ b/src/gbm/gbtree.h @@ -40,10 +40,7 @@ enum class TreeMethod : int { }; // boosting process types -enum class TreeProcessType : int { - kDefault = 0, - kUpdate = 1 -}; +enum class TreeProcessType : int { kDefault = 0, kUpdate = 1 }; // Sampling type for dart weights. enum class DartSampleType : std::int32_t { @@ -72,15 +69,16 @@ struct GBTreeTrainParam : public XGBoostParameter { .set_default(TreeProcessType::kDefault) .add_enum("default", TreeProcessType::kDefault) .add_enum("update", TreeProcessType::kUpdate) - .describe("Whether to run the normal boosting process that creates new trees,"\ - " or to update the trees in an existing model."); + .describe( + "Whether to run the normal boosting process that creates new trees," + " or to update the trees in an existing model."); DMLC_DECLARE_ALIAS(updater_seq, updater); DMLC_DECLARE_FIELD(tree_method) .set_default(TreeMethod::kAuto) - .add_enum("auto", TreeMethod::kAuto) - .add_enum("approx", TreeMethod::kApprox) - .add_enum("exact", TreeMethod::kExact) - .add_enum("hist", TreeMethod::kHist) + .add_enum("auto", TreeMethod::kAuto) + .add_enum("approx", TreeMethod::kApprox) + .add_enum("exact", TreeMethod::kExact) + .add_enum("hist", TreeMethod::kHist) .describe("Choice of tree construction method."); } }; @@ -268,10 +266,9 @@ class GBTree : public GradientBooster { } }); } else { - LOG(FATAL) - << "Unknown feature importance type, expected one of: " - << R"({"weight", "total_gain", "total_cover", "gain", "cover"}, got: )" - << importance_type; + LOG(FATAL) << "Unknown feature importance type, expected one of: " + << R"({"weight", "total_gain", "total_cover", "gain", "cover"}, got: )" + << importance_type; } if (importance_type == "gain" || importance_type == "cover") { for (size_t i = 0; i < gain_map.size(); ++i) { @@ -291,9 +288,8 @@ class GBTree : public GradientBooster { [[nodiscard]] CatContainer const* Cats() const override { return this->model_.Cats(); } - void PredictLeaf(DMatrix* p_fmat, - HostDeviceVector* out_preds, - uint32_t layer_begin, uint32_t layer_end) override { + void PredictLeaf(DMatrix* p_fmat, HostDeviceVector* out_preds, uint32_t layer_begin, + uint32_t layer_end) override { auto [tree_begin, tree_end] = detail::LayerToTree(model_, layer_begin, layer_end); CHECK_EQ(tree_begin, 0) << "Predict leaf supports only iteration end: [0, " "n_iteration), use model slicing instead."; @@ -345,7 +341,7 @@ class GBTree : public GradientBooster { GBTreeTrainParam tparam_; // Tree training parameter tree::TrainParam tree_param_; - bool specified_updater_ {false}; + bool specified_updater_{false}; // the updaters that can be applied to each of tree std::vector> updaters_; // Predictors diff --git a/src/gbm/gbtree_model.cc b/src/gbm/gbtree_model.cc index ecb6a66e9c17..c91c99683f05 100644 --- a/src/gbm/gbtree_model.cc +++ b/src/gbm/gbtree_model.cc @@ -5,8 +5,10 @@ #include // for transform, max_element #include // for size_t +#include // for make_shared #include // for partial_sum #include // for move, pair +#include // for vector #include "../common/threading_utils.h" // for ParallelFor #include "xgboost/context.h" // for Context diff --git a/src/learner.cc b/src/learner.cc index 26ebc1e6f9c4..94ee79df3967 100644 --- a/src/learner.cc +++ b/src/learner.cc @@ -6,38 +6,39 @@ */ #include "xgboost/learner.h" -#include // for Stream -#include // for FieldEntry, DMLC_DECLARE_FIELD, Parameter, DMLC... -#include // for ThreadLocalStore - -#include // for equal, max, transform, sort, find_if, all_of -#include // for atomic -#include // for isalpha, isspace -#include // for isnan, isinf -#include // for int32_t, uint32_t, int64_t, uint64_t -#include // for atoi -#include // for memcpy, size_t, memset -#include // for operator<<, setiosflags -#include // for back_insert_iterator, distance, back_inserter -#include // for numeric_limits -#include // for allocator, unique_ptr, shared_ptr, operator== -#include // for mutex, lock_guard -#include // for operator<<, basic_ostream, basic_ostream::opera... -#include // for stack -#include // for basic_string, char_traits, operator<, string -#include // for errc -#include // for operator!=, unordered_map -#include // for pair, as_const, move, swap -#include // for vector +#include // for Stream +#include // for FieldEntry, DMLC_DECLARE_FIELD, Parameter, DMLC... +#include // for ThreadLocalStore + +#include // for equal, max, transform, sort, find_if, all_of +#include // for atomic +#include // for isalpha, isspace +#include // for isnan, isinf +#include // for int32_t, uint32_t, int64_t, uint64_t +#include // for atoi +#include // for memcpy, size_t, memset +#include // for operator<<, setiosflags +#include // for back_insert_iterator, distance, back_inserter +#include // for numeric_limits +#include +#include // for allocator, unique_ptr, shared_ptr, operator== +#include // for mutex, lock_guard +#include // for operator<<, basic_ostream, basic_ostream::opera... +#include // for stack +#include // for basic_string, char_traits, operator<, string +#include // for errc +#include // for operator!=, unordered_map +#include // for pair, as_const, move, swap +#include // for vector #include "collective/aggregator.h" // for ApplyWithLabels #include "collective/communicator-inl.h" // for Allreduce, Broadcast, GetRank, IsDistributed #include "common/api_entry.h" // for XGBAPIThreadLocalEntry -#include "common/param_array.h" // for ParamArray #include "common/charconv.h" // for to_chars, to_chars_result, NumericLimits, from_... #include "common/error_msg.h" // for MaxFeatureSize, WarnOldSerialization, ... #include "common/io.h" // for PeekableInStream, ReadAll, FixedSizeStream, Mem... #include "common/observer.h" // for TrainingObserver +#include "common/param_array.h" // for ParamArray #include "common/random.h" // for GlobalRandom #include "common/timer.h" // for Monitor #include "common/version.h" // for Version @@ -300,7 +301,7 @@ void LearnerModelParam::Copy(LearnerModelParam const& that) { struct LearnerTrainParam : public XGBoostParameter { // flag to disable default metric - bool disable_default_eval_metric {false}; + bool disable_default_eval_metric{false}; // FIXME(trivialfis): The following parameters belong to model itself, but can be // specified by users. Move them to model parameter once we can get rid of binary IO. std::string booster; @@ -328,12 +329,11 @@ struct LearnerTrainParam : public XGBoostParameter { } }; - DMLC_REGISTER_PARAMETER(LearnerModelParamLegacy); DMLC_REGISTER_PARAMETER(LearnerTrainParam); using LearnerAPIThreadLocalStore = - dmlc::ThreadLocalStore>; + dmlc::ThreadLocalStore>; namespace { /** @@ -614,7 +614,7 @@ class LearnerConfiguration : public Intercept { void SaveConfig(Json* p_out) const override { CHECK(!this->need_configuration_) << "Call Configure before saving model."; Version::Save(p_out); - Json& out { *p_out }; + Json& out{*p_out}; // parameters out["learner"] = Object(); auto& learner_parameters = out["learner"]; @@ -642,8 +642,7 @@ class LearnerConfiguration : public Intercept { void SetParam(const std::string& key, const std::string& value) override { this->need_configuration_ = true; if (key == kEvalMetric) { - if (std::find(metric_names_.cbegin(), metric_names_.cend(), - value) == metric_names_.cend()) { + if (std::find(metric_names_.cbegin(), metric_names_.cend(), value) == metric_names_.cend()) { metric_names_.emplace_back(value); } } else { @@ -657,9 +656,7 @@ class LearnerConfiguration : public Intercept { } } - uint32_t GetNumFeature() const override { - return learner_model_param_.num_feature; - } + uint32_t GetNumFeature() const override { return learner_model_param_.num_feature; } void SetAttr(const std::string& key, const std::string& value) override { attributes_[key] = value; @@ -674,22 +671,18 @@ class LearnerConfiguration : public Intercept { bool DelAttr(const std::string& key) override { auto it = attributes_.find(key); - if (it == attributes_.end()) { return false; } + if (it == attributes_.end()) { + return false; + } attributes_.erase(it); return true; } - void SetFeatureNames(std::vector const& fn) override { - feature_names_ = fn; - } + void SetFeatureNames(std::vector const& fn) override { feature_names_ = fn; } - void GetFeatureNames(std::vector* fn) const override { - *fn = feature_names_; - } + void GetFeatureNames(std::vector* fn) const override { *fn = feature_names_; } - void SetFeatureTypes(std::vector const& ft) override { - this->feature_types_ = ft; - } + void SetFeatureTypes(std::vector const& ft) override { this->feature_types_ = ft; } void GetFeatureTypes(std::vector* p_ft) const override { auto& ft = *p_ft; @@ -716,13 +709,13 @@ class LearnerConfiguration : public Intercept { private: void ValidateParameters() { - Json config { Object() }; + Json config{Object()}; this->SaveConfig(&config); std::stack stack; stack.push(config); std::string const postfix{"_param"}; - auto is_parameter = [&postfix](std::string const &key) { + auto is_parameter = [&postfix](std::string const& key) { return key.size() > postfix.size() && std::equal(postfix.rbegin(), postfix.rend(), key.rbegin()); }; @@ -738,7 +731,7 @@ class LearnerConfiguration : public Intercept { while (!stack.empty()) { auto j_obj = stack.top(); stack.pop(); - auto const &obj = get(j_obj); + auto const& obj = get(j_obj); for (auto const& kv : obj) { if (is_parameter(kv.first)) { @@ -766,7 +759,7 @@ class LearnerConfiguration : public Intercept { std::sort(keys.begin(), keys.end()); std::vector provided; - for (auto const &kv : cfg_) { + for (auto const& kv : cfg_) { if (std::any_of(kv.first.cbegin(), kv.first.cend(), [](char ch) { return std::isspace(ch); })) { LOG(FATAL) << "Invalid parameter \"" << kv.first << "\" contains whitespace."; @@ -776,8 +769,8 @@ class LearnerConfiguration : public Intercept { std::sort(provided.begin(), provided.end()); std::vector diff; - std::set_difference(provided.begin(), provided.end(), keys.begin(), - keys.end(), std::back_inserter(diff)); + std::set_difference(provided.begin(), provided.end(), keys.begin(), keys.end(), + std::back_inserter(diff)); if (diff.size() != 0) { std::stringstream ss; ss << "\nParameters: { "; @@ -817,8 +810,7 @@ class LearnerConfiguration : public Intercept { void ConfigureGBM(LearnerTrainParam const& old, Args const& args) { if (gbm_ == nullptr || old.booster != tparam_.booster) { - gbm_.reset(GradientBooster::Create(tparam_.booster, &ctx_, - &learner_model_param_)); + gbm_.reset(GradientBooster::Create(tparam_.booster, &ctx_, &learner_model_param_)); } gbm_->Configure(args); } @@ -833,8 +825,7 @@ class LearnerConfiguration : public Intercept { } } - if (cfg_.find("max_delta_step") == cfg_.cend() && - cfg_.find("objective") != cfg_.cend() && + if (cfg_.find("max_delta_step") == cfg_.cend() && cfg_.find("objective") != cfg_.cend() && tparam_.objective == "count:poisson") { // max_delta_step is a duplicated parameter in Poisson regression and tree param. // Rename one of them once binary IO is gone. @@ -844,7 +835,7 @@ class LearnerConfiguration : public Intercept { obj_.reset(ObjFunction::Create(tparam_.objective, &ctx_)); } - bool has_nc {cfg_.find("num_class") != cfg_.cend()}; + bool has_nc{cfg_.find("num_class") != cfg_.cend()}; // Inject num_class into configuration. // FIXME(jiamingy): Remove the duplicated parameter in softmax cfg_["num_class"] = std::to_string(mparam_.num_class); @@ -858,7 +849,9 @@ class LearnerConfiguration : public Intercept { void ConfigureMetrics(Args const& args) { for (auto const& name : metric_names_) { - auto DupCheck = [&name](std::unique_ptr const& m) { return m->Name() != name; }; + auto DupCheck = [&name](std::unique_ptr const& m) { + return m->Name() != name; + }; if (std::all_of(metrics_.begin(), metrics_.end(), DupCheck)) { metrics_.emplace_back(std::unique_ptr(Metric::Create(name, &ctx_))); } @@ -877,7 +870,7 @@ class LearnerConfiguration : public Intercept { } }; -std::string const LearnerConfiguration::kEvalMetric {"eval_metric"}; // NOLINT +std::string const LearnerConfiguration::kEvalMetric{"eval_metric"}; // NOLINT class LearnerIO : public LearnerConfiguration { protected: @@ -908,8 +901,7 @@ class LearnerIO : public LearnerConfiguration { auto const& gradient_booster = learner.at("gradient_booster"); name = get(gradient_booster["name"]); tparam_.UpdateAllowUnknown(Args{{"booster", name}}); - gbm_.reset( - GradientBooster::Create(tparam_.booster, &ctx_, &learner_model_param_)); + gbm_.reset(GradientBooster::Create(tparam_.booster, &ctx_, &learner_model_param_)); gbm_->LoadModel(gradient_booster); auto const& j_attributes = get(learner.at("attributes")); @@ -943,7 +935,7 @@ class LearnerIO : public LearnerConfiguration { this->CheckModelInitialized(); Version::Save(p_out); - Json& out { *p_out }; + Json& out{*p_out}; out["learner"] = Object(); auto& learner = out["learner"]; @@ -1020,8 +1012,7 @@ class LearnerIO : public LearnerConfiguration { */ class LearnerImpl : public LearnerIO { public: - explicit LearnerImpl(std::vector > cache) - : LearnerIO{cache} {} + explicit LearnerImpl(std::vector> cache) : LearnerIO{cache} {} ~LearnerImpl() override { auto local_map = LearnerAPIThreadLocalStore::Get(); if (local_map->find(this) != local_map->cend()) { @@ -1148,8 +1139,7 @@ class LearnerImpl : public LearnerIO { this->monitor_.Stop(__func__); } - std::string EvalOneIter(int iter, - const std::vector>& data_sets, + std::string EvalOneIter(int iter, const std::vector>& data_sets, const std::vector& data_names) override { monitor_.Start("EvalOneIter"); this->Configure(); @@ -1173,7 +1163,7 @@ class LearnerImpl : public LearnerIO { this->ValidateDMatrix(m.get(), false); this->PredictRaw(m.get(), predt.get(), false, 0, 0); - auto &out = output_predictions_.Cache(m, ctx_.Device())->predictions; + auto& out = output_predictions_.Cache(m, ctx_.Device())->predictions; out.Resize(predt->predictions.Size()); out.Copy(predt->predictions); @@ -1191,8 +1181,7 @@ class LearnerImpl : public LearnerIO { HostDeviceVector* out_preds, bst_layer_t layer_begin, bst_layer_t layer_end, bool training, bool pred_leaf, bool pred_contribs, bool approx_contribs, bool pred_interactions) override { - int multiple_predictions = static_cast(pred_leaf) + - static_cast(pred_interactions) + + int multiple_predictions = static_cast(pred_leaf) + static_cast(pred_interactions) + static_cast(pred_contribs); this->Configure(); if (training) { @@ -1222,7 +1211,9 @@ class LearnerImpl : public LearnerIO { } int32_t BoostedRounds() const override { - if (!this->gbm_) { return 0; } // haven't call train or LoadModel. + if (!this->gbm_) { + return 0; + } // haven't call train or LoadModel. CHECK(!this->need_configuration_); return this->gbm_->BoostedRounds(); } @@ -1279,7 +1270,7 @@ class LearnerImpl : public LearnerIO { * predictor, when it equals 0, this means we are using all the trees * \param training allow dropout when the DART booster is being used */ - void PredictRaw(DMatrix *data, PredictionCacheEntry *out_preds, bool training, + void PredictRaw(DMatrix* data, PredictionCacheEntry* out_preds, bool training, unsigned layer_begin, unsigned layer_end) const { CHECK(gbm_ != nullptr) << "Predict must happen after Load or configuration"; this->CheckModelInitialized(); @@ -1325,8 +1316,7 @@ class LearnerImpl : public LearnerIO { constexpr int32_t LearnerImpl::kRandSeedMagic; -Learner* Learner::Create( - const std::vector >& cache_data) { +Learner* Learner::Create(const std::vector>& cache_data) { return new LearnerImpl(cache_data); } } // namespace xgboost diff --git a/src/linear/coordinate_common.h b/src/linear/coordinate_common.h index 60959f122d00..061ed0deef27 100644 --- a/src/linear/coordinate_common.h +++ b/src/linear/coordinate_common.h @@ -23,11 +23,9 @@ namespace linear { struct CoordinateParam : public XGBoostParameter { int top_k; DMLC_DECLARE_PARAMETER(CoordinateParam) { - DMLC_DECLARE_FIELD(top_k) - .set_lower_bound(0) - .set_default(0) - .describe("The number of top features to select in 'thrifty' feature_selector. " - "The value of zero means using all the features."); + DMLC_DECLARE_FIELD(top_k).set_lower_bound(0).set_default(0).describe( + "The number of top features to select in 'thrifty' feature_selector. " + "The value of zero means using all the features."); } }; @@ -43,8 +41,8 @@ struct CoordinateParam : public XGBoostParameter { * * \return The weight update. */ -inline double CoordinateDelta(double sum_grad, double sum_hess, double w, - double reg_alpha, double reg_lambda) { +inline double CoordinateDelta(double sum_grad, double sum_hess, double w, double reg_alpha, + double reg_lambda) { if (sum_hess < 1e-5f) return 0.0f; const double sum_grad_l2 = sum_grad + reg_lambda * w; const double sum_hess_l2 = sum_hess + reg_lambda; @@ -136,10 +134,8 @@ inline std::pair GetGradientParallel(Context const *ctx, int gro sum_hess_tloc[t_idx] += p.GetHess() * v * v; }); } - double sum_grad = - std::accumulate(sum_grad_tloc.cbegin(), sum_grad_tloc.cend(), 0.0); - double sum_hess = - std::accumulate(sum_hess_tloc.cbegin(), sum_hess_tloc.cend(), 0.0); + double sum_grad = std::accumulate(sum_grad_tloc.cbegin(), sum_grad_tloc.cend(), 0.0); + double sum_hess = std::accumulate(sum_hess_tloc.cbegin(), sum_hess_tloc.cend(), 0.0); return std::make_pair(sum_grad, sum_hess); } @@ -243,8 +239,8 @@ class FeatureSelector { * \param lambda Regularisation lambda. * \param param A parameter with algorithm-dependent use. */ - virtual void Setup(Context const *, const gbm::GBLinearModel &, - const std::vector &, DMatrix *, float, float, int) {} + virtual void Setup(Context const *, const gbm::GBLinearModel &, const std::vector &, + DMatrix *, float, float, int) {} /** * \brief Select next coordinate to update. * @@ -340,9 +336,9 @@ class GreedyFeatureSelector : public FeatureSelector { } } - int NextFeature(Context const* ctx, int, const gbm::GBLinearModel &model, - int group_idx, const std::vector &gpair, - DMatrix *p_fmat, float alpha, float lambda) override { + int NextFeature(Context const *ctx, int, const gbm::GBLinearModel &model, int group_idx, + const std::vector &gpair, DMatrix *p_fmat, float alpha, + float lambda) override { // k-th selected feature for a group auto k = counter_[group_idx]++; // stop after either reaching top-K or going through all the features in a group @@ -373,7 +369,7 @@ class GreedyFeatureSelector : public FeatureSelector { for (bst_omp_uint fidx = 0; fidx < nfeat; ++fidx) { auto &s = gpair_sums_[group_idx * nfeat + fidx]; float dw = std::abs(static_cast( - CoordinateDelta(s.first, s.second, model[fidx][group_idx], alpha, lambda))); + CoordinateDelta(s.first, s.second, model[fidx][group_idx], alpha, lambda))); if (dw > best_weight_update) { best_weight_update = dw; best_fidx = fidx; @@ -446,15 +442,14 @@ class ThriftyFeatureSelector : public FeatureSelector { for (bst_omp_uint i = 0; i < nfeat; ++i) { auto ii = gid * nfeat + i; auto &s = gpair_sums_[ii]; - deltaw_[ii] = static_cast(CoordinateDelta( - s.first, s.second, model[i][gid], alpha, lambda)); + deltaw_[ii] = static_cast( + CoordinateDelta(s.first, s.second, model[i][gid], alpha, lambda)); } // sort in descending order of deltaw abs values auto start = sorted_idx_.begin() + gid * nfeat; - std::sort(start, start + nfeat, - [pdeltaw](size_t i, size_t j) { - return std::abs(*(pdeltaw + i)) > std::abs(*(pdeltaw + j)); - }); + std::sort(start, start + nfeat, [pdeltaw](size_t i, size_t j) { + return std::abs(*(pdeltaw + i)) > std::abs(*(pdeltaw + j)); + }); counter_[gid] = 0u; } } diff --git a/src/linear/linear_updater.cc b/src/linear/linear_updater.cc index e66206196bce..dfae93b3859f 100644 --- a/src/linear/linear_updater.cc +++ b/src/linear/linear_updater.cc @@ -1,8 +1,11 @@ /*! * Copyright 2018 */ -#include #include +#include + +#include + #include "./param.h" namespace dmlc { @@ -12,7 +15,7 @@ DMLC_REGISTRY_ENABLE(::xgboost::LinearUpdaterReg); namespace xgboost { LinearUpdater* LinearUpdater::Create(const std::string& name, Context const* ctx) { - auto *e = ::dmlc::Registry< ::xgboost::LinearUpdaterReg>::Get()->Find(name); + auto* e = ::dmlc::Registry< ::xgboost::LinearUpdaterReg>::Get()->Find(name); if (e == nullptr) { LOG(FATAL) << "Unknown linear updater " << name; } diff --git a/src/linear/param.h b/src/linear/param.h index 7cf443027a05..f56fd33f3656 100644 --- a/src/linear/param.h +++ b/src/linear/param.h @@ -12,13 +12,7 @@ namespace linear { /** * \brief A set of available FeatureSelector's */ -enum FeatureSelectorEnum { - kCyclic = 0, - kShuffle, - kThrifty, - kGreedy, - kRandom -}; +enum FeatureSelectorEnum { kCyclic = 0, kShuffle, kThrifty, kGreedy, kRandom }; struct LinearTrainParam : public XGBoostParameter { /*! \brief learning_rate */ @@ -38,10 +32,8 @@ struct LinearTrainParam : public XGBoostParameter { .set_lower_bound(0.0f) .set_default(0.0f) .describe("L2 regularization on weights."); - DMLC_DECLARE_FIELD(reg_alpha) - .set_lower_bound(0.0f) - .set_default(0.0f) - .describe("L1 regularization on weights."); + DMLC_DECLARE_FIELD(reg_alpha).set_lower_bound(0.0f).set_default(0.0f).describe( + "L1 regularization on weights."); DMLC_DECLARE_FIELD(feature_selector) .set_default(kCyclic) .add_enum("cyclic", kCyclic) diff --git a/src/linear/updater_coordinate.cc b/src/linear/updater_coordinate.cc index 709d1fc8772d..e63ddc65fe17 100644 --- a/src/linear/updater_coordinate.cc +++ b/src/linear/updater_coordinate.cc @@ -4,8 +4,13 @@ */ #include -#include "./param.h" + +#include +#include +#include + #include "../common/timer.h" +#include "./param.h" #include "coordinate_common.h" #include "xgboost/json.h" @@ -24,17 +29,15 @@ DMLC_REGISTRY_FILE_TAG(updater_coordinate); class CoordinateUpdater : public LinearUpdater { public: // set training parameter - void Configure(Args const& args) override { - const std::vector > rest { - tparam_.UpdateAllowUnknown(args) - }; + void Configure(Args const &args) override { + const std::vector > rest{tparam_.UpdateAllowUnknown(args)}; cparam_.UpdateAllowUnknown(rest); selector_.reset(FeatureSelector::Create(tparam_.feature_selector)); monitor_.Init("CoordinateUpdater"); } - void LoadConfig(Json const& in) override { - auto const& config = get(in); + void LoadConfig(Json const &in) override { + auto const &config = get(in); FromJson(config.at("linear_train_param"), &tparam_); FromJson(config.at("coordinate_param"), &cparam_); } @@ -79,12 +82,11 @@ class CoordinateUpdater : public LinearUpdater { gbm::GBLinearModel *model) { const int ngroup = model->learner_model_param->num_output_group; bst_float &w = (*model)[fidx][group_idx]; - auto gradient = GetGradientParallel(ctx_, group_idx, ngroup, fidx, - *in_gpair, p_fmat); - auto dw = static_cast( - tparam_.learning_rate * - CoordinateDelta(gradient.first, gradient.second, w, tparam_.reg_alpha_denorm, - tparam_.reg_lambda_denorm)); + auto gradient = GetGradientParallel(ctx_, group_idx, ngroup, fidx, *in_gpair, p_fmat); + auto dw = + static_cast(tparam_.learning_rate * CoordinateDelta(gradient.first, gradient.second, + w, tparam_.reg_alpha_denorm, + tparam_.reg_lambda_denorm)); w += dw; UpdateResidualParallel(ctx_, fidx, group_idx, ngroup, dw, in_gpair, p_fmat); } diff --git a/src/linear/updater_gpu_coordinate.cu b/src/linear/updater_gpu_coordinate.cu index e1bc5b73af53..3c355f2e6e2d 100644 --- a/src/linear/updater_gpu_coordinate.cu +++ b/src/linear/updater_gpu_coordinate.cu @@ -7,13 +7,16 @@ #include #include #include -#include "xgboost/span.h" -#include "coordinate_common.h" +#include +#include + #include "../common/common.h" #include "../common/device_helpers.cuh" #include "../common/timer.h" #include "./param.h" +#include "coordinate_common.h" +#include "xgboost/span.h" namespace xgboost::linear { @@ -35,8 +38,8 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT monitor_.Init("GPUCoordinateUpdater"); } - void LoadConfig(Json const& in) override { - auto const& config = get(in); + void LoadConfig(Json const &in) override { + auto const &config = get(in); FromJson(config.at("linear_train_param"), &tparam_); FromJson(config.at("coordinate_param"), &coord_param_); } @@ -71,12 +74,9 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT auto cmp = [](Entry e1, Entry e2) { return e1.index < e2.index; }; - auto column_begin = - std::lower_bound(col.cbegin(), col.cend(), - xgboost::Entry(0, 0.0f), cmp); + auto column_begin = std::lower_bound(col.cbegin(), col.cend(), xgboost::Entry(0, 0.0f), cmp); auto column_end = - std::lower_bound(col.cbegin(), col.cend(), - xgboost::Entry(num_row_, 0.0f), cmp); + std::lower_bound(col.cbegin(), col.cend(), xgboost::Entry(num_row_, 0.0f), cmp); column_segments.emplace_back(static_cast(column_begin - col.cbegin()), static_cast(column_end - col.cbegin())); row_ptr_.push_back(row_ptr_.back() + (column_end - column_begin)); @@ -86,10 +86,8 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT for (size_t fidx = 0; fidx < batch.Size(); fidx++) { auto col = page[fidx]; auto seg = column_segments[fidx]; - dh::safe_cuda(cudaMemcpy( - data_.data().get() + row_ptr_[fidx], - col.data() + seg.first, - sizeof(Entry) * (seg.second - seg.first), cudaMemcpyHostToDevice)); + dh::safe_cuda(cudaMemcpy(data_.data().get() + row_ptr_[fidx], col.data() + seg.first, + sizeof(Entry) * (seg.second - seg.first), cudaMemcpyHostToDevice)); } } @@ -136,9 +134,8 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT if (ctx_->IsCUDA()) { grad = GetBiasGradient(group_idx, model->learner_model_param->num_output_group); } - auto dbias = static_cast( - tparam_.learning_rate * - CoordinateDeltaBias(grad.GetGrad(), grad.GetHess())); + auto dbias = static_cast(tparam_.learning_rate * + CoordinateDeltaBias(grad.GetGrad(), grad.GetHess())); model->Bias()[group_idx] += dbias; // Update residual @@ -148,18 +145,17 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT } } - void UpdateFeature(int fidx, int group_idx, - gbm::GBLinearModel *model) { + void UpdateFeature(int fidx, int group_idx, gbm::GBLinearModel *model) { bst_float &w = (*model)[fidx][group_idx]; // Get gradient auto grad = GradientPair(0, 0); if (ctx_->IsCUDA()) { grad = GetGradient(group_idx, model->learner_model_param->num_output_group, fidx); } - auto dw = static_cast(tparam_.learning_rate * - CoordinateDelta(grad.GetGrad(), grad.GetHess(), - w, tparam_.reg_alpha_denorm, - tparam_.reg_lambda_denorm)); + auto dw = + static_cast(tparam_.learning_rate * CoordinateDelta(grad.GetGrad(), grad.GetHess(), + w, tparam_.reg_alpha_denorm, + tparam_.reg_lambda_denorm)); w += dw; if (ctx_->IsCUDA()) { @@ -174,8 +170,7 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT auto f = [=] __device__(size_t idx) { return idx * num_group + group_idx; }; // NOLINT - thrust::transform_iterator skip( - counting, f); + thrust::transform_iterator skip(counting, f); auto perm = thrust::make_permutation_iterator(gpair_.data(), skip); return dh::SumReduction(perm, num_row_); @@ -203,8 +198,8 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT auto g = d_gpair[entry.index * num_group + group_idx]; return GradientPair{g.GetGrad() * entry.fvalue, g.GetHess() * entry.fvalue * entry.fvalue}; }; // NOLINT - thrust::transform_iterator - multiply_iterator(counting, f); + thrust::transform_iterator multiply_iterator( + counting, f); return dh::SumReduction(multiply_iterator, col_size); } @@ -221,15 +216,11 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT } private: - bool IsEmpty() { - return num_row_ == 0; - } + bool IsEmpty() { return num_row_ == 0; } void UpdateGpair(const std::vector &host_gpair) { - dh::safe_cuda(cudaMemcpyAsync( - gpair_.data().get(), - host_gpair.data(), - gpair_.size() * sizeof(GradientPair), cudaMemcpyHostToDevice)); + dh::safe_cuda(cudaMemcpyAsync(gpair_.data().get(), host_gpair.data(), + gpair_.size() * sizeof(GradientPair), cudaMemcpyHostToDevice)); } // training parameter diff --git a/src/linear/updater_shotgun.cc b/src/linear/updater_shotgun.cc index 78fb1fe1b690..9711b4fc3726 100644 --- a/src/linear/updater_shotgun.cc +++ b/src/linear/updater_shotgun.cc @@ -4,6 +4,9 @@ */ #include + +#include + #include "coordinate_common.h" namespace xgboost::linear { @@ -13,21 +16,20 @@ DMLC_REGISTRY_FILE_TAG(updater_shotgun); class ShotgunUpdater : public LinearUpdater { public: // set training parameter - void Configure(Args const& args) override { + void Configure(Args const &args) override { param_.UpdateAllowUnknown(args); - if (param_.feature_selector != kCyclic && - param_.feature_selector != kShuffle) { + if (param_.feature_selector != kCyclic && param_.feature_selector != kShuffle) { LOG(FATAL) << "Unsupported feature selector for shotgun updater.\n" << "Supported options are: {cyclic, shuffle}"; } selector_.reset(FeatureSelector::Create(param_.feature_selector)); } - void LoadConfig(Json const& in) override { - auto const& config = get(in); + void LoadConfig(Json const &in) override { + auto const &config = get(in); FromJson(config.at("linear_train_param"), ¶m_); } - void SaveConfig(Json* p_out) const override { - auto& out = *p_out; + void SaveConfig(Json *p_out) const override { + auto &out = *p_out; out["linear_train_param"] = ToJson(param_); } @@ -39,10 +41,10 @@ class ShotgunUpdater : public LinearUpdater { // update bias for (int gid = 0; gid < ngroup; ++gid) { - auto grad = GetBiasGradientParallel(gid, ngroup, gpair->ConstHostVector(), p_fmat, - ctx_->Threads()); + auto grad = + GetBiasGradientParallel(gid, ngroup, gpair->ConstHostVector(), p_fmat, ctx_->Threads()); auto dbias = static_cast(param_.learning_rate * - CoordinateDeltaBias(grad.first, grad.second)); + CoordinateDeltaBias(grad.first, grad.second)); model->Bias()[gid] += dbias; UpdateBiasResidualParallel(ctx_, gid, ngroup, dbias, &gpair->HostVector(), p_fmat); } diff --git a/src/logging.cc b/src/logging.cc index 4cf74207d2d9..fff52389e5c3 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -13,8 +13,7 @@ #if !defined(XGBOOST_STRICT_R_MODE) || XGBOOST_STRICT_R_MODE == 0 // Override logging mechanism for non-R interfaces void dmlc::CustomLogMessage::Log(const std::string& msg) { - const xgboost::LogCallbackRegistry *registry = - xgboost::LogCallbackRegistryStore::Get(); + const xgboost::LogCallbackRegistry* registry = xgboost::LogCallbackRegistryStore::Get(); auto callback = registry->Get(); callback(msg.c_str()); } @@ -39,8 +38,7 @@ TrackerLogger::~TrackerLogger() { namespace xgboost { bool ConsoleLogger::ShouldLog(LogVerbosity verbosity) { - return static_cast(verbosity) <= - (GlobalConfigThreadLocalStore::Get()->verbosity) || + return static_cast(verbosity) <= (GlobalConfigThreadLocalStore::Get()->verbosity) || verbosity == LV::kIgnore; } @@ -49,50 +47,43 @@ void ConsoleLogger::Configure(Args const& args) { param.UpdateAllowUnknown(args); } -ConsoleLogger::LogVerbosity ConsoleLogger::DefaultVerbosity() { - return LogVerbosity::kWarning; -} +ConsoleLogger::LogVerbosity ConsoleLogger::DefaultVerbosity() { return LogVerbosity::kWarning; } ConsoleLogger::LogVerbosity ConsoleLogger::GlobalVerbosity() { - LogVerbosity global_verbosity { LogVerbosity::kWarning }; + LogVerbosity global_verbosity{LogVerbosity::kWarning}; switch (GlobalConfigThreadLocalStore::Get()->verbosity) { - case 0: - global_verbosity = LogVerbosity::kSilent; - break; - case 1: - global_verbosity = LogVerbosity::kWarning; - break; - case 2: - global_verbosity = LogVerbosity::kInfo; - break; - case 3: - global_verbosity = LogVerbosity::kDebug; - default: - // global verbosity doesn't require kIgnore - break; + case 0: + global_verbosity = LogVerbosity::kSilent; + break; + case 1: + global_verbosity = LogVerbosity::kWarning; + break; + case 2: + global_verbosity = LogVerbosity::kInfo; + break; + case 3: + global_verbosity = LogVerbosity::kDebug; + default: + // global verbosity doesn't require kIgnore + break; } return global_verbosity; } -ConsoleLogger::ConsoleLogger(LogVerbosity cur_verb) : - cur_verbosity_{cur_verb} {} +ConsoleLogger::ConsoleLogger(LogVerbosity cur_verb) : cur_verbosity_{cur_verb} {} -ConsoleLogger::ConsoleLogger( - const std::string& file, int line, LogVerbosity cur_verb) { +ConsoleLogger::ConsoleLogger(const std::string& file, int line, LogVerbosity cur_verb) { cur_verbosity_ = cur_verb; switch (cur_verbosity_) { case LogVerbosity::kWarning: - BaseLogger::log_stream_ << "WARNING: " - << file << ":" << line << ": "; + BaseLogger::log_stream_ << "WARNING: " << file << ":" << line << ": "; break; case LogVerbosity::kDebug: - BaseLogger::log_stream_ << "DEBUG: " - << file << ":" << line << ": "; + BaseLogger::log_stream_ << "DEBUG: " << file << ":" << line << ": "; break; case LogVerbosity::kInfo: - BaseLogger::log_stream_ << "INFO: " - << file << ":" << line << ": "; + BaseLogger::log_stream_ << "INFO: " << file << ":" << line << ": "; break; case LogVerbosity::kIgnore: BaseLogger::log_stream_ << file << ":" << line << ": "; diff --git a/src/metric/auc.cc b/src/metric/auc.cc index fcb774a4aa70..44907655aa93 100644 --- a/src/metric/auc.cc +++ b/src/metric/auc.cc @@ -14,7 +14,7 @@ #include #include -#include "../common/algorithm.h" // ArgSort +#include "../common/algorithm.h" // ArgSort #include "../common/math.h" #include "../common/optional_weight.h" // OptionalWeights #include "metric_common.h" // MetricNoCache @@ -32,10 +32,10 @@ DMLC_REGISTRY_FILE_TAG(auc); * handle the normalization. */ template -std::tuple -BinaryAUC(common::Span predts, linalg::VectorView labels, - common::OptionalWeights weights, - std::vector const &sorted_idx, Fn &&area_fn) { +std::tuple BinaryAUC(common::Span predts, + linalg::VectorView labels, + common::OptionalWeights weights, + std::vector const &sorted_idx, Fn &&area_fn) { CHECK_NE(labels.Size(), 0); CHECK_EQ(labels.Size(), predts.size()); auto p_predts = predts.data(); @@ -149,7 +149,7 @@ std::tuple BinaryROCAUC(Context const *ctx, /** * Calculate AUC for 1 ranking group; */ -double GroupRankingROC(Context const* ctx, common::Span predts, +double GroupRankingROC(Context const *ctx, common::Span predts, linalg::VectorView labels, float w) { // on ranking, we just count all pairs. double auc{0}; @@ -257,7 +257,7 @@ std::pair RankingAUC(Context const *ctx, std::vector co template class EvalAUC : public MetricNoCache { double Eval(const HostDeviceVector &preds, const MetaInfo &info) override { - double auc {0}; + double auc{0}; if (ctx_->Device().IsCUDA()) { preds.SetDevice(ctx_->Device()); info.labels.SetDevice(ctx_->Device()); @@ -286,8 +286,7 @@ class EvalAUC : public MetricNoCache { uint32_t valid_groups = 0; if (info.labels.Size() != 0) { CHECK_EQ(info.group_ptr_.back(), info.labels.Size()); - std::tie(auc, valid_groups) = - static_cast(this)->EvalRanking(preds, info); + std::tie(auc, valid_groups) = static_cast(this)->EvalRanking(preds, info); } if (valid_groups != info.group_ptr_.size() - 1) { InvalidGroupAUC(); @@ -311,8 +310,7 @@ class EvalAUC : public MetricNoCache { */ double fp{0}, tp{0}; if (!(preds.Empty() || info.labels.Size() == 0)) { - std::tie(fp, tp, auc) = - static_cast(this)->EvalBinary(preds, info); + std::tie(fp, tp, auc) = static_cast(this)->EvalBinary(preds, info); } auc = collective::GlobalRatio(ctx_, info, auc, fp * tp); if (!std::isnan(auc)) { @@ -346,8 +344,8 @@ class EvalROCAUC : public EvalAUC { return std::make_pair(auc, valid_groups); } - double EvalMultiClass(HostDeviceVector const &predts, - MetaInfo const &info, size_t n_classes) { + double EvalMultiClass(HostDeviceVector const &predts, MetaInfo const &info, + size_t n_classes) { double auc{0}; auto n_threads = ctx_->Threads(); CHECK_NE(n_classes, 0); @@ -359,8 +357,8 @@ class EvalROCAUC : public EvalAUC { return auc; } - std::tuple - EvalBinary(HostDeviceVector const &predts, MetaInfo const &info) { + std::tuple EvalBinary(HostDeviceVector const &predts, + MetaInfo const &info) { double fp, tp, auc; if (ctx_->IsCUDA()) { std::tie(fp, tp, auc) = @@ -374,14 +372,12 @@ class EvalROCAUC : public EvalAUC { } public: - [[nodiscard]] char const* Name() const override { - return "auc"; - } + [[nodiscard]] char const *Name() const override { return "auc"; } }; XGBOOST_REGISTER_METRIC(EvalAUC, "auc") -.describe("Receiver Operating Characteristic Area Under the Curve.") -.set_body([](const char*) { return new EvalROCAUC(); }); + .describe("Receiver Operating Characteristic Area Under the Curve.") + .set_body([](const char *) { return new EvalROCAUC(); }); #if !defined(XGBOOST_USE_CUDA) std::tuple GPUBinaryROCAUC(Context const *, common::Span, @@ -410,8 +406,8 @@ class EvalPRAUC : public EvalAUC { std::shared_ptr d_cache_; public: - std::tuple - EvalBinary(HostDeviceVector const &predts, MetaInfo const &info) { + std::tuple EvalBinary(HostDeviceVector const &predts, + MetaInfo const &info) { double pr, re, auc; if (ctx_->IsCUDA()) { std::tie(pr, re, auc) = GPUBinaryPRAUC(ctx_, predts.ConstDeviceSpan(), info, &this->d_cache_); diff --git a/src/metric/auc.cu b/src/metric/auc.cu index 6dbb6e588531..6a22cbabcfc1 100644 --- a/src/metric/auc.cu +++ b/src/metric/auc.cu @@ -68,7 +68,7 @@ struct DeviceAUCCache { template void InitCacheOnce(common::Span predts, std::shared_ptr *p_cache) { - auto& cache = *p_cache; + auto &cache = *p_cache; if (!cache) { cache.reset(new DeviceAUCCache); } @@ -101,7 +101,7 @@ std::tuple GPUBinaryAUC(Context const *ctx, * Linear scan */ auto get_weight = common::OptionalWeights{weights}; - auto get_fp_tp = [=]XGBOOST_DEVICE(size_t i) { + auto get_fp_tp = [=] XGBOOST_DEVICE(size_t i) { size_t idx = d_sorted_idx[i]; float label = labels(idx); @@ -189,8 +189,7 @@ std::tuple GPUBinaryROCAUC(Context const *ctx, cache); } -void Transpose(common::Span in, common::Span out, size_t m, - size_t n) { +void Transpose(common::Span in, common::Span out, size_t m, size_t n) { CHECK_EQ(in.size(), out.size()); CHECK_EQ(in.size(), m * n); dh::LaunchN(in.size(), [=] XGBOOST_DEVICE(size_t i) { @@ -248,8 +247,7 @@ void SegmentedFPTP(Context const *ctx, common::Span d_fptp, Fn segment_id) // shrink down to pair auto fptp_it_out = thrust::make_transform_output_iterator( dh::TypedDiscard{}, [d_fptp] XGBOOST_DEVICE(Triple const &t) { - d_fptp[thrust::get<0>(t)] = - cuda::std::make_pair(thrust::get<1>(t), thrust::get<2>(t)); + d_fptp[thrust::get<0>(t)] = cuda::std::make_pair(thrust::get<1>(t), thrust::get<2>(t)); return t; }); common::InclusiveScan( @@ -279,11 +277,11 @@ void SegmentedReduceAUC(Context const *ctx, common::Span d_unique_ common::Span d_auc) { auto d_fptp = dh::ToSpan(cache->fptp); auto d_neg_pos = dh::ToSpan(cache->neg_pos); - auto key_in = dh::MakeTransformIterator( - thrust::make_counting_iterator(0), [=] XGBOOST_DEVICE(size_t i) { - size_t class_id = segment_id(d_unique_idx[i]); - return class_id; - }); + auto key_in = dh::MakeTransformIterator(thrust::make_counting_iterator(0), + [=] XGBOOST_DEVICE(size_t i) { + size_t class_id = segment_id(d_unique_idx[i]); + return class_id; + }); auto val_in = dh::MakeTransformIterator( thrust::make_counting_iterator(0), [=] XGBOOST_DEVICE(size_t i) { size_t class_id = segment_id(d_unique_idx[i]); @@ -342,7 +340,7 @@ double GPUMultiClassAUCOVR(Context const *ctx, MetaInfo const &info, dh::caching_device_vector d_auc(n_classes, 0); auto get_weight = common::OptionalWeights{weights}; auto d_fptp = dh::ToSpan(cache->fptp); - auto get_fp_tp = [=]XGBOOST_DEVICE(size_t i) { + auto get_fp_tp = [=] XGBOOST_DEVICE(size_t i) { size_t idx = d_sorted_idx[i]; size_t class_id = i / n_samples; @@ -354,8 +352,7 @@ double GPUMultiClassAUCOVR(Context const *ctx, MetaInfo const &info, float tp = label * w; return cuda::std::make_pair(fp, tp); }; // NOLINT - dh::LaunchN(d_sorted_idx.size(), - [=] XGBOOST_DEVICE(size_t i) { d_fptp[i] = get_fp_tp(i); }); + dh::LaunchN(d_sorted_idx.size(), [=] XGBOOST_DEVICE(size_t i) { d_fptp[i] = get_fp_tp(i); }); /** * Handle duplicated predictions @@ -378,7 +375,9 @@ double GPUMultiClassAUCOVR(Context const *ctx, MetaInfo const &info, dh::tbegin(d_unique_idx), std::equal_to>{}); d_unique_idx = d_unique_idx.subspan(0, n_uniques); - auto get_class_id = [=] XGBOOST_DEVICE(size_t idx) { return idx / n_samples; }; + auto get_class_id = [=] XGBOOST_DEVICE(size_t idx) { + return idx / n_samples; + }; SegmentedFPTP(ctx, d_fptp, get_class_id); // scatter unique FP_PREV/TP_PREV values @@ -388,7 +387,7 @@ double GPUMultiClassAUCOVR(Context const *ctx, MetaInfo const &info, dh::LaunchN(d_unique_idx.size(), ctx->CUDACtx()->Stream(), [=] XGBOOST_DEVICE(size_t i) { if (d_unique_idx[i] % n_samples == 0) { // first unique index is 0 assert(d_unique_idx[i] % n_samples == 0); - d_neg_pos[d_unique_idx[i]] = {0, 0}; // class_id * n_samples = i + d_neg_pos[d_unique_idx[i]] = {0, 0}; // class_id * n_samples = i return; } uint32_t class_id = d_unique_idx[i] / n_samples; @@ -452,7 +451,7 @@ void MultiClassSortedIdx(Context const *ctx, common::Span predts, double GPUMultiClassROCAUC(Context const *ctx, common::Span predts, MetaInfo const &info, std::shared_ptr *p_cache, std::size_t n_classes) { - auto& cache = *p_cache; + auto &cache = *p_cache; InitCacheOnce(predts, p_cache); /** @@ -480,7 +479,7 @@ struct RankScanItem { std::pair GPURankingAUC(Context const *ctx, common::Span predts, MetaInfo const &info, std::shared_ptr *p_cache) { - auto& cache = *p_cache; + auto &cache = *p_cache; InitCacheOnce(predts, p_cache); dh::caching_device_vector group_ptr(info.group_ptr_); @@ -519,7 +518,7 @@ std::pair GPURankingAUC(Context const *ctx, common::Span< std::numeric_limits::max()); CHECK_LT(n_threads, std::numeric_limits::max()); // get the coordinate in nested summation - auto get_i_j = [=]XGBOOST_DEVICE(size_t idx, size_t query_group_idx) { + auto get_i_j = [=] XGBOOST_DEVICE(size_t idx, size_t query_group_idx) { auto data_group_begin = d_group_ptr[query_group_idx]; size_t n_samples = d_group_ptr[query_group_idx + 1] - data_group_begin; auto thread_group_begin = d_threads_group_ptr[query_group_idx]; @@ -599,7 +598,7 @@ std::tuple GPUBinaryPRAUC(Context const *ctx, common::Span predts, MetaInfo const &info, std::shared_ptr *p_cache) { - auto& cache = *p_cache; + auto &cache = *p_cache; InitCacheOnce(predts, p_cache); /** @@ -611,12 +610,11 @@ std::tuple GPUBinaryPRAUC(Context const *ctx, auto labels = info.labels.View(ctx->Device()); auto d_weights = info.weights_.ConstDeviceSpan(); auto get_weight = common::OptionalWeights{d_weights}; - auto it = dh::MakeTransformIterator( - thrust::make_counting_iterator(0ul), [=] XGBOOST_DEVICE(size_t i) { - auto w = get_weight[d_sorted_idx[i]]; - return cuda::std::make_pair(labels(d_sorted_idx[i]) * w, - (1.0f - labels(d_sorted_idx[i])) * w); - }); + auto it = dh::MakeTransformIterator(thrust::make_counting_iterator(0ul), [=] XGBOOST_DEVICE( + size_t i) { + auto w = get_weight[d_sorted_idx[i]]; + return cuda::std::make_pair(labels(d_sorted_idx[i]) * w, (1.0f - labels(d_sorted_idx[i])) * w); + }); double total_pos, total_neg; thrust::tie(total_pos, total_neg) = thrust::reduce(ctx->CUDACtx()->CTP(), it, it + labels.Size(), Pair{0.0, 0.0}, PairPlus{}); @@ -625,8 +623,7 @@ std::tuple GPUBinaryPRAUC(Context const *ctx, return {0.0f, 0.0f, 0.0f}; } - auto fn = [total_pos] XGBOOST_DEVICE(double fp_prev, double fp, double tp_prev, - double tp) { + auto fn = [total_pos] XGBOOST_DEVICE(double fp_prev, double fp, double tp_prev, double tp) { return detail::CalcDeltaPRAUC(fp_prev, fp, tp_prev, tp, total_pos); }; double fp, tp, auc; @@ -637,7 +634,7 @@ std::tuple GPUBinaryPRAUC(Context const *ctx, double GPUMultiClassPRAUC(Context const *ctx, common::Span predts, MetaInfo const &info, std::shared_ptr *p_cache, std::size_t n_classes) { - auto& cache = *p_cache; + auto &cache = *p_cache; InitCacheOnce(predts, p_cache); /** @@ -656,11 +653,10 @@ double GPUMultiClassPRAUC(Context const *ctx, common::Span predts, auto labels = info.labels.View(ctx->Device()); auto n_samples = info.num_row_; dh::caching_device_vector totals(n_classes); - auto key_it = - dh::MakeTransformIterator(thrust::make_counting_iterator(0ul), - [n_samples] XGBOOST_DEVICE(size_t i) { - return i / n_samples; // class id - }); + auto key_it = dh::MakeTransformIterator(thrust::make_counting_iterator(0ul), + [n_samples] XGBOOST_DEVICE(size_t i) { + return i / n_samples; // class id + }); auto get_weight = common::OptionalWeights{d_weights}; auto val_it = dh::MakeTransformIterator>( thrust::make_counting_iterator(0ul), [=] XGBOOST_DEVICE(size_t i) { @@ -678,11 +674,10 @@ double GPUMultiClassPRAUC(Context const *ctx, common::Span predts, * Calculate AUC */ auto d_totals = dh::ToSpan(totals); - auto fn = [d_totals] XGBOOST_DEVICE(double fp_prev, double fp, double tp_prev, - double tp, size_t class_id) { + auto fn = [d_totals] XGBOOST_DEVICE(double fp_prev, double fp, double tp_prev, double tp, + size_t class_id) { auto total_pos = d_totals[class_id].first; - return detail::CalcDeltaPRAUC(fp_prev, fp, tp_prev, tp, - d_totals[class_id].first); + return detail::CalcDeltaPRAUC(fp_prev, fp, tp_prev, tp, d_totals[class_id].first); }; return GPUMultiClassAUCOVR(ctx, info, d_class_ptr, n_classes, cache, fn); } @@ -754,8 +749,7 @@ std::pair GPURankingPRAUCImpl(Context const *ctx, // scatter unique FP_PREV/TP_PREV values auto d_neg_pos = dh::ToSpan(cache->neg_pos); dh::LaunchN(d_unique_idx.size(), [=] XGBOOST_DEVICE(size_t i) { - if (thrust::binary_search(thrust::seq, d_unique_class_ptr.cbegin(), - d_unique_class_ptr.cend(), + if (thrust::binary_search(thrust::seq, d_unique_class_ptr.cbegin(), d_unique_class_ptr.cend(), i)) { // first unique index is 0 d_neg_pos[d_unique_idx[i]] = {0, 0}; return; @@ -838,17 +832,18 @@ std::pair GPURankingPRAUC(Context const *ctx, auto key_it = dh::MakeTransformIterator( thrust::make_counting_iterator(0ul), [=] XGBOOST_DEVICE(size_t i) { return dh::SegmentId(d_group_ptr, i); }); - auto val_it = dh::MakeTransformIterator( - thrust::make_counting_iterator(0ul), [=] XGBOOST_DEVICE(size_t i) { - float w = 1.0f; - if (!d_weights.empty()) { - // Avoid a binary search if the groups are not weighted. - auto g = dh::SegmentId(d_group_ptr, i); - w = d_weights[g]; - } - auto y = labels(i); - return cuda::std::make_pair(y * w, (1.0 - y) * w); - }); + auto val_it = dh::MakeTransformIterator(thrust::make_counting_iterator(0ul), + [=] XGBOOST_DEVICE(size_t i) { + float w = 1.0f; + if (!d_weights.empty()) { + // Avoid a binary search if the groups are not + // weighted. + auto g = dh::SegmentId(d_group_ptr, i); + w = d_weights[g]; + } + auto y = labels(i); + return cuda::std::make_pair(y * w, (1.0 - y) * w); + }); thrust::reduce_by_key(ctx->CUDACtx()->CTP(), key_it, key_it + predts.size(), val_it, thrust::make_discard_iterator(), totals.begin(), std::equal_to{}, PairPlus{}); // NOLINT @@ -857,11 +852,10 @@ std::pair GPURankingPRAUC(Context const *ctx, * Calculate AUC */ auto d_totals = dh::ToSpan(totals); - auto fn = [d_totals] XGBOOST_DEVICE(double fp_prev, double fp, double tp_prev, - double tp, size_t group_id) { + auto fn = [d_totals] XGBOOST_DEVICE(double fp_prev, double fp, double tp_prev, double tp, + size_t group_id) { auto total_pos = d_totals[group_id].first; - return detail::CalcDeltaPRAUC(fp_prev, fp, tp_prev, tp, - d_totals[group_id].first); + return detail::CalcDeltaPRAUC(fp_prev, fp, tp_prev, tp, d_totals[group_id].first); }; return GPURankingPRAUCImpl(ctx, predts, info, d_group_ptr, cache, fn); } diff --git a/src/metric/auc.h b/src/metric/auc.h index f27a1dda6160..a5115ae8292c 100644 --- a/src/metric/auc.h +++ b/src/metric/auc.h @@ -55,8 +55,7 @@ std::pair GPURankingPRAUC(Context const *ctx, std::shared_ptr *cache); namespace detail { -XGBOOST_DEVICE inline double CalcH(double fp_a, double fp_b, double tp_a, - double tp_b) { +XGBOOST_DEVICE inline double CalcH(double fp_a, double fp_b, double tp_a, double tp_b) { return (fp_b - fp_a) / (tp_b - tp_a); } @@ -66,8 +65,7 @@ XGBOOST_DEVICE inline double CalcB(double fp_a, double h, double tp_a, double to XGBOOST_DEVICE inline double CalcA(double h) { return h + 1; } -XGBOOST_DEVICE inline double CalcDeltaPRAUC(double fp_prev, double fp, - double tp_prev, double tp, +XGBOOST_DEVICE inline double CalcDeltaPRAUC(double fp_prev, double fp, double tp_prev, double tp, double total_pos) { double pr_prev = tp_prev / total_pos; double pr = tp / total_pos; @@ -85,9 +83,7 @@ XGBOOST_DEVICE inline double CalcDeltaPRAUC(double fp_prev, double fp, double area = 0; if (b != 0.0) { - area = (pr - pr_prev - - b / a * (std::log(a * pr + b) - std::log(a * pr_prev + b))) / - a; + area = (pr - pr_prev - b / a * (std::log(a * pr + b) - std::log(a * pr_prev + b))) / a; } else { area = (pr - pr_prev) / a; } @@ -96,8 +92,8 @@ XGBOOST_DEVICE inline double CalcDeltaPRAUC(double fp_prev, double fp, } // namespace detail inline void InvalidGroupAUC() { - LOG(INFO) << "Invalid group with less than 3 samples is found on worker " - << collective::GetRank() << ". Calculating AUC value requires at " + LOG(INFO) << "Invalid group with less than 3 samples is found on worker " << collective::GetRank() + << ". Calculating AUC value requires at " << "least 2 pairs of samples."; } diff --git a/src/metric/elementwise_metric.cu b/src/metric/elementwise_metric.cu index 78b846dcebad..ec70e17fd4bc 100644 --- a/src/metric/elementwise_metric.cu +++ b/src/metric/elementwise_metric.cu @@ -8,9 +8,13 @@ */ #include +#include #include #include +#include #include // for accumulate +#include +#include #include "../common/math.h" #include "../common/nvtx_utils.h" // for xgboost_NVTX_FN_RANGE @@ -23,14 +27,14 @@ #include "xgboost/metric.h" #if defined(XGBOOST_USE_CUDA) -#include // thrust::plus<> +#include // thrust::plus<> #include #include #include "../common/cuda_context.cuh" // for CUDAContext #else #include "../common/common.h" // for AssertGPUSupport -#endif // XGBOOST_USE_CUDA +#endif // XGBOOST_USE_CUDA namespace xgboost::metric { // tag the this file, used by force static link later. @@ -104,9 +108,7 @@ PackedReduceResult Reduce(Context const* ctx, MetaInfo const& info, Fn&& loss, } // anonymous namespace struct EvalRowRMSE { - char const *Name() const { - return "rmse"; - } + char const* Name() const { return "rmse"; } XGBOOST_DEVICE bst_float EvalRow(bst_float label, bst_float pred) const { bst_float diff = label - pred; @@ -118,9 +120,7 @@ struct EvalRowRMSE { }; struct EvalRowRMSLE { - char const* Name() const { - return "rmsle"; - } + char const* Name() const { return "rmsle"; } XGBOOST_DEVICE bst_float EvalRow(bst_float label, bst_float pred) const { bst_float diff = std::log1p(label) - std::log1p(pred); @@ -132,28 +132,20 @@ struct EvalRowRMSLE { }; struct EvalRowMAE { - const char *Name() const { - return "mae"; - } + const char* Name() const { return "mae"; } XGBOOST_DEVICE bst_float EvalRow(bst_float label, bst_float pred) const { return std::abs(label - pred); } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } }; struct EvalRowMAPE { - const char *Name() const { - return "mape"; - } + const char* Name() const { return "mape"; } XGBOOST_DEVICE bst_float EvalRow(bst_float label, bst_float pred) const { return std::abs((label - pred) / label); } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } }; namespace { @@ -168,14 +160,10 @@ XGBOOST_DEVICE inline float LogLoss(float y, float py) { } // anonymous namespace struct EvalRowLogLoss { - const char *Name() const { - return "logloss"; - } + const char* Name() const { return "logloss"; } XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float py) const { return LogLoss(y, py); } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } }; class PseudoErrorLoss : public MetricNoCache { @@ -229,7 +217,7 @@ struct EvalError { has_param_ = false; } } - [[nodiscard]] const char *Name() const { + [[nodiscard]] const char* Name() const { static thread_local std::string name; if (has_param_) { std::ostringstream os; @@ -247,9 +235,7 @@ struct EvalError { return pred > threshold_ ? 1.0f - label : label; } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } private: bst_float threshold_; @@ -257,9 +243,7 @@ struct EvalError { }; struct EvalPoissonNegLogLik { - [[nodiscard]] const char *Name() const { - return "poisson-nloglik"; - } + [[nodiscard]] const char* Name() const { return "poisson-nloglik"; } [[nodiscard]] XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float py) const { const bst_float eps = 1e-16f; @@ -267,9 +251,7 @@ struct EvalPoissonNegLogLik { return common::LogGamma(y + 1.0f) + py - std::log(py) * y; } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } }; /** @@ -280,7 +262,7 @@ struct EvalPoissonNegLogLik { * predt >= 0 */ struct EvalGammaDeviance { - [[nodiscard]] const char *Name() const { return "gamma-deviance"; } + [[nodiscard]] const char* Name() const { return "gamma-deviance"; } [[nodiscard]] XGBOOST_DEVICE bst_float EvalRow(bst_float label, bst_float predt) const { predt += kRtEps; @@ -297,9 +279,7 @@ struct EvalGammaDeviance { }; struct EvalGammaNLogLik { - static const char *Name() { - return "gamma-nloglik"; - } + static const char* Name() { return "gamma-nloglik"; } [[nodiscard]] XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float py) const { py = std::max(py, 1e-6f); @@ -314,20 +294,16 @@ struct EvalGammaNLogLik { // general form for exponential family. return -((y * theta - b) / a + c); } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } }; struct EvalTweedieNLogLik { explicit EvalTweedieNLogLik(const char* param) { - CHECK(param != nullptr) - << "tweedie-nloglik must be in format tweedie-nloglik@rho"; + CHECK(param != nullptr) << "tweedie-nloglik must be in format tweedie-nloglik@rho"; rho_ = atof(param); - CHECK(rho_ < 2 && rho_ >= 1) - << "tweedie variance power must be in interval [1, 2)"; + CHECK(rho_ < 2 && rho_ >= 1) << "tweedie variance power must be in interval [1, 2)"; } - [[nodiscard]] const char *Name() const { + [[nodiscard]] const char* Name() const { static thread_local std::string name; std::ostringstream os; os << "tweedie-nloglik@" << rho_; @@ -340,9 +316,7 @@ struct EvalTweedieNLogLik { bst_float b = std::exp((2 - rho_) * std::log(p)) / (2 - rho_); return -a + b; } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } protected: bst_float rho_; @@ -429,14 +403,12 @@ XGBOOST_REGISTER_METRIC(GammaNLogLik, "gamma-nloglik") .set_body([](const char*) { return new EvalEWiseBase(); }); XGBOOST_REGISTER_METRIC(Error, "error") -.describe("Binary classification error.") -.set_body([](const char* param) { return new EvalEWiseBase(param); }); + .describe("Binary classification error.") + .set_body([](const char* param) { return new EvalEWiseBase(param); }); XGBOOST_REGISTER_METRIC(TweedieNLogLik, "tweedie-nloglik") -.describe("tweedie-nloglik@rho for tweedie regression.") -.set_body([](const char* param) { - return new EvalEWiseBase(param); -}); + .describe("tweedie-nloglik@rho for tweedie regression.") + .set_body([](const char* param) { return new EvalEWiseBase(param); }); class QuantileError : public MetricNoCache { HostDeviceVector alpha_; @@ -475,7 +447,8 @@ class QuantileError : public MetricNoCache { : info.weights_.ConstDeviceSpan()}; auto result = Reduce( - ctx, info, [=] XGBOOST_DEVICE(std::size_t i, std::size_t sample_id, std::size_t target_id) { + ctx, info, + [=] XGBOOST_DEVICE(std::size_t i, std::size_t sample_id, std::size_t target_id) { auto idx = linalg::UnravelIndex(i, y_predt.Shape()); sample_id = std::get<0>(idx); std::size_t quantile_id = std::get<1>(idx); @@ -491,7 +464,8 @@ class QuantileError : public MetricNoCache { auto l = loss(y_predt(sample_id, quantile_id, target_id), y_true(sample_id, target_id)) * w; return std::make_tuple(l, w); - }, alpha_.Size()); + }, + alpha_.Size()); std::array dat{result.Residue(), result.Weights()}; auto rc = collective::GlobalSum(ctx, info, linalg::MakeVec(dat.data(), dat.size())); collective::SafeColl(rc); diff --git a/src/metric/metric.cc b/src/metric/metric.cc index d7e2683ecc02..f2c9ec5c8225 100644 --- a/src/metric/metric.cc +++ b/src/metric/metric.cc @@ -7,6 +7,8 @@ #include #include +#include + #include "metric_common.h" namespace xgboost { @@ -25,7 +27,7 @@ Metric* CreateMetricImpl(const std::string& name) { prefix = buf; param = nullptr; } - auto *e = ::dmlc::Registry::Get()->Find(prefix.c_str()); + auto* e = ::dmlc::Registry::Get()->Find(prefix.c_str()); if (e == nullptr) { return nullptr; } @@ -33,7 +35,7 @@ Metric* CreateMetricImpl(const std::string& name) { return p_metric; } else { std::string prefix = buf.substr(0, pos); - auto *e = ::dmlc::Registry::Get()->Find(prefix.c_str()); + auto* e = ::dmlc::Registry::Get()->Find(prefix.c_str()); if (e == nullptr) { return nullptr; } @@ -42,8 +44,7 @@ Metric* CreateMetricImpl(const std::string& name) { } } -Metric * -Metric::Create(const std::string& name, Context const* ctx) { +Metric* Metric::Create(const std::string& name, Context const* ctx) { auto metric = CreateMetricImpl(name); if (metric == nullptr) { LOG(FATAL) << "Unknown metric function " << name; diff --git a/src/metric/multiclass_metric.cu b/src/metric/multiclass_metric.cu index 8b06c59327b3..84b70707ac55 100644 --- a/src/metric/multiclass_metric.cu +++ b/src/metric/multiclass_metric.cu @@ -10,13 +10,14 @@ #include #include #include // for accumulate +#include #include "../common/math.h" #include "../common/threading_utils.h" #include "metric_common.h" // MetricNoCache #if defined(XGBOOST_USE_CUDA) -#include // thrust::plus<> +#include // thrust::plus<> #include #include @@ -49,33 +50,29 @@ class MultiClassMetricsReduction { const auto& h_weights = weights.HostVector(); const auto& h_preds = preds.HostVector(); - std::atomic label_error {0}; + std::atomic label_error{0}; bool const is_null_weight = weights.Size() == 0; std::vector scores_tloc(n_threads, 0); std::vector weights_tloc(n_threads, 0); common::ParallelFor(ndata, n_threads, [&](size_t idx) { - bst_float weight = is_null_weight ? 1.0f : h_weights[idx]; - auto label = static_cast(h_labels[idx]); - if (label >= 0 && label < static_cast(n_class)) { - auto t_idx = omp_get_thread_num(); - scores_tloc[t_idx] += - EvalRowPolicy::EvalRow(label, h_preds.data() + idx * n_class, - n_class) * - weight; - weights_tloc[t_idx] += weight; - } else { - label_error = label; - } + bst_float weight = is_null_weight ? 1.0f : h_weights[idx]; + auto label = static_cast(h_labels[idx]); + if (label >= 0 && label < static_cast(n_class)) { + auto t_idx = omp_get_thread_num(); + scores_tloc[t_idx] += + EvalRowPolicy::EvalRow(label, h_preds.data() + idx * n_class, n_class) * weight; + weights_tloc[t_idx] += weight; + } else { + label_error = label; + } }); - double residue_sum = - std::accumulate(scores_tloc.cbegin(), scores_tloc.cend(), 0.0); - double weights_sum = - std::accumulate(weights_tloc.cbegin(), weights_tloc.cend(), 0.0); + double residue_sum = std::accumulate(scores_tloc.cbegin(), scores_tloc.cend(), 0.0); + double weights_sum = std::accumulate(weights_tloc.cbegin(), weights_tloc.cend(), 0.0); CheckLabelError(label_error, n_class); - PackedReduceResult res { residue_sum, weights_sum }; + PackedReduceResult res{residue_sum, weights_sum}; return res; } @@ -101,22 +98,19 @@ class MultiClassMetricsReduction { s_label_error[0] = 0; PackedReduceResult result = thrust::transform_reduce( - ctx->CUDACtx()->CTP(), - begin, end, + ctx->CUDACtx()->CTP(), begin, end, [=] XGBOOST_DEVICE(size_t idx) { bst_float weight = is_null_weight ? 1.0f : s_weights[idx]; bst_float residue = 0; auto label = static_cast(s_labels[idx]); if (label >= 0 && label < static_cast(n_class)) { - residue = EvalRowPolicy::EvalRow( - label, &s_preds[idx * n_class], n_class) * weight; + residue = EvalRowPolicy::EvalRow(label, &s_preds[idx * n_class], n_class) * weight; } else { s_label_error[0] = label; } - return PackedReduceResult{ residue, weight }; + return PackedReduceResult{residue, weight}; }, - PackedReduceResult(), - thrust::plus()); + PackedReduceResult(), thrust::plus()); CheckLabelError(s_label_error[0], n_class); return result; @@ -156,9 +150,9 @@ class MultiClassMetricsReduction { * \brief base class of multi-class evaluation * \tparam Derived the name of subclass */ -template +template struct EvalMClassBase : public MetricNoCache { - double Eval(const HostDeviceVector &preds, const MetaInfo &info) override { + double Eval(const HostDeviceVector& preds, const MetaInfo& info) override { if (info.labels.Size() == 0) { CHECK_EQ(preds.Size(), 0); } else { @@ -167,9 +161,8 @@ struct EvalMClassBase : public MetricNoCache { std::array dat{0.0, 0.0}; if (info.labels.Size() != 0) { const size_t nclass = preds.Size() / info.labels.Size(); - CHECK_GE(nclass, 1U) - << "mlogloss and merror are only used for multi-class classification," - << " use logloss for binary classification"; + CHECK_GE(nclass, 1U) << "mlogloss and merror are only used for multi-class classification," + << " use logloss for binary classification"; auto result = reducer_.Reduce(this->ctx_, nclass, info.weights_, *info.labels.Data(), preds); dat[0] = result.Residue(); dat[1] = result.Weights(); @@ -185,41 +178,31 @@ struct EvalMClassBase : public MetricNoCache { * \param pred prediction value of current instance * \param nclass number of class in the prediction */ - XGBOOST_DEVICE static bst_float EvalRow(int label, - const bst_float *pred, - size_t nclass); + XGBOOST_DEVICE static bst_float EvalRow(int label, const bst_float* pred, size_t nclass); /*! * \brief to be overridden by subclass, final transformation * \param esum the sum statistics returned by EvalRow * \param wsum sum of weight */ - inline static double GetFinal(double esum, double wsum) { - return esum / wsum; - } + inline static double GetFinal(double esum, double wsum) { return esum / wsum; } private: MultiClassMetricsReduction reducer_; // used to store error message - const char *error_msg_; + const char* error_msg_; }; /*! \brief match error */ struct EvalMatchError : public EvalMClassBase { - const char* Name() const override { - return "merror"; - } - XGBOOST_DEVICE static bst_float EvalRow(int label, - const bst_float *pred, - size_t nclass) { + const char* Name() const override { return "merror"; } + XGBOOST_DEVICE static bst_float EvalRow(int label, const bst_float* pred, size_t nclass) { return common::FindMaxIndex(pred, pred + nclass) != pred + static_cast(label); } }; /*! \brief match error */ struct EvalMultiLogLoss : public EvalMClassBase { - const char* Name() const override { - return "mlogloss"; - } + const char* Name() const override { return "mlogloss"; } XGBOOST_DEVICE static bst_float EvalRow(int label, const bst_float* pred, size_t /*nclass*/) { const bst_float eps = 1e-16f; auto k = static_cast(label); diff --git a/src/metric/rank_metric.cc b/src/metric/rank_metric.cc index d8f69513c24b..219766e3fd02 100644 --- a/src/metric/rank_metric.cc +++ b/src/metric/rank_metric.cc @@ -6,34 +6,34 @@ #include #include -#include // for stable_sort, copy, fill_n, min, max -#include // for array -#include // for log, sqrt -#include // for less, greater -#include // for operator!=, _Rb_tree_const_iterator -#include // for allocator, unique_ptr, shared_ptr, __shared_... -#include // for accumulate -#include // for operator<<, basic_ostream, ostringstream -#include // for char_traits, operator<, basic_string, to_string -#include // for pair, make_pair -#include // for vector - -#include "../collective/aggregator.h" // for ApplyWithLabels -#include "../common/algorithm.h" // for ArgSort, Sort -#include "../common/linalg_op.h" // for cbegin, cend -#include "../common/optional_weight.h" // for OptionalWeights, MakeOptionalWeights -#include "dmlc/common.h" // for OMPException -#include "metric_common.h" // for MetricNoCache, GPUMetric, PackedReduceResult -#include "xgboost/base.h" // for bst_float, bst_omp_uint, bst_group_t, Args -#include "xgboost/cache.h" // for DMatrixCache -#include "xgboost/context.h" // for Context -#include "xgboost/data.h" // for MetaInfo, DMatrix -#include "xgboost/host_device_vector.h" // for HostDeviceVector -#include "xgboost/json.h" // for Json, FromJson, IsA, ToJson, get, Null, Object -#include "xgboost/linalg.h" // for Tensor, TensorView, Range, VectorView, MakeT... -#include "xgboost/logging.h" // for CHECK, ConsoleLogger, LOG_INFO, CHECK_EQ -#include "xgboost/metric.h" // for MetricReg, XGBOOST_REGISTER_METRIC, Metric -#include "xgboost/string_view.h" // for StringView +#include // for stable_sort, copy, fill_n, min, max +#include // for array +#include // for log, sqrt +#include // for less, greater +#include // for operator!=, _Rb_tree_const_iterator +#include // for allocator, unique_ptr, shared_ptr, __shared_... +#include // for accumulate +#include // for operator<<, basic_ostream, ostringstream +#include // for char_traits, operator<, basic_string, to_string +#include // for pair, make_pair +#include // for vector + +#include "../collective/aggregator.h" // for ApplyWithLabels +#include "../common/algorithm.h" // for ArgSort, Sort +#include "../common/linalg_op.h" // for cbegin, cend +#include "../common/optional_weight.h" // for OptionalWeights, MakeOptionalWeights +#include "dmlc/common.h" // for OMPException +#include "metric_common.h" // for MetricNoCache, GPUMetric, PackedReduceResult +#include "xgboost/base.h" // for bst_float, bst_omp_uint, bst_group_t, Args +#include "xgboost/cache.h" // for DMatrixCache +#include "xgboost/context.h" // for Context +#include "xgboost/data.h" // for MetaInfo, DMatrix +#include "xgboost/host_device_vector.h" // for HostDeviceVector +#include "xgboost/json.h" // for Json, FromJson, IsA, ToJson, get, Null, Object +#include "xgboost/linalg.h" // for Tensor, TensorView, Range, VectorView, MakeT... +#include "xgboost/logging.h" // for CHECK, ConsoleLogger, LOG_INFO, CHECK_EQ +#include "xgboost/metric.h" // for MetricReg, XGBOOST_REGISTER_METRIC, Metric +#include "xgboost/string_view.h" // for StringView namespace { using PredIndPair = std::pair; @@ -63,7 +63,7 @@ struct EvalAMS : public MetricNoCache { const auto ndata = static_cast(info.labels.Size()); PredIndPairContainer rec(ndata); - const auto &h_preds = preds.ConstHostVector(); + const auto& h_preds = preds.ConstHostVector(); common::ParallelFor(ndata, ctx_->Threads(), [&](bst_omp_uint i) { rec[i] = std::make_pair(h_preds[i], i); }); common::Sort(ctx_, rec.begin(), rec.end(), @@ -74,7 +74,7 @@ struct EvalAMS : public MetricNoCache { unsigned thresindex = 0; double s_tp = 0.0, b_fp = 0.0, tams = 0.0; const auto& labels = info.labels.View(DeviceOrd::CPU()); - for (unsigned i = 0; i < static_cast(ndata-1) && i < ntop; ++i) { + for (unsigned i = 0; i < static_cast(ndata - 1) && i < ntop; ++i) { const unsigned ridx = rec[i].second; const bst_float wt = info.GetWeight(ridx); if (labels(ridx) > 0.5f) { @@ -95,13 +95,11 @@ struct EvalAMS : public MetricNoCache { return static_cast(tams); } else { return static_cast( - sqrt(2 * ((s_tp + b_fp + br) * log(1.0 + s_tp/(b_fp + br)) - s_tp))); + sqrt(2 * ((s_tp + b_fp + br) * log(1.0 + s_tp / (b_fp + br)) - s_tp))); } } - [[nodiscard]] const char* Name() const override { - return name_.c_str(); - } + [[nodiscard]] const char* Name() const override { return name_.c_str(); } private: std::string name_; @@ -112,13 +110,12 @@ struct EvalAMS : public MetricNoCache { struct EvalRank : public MetricNoCache, public EvalRankConfig { public: double Eval(const HostDeviceVector& preds, const MetaInfo& info) override { - CHECK_EQ(preds.Size(), info.labels.Size()) - << "label size predict size not match"; + CHECK_EQ(preds.Size(), info.labels.Size()) << "label size predict size not match"; // quick consistency when group is not available std::vector tgptr(2, 0); tgptr[1] = static_cast(preds.Size()); - const auto &gptr = info.group_ptr_.size() == 0 ? tgptr : info.group_ptr_; + const auto& gptr = info.group_ptr_.size() == 0 ? tgptr : info.group_ptr_; CHECK_NE(gptr.size(), 0U) << "must specify group when constructing rank file"; CHECK_EQ(gptr.back(), preds.Size()) @@ -133,7 +130,7 @@ struct EvalRank : public MetricNoCache, public EvalRankConfig { { const auto& labels = info.labels.HostView(); - const auto &h_preds = preds.ConstHostVector(); + const auto& h_preds = preds.ConstHostVector(); dmlc::OMPException exc; #pragma omp parallel num_threads(ctx_->Threads()) @@ -160,16 +157,14 @@ struct EvalRank : public MetricNoCache, public EvalRankConfig { return collective::GlobalRatio(ctx_, info, sum_metric, static_cast(ngroups)); } - [[nodiscard]] const char* Name() const override { - return name.c_str(); - } + [[nodiscard]] const char* Name() const override { return name.c_str(); } protected: explicit EvalRank(const char* name, const char* param) { this->name = ltr::ParseMetricName(name, param, &topn, &minus); } - virtual double EvalGroup(PredIndPairContainer *recptr) const = 0; + virtual double EvalGroup(PredIndPairContainer* recptr) const = 0; }; /*! \brief Cox: Partial likelihood of the Cox proportional hazards model */ @@ -181,12 +176,12 @@ struct EvalCox : public MetricNoCache { using namespace std; // NOLINT(*) const auto ndata = static_cast(info.labels.Size()); - const auto &label_order = info.LabelAbsSort(ctx_); + const auto& label_order = info.LabelAbsSort(ctx_); // pre-compute a sum for the denominator double exp_p_sum = 0; // we use double because we might need the precision with large datasets - const auto &h_preds = preds.ConstHostVector(); + const auto& h_preds = preds.ConstHostVector(); for (omp_ulong i = 0; i < ndata; ++i) { exp_p_sum += h_preds[i]; } @@ -211,21 +206,19 @@ struct EvalCox : public MetricNoCache { } } - return out/num_events; // normalize by the number of events + return out / num_events; // normalize by the number of events } - [[nodiscard]] const char* Name() const override { - return "cox-nloglik"; - } + [[nodiscard]] const char* Name() const override { return "cox-nloglik"; } }; XGBOOST_REGISTER_METRIC(AMS, "ams") -.describe("AMS metric for higgs.") -.set_body([](const char* param) { return new EvalAMS(param); }); + .describe("AMS metric for higgs.") + .set_body([](const char* param) { return new EvalAMS(param); }); XGBOOST_REGISTER_METRIC(Cox, "cox-nloglik") -.describe("Negative log partial likelihood of Cox proportional hazards model.") -.set_body([](const char*) { return new EvalCox(); }); + .describe("Negative log partial likelihood of Cox proportional hazards model.") + .set_body([](const char*) { return new EvalCox(); }); // ranking metrics that requires cache template diff --git a/src/metric/rank_metric.cu b/src/metric/rank_metric.cu index b3e41a5a5b53..da6b20123d09 100644 --- a/src/metric/rank_metric.cu +++ b/src/metric/rank_metric.cu @@ -5,15 +5,15 @@ #include // for make_counting_iterator #include // for reduce -#include // for transform -#include // for size_t -#include // for shared_ptr -#include // for vector - -#include "../common/cuda_context.cuh" // for CUDAContext -#include "../common/device_helpers.cuh" // for MakeTransformIterator -#include "../common/optional_weight.h" // for MakeOptionalWeights -#include "../common/ranking_utils.cuh" // for CalcQueriesDCG, NDCGCache +#include // for transform +#include // for size_t +#include // for shared_ptr +#include // for vector + +#include "../common/cuda_context.cuh" // for CUDAContext +#include "../common/device_helpers.cuh" // for MakeTransformIterator +#include "../common/optional_weight.h" // for MakeOptionalWeights +#include "../common/ranking_utils.cuh" // for CalcQueriesDCG, NDCGCache #include "metric_common.h" #include "rank_metric.h" #include "xgboost/base.h" // for XGBOOST_DEVICE diff --git a/src/metric/survival_metric.cu b/src/metric/survival_metric.cu index e0cc10f5bb09..ca7babb8f5cd 100644 --- a/src/metric/survival_metric.cu +++ b/src/metric/survival_metric.cu @@ -21,7 +21,7 @@ #if defined(XGBOOST_USE_CUDA) #include "../common/cuda_context.cuh" // for CUDAContext -#endif // XGBOOST_USE_CUDA +#endif // XGBOOST_USE_CUDA using AFTParam = xgboost::common::AFTParam; using ProbabilityDistributionType = xgboost::common::ProbabilityDistributionType; @@ -36,9 +36,7 @@ template class ElementWiseSurvivalMetricsReduction { public: ElementWiseSurvivalMetricsReduction() = default; - void Configure(EvalRow policy) { - policy_ = policy; - } + void Configure(EvalRow policy) { policy_ = policy; } [[nodiscard]] PackedReduceResult CpuReduceMetrics( const HostDeviceVector& weights, @@ -57,14 +55,12 @@ class ElementWiseSurvivalMetricsReduction { std::vector weight_tloc(n_threads, 0.0); common::ParallelFor(ndata, n_threads, [&](size_t i) { - const double wt = - h_weights.empty() ? 1.0 : static_cast(h_weights[i]); + const double wt = h_weights.empty() ? 1.0 : static_cast(h_weights[i]); auto t_idx = omp_get_thread_num(); - score_tloc[t_idx] += - policy_.EvalRow(static_cast(h_labels_lower_bound[i]), - static_cast(h_labels_upper_bound[i]), - static_cast(h_preds[i])) * - wt; + score_tloc[t_idx] += policy_.EvalRow(static_cast(h_labels_lower_bound[i]), + static_cast(h_labels_upper_bound[i]), + static_cast(h_preds[i])) * + wt; weight_tloc[t_idx] += wt; }); @@ -145,41 +141,32 @@ class ElementWiseSurvivalMetricsReduction { struct EvalIntervalRegressionAccuracy { void Configure(const Args&) {} - [[nodiscard]] const char* Name() const { - return "interval-regression-accuracy"; - } + [[nodiscard]] const char* Name() const { return "interval-regression-accuracy"; } - XGBOOST_DEVICE double EvalRow( - double label_lower_bound, double label_upper_bound, double log_pred) const { + XGBOOST_DEVICE double EvalRow(double label_lower_bound, double label_upper_bound, + double log_pred) const { const double pred = exp(log_pred); return (pred >= label_lower_bound && pred <= label_upper_bound) ? 1.0 : 0.0; } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } }; /*! \brief Negative log likelihood of Accelerated Failure Time model */ template struct EvalAFTNLogLik { - void Configure(const Args& args) { - param_.UpdateAllowUnknown(args); - } + void Configure(const Args& args) { param_.UpdateAllowUnknown(args); } - [[nodiscard]] const char* Name() const { - return "aft-nloglik"; - } + [[nodiscard]] const char* Name() const { return "aft-nloglik"; } - XGBOOST_DEVICE double EvalRow( - double label_lower_bound, double label_upper_bound, double pred) const { - return AFTLoss::Loss( - label_lower_bound, label_upper_bound, pred, param_.aft_loss_distribution_scale); + XGBOOST_DEVICE double EvalRow(double label_lower_bound, double label_upper_bound, + double pred) const { + return AFTLoss::Loss(label_lower_bound, label_upper_bound, pred, + param_.aft_loss_distribution_scale); } - static double GetFinal(double esum, double wsum) { - return wsum == 0 ? esum : esum / wsum; - } + static double GetFinal(double esum, double wsum) { return wsum == 0 ? esum : esum / wsum; } + private: AFTParam param_; }; @@ -208,9 +195,7 @@ struct EvalEWiseSurvivalBase : public MetricNoCache { return Policy::GetFinal(dat[0], dat[1]); } - [[nodiscard]] const char* Name() const override { - return policy_.Name(); - } + [[nodiscard]] const char* Name() const override { return policy_.Name(); } private: Policy policy_; @@ -221,9 +206,7 @@ struct EvalEWiseSurvivalBase : public MetricNoCache { // This class exists because we want to perform dispatch according to the distribution type at // configuration time, not at prediction time. struct AFTNLogLikDispatcher : public MetricNoCache { - [[nodiscard]] const char* Name() const override { - return "aft-nloglik"; - } + [[nodiscard]] const char* Name() const override { return "aft-nloglik"; } double Eval(const HostDeviceVector& preds, const MetaInfo& info) override { CHECK(metric_) << "AFT metric must be configured first, with distribution type and scale"; @@ -233,17 +216,18 @@ struct AFTNLogLikDispatcher : public MetricNoCache { void Configure(const Args& args) override { param_.UpdateAllowUnknown(args); switch (param_.aft_loss_distribution) { - case common::ProbabilityDistributionType::kNormal: - metric_.reset(new EvalEWiseSurvivalBase>(ctx_)); - break; - case common::ProbabilityDistributionType::kLogistic: - metric_.reset(new EvalEWiseSurvivalBase>(ctx_)); - break; - case common::ProbabilityDistributionType::kExtreme: - metric_.reset(new EvalEWiseSurvivalBase>(ctx_)); - break; - default: - LOG(FATAL) << "Unknown probability distribution"; + case common::ProbabilityDistributionType::kNormal: + metric_.reset(new EvalEWiseSurvivalBase>(ctx_)); + break; + case common::ProbabilityDistributionType::kLogistic: + metric_.reset( + new EvalEWiseSurvivalBase>(ctx_)); + break; + case common::ProbabilityDistributionType::kExtreme: + metric_.reset(new EvalEWiseSurvivalBase>(ctx_)); + break; + default: + LOG(FATAL) << "Unknown probability distribution"; } metric_->Configure(args); } @@ -254,9 +238,7 @@ struct AFTNLogLikDispatcher : public MetricNoCache { out["aft_loss_param"] = ToJson(param_); } - void LoadConfig(const Json& in) override { - FromJson(in["aft_loss_param"], ¶m_); - } + void LoadConfig(const Json& in) override { FromJson(in["aft_loss_param"], ¶m_); } private: AFTParam param_; diff --git a/src/objective/adaptive.cu b/src/objective/adaptive.cu index 2e0a683de9c7..83cfc99f1055 100644 --- a/src/objective/adaptive.cu +++ b/src/objective/adaptive.cu @@ -3,7 +3,9 @@ */ #include -#include // NOLINT +#include +#include // NOLINT +#include #include "../collective/aggregator.h" #include "../common/cuda_context.cuh" // CUDAContext diff --git a/src/objective/adaptive.h b/src/objective/adaptive.h index ef1aaea54ab2..fbb67104ff98 100644 --- a/src/objective/adaptive.h +++ b/src/objective/adaptive.h @@ -54,7 +54,8 @@ inline void UpdateLeafValues(Context const* ctx, std::vector* p_quantiles collective::SafeColl(rc); // convert to 0 for all reduce - std::replace_if(quantiles.begin(), quantiles.end(), [](float q) { return std::isnan(q); }, 0.f); + std::replace_if( + quantiles.begin(), quantiles.end(), [](float q) { return std::isnan(q); }, 0.f); // use the mean value rc = collective::GlobalSum(ctx, info, linalg::MakeVec(quantiles.data(), quantiles.size())); collective::SafeColl(rc); diff --git a/src/objective/aft_obj.cu b/src/objective/aft_obj.cu index f535fa0aecae..910521b9388c 100644 --- a/src/objective/aft_obj.cu +++ b/src/objective/aft_obj.cu @@ -31,9 +31,7 @@ DMLC_REGISTRY_FILE_TAG(aft_obj_gpu); class AFTObj : public ObjFunction { public: - void Configure(Args const& args) override { - param_.UpdateAllowUnknown(args); - } + void Configure(Args const& args) override { param_.UpdateAllowUnknown(args); } ObjInfo Task() const override { return ObjInfo::kSurvival; } @@ -42,27 +40,24 @@ class AFTObj : public ObjFunction { linalg::Matrix* out_gpair, size_t ndata, DeviceOrd device, bool is_null_weight, float aft_loss_distribution_scale) { common::Transform<>::Init( - [=] XGBOOST_DEVICE(size_t _idx, - common::Span _out_gpair, - common::Span _preds, - common::Span _labels_lower_bound, - common::Span _labels_upper_bound, - common::Span _weights) { - const double pred = static_cast(_preds[_idx]); - const double label_lower_bound = static_cast(_labels_lower_bound[_idx]); - const double label_upper_bound = static_cast(_labels_upper_bound[_idx]); - const float grad = static_cast( - AFTLoss::Gradient(label_lower_bound, label_upper_bound, - pred, aft_loss_distribution_scale)); - const float hess = static_cast( - AFTLoss::Hessian(label_lower_bound, label_upper_bound, - pred, aft_loss_distribution_scale)); - const bst_float w = is_null_weight ? 1.0f : _weights[_idx]; - _out_gpair[_idx] = GradientPair(grad * w, hess * w); - }, - common::Range{0, static_cast(ndata)}, this->ctx_->Threads(), device).Eval( - out_gpair->Data(), &preds, &info.labels_lower_bound_, &info.labels_upper_bound_, - &info.weights_); + [=] XGBOOST_DEVICE(size_t _idx, common::Span _out_gpair, + common::Span _preds, + common::Span _labels_lower_bound, + common::Span _labels_upper_bound, + common::Span _weights) { + const double pred = static_cast(_preds[_idx]); + const double label_lower_bound = static_cast(_labels_lower_bound[_idx]); + const double label_upper_bound = static_cast(_labels_upper_bound[_idx]); + const float grad = static_cast(AFTLoss::Gradient( + label_lower_bound, label_upper_bound, pred, aft_loss_distribution_scale)); + const float hess = static_cast(AFTLoss::Hessian( + label_lower_bound, label_upper_bound, pred, aft_loss_distribution_scale)); + const bst_float w = is_null_weight ? 1.0f : _weights[_idx]; + _out_gpair[_idx] = GradientPair(grad * w, hess * w); + }, + common::Range{0, static_cast(ndata)}, this->ctx_->Threads(), device) + .Eval(out_gpair->Data(), &preds, &info.labels_lower_bound_, &info.labels_upper_bound_, + &info.weights_); } void GetGradient(const HostDeviceVector& preds, const MetaInfo& info, int /*iter*/, @@ -77,28 +72,28 @@ class AFTObj : public ObjFunction { const bool is_null_weight = info.weights_.Size() == 0; if (!is_null_weight) { CHECK_EQ(info.weights_.Size(), ndata) - << "Number of weights should be equal to number of data points."; + << "Number of weights should be equal to number of data points."; } switch (param_.aft_loss_distribution) { - case common::ProbabilityDistributionType::kNormal: - GetGradientImpl(preds, info, out_gpair, ndata, device, - is_null_weight, aft_loss_distribution_scale); - break; - case common::ProbabilityDistributionType::kLogistic: - GetGradientImpl(preds, info, out_gpair, ndata, device, + case common::ProbabilityDistributionType::kNormal: + GetGradientImpl(preds, info, out_gpair, ndata, device, is_null_weight, aft_loss_distribution_scale); - break; - case common::ProbabilityDistributionType::kExtreme: - GetGradientImpl(preds, info, out_gpair, ndata, device, - is_null_weight, aft_loss_distribution_scale); - break; - default: - LOG(FATAL) << "Unrecognized distribution"; + break; + case common::ProbabilityDistributionType::kLogistic: + GetGradientImpl(preds, info, out_gpair, ndata, device, + is_null_weight, aft_loss_distribution_scale); + break; + case common::ProbabilityDistributionType::kExtreme: + GetGradientImpl(preds, info, out_gpair, ndata, device, + is_null_weight, aft_loss_distribution_scale); + break; + default: + LOG(FATAL) << "Unrecognized distribution"; } } - void PredTransform(HostDeviceVector *io_preds) const override { + void PredTransform(HostDeviceVector* io_preds) const override { // Trees give us a prediction in log scale, so exponentiate common::Transform<>::Init( [] XGBOOST_DEVICE(size_t _idx, common::Span _preds) { @@ -120,9 +115,7 @@ class AFTObj : public ObjFunction { }); } - const char* DefaultEvalMetric() const override { - return "aft-nloglik"; - } + const char* DefaultEvalMetric() const override { return "aft-nloglik"; } void SaveConfig(Json* p_out) const override { auto& out = *p_out; @@ -130,9 +123,7 @@ class AFTObj : public ObjFunction { out["aft_loss_param"] = ToJson(param_); } - void LoadConfig(Json const& in) override { - FromJson(in["aft_loss_param"], ¶m_); - } + void LoadConfig(Json const& in) override { FromJson(in["aft_loss_param"], ¶m_); } Json DefaultMetricConfig() const override { Json config{Object{}}; config["name"] = String{this->DefaultEvalMetric()}; @@ -145,9 +136,9 @@ class AFTObj : public ObjFunction { }; // register the objective functions -XGBOOST_REGISTER_OBJECTIVE(AFTObj, "survival:aft") - .describe("AFT loss function") - .set_body([]() { return new AFTObj(); }); +XGBOOST_REGISTER_OBJECTIVE(AFTObj, "survival:aft").describe("AFT loss function").set_body([]() { + return new AFTObj(); +}); } // namespace obj } // namespace xgboost diff --git a/src/objective/hinge.cu b/src/objective/hinge.cu index 285f65c6f4f5..140c8c21431d 100644 --- a/src/objective/hinge.cu +++ b/src/objective/hinge.cu @@ -7,6 +7,7 @@ #include // for max #include // for size_t #include // for int32_t +#include #include "../common/common.h" // for Range #include "../common/linalg_op.h" // for ElementWiseKernel diff --git a/src/objective/init_estimation.cc b/src/objective/init_estimation.cc index f94d2f8ba286..1bc033d4d6d2 100644 --- a/src/objective/init_estimation.cc +++ b/src/objective/init_estimation.cc @@ -3,7 +3,7 @@ */ #include "init_estimation.h" -#include // unique_ptr +#include // unique_ptr #include "../common/stats.h" // Mean #include "../tree/fit_stump.h" // FitStump diff --git a/src/objective/objective.cc b/src/objective/objective.cc index 8731394dfc25..66ce6e2083b9 100644 --- a/src/objective/objective.cc +++ b/src/objective/objective.cc @@ -18,14 +18,13 @@ namespace xgboost { // implement factory functions ObjFunction* ObjFunction::Create(const std::string& name, Context const* ctx) { std::string obj_name = name; - auto *e = ::dmlc::Registry< ::xgboost::ObjFunctionReg>::Get()->Find(obj_name); + auto* e = ::dmlc::Registry< ::xgboost::ObjFunctionReg>::Get()->Find(obj_name); if (e == nullptr) { std::stringstream ss; for (const auto& entry : ::dmlc::Registry< ::xgboost::ObjFunctionReg>::List()) { ss << "Objective candidate: " << entry->name << "\n"; } - LOG(FATAL) << "Unknown objective function: `" << name << "`\n" - << ss.str(); + LOG(FATAL) << "Unknown objective function: `" << name << "`\n" << ss.str(); } auto pobj = (e->body)(); pobj->ctx_ = ctx; diff --git a/src/objective/quantile_obj.cu b/src/objective/quantile_obj.cu index c9241bb627f0..4b884b62de52 100644 --- a/src/objective/quantile_obj.cu +++ b/src/objective/quantile_obj.cu @@ -1,10 +1,11 @@ /** * Copyright 2023-2026, XGBoost contributors */ -#include // std::array -#include // std::size_t -#include // std::int32_t -#include // std::vector +#include +#include // std::array +#include // std::size_t +#include // std::int32_t +#include // std::vector #include "../common/linalg_op.h" // ElementWiseKernel,cbegin,cend #include "../common/quantile_loss_utils.h" // QuantileLossParam @@ -20,9 +21,9 @@ #if defined(XGBOOST_USE_CUDA) -#include "../common/stats.cuh" // SegmentedQuantile +#include "../common/stats.cuh" // SegmentedQuantile -#endif // defined(XGBOOST_USE_CUDA) +#endif // defined(XGBOOST_USE_CUDA) namespace xgboost::obj { class QuantileRegression : public ObjFunction { diff --git a/src/objective/regression_loss.h b/src/objective/regression_loss.h index ad1dcc88e6da..d2dd04e6b14c 100644 --- a/src/objective/regression_loss.h +++ b/src/objective/regression_loss.h @@ -131,9 +131,7 @@ class GammaDeviance { } XGBOOST_DEVICE static bool CheckIntercept(float base_score) { return base_score > 0; } - XGBOOST_DEVICE static float FirstOrderGradient(float p, float y) { - return 1.0f - y / p; - } + XGBOOST_DEVICE static float FirstOrderGradient(float p, float y) { return 1.0f - y / p; } XGBOOST_DEVICE static float SecondOrderGradient(float p, float y) { return y / p; } static ObjInfo Info() { return ObjInfo::kRegression; } static const char* Name() { return "reg:gamma"; } diff --git a/src/objective/regression_obj.cu b/src/objective/regression_obj.cu index a7a2994a2544..b153d6f54c5c 100644 --- a/src/objective/regression_obj.cu +++ b/src/objective/regression_obj.cu @@ -9,7 +9,8 @@ #include // for all_of #include #include // for int32_t -#include // for vector +#include +#include // for vector #include "../common/common.h" #include "../common/linalg_op.h" // for ElementWiseKernel @@ -40,7 +41,7 @@ #include "../common/algorithm.cuh" // for AllOf #include "../common/cuda_context.cuh" // for CUDAContext #include "../common/device_helpers.cuh" // for MakeIndexTransformIter -#endif // defined(XGBOOST_USE_CUDA) +#endif // defined(XGBOOST_USE_CUDA) namespace xgboost::obj { namespace { @@ -122,7 +123,7 @@ void ProbToMarginImpl(Context const* ctx, linalg::Vector* base_score, Fn& DMLC_REGISTRY_FILE_TAG(regression_obj_gpu); #endif // defined(XGBOOST_USE_CUDA) -template +template class RegLossObj : public FitInterceptGlmLike { protected: HostDeviceVector additional_input_; @@ -168,15 +169,13 @@ class RegLossObj : public FitInterceptGlmLike { common::Transform<>::Init( [block_size, ndata, n_targets] XGBOOST_DEVICE( size_t data_block_idx, common::Span _additional_input, - common::Span _out_gpair, - common::Span _preds, - common::Span _labels, - common::Span _weights) { + common::Span _out_gpair, common::Span _preds, + common::Span _labels, common::Span _weights) { const bst_float* preds_ptr = _preds.data(); const bst_float* labels_ptr = _labels.data(); const bst_float* weights_ptr = _weights.data(); GradientPair* out_gpair_ptr = _out_gpair.data(); - const size_t begin = data_block_idx*block_size; + const size_t begin = data_block_idx * block_size; const size_t end = std::min(ndata, begin + block_size); const float _scale_pos_weight = _additional_input[0]; const bool _is_null_weight = _additional_input[1]; @@ -193,16 +192,13 @@ class RegLossObj : public FitInterceptGlmLike { } }, common::Range{0, static_cast(n_data_blocks)}, nthreads, device) - .Eval(&additional_input_, out_gpair->Data(), &preds, info.labels.Data(), - &info.weights_); + .Eval(&additional_input_, out_gpair->Data(), &preds, info.labels.Data(), &info.weights_); } public: - [[nodiscard]] const char* DefaultEvalMetric() const override { - return Loss::DefaultEvalMetric(); - } + [[nodiscard]] const char* DefaultEvalMetric() const override { return Loss::DefaultEvalMetric(); } - void PredTransform(HostDeviceVector *io_preds) const override { + void PredTransform(HostDeviceVector* io_preds) const override { common::Transform<>::Init( [] XGBOOST_DEVICE(size_t _idx, common::Span _preds) { _preds[_idx] = Loss::PredTransform(_preds[_idx]); @@ -250,21 +246,22 @@ class RegLossObj : public FitInterceptGlmLike { DMLC_REGISTER_PARAMETER(RegLossParam); XGBOOST_REGISTER_OBJECTIVE(SquaredLossRegression, LinearSquareLoss::Name()) -.describe("Regression with squared error.") -.set_body([]() { return new RegLossObj(); }); + .describe("Regression with squared error.") + .set_body([]() { return new RegLossObj(); }); XGBOOST_REGISTER_OBJECTIVE(LogisticRegression, LogisticRegression::Name()) -.describe("Logistic regression for probability regression task.") -.set_body([]() { return new RegLossObj(); }); + .describe("Logistic regression for probability regression task.") + .set_body([]() { return new RegLossObj(); }); XGBOOST_REGISTER_OBJECTIVE(LogisticClassification, LogisticClassification::Name()) -.describe("Logistic regression for binary classification task.") -.set_body([]() { return new RegLossObj(); }); + .describe("Logistic regression for binary classification task.") + .set_body([]() { return new RegLossObj(); }); XGBOOST_REGISTER_OBJECTIVE(LogisticRaw, LogisticRaw::Name()) -.describe("Logistic regression for classification, output score " - "before logistic transformation.") -.set_body([]() { return new RegLossObj(); }); + .describe( + "Logistic regression for classification, output score " + "before logistic transformation.") + .set_body([]() { return new RegLossObj(); }); XGBOOST_REGISTER_OBJECTIVE(GammaRegression, GammaDeviance::Name()) .describe("Gamma regression using the gamma deviance loss with log link.") @@ -272,10 +269,11 @@ XGBOOST_REGISTER_OBJECTIVE(GammaRegression, GammaDeviance::Name()) // Deprecated functions XGBOOST_REGISTER_OBJECTIVE(LinearRegression, "reg:linear") -.describe("Regression with squared error.") -.set_body([]() { - LOG(WARNING) << "reg:linear is now deprecated in favor of reg:squarederror."; - return new RegLossObj(); }); + .describe("Regression with squared error.") + .set_body([]() { + LOG(WARNING) << "reg:linear is now deprecated in favor of reg:squarederror."; + return new RegLossObj(); + }); // End deprecated class SquaredLogErrorRegression : public FitIntercept { @@ -398,9 +396,12 @@ XGBOOST_REGISTER_OBJECTIVE(PseudoHuberRegression, "reg:pseudohubererror") struct PoissonRegressionParam : public XGBoostParameter { float max_delta_step; DMLC_DECLARE_PARAMETER(PoissonRegressionParam) { - DMLC_DECLARE_FIELD(max_delta_step).set_lower_bound(0.0f).set_default(0.7f) - .describe("Maximum delta step we allow each weight estimation to be." \ - " This parameter is required for possion regression."); + DMLC_DECLARE_FIELD(max_delta_step) + .set_lower_bound(0.0f) + .set_default(0.7f) + .describe( + "Maximum delta step we allow each weight estimation to be." + " This parameter is required for possion regression."); } }; @@ -456,9 +457,7 @@ class PoissonRegression : public FitInterceptGlmLike { void ProbToMargin(linalg::Vector* base_score) const override { ProbToMarginImpl(this->ctx_, base_score, [] XGBOOST_DEVICE(float v) { return std::log(v); }); } - [[nodiscard]] const char* DefaultEvalMetric() const override { - return "poisson-nloglik"; - } + [[nodiscard]] const char* DefaultEvalMetric() const override { return "poisson-nloglik"; } void SaveConfig(Json* p_out) const override { auto& out = *p_out; @@ -466,9 +465,7 @@ class PoissonRegression : public FitInterceptGlmLike { out["poisson_regression_param"] = ToJson(param_); } - void LoadConfig(Json const& in) override { - FromJson(in["poisson_regression_param"], ¶m_); - } + void LoadConfig(Json const& in) override { FromJson(in["poisson_regression_param"], ¶m_); } private: PoissonRegressionParam param_; @@ -478,9 +475,8 @@ class PoissonRegression : public FitInterceptGlmLike { DMLC_REGISTER_PARAMETER(PoissonRegressionParam); XGBOOST_REGISTER_OBJECTIVE(PoissonRegression, "count:poisson") -.describe("Poisson regression for count data.") -.set_body([]() { return new PoissonRegression(); }); - + .describe("Poisson regression for count data.") + .set_body([]() { return new PoissonRegression(); }); // cox regression for survival data (negative values mean they are censored) class CoxRegression : public FitIntercept { @@ -495,9 +491,9 @@ class CoxRegression : public FitIntercept { const auto& preds_h = preds.HostVector(); out_gpair->Reshape(info.num_row_, this->Targets(info)); auto gpair = out_gpair->HostView(); - const std::vector &label_order = info.LabelAbsSort(ctx_); + const std::vector& label_order = info.LabelAbsSort(ctx_); - const omp_ulong ndata = static_cast(preds_h.size()); // NOLINT(*) + const omp_ulong ndata = static_cast(preds_h.size()); // NOLINT(*) const bool is_null_weight = info.weights_.Size() == 0; if (!is_null_weight) { CHECK_EQ(info.weights_.Size(), ndata) @@ -517,7 +513,7 @@ class CoxRegression : public FitIntercept { double last_exp_p = 0.0; double last_abs_y = 0.0; double accumulated_sum = 0; - for (omp_ulong i = 0; i < ndata; ++i) { // NOLINT(*) + for (omp_ulong i = 0; i < ndata; ++i) { // NOLINT(*) const size_t ind = label_order[i]; const double p = preds_h[ind]; const double exp_p = std::exp(p); @@ -532,16 +528,16 @@ class CoxRegression : public FitIntercept { exp_p_sum -= accumulated_sum; accumulated_sum = 0; } else { - CHECK(last_abs_y <= abs_y) << "CoxRegression: labels must be in sorted order, " << - "MetaInfo::LabelArgsort failed!"; + CHECK(last_abs_y <= abs_y) << "CoxRegression: labels must be in sorted order, " + << "MetaInfo::LabelArgsort failed!"; } if (y > 0) { - r_k += 1.0/exp_p_sum; - s_k += 1.0/(exp_p_sum*exp_p_sum); + r_k += 1.0 / exp_p_sum; + s_k += 1.0 / (exp_p_sum * exp_p_sum); } - const double grad = exp_p*r_k - static_cast(y > 0); + const double grad = exp_p * r_k - static_cast(y > 0); const double hess = exp_p * r_k - exp_p * exp_p * s_k; gpair(ind) = GradientPair(grad * w, hess * w); @@ -549,22 +545,18 @@ class CoxRegression : public FitIntercept { last_exp_p = exp_p; } } - void PredTransform(HostDeviceVector *io_preds) const override { - std::vector &preds = io_preds->HostVector(); - const long ndata = static_cast(preds.size()); // NOLINT(*) - common::ParallelFor(ndata, ctx_->Threads(), [&](long j) { // NOLINT(*) + void PredTransform(HostDeviceVector* io_preds) const override { + std::vector& preds = io_preds->HostVector(); + const long ndata = static_cast(preds.size()); // NOLINT(*) + common::ParallelFor(ndata, ctx_->Threads(), [&](long j) { // NOLINT(*) preds[j] = std::exp(preds[j]); }); } - void EvalTransform(HostDeviceVector *io_preds) override { - PredTransform(io_preds); - } + void EvalTransform(HostDeviceVector* io_preds) override { PredTransform(io_preds); } void ProbToMargin(linalg::Vector* base_score) const override { ProbToMarginImpl(this->ctx_, base_score, [] XGBOOST_DEVICE(float v) { return std::log(v); }); } - [[nodiscard]] const char* DefaultEvalMetric() const override { - return "cox-nloglik"; - } + [[nodiscard]] const char* DefaultEvalMetric() const override { return "cox-nloglik"; } void SaveConfig(Json* p_out) const override { auto& out = *p_out; @@ -575,16 +567,18 @@ class CoxRegression : public FitIntercept { // register the objective function XGBOOST_REGISTER_OBJECTIVE(CoxRegression, "survival:cox") -.describe("Cox regression for censored survival data (negative labels are considered censored).") -.set_body([]() { return new CoxRegression(); }); - + .describe( + "Cox regression for censored survival data (negative labels are considered censored).") + .set_body([]() { return new CoxRegression(); }); // declare parameter struct TweedieRegressionParam : public XGBoostParameter { float tweedie_variance_power; DMLC_DECLARE_PARAMETER(TweedieRegressionParam) { - DMLC_DECLARE_FIELD(tweedie_variance_power).set_range(1.0f, 2.0f).set_default(1.5f) - .describe("Tweedie variance power. Must be between in range [1, 2)."); + DMLC_DECLARE_FIELD(tweedie_variance_power) + .set_range(1.0f, 2.0f) + .set_default(1.5f) + .describe("Tweedie variance power. Must be between in range [1, 2)."); } }; @@ -623,18 +617,17 @@ class TweedieRegression : public FitInterceptGlmLike { auto weight = common::MakeOptionalWeights(ctx_->Device(), info.weights_); const float rho = param_.tweedie_variance_power; - linalg::ElementWiseKernel(this->ctx_, labels, - [=] XGBOOST_DEVICE(std::size_t i, std::size_t j) mutable { - auto p = predt(i, j); - auto y = labels(i, j); - auto w = weight[i]; - auto grad = -y * expf((1 - rho) * p) + expf((2 - rho) * p); - auto hess = -y * (1 - rho) * std::exp((1 - rho) * p) + - (2 - rho) * expf((2 - rho) * p); - gpair(i, j) = GradientPair{grad * w, hess * w}; - }); + linalg::ElementWiseKernel( + this->ctx_, labels, [=] XGBOOST_DEVICE(std::size_t i, std::size_t j) mutable { + auto p = predt(i, j); + auto y = labels(i, j); + auto w = weight[i]; + auto grad = -y * expf((1 - rho) * p) + expf((2 - rho) * p); + auto hess = -y * (1 - rho) * std::exp((1 - rho) * p) + (2 - rho) * expf((2 - rho) * p); + gpair(i, j) = GradientPair{grad * w, hess * w}; + }); } - void PredTransform(HostDeviceVector *io_preds) const override { + void PredTransform(HostDeviceVector* io_preds) const override { common::Transform<>::Init( [] XGBOOST_DEVICE(size_t _idx, common::Span _preds) { _preds[_idx] = expf(_preds[_idx]); @@ -647,18 +640,14 @@ class TweedieRegression : public FitInterceptGlmLike { ProbToMarginImpl(this->ctx_, base_score, [] XGBOOST_DEVICE(float v) { return std::log(v); }); } - [[nodiscard]] const char* DefaultEvalMetric() const override { - return metric_.c_str(); - } + [[nodiscard]] const char* DefaultEvalMetric() const override { return metric_.c_str(); } void SaveConfig(Json* p_out) const override { auto& out = *p_out; out["name"] = String("reg:tweedie"); out["tweedie_regression_param"] = ToJson(param_); } - void LoadConfig(Json const& in) override { - FromJson(in["tweedie_regression_param"], ¶m_); - } + void LoadConfig(Json const& in) override { FromJson(in["tweedie_regression_param"], ¶m_); } private: std::string metric_; @@ -669,8 +658,8 @@ class TweedieRegression : public FitInterceptGlmLike { DMLC_REGISTER_PARAMETER(TweedieRegressionParam); XGBOOST_REGISTER_OBJECTIVE(TweedieRegression, "reg:tweedie") -.describe("Tweedie regression for insurance data.") -.set_body([]() { return new TweedieRegression(); }); + .describe("Tweedie regression for insurance data.") + .set_body([]() { return new TweedieRegression(); }); class MeanAbsoluteError : public ObjFunction { public: diff --git a/src/objective/regression_param.h b/src/objective/regression_param.h index 8f5cd7112cc4..9f88ab9f7648 100644 --- a/src/objective/regression_param.h +++ b/src/objective/regression_param.h @@ -15,8 +15,10 @@ struct RegLossParam : public XGBoostParameter { float scale_pos_weight; // declare parameters DMLC_DECLARE_PARAMETER(RegLossParam) { - DMLC_DECLARE_FIELD(scale_pos_weight).set_default(1.0f).set_lower_bound(0.0f) - .describe("Scale the weight of positive examples by this factor"); + DMLC_DECLARE_FIELD(scale_pos_weight) + .set_default(1.0f) + .set_lower_bound(0.0f) + .describe("Scale the weight of positive examples by this factor"); } }; diff --git a/src/predictor/array_tree_layout.h b/src/predictor/array_tree_layout.h index abb80403f839..3a4d3b1ce7fe 100644 --- a/src/predictor/array_tree_layout.h +++ b/src/predictor/array_tree_layout.h @@ -10,8 +10,8 @@ #include #include // for conditional_t -#include "../common/categorical.h" // for IsCat -#include "xgboost/tree_model.h" // for RegTree +#include "../common/categorical.h" // for IsCat +#include "xgboost/tree_model.h" // for RegTree namespace xgboost::predictor { @@ -46,38 +46,39 @@ class ArrayTreeLayout { std::array split_index_; std::array split_cond_; - /* The nodes at tree levels 0, 1, ..., kNumDeepLevels - 1 are unrolled into an array-based structure. - * If the tree has additional levels, this array stores the node indices of the sub-trees at level kNumDeepLevels. - * This is necessary to continue processing nodes that are not eligible for array-based unrolling. - * The number of sub-trees packed into this array is equal to the number of nodes at tree level kNumDeepLevels, - * which is calculated as (1u << kNumDeepLevels) == kNodesCount + 1. + /* The nodes at tree levels 0, 1, ..., kNumDeepLevels - 1 are unrolled into an array-based + * structure. If the tree has additional levels, this array stores the node indices of the + * sub-trees at level kNumDeepLevels. This is necessary to continue processing nodes that are not + * eligible for array-based unrolling. The number of sub-trees packed into this array is equal to + * the number of nodes at tree level kNumDeepLevels, which is calculated as (1u << kNumDeepLevels) + * == kNodesCount + 1. */ // Mapping from array node index to the RegTree node index. std::array nidx_in_tree_; - /** - * @brief Traverse the top levels of original tree and fill internal arrays - * - * @tparam depth the tree level being processing - * - * @param tree the original tree - * @param cats matrix of categorical splits - * @param nidx_array node idx in the array layout - * @param nidx node idx in the original tree - */ + /** + * @brief Traverse the top levels of original tree and fill internal arrays + * + * @tparam depth the tree level being processing + * + * @param tree the original tree + * @param cats matrix of categorical splits + * @param nidx_array node idx in the array layout + * @param nidx node idx in the original tree + */ template void Populate(TreeView const& tree, RegTree::CategoricalSplitMatrix const& cats, bst_node_t nidx_array = 0, bst_node_t nidx = 0) { if constexpr (depth == kNumDeepLevels + 1) { return; } else if constexpr (depth == kNumDeepLevels) { - /* We store the node index in the original tree to ensure continued processing - * for nodes that are not eligible for array layout optimization. - */ - nidx_in_tree_[nidx_array - kNodesCount] = nidx; + /* We store the node index in the original tree to ensure continued processing + * for nodes that are not eligible for array layout optimization. + */ + nidx_in_tree_[nidx_array - kNodesCount] = nidx; } else { if (tree.IsLeaf(nidx)) { - split_index_[nidx_array] = 0; + split_index_[nidx_array] = 0; /* * If the tree is not fully populated, we can reduce transfer costs. @@ -87,7 +88,7 @@ class ArrayTreeLayout { */ if constexpr (any_missing) default_left_[nidx_array] = 0; if constexpr (has_categorical) is_cat_[nidx_array] = 0; - split_cond_[nidx_array] = std::numeric_limits::quiet_NaN(); + split_cond_[nidx_array] = std::numeric_limits::quiet_NaN(); Populate(tree, cats, 2 * nidx_array + 2, nidx); } else { @@ -95,21 +96,21 @@ class ArrayTreeLayout { if constexpr (has_categorical) { is_cat_[nidx_array] = common::IsCat(cats.split_type, nidx); if (is_cat_[nidx_array]) { - cat_segment_[nidx_array] = cats.categories.subspan(cats.node_ptr[nidx].beg, - cats.node_ptr[nidx].size); + cat_segment_[nidx_array] = + cats.categories.subspan(cats.node_ptr[nidx].beg, cats.node_ptr[nidx].size); } } - split_index_[nidx_array] = tree.SplitIndex(nidx); - split_cond_[nidx_array] = tree.SplitCond(nidx); + split_index_[nidx_array] = tree.SplitIndex(nidx); + split_cond_[nidx_array] = tree.SplitCond(nidx); /* * LeftChild is used to determine if a node is a leaf, so it is always a valid value. * However, RightChild can be invalid in some exotic cases. - * A tree with an invalid RightChild can still be correctly processed using classical methods - * if the split conditions are correct. - * However, in an array layout, an invalid RightChild, even if unreachable, can lead to memory corruption. - * A check should be added to prevent this. + * A tree with an invalid RightChild can still be correctly processed using classical + * methods if the split conditions are correct. However, in an array layout, an invalid + * RightChild, even if unreachable, can lead to memory corruption. A check should be added + * to prevent this. */ Populate(tree, cats, 2 * nidx_array + 1, tree.LeftChild(nidx)); bst_node_t right_child = tree.RightChild(nidx); @@ -123,7 +124,7 @@ class ArrayTreeLayout { bool GetDecision(float fvalue, bst_node_t nidx) const { if constexpr (has_categorical) { if (is_cat_[nidx]) { - return common::Decision(cat_segment_[nidx], fvalue); + return common::Decision(cat_segment_[nidx], fvalue); } else { return fvalue < split_cond_[nidx]; } @@ -139,25 +140,17 @@ class ArrayTreeLayout { constexpr static int kMaxNumDeepLevels = 6; static_assert(kNumDeepLevels <= kMaxNumDeepLevels); - ArrayTreeLayout(TreeView const& tree, RegTree::CategoricalSplitMatrix const &cats) { + ArrayTreeLayout(TreeView const& tree, RegTree::CategoricalSplitMatrix const& cats) { Populate(tree, cats); } - const auto& SplitIndex() const { - return split_index_; - } + const auto& SplitIndex() const { return split_index_; } - const auto& SplitCond() const { - return split_cond_; - } + const auto& SplitCond() const { return split_cond_; } - const auto& DefaultLeft() const { - return default_left_; - } + const auto& DefaultLeft() const { return default_left_; } - const auto& NidxInTree() const { - return nidx_in_tree_; - } + const auto& NidxInTree() const { return nidx_in_tree_; } /** * @brief Traverse the top levels of the tree for the entire block_size. diff --git a/src/predictor/cpu_predictor.cc b/src/predictor/cpu_predictor.cc index 7dfa03bbbec1..081810813234 100644 --- a/src/predictor/cpu_predictor.cc +++ b/src/predictor/cpu_predictor.cc @@ -5,7 +5,9 @@ #include // for assert #include // for size_t #include // for uint32_t, int32_t, uint64_t +#include // for numeric_limits #include // for unique_ptr, shared_ptr +#include // for swap #include // for vector #include "../collective/allreduce.h" // for Allreduce diff --git a/src/predictor/gpu_predictor.cu b/src/predictor/gpu_predictor.cu index e6bbc3d0e650..d621917335fe 100644 --- a/src/predictor/gpu_predictor.cu +++ b/src/predictor/gpu_predictor.cu @@ -6,9 +6,15 @@ #include #include +#include // for min, max_element #include // for proclaim_return_type #include // for swap +#include // for numeric_limits #include +#include // for string +#include // for unordered_map +#include // for forward +#include // for vector #include "../collective/allreduce.h" #include "../common/bitfield.h" @@ -724,18 +730,17 @@ class ColumnSplitHelper { SparsePageView data{ctx_, batch, num_features}; auto const grid = static_cast(common::DivRoundUp(num_rows, kBlockThreads)); auto d_tree_groups = d_model.tree_groups; - dh::LaunchKernel {grid, kBlockThreads, shared_memory_bytes, ctx_->CUDACtx()->Stream()}( + dh::LaunchKernel{grid, kBlockThreads, shared_memory_bytes, ctx_->CUDACtx()->Stream()}( MaskBitVectorKernel, data, d_model.Trees(), decision_bits, missing_bits, d_model.tree_begin, d_model.tree_end, num_features, num_nodes, use_shared, std::numeric_limits::quiet_NaN()); AllReduceBitVectors(&decision_storage, &missing_storage); - dh::LaunchKernel {grid, kBlockThreads, 0, ctx_->CUDACtx()->Stream()}( + dh::LaunchKernel{grid, kBlockThreads, 0, ctx_->CUDACtx()->Stream()}( PredictByBitVectorKernel, d_model.Trees(), - out_preds->DeviceSpan().subspan(batch_offset), d_tree_groups, - decision_bits, missing_bits, d_model.tree_begin, d_model.tree_end, num_rows, num_nodes, - num_group); + out_preds->DeviceSpan().subspan(batch_offset), d_tree_groups, decision_bits, missing_bits, + d_model.tree_begin, d_model.tree_end, num_rows, num_nodes, num_group); batch_offset += batch.Size() * num_group; } @@ -858,8 +863,7 @@ class LaunchConfig { } public: - LaunchConfig(Context const* ctx, bst_feature_t n_features) - : ctx_{ctx}, n_features_{n_features} {} + LaunchConfig(Context const* ctx, bst_feature_t n_features) : ctx_{ctx}, n_features_{n_features} {} template void ForEachBatch(DMatrix* p_fmat, Fn&& fn) { @@ -1055,18 +1059,16 @@ class GPUPredictor : public xgboost::Predictor { } } - LaunchPredict(this->ctx_, false, enc::DeviceColumnsView{}, model, - [&](auto&& cfg, auto&& acc) { - using EncAccessor = std::remove_reference_t; - CHECK((std::is_same_v)); - using LoaderImpl = DeviceAdapterLoader; - using Loader = - typename common::GetValueT::template LoaderType; - cfg.template AllocShmem(); - cfg.template LaunchPredictKernel( - m->Value(), missing, n_features, d_model, acc, 0, &out_preds->predictions); - }); + LaunchPredict(this->ctx_, false, enc::DeviceColumnsView{}, model, [&](auto&& cfg, auto&& acc) { + using EncAccessor = std::remove_reference_t; + CHECK((std::is_same_v)); + using LoaderImpl = DeviceAdapterLoader; + using Loader = + typename common::GetValueT::template LoaderType; + cfg.template AllocShmem(); + cfg.template LaunchPredictKernel(m->Value(), missing, n_features, d_model, acc, 0, + &out_preds->predictions); + }); } [[nodiscard]] bool InplacePredict(std::shared_ptr p_m, gbm::GBTreeModel const& model, diff --git a/src/predictor/treeshap.cc b/src/predictor/treeshap.cc index bae297c973a8..45273efa86b0 100644 --- a/src/predictor/treeshap.cc +++ b/src/predictor/treeshap.cc @@ -5,6 +5,7 @@ #include // copy #include // std::uint32_t +#include // for vector #include "../tree/tree_view.h" // for ScalarTreeView #include "predict_fn.h" // GetNextNode diff --git a/src/tree/constraints.cc b/src/tree/constraints.cc index a1aa9dc0570e..96d39137df1d 100644 --- a/src/tree/constraints.cc +++ b/src/tree/constraints.cc @@ -1,14 +1,15 @@ /*! * Copyright 2018-2019 by Contributors */ +#include "constraints.h" + #include #include #include -#include "xgboost/span.h" -#include "xgboost/json.h" -#include "constraints.h" #include "param.h" +#include "xgboost/json.h" +#include "xgboost/span.h" namespace xgboost { void FeatureInteractionConstraintHost::Configure(tree::TrainParam const& param, @@ -33,10 +34,11 @@ void FeatureInteractionConstraintHost::Reset() { std::vector> tmp; try { ParseInteractionConstraint(this->interaction_constraint_str_, &tmp); - } catch (dmlc::Error const &e) { + } catch (dmlc::Error const& e) { LOG(FATAL) << "Failed to parse feature interaction constraint:\n" << this->interaction_constraint_str_ << "\n" - << "With error:\n" << e.what(); + << "With error:\n" + << e.what(); } for (const auto& e : tmp) { interaction_constraints_.emplace_back(e.begin(), e.end()); @@ -55,13 +57,13 @@ void FeatureInteractionConstraintHost::Reset() { splits_.resize(1, std::unordered_set()); } -void FeatureInteractionConstraintHost::SplitImpl( - bst_node_t node_id, bst_feature_t feature_id, bst_node_t left_id, bst_node_t right_id) { +void FeatureInteractionConstraintHost::SplitImpl(bst_node_t node_id, bst_feature_t feature_id, + bst_node_t left_id, bst_node_t right_id) { bst_node_t newsize = std::max(left_id, right_id) + 1; // Record previous splits for child nodes auto feature_splits = splits_[node_id]; // fid history of current node - feature_splits.insert(feature_id); // add feature of current node + feature_splits.insert(feature_id); // add feature of current node splits_.resize(newsize); splits_[left_id] = feature_splits; splits_[right_id] = feature_splits; @@ -77,7 +79,7 @@ void FeatureInteractionConstraintHost::SplitImpl( } // Loop across specified interactions in constraints - for (const auto &constraint : interaction_constraints_) { + for (const auto& constraint : interaction_constraints_) { // flags whether the specified interaction is still relevant bst_uint flag = 1; @@ -86,7 +88,7 @@ void FeatureInteractionConstraintHost::SplitImpl( for (bst_uint checkvar : feature_splits) { if (constraint.count(checkvar) == 0) { flag = 0; - break; // interaction is not relevant due to unmet constraint + break; // interaction is not relevant due to unmet constraint } } diff --git a/src/tree/constraints.cu b/src/tree/constraints.cu index 31960bd3955f..9d4d0094c576 100644 --- a/src/tree/constraints.cu +++ b/src/tree/constraints.cu @@ -6,8 +6,10 @@ #include #include +#include // for distance #include #include +#include #include "../common/cuda_context.cuh" // for CUDAContext #include "../common/device_helpers.cuh" @@ -18,12 +20,10 @@ namespace xgboost { -size_t FeatureInteractionConstraintDevice::Features() const { - return d_sets_ptr_.size() - 1; -} +size_t FeatureInteractionConstraintDevice::Features() const { return d_sets_ptr_.size() - 1; } -void FeatureInteractionConstraintDevice::Configure( - tree::TrainParam const& param, int32_t const n_features) { +void FeatureInteractionConstraintDevice::Configure(tree::TrainParam const& param, + int32_t const n_features) { has_constraint_ = true; if (param.interaction_constraints.length() == 0) { has_constraint_ = false; @@ -38,7 +38,8 @@ void FeatureInteractionConstraintDevice::Configure( } catch (dmlc::Error const& e) { LOG(FATAL) << "Failed to parse feature interaction constraint:\n" << param.interaction_constraints << "\n" - << "With error:\n" << e.what(); + << "With error:\n" + << e.what(); } n_sets_ = h_feature_constraints.size(); @@ -48,7 +49,7 @@ void FeatureInteractionConstraintDevice::Configure( } // --- Initialize allowed features attached to nodes. - int32_t n_nodes { param.MaxNodes() }; + int32_t n_nodes{param.MaxNodes()}; node_constraints_.resize(n_nodes); node_constraints_storage_.resize(n_nodes); for (auto& n : node_constraints_storage_) { @@ -58,8 +59,8 @@ void FeatureInteractionConstraintDevice::Configure( auto span = dh::ToSpan(node_constraints_storage_[i]); node_constraints_[i] = LBitField64(span); } - s_node_constraints_ = common::Span(node_constraints_.data(), - node_constraints_.size()); + s_node_constraints_ = + common::Span(node_constraints_.data(), node_constraints_.size()); // Represent constraints as CSR format, flatten is the value vector, // ptr is row_ptr vector in CSR. @@ -88,7 +89,7 @@ void FeatureInteractionConstraintDevice::Configure( // --- Compute interaction sets attached to each feature. // Use a set to eliminate duplicated entries. - std::vector > h_features_set(n_features); + std::vector> h_features_set(n_features); int32_t cid = 0; for (auto const& constraints : h_feature_constraints) { for (auto const& feat : constraints) { @@ -99,7 +100,7 @@ void FeatureInteractionConstraintDevice::Configure( // Compute device sets. std::vector h_sets; int32_t ptr = 0; - std::vector h_sets_ptr {ptr}; + std::vector h_sets_ptr{ptr}; for (auto const& feature : h_features_set) { for (auto constraint_id : feature) { h_sets.emplace_back(constraint_id); @@ -126,8 +127,8 @@ void FeatureInteractionConstraintDevice::Configure( } FeatureInteractionConstraintDevice::FeatureInteractionConstraintDevice( - tree::TrainParam const& param, int32_t const n_features) : - has_constraint_{true}, n_sets_{0} { + tree::TrainParam const& param, int32_t const n_features) + : has_constraint_{true}, n_sets_{0} { this->Configure(param, n_features); } @@ -137,8 +138,8 @@ void FeatureInteractionConstraintDevice::Reset(Context const* ctx) { } } -__global__ void ClearBuffersKernel( - LBitField64 result_buffer_output, LBitField64 result_buffer_input) { +__global__ void ClearBuffersKernel(LBitField64 result_buffer_output, + LBitField64 result_buffer_input) { auto tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid < result_buffer_output.Capacity()) { result_buffer_output.Clear(tid); @@ -152,16 +153,17 @@ void FeatureInteractionConstraintDevice::ClearBuffers() { CHECK_EQ(output_buffer_bits_.Capacity(), input_buffer_bits_.Capacity()); CHECK_LE(feature_buffer_.Capacity(), output_buffer_bits_.Capacity()); uint32_t constexpr kBlockThreads = 256; - auto const n_grids = static_cast( - common::DivRoundUp(input_buffer_bits_.Capacity(), kBlockThreads)); - dh::LaunchKernel {n_grids, kBlockThreads} ( - ClearBuffersKernel, - output_buffer_bits_, input_buffer_bits_); + auto const n_grids = + static_cast(common::DivRoundUp(input_buffer_bits_.Capacity(), kBlockThreads)); + dh::LaunchKernel{n_grids, kBlockThreads}(ClearBuffersKernel, output_buffer_bits_, + input_buffer_bits_); } common::Span FeatureInteractionConstraintDevice::QueryNode(Context const* ctx, bst_node_t node_id) { - if (!has_constraint_) { return {}; } + if (!has_constraint_) { + return {}; + } CHECK_LT(node_id, s_node_constraints_.size()); ClearBuffers(); @@ -208,14 +210,11 @@ common::Span FeatureInteractionConstraintDevice::Query( CHECK_EQ(input_buffer_bits_.Capacity(), output_buffer_bits_.Capacity()); uint32_t constexpr kBlockThreads = 256; - auto n_grids = static_cast( - common::DivRoundUp(output_buffer_bits_.Capacity(), kBlockThreads)); - dh::LaunchKernel {n_grids, kBlockThreads} ( - SetInputBufferKernel, - feature_list, input_buffer_bits_); - dh::LaunchKernel {n_grids, kBlockThreads} ( - QueryFeatureListKernel, - node_constraints, input_buffer_bits_, output_buffer_bits_); + auto n_grids = + static_cast(common::DivRoundUp(output_buffer_bits_.Capacity(), kBlockThreads)); + dh::LaunchKernel{n_grids, kBlockThreads}(SetInputBufferKernel, feature_list, input_buffer_bits_); + dh::LaunchKernel{n_grids, kBlockThreads}(QueryFeatureListKernel, node_constraints, + input_buffer_bits_, output_buffer_bits_); thrust::counting_iterator begin(0); thrust::counting_iterator end(result_buffer_.size()); @@ -223,17 +222,14 @@ common::Span FeatureInteractionConstraintDevice::Query( LBitField64 local_result_buffer = output_buffer_bits_; thrust::device_ptr const out_end = thrust::copy_if( - thrust::device, - begin, end, - result_buffer_.data(), - [=]__device__(int32_t pos) { + thrust::device, begin, end, result_buffer_.data(), [=] __device__(int32_t pos) { bool res = local_result_buffer.Check(pos); return res; }); size_t const n_available = std::distance(result_buffer_.data(), out_end); - common::Span result = - {s_result_buffer_.data(), s_result_buffer_.data() + n_available}; + common::Span result = {s_result_buffer_.data(), + s_result_buffer_.data() + n_available}; return result; } @@ -242,17 +238,15 @@ common::Span FeatureInteractionConstraintDevice::Query( __global__ void RestoreFeatureListFromSetsKernel( LBitField64 feature_buffer, - bst_feature_t fid, - common::Span feature_interactions, + bst_feature_t fid, common::Span feature_interactions, common::Span feature_interactions_ptr, // of size n interaction set + 1 - common::Span interactions_list, - common::Span interactions_list_ptr) { + common::Span interactions_list, common::Span interactions_list_ptr) { auto const tid_x = threadIdx.x + blockIdx.x * blockDim.x; auto const tid_y = threadIdx.y + blockIdx.y * blockDim.y; // painful mapping: fid -> sets related to it -> features related to sets. auto const beg = interactions_list_ptr[fid]; - auto const end = interactions_list_ptr[fid+1]; + auto const end = interactions_list_ptr[fid + 1]; auto const n_sets = end - beg; if (tid_x < n_sets) { auto const set_id_pos = beg + tid_x; @@ -266,10 +260,8 @@ __global__ void RestoreFeatureListFromSetsKernel( } } -__global__ void InteractionConstraintSplitKernel(LBitField64 feature, - int32_t feature_id, - LBitField64 node, - LBitField64 left, +__global__ void InteractionConstraintSplitKernel(LBitField64 feature, int32_t feature_id, + LBitField64 node, LBitField64 left, LBitField64 right) { auto tid = threadIdx.x + blockDim.x * blockIdx.x; if (tid > node.Capacity()) { @@ -279,7 +271,7 @@ __global__ void InteractionConstraintSplitKernel(LBitField64 feature, node |= feature; // enable constraints from parent - left |= node; + left |= node; right |= node; if (tid == feature_id) { @@ -291,15 +283,15 @@ __global__ void InteractionConstraintSplitKernel(LBitField64 feature, } } -void FeatureInteractionConstraintDevice::Split( - bst_node_t node_id, bst_feature_t feature_id, bst_node_t left_id, bst_node_t right_id) { - if (!has_constraint_) { return; } - CHECK_NE(node_id, left_id) - << " Split node: " << node_id << " and its left child: " - << left_id << " cannot be the same."; - CHECK_NE(node_id, right_id) - << " Split node: " << node_id << " and its right child: " - << right_id << " cannot be the same."; +void FeatureInteractionConstraintDevice::Split(bst_node_t node_id, bst_feature_t feature_id, + bst_node_t left_id, bst_node_t right_id) { + if (!has_constraint_) { + return; + } + CHECK_NE(node_id, left_id) << " Split node: " << node_id << " and its left child: " << left_id + << " cannot be the same."; + CHECK_NE(node_id, right_id) << " Split node: " << node_id << " and its right child: " << right_id + << " cannot be the same."; CHECK_LT(right_id, s_node_constraints_.size()); CHECK_NE(s_node_constraints_.size(), 0); @@ -308,22 +300,15 @@ void FeatureInteractionConstraintDevice::Split( LBitField64 right = s_node_constraints_[right_id]; dim3 const block3(16, 64, 1); - dim3 const grid3(common::DivRoundUp(n_sets_, 16), - common::DivRoundUp(s_fconstraints_.size(), 64)); - dh::LaunchKernel {grid3, block3} ( - RestoreFeatureListFromSetsKernel, - feature_buffer_, feature_id, - s_fconstraints_, s_fconstraints_ptr_, - s_sets_, s_sets_ptr_); + dim3 const grid3(common::DivRoundUp(n_sets_, 16), common::DivRoundUp(s_fconstraints_.size(), 64)); + dh::LaunchKernel{grid3, block3}(RestoreFeatureListFromSetsKernel, feature_buffer_, feature_id, + s_fconstraints_, s_fconstraints_ptr_, s_sets_, s_sets_ptr_); uint32_t constexpr kBlockThreads = 256; auto n_grids = static_cast(common::DivRoundUp(node.Capacity(), kBlockThreads)); - dh::LaunchKernel {n_grids, kBlockThreads} ( - InteractionConstraintSplitKernel, - feature_buffer_, - feature_id, - node, left, right); + dh::LaunchKernel{n_grids, kBlockThreads}(InteractionConstraintSplitKernel, feature_buffer_, + feature_id, node, left, right); // clear the buffer after use thrust::fill_n(dh::CachingThrustPolicy(), feature_buffer_.Data(), feature_buffer_.NumValues(), 0); diff --git a/src/tree/constraints.h b/src/tree/constraints.h index 3789d2a24ddb..5e15d3204732 100644 --- a/src/tree/constraints.h +++ b/src/tree/constraints.h @@ -22,13 +22,13 @@ class FeatureInteractionConstraintHost { // interaction_constraints_[constraint_id] contains a single interaction // constraint, which specifies a group of feature IDs that can interact // with each other - std::vector< std::unordered_set > interaction_constraints_; + std::vector > interaction_constraints_; // int_cont_[nid] contains the set of all feature IDs that are allowed to // be used for a split at node nid - std::vector< std::unordered_set > node_constraints_; + std::vector > node_constraints_; // splits_[nid] contains the set of all feature IDs that have been used for // splits in node nid and its parents - std::vector< std::unordered_set > splits_; + std::vector > splits_; // string passed by user. std::string interaction_constraint_str_; // number of features in DMatrix/Booster @@ -40,8 +40,7 @@ class FeatureInteractionConstraintHost { public: FeatureInteractionConstraintHost() = default; - void Split(int32_t node_id, bst_feature_t feature_id, bst_node_t left_id, - bst_node_t right_id) { + void Split(int32_t node_id, bst_feature_t feature_id, bst_node_t left_id, bst_node_t right_id) { if (!enabled_) { return; } else { @@ -50,7 +49,9 @@ class FeatureInteractionConstraintHost { } bool Query(bst_node_t nid, bst_feature_t fid) const { - if (!enabled_) { return true; } + if (!enabled_) { + return true; + } return node_constraints_.at(nid).find(fid) != node_constraints_.at(nid).cend(); } diff --git a/src/tree/driver.h b/src/tree/driver.h index c3189a70c64f..368ff6f8ba82 100644 --- a/src/tree/driver.h +++ b/src/tree/driver.h @@ -4,8 +4,10 @@ #ifndef XGBOOST_TREE_DRIVER_H_ #define XGBOOST_TREE_DRIVER_H_ #include + #include #include + #include "./param.h" namespace xgboost { @@ -28,9 +30,8 @@ inline bool LossGuide(const ExpandEntryT& lhs, const ExpandEntryT& rhs) { // Drives execution of tree building on device template class Driver { - using ExpandQueue = - std::priority_queue, - std::function>; + using ExpandQueue = std::priority_queue, + std::function>; public: explicit Driver(TrainParam param, std::size_t max_node_batch_size = 256) @@ -47,14 +48,12 @@ class Driver { } } } - void Push(const std::vector &entries) { + void Push(const std::vector& entries) { this->Push(entries.begin(), entries.end()); } void Push(ExpandEntryT const& e) { queue_.push(e); } - bool IsEmpty() { - return queue_.empty(); - } + bool IsEmpty() { return queue_.empty(); } // Can a child of this entry still be expanded? // can be used to avoid extra work diff --git a/src/tree/fit_stump.cc b/src/tree/fit_stump.cc index 144abcbd8131..4c1ec12bd798 100644 --- a/src/tree/fit_stump.cc +++ b/src/tree/fit_stump.cc @@ -22,8 +22,7 @@ namespace xgboost::tree { namespace cpu_impl { void FitStump(Context const* ctx, MetaInfo const& info, - linalg::TensorView gpair, - linalg::VectorView out) { + linalg::TensorView gpair, linalg::VectorView out) { auto n_targets = out.Size(); CHECK_EQ(n_targets, gpair.Shape(1)); linalg::Tensor sum_tloc = diff --git a/src/tree/gpu_hist/evaluate_splits.cu b/src/tree/gpu_hist/evaluate_splits.cu index a4e7a5fe700d..da049d8fdc65 100644 --- a/src/tree/gpu_hist/evaluate_splits.cu +++ b/src/tree/gpu_hist/evaluate_splits.cu @@ -3,6 +3,7 @@ */ #include // for :max #include // for numeric_limits +#include #include "../../collective/allgather.h" #include "../../collective/communicator-inl.h" // for GetWorldSize, GetRank @@ -14,20 +15,18 @@ namespace xgboost::tree { // With constraints -XGBOOST_DEVICE float LossChangeMissing(const GradientPairInt64 &scan, - const GradientPairInt64 &missing, - const GradientPairInt64 &parent_sum, - const GPUTrainingParam ¶m, bst_node_t nidx, - bst_feature_t fidx, - TreeEvaluator::SplitEvaluator evaluator, - bool &missing_left_out, const GradientQuantiser& quantiser) { // NOLINT +XGBOOST_DEVICE float LossChangeMissing( + const GradientPairInt64 &scan, const GradientPairInt64 &missing, + const GradientPairInt64 &parent_sum, const GPUTrainingParam ¶m, bst_node_t nidx, + bst_feature_t fidx, TreeEvaluator::SplitEvaluator evaluator, + bool &missing_left_out, const GradientQuantiser &quantiser) { // NOLINT const auto left_sum = scan + missing; - float missing_left_gain = evaluator.CalcSplitGain( - param, nidx, fidx, quantiser.ToFloatingPoint(left_sum), - quantiser.ToFloatingPoint(parent_sum - left_sum)); - float missing_right_gain = evaluator.CalcSplitGain( - param, nidx, fidx, quantiser.ToFloatingPoint(scan), - quantiser.ToFloatingPoint(parent_sum - scan)); + float missing_left_gain = + evaluator.CalcSplitGain(param, nidx, fidx, quantiser.ToFloatingPoint(left_sum), + quantiser.ToFloatingPoint(parent_sum - left_sum)); + float missing_right_gain = + evaluator.CalcSplitGain(param, nidx, fidx, quantiser.ToFloatingPoint(scan), + quantiser.ToFloatingPoint(parent_sum - scan)); missing_left_out = missing_left_gain > missing_right_gain; return missing_left_out ? missing_left_gain : missing_right_gain; @@ -70,11 +69,13 @@ class EvaluateSplitAgent { SumCallbackOp prefix_op; static float constexpr kNullGain = -std::numeric_limits::infinity(); - __device__ EvaluateSplitAgent( - TempStorage *temp_storage, int fidx, const EvaluateSplitInputs &inputs, - const EvaluateSplitSharedInputs &shared_inputs, - const TreeEvaluator::SplitEvaluator &evaluator) - : temp_storage(temp_storage), nidx(inputs.nidx), fidx(fidx), + __device__ EvaluateSplitAgent(TempStorage *temp_storage, int fidx, + const EvaluateSplitInputs &inputs, + const EvaluateSplitSharedInputs &shared_inputs, + const TreeEvaluator::SplitEvaluator &evaluator) + : temp_storage(temp_storage), + nidx(inputs.nidx), + fidx(fidx), min_fvalue(__ldg(shared_inputs.min_fvalue.data() + fidx)), gidx_begin(__ldg(shared_inputs.feature_segments.data() + fidx)), gidx_end(__ldg(shared_inputs.feature_segments.data() + fidx + 1)), @@ -82,11 +83,10 @@ class EvaluateSplitAgent { node_histogram(inputs.gradient_histogram.data()), rounding(shared_inputs.rounding), parent_sum(dh::LDGIterator(&inputs.parent_sum)[0]), - param(shared_inputs.param), evaluator(evaluator), + param(shared_inputs.param), + evaluator(evaluator), missing(parent_sum - ReduceFeature()) { - static_assert( - kBlockSize == 32, - "This kernel relies on the assumption block_size == warp_size"); + static_assert(kBlockSize == 32, "This kernel relies on the assumption block_size == warp_size"); // There should be no missing value gradients for a dense matrix KERNEL_CHECK(!shared_inputs.is_dense || missing.GetQuantisedHess() == 0); } @@ -359,8 +359,8 @@ void GPUHistEvaluator::LaunchEvaluateSplits( } size_t combined_num_features = max_active_features * d_inputs.size(); - dh::TemporaryArray feature_best_splits( - combined_num_features, DeviceSplitCandidate()); + dh::TemporaryArray feature_best_splits(combined_num_features, + DeviceSplitCandidate()); // One block for each feature uint32_t constexpr kBlockThreads = 32; @@ -447,8 +447,7 @@ void GPUHistEvaluator::EvaluateSplits(Context const *ctx, const std::vector #include // uint32_t, int32_t #include // for proclaim_copyable_arguments -#include // for unique_ptr +#include +#include +#include // for unique_ptr +#include +#include #include "../../collective/aggregator.h" #include "../../common/cuda_context.cuh" // for CUDAContext diff --git a/src/tree/gpu_hist/multi_evaluate_splits.cu b/src/tree/gpu_hist/multi_evaluate_splits.cu index 7a19215f7343..cdf60b706f14 100644 --- a/src/tree/gpu_hist/multi_evaluate_splits.cu +++ b/src/tree/gpu_hist/multi_evaluate_splits.cu @@ -3,12 +3,14 @@ */ #include // for reduce_by_key, reduce +#include // for max #include // for BlockScan #include // for KeyValuePair #include // for WarpReduce #include // for get_sreg_laneid #include // for identity -#include // for vector +#include +#include // for vector #include "../../common/cuda_context.cuh" #include "../tree_view.h" // for MultiTargetTreeView diff --git a/src/tree/gpu_hist/row_partitioner.cu b/src/tree/gpu_hist/row_partitioner.cu index a54e854173f6..79ec915dc87c 100644 --- a/src/tree/gpu_hist/row_partitioner.cu +++ b/src/tree/gpu_hist/row_partitioner.cu @@ -3,6 +3,7 @@ */ #include // for sequence +#include #include // for vector #include "../../common/cuda_context.cuh" // for CUDAContext diff --git a/src/tree/hist/hist_cache.h b/src/tree/hist/hist_cache.h index d70941b0c103..43b1b283e04f 100644 --- a/src/tree/hist/hist_cache.h +++ b/src/tree/hist/hist_cache.h @@ -48,13 +48,13 @@ class BoundedHistCollection { BoundedHistCollection() = default; common::GHistRow operator[](std::size_t idx) { auto offset = node_map_.at(idx); - return common::Span{data_->data(), static_cast(data_->size())}.subspan( - offset, n_total_bins_); + return common::Span{data_->data(), static_cast(data_->size())}.subspan(offset, + n_total_bins_); } common::ConstGHistRow operator[](std::size_t idx) const { auto offset = node_map_.at(idx); - return common::Span{data_->data(), static_cast(data_->size())}.subspan( - offset, n_total_bins_); + return common::Span{data_->data(), static_cast(data_->size())}.subspan(offset, + n_total_bins_); } void Reset(bst_bin_t n_total_bins, std::size_t n_cached_nodes) { n_total_bins_ = n_total_bins; diff --git a/src/tree/hist/histogram.h b/src/tree/hist/histogram.h index 192292c57b7b..64ae54674c47 100644 --- a/src/tree/hist/histogram.h +++ b/src/tree/hist/histogram.h @@ -4,17 +4,17 @@ #ifndef XGBOOST_TREE_HIST_HISTOGRAM_H_ #define XGBOOST_TREE_HIST_HISTOGRAM_H_ -#include // for max -#include // for size_t -#include // for int32_t -#include // for move -#include // for vector +#include // for max +#include // for size_t +#include // for int32_t +#include // for move +#include // for vector #include "../../collective/allreduce.h" // for Allreduce +#include "../../common/cache_manager.h" // for CacheManager #include "../../common/hist_util.h" // for GHistRow, ParallelGHi... #include "../../common/row_set.h" // for RowSetCollection #include "../../common/threading_utils.h" // for ParallelFor2d, Range1d, BlockedSpace2d -#include "../../common/cache_manager.h" // for CacheManager #include "../../data/gradient_index.h" // for GHistIndexMatrix #include "expand_entry.h" // for MultiExpandEntry, CPUExpandEntry #include "hist_cache.h" // for BoundedHistCollection @@ -79,7 +79,7 @@ class HistogramBuilder { common::ParallelFor2d(space, this->n_threads_, [&](size_t nid_in_set, common::Range1d r) { const auto tid = static_cast(omp_get_thread_num()); bst_node_t const nidx = nodes_to_build[nid_in_set]; - auto const& elem = row_set_collection[nidx]; + auto const &elem = row_set_collection[nidx]; auto start_of_row_set = std::min(r.begin(), elem.Size()); auto end_of_row_set = std::min(r.end(), elem.Size()); auto rid_set = common::Span{elem.begin() + start_of_row_set, @@ -227,9 +227,8 @@ class HistogramBuilder { template common::BlockedSpace2d ConstructHistSpace(Partitioner const &partitioners, std::vector const &nodes_to_build, - const GHistIndexMatrix &gidx, - std::size_t l1_size, bst_bin_t max_bin, - bool read_by_column) { + const GHistIndexMatrix &gidx, std::size_t l1_size, + bst_bin_t max_bin, bool read_by_column) { // FIXME(jiamingy): Handle different size of space. Right now we use the maximum // partition size for the buffer, which might not be efficient if partition sizes // has significant variance. @@ -255,12 +254,12 @@ common::BlockedSpace2d ConstructHistSpace(Partitioner const &partitioners, std::size_t space_in_l1_for_rows; if (read_by_column) { - /* In this case, an accurate block_size estimate is performance-critical. - * For column-wise histogram construction, each column is processed over the - * same block of rows. If the block fits in L1, the row data are loaded once - * and reused across all columns; otherwise, the cache must be refilled for - * each column. - */ + /* In this case, an accurate block_size estimate is performance-critical. + * For column-wise histogram construction, each column is processed over the + * same block of rows. If the block fits in L1, the row data are loaded once + * and reused across all columns; otherwise, the cache must be refilled for + * each column. + */ /* First step: determine whether one histogram column fits into L1. * Note: column-wise kernel is used for dense data only. @@ -272,11 +271,11 @@ common::BlockedSpace2d ConstructHistSpace(Partitioner const &partitioners, space_in_l1_for_rows = usable_l1_size - (hist_col_fit_to_l1 ? hist_col_size : 0); } else { /* In this case, block_size is less critical. - * For row-wise histogram construction, columns are processed for each row. - * Rows do not need to remain in L1 across iterations, but choosing a - * reasonable block_size allows the histogram buffer and offsets to stay in L1, - * which gives a small performance benefit. - */ + * For row-wise histogram construction, columns are processed for each row. + * Rows do not need to remain in L1 across iterations, but choosing a + * reasonable block_size allows the histogram buffer and offsets to stay in L1, + * which gives a small performance benefit. + */ /* First step: estimate the size of the histogram and the offsets vector. */ std::size_t n_bins = gidx.cut.Ptrs().back(); @@ -306,10 +305,9 @@ common::BlockedSpace2d ConstructHistSpace(Partitioner const &partitioners, constexpr std::size_t kMinBlockSize = kCacheLineSize / sizeof(GradientPair); block_size = std::max(kMinBlockSize, block_size); - common::BlockedSpace2d space{ - nodes_to_build.size(), [&](size_t nidx_in_set) { - return partition_size[nidx_in_set]; - }, block_size}; + common::BlockedSpace2d space{nodes_to_build.size(), + [&](size_t nidx_in_set) { return partition_size[nidx_in_set]; }, + block_size}; return space; } @@ -328,17 +326,18 @@ class MultiHistogramBuilder { size_t hist_size = 2 * sizeof(double) * nbins; double l3_per_thread = static_cast(cache_manager_.L3Size()) / ctx_->Threads(); - double usable_cache_size = 0.8 * (cache_manager_.L2Size() + l3_per_thread); + double usable_cache_size = 0.8 * (cache_manager_.L2Size() + l3_per_thread); const bool hist_fit_to_l2 = usable_cache_size > hist_size; /* In row-wise histogram construction, each iteration of the outer (row-wise) loop * accesses bins across the entire histogram; the bins are not localized. - * If the histogram is too large to fit in L2 cache, random access becomes a major performance bottleneck. + * If the histogram is too large to fit in L2 cache, random access becomes a major performance + * bottleneck. * * or dense data, using column-wise histogram construction, - * each iteration of the outer (column-wise) loop accesses only a localized portion of the histogram: - * idx_bin = gradient_index(row_id, col_id) + offset[col_id]. - * This improves cache locality, so the column-wise kernel outperforms the row-wise kernel in this case. + * each iteration of the outer (column-wise) loop accesses only a localized portion of the + * histogram: idx_bin = gradient_index(row_id, col_id) + offset[col_id]. This improves cache + * locality, so the column-wise kernel outperforms the row-wise kernel in this case. */ bool read_by_column = !hist_fit_to_l2 && gidx.IsDense(); return read_by_column; @@ -368,8 +367,8 @@ class MultiHistogramBuilder { for (auto const &gidx : p_fmat->GetBatches(ctx_, param)) { bool read_by_column = ReadByColumn(gidx, force_read_by_column); - auto space = ConstructHistSpace(partitioners, nodes, gidx, - cache_manager_.L1Size(), param.max_bin, read_by_column); + auto space = ConstructHistSpace(partitioners, nodes, gidx, cache_manager_.L1Size(), + param.max_bin, read_by_column); for (bst_target_t t{0}; t < n_targets; ++t) { auto t_gpair = gpair.Slice(linalg::All(), t); this->target_builders_[t].BuildHist(page_idx, space, gidx, @@ -410,8 +409,8 @@ class MultiHistogramBuilder { for (auto const &page : p_fmat->GetBatches(ctx_, param)) { bool read_by_column = ReadByColumn(page, force_read_by_column); - auto space = ConstructHistSpace(partitioners, nodes_to_build, page, - cache_manager_.L1Size(), param.max_bin, read_by_column); + auto space = ConstructHistSpace(partitioners, nodes_to_build, page, cache_manager_.L1Size(), + param.max_bin, read_by_column); auto n_targets = gpair.Shape(1); for (bst_target_t t = 0; t < n_targets; ++t) { diff --git a/src/tree/param.cc b/src/tree/param.cc index 1a52d9a8ba62..badb1bee34ca 100644 --- a/src/tree/param.cc +++ b/src/tree/param.cc @@ -1,12 +1,15 @@ /*! * Copyright by Contributors 2019 */ +#include "param.h" + +#include #include -#include +#include #include +#include #include "xgboost/json.h" -#include "param.h" namespace std { std::istream &operator>>(std::istream &is, std::vector &t) { @@ -82,9 +85,8 @@ std::istream &operator>>(std::istream &is, std::vector &t) { } // namespace std namespace xgboost { -void ParseInteractionConstraint( - std::string const &constraint_str, - std::vector> *p_out) { +void ParseInteractionConstraint(std::string const &constraint_str, + std::vector> *p_out) { auto &out = *p_out; auto j_inc = Json::Load({constraint_str.c_str(), constraint_str.size()}); auto const &all = get(j_inc); @@ -97,12 +99,10 @@ void ParseInteractionConstraint( out[i].emplace_back(u); } else if (IsA(v)) { double d = get(v); - CHECK_EQ(std::floor(d), d) - << "Found floating point number in interaction constraints"; + CHECK_EQ(std::floor(d), d) << "Found floating point number in interaction constraints"; out[i].emplace_back(static_cast(d)); } else { - LOG(FATAL) << "Unknown value type for interaction constraint:" - << v.GetValue().TypeStr(); + LOG(FATAL) << "Unknown value type for interaction constraint:" << v.GetValue().TypeStr(); } } } diff --git a/src/tree/param.h b/src/tree/param.h index 1caa3147fe5f..957a85e696ae 100644 --- a/src/tree/param.h +++ b/src/tree/param.h @@ -86,16 +86,14 @@ struct TrainParam : public XGBoostParameter { DMLC_DECLARE_FIELD(min_split_loss) .set_lower_bound(0.0f) .set_default(0.0f) - .describe( - "Minimum loss reduction required to make a further partition."); - DMLC_DECLARE_FIELD(max_depth) + .describe("Minimum loss reduction required to make a further partition."); + DMLC_DECLARE_FIELD(max_depth).set_lower_bound(0).set_default(6).describe( + "Maximum depth of the tree; 0 indicates no limit; a limit is required " + "for depthwise policy"); + DMLC_DECLARE_FIELD(max_leaves) .set_lower_bound(0) - .set_default(6) - .describe( - "Maximum depth of the tree; 0 indicates no limit; a limit is required " - "for depthwise policy"); - DMLC_DECLARE_FIELD(max_leaves).set_lower_bound(0).set_default(0).describe( - "Maximum number of leaves; 0 indicates no limit."); + .set_default(0) + .describe("Maximum number of leaves; 0 indicates no limit."); DMLC_DECLARE_FIELD(max_bin).set_lower_bound(2).set_default(256).describe( "if using histogram-based algorithm, maximum number of bins per feature"); DMLC_DECLARE_FIELD(grow_policy) @@ -124,15 +122,14 @@ struct TrainParam : public XGBoostParameter { .set_lower_bound(0.0f) .set_default(1.0f) .describe("L2 regularization on leaf weight"); - DMLC_DECLARE_FIELD(reg_alpha) - .set_lower_bound(0.0f) - .set_default(0.0f) - .describe("L1 regularization on leaf weight"); + DMLC_DECLARE_FIELD(reg_alpha).set_lower_bound(0.0f).set_default(0.0f).describe( + "L1 regularization on leaf weight"); DMLC_DECLARE_FIELD(max_delta_step) .set_lower_bound(0.0f) .set_default(0.0f) - .describe("Maximum delta step we allow each tree's weight estimate to be. "\ - "If the value is set to 0, it means there is no constraint"); + .describe( + "Maximum delta step we allow each tree's weight estimate to be. " + "If the value is set to 0, it means there is no constraint"); DMLC_DECLARE_FIELD(subsample) .set_range(0.0f, 1.0f) .set_default(1.0f) @@ -165,11 +162,12 @@ struct TrainParam : public XGBoostParameter { .describe("Constraint of variable monotonicity"); DMLC_DECLARE_FIELD(interaction_constraints) .set_default("") - .describe("Constraints for interaction representing permitted interactions." - "The constraints must be specified in the form of a nest list," - "e.g. [[0, 1], [2, 3, 4]], where each inner list is a group of" - "indices of features that are allowed to interact with each other." - "See tutorial for more information"); + .describe( + "Constraints for interaction representing permitted interactions." + "The constraints must be specified in the form of a nest list," + "e.g. [[0, 1], [2, 3, 4]], where each inner list is a group of" + "indices of features that are allowed to interact with each other." + "See tutorial for more information"); // ------ From cpu quantile histogram -------. DMLC_DECLARE_FIELD(sparse_threshold) @@ -214,10 +212,10 @@ struct TrainParam : public XGBoostParameter { // functions for L1 cost template XGBOOST_DEVICE inline static T1 ThresholdL1(T1 w, T2 alpha) { - if (w > + alpha) { + if (w > +alpha) { return w - alpha; } - if (w < - alpha) { + if (w < -alpha) { return w + alpha; } return 0.0; @@ -266,8 +264,7 @@ XGBOOST_DEVICE T CalcGain(TrainingParams const &p, T sum_grad, T sum_hess) { } } -template +template XGBOOST_DEVICE inline T CalcGain(const TrainingParams &p, StatT stat) { return CalcGain(p, stat.GetGrad(), stat.GetHess()); } @@ -310,29 +307,27 @@ inline double CalcGainGivenWeight(TrainParam const &p, struct XGBOOST_ALIGNAS(16) GradStats { using GradType = double; /*! \brief sum gradient statistics */ - GradType sum_grad { 0 }; + GradType sum_grad{0}; /*! \brief sum hessian statistics */ - GradType sum_hess { 0 }; + GradType sum_hess{0}; public: [[nodiscard]] XGBOOST_DEVICE GradType GetGrad() const { return sum_grad; } [[nodiscard]] XGBOOST_DEVICE GradType GetHess() const { return sum_hess; } - friend std::ostream& operator<<(std::ostream& os, GradStats s) { + friend std::ostream &operator<<(std::ostream &os, GradStats s) { os << s.GetGrad() << "/" << s.GetHess(); return os; } XGBOOST_DEVICE GradStats() { - static_assert(sizeof(GradStats) == 16, - "Size of GradStats is not 16 bytes."); + static_assert(sizeof(GradStats) == 16, "Size of GradStats is not 16 bytes."); } template XGBOOST_DEVICE explicit GradStats(const GpairT &sum) : sum_grad(sum.GetGrad()), sum_hess(sum.GetHess()) {} - explicit GradStats(const GradType grad, const GradType hess) - : sum_grad(grad), sum_hess(hess) {} + explicit GradStats(const GradType grad, const GradType hess) : sum_grad(grad), sum_hess(hess) {} /*! * \brief accumulate statistics * \param p the gradient pair @@ -340,16 +335,16 @@ struct XGBOOST_ALIGNAS(16) GradStats { inline void Add(GradientPair p) { this->Add(p.GetGrad(), p.GetHess()); } /*! \brief add statistics to the data */ - inline void Add(const GradStats& b) { + inline void Add(const GradStats &b) { sum_grad += b.sum_grad; sum_hess += b.sum_hess; } /*! \brief same as add, reduce is used in All Reduce */ - inline static void Reduce(GradStats& a, const GradStats& b) { // NOLINT(*) + inline static void Reduce(GradStats &a, const GradStats &b) { // NOLINT(*) a.Add(b); } /*! \brief set current value to a - b */ - inline void SetSubstract(const GradStats& a, const GradStats& b) { + inline void SetSubstract(const GradStats &a, const GradStats &b) { sum_grad = a.sum_grad - b.sum_grad; sum_hess = a.sum_hess - b.sum_hess; } @@ -379,10 +374,10 @@ inline GradStats &CopyStats(GradStats const &src, GradStats *dst) { // NOLINT * \brief statistics that is helpful to store * and represent a split solution for the tree */ -template +template struct SplitEntryContainer { /*! \brief loss change after split this node */ - bst_float loss_chg {0.0f}; + bst_float loss_chg{0.0f}; /*! \brief split index */ bst_feature_t sindex{0}; bst_float split_value{0.0f}; @@ -551,8 +546,8 @@ struct SplitEntryContainer { } /*! \brief same as update, used by AllReduce*/ - inline static void Reduce(SplitEntryContainer &dst, // NOLINT(*) - const SplitEntryContainer &src) { // NOLINT(*) + inline static void Reduce(SplitEntryContainer &dst, // NOLINT(*) + const SplitEntryContainer &src) { // NOLINT(*) dst.Update(src); } }; @@ -570,9 +565,8 @@ using SplitEntry = SplitEntryContainer; * * \param p_out Pointer to output */ -void ParseInteractionConstraint( - std::string const &constraint_str, - std::vector> *p_out); +void ParseInteractionConstraint(std::string const &constraint_str, + std::vector> *p_out); } // namespace xgboost // define string serializer for vector, to get the arguments diff --git a/src/tree/split_evaluator.h b/src/tree/split_evaluator.h index 43e093b31370..36fd245cf43e 100644 --- a/src/tree/split_evaluator.h +++ b/src/tree/split_evaluator.h @@ -1,8 +1,8 @@ /** * Copyright 2018-2023 by Contributors * \file split_evaluator.h - * \brief Used for implementing a loss term specific to decision trees. Useful for custom regularisation. - * \author Henry Gouk + * \brief Used for implementing a loss term specific to decision trees. Useful for custom + * regularisation. \author Henry Gouk */ #ifndef XGBOOST_TREE_SPLIT_EVALUATOR_H_ @@ -79,7 +79,7 @@ class TreeEvaluator { float wright = this->CalcWeight(nidx, param, right); float gain = this->CalcGainGivenWeight(param, left, wleft) + - this->CalcGainGivenWeight(param, right, wright); + this->CalcGainGivenWeight(param, right, wright); if (constraint == 0) { // no constraint @@ -92,7 +92,7 @@ class TreeEvaluator { } template - XGBOOST_DEVICE float CalcWeight(bst_node_t nodeid, const ParamT ¶m, + XGBOOST_DEVICE float CalcWeight(bst_node_t nodeid, const ParamT& param, GradientSumT const& stats) const { float w = ::xgboost::tree::CalcWeight(param, stats); if (!has_constraint) { @@ -138,11 +138,10 @@ class TreeEvaluator { return Divide(common::Sqr(ThresholdL1(stats.GetGrad(), p.reg_alpha)), (stats.GetHess() + p.reg_lambda)); } - return tree::CalcGainGivenWeight(p, stats.GetGrad(), - stats.GetHess(), w); + return tree::CalcGainGivenWeight(p, stats.GetGrad(), stats.GetHess(), w); } template - XGBOOST_DEVICE float CalcGain(bst_node_t nid, ParamT const &p, + XGBOOST_DEVICE float CalcGain(bst_node_t nid, ParamT const& p, GradientSumT const& stats) const { return this->CalcGainGivenWeight(p, stats, this->CalcWeight(nid, p, stats)); } @@ -150,7 +149,8 @@ class TreeEvaluator { public: /* Get a view to the evaluator that can be passed down to device. */ - template auto GetEvaluator() const { + template + auto GetEvaluator() const { if (device_.IsCUDA()) { auto constraints = monotone_.ConstDevicePointer(); return SplitEvaluator{constraints, lower_bounds_.ConstDevicePointer(), @@ -163,8 +163,8 @@ class TreeEvaluator { } template - void AddSplit(bst_node_t nodeid, bst_node_t leftid, bst_node_t rightid, - bst_feature_t f, float left_weight, float right_weight) { + void AddSplit(bst_node_t nodeid, bst_node_t leftid, bst_node_t rightid, bst_feature_t f, + float left_weight, float right_weight) { if (!has_constraint_) { return; } @@ -178,8 +178,7 @@ class TreeEvaluator { } common::Transform<>::Init( - [=] XGBOOST_DEVICE(size_t, common::Span lower, - common::Span upper, + [=] XGBOOST_DEVICE(size_t, common::Span lower, common::Span upper, common::Span monotone) { lower[leftid] = lower[nodeid]; upper[leftid] = upper[nodeid]; diff --git a/src/tree/tree_model.cc b/src/tree/tree_model.cc index f65f462176eb..39d0729f84f1 100644 --- a/src/tree/tree_model.cc +++ b/src/tree/tree_model.cc @@ -8,11 +8,17 @@ #include #include +#include #include #include #include +#include +#include #include +#include #include // for is_floating_point_v +#include +#include #include "../common/categorical.h" // for GetNodeCats #include "../common/common.h" // for EscapeU8 @@ -98,25 +104,19 @@ class TreeGenerator { return result; } - virtual std::string Indicator(TreeView /*tree*/, - int32_t /*nid*/, uint32_t /*depth*/) const { + virtual std::string Indicator(TreeView /*tree*/, int32_t /*nid*/, uint32_t /*depth*/) const { return ""; } virtual std::string Categorical(TreeView, int32_t, uint32_t) const = 0; - virtual std::string Integer(TreeView /*tree*/, - int32_t /*nid*/, uint32_t /*depth*/) const { + virtual std::string Integer(TreeView /*tree*/, int32_t /*nid*/, uint32_t /*depth*/) const { return ""; } - virtual std::string Quantitive(TreeView /*tree*/, - int32_t /*nid*/, uint32_t /*depth*/) const { - return ""; - } - virtual std::string NodeStat(TreeView /*tree*/, int32_t /*nid*/) const { + virtual std::string Quantitive(TreeView /*tree*/, int32_t /*nid*/, uint32_t /*depth*/) const { return ""; } + virtual std::string NodeStat(TreeView /*tree*/, int32_t /*nid*/) const { return ""; } - virtual std::string PlainNode(TreeView /*tree*/, - int32_t /*nid*/, uint32_t /*depth*/) const = 0; + virtual std::string PlainNode(TreeView /*tree*/, int32_t /*nid*/, uint32_t /*depth*/) const = 0; virtual std::string SplitNode(TreeView tree, int32_t nid, uint32_t depth) { auto const split_index = tree.SplitIndex(nid); @@ -124,41 +124,39 @@ class TreeGenerator { auto is_categorical = tree.SplitType(nid) == FeatureType::kCategorical; if (split_index < fmap_.Size()) { auto check_categorical = [&]() { - CHECK(is_categorical) - << fmap_.Name(split_index) - << " in feature map is numerical but tree node is categorical."; + CHECK(is_categorical) << fmap_.Name(split_index) + << " in feature map is numerical but tree node is categorical."; }; auto check_numerical = [&]() { auto is_numerical = !is_categorical; - CHECK(is_numerical) - << fmap_.Name(split_index) - << " in feature map is categorical but tree node is numerical."; + CHECK(is_numerical) << fmap_.Name(split_index) + << " in feature map is categorical but tree node is numerical."; }; switch (fmap_.TypeOf(split_index)) { - case FeatureMap::kCategorical: { - check_categorical(); - result = this->Categorical(tree, nid, depth); - break; - } - case FeatureMap::kIndicator: { - check_numerical(); - result = this->Indicator(tree, nid, depth); - break; - } - case FeatureMap::kInteger: { - check_numerical(); - result = this->Integer(tree, nid, depth); - break; - } - case FeatureMap::kFloat: - case FeatureMap::kQuantitive: { - check_numerical(); - result = this->Quantitive(tree, nid, depth); - break; - } - default: - LOG(FATAL) << "Unknown feature map type."; + case FeatureMap::kCategorical: { + check_categorical(); + result = this->Categorical(tree, nid, depth); + break; + } + case FeatureMap::kIndicator: { + check_numerical(); + result = this->Indicator(tree, nid, depth); + break; + } + case FeatureMap::kInteger: { + check_numerical(); + result = this->Integer(tree, nid, depth); + break; + } + case FeatureMap::kFloat: + case FeatureMap::kQuantitive: { + check_numerical(); + result = this->Quantitive(tree, nid, depth); + break; + } + default: + LOG(FATAL) << "Unknown feature map type."; } } else { if (is_categorical) { @@ -174,21 +172,15 @@ class TreeGenerator { virtual std::string BuildTree(TreeView tree, int32_t nid, uint32_t depth) = 0; public: - TreeGenerator(FeatureMap const& _fmap, bool with_stats) : - fmap_{_fmap}, with_stats_{with_stats} {} + TreeGenerator(FeatureMap const& _fmap, bool with_stats) : fmap_{_fmap}, with_stats_{with_stats} {} virtual ~TreeGenerator() = default; - virtual void BuildTree(TreeView tree) { - ss_ << this->BuildTree(tree, 0, 0); - } + virtual void BuildTree(TreeView tree) { ss_ << this->BuildTree(tree, 0, 0); } - std::string Str() const { - return ss_.str(); - } + std::string Str() const { return ss_.str(); } }; } // namespace xgboost - namespace xgboost { namespace { template @@ -240,12 +232,12 @@ class TextGenerator : public TreeGenerator { static std::string kStatTemplate = ",cover={cover}"; std::string result = SuperT::Match( kLeafTemplate, - {{"{tabs}", SuperT::Tabs(depth)}, - {"{nid}", std::to_string(nid)}, - {"{leaf}", ToStr(tree.LeafValue(nid))}, - {"{stats}", SuperT::with_stats_ ? - SuperT::Match(kStatTemplate, - {{"{cover}", ToStr(tree.SumHess(nid))}}) : ""}}); + {{"{tabs}", SuperT::Tabs(depth)}, + {"{nid}", std::to_string(nid)}, + {"{leaf}", ToStr(tree.LeafValue(nid))}, + {"{stats}", SuperT::with_stats_ + ? SuperT::Match(kStatTemplate, {{"{cover}", ToStr(tree.SumHess(nid))}}) + : ""}}); return result; } @@ -253,27 +245,25 @@ class TextGenerator : public TreeGenerator { static std::string const kIndicatorTemplate = "{nid}:[{fname}] yes={yes},no={no}"; int32_t nyes = tree.DefaultLeft(nid) ? tree.RightChild(nid) : tree.LeftChild(nid); auto split_index = tree.SplitIndex(nid); - std::string result = SuperT::Match( - kIndicatorTemplate, - {{"{nid}", std::to_string(nid)}, - {"{fname}", GetFeatureName(SuperT::fmap_, split_index)}, - {"{yes}", std::to_string(nyes)}, - {"{no}", std::to_string(tree.DefaultChild(nid))}}); + std::string result = + SuperT::Match(kIndicatorTemplate, {{"{nid}", std::to_string(nid)}, + {"{fname}", GetFeatureName(SuperT::fmap_, split_index)}, + {"{yes}", std::to_string(nyes)}, + {"{no}", std::to_string(tree.DefaultChild(nid))}}); return result; } std::string SplitNodeImpl(TreeView tree, bst_node_t nid, std::string const& template_str, std::string cond, uint32_t depth) const { auto split_index = tree.SplitIndex(nid); - std::string const result = SuperT::Match( - template_str, - {{"{tabs}", SuperT::Tabs(depth)}, - {"{nid}", std::to_string(nid)}, - {"{fname}", GetFeatureName(SuperT::fmap_, split_index)}, - {"{cond}", cond}, - {"{left}", std::to_string(tree.LeftChild(nid))}, - {"{right}", std::to_string(tree.RightChild(nid))}, - {"{missing}", std::to_string(tree.DefaultChild(nid))}}); + std::string const result = + SuperT::Match(template_str, {{"{tabs}", SuperT::Tabs(depth)}, + {"{nid}", std::to_string(nid)}, + {"{fname}", GetFeatureName(SuperT::fmap_, split_index)}, + {"{cond}", cond}, + {"{left}", std::to_string(tree.LeftChild(nid))}, + {"{right}", std::to_string(tree.RightChild(nid))}, + {"{missing}", std::to_string(tree.DefaultChild(nid))}}); return result; } @@ -282,11 +272,9 @@ class TextGenerator : public TreeGenerator { "{tabs}{nid}:[{fname}<{cond}] yes={left},no={right},missing={missing}"; auto cond = tree.SplitCond(nid); const bst_float floored = std::floor(cond); - const int32_t integer_threshold - = (floored == cond) ? static_cast(floored) - : static_cast(floored) + 1; - return SplitNodeImpl(tree, nid, kIntegerTemplate, - std::to_string(integer_threshold), depth); + const int32_t integer_threshold = + (floored == cond) ? static_cast(floored) : static_cast(floored) + 1; + return SplitNodeImpl(tree, nid, kIntegerTemplate, std::to_string(integer_threshold), depth); } std::string Quantitive(TreeView tree, int32_t nid, uint32_t depth) const override { @@ -316,8 +304,7 @@ class TextGenerator : public TreeGenerator { static std::string const kStatTemplate = ",gain={loss_chg},cover={sum_hess}"; std::string const result = SuperT::Match( kStatTemplate, - {{"{loss_chg}", ToStr(tree.LossChg(nid))}, - {"{sum_hess}", ToStr(tree.SumHess(nid))}}); + {{"{loss_chg}", ToStr(tree.LossChg(nid))}, {"{sum_hess}", ToStr(tree.SumHess(nid))}}); return result; } @@ -327,19 +314,16 @@ class TextGenerator : public TreeGenerator { } static std::string const kNodeTemplate = "{parent}{stat}\n{left}\n{right}"; auto result = SuperT::Match( - kNodeTemplate, - {{"{parent}", this->SplitNode(tree, nid, depth)}, - {"{stat}", SuperT::with_stats_ ? this->NodeStat(tree, nid) : ""}, - {"{left}", this->BuildTree(tree, tree.LeftChild(nid), depth+1)}, - {"{right}", this->BuildTree(tree, tree.RightChild(nid), depth+1)}}); + kNodeTemplate, {{"{parent}", this->SplitNode(tree, nid, depth)}, + {"{stat}", SuperT::with_stats_ ? this->NodeStat(tree, nid) : ""}, + {"{left}", this->BuildTree(tree, tree.LeftChild(nid), depth + 1)}, + {"{right}", this->BuildTree(tree, tree.RightChild(nid), depth + 1)}}); return result; } void BuildTree(TreeView tree) override { static std::string const& kTreeTemplate = "{nodes}\n"; - auto result = SuperT::Match( - kTreeTemplate, - {{"{nodes}", this->BuildTree(tree, 0, 0)}}); + auto result = SuperT::Match(kTreeTemplate, {{"{nodes}", this->BuildTree(tree, 0, 0)}}); SuperT::ss_ << result; } }; @@ -360,10 +344,8 @@ class JsonGenerator : public TreeGenerator { } std::string LeafNode(TreeView tree, bst_node_t nid, uint32_t) const override { - static std::string const kLeafTemplate = - R"L({ "nodeid": {nid}, "leaf": {leaf} {stat}})L"; - static std::string const kStatTemplate = - R"S(, "cover": {sum_hess} )S"; + static std::string const kLeafTemplate = R"L({ "nodeid": {nid}, "leaf": {leaf} {stat}})L"; + static std::string const kStatTemplate = R"S(, "cover": {sum_hess} )S"; std::string result = SuperT::Match( kLeafTemplate, {{"{nid}", std::to_string(nid)}, @@ -423,15 +405,13 @@ class JsonGenerator : public TreeGenerator { std::string Integer(TreeView tree, int32_t nid, uint32_t depth) const override { auto cond = tree.SplitCond(nid); const bst_float floored = std::floor(cond); - const int32_t integer_threshold - = (floored == cond) ? static_cast(floored) - : static_cast(floored) + 1; + const int32_t integer_threshold = + (floored == cond) ? static_cast(floored) : static_cast(floored) + 1; static std::string const kIntegerTemplate = R"I( "nodeid": {nid}, "depth": {depth}, "split": "{fname}", )I" R"I("split_condition": {cond}, "yes": {left}, "no": {right}, )I" R"I("missing": {missing})I"; - return SplitNodeImpl(tree, nid, kIntegerTemplate, - std::to_string(integer_threshold), depth); + return SplitNodeImpl(tree, nid, kIntegerTemplate, std::to_string(integer_threshold), depth); } std::string Quantitive(TreeView tree, int32_t nid, uint32_t depth) const override { @@ -453,12 +433,9 @@ class JsonGenerator : public TreeGenerator { } std::string NodeStat(TreeView tree, int32_t nid) const override { - static std::string kStatTemplate = - R"S(, "gain": {loss_chg}, "cover": {sum_hess})S"; - auto result = SuperT::Match( - kStatTemplate, - {{"{loss_chg}", ToStr(tree.LossChg(nid))}, - {"{sum_hess}", ToStr(tree.SumHess(nid))}}); + static std::string kStatTemplate = R"S(, "gain": {loss_chg}, "cover": {sum_hess})S"; + auto result = SuperT::Match(kStatTemplate, {{"{loss_chg}", ToStr(tree.LossChg(nid))}, + {"{sum_hess}", ToStr(tree.SumHess(nid))}}); return result; } @@ -467,12 +444,11 @@ class JsonGenerator : public TreeGenerator { static std::string const kSplitNodeTemplate = "{{properties} {stat}, \"children\": [{left}, {right}\n{indent}]}"; auto result = SuperT::Match( - kSplitNodeTemplate, - {{"{properties}", properties}, - {"{stat}", SuperT::with_stats_ ? this->NodeStat(tree, nid) : ""}, - {"{left}", this->BuildTree(tree, tree.LeftChild(nid), depth+1)}, - {"{right}", this->BuildTree(tree, tree.RightChild(nid), depth+1)}, - {"{indent}", this->Indent(depth)}}); + kSplitNodeTemplate, {{"{properties}", properties}, + {"{stat}", SuperT::with_stats_ ? this->NodeStat(tree, nid) : ""}, + {"{left}", this->BuildTree(tree, tree.LeftChild(nid), depth + 1)}, + {"{right}", this->BuildTree(tree, tree.RightChild(nid), depth + 1)}, + {"{indent}", this->Indent(depth)}}); return result; } @@ -495,22 +471,16 @@ struct GraphvizParam : public XGBoostParameter { std::string leaf_node_params; std::string graph_attrs; - DMLC_DECLARE_PARAMETER(GraphvizParam){ - DMLC_DECLARE_FIELD(yes_color) - .set_default("#0000FF") - .describe("Edge color when meets the node condition."); - DMLC_DECLARE_FIELD(no_color) - .set_default("#FF0000") - .describe("Edge color when doesn't meet the node condition."); - DMLC_DECLARE_FIELD(rankdir) - .set_default("TB") - .describe("Passed to graphiz via graph_attr."); + DMLC_DECLARE_PARAMETER(GraphvizParam) { + DMLC_DECLARE_FIELD(yes_color).set_default("#0000FF").describe( + "Edge color when meets the node condition."); + DMLC_DECLARE_FIELD(no_color).set_default("#FF0000").describe( + "Edge color when doesn't meet the node condition."); + DMLC_DECLARE_FIELD(rankdir).set_default("TB").describe("Passed to graphiz via graph_attr."); DMLC_DECLARE_FIELD(condition_node_params) .set_default("") .describe("Conditional node configuration"); - DMLC_DECLARE_FIELD(leaf_node_params) - .set_default("") - .describe("Leaf node configuration"); + DMLC_DECLARE_FIELD(leaf_node_params).set_default("").describe("Leaf node configuration"); DMLC_DECLARE_FIELD(graph_attrs) .set_default("") .describe("Any other extra attributes for graphviz `graph_attr`."); @@ -535,9 +505,8 @@ class GraphvizGenerator : public TreeGenerator { try { dmlc::JSONReader reader(&iss); reader.Read(&kwargs); - } catch(dmlc::Error const& e) { - LOG(FATAL) << "Failed to parse graphviz parameters:\n\t" - << attrs << "\n" + } catch (dmlc::Error const& e) { + LOG(FATAL) << "Failed to parse graphviz parameters:\n\t" << attrs << "\n" << "With error:\n" << e.what(); } @@ -600,11 +569,10 @@ class GraphvizGenerator : public TreeGenerator { branch = std::string{left ? "yes" : "no"} + std::string{is_missing ? ", missing" : ""}; } std::string buffer = - SuperT::Match(kEdgeTemplate, - {{"{nid}", std::to_string(nidx)}, - {"{child}", std::to_string(child)}, - {"{color}", is_missing ? param_.yes_color : param_.no_color}, - {"{branch}", branch}}); + SuperT::Match(kEdgeTemplate, {{"{nid}", std::to_string(nidx)}, + {"{child}", std::to_string(child)}, + {"{color}", is_missing ? param_.yes_color : param_.no_color}, + {"{branch}", branch}}); return buffer; } @@ -645,9 +613,8 @@ class GraphvizGenerator : public TreeGenerator { }; std::string NodeStat(TreeView tree, bst_node_t nidx) const override { - return SuperT::Match("\ngain={gain}\ncover={cover}", - {{"{cover}", ToStr(tree.SumHess(nidx))}, - {"{gain}", ToStr(tree.LossChg(nidx))}}); + return SuperT::Match("\ngain={gain}\ncover={cover}", {{"{cover}", ToStr(tree.SumHess(nidx))}, + {"{gain}", ToStr(tree.LossChg(nidx))}}); } std::string Categorical(TreeView tree, bst_node_t nidx, uint32_t /*depth*/) const override { @@ -694,10 +661,9 @@ class GraphvizGenerator : public TreeGenerator { ? this->Categorical(tree, nidx, depth) : this->PlainNode(tree, nidx, depth); auto result = SuperT::Match( - kNodeTemplate, - {{"{parent}", node}, - {"{left}", this->BuildTree(tree, tree.LeftChild(nidx), depth+1)}, - {"{right}", this->BuildTree(tree, tree.RightChild(nidx), depth+1)}}); + kNodeTemplate, {{"{parent}", node}, + {"{left}", this->BuildTree(tree, tree.LeftChild(nidx), depth + 1)}, + {"{right}", this->BuildTree(tree, tree.RightChild(nidx), depth + 1)}}); return result; } @@ -707,11 +673,9 @@ class GraphvizGenerator : public TreeGenerator { " graph [ rankdir={rankdir} ]\n" "{graph_attrs}\n" "{nodes}}"; - auto result = SuperT::Match( - kTreeTemplate, - {{"{rankdir}", param_.rankdir}, - {"{graph_attrs}", param_.graph_attrs}, - {"{nodes}", this->BuildTree(tree, 0, 0)}}); + auto result = SuperT::Match(kTreeTemplate, {{"{rankdir}", param_.rankdir}, + {"{graph_attrs}", param_.graph_attrs}, + {"{nodes}", this->BuildTree(tree, 0, 0)}}); SuperT::ss_ << result; }; }; @@ -791,7 +755,7 @@ bool RegTree::Equal(const RegTree& b) const { return false; } auto const& self = *this; - bool ret { true }; + bool ret{true}; auto sc_tree = this->HostScView(); auto const& lhs = self.nodes_.ConstHostVector(); auto const& rhs = b.nodes_.ConstHostVector(); @@ -845,17 +809,15 @@ bool RegTree::Equal(const RegTree& b) const { } void RegTree::ExpandNode(bst_node_t nid, unsigned split_index, bst_float split_value, - bool default_left, bst_float base_weight, - bst_float left_leaf_weight, - bst_float right_leaf_weight, bst_float loss_change, - float sum_hess, float left_sum, float right_sum, - bst_node_t leaf_right_child) { + bool default_left, bst_float base_weight, bst_float left_leaf_weight, + bst_float right_leaf_weight, bst_float loss_change, float sum_hess, + float left_sum, float right_sum, bst_node_t leaf_right_child) { CHECK(!IsMultiTarget()); int pleft = this->AllocNode(); int pright = this->AllocNode(); auto& h_nodes = nodes_.HostVector(); - auto &node = h_nodes[nid]; + auto& node = h_nodes[nid]; CHECK(node.IsLeaf()); node.SetLeftChild(pleft); node.SetRightChild(pright); diff --git a/src/tree/updater_colmaker.cc b/src/tree/updater_colmaker.cc index 7d52f917d58a..371ec2005e5c 100644 --- a/src/tree/updater_colmaker.cc +++ b/src/tree/updater_colmaker.cc @@ -6,6 +6,9 @@ */ #include #include +#include +#include +#include #include #include "../collective/communicator-inl.h" // for IsDistributed @@ -57,15 +60,13 @@ struct ColMakerTrainParam : XGBoostParameter { DMLC_REGISTER_PARAMETER(ColMakerTrainParam); /*! \brief column-wise update to construct a tree */ -class ColMaker: public TreeUpdater { +class ColMaker : public TreeUpdater { public: explicit ColMaker(Context const *ctx) : TreeUpdater(ctx) {} - void Configure(const Args &args) override { - colmaker_param_.UpdateAllowUnknown(args); - } + void Configure(const Args &args) override { colmaker_param_.UpdateAllowUnknown(args); } - void LoadConfig(Json const& in) override { - auto const& config = get(in); + void LoadConfig(Json const &in) override { + auto const &config = get(in); FromJson(config.at("colmaker_train_param"), &this->colmaker_param_); } void SaveConfig(Json *p_out) const override { @@ -73,9 +74,7 @@ class ColMaker: public TreeUpdater { out["colmaker_train_param"] = ToJson(colmaker_param_); } - char const* Name() const override { - return "grow_colmaker"; - } + char const *Name() const override { return "grow_colmaker"; } void LazyGetColumnDensity(DMatrix *dmat) { // Finds densities if we don't already have them @@ -90,8 +89,7 @@ class ColMaker: public TreeUpdater { column_densities_.resize(column_size.size()); for (auto i = 0u; i < column_densities_.size(); i++) { size_t nmiss = dmat->Info().num_row_ - column_size[i]; - column_densities_[i] = - 1.0f - (static_cast(nmiss)) / dmat->Info().num_row_; + column_densities_[i] = 1.0f - (static_cast(nmiss)) / dmat->Info().num_row_; } } } @@ -138,7 +136,7 @@ class ColMaker: public TreeUpdater { /*! \brief statistics of data */ GradStats stats; /*! \brief last feature value scanned */ - bst_float last_fvalue { 0 }; + bst_float last_fvalue{0}; /*! \brief current best solution */ SplitEntry best; // constructor @@ -148,9 +146,9 @@ class ColMaker: public TreeUpdater { /*! \brief statics for node entry */ GradStats stats; /*! \brief loss of this node, without split */ - bst_float root_gain { 0.0f }; + bst_float root_gain{0.0f}; /*! \brief weight calculated related to current data */ - bst_float weight { 0.0f }; + bst_float weight{0.0f}; /*! \brief current best solution */ SplitEntry best; // constructor @@ -161,8 +159,8 @@ class ColMaker: public TreeUpdater { public: // constructor explicit Builder(const TrainParam ¶m, const ColMakerTrainParam &colmaker_train_param, - FeatureInteractionConstraintHost _interaction_constraints, - Context const *ctx, const std::vector &column_densities) + FeatureInteractionConstraintHost _interaction_constraints, Context const *ctx, + const std::vector &column_densities) : param_(param), colmaker_train_param_{colmaker_train_param}, ctx_{ctx}, @@ -170,9 +168,7 @@ class ColMaker: public TreeUpdater { interaction_constraints_{std::move(_interaction_constraints)}, column_densities_(column_densities) {} // update one tree, growing - virtual void Update(const std::vector& gpair, - DMatrix* p_fmat, - RegTree* p_tree) { + virtual void Update(const std::vector &gpair, DMatrix *p_fmat, RegTree *p_tree) { std::vector newnodes; this->InitData(gpair, *p_fmat); this->InitNewNode(qexpand_, gpair, *p_fmat, *p_tree); @@ -213,8 +209,7 @@ class ColMaker: public TreeUpdater { protected: // initialize temp data structure - inline void InitData(const std::vector& gpair, - const DMatrix& fmat) { + inline void InitData(const std::vector &gpair, const DMatrix &fmat) { { // setup position position_.resize(gpair.size()); @@ -227,10 +222,10 @@ class ColMaker: public TreeUpdater { // mark subsample if (param_.subsample < 1.0f) { CHECK_EQ(param_.sampling_method, TrainParam::kUniform) - << "Only uniform sampling is supported, " - << "gradient-based sampling is only support by GPU Hist."; + << "Only uniform sampling is supported, " + << "gradient-based sampling is only support by GPU Hist."; std::bernoulli_distribution coin_flip(param_.subsample); - auto& rnd = common::GlobalRandom(); + auto &rnd = common::GlobalRandom(); for (size_t ridx = 0; ridx < position_.size(); ++ridx) { if (gpair[ridx].GetHess() < 0.0f) continue; if (!coin_flip(rnd)) position_[ridx] = ~position_[ridx]; @@ -250,14 +245,16 @@ class ColMaker: public TreeUpdater { // reserve a small space stemp_.clear(); stemp_.resize(this->ctx_->Threads(), std::vector()); - for (auto& i : stemp_) { - i.clear(); i.reserve(256); + for (auto &i : stemp_) { + i.clear(); + i.reserve(256); } snode_.reserve(256); } { // expand query - qexpand_.reserve(256); qexpand_.clear(); + qexpand_.reserve(256); + qexpand_.clear(); qexpand_.push_back(0); } } @@ -266,7 +263,7 @@ class ColMaker: public TreeUpdater { * and NodeEntry for all the new nodes in qexpand */ void InitNewNode(const std::vector &qexpand, const std::vector &gpair, - const DMatrix &fmat, RegTree const& tree) { + const DMatrix &fmat, RegTree const &tree) { auto n_nodes = tree.NumNodes(); auto sc_tree = tree.HostScView(); { @@ -276,7 +273,7 @@ class ColMaker: public TreeUpdater { } snode_.resize(n_nodes, NodeEntry()); } - const MetaInfo& info = fmat.Info(); + const MetaInfo &info = fmat.Info(); // setup position common::ParallelFor(info.num_row_, ctx_->Threads(), [&](auto ridx) { int32_t const tid = omp_get_thread_num(); @@ -286,7 +283,7 @@ class ColMaker: public TreeUpdater { // sum the per thread statistics together for (int nid : qexpand) { GradStats stats; - for (auto& s : stemp_) { + for (auto &s : stemp_) { stats.Add(s[nid].stats); } // update node statistics @@ -318,8 +315,8 @@ class ColMaker: public TreeUpdater { // update enumeration solution inline void UpdateEnumeration( - int nid, GradientPair gstats, bst_float fvalue, int d_step, - bst_uint fid, GradStats &c, std::vector &temp, // NOLINT(*) + int nid, GradientPair gstats, bst_float fvalue, int d_step, bst_uint fid, GradStats &c, + std::vector &temp, // NOLINT(*) TreeEvaluator::SplitEvaluator const &evaluator) const { // get the statistics of nid ThreadEntry &e = temp[nid]; @@ -329,34 +326,27 @@ class ColMaker: public TreeUpdater { e.last_fvalue = fvalue; } else { // try to find a split - if (fvalue != e.last_fvalue && - e.stats.sum_hess >= param_.min_child_weight) { + if (fvalue != e.last_fvalue && e.stats.sum_hess >= param_.min_child_weight) { c.SetSubstract(snode_[nid].stats, e.stats); if (c.sum_hess >= param_.min_child_weight) { - bst_float loss_chg {0}; + bst_float loss_chg{0}; if (d_step == -1) { loss_chg = static_cast( - evaluator.CalcSplitGain(param_, nid, fid, c, e.stats) - - snode_[nid].root_gain); + evaluator.CalcSplitGain(param_, nid, fid, c, e.stats) - snode_[nid].root_gain); bst_float proposed_split = (fvalue + e.last_fvalue) * 0.5f; - if ( proposed_split == fvalue ) { - e.best.Update(loss_chg, fid, e.last_fvalue, - d_step == -1, false, c, e.stats); + if (proposed_split == fvalue) { + e.best.Update(loss_chg, fid, e.last_fvalue, d_step == -1, false, c, e.stats); } else { - e.best.Update(loss_chg, fid, proposed_split, - d_step == -1, false, c, e.stats); + e.best.Update(loss_chg, fid, proposed_split, d_step == -1, false, c, e.stats); } } else { loss_chg = static_cast( - evaluator.CalcSplitGain(param_, nid, fid, e.stats, c) - - snode_[nid].root_gain); + evaluator.CalcSplitGain(param_, nid, fid, e.stats, c) - snode_[nid].root_gain); bst_float proposed_split = (fvalue + e.last_fvalue) * 0.5f; - if ( proposed_split == fvalue ) { - e.best.Update(loss_chg, fid, e.last_fvalue, - d_step == -1, false, e.stats, c); + if (proposed_split == fvalue) { + e.best.Update(loss_chg, fid, e.last_fvalue, d_step == -1, false, e.stats, c); } else { - e.best.Update(loss_chg, fid, proposed_split, - d_step == -1, false, e.stats, c); + e.best.Update(loss_chg, fid, proposed_split, d_step == -1, false, e.stats, c); } } } @@ -367,11 +357,10 @@ class ColMaker: public TreeUpdater { } } // same as EnumerateSplit, with cacheline prefetch optimization - void EnumerateSplit( - const Entry *begin, const Entry *end, int d_step, bst_uint fid, - const std::vector &gpair, - std::vector &temp, // NOLINT(*) - TreeEvaluator::SplitEvaluator const &evaluator) const { + void EnumerateSplit(const Entry *begin, const Entry *end, int d_step, bst_uint fid, + const std::vector &gpair, + std::vector &temp, // NOLINT(*) + TreeEvaluator::SplitEvaluator const &evaluator) const { const std::vector &qexpand = qexpand_; // clear all the temp statistics for (auto nid : qexpand) { @@ -402,10 +391,10 @@ class ColMaker: public TreeUpdater { } for (i = 0, p = it; i < kBuffer; ++i, p += d_step) { const int nid = buf_position[i]; - if (nid < 0 || !interaction_constraints_.Query(nid, fid)) { continue; } - this->UpdateEnumeration(nid, buf_gpair[i], - p->fvalue, d_step, - fid, c, temp, evaluator); + if (nid < 0 || !interaction_constraints_.Query(nid, fid)) { + continue; + } + this->UpdateEnumeration(nid, buf_gpair[i], p->fvalue, d_step, fid, c, temp, evaluator); } } @@ -416,32 +405,27 @@ class ColMaker: public TreeUpdater { } for (it = align_end, i = 0; it != end; ++i, it += d_step) { const int nid = buf_position[i]; - if (nid < 0 || !interaction_constraints_.Query(nid, fid)) { continue; } - this->UpdateEnumeration(nid, buf_gpair[i], - it->fvalue, d_step, - fid, c, temp, evaluator); + if (nid < 0 || !interaction_constraints_.Query(nid, fid)) { + continue; + } + this->UpdateEnumeration(nid, buf_gpair[i], it->fvalue, d_step, fid, c, temp, evaluator); } // finish updating all statistics, check if it is possible to include all sum statistics for (int nid : qexpand) { ThreadEntry &e = temp[nid]; c.SetSubstract(snode_[nid].stats, e.stats); - if (e.stats.sum_hess >= param_.min_child_weight && - c.sum_hess >= param_.min_child_weight) { + if (e.stats.sum_hess >= param_.min_child_weight && c.sum_hess >= param_.min_child_weight) { bst_float loss_chg; const bst_float gap = std::abs(e.last_fvalue) + kRtEps; - const bst_float delta = d_step == +1 ? gap: -gap; + const bst_float delta = d_step == +1 ? gap : -gap; if (d_step == -1) { loss_chg = static_cast( - evaluator.CalcSplitGain(param_, nid, fid, c, e.stats) - - snode_[nid].root_gain); - e.best.Update(loss_chg, fid, e.last_fvalue + delta, d_step == -1, - false, c, e.stats); + evaluator.CalcSplitGain(param_, nid, fid, c, e.stats) - snode_[nid].root_gain); + e.best.Update(loss_chg, fid, e.last_fvalue + delta, d_step == -1, false, c, e.stats); } else { loss_chg = static_cast( - evaluator.CalcSplitGain(param_, nid, fid, e.stats, c) - - snode_[nid].root_gain); - e.best.Update(loss_chg, fid, e.last_fvalue + delta, d_step == -1, - false, e.stats, c); + evaluator.CalcSplitGain(param_, nid, fid, e.stats, c) - snode_[nid].root_gain); + e.best.Update(loss_chg, fid, e.last_fvalue + delta, d_step == -1, false, e.stats, c); } } } @@ -491,17 +475,13 @@ class ColMaker: public TreeUpdater { // now we know the solution in snode[nid], set split if (e.best.loss_chg > kRtEps) { bst_float left_leaf_weight = - evaluator.CalcWeight(nid, param_, e.best.left_sum) * - param_.learning_rate; + evaluator.CalcWeight(nid, param_, e.best.left_sum) * param_.learning_rate; bst_float right_leaf_weight = - evaluator.CalcWeight(nid, param_, e.best.right_sum) * - param_.learning_rate; - p_tree->ExpandNode(nid, e.best.SplitIndex(), e.best.split_value, - e.best.DefaultLeft(), e.weight, left_leaf_weight, - right_leaf_weight, e.best.loss_chg, - e.stats.sum_hess, - e.best.left_sum.GetHess(), e.best.right_sum.GetHess(), - 0); + evaluator.CalcWeight(nid, param_, e.best.right_sum) * param_.learning_rate; + p_tree->ExpandNode(nid, e.best.SplitIndex(), e.best.split_value, e.best.DefaultLeft(), + e.weight, left_leaf_weight, right_leaf_weight, e.best.loss_chg, + e.stats.sum_hess, e.best.left_sum.GetHess(), + e.best.right_sum.GetHess(), 0); } else { (*p_tree)[nid].SetLeaf(e.weight * param_.learning_rate); } @@ -586,15 +566,15 @@ class ColMaker: public TreeUpdater { position_[ridx] = SamplePosition::Encode(nidx, !is_invalid); } // --data fields-- - const TrainParam& param_; - const ColMakerTrainParam& colmaker_train_param_; + const TrainParam ¶m_; + const ColMakerTrainParam &colmaker_train_param_; // number of omp thread used during training - Context const* ctx_; + Context const *ctx_; std::shared_ptr column_sampler_; // Instance Data: current node position in the tree of each instance std::vector position_; // PerThread x PerTreeNode: statistics for per thread construction - std::vector< std::vector > stemp_; + std::vector> stemp_; /*! \brief TreeNode Data: statistics for each constructed node */ std::vector snode_; /*! \brief queue of nodes to be expanded */ diff --git a/src/tree/updater_prune.cc b/src/tree/updater_prune.cc index 2e95885b88d7..10cca854e3fa 100644 --- a/src/tree/updater_prune.cc +++ b/src/tree/updater_prune.cc @@ -7,6 +7,7 @@ #include #include +#include #include "../common/timer.h" #include "./param.h" @@ -55,12 +56,11 @@ class TreePruner : public TreeUpdater { } bst_node_t pid = tree[nid].Parent(); CHECK(!tree[pid].IsLeaf()); - RTreeNodeStat const &s = tree.Stat(pid); + RTreeNodeStat const& s = tree.Stat(pid); // Only prune when both child are leaf. auto left = tree[pid].LeftChild(); auto right = tree[pid].RightChild(); - bool balanced = tree[left].IsLeaf() && - right != RegTree::kInvalidNodeId && tree[right].IsLeaf(); + bool balanced = tree[left].IsLeaf() && right != RegTree::kInvalidNodeId && tree[right].IsLeaf(); if (balanced && param->NeedPrune(s.loss_chg, depth)) { // need to be pruned tree.ChangeToLeaf(pid, param->learning_rate * s.base_weight); @@ -79,8 +79,7 @@ class TreePruner : public TreeUpdater { npruned = this->TryPruneLeaf(param, p_tree, nid, tree.GetDepth(nid), npruned); } } - LOG(INFO) << "tree pruning end, " - << tree.NumExtraNodes() << " extra nodes, " << npruned + LOG(INFO) << "tree pruning end, " << tree.NumExtraNodes() << " extra nodes, " << npruned << " pruned nodes, max_depth=" << tree.MaxDepth(); } diff --git a/src/tree/updater_quantile_hist.cc b/src/tree/updater_quantile_hist.cc index 933deea2b3ef..82cf0d0a6f16 100644 --- a/src/tree/updater_quantile_hist.cc +++ b/src/tree/updater_quantile_hist.cc @@ -322,7 +322,7 @@ class MultiTargetHistBuilder { * gradient during tree building. This function replaces those weights with new weights * calculated from value gradient. */ - void ExpandTreeLeaf(linalg::Matrix const& full_grad, RegTree *p_tree) { + void ExpandTreeLeaf(linalg::Matrix const &full_grad, RegTree *p_tree) { auto tree = p_tree->HostMtView(); auto n_targets = p_tree->NumTargets(); auto value_gpair = full_grad.HostView(); diff --git a/src/tree/updater_refresh.cc b/src/tree/updater_refresh.cc index 62639e3542a3..1c12c5e68dba 100644 --- a/src/tree/updater_refresh.cc +++ b/src/tree/updater_refresh.cc @@ -41,7 +41,7 @@ class TreeRefresher : public TreeUpdater { CHECK_EQ(gpair->Shape(1), 1) << MTNotImplemented(); const std::vector &gpair_h = gpair->Data()->ConstHostVector(); // Thread local variables. - std::vector > stemp; + std::vector> stemp; std::vector fvec_temp; // setup temp space for each thread const int nthread = ctx_->Threads(); @@ -125,8 +125,7 @@ class TreeRefresher : public TreeUpdater { } void Refresh(TrainParam const *param, const GradStats *gstats, int nid, RegTree *p_tree) { RegTree &tree = *p_tree; - tree.Stat(nid).base_weight = - static_cast(CalcWeight(*param, gstats[nid])); + tree.Stat(nid).base_weight = static_cast(CalcWeight(*param, gstats[nid])); tree.Stat(nid).sum_hess = static_cast(gstats[nid].sum_hess); if (tree[nid].IsLeaf()) { if (param->refresh_leaf) { diff --git a/tests/cpp/c_api/test_c_api.cc b/tests/cpp/c_api/test_c_api.cc index 2a3b01432a62..2211f51c373f 100644 --- a/tests/cpp/c_api/test_c_api.cc +++ b/tests/cpp/c_api/test_c_api.cc @@ -13,7 +13,8 @@ #include // std::size_t #include // std::filesystem #include // std::numeric_limits -#include // std::string +#include +#include // std::string #include #include "../../../src/c_api/c_api_error.h" @@ -37,9 +38,8 @@ TEST(CAPI, XGDMatrixCreateFromMatOmp) { data[i] = std::numeric_limits::quiet_NaN(); } - XGDMatrixCreateFromMat_omp(data.data(), row, num_cols, - std::numeric_limits::quiet_NaN(), &handle, - 0); + XGDMatrixCreateFromMat_omp(data.data(), row, num_cols, std::numeric_limits::quiet_NaN(), + &handle, 0); std::shared_ptr *dmat = static_cast *>(handle); @@ -64,7 +64,7 @@ TEST(CAPI, XGDMatrixCreateFromMatOmp) { namespace xgboost { TEST(CAPI, Version) { - int patch {0}; + int patch{0}; XGBoostVersion(NULL, NULL, &patch); // NOLINT ASSERT_EQ(patch, XGBOOST_VER_PATCH); } @@ -108,7 +108,7 @@ TEST(CAPI, XGDMatrixCreateFromCSR) { TEST(CAPI, ConfigIO) { size_t constexpr kRows = 10; auto p_dmat = RandomDataGenerator(kRows, 10, 0).GenerateDMatrix(); - std::vector> mat {p_dmat}; + std::vector> mat{p_dmat}; std::vector labels(kRows); for (size_t i = 0; i < labels.size(); ++i) { labels[i] = i; @@ -116,21 +116,21 @@ TEST(CAPI, ConfigIO) { p_dmat->Info().labels.Data()->HostVector() = labels; p_dmat->Info().labels.Reshape(kRows); - std::shared_ptr learner { Learner::Create(mat) }; + std::shared_ptr learner{Learner::Create(mat)}; BoosterHandle handle = learner.get(); learner->UpdateOneIter(0, p_dmat); - std::array out; - bst_ulong len {0}; + std::array out; + bst_ulong len{0}; XGBoosterSaveJsonConfig(handle, &len, out.data()); - std::string config_str_0 { out[0] }; + std::string config_str_0{out[0]}; auto config_0 = Json::Load({config_str_0.c_str(), config_str_0.size()}); XGBoosterLoadJsonConfig(handle, out[0]); - bst_ulong len_1 {0}; - std::string config_str_1 { out[0] }; + bst_ulong len_1{0}; + std::string config_str_1{out[0]}; XGBoosterSaveJsonConfig(handle, &len_1, out.data()); auto config_1 = Json::Load({config_str_1.c_str(), config_str_1.size()}); @@ -143,7 +143,7 @@ TEST(CAPI, JsonModelIO) { auto tempdir = std::filesystem::temp_directory_path(); auto p_dmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatrix(); - std::vector> mat {p_dmat}; + std::vector> mat{p_dmat}; std::vector labels(kRows); for (size_t i = 0; i < labels.size(); ++i) { labels[i] = i; @@ -151,7 +151,7 @@ TEST(CAPI, JsonModelIO) { p_dmat->Info().labels.Data()->HostVector() = labels; p_dmat->Info().labels.Reshape(kRows); - std::shared_ptr learner { Learner::Create(mat) }; + std::shared_ptr learner{Learner::Create(mat)}; learner->UpdateOneIter(0, p_dmat); BoosterHandle handle = learner.get(); @@ -160,7 +160,7 @@ TEST(CAPI, JsonModelIO) { XGBoosterSaveModel(handle, modelfile_0.u8string().c_str()); XGBoosterLoadModel(handle, modelfile_0.u8string().c_str()); - bst_ulong num_feature {0}; + bst_ulong num_feature{0}; ASSERT_EQ(XGBoosterGetNumFeature(handle, &num_feature), 0); ASSERT_EQ(num_feature, kCols); @@ -204,7 +204,7 @@ TEST(CAPI, JsonModelIO) { TEST(CAPI, CatchDMLCError) { DMatrixHandle out; ASSERT_EQ(XGDMatrixCreateFromFile("foo", 0, &out), -1); - EXPECT_THROW({ dmlc::Stream::Create("foo", "r"); }, dmlc::Error); + EXPECT_THROW({ dmlc::Stream::Create("foo", "r"); }, dmlc::Error); } TEST(CAPI, CatchDMLCErrorURI) { @@ -215,7 +215,7 @@ TEST(CAPI, CatchDMLCErrorURI) { Json::Dump(config, &config_str); DMatrixHandle out; ASSERT_EQ(XGDMatrixCreateFromURI(config_str.c_str(), &out), -1); - EXPECT_THROW({ dmlc::Stream::Create("foo", "r"); }, dmlc::Error); + EXPECT_THROW({ dmlc::Stream::Create("foo", "r"); }, dmlc::Error); } TEST(CAPI, DMatrixSetFeatureName) { @@ -225,24 +225,21 @@ TEST(CAPI, DMatrixSetFeatureName) { DMatrixHandle handle; std::vector data(kCols * kRows, 1.5); - XGDMatrixCreateFromMat_omp(data.data(), kRows, kCols, - std::numeric_limits::quiet_NaN(), &handle, - 0); + XGDMatrixCreateFromMat_omp(data.data(), kRows, kCols, std::numeric_limits::quiet_NaN(), + &handle, 0); std::vector feature_names; for (bst_feature_t i = 0; i < kCols; ++i) { feature_names.emplace_back(std::to_string(i)); } - std::vector c_feature_names; + std::vector c_feature_names; c_feature_names.resize(feature_names.size()); - std::transform(feature_names.cbegin(), feature_names.cend(), - c_feature_names.begin(), + std::transform(feature_names.cbegin(), feature_names.cend(), c_feature_names.begin(), [](auto const &str) { return str.c_str(); }); XGDMatrixSetStrFeatureInfo(handle, u8"feature_name", c_feature_names.data(), c_feature_names.size()); bst_ulong out_len = 0; char const **c_out_features; - XGDMatrixGetStrFeatureInfo(handle, u8"feature_name", &out_len, - &c_out_features); + XGDMatrixGetStrFeatureInfo(handle, u8"feature_name", &out_len, &c_out_features); CHECK_EQ(out_len, kCols); std::vector out_features; @@ -254,8 +251,7 @@ TEST(CAPI, DMatrixSetFeatureName) { static_assert(sizeof(feat_types) / sizeof(feat_types[0]) == kCols); XGDMatrixSetStrFeatureInfo(handle, "feature_type", feat_types.data(), kCols); char const **c_out_types; - XGDMatrixGetStrFeatureInfo(handle, u8"feature_type", &out_len, - &c_out_types); + XGDMatrixGetStrFeatureInfo(handle, u8"feature_type", &out_len, &c_out_types); for (bst_ulong i = 0; i < out_len; ++i) { ASSERT_STREQ(feat_types[i], c_out_types[i]); } @@ -270,7 +266,7 @@ int TestExceptionCatching() { } TEST(CAPI, Exception) { - ASSERT_NO_THROW({TestExceptionCatching();}); + ASSERT_NO_THROW({ TestExceptionCatching(); }); ASSERT_EQ(TestExceptionCatching(), -1); auto error = XGBGetLastError(); // Not null @@ -293,8 +289,7 @@ TEST(CAPI, XGBGlobalConfig) { ASSERT_EQ(ret, 0); std::string updated_config_str{updated_config_cstr}; - auto updated_config = - Json::Load({updated_config_str.data(), updated_config_str.size()}); + auto updated_config = Json::Load({updated_config_str.data(), updated_config_str.size()}); ASSERT_EQ(get(updated_config["verbosity"]), 0); ASSERT_EQ(get(updated_config["use_rmm"]), false); } @@ -311,8 +306,7 @@ TEST(CAPI, XGBGlobalConfig) { ASSERT_EQ(ret, 0); std::string updated_config_str{updated_config_cstr}; - auto updated_config = - Json::Load({updated_config_str.data(), updated_config_str.size()}); + auto updated_config = Json::Load({updated_config_str.data(), updated_config_str.size()}); ASSERT_EQ(get(updated_config["use_rmm"]), true); } { @@ -322,7 +316,7 @@ TEST(CAPI, XGBGlobalConfig) { } )json"; ret = XGBSetGlobalConfig(config_str); - ASSERT_EQ(ret , -1); + ASSERT_EQ(ret, -1); auto err = std::string{XGBGetLastError()}; ASSERT_NE(err.find("foo"), std::string::npos); } @@ -334,7 +328,7 @@ TEST(CAPI, XGBGlobalConfig) { } )json"; ret = XGBSetGlobalConfig(config_str); - ASSERT_EQ(ret , -1); + ASSERT_EQ(ret, -1); auto err = std::string{XGBGetLastError()}; ASSERT_NE(err.find("foo"), std::string::npos); ASSERT_EQ(err.find("verbosity"), std::string::npos); @@ -342,7 +336,7 @@ TEST(CAPI, XGBGlobalConfig) { } TEST(CAPI, BuildInfo) { - char const* out; + char const *out; XGBuildInfo(&out); auto loaded = Json::Load(StringView{out}); ASSERT_TRUE(get(loaded).find("USE_OPENMP") != get(loaded).cend()); diff --git a/tests/cpp/categorical_helpers.h b/tests/cpp/categorical_helpers.h index c841b4443527..1429e2310aea 100644 --- a/tests/cpp/categorical_helpers.h +++ b/tests/cpp/categorical_helpers.h @@ -3,16 +3,17 @@ * * \brief Utilities for testing categorical data support. */ +#ifndef TESTS_CPP_CATEGORICAL_HELPERS_H_ +#define TESTS_CPP_CATEGORICAL_HELPERS_H_ #include #include -#include "xgboost/span.h" -#include "helpers.h" #include "../../src/common/categorical.h" +#include "helpers.h" +#include "xgboost/span.h" namespace xgboost { -inline std::vector OneHotEncodeFeature(std::vector x, - size_t num_cat) { +inline std::vector OneHotEncodeFeature(std::vector x, size_t num_cat) { std::vector ret(x.size() * num_cat, 0); size_t n_rows = x.size(); for (size_t r = 0; r < n_rows; ++r) { @@ -22,4 +23,6 @@ inline std::vector OneHotEncodeFeature(std::vector x, return ret; } -} // namespace xgboost +} // namespace xgboost + +#endif // TESTS_CPP_CATEGORICAL_HELPERS_H_ diff --git a/tests/cpp/collective/test_allgather.cc b/tests/cpp/collective/test_allgather.cc index 7764a2adcc07..6e590dc3cce7 100644 --- a/tests/cpp/collective/test_allgather.cc +++ b/tests/cpp/collective/test_allgather.cc @@ -8,10 +8,12 @@ #include // for seconds #include // for size_t #include // for int32_t -#include // for iota -#include // for string -#include // for thread -#include // for vector +#include +#include // for iota +#include // for string +#include // for thread +#include +#include // for vector #include "../../../src/collective/allgather.h" // for RingAllgather #include "../../../src/collective/coll.h" // for Coll @@ -121,7 +123,7 @@ class Worker : public WorkerForTest { CheckV(s_recv); // Test inplace - auto test_inplace = [&] (AllgatherVAlgo algo) { + auto test_inplace = [&](AllgatherVAlgo algo) { std::fill_n(s_recv.data(), s_recv.size(), 0); auto current = s_recv.subspan(recv_segments[comm_.Rank()], recv_segments[comm_.Rank() + 1] - recv_segments[comm_.Rank()]); diff --git a/tests/cpp/collective/test_allgather.cu b/tests/cpp/collective/test_allgather.cu index d0c34cdc3843..4b0440620f90 100644 --- a/tests/cpp/collective/test_allgather.cu +++ b/tests/cpp/collective/test_allgather.cu @@ -9,7 +9,8 @@ #include // for size_t #include // for int32_t, int64_t -#include // for vector +#include +#include // for vector #include "../../../src/collective/allgather.h" // for RingAllgather #include "../../../src/common/device_helpers.cuh" // for ToSpan, device_vector diff --git a/tests/cpp/collective/test_allreduce.cc b/tests/cpp/collective/test_allreduce.cc index 6da214475bca..a8d4cb9ec2af 100644 --- a/tests/cpp/collective/test_allreduce.cc +++ b/tests/cpp/collective/test_allreduce.cc @@ -3,12 +3,16 @@ */ #include +#include +#include #include // for iota +#include +#include #include "../../../src/collective/allreduce.h" #include "../../../src/collective/coll.h" // for Coll -#include "../../../src/common/type.h" // for EraseType -#include "test_worker.h" // for WorkerForTest, TestDistributed +#include "../../../src/common/type.h" // for EraseType +#include "test_worker.h" // for WorkerForTest, TestDistributed namespace xgboost::collective { namespace { diff --git a/tests/cpp/collective/test_allreduce.cu b/tests/cpp/collective/test_allreduce.cu index 482c14ffc154..6a2c17680c80 100644 --- a/tests/cpp/collective/test_allreduce.cu +++ b/tests/cpp/collective/test_allreduce.cu @@ -5,6 +5,9 @@ #include #include // for host_vector +#include +#include + #include "../../../src/collective/comm.cuh" // for NCCLComm #include "../../../src/common/cuda_rt_utils.h" // for AllVisibleGPUs #include "../../../src/common/device_helpers.cuh" // for ToSpan, device_vector diff --git a/tests/cpp/collective/test_broadcast.cc b/tests/cpp/collective/test_broadcast.cc index 1b1d73428be1..34175494a6c8 100644 --- a/tests/cpp/collective/test_broadcast.cc +++ b/tests/cpp/collective/test_broadcast.cc @@ -4,6 +4,7 @@ #include #include +#include #include // for int32_t #include // for string #include // for thread diff --git a/tests/cpp/collective/test_coll_c_api.cc b/tests/cpp/collective/test_coll_c_api.cc index c24ddd275983..516d1c63714e 100644 --- a/tests/cpp/collective/test_coll_c_api.cc +++ b/tests/cpp/collective/test_coll_c_api.cc @@ -5,7 +5,9 @@ #include #include // for ""s +#include #include // for thread +#include #include "../../../src/collective/allgather.h" // for RingAllgather #include "../../../src/collective/tracker.h" diff --git a/tests/cpp/collective/test_comm.cc b/tests/cpp/collective/test_comm.cc index dc3351b2b148..a389e5197563 100644 --- a/tests/cpp/collective/test_comm.cc +++ b/tests/cpp/collective/test_comm.cc @@ -3,6 +3,9 @@ */ #include +#include +#include + #include "../../../src/collective/comm.h" #include "../../../src/common/type.h" // for EraseType #include "test_worker.h" // for TrackerTest @@ -28,7 +31,9 @@ TEST_F(CommTest, Channel) { auto rc = Success() << [&] { return p_chan->SendAll( EraseType(common::Span{&i, static_cast(1)})); - } << [&] { return p_chan->Block(); }; + } << [&] { + return p_chan->Block(); + }; SafeColl(rc); } else { auto p_chan = worker.Comm().Chan(i - 1); @@ -36,7 +41,9 @@ TEST_F(CommTest, Channel) { auto rc = Success() << [&] { return p_chan->RecvAll( EraseType(common::Span{&r, static_cast(1)})); - } << [&] { return p_chan->Block(); }; + } << [&] { + return p_chan->Block(); + }; SafeColl(rc); ASSERT_EQ(r, i - 1); } diff --git a/tests/cpp/collective/test_comm_group.cc b/tests/cpp/collective/test_comm_group.cc index 3d4cbc39910f..bd7f2c8eeaba 100644 --- a/tests/cpp/collective/test_comm_group.cc +++ b/tests/cpp/collective/test_comm_group.cc @@ -4,10 +4,12 @@ #include #include // for Json +#include #include // for seconds #include // for int32_t -#include // for string -#include // for thread +#include +#include // for string +#include // for thread #include "../../../src/collective/comm.h" #include "../../../src/collective/comm_group.h" diff --git a/tests/cpp/collective/test_result.cc b/tests/cpp/collective/test_result.cc index 1c7194f92f5c..4c8e793d6b10 100644 --- a/tests/cpp/collective/test_result.cc +++ b/tests/cpp/collective/test_result.cc @@ -4,6 +4,9 @@ #include #include +#include +#include + namespace xgboost::collective { TEST(Result, Concat) { auto rc0 = Fail("foo"); diff --git a/tests/cpp/collective/test_socket.cc b/tests/cpp/collective/test_socket.cc index 8e455d100f0d..e6bedf6d89b5 100644 --- a/tests/cpp/collective/test_socket.cc +++ b/tests/cpp/collective/test_socket.cc @@ -4,7 +4,8 @@ #include #include -#include // EADDRNOTAVAIL +#include // EADDRNOTAVAIL +#include #include // std::error_code, std::system_category #include "test_worker.h" // for SocketTest diff --git a/tests/cpp/collective/test_worker.h b/tests/cpp/collective/test_worker.h index 29d4f8998276..37532850f2ef 100644 --- a/tests/cpp/collective/test_worker.h +++ b/tests/cpp/collective/test_worker.h @@ -5,13 +5,14 @@ #include #include // for InitNewThread -#include // for seconds -#include // for int32_t -#include // for ifstream -#include // for string -#include // for thread -#include // for move -#include // for vector +#include // for max +#include // for seconds +#include // for int32_t +#include // for ifstream +#include // for string +#include // for thread +#include // for move +#include // for vector #include "../../../src/collective/comm.h" // for RabitComm #include "../../../src/collective/communicator-inl.h" // for Init, Finalize diff --git a/tests/cpp/common/test_algorithm.cc b/tests/cpp/common/test_algorithm.cc index 630460714e37..eab94a0e2457 100644 --- a/tests/cpp/common/test_algorithm.cc +++ b/tests/cpp/common/test_algorithm.cc @@ -6,6 +6,8 @@ #include #include // is_sorted +#include +#include #include "../../../src/common/algorithm.h" diff --git a/tests/cpp/common/test_algorithm.cu b/tests/cpp/common/test_algorithm.cu index 0db737ad9f27..6016b37df94c 100644 --- a/tests/cpp/common/test_algorithm.cu +++ b/tests/cpp/common/test_algorithm.cu @@ -6,8 +6,10 @@ #include // sequence #include // is_sorted -#include // is_sorted -#include // size_t +#include // is_sorted +#include // size_t +#include +#include #include "../../../src/common/algorithm.cuh" #include "../../../src/common/device_helpers.cuh" diff --git a/tests/cpp/common/test_bitfield.cc b/tests/cpp/common/test_bitfield.cc index 564776642301..4c8d0269f37b 100644 --- a/tests/cpp/common/test_bitfield.cc +++ b/tests/cpp/common/test_bitfield.cc @@ -2,6 +2,9 @@ * Copyright 2019-2023, XGBoost contributors */ #include + +#include + #include "../../../src/common/bitfield.h" namespace xgboost { @@ -10,9 +13,9 @@ TEST(BitField, Check) { { std::vector storage(4, 0); storage[2] = 2; - auto bits = LBitField64({storage.data(), - static_cast::index_type>( - storage.size())}); + auto bits = LBitField64( + {storage.data(), + static_cast::index_type>(storage.size())}); size_t true_bit = 190; for (size_t i = true_bit + 1; i < bits.Capacity(); ++i) { ASSERT_FALSE(bits.Check(i)); @@ -26,9 +29,9 @@ TEST(BitField, Check) { { std::vector storage(4, 0); storage[2] = 1 << 3; - auto bits = RBitField8({storage.data(), - static_cast::index_type>( - storage.size())}); + auto bits = RBitField8( + {storage.data(), + static_cast::index_type>(storage.size())}); size_t true_bit = 19; for (size_t i = 0; i < true_bit; ++i) { ASSERT_FALSE(bits.Check(i)); @@ -69,12 +72,8 @@ void TestBitFieldSet(typename BitFieldT::value_type res, size_t index, size_t tr } TEST(BitField, Set) { - { - TestBitFieldSet(2, 2, 190); - } - { - TestBitFieldSet(1 << 3, 2, 19); - } + { TestBitFieldSet(2, 2, 190); } + { TestBitFieldSet(1 << 3, 2, 19); } } template @@ -90,12 +89,8 @@ void TestBitFieldClear(size_t clear_bit) { } TEST(BitField, Clear) { - { - TestBitFieldClear(190); - } - { - TestBitFieldClear(19); - } + { TestBitFieldClear(190); } + { TestBitFieldClear(19); } } TEST(BitField, CTZ) { diff --git a/tests/cpp/common/test_bitfield.cu b/tests/cpp/common/test_bitfield.cu index a9b183c43740..91297c25153a 100644 --- a/tests/cpp/common/test_bitfield.cu +++ b/tests/cpp/common/test_bitfield.cu @@ -4,7 +4,9 @@ #include #include #include + #include + #include "../../../src/common/bitfield.h" #include "../../../src/common/device_helpers.cuh" @@ -18,7 +20,7 @@ __global__ void TestSetKernel(LBitField64 bits) { } TEST(BitField, StorageSize) { - size_t constexpr kElements { 16 }; + size_t constexpr kElements{16}; size_t size = LBitField64::ComputeStorageSize(kElements); ASSERT_EQ(1, size); size = RBitField8::ComputeStorageSize(4); diff --git a/tests/cpp/common/test_categorical.cc b/tests/cpp/common/test_categorical.cc index 4e6e696ecfdd..65e94aeb7bb6 100644 --- a/tests/cpp/common/test_categorical.cc +++ b/tests/cpp/common/test_categorical.cc @@ -6,6 +6,8 @@ #include #include +#include +#include #include "../../../src/common/categorical.h" #include "../helpers.h" @@ -66,8 +68,8 @@ TEST(Categorical, MinimalSet) { HostDeviceVector predt; { - std::vector data{static_cast(kCat), - static_cast(kCat + 1), 32.0f, 33.0f, 34.0f}; + std::vector data{static_cast(kCat), static_cast(kCat + 1), 32.0f, 33.0f, + 34.0f}; auto test = GetDMatrixFromData(data, data.size(), kCols); learner->Predict(test, false, &predt, 0, 0, false, /*pred_leaf=*/true); ASSERT_EQ(predt.Size(), data.size()); diff --git a/tests/cpp/common/test_charconv.cc b/tests/cpp/common/test_charconv.cc index 0e43ea51e810..00890a27ae69 100644 --- a/tests/cpp/common/test_charconv.cc +++ b/tests/cpp/common/test_charconv.cc @@ -20,9 +20,12 @@ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. */ -#include #include + +#include #include +#include + #include "../../../src/common/charconv.h" namespace xgboost { @@ -40,7 +43,7 @@ static float Int32Bits2Float(uint32_t bits) { return f; } -void TestRyu(char const *res, float v) { +void TestRyu(char const* res, float v) { char result[xgboost::NumericLimits::kToCharsSize]; auto ret = to_chars(result, result + sizeof(result), v); *ret.ptr = '\0'; @@ -60,13 +63,9 @@ TEST(Ryu, Subnormal) { TestRyu("1E-45", std::numeric_limits::denorm_min()); } -TEST(Ryu, Denormal) { - TestRyu("1E-45", std::numeric_limits::denorm_min()); -} +TEST(Ryu, Denormal) { TestRyu("1E-45", std::numeric_limits::denorm_min()); } -TEST(Ryu, SwitchToSubnormal) { - TestRyu("1.1754944E-38", 1.1754944E-38f); -} +TEST(Ryu, SwitchToSubnormal) { TestRyu("1.1754944E-38", 1.1754944E-38f); } TEST(Ryu, MinAndMax) { TestRyu("3.4028235E38", Int32Bits2Float(0x7f7fffff)); @@ -134,7 +133,7 @@ TEST(Ryu, Regression) { TEST(Ryu, RoundTrip) { float f = -1.1493590134238582e-40; - char result[NumericLimits::kToCharsSize] { 0 }; + char result[NumericLimits::kToCharsSize]{0}; auto ret = to_chars(result, result + sizeof(result), f); size_t dis = std::distance(result, ret.ptr); float back; @@ -157,7 +156,7 @@ TEST(Ryu, LooksLikePow5) { } TEST(Ryu, OutputLength) { - TestRyu("1E0", 1.0f); // already tested in Basic + TestRyu("1E0", 1.0f); // already tested in Basic TestRyu("1.2E0", 1.2f); TestRyu("1.23E0", 1.23f); TestRyu("1.234E0", 1.234f); @@ -201,7 +200,7 @@ TEST(Ryu, MinMax) { TEST(Ryu, MantissaRoundingOverflow) { TestRyuParse(1.0f, "0.999999999"); TestRyuParse(INFINITY, "3.4028236e+38"); - TestRyuParse(1.1754944e-38f, "1.17549430e-38"); // FLT_MIN + TestRyuParse(1.1754944e-38f, "1.17549430e-38"); // FLT_MIN } TEST(Ryu, TrailingZeros) { diff --git a/tests/cpp/common/test_compressed_iterator.cc b/tests/cpp/common/test_compressed_iterator.cc index 6b8a8310bfd6..4fcbde0dadec 100644 --- a/tests/cpp/common/test_compressed_iterator.cc +++ b/tests/cpp/common/test_compressed_iterator.cc @@ -1,9 +1,11 @@ /** * Copyright 2017-2024, XGBoost contributors */ +#include +#include + #include "../../../src/common/compressed_iterator.h" #include "gtest/gtest.h" -#include namespace xgboost::common { TEST(CompressedIterator, Size) { @@ -21,8 +23,8 @@ TEST(CompressedIterator, Size) { } TEST(CompressedIterator, Test) { - ASSERT_TRUE(detail::SymbolBits(256) == 8); - ASSERT_TRUE(detail::SymbolBits(150) == 8); + ASSERT_EQ(detail::SymbolBits(256), 8); + ASSERT_EQ(detail::SymbolBits(150), 8); std::vector test_cases = {1, 3, 426, 21, 64, 256, 100000, INT32_MAX}; int num_elements = 1000; int repetitions = 1000; @@ -31,14 +33,12 @@ TEST(CompressedIterator, Test) { for (auto alphabet_size : test_cases) { for (int i = 0; i < repetitions; i++) { std::vector input(num_elements); - std::generate(input.begin(), input.end(), - [=]() { return rand() % alphabet_size; }); + std::generate(input.begin(), input.end(), [=]() { return rand() % alphabet_size; }); CompressedBufferWriter cbw(alphabet_size); // Test write entire array std::vector buffer( - CompressedBufferWriter::CalculateBufferSize(input.size(), - alphabet_size)); + CompressedBufferWriter::CalculateBufferSize(input.size(), alphabet_size)); cbw.Write(buffer.data(), input.begin(), input.end()); @@ -48,12 +48,11 @@ TEST(CompressedIterator, Test) { output[i] = ci[i]; } - ASSERT_TRUE(input == output); + ASSERT_EQ(input, output); // Test write Symbol std::vector buffer2( - CompressedBufferWriter::CalculateBufferSize(input.size(), - alphabet_size)); + CompressedBufferWriter::CalculateBufferSize(input.size(), alphabet_size)); for (size_t i = 0; i < input.size(); i++) { cbw.WriteSymbol(buffer2.data(), input[i], i); } diff --git a/tests/cpp/common/test_cuda_dr_utils.cc b/tests/cpp/common/test_cuda_dr_utils.cc index 5876c11e376b..5c146e3c1db9 100644 --- a/tests/cpp/common/test_cuda_dr_utils.cc +++ b/tests/cpp/common/test_cuda_dr_utils.cc @@ -44,12 +44,8 @@ TEST(DrUtils, GetC2cLinkCountFromSmi) { ASSERT_EQ(lc, -1); } - { - [[maybe_unused]] auto _ = GetC2cLinkCountFromSmi(); - } - { - [[maybe_unused]] auto _ = GetC2cLinkCountFromSmiGlobal(); - } + { [[maybe_unused]] auto _ = GetC2cLinkCountFromSmi(); } + { [[maybe_unused]] auto _ = GetC2cLinkCountFromSmiGlobal(); } } } // namespace xgboost::cudr #endif // defined(XGBOOST_USE_CUDA) diff --git a/tests/cpp/common/test_cuda_host_allocator.cu b/tests/cpp/common/test_cuda_host_allocator.cu index c7cad2d07d93..5656452b4808 100644 --- a/tests/cpp/common/test_cuda_host_allocator.cu +++ b/tests/cpp/common/test_cuda_host_allocator.cu @@ -7,9 +7,9 @@ #include #include "../../../src/common/cuda_pinned_allocator.h" -#include "../../../src/common/cuda_stream.h" // for DefaultStream +#include "../../../src/common/cuda_stream.h" // for DefaultStream #include "../../../src/common/device_helpers.cuh" -#include "../../../src/common/numeric.h" // for Iota +#include "../../../src/common/numeric.h" // for Iota namespace xgboost { TEST(CudaHostMalloc, Pinned) { diff --git a/tests/cpp/common/test_cuda_rt_utils.cu b/tests/cpp/common/test_cuda_rt_utils.cu index 0465fa65194e..87f06a0f8d23 100644 --- a/tests/cpp/common/test_cuda_rt_utils.cu +++ b/tests/cpp/common/test_cuda_rt_utils.cu @@ -5,7 +5,8 @@ #include #include // for int32_t -#include // for set +#include +#include // for set #include "../../../src/common/cuda_stream_pool.h" diff --git a/tests/cpp/common/test_device_helpers.cu b/tests/cpp/common/test_device_helpers.cu index 8ff413cc22cb..9de52c6f94f0 100644 --- a/tests/cpp/common/test_device_helpers.cu +++ b/tests/cpp/common/test_device_helpers.cu @@ -8,6 +8,7 @@ #include #include #include // for equal_to +#include #include #include "../../../src/common/cuda_context.cuh" @@ -26,21 +27,18 @@ void TestAtomicSizeT() { size_t constexpr kThreads = 235; dh::device_vector out(1, 0); auto d_out = dh::ToSpan(out); - dh::LaunchN(kThreads, [=] __device__(size_t idx) { - atomicAdd(&d_out[0], static_cast(1)); - }); + dh::LaunchN(kThreads, + [=] __device__(size_t idx) { atomicAdd(&d_out[0], static_cast(1)); }); ASSERT_EQ(out[0], kThreads); } -TEST(AtomicAdd, SizeT) { - TestAtomicSizeT(); -} +TEST(AtomicAdd, SizeT) { TestAtomicSizeT(); } void TestSegmentID() { std::vector segments{0, 1, 3}; thrust::device_vector d_segments(segments); auto s_segments = dh::ToSpan(d_segments); - dh::LaunchN(1, [=]__device__(size_t idx) { + dh::LaunchN(1, [=] __device__(size_t idx) { auto id = dh::SegmentId(s_segments, 0); SPAN_CHECK(id == 0); id = dh::SegmentId(s_segments, 1); @@ -50,9 +48,7 @@ void TestSegmentID() { }); } -TEST(SegmentID, Basic) { - TestSegmentID(); -} +TEST(SegmentID, Basic) { TestSegmentID(); } TEST(SegmentedUnique, Basic) { std::vector values{0.1f, 0.2f, 0.3f, 0.62448811531066895f, 0.62448811531066895f, 0.4f}; @@ -72,7 +68,7 @@ TEST(SegmentedUnique, Basic) { CHECK_EQ(n_uniques, 5); std::vector values_sol{0.1f, 0.2f, 0.3f, 0.62448811531066895f, 0.4f}; - for (size_t i = 0 ; i < values_sol.size(); i ++) { + for (size_t i = 0; i < values_sol.size(); i++) { ASSERT_EQ(d_vals_out[i], values_sol[i]); } @@ -88,7 +84,7 @@ TEST(SegmentedUnique, Basic) { d_values.data().get(), d_values.data().get() + d_values.size(), d_segs_out.data().get(), d_vals_out.data().get(), std::equal_to{}); ASSERT_EQ(n_uniques, values.size()); - for (size_t i = 0 ; i < values.size(); i ++) { + for (size_t i = 0; i < values.size(); i++) { ASSERT_EQ(d_vals_out[i], values[i]); } } @@ -122,8 +118,8 @@ void TestSegmentedUniqueRegression(std::vector values, size_t n_dup d_values.data().get(), d_values.data().get() + d_values.size(), d_segments_out.data().get(), d_values.data().get(), SketchUnique{}); ASSERT_EQ(n_uniques, values.size() - n_duplicated); - ASSERT_TRUE(thrust::is_sorted(thrust::device, d_values.begin(), - d_values.begin() + n_uniques, IsSorted{})); + ASSERT_TRUE(thrust::is_sorted(thrust::device, d_values.begin(), d_values.begin() + n_uniques, + IsSorted{})); ASSERT_EQ(segments.at(0), d_segments_out[0]); ASSERT_EQ(segments.at(1), d_segments_out[1] + n_duplicated); } @@ -132,37 +128,29 @@ TEST(DeviceHelpers, Reduce) { size_t kSize = std::numeric_limits::max(); auto it = thrust::make_counting_iterator(0ul); dh::XGBCachingDeviceAllocator alloc; - auto batched = dh::Reduce(thrust::cuda::par(alloc), it, it + kSize, 0ul, thrust::maximum{}); + auto batched = + dh::Reduce(thrust::cuda::par(alloc), it, it + kSize, 0ul, thrust::maximum{}); CHECK_EQ(batched, kSize - 1); } - TEST(SegmentedUnique, Regression) { { - std::vector values{{3149, 3150, 1, 0.62392902374267578}, - {3151, 3152, 1, 0.62418866157531738}, - {3152, 3153, 1, 0.62419462203979492}, - {3153, 3154, 1, 0.62431186437606812}, - {3154, 3155, 1, 0.6244881153106689453125}, - {3155, 3156, 1, 0.6244881153106689453125}, - {3155, 3156, 1, 0.6244881153106689453125}, - {3155, 3156, 1, 0.6244881153106689453125}, - {3157, 3158, 1, 0.62552797794342041}, - {3158, 3159, 1, 0.6256556510925293}, - {3159, 3160, 1, 0.62571090459823608}, - {3160, 3161, 1, 0.62577134370803833}}; + std::vector values{ + {3149, 3150, 1, 0.62392902374267578}, {3151, 3152, 1, 0.62418866157531738}, + {3152, 3153, 1, 0.62419462203979492}, {3153, 3154, 1, 0.62431186437606812}, + {3154, 3155, 1, 0.6244881153106689453125}, {3155, 3156, 1, 0.6244881153106689453125}, + {3155, 3156, 1, 0.6244881153106689453125}, {3155, 3156, 1, 0.6244881153106689453125}, + {3157, 3158, 1, 0.62552797794342041}, {3158, 3159, 1, 0.6256556510925293}, + {3159, 3160, 1, 0.62571090459823608}, {3160, 3161, 1, 0.62577134370803833}}; TestSegmentedUniqueRegression(values, 3); } { - std::vector values{{3149, 3150, 1, 0.62392902374267578}, - {3151, 3152, 1, 0.62418866157531738}, - {3152, 3153, 1, 0.62419462203979492}, - {3153, 3154, 1, 0.62431186437606812}, - {3154, 3155, 1, 0.6244881153106689453125}, - {3157, 3158, 1, 0.62552797794342041}, - {3158, 3159, 1, 0.6256556510925293}, - {3159, 3160, 1, 0.62571090459823608}, - {3160, 3161, 1, 0.62577134370803833}}; + std::vector values{ + {3149, 3150, 1, 0.62392902374267578}, {3151, 3152, 1, 0.62418866157531738}, + {3152, 3153, 1, 0.62419462203979492}, {3153, 3154, 1, 0.62431186437606812}, + {3154, 3155, 1, 0.6244881153106689453125}, {3157, 3158, 1, 0.62552797794342041}, + {3158, 3159, 1, 0.6256556510925293}, {3159, 3160, 1, 0.62571090459823608}, + {3160, 3161, 1, 0.62577134370803833}}; TestSegmentedUniqueRegression(values, 0); } { @@ -173,8 +161,8 @@ TEST(SegmentedUnique, Regression) { TEST(Allocator, DISABLED_OOM) { auto size = dh::AvailableMemory(0) * 4; - ASSERT_THROW({dh::caching_device_vector vec(size);}, dmlc::Error); - ASSERT_THROW({dh::device_vector vec(size);}, dmlc::Error); + ASSERT_THROW({ dh::caching_device_vector vec(size); }, dmlc::Error); + ASSERT_THROW({ dh::device_vector vec(size); }, dmlc::Error); // Clear last error so we don't fail subsequent tests cudaGetLastError(); } diff --git a/tests/cpp/common/test_device_vector.cu b/tests/cpp/common/test_device_vector.cu index 16a847648eb7..7570c6f03832 100644 --- a/tests/cpp/common/test_device_vector.cu +++ b/tests/cpp/common/test_device_vector.cu @@ -7,6 +7,8 @@ #include // for iota #include // for thread +#include +#include #include "../../../src/common/cuda_rt_utils.h" // for DrVersion #include "../../../src/common/device_helpers.cuh" // for CachingThrustPolicy, PinnedMemory @@ -151,7 +153,7 @@ TEST(TestVirtualMem, Version) { PinnedMemory pinned; #if defined(xgboost_IS_WIN) ASSERT_FALSE(pinned.IsVm()); -#else // defined(xgboost_IS_WIN) +#else // defined(xgboost_IS_WIN) if (major == 12 && minor >= 5 || major > 12) { ASSERT_TRUE(pinned.IsVm()); } else { diff --git a/tests/cpp/common/test_gpu_compressed_iterator.cu b/tests/cpp/common/test_gpu_compressed_iterator.cu index e633eefb9599..ca54f85845a0 100644 --- a/tests/cpp/common/test_gpu_compressed_iterator.cu +++ b/tests/cpp/common/test_gpu_compressed_iterator.cu @@ -32,11 +32,9 @@ struct ReadSymbolFunction { CompressedIterator ci; int* output_data_d; ReadSymbolFunction(CompressedIterator ci, int* output_data_d) - : ci(ci), output_data_d(output_data_d) {} + : ci(ci), output_data_d(output_data_d) {} - __device__ void operator()(size_t i) { - output_data_d[i] = ci[i]; - } + __device__ void operator()(size_t i) { output_data_d[i] = ci[i]; } }; TEST(CompressedIterator, TestGPU) { @@ -49,20 +47,17 @@ TEST(CompressedIterator, TestGPU) { for (auto alphabet_size : test_cases) { for (int i = 0; i < repetitions; i++) { std::vector input(num_elements); - std::generate(input.begin(), input.end(), - [=]() { return rand() % alphabet_size; }); + std::generate(input.begin(), input.end(), [=]() { return rand() % alphabet_size; }); CompressedBufferWriter cbw(alphabet_size); thrust::device_vector input_d(input); thrust::device_vector buffer_d( - CompressedBufferWriter::CalculateBufferSize(input.size(), - alphabet_size)); + CompressedBufferWriter::CalculateBufferSize(input.size(), alphabet_size)); // write the data on device auto input_data_d = input_d.data().get(); auto buffer_data_d = buffer_d.data().get(); - dh::LaunchN(input_d.size(), - WriteSymbolFunction(cbw, buffer_data_d, input_data_d)); + dh::LaunchN(input_d.size(), WriteSymbolFunction(cbw, buffer_data_d, input_data_d)); // read the data on device CompressedIterator ci(buffer_d.data().get(), alphabet_size); diff --git a/tests/cpp/common/test_group_data.cc b/tests/cpp/common/test_group_data.cc index 719bc3fc5a13..4879172f1706 100644 --- a/tests/cpp/common/test_group_data.cc +++ b/tests/cpp/common/test_group_data.cc @@ -3,6 +3,9 @@ */ #include #include + +#include + #include "../../../src/common/group_data.h" namespace xgboost { @@ -35,8 +38,7 @@ TEST(GroupData, ParallelGroupBuilder) { EXPECT_EQ(offsets, expected_offsets); // Create new builder, add one more row given already populated offsets/data - ParallelGroupBuilder builder2(&offsets, &data, - offsets.size() - 1); + ParallelGroupBuilder builder2(&offsets, &data, offsets.size() - 1); builder2.InitBudget(0, 1); builder2.AddBudget(2, 0, 2); builder2.InitStorage(); diff --git a/tests/cpp/common/test_hist_util.cc b/tests/cpp/common/test_hist_util.cc index 2017df632e18..a41658bd4986 100644 --- a/tests/cpp/common/test_hist_util.cc +++ b/tests/cpp/common/test_hist_util.cc @@ -7,7 +7,9 @@ #include // for ExtMemConfig #include // for HostDeviceVector -#include // for shared_ptr +#include +#include +#include // for shared_ptr #include #include @@ -27,14 +29,14 @@ void ParallelGHistBuilderReset() { HistCollection collection; collection.Init(kBins); - for(size_t inode = 0; inode < kNodesExtended; inode++) { + for (size_t inode = 0; inode < kNodesExtended; inode++) { collection.AddHistRow(inode); collection.AllocateData(inode); } ParallelGHistBuilder hist_builder; hist_builder.Init(kBins); std::vector target_hist(kNodes); - for(size_t i = 0; i < target_hist.size(); ++i) { + for (size_t i = 0; i < target_hist.size(); ++i) { target_hist[i] = collection[i]; } @@ -54,7 +56,7 @@ void ParallelGHistBuilderReset() { // reset and extend buffer target_hist.resize(kNodesExtended); - for(size_t i = 0; i < target_hist.size(); ++i) { + for (size_t i = 0; i < target_hist.size(); ++i) { target_hist[i] = collection[i]; } common::BlockedSpace2d space2( @@ -73,7 +75,7 @@ void ParallelGHistBuilderReset() { }); } -void ParallelGHistBuilderReduceHist(){ +void ParallelGHistBuilderReduceHist() { constexpr size_t kBins = 10; constexpr size_t kNodes = 5; constexpr size_t kTasksPerNode = 10; @@ -83,14 +85,14 @@ void ParallelGHistBuilderReduceHist(){ HistCollection collection; collection.Init(kBins); - for(size_t inode = 0; inode < kNodes; inode++) { + for (size_t inode = 0; inode < kNodes; inode++) { collection.AddHistRow(inode); collection.AllocateData(inode); } ParallelGHistBuilder hist_builder; hist_builder.Init(kBins); std::vector target_hist(kNodes); - for(size_t i = 0; i < target_hist.size(); ++i) { + for (size_t i = 0; i < target_hist.size(); ++i) { target_hist[i] = collection[i]; } @@ -103,17 +105,17 @@ void ParallelGHistBuilderReduceHist(){ const size_t tid = omp_get_thread_num(); GHistRow hist = hist_builder.GetInitializedHist(tid, inode); - for(size_t i = 0; i < kBins; ++i) { + for (size_t i = 0; i < kBins; ++i) { hist[i].Add(kValue, kValue); } }); - for(size_t inode = 0; inode < kNodes; inode++) { + for (size_t inode = 0; inode < kNodes; inode++) { hist_builder.ReduceHist(inode, 0, kBins); // We had kTasksPerNode tasks to add kValue to each bin for each node // So, after reducing we expect to have (kValue * kTasksPerNode) in each node - for(size_t i = 0; i < kBins; ++i) { + for (size_t i = 0; i < kBins; ++i) { ASSERT_EQ(kValue * kTasksPerNode, collection[inode][i].GetGrad()); ASSERT_EQ(kValue * kTasksPerNode, collection[inode][i].GetHess()); } @@ -153,7 +155,7 @@ TEST(CutsBuilder, SearchGroupInd) { EXPECT_THROW(HostSketchContainer::SearchGroupIndFromRow(p_mat->Info().group_ptr_, 17), dmlc::Error); - std::vector group_ptr {0, 1, 2}; + std::vector group_ptr{0, 1, 2}; CHECK_EQ(HostSketchContainer::SearchGroupIndFromRow(group_ptr, 1), 1); } @@ -272,11 +274,10 @@ TEST(HistUtil, DenseCutsExternalMemory) { } TEST(HistUtil, IndexBinBound) { - uint64_t bin_sizes[] = { static_cast(std::numeric_limits::max()) + 1, - static_cast(std::numeric_limits::max()) + 1, - static_cast(std::numeric_limits::max()) + 2 }; - BinTypeSize expected_bin_type_sizes[] = {kUint8BinsTypeSize, - kUint16BinsTypeSize, + uint64_t bin_sizes[] = {static_cast(std::numeric_limits::max()) + 1, + static_cast(std::numeric_limits::max()) + 1, + static_cast(std::numeric_limits::max()) + 2}; + BinTypeSize expected_bin_type_sizes[] = {kUint8BinsTypeSize, kUint16BinsTypeSize, kUint32BinsTypeSize}; size_t constexpr kRows = 100; size_t constexpr kCols = 10; @@ -286,7 +287,7 @@ TEST(HistUtil, IndexBinBound) { auto p_fmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatrix(); GHistIndexMatrix hmat(&ctx, p_fmat.get(), max_bin, 0.5, false); - EXPECT_EQ(hmat.index.Size(), kRows*kCols); + EXPECT_EQ(hmat.index.Size(), kRows * kCols); EXPECT_EQ(expected_bin_type_sizes[bin_id++], hmat.index.GetBinTypeSize()); } } @@ -300,9 +301,10 @@ void CheckIndexData(T const* data_ptr, uint32_t const* offsets, const GHistIndex } TEST(HistUtil, IndexBinData) { - uint64_t constexpr kBinSizes[] = { static_cast(std::numeric_limits::max()) + 1, - static_cast(std::numeric_limits::max()) + 1, - static_cast(std::numeric_limits::max()) + 2 }; + uint64_t constexpr kBinSizes[] = { + static_cast(std::numeric_limits::max()) + 1, + static_cast(std::numeric_limits::max()) + 1, + static_cast(std::numeric_limits::max()) + 2}; size_t constexpr kRows = 100; size_t constexpr kCols = 10; Context ctx; @@ -311,19 +313,16 @@ TEST(HistUtil, IndexBinData) { auto p_fmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatrix(); GHistIndexMatrix hmat(&ctx, p_fmat.get(), max_bin, 0.5, false); uint32_t const* offsets = hmat.index.Offset(); - EXPECT_EQ(hmat.index.Size(), kRows*kCols); + EXPECT_EQ(hmat.index.Size(), kRows * kCols); switch (max_bin) { case kBinSizes[0]: - CheckIndexData(hmat.index.data(), - offsets, hmat, kCols); + CheckIndexData(hmat.index.data(), offsets, hmat, kCols); break; case kBinSizes[1]: - CheckIndexData(hmat.index.data(), - offsets, hmat, kCols); + CheckIndexData(hmat.index.data(), offsets, hmat, kCols); break; case kBinSizes[2]: - CheckIndexData(hmat.index.data(), - offsets, hmat, kCols); + CheckIndexData(hmat.index.data(), offsets, hmat, kCols); break; } } diff --git a/tests/cpp/common/test_hist_util.cu b/tests/cpp/common/test_hist_util.cu index 3ae9229cc9b5..90b62b9227c6 100644 --- a/tests/cpp/common/test_hist_util.cu +++ b/tests/cpp/common/test_hist_util.cu @@ -11,9 +11,10 @@ #include // for floor #include // for size_t #include // for numeric_limits -#include // for string, to_string -#include // for tuple, make_tuple -#include // for vector +#include +#include // for string, to_string +#include // for tuple, make_tuple +#include // for vector #include "../../../include/xgboost/logging.h" #include "../../../src/common/cuda_context.cuh" @@ -83,8 +84,8 @@ TEST(HistUtil, DeviceSketchMemory) { ConsoleLogger::Configure({{"verbosity", "3"}}); auto device_cuts = DeviceSketch(&ctx, dmat.get(), num_bins); - size_t bytes_required = detail::RequiredMemory( - num_rows, num_columns, num_rows * num_columns, num_bins, false); + size_t bytes_required = + detail::RequiredMemory(num_rows, num_columns, num_rows * num_columns, num_bins, false); EXPECT_LE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 1.05); EXPECT_GE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 0.95); ConsoleLogger::Configure({{"verbosity", "0"}}); @@ -104,8 +105,8 @@ TEST(HistUtil, DeviceSketchWeightsMemory) { auto device_cuts = DeviceSketch(&ctx, dmat.get(), num_bins); ConsoleLogger::Configure({{"verbosity", "0"}}); - size_t bytes_required = detail::RequiredMemory( - num_rows, num_columns, num_rows * num_columns, num_bins, true); + size_t bytes_required = + detail::RequiredMemory(num_rows, num_columns, num_rows * num_columns, num_bins, true); EXPECT_LE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 1.05); EXPECT_GE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required); } @@ -118,7 +119,7 @@ TEST(HistUtil, DeviceSketchDeterminism) { auto x = GenerateRandom(num_rows, num_columns); auto dmat = GetDMatrixFromData(x, num_rows, num_columns); auto reference_sketch = DeviceSketch(&ctx, dmat.get(), num_bins); - size_t constexpr kRounds{ 100 }; + size_t constexpr kRounds{100}; for (size_t r = 0; r < kRounds; ++r) { auto new_sketch = DeviceSketch(&ctx, dmat.get(), num_bins); ASSERT_EQ(reference_sketch.Values(), new_sketch.Values()); @@ -331,7 +332,7 @@ TEST(HistUtil, DeviceSketchMultipleColumnsExternal) { auto ctx = MakeCUDACtx(0); auto bin_sizes = {2, 16, 256, 512}; auto sizes = {100, 1000, 1500}; - int num_columns =5; + int num_columns = 5; for (auto num_rows : sizes) { HostDeviceVector x{GenerateRandom(num_rows, num_columns)}; common::TemporaryDirectory temp; @@ -375,7 +376,8 @@ auto MakeUnweightedCutsForTest(Context const* ctx, Adapter adapter, int32_t num_ } template -void ValidateBatchedCuts(Context const* ctx, Adapter adapter, int num_bins, DMatrix* dmat, size_t batch_size = 0) { +void ValidateBatchedCuts(Context const* ctx, Adapter adapter, int num_bins, DMatrix* dmat, + size_t batch_size = 0) { common::HistogramCuts batched_cuts = MakeUnweightedCutsForTest( ctx, adapter, num_bins, std::numeric_limits::quiet_NaN(), batch_size); ValidateCuts(batched_cuts, dmat, num_bins); @@ -386,10 +388,10 @@ TEST(HistUtil, AdapterDeviceSketch) { int rows = 5; int cols = 1; int num_bins = 4; - float missing = - 1.0; - thrust::device_vector< float> data(rows*cols); + float missing = -1.0; + thrust::device_vector data(rows * cols); auto json_array_interface = Generate2dArrayInterface(rows, cols, "{ 1.0,2.0,3.0,4.0,5.0 }; + data = std::vector{1.0, 2.0, 3.0, 4.0, 5.0}; std::string str; Json::Dump(json_array_interface, &str); @@ -418,8 +420,8 @@ TEST(HistUtil, AdapterDeviceSketchMemory) { auto cuts = MakeUnweightedCutsForTest(&ctx, adapter, num_bins, std::numeric_limits::quiet_NaN()); ConsoleLogger::Configure({{"verbosity", "0"}}); - size_t bytes_required = detail::RequiredMemory( - num_rows, num_columns, num_rows * num_columns, num_bins, false); + size_t bytes_required = + detail::RequiredMemory(num_rows, num_columns, num_rows * num_columns, num_bins, false); EXPECT_LE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 1.05); EXPECT_GE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 0.95); } @@ -443,8 +445,8 @@ TEST(HistUtil, AdapterSketchSlidingWindowMemory) { std::numeric_limits::quiet_NaN(), &sketch_container); HistogramCuts cuts; sketch_container.MakeCuts(&ctx, &cuts, info.IsColumnSplit()); - size_t bytes_required = detail::RequiredMemory( - num_rows, num_columns, num_rows * num_columns, num_bins, false); + size_t bytes_required = + detail::RequiredMemory(num_rows, num_columns, num_rows * num_columns, num_bins, false); EXPECT_LE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 1.05); EXPECT_GE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 0.95); ConsoleLogger::Configure({{"verbosity", "0"}}); @@ -474,14 +476,14 @@ TEST(HistUtil, AdapterSketchSlidingWindowWeightedMemory) { HistogramCuts cuts; sketch_container.MakeCuts(&ctx, &cuts, info.IsColumnSplit()); ConsoleLogger::Configure({{"verbosity", "0"}}); - size_t bytes_required = detail::RequiredMemory( - num_rows, num_columns, num_rows * num_columns, num_bins, true); + size_t bytes_required = + detail::RequiredMemory(num_rows, num_columns, num_rows * num_columns, num_bins, true); EXPECT_LE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required * 1.05); EXPECT_GE(dh::GlobalMemoryLogger().PeakMemory(), bytes_required); } -void TestCategoricalSketchAdapter(size_t n, size_t num_categories, - int32_t num_bins, bool weighted) { +void TestCategoricalSketchAdapter(size_t n, size_t num_categories, int32_t num_bins, + bool weighted) { auto ctx = MakeCUDACtx(0); auto h_x = GenerateRandomCategoricalSingleColumn(n, num_categories); thrust::device_vector x(h_x); @@ -699,7 +701,7 @@ TEST(HistUtil, DeviceSketchFromGroupWeights) { ASSERT_EQ(cuts.Ptrs().size(), weighted_cuts.Ptrs().size()); for (size_t i = 0; i < cuts.Values().size(); ++i) { - EXPECT_EQ(cuts.Values()[i], weighted_cuts.Values()[i]) << "i:"<< i; + EXPECT_EQ(cuts.Values()[i], weighted_cuts.Values()[i]) << "i:" << i; } for (size_t i = 0; i < cuts.MinValues().size(); ++i) { ASSERT_EQ(cuts.MinValues()[i], weighted_cuts.MinValues()[i]); diff --git a/tests/cpp/common/test_hist_util.h b/tests/cpp/common/test_hist_util.h index f4818b82fe8c..6c3fb4714174 100644 --- a/tests/cpp/common/test_hist_util.h +++ b/tests/cpp/common/test_hist_util.h @@ -4,8 +4,12 @@ #pragma once #include +#include +#include +#include #include // for shared_ptr #include +#include #include #include @@ -23,9 +27,9 @@ // Some helper functions used to test both GPU and CPU algorithms // namespace xgboost::common { - // Generate columns with different ranges +// Generate columns with different ranges inline std::vector GenerateRandom(int num_rows, int num_columns) { - std::vector x(num_rows*num_columns); + std::vector x(num_rows * num_columns); std::mt19937 rng(0); std::uniform_real_distribution dist(0.0, 1.0); std::generate(x.begin(), x.end(), [&]() { return dist(rng); }); @@ -46,15 +50,14 @@ inline std::vector GenerateRandomWeights(int num_rows) { } #ifdef __CUDACC__ -inline data::CupyAdapter AdapterFromData(const thrust::device_vector &x, - int num_rows, int num_columns) { +inline data::CupyAdapter AdapterFromData(const thrust::device_vector& x, int num_rows, + int num_columns) { Json array_interface{Object()}; std::vector shape = {Json(static_cast(num_rows)), - Json(static_cast(num_columns))}; + Json(static_cast(num_columns))}; array_interface["shape"] = Array(shape); - std::vector j_data{ - Json(Integer(reinterpret_cast(x.data().get()))), - Json(Boolean(false))}; + std::vector j_data{Json(Integer(reinterpret_cast(x.data().get()))), + Json(Boolean(false))}; array_interface["data"] = j_data; array_interface["version"] = 3; array_interface["typestr"] = String(" &x, } #endif -inline std::shared_ptr -GetDMatrixFromData(const std::vector &x, int num_rows, int num_columns) { +inline std::shared_ptr GetDMatrixFromData(const std::vector& x, + int num_rows, int num_columns) { data::DenseAdapter adapter(x.data(), num_rows, num_columns); - return std::shared_ptr(new data::SimpleDMatrix( - &adapter, std::numeric_limits::quiet_NaN(), 1)); + return std::shared_ptr( + new data::SimpleDMatrix(&adapter, std::numeric_limits::quiet_NaN(), 1)); } // Test that elements are approximately equally distributed among bins @@ -84,27 +87,24 @@ inline void TestBinDistribution(const HistogramCuts& cuts, int column_idx, bin_weights.at(bin_idx) += sorted_weights[i]; } int local_num_bins = cuts.Ptrs()[column_idx + 1] - cuts.Ptrs()[column_idx]; - auto total_weight = std::accumulate(sorted_weights.begin(), sorted_weights.end(),0); + auto total_weight = std::accumulate(sorted_weights.begin(), sorted_weights.end(), 0); int expected_bin_weight = total_weight / local_num_bins; // Allow up to 30% deviation. This test is not very strict, it only ensures // roughly equal distribution - int allowable_error = std::max(2, int(expected_bin_weight * 0.3)); + int allowable_error = std::max(2, static_cast(expected_bin_weight * 0.3)); // First and last bin can have smaller for (auto& kv : bin_weights) { - ASSERT_LE(std::abs(bin_weights[kv.first] - expected_bin_weight), - allowable_error); + ASSERT_LE(std::abs(bin_weights[kv.first] - expected_bin_weight), allowable_error); } } // Test sketch quantiles against the real quantiles Not a very strict // test -inline void TestRank(const std::vector &column_cuts, - const std::vector &sorted_x, - const std::vector &sorted_weights) { +inline void TestRank(const std::vector& column_cuts, const std::vector& sorted_x, + const std::vector& sorted_weights) { double eps = 0.05; - auto total_weight = - std::accumulate(sorted_weights.begin(), sorted_weights.end(), 0.0); + auto total_weight = std::accumulate(sorted_weights.begin(), sorted_weights.end(), 0.0); // Ignore the last cut, its special double sum_weight = 0.0; size_t j = 0; @@ -121,14 +121,12 @@ inline void TestRank(const std::vector &column_cuts, inline void ValidateColumn(const HistogramCuts& cuts, int column_idx, const std::vector& sorted_column, - const std::vector& sorted_weights, - size_t num_bins) { - + const std::vector& sorted_weights, size_t num_bins) { // Check the endpoints are correct CHECK_GT(sorted_column.size(), 0); EXPECT_LT(cuts.MinValues().at(column_idx), sorted_column.front()); EXPECT_GT(cuts.Values()[cuts.Ptrs()[column_idx]], sorted_column.front()); - EXPECT_GE(cuts.Values()[cuts.Ptrs()[column_idx+1]-1], sorted_column.back()); + EXPECT_GE(cuts.Values()[cuts.Ptrs()[column_idx + 1] - 1], sorted_column.back()); // Check the cuts are sorted auto cuts_begin = cuts.Values().begin() + cuts.Ptrs()[column_idx]; @@ -152,8 +150,7 @@ inline void ValidateColumn(const HistogramCuts& cuts, int column_idx, int num_cuts_column = cuts.Ptrs()[column_idx + 1] - cuts.Ptrs()[column_idx]; std::vector column_cuts(num_cuts_column); std::copy(cuts.Values().begin() + cuts.Ptrs()[column_idx], - cuts.Values().begin() + cuts.Ptrs()[column_idx + 1], - column_cuts.begin()); + cuts.Values().begin() + cuts.Ptrs()[column_idx + 1], column_cuts.begin()); TestBinDistribution(cuts, column_idx, sorted_column, sorted_weights); TestRank(column_cuts, sorted_column, sorted_weights); } @@ -204,8 +201,8 @@ inline void ValidateCuts(const HistogramCuts& cuts, DMatrix* dmat, int num_bins) * \param sketch Sketch function, can be on device or on host. */ template -void TestCategoricalSketch(size_t n, size_t num_categories, int32_t num_bins, - bool weighted, Fn sketch) { +void TestCategoricalSketch(size_t n, size_t num_categories, int32_t num_bins, bool weighted, + Fn sketch) { auto x = GenerateRandomCategoricalSingleColumn(n, num_categories); auto dmat = GetDMatrixFromData(x, n, 1); dmat->Info().feature_types.HostVector().push_back(FeatureType::kCategorical); diff --git a/tests/cpp/common/test_host_device_vector.cu b/tests/cpp/common/test_host_device_vector.cu index 7c3c2cd070cf..40e111c45b84 100644 --- a/tests/cpp/common/test_host_device_vector.cu +++ b/tests/cpp/common/test_host_device_vector.cu @@ -6,6 +6,10 @@ #include #include +#include +#include +#include + #include "../../../src/common/cuda_rt_utils.h" // for SetDevice #include "../../../src/common/device_helpers.cuh" @@ -25,12 +29,10 @@ struct HostDeviceVectorSetDeviceHandler { SetCudaSetDeviceHandler(f); } - ~HostDeviceVectorSetDeviceHandler() { - SetCudaSetDeviceHandler(nullptr); - } + ~HostDeviceVectorSetDeviceHandler() { SetCudaSetDeviceHandler(nullptr); } }; -void InitHostDeviceVector(size_t n, DeviceOrd device, HostDeviceVector *v) { +void InitHostDeviceVector(size_t n, DeviceOrd device, HostDeviceVector* v) { // create the vector v->SetDevice(device); v->Resize(n); @@ -55,40 +57,35 @@ void InitHostDeviceVector(size_t n, DeviceOrd device, HostDeviceVector *v) std::copy_n(thrust::make_counting_iterator(0), n, data_h.begin()); } -void PlusOne(HostDeviceVector *v) { +void PlusOne(HostDeviceVector* v) { auto device = v->Device(); SetDeviceForTest(device); thrust::transform(dh::tcbegin(*v), dh::tcend(*v), dh::tbegin(*v), - [=]__device__(unsigned int a){ return a + 1; }); + [=] __device__(unsigned int a) { return a + 1; }); ASSERT_TRUE(v->DeviceCanWrite()); } -void CheckDevice(HostDeviceVector* v, - size_t size, - unsigned int first, - GPUAccess access) { +void CheckDevice(HostDeviceVector* v, size_t size, unsigned int first, GPUAccess access) { ASSERT_EQ(v->Size(), size); SetDeviceForTest(v->Device()); - ASSERT_TRUE(thrust::equal(dh::tcbegin(*v), dh::tcend(*v), - thrust::make_counting_iterator(first))); + ASSERT_TRUE(thrust::equal(dh::tcbegin(*v), dh::tcend(*v), thrust::make_counting_iterator(first))); ASSERT_TRUE(v->DeviceCanRead()); // ensure that the device has at most the access specified by access ASSERT_EQ(v->DeviceCanWrite(), access == GPUAccess::kWrite); ASSERT_EQ(v->HostCanRead(), access == GPUAccess::kRead); ASSERT_FALSE(v->HostCanWrite()); - ASSERT_TRUE(thrust::equal(dh::tbegin(*v), dh::tend(*v), - thrust::make_counting_iterator(first))); + ASSERT_TRUE(thrust::equal(dh::tbegin(*v), dh::tend(*v), thrust::make_counting_iterator(first))); ASSERT_TRUE(v->DeviceCanRead()); ASSERT_TRUE(v->DeviceCanWrite()); ASSERT_FALSE(v->HostCanRead()); ASSERT_FALSE(v->HostCanWrite()); } -void CheckHost(HostDeviceVector *v, GPUAccess access) { - const std::vector& data_h = access == GPUAccess::kNone ? - v->HostVector() : v->ConstHostVector(); +void CheckHost(HostDeviceVector* v, GPUAccess access) { + const std::vector& data_h = + access == GPUAccess::kNone ? v->HostVector() : v->ConstHostVector(); for (size_t i = 0; i < v->Size(); ++i) { ASSERT_EQ(data_h.at(i), i + 1); } @@ -137,11 +134,11 @@ TEST(HostDeviceVector, Copy) { } TEST(HostDeviceVector, SetDevice) { - std::vector h_vec (2345); + std::vector h_vec(2345); for (size_t i = 0; i < h_vec.size(); ++i) { h_vec[i] = i; } - HostDeviceVector vec (h_vec); + HostDeviceVector vec(h_vec); auto device = DeviceOrd::CUDA(0); vec.SetDevice(device); @@ -157,7 +154,7 @@ TEST(HostDeviceVector, SetDevice) { } TEST(HostDeviceVector, Span) { - HostDeviceVector vec {1.0f, 2.0f, 3.0f, 4.0f}; + HostDeviceVector vec{1.0f, 2.0f, 3.0f, 4.0f}; vec.SetDevice(DeviceOrd::CUDA(0)); auto span = vec.DeviceSpan(); ASSERT_EQ(vec.Size(), span.size()); @@ -177,8 +174,8 @@ TEST(HostDeviceVector, Span) { } TEST(HostDeviceVector, Empty) { - HostDeviceVector vec {1.0f, 2.0f, 3.0f, 4.0f}; - HostDeviceVector another { std::move(vec) }; + HostDeviceVector vec{1.0f, 2.0f, 3.0f, 4.0f}; + HostDeviceVector another{std::move(vec)}; ASSERT_FALSE(another.Empty()); ASSERT_TRUE(vec.Empty()); } diff --git a/tests/cpp/common/test_intrusive_ptr.cc b/tests/cpp/common/test_intrusive_ptr.cc index 3b6f4083d55e..e574298c9ee4 100644 --- a/tests/cpp/common/test_intrusive_ptr.cc +++ b/tests/cpp/common/test_intrusive_ptr.cc @@ -4,6 +4,8 @@ #include #include +#include + namespace xgboost { namespace { class NotCopyConstructible { @@ -13,7 +15,7 @@ class NotCopyConstructible { explicit NotCopyConstructible(float d) : data{d} {} NotCopyConstructible(NotCopyConstructible const &that) = delete; NotCopyConstructible &operator=(NotCopyConstructible const &that) = delete; - NotCopyConstructible(NotCopyConstructible&& that) = default; + NotCopyConstructible(NotCopyConstructible &&that) = default; }; static_assert(!std::is_trivially_copy_constructible_v); static_assert(!std::is_trivially_copy_assignable_v); @@ -21,10 +23,9 @@ static_assert(!std::is_trivially_copy_assignable_v); class ForIntrusivePtrTest { public: mutable class IntrusivePtrCell ref; - float data { 0 }; + float data{0}; - friend IntrusivePtrCell & - IntrusivePtrRefCount(ForIntrusivePtrTest const *t) noexcept { // NOLINT + friend IntrusivePtrCell &IntrusivePtrRefCount(ForIntrusivePtrTest const *t) noexcept { // NOLINT return t->ref; } @@ -36,11 +37,11 @@ class ForIntrusivePtrTest { } // anonymous namespace TEST(IntrusivePtr, Basic) { - IntrusivePtr ptr {new ForIntrusivePtrTest}; + IntrusivePtr ptr{new ForIntrusivePtrTest}; auto p = ptr.get(); // Copy ctor - IntrusivePtr ptr_1 { ptr }; + IntrusivePtr ptr_1{ptr}; ASSERT_EQ(ptr_1.get(), p); ASSERT_EQ((*ptr_1).data, ptr_1->data); @@ -48,13 +49,13 @@ TEST(IntrusivePtr, Basic) { // hash ASSERT_EQ(std::hash>{}(ptr_1), - std::hash{}(ptr_1.get())); + std::hash{}(ptr_1.get())); // Raw ptr comparison ASSERT_EQ(ptr, p); ASSERT_EQ(ptr_1, ptr); - ForIntrusivePtrTest* raw_ptr {nullptr}; + ForIntrusivePtrTest *raw_ptr{nullptr}; ASSERT_NE(ptr_1, raw_ptr); ASSERT_NE(raw_ptr, ptr_1); @@ -98,7 +99,7 @@ TEST(IntrusivePtr, Basic) { ASSERT_EQ(ptr_3.use_count(), 2); // Move ctor - IntrusivePtr ptr_4 { std::move(ptr_3) }; + IntrusivePtr ptr_4{std::move(ptr_3)}; ASSERT_EQ(ptr_3.use_count(), 0); // NOLINT ASSERT_EQ(ptr_4.use_count(), 2); @@ -107,4 +108,4 @@ TEST(IntrusivePtr, Basic) { ASSERT_EQ(ptr_1, ptr_1); ASSERT_EQ(ptr_1 < ptr_2, ptr_1.get() < ptr_2.get()); } -} // namespace xgboost +} // namespace xgboost diff --git a/tests/cpp/common/test_io.cc b/tests/cpp/common/test_io.cc index 0d0a6900dc4e..9671c7fc5c89 100644 --- a/tests/cpp/common/test_io.cc +++ b/tests/cpp/common/test_io.cc @@ -5,7 +5,11 @@ #include // for size_t #include // for ofstream +#include #include // for iota +#include +#include +#include #include "../../../src/common/io.h" #include "../filesystem.h" // TemporaryDirectory diff --git a/tests/cpp/common/test_json.cc b/tests/cpp/common/test_json.cc index a5b803354840..4fcee922394c 100644 --- a/tests/cpp/common/test_json.cc +++ b/tests/cpp/common/test_json.cc @@ -7,6 +7,9 @@ #include // for numeric_limits #include #include // for iota +#include +#include +#include #include "../../../src/common/io.h" #include "../../../src/common/json_utils.h" @@ -249,12 +252,12 @@ TEST(Json, ParseArray) { } TEST(Json, Null) { - Json json {JsonNull()}; + Json json{JsonNull()}; std::string ss; Json::Dump(json, &ss); ASSERT_EQ(ss, "null"); - std::string null_input {R"null({"key": null })null"}; + std::string null_input{R"null({"key": null })null"}; json = Json::Load({null_input.c_str(), null_input.size()}); ASSERT_TRUE(IsA(json["key"])); @@ -277,7 +280,7 @@ TEST(Json, EmptyObject) { auto json = Json::Load(StringView{str.c_str(), str.size()}); ASSERT_TRUE(IsA(json["statistic"])); - str = R"json({"Config": {},"Model": {}})json"; // NOLINT + str = R"json({"Config": {},"Model": {}})json"; // NOLINT json = Json::Load(StringView{str.c_str(), str.size()}); ASSERT_TRUE(IsA(json["Model"])); } @@ -301,7 +304,7 @@ TEST(Json, Boolean) { "right_child": false } )json"; - Json j {Json::Load(StringView{str.c_str(), str.size()})}; + Json j{Json::Load(StringView{str.c_str(), str.size()})}; ASSERT_EQ(get(j["left_child"]), true); ASSERT_EQ(get(j["right_child"]), false); @@ -313,7 +316,7 @@ TEST(Json, Boolean) { TEST(Json, Indexing) { auto str = GetModelStr(); JsonReader reader(StringView{str.c_str(), str.size()}); - Json j {Json::Load(&reader)}; + Json j{Json::Load(&reader)}; auto& value_1 = j["model_parameter"]; auto& value = value_1["base_score"]; std::string result = Cast(&value.GetValue())->GetString(); @@ -331,18 +334,18 @@ TEST(Json, AssigningObjects) { { std::map objects; - Json json_objects { JsonObject() }; - std::vector arr_0 (1, Json(3.3f)); + Json json_objects{JsonObject()}; + std::vector arr_0(1, Json(3.3f)); json_objects["tree_parameters"] = JsonArray(arr_0); std::vector json_arr = get(json_objects["tree_parameters"]); ASSERT_NEAR(get(json_arr[0]), 3.3f, kRtEps); } { - Json json_object { JsonObject() }; + Json json_object{JsonObject()}; auto str = JsonString("1"); auto& k = json_object["1"]; - k = std::move(str); + k = std::move(str); ASSERT_TRUE(str.GetString().empty()); // NOLINT auto& m = json_object["1"]; std::string value = get(m); @@ -354,9 +357,9 @@ TEST(Json, AssigningObjects) { TEST(Json, AssigningArray) { Json json; json = JsonArray(); - std::vector tmp_0 {Json(Number(1.0f)), Json(Number(2.0f))}; + std::vector tmp_0{Json(Number(1.0f)), Json(Number(2.0f))}; json = tmp_0; - std::vector tmp_1 {Json(Number(3.0f))}; + std::vector tmp_1{Json(Number(3.0f))}; get(json) = tmp_1; std::vector res = get(json); ASSERT_EQ(get(res[0]), 3); @@ -365,14 +368,14 @@ TEST(Json, AssigningArray) { TEST(Json, AssigningNumber) { { // right value - Json json = Json{ Number(4.0f) }; + Json json = Json{Number(4.0f)}; get(json) = 15; ASSERT_EQ(get(json), 15); } { // left value ref - Json json = Json{ Number(4.0f) }; + Json json = Json{Number(4.0f)}; Number::Float& ref = get(json); ref = 15; ASSERT_EQ(get(json), 15); @@ -380,7 +383,7 @@ TEST(Json, AssigningNumber) { { // left value - Json json = Json{ Number(4.0f) }; + Json json = Json{Number(4.0f)}; double value = get(json); ASSERT_EQ(value, 4); value = 15; // NOLINT @@ -388,7 +391,7 @@ TEST(Json, AssigningNumber) { } { - Json value {Number(std::numeric_limits::quiet_NaN())}; + Json value{Number(std::numeric_limits::quiet_NaN())}; ASSERT_TRUE(IsA(value)); } } @@ -396,14 +399,14 @@ TEST(Json, AssigningNumber) { TEST(Json, AssigningString) { { // right value - Json json = Json{ String("str") }; + Json json = Json{String("str")}; get(json) = "modified"; ASSERT_EQ(get(json), "modified"); } { // left value ref - Json json = Json{ String("str") }; + Json json = Json{String("str")}; std::string& ref = get(json); ref = "modified"; ASSERT_EQ(get(json), "modified"); @@ -411,7 +414,7 @@ TEST(Json, AssigningString) { { // left value - Json json = Json{ String("str") }; + Json json = Json{String("str")}; std::string value = get(json); value = "modified"; ASSERT_EQ(get(json), "str"); @@ -444,11 +447,11 @@ TEST(Json, Invalid) { bool has_thrown = false; try { Json load{Json::Load(StringView(str.c_str(), str.size()))}; - } catch (dmlc::Error const &e) { + } catch (dmlc::Error const& e) { std::string msg = e.what(); ASSERT_NE(msg.find("Unknown"), std::string::npos); has_thrown = true; - }; + } ASSERT_TRUE(has_thrown); } { @@ -456,11 +459,11 @@ TEST(Json, Invalid) { bool has_thrown = false; try { Json load{Json::Load(StringView(str.c_str(), str.size()))}; - } catch (dmlc::Error const &e) { + } catch (dmlc::Error const& e) { std::string msg = e.what(); ASSERT_NE(msg.find("position: 1"), std::string::npos); has_thrown = true; - }; + } ASSERT_TRUE(has_thrown); } { @@ -473,7 +476,7 @@ TEST(Json, Invalid) { // EOF is printed as 255 on s390x ASSERT_TRUE(msg.find("EOF") != std::string::npos || msg.find("255") != std::string::npos); has_thrown = true; - }; + } ASSERT_TRUE(has_thrown); } } @@ -483,7 +486,7 @@ TEST(Json, CopyUnicode) { std::string json_str = R"json( {"m": ["\ud834\udd1e", "\u20ac", "\u0416", "\u00f6"]} )json"; - Json loaded {Json::Load(StringView{json_str.c_str(), json_str.size()})}; + Json loaded{Json::Load(StringView{json_str.c_str(), json_str.size()})}; std::string dumped_string; Json::Dump(loaded, &dumped_string); @@ -493,11 +496,11 @@ TEST(Json, CopyUnicode) { TEST(Json, WrongCasts) { { - Json json = Json{ String{"str"} }; + Json json = Json{String{"str"}}; ASSERT_ANY_THROW(get(json)); } { - Json json = Json{ Array{ std::vector{ Json{ Number{1.0f} } } } }; + Json json = Json{Array{std::vector{Json{Number{1.0f}}}}}; ASSERT_ANY_THROW(get(json)); } { @@ -742,11 +745,12 @@ TEST(UBJson, Basic) { } } - TEST(Json, TypeCheck) { Json config{Object{}}; config["foo"] = String{"bar"}; - auto test = [&]() { TypeCheck(config["foo"], "foo"); }; + auto test = [&]() { + TypeCheck(config["foo"], "foo"); + }; ASSERT_THROW({ test(); }, dmlc::Error); try { test(); diff --git a/tests/cpp/common/test_linalg.cc b/tests/cpp/common/test_linalg.cc index b9228f31226c..6cb5c78d9661 100644 --- a/tests/cpp/common/test_linalg.cc +++ b/tests/cpp/common/test_linalg.cc @@ -1,6 +1,8 @@ /** * Copyright 2021-2026, XGBoost Contributors */ +#include "test_linalg.h" // for TestLinalgDispatch + #include #include #include // for HostDeviceVector @@ -8,10 +10,10 @@ #include // size_t #include // iota +#include // for move #include // for vector #include "../../../src/common/linalg_op.h" -#include "test_linalg.h" // for TestLinalgDispatch namespace xgboost::linalg { namespace { diff --git a/tests/cpp/common/test_linalg.cu b/tests/cpp/common/test_linalg.cu index 6a34513db5b1..364408b888e6 100644 --- a/tests/cpp/common/test_linalg.cu +++ b/tests/cpp/common/test_linalg.cu @@ -6,6 +6,8 @@ #include // for make_constant_iterator #include // for sequence +#include + #include "../../../src/common/cuda_context.cuh" #include "../../../src/common/linalg_op.h" #include "../../../src/common/optional_weight.h" // for MakeOptionalWeights diff --git a/tests/cpp/common/test_math.cc b/tests/cpp/common/test_math.cc index 6ff3107b39df..b7cbf85ef82f 100644 --- a/tests/cpp/common/test_math.cc +++ b/tests/cpp/common/test_math.cc @@ -2,7 +2,9 @@ * Copyright 2025, XGBoost Contributors */ #include + #include // for accumulate +#include #include "../../../src/common/math.h" diff --git a/tests/cpp/common/test_monitor.cc b/tests/cpp/common/test_monitor.cc index bc918af643bd..51e8b114ac4d 100644 --- a/tests/cpp/common/test_monitor.cc +++ b/tests/cpp/common/test_monitor.cc @@ -1,18 +1,23 @@ +/** + * Copyright 2026, XGBoost contributors + */ #include #include + #include +#include + #include "../../../src/common/timer.h" namespace xgboost { namespace common { TEST(Monitor, Logging) { - auto run_monitor = - []() { - Monitor monitor_; - monitor_.Init("Monitor test"); - monitor_.Start("basic"); - monitor_.Stop("basic"); - }; + auto run_monitor = []() { + Monitor monitor_; + monitor_.Init("Monitor test"); + monitor_.Start("basic"); + monitor_.Stop("basic"); + }; Args args = {std::make_pair("verbosity", "3")}; ConsoleLogger::Configure(args); diff --git a/tests/cpp/common/test_numa_topo.cc b/tests/cpp/common/test_numa_topo.cc index b0e904bf30a5..9643a675fb42 100644 --- a/tests/cpp/common/test_numa_topo.cc +++ b/tests/cpp/common/test_numa_topo.cc @@ -5,7 +5,8 @@ #include // for path #include // for ofstream -#include // for vector +#include +#include // for vector #include "../../../src/common/numa_topo.h" #include "../filesystem.h" // for TemporaryDirectory diff --git a/tests/cpp/common/test_numeric.cc b/tests/cpp/common/test_numeric.cc index 2a91d2d72a78..badcb3e40ab1 100644 --- a/tests/cpp/common/test_numeric.cc +++ b/tests/cpp/common/test_numeric.cc @@ -4,6 +4,7 @@ #include #include +#include #include "../../../src/common/numeric.h" diff --git a/tests/cpp/common/test_parameter.cc b/tests/cpp/common/test_parameter.cc index bcd1dc98f6f2..9458d15f5ace 100644 --- a/tests/cpp/common/test_parameter.cc +++ b/tests/cpp/common/test_parameter.cc @@ -5,11 +5,12 @@ #include #include +#include +#include + #include "xgboost/json.h" // for ToJson, FromJson -enum class Foo : int { - kBar = 0, kFrog = 1, kCat = 2, kDog = 3 -}; +enum class Foo : int { kBar = 0, kFrog = 1, kCat = 2, kDog = 3 }; DECLARE_FIELD_ENUM_CLASS(Foo); @@ -18,13 +19,12 @@ struct MyEnumParam : xgboost::XGBoostParameter { int bar; DMLC_DECLARE_PARAMETER(MyEnumParam) { DMLC_DECLARE_FIELD(foo) - .set_default(Foo::kBar) - .add_enum("bar", Foo::kBar) - .add_enum("frog", Foo::kFrog) - .add_enum("cat", Foo::kCat) - .add_enum("dog", Foo::kDog); - DMLC_DECLARE_FIELD(bar) - .set_default(-1); + .set_default(Foo::kBar) + .add_enum("bar", Foo::kBar) + .add_enum("frog", Foo::kFrog) + .add_enum("cat", Foo::kCat) + .add_enum("dog", Foo::kDog); + DMLC_DECLARE_FIELD(bar).set_default(-1); } }; @@ -32,11 +32,9 @@ DMLC_REGISTER_PARAMETER(MyEnumParam); TEST(EnumClassParam, Basic) { MyEnumParam param; - std::map kwargs{ - {"foo", "frog"}, {"bar", "10"} - }; + std::map kwargs{{"foo", "frog"}, {"bar", "10"}}; // try initializing - param.Init(kwargs); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult) + param.Init(kwargs); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult) ASSERT_EQ(param.foo, Foo::kFrog); ASSERT_EQ(param.bar, 10); @@ -60,14 +58,12 @@ TEST(EnumClassParam, Basic) { } struct UpdatableParam : xgboost::XGBoostParameter { - float f { 0.0f }; - double d { 0.0 }; + float f{0.0f}; + double d{0.0}; DMLC_DECLARE_PARAMETER(UpdatableParam) { - DMLC_DECLARE_FIELD(f) - .set_default(11.0f); - DMLC_DECLARE_FIELD(d) - .set_default(2.71828f); + DMLC_DECLARE_FIELD(f).set_default(11.0f); + DMLC_DECLARE_FIELD(d).set_default(2.71828f); } }; diff --git a/tests/cpp/common/test_partition_builder.cc b/tests/cpp/common/test_partition_builder.cc index 36fb7a8d9870..2aae90e1fcc9 100644 --- a/tests/cpp/common/test_partition_builder.cc +++ b/tests/cpp/common/test_partition_builder.cc @@ -17,38 +17,36 @@ TEST(PartitionBuilder, BasicTest) { constexpr size_t kNodes = 5; constexpr size_t kTasks = 3 + 5 + 10 + 1 + 2; - std::vector tasks = { 3, 5, 10, 1, 2 }; + std::vector tasks = {3, 5, 10, 1, 2}; PartitionBuilder builder; - builder.Init(kTasks, kNodes, [&](size_t i) { - return tasks[i]; - }); + builder.Init(kTasks, kNodes, [&](size_t i) { return tasks[i]; }); - std::vector rows_for_left_node = { 2, 12, 0, 16, 8 }; + std::vector rows_for_left_node = {2, 12, 0, 16, 8}; - for(size_t nid = 0; nid < kNodes; ++nid) { + for (size_t nid = 0; nid < kNodes; ++nid) { size_t value_left = 0; size_t value_right = 0; size_t left_total = tasks[nid] * rows_for_left_node[nid]; - for(size_t j = 0; j < tasks[nid]; ++j) { - size_t begin = kBlockSize*j; - size_t end = kBlockSize*(j+1); + for (size_t j = 0; j < tasks[nid]; ++j) { + size_t begin = kBlockSize * j; + size_t end = kBlockSize * (j + 1); const size_t id = builder.GetTaskIdx(nid, begin); builder.AllocateForTask(id); - auto left = builder.GetLeftBuffer(nid, begin, end); + auto left = builder.GetLeftBuffer(nid, begin, end); auto right = builder.GetRightBuffer(nid, begin, end); - size_t n_left = rows_for_left_node[nid]; + size_t n_left = rows_for_left_node[nid]; size_t n_right = kBlockSize - rows_for_left_node[nid]; - for(size_t i = 0; i < n_left; i++) { + for (size_t i = 0; i < n_left; i++) { left[i] = value_left++; } - for(size_t i = 0; i < n_right; i++) { + for (size_t i = 0; i < n_right; i++) { right[i] = left_total + value_right++; } @@ -60,16 +58,15 @@ TEST(PartitionBuilder, BasicTest) { std::vector v(*std::max_element(tasks.begin(), tasks.end()) * kBlockSize); - for(size_t nid = 0; nid < kNodes; ++nid) { - - for(size_t j = 0; j < tasks[nid]; ++j) { - builder.MergeToArray(nid, kBlockSize*j, v.data()); + for (size_t nid = 0; nid < kNodes; ++nid) { + for (size_t j = 0; j < tasks[nid]; ++j) { + builder.MergeToArray(nid, kBlockSize * j, v.data()); } - for(size_t j = 0; j < tasks[nid] * kBlockSize; ++j) { + for (size_t j = 0; j < tasks[nid] * kBlockSize; ++j) { ASSERT_EQ(v[j], j); } - size_t n_left = builder.GetNLeftElems(nid); + size_t n_left = builder.GetNLeftElems(nid); size_t n_right = builder.GetNRightElems(nid); ASSERT_EQ(n_left, rows_for_left_node[nid] * tasks[nid]); diff --git a/tests/cpp/common/test_probability_distribution.cc b/tests/cpp/common/test_probability_distribution.cc index e8aebede6180..8caeadb848cd 100644 --- a/tests/cpp/common/test_probability_distribution.cc +++ b/tests/cpp/common/test_probability_distribution.cc @@ -2,11 +2,12 @@ * Copyright (c) by Contributors 2020 */ #include -#include + #include +#include -#include "xgboost/logging.h" #include "../../../src/common/probability_distribution.h" +#include "xgboost/logging.h" namespace xgboost { namespace common { @@ -93,7 +94,7 @@ TEST(ProbabilityDistribution, ExtremeDist) { const double x = static_cast(i) / 1000.0 - 20.0; // Numerical integration using Trapezoid Rule (p. 257, Sauer) mean += - 5e-4 * ((x - 1e-3) * ExtremeDistribution::PDF(x - 1e-3) + x * ExtremeDistribution::PDF(x)); + 5e-4 * ((x - 1e-3) * ExtremeDistribution::PDF(x - 1e-3) + x * ExtremeDistribution::PDF(x)); } EXPECT_NEAR(mean, -kEulerMascheroni, 1e-7); @@ -105,11 +106,11 @@ TEST(ProbabilityDistribution, ExtremeDist) { for (int i = 0; i <= 25000; ++i) { const double x = static_cast(i) / 1000.0 - 20.0; // Numerical integration using Trapezoid Rule (p. 257, Sauer) - variance += 5e-4 * ((x - 1e-3 - mean) * (x - 1e-3 - mean) * ExtremeDistribution::PDF(x - 1e-3) - + (x - mean) * (x - mean) * ExtremeDistribution::PDF(x)); + variance += 5e-4 * ((x - 1e-3 - mean) * (x - 1e-3 - mean) * ExtremeDistribution::PDF(x - 1e-3) + + (x - mean) * (x - mean) * ExtremeDistribution::PDF(x)); } EXPECT_NEAR(variance, kPI * kPI / 6.0, 1e-6); } -} // namespace common +} // namespace common } // namespace xgboost diff --git a/tests/cpp/common/test_quantile.cc b/tests/cpp/common/test_quantile.cc index 1ef6572599fc..8d6a7e7a9109 100644 --- a/tests/cpp/common/test_quantile.cc +++ b/tests/cpp/common/test_quantile.cc @@ -5,7 +5,11 @@ #include +#include #include // for int64_t +#include +#include +#include #include "../../../src/collective/allreduce.h" #include "../../../src/common/hist_util.h" @@ -309,81 +313,79 @@ void TestSameOnAllWorkers() { constexpr size_t kRows = 1000, kCols = 100; Context ctx; - RunWithSeedsAndBins( - kRows, [=, &ctx](int32_t seed, size_t n_bins, MetaInfo const&) { - auto rank = collective::GetRank(); - HostDeviceVector storage; - std::vector ft(kCols); - for (size_t i = 0; i < ft.size(); ++i) { - ft[i] = (i % 2 == 0) ? FeatureType::kNumerical : FeatureType::kCategorical; - } - - auto m = RandomDataGenerator{kRows, kCols, 0} - .Device(DeviceOrd::CPU()) - .Type(ft) - .MaxCategory(17) - .Seed(rank + seed) - .GenerateDMatrix(); - auto cuts = SketchOnDMatrix(&ctx, m.get(), n_bins); - std::vector cut_values(cuts.Values().size() * world, 0); - std::vector< - typename std::remove_reference_t::value_type> - cut_ptrs(cuts.Ptrs().size() * world, 0); - std::vector cut_min_values(cuts.MinValues().size() * world, 0); - - std::int64_t value_size = cuts.Values().size(); - std::int64_t ptr_size = cuts.Ptrs().size(); - std::int64_t min_value_size = cuts.MinValues().size(); - - auto rc = collective::Success() << [&] { - return collective::Allreduce(&ctx, &value_size, collective::Op::kMax); - } << [&] { - return collective::Allreduce(&ctx, &ptr_size, collective::Op::kMax); - } << [&] { - return collective::Allreduce(&ctx, &min_value_size, collective::Op::kMax); - }; - collective::SafeColl(rc); - ASSERT_EQ(ptr_size, kCols + 1); - ASSERT_EQ(min_value_size, kCols); - - std::size_t value_offset = value_size * rank; - std::copy(cuts.Values().begin(), cuts.Values().end(), cut_values.begin() + value_offset); - std::size_t ptr_offset = ptr_size * rank; - std::copy(cuts.Ptrs().cbegin(), cuts.Ptrs().cend(), cut_ptrs.begin() + ptr_offset); - std::size_t min_values_offset = min_value_size * rank; - std::copy(cuts.MinValues().cbegin(), cuts.MinValues().cend(), - cut_min_values.begin() + min_values_offset); - - rc = std::move(rc) << [&] { - return collective::Allreduce(&ctx, linalg::MakeVec(cut_values.data(), cut_values.size()), - collective::Op::kSum); - } << [&] { - return collective::Allreduce(&ctx, linalg::MakeVec(cut_ptrs.data(), cut_ptrs.size()), - collective::Op::kSum); - } << [&] { - return collective::Allreduce( - &ctx, linalg::MakeVec(cut_min_values.data(), cut_min_values.size()), - collective::Op::kSum); - }; - collective::SafeColl(rc); - - for (std::int32_t i = 0; i < world; i++) { - for (std::int64_t j = 0; j < value_size; ++j) { - size_t idx = i * value_size + j; - ASSERT_NEAR(cuts.Values().at(j), cut_values.at(idx), kRtEps); - } - - for (std::int64_t j = 0; j < ptr_size; ++j) { - size_t idx = i * ptr_size + j; - EXPECT_EQ(cuts.Ptrs().at(j), cut_ptrs.at(idx)); - } - - for (std::int64_t j = 0; j < min_value_size; ++j) { - size_t idx = i * min_value_size + j; - ASSERT_EQ(cuts.MinValues().at(j), cut_min_values.at(idx)); - } - } - }); + RunWithSeedsAndBins(kRows, [=, &ctx](int32_t seed, size_t n_bins, MetaInfo const&) { + auto rank = collective::GetRank(); + HostDeviceVector storage; + std::vector ft(kCols); + for (size_t i = 0; i < ft.size(); ++i) { + ft[i] = (i % 2 == 0) ? FeatureType::kNumerical : FeatureType::kCategorical; + } + + auto m = RandomDataGenerator{kRows, kCols, 0} + .Device(DeviceOrd::CPU()) + .Type(ft) + .MaxCategory(17) + .Seed(rank + seed) + .GenerateDMatrix(); + auto cuts = SketchOnDMatrix(&ctx, m.get(), n_bins); + std::vector cut_values(cuts.Values().size() * world, 0); + std::vector::value_type> cut_ptrs( + cuts.Ptrs().size() * world, 0); + std::vector cut_min_values(cuts.MinValues().size() * world, 0); + + std::int64_t value_size = cuts.Values().size(); + std::int64_t ptr_size = cuts.Ptrs().size(); + std::int64_t min_value_size = cuts.MinValues().size(); + + auto rc = collective::Success() << [&] { + return collective::Allreduce(&ctx, &value_size, collective::Op::kMax); + } << [&] { + return collective::Allreduce(&ctx, &ptr_size, collective::Op::kMax); + } << [&] { + return collective::Allreduce(&ctx, &min_value_size, collective::Op::kMax); + }; + collective::SafeColl(rc); + ASSERT_EQ(ptr_size, kCols + 1); + ASSERT_EQ(min_value_size, kCols); + + std::size_t value_offset = value_size * rank; + std::copy(cuts.Values().begin(), cuts.Values().end(), cut_values.begin() + value_offset); + std::size_t ptr_offset = ptr_size * rank; + std::copy(cuts.Ptrs().cbegin(), cuts.Ptrs().cend(), cut_ptrs.begin() + ptr_offset); + std::size_t min_values_offset = min_value_size * rank; + std::copy(cuts.MinValues().cbegin(), cuts.MinValues().cend(), + cut_min_values.begin() + min_values_offset); + + rc = std::move(rc) << [&] { + return collective::Allreduce(&ctx, linalg::MakeVec(cut_values.data(), cut_values.size()), + collective::Op::kSum); + } << [&] { + return collective::Allreduce(&ctx, linalg::MakeVec(cut_ptrs.data(), cut_ptrs.size()), + collective::Op::kSum); + } << [&] { + return collective::Allreduce(&ctx, + linalg::MakeVec(cut_min_values.data(), cut_min_values.size()), + collective::Op::kSum); + }; + collective::SafeColl(rc); + + for (std::int32_t i = 0; i < world; i++) { + for (std::int64_t j = 0; j < value_size; ++j) { + size_t idx = i * value_size + j; + ASSERT_NEAR(cuts.Values().at(j), cut_values.at(idx), kRtEps); + } + + for (std::int64_t j = 0; j < ptr_size; ++j) { + size_t idx = i * ptr_size + j; + EXPECT_EQ(cuts.Ptrs().at(j), cut_ptrs.at(idx)); + } + + for (std::int64_t j = 0; j < min_value_size; ++j) { + size_t idx = i * min_value_size + j; + ASSERT_EQ(cuts.MinValues().at(j), cut_min_values.at(idx)); + } + } + }); } } // anonymous namespace diff --git a/tests/cpp/common/test_quantile.cu b/tests/cpp/common/test_quantile.cu index 7be12ac9c908..c567259a9bfb 100644 --- a/tests/cpp/common/test_quantile.cu +++ b/tests/cpp/common/test_quantile.cu @@ -3,6 +3,12 @@ */ #include +#include +#include +#include +#include +#include + #include "../../../src/collective/allreduce.h" #include "../../../src/common/hist_util.cuh" #include "../../../src/common/quantile.cuh" @@ -18,7 +24,7 @@ struct IsSorted { return a.value < b.value; } }; -} +} // namespace namespace common { class MGPUQuantileTest : public collective::BaseMGPUTest {}; @@ -29,7 +35,7 @@ TEST(GPUQuantile, Basic) { HostDeviceVector ft; SketchContainer sketch(ft, kBins, kCols, kRows, ctx.Device()); dh::caching_device_vector entries; - dh::device_vector cuts_ptr(kCols+1); + dh::device_vector cuts_ptr(kCols + 1); thrust::fill(cuts_ptr.begin(), cuts_ptr.end(), 0); // Push empty sketch.Push(&ctx, dh::ToSpan(entries), dh::ToSpan(cuts_ptr), dh::ToSpan(cuts_ptr), 0); @@ -38,51 +44,51 @@ TEST(GPUQuantile, Basic) { void TestSketchUnique(float sparsity) { constexpr size_t kRows = 1000, kCols = 100; - RunWithSeedsAndBins(kRows, [kRows, kCols, sparsity](std::int32_t seed, bst_bin_t n_bins, - MetaInfo const& info) { - auto ctx = MakeCUDACtx(0); - HostDeviceVector ft; - SketchContainer sketch(ft, n_bins, kCols, kRows, ctx.Device()); - - HostDeviceVector storage; - std::string interface_str = RandomDataGenerator{kRows, kCols, sparsity} - .Seed(seed) - .Device(ctx.Device()) - .GenerateArrayInterface(&storage); - data::CupyAdapter adapter(interface_str); - AdapterDeviceSketch(&ctx, adapter.Value(), n_bins, info, - std::numeric_limits::quiet_NaN(), &sketch); - auto n_cuts = detail::RequiredSampleCutsPerColumn(n_bins, kRows); - - dh::caching_device_vector column_sizes_scan; - HostDeviceVector cut_sizes_scan; - auto batch = adapter.Value(); - data::IsValidFunctor is_valid(std::numeric_limits::quiet_NaN()); - auto batch_iter = dh::MakeTransformIterator( - thrust::make_counting_iterator(0llu), - [=] __device__(size_t idx) { return batch.GetElement(idx); }); - auto end = kCols * kRows; - detail::GetColumnSizesScan(ctx.CUDACtx(), ctx.Device(), kCols, n_cuts, - IterSpan{batch_iter, end}, is_valid, &cut_sizes_scan, - &column_sizes_scan); - auto const& cut_sizes = cut_sizes_scan.HostVector(); - ASSERT_LE(sketch.Data().size(), cut_sizes.back()); - - std::vector h_columns_ptr(sketch.ColumnsPtr().size()); - dh::CopyDeviceSpanToVector(&h_columns_ptr, sketch.ColumnsPtr()); - ASSERT_EQ(sketch.Data().size(), h_columns_ptr.back()); - - sketch.Unique(&ctx); - - std::vector h_data(sketch.Data().size()); - thrust::copy(dh::tcbegin(sketch.Data()), dh::tcend(sketch.Data()), h_data.begin()); - - for (size_t i = 1; i < h_columns_ptr.size(); ++i) { - auto begin = h_columns_ptr[i - 1]; - auto column = common::Span(h_data).subspan(begin, h_columns_ptr[i] - begin); - ASSERT_TRUE(std::is_sorted(column.begin(), column.end(), IsSorted{})); - } - }); + RunWithSeedsAndBins( + kRows, [kRows, kCols, sparsity](std::int32_t seed, bst_bin_t n_bins, MetaInfo const& info) { + auto ctx = MakeCUDACtx(0); + HostDeviceVector ft; + SketchContainer sketch(ft, n_bins, kCols, kRows, ctx.Device()); + + HostDeviceVector storage; + std::string interface_str = RandomDataGenerator{kRows, kCols, sparsity} + .Seed(seed) + .Device(ctx.Device()) + .GenerateArrayInterface(&storage); + data::CupyAdapter adapter(interface_str); + AdapterDeviceSketch(&ctx, adapter.Value(), n_bins, info, + std::numeric_limits::quiet_NaN(), &sketch); + auto n_cuts = detail::RequiredSampleCutsPerColumn(n_bins, kRows); + + dh::caching_device_vector column_sizes_scan; + HostDeviceVector cut_sizes_scan; + auto batch = adapter.Value(); + data::IsValidFunctor is_valid(std::numeric_limits::quiet_NaN()); + auto batch_iter = dh::MakeTransformIterator( + thrust::make_counting_iterator(0llu), + [=] __device__(size_t idx) { return batch.GetElement(idx); }); + auto end = kCols * kRows; + detail::GetColumnSizesScan(ctx.CUDACtx(), ctx.Device(), kCols, n_cuts, + IterSpan{batch_iter, end}, is_valid, &cut_sizes_scan, + &column_sizes_scan); + auto const& cut_sizes = cut_sizes_scan.HostVector(); + ASSERT_LE(sketch.Data().size(), cut_sizes.back()); + + std::vector h_columns_ptr(sketch.ColumnsPtr().size()); + dh::CopyDeviceSpanToVector(&h_columns_ptr, sketch.ColumnsPtr()); + ASSERT_EQ(sketch.Data().size(), h_columns_ptr.back()); + + sketch.Unique(&ctx); + + std::vector h_data(sketch.Data().size()); + thrust::copy(dh::tcbegin(sketch.Data()), dh::tcend(sketch.Data()), h_data.begin()); + + for (size_t i = 1; i < h_columns_ptr.size(); ++i) { + auto begin = h_columns_ptr[i - 1]; + auto column = common::Span(h_data).subspan(begin, h_columns_ptr[i] - begin); + ASSERT_TRUE(std::is_sorted(column.begin(), column.end(), IsSorted{})); + } + }); } TEST(GPUQuantile, Unique) { @@ -110,11 +116,9 @@ void TestQuantileElemRank(DeviceOrd device, Span in, float prev_rmax = in_column[idx - 1].rmax; float rmin_next = in_column[idx].RMinNext(); if (with_error) { - ASSERT_GE(in_column[idx].rmin + in_column[idx].rmin * kRtEps, - prev_rmin); + ASSERT_GE(in_column[idx].rmin + in_column[idx].rmin * kRtEps, prev_rmin); ASSERT_GE(in_column[idx].rmax + in_column[idx].rmin * kRtEps, prev_rmax); - ASSERT_GE(in_column[idx].rmax + in_column[idx].rmin * kRtEps, - rmin_next); + ASSERT_GE(in_column[idx].rmax + in_column[idx].rmin * kRtEps, rmin_next); } else { ASSERT_GE(in_column[idx].rmin, prev_rmin); ASSERT_GE(in_column[idx].rmax, prev_rmax); @@ -236,10 +240,9 @@ TEST(GPUQuantile, MergeBasic) { ASSERT_EQ(h_columns_ptr.back(), sketch_1.Data().size() + size_before_merge); sketch_0.Unique(&ctx); - ASSERT_TRUE( - thrust::is_sorted(thrust::device, sketch_0.Data().data(), - sketch_0.Data().data() + sketch_0.Data().size(), - detail::SketchUnique{})); + ASSERT_TRUE(thrust::is_sorted(thrust::device, sketch_0.Data().data(), + sketch_0.Data().data() + sketch_0.Data().size(), + detail::SketchUnique{})); }); } @@ -266,8 +269,7 @@ void TestMergeDuplicated(int32_t n_bins, size_t cols, size_t rows, float frac) { .Seed(seed) .GenerateArrayInterface(&storage_1); auto data_1 = storage_1.DeviceSpan(); - auto tuple_it = thrust::make_tuple( - thrust::make_counting_iterator(0ul), data_1.data()); + auto tuple_it = thrust::make_tuple(thrust::make_counting_iterator(0ul), data_1.data()); using Tuple = thrust::tuple; auto it = thrust::make_zip_iterator(tuple_it); thrust::transform(thrust::device, it, it + data_1.size(), data_1.data(), @@ -300,7 +302,7 @@ void TestMergeDuplicated(int32_t n_bins, size_t cols, size_t rows, float frac) { dh::CopyDeviceSpanToVector(&h_data, sketch_0.Data()); for (size_t i = 1; i < h_columns_ptr.size(); ++i) { auto begin = h_columns_ptr[i - 1]; - auto column = Span {h_data}.subspan(begin, h_columns_ptr[i] - begin); + auto column = Span{h_data}.subspan(begin, h_columns_ptr[i] - begin); ASSERT_TRUE(std::is_sorted(column.begin(), column.end(), IsSorted{})); } } @@ -322,8 +324,8 @@ TEST(GPUQuantile, MultiMerge) { auto ctx = MakeCUDACtx(0); SketchContainer sketch_on_single_node(ft, n_bins, kCols, kRows, ctx.Device()); - size_t intermediate_num_cuts = std::min( - kRows * world, static_cast(n_bins * WQSketch::kFactor)); + size_t intermediate_num_cuts = + std::min(kRows * world, static_cast(n_bins * WQSketch::kFactor)); std::vector containers; for (auto rank = 0; rank < world; ++rank) { HostDeviceVector storage; @@ -337,7 +339,7 @@ TEST(GPUQuantile, MultiMerge) { AdapterDeviceSketch(&ctx, adapter.Value(), n_bins, info, std::numeric_limits::quiet_NaN(), &containers.back()); } - for (auto &sketch : containers) { + for (auto& sketch : containers) { sketch.Prune(&ctx, intermediate_num_cuts); sketch_on_single_node.Merge(&ctx, sketch.ColumnsPtr(), sketch.Data()); sketch_on_single_node.FixError(); @@ -616,7 +618,8 @@ TEST(GPUQuantile, Push) { HostDeviceVector ft; SketchContainer sketch(ft, n_bins, kCols, kRows, ctx.Device()); - sketch.Push(&ctx, dh::ToSpan(d_entries), dh::ToSpan(columns_ptr), dh::ToSpan(columns_ptr), kRows, {}); + sketch.Push(&ctx, dh::ToSpan(d_entries), dh::ToSpan(columns_ptr), dh::ToSpan(columns_ptr), kRows, + {}); auto sketch_data = sketch.Data(); @@ -656,7 +659,7 @@ TEST(GPUQuantile, MultiColPush) { int32_t n_bins = 16; HostDeviceVector ft; SketchContainer sketch(ft, n_bins, kCols, kRows, ctx.Device()); - dh::device_vector d_entries {entries}; + dh::device_vector d_entries{entries}; dh::device_vector columns_ptr(kCols + 1, 0); for (size_t i = 1; i < kCols + 1; ++i) { diff --git a/tests/cpp/common/test_quantile.h b/tests/cpp/common/test_quantile.h index 38ace76c4d13..6dda69dda819 100644 --- a/tests/cpp/common/test_quantile.h +++ b/tests/cpp/common/test_quantile.h @@ -1,8 +1,8 @@ /** * Copyright 2020-2024, XGBoost Contributors */ -#ifndef XGBOOST_TESTS_CPP_COMMON_TEST_QUANTILE_H_ -#define XGBOOST_TESTS_CPP_COMMON_TEST_QUANTILE_H_ +#ifndef TESTS_CPP_COMMON_TEST_QUANTILE_H_ +#define TESTS_CPP_COMMON_TEST_QUANTILE_H_ #include #include @@ -10,11 +10,12 @@ #include "../helpers.h" namespace xgboost::common { -template void RunWithSeedsAndBins(size_t rows, Fn fn) { +template +void RunWithSeedsAndBins(size_t rows, Fn fn) { std::vector seeds(2); SimpleLCG lcg; SimpleRealUniformDistribution dist(3, 1000); - std::generate(seeds.begin(), seeds.end(), [&](){ return dist(&lcg); }); + std::generate(seeds.begin(), seeds.end(), [&]() { return dist(&lcg); }); std::vector bins(2); for (size_t i = 0; i < bins.size() - 1; ++i) { @@ -39,4 +40,4 @@ template void RunWithSeedsAndBins(size_t rows, Fn fn) { } } // namespace xgboost::common -#endif // XGBOOST_TESTS_CPP_COMMON_TEST_QUANTILE_H_ +#endif // TESTS_CPP_COMMON_TEST_QUANTILE_H_ diff --git a/tests/cpp/common/test_random.cc b/tests/cpp/common/test_random.cc index f22cdfee9b05..e9d2f07ea172 100644 --- a/tests/cpp/common/test_random.cc +++ b/tests/cpp/common/test_random.cc @@ -1,6 +1,8 @@ /** * Copyright 2018-2025, XGBoost Contributors */ +#include + #include "../../../src/common/random.h" #include "../helpers.h" #include "gtest/gtest.h" @@ -80,7 +82,7 @@ TEST(ColumnSampler, ThreadSynchronisation) { size_t levels = 5; std::vector reference_result; HostDeviceVector feature_weights; - bool success = true; // Cannot use google test asserts in multithreaded region + bool success = true; // Cannot use google test asserts in multithreaded region #pragma omp parallel num_threads(n_threads) { for (auto j = 0ull; j < iterations; j++) { diff --git a/tests/cpp/common/test_ranking_utils.cc b/tests/cpp/common/test_ranking_utils.cc index b57ee90cdba5..36bc8fb0c369 100644 --- a/tests/cpp/common/test_ranking_utils.cc +++ b/tests/cpp/common/test_ranking_utils.cc @@ -4,18 +4,19 @@ #include "test_ranking_utils.h" #include -#include // for Args, bst_group_t, kRtEps -#include // for Context -#include // for MetaInfo, DMatrix -#include // for HostDeviceVector -#include // for Error -#include // for StringView - -#include // for size_t -#include // for uint32_t -#include // for iota -#include // for move -#include // for vector +#include // for Args, bst_group_t, kRtEps +#include // for Context +#include // for MetaInfo, DMatrix +#include // for HostDeviceVector +#include // for Error +#include // for StringView + +#include // for size_t +#include // for uint32_t +#include // for make_shared +#include // for iota +#include // for move +#include // for vector #include "../../../src/common/numeric.h" // for Iota #include "../../../src/common/ranking_utils.h" // for LambdaRankParam, ParseMetricName, MakeMet... @@ -126,7 +127,9 @@ void TestNDCGCache(Context const* ctx) { info.group_ptr_ = {static_cast(0), static_cast(info.num_row_)}; { - auto fail = [&]() { NDCGCache cache{ctx, info, param}; }; + auto fail = [&]() { + NDCGCache cache{ctx, info, param}; + }; // empty label ASSERT_THROW(fail(), dmlc::Error); info.labels = linalg::Matrix{{0.0f, 0.1f, 0.2f}, {3}, DeviceOrd::CPU()}; @@ -191,7 +194,9 @@ void TestMAPCache(Context const* ctx) { info.num_row_ = h_data.size(); info.labels.Data()->HostVector() = std::move(h_data); - auto fail = [&]() { std::make_shared(ctx, info, param); }; + auto fail = [&]() { + std::make_shared(ctx, info, param); + }; // binary label ASSERT_THROW(fail(), dmlc::Error); diff --git a/tests/cpp/common/test_ranking_utils.cu b/tests/cpp/common/test_ranking_utils.cu index 378394d67c26..99bc0ecbdb96 100644 --- a/tests/cpp/common/test_ranking_utils.cu +++ b/tests/cpp/common/test_ranking_utils.cu @@ -2,14 +2,14 @@ * Copyright 2023 by XGBoost Contributors */ #include -#include // for Args, XGBOOST_DEVICE, bst_group_t, kRtEps -#include // for Context -#include // for MakeTensorView, Vector - -#include // for size_t -#include // for shared_ptr -#include // for iota -#include // for vector +#include // for Args, XGBOOST_DEVICE, bst_group_t, kRtEps +#include // for Context +#include // for MakeTensorView, Vector + +#include // for size_t +#include // for shared_ptr +#include // for iota +#include // for vector #include "../../../src/common/algorithm.cuh" // for SegmentedSequence #include "../../../src/common/cuda_context.cuh" // for CUDAContext diff --git a/tests/cpp/common/test_ref_resource_view.cu b/tests/cpp/common/test_ref_resource_view.cu index b29849bfbc1d..bc492cae9443 100644 --- a/tests/cpp/common/test_ref_resource_view.cu +++ b/tests/cpp/common/test_ref_resource_view.cu @@ -9,6 +9,11 @@ #include // for make_constant_iterator #include // for sequence +#include +#include +#include +#include + #include "../../../src/common/ref_resource_view.cuh" #include "../../../src/common/threadpool.h" // for ThreadPool #include "../helpers.h" // for MakeCUDACtx diff --git a/tests/cpp/common/test_span.cc b/tests/cpp/common/test_span.cc index f7656b2a5fa2..1059ab73af5c 100644 --- a/tests/cpp/common/test_span.cc +++ b/tests/cpp/common/test_span.cc @@ -22,7 +22,7 @@ static_assert(std::is_trivially_copy_constructible_v); TEST(Span, TestStatus) { int status = 1; - TestTestStatus {&status}(); + TestTestStatus{&status}(); ASSERT_EQ(status, -1); std::vector foo; @@ -56,11 +56,11 @@ TEST(Span, DlfConstructors) { // Init list. { - Span s {}; + Span s{}; ASSERT_EQ(s.size(), 0); ASSERT_EQ(s.data(), nullptr); - Span cs {}; + Span cs{}; ASSERT_EQ(cs.size(), 0); ASSERT_EQ(cs.data(), nullptr); } @@ -69,21 +69,21 @@ TEST(Span, DlfConstructors) { TEST(Span, FromNullPtr) { // dynamic extent { - Span s {nullptr, static_cast::index_type>(0)}; + Span s{nullptr, static_cast::index_type>(0)}; ASSERT_EQ(s.size(), 0); ASSERT_EQ(s.data(), nullptr); - Span cs {nullptr, static_cast::index_type>(0)}; + Span cs{nullptr, static_cast::index_type>(0)}; ASSERT_EQ(cs.size(), 0); ASSERT_EQ(cs.data(), nullptr); } // static extent { - Span s {nullptr, static_cast::index_type>(0)}; + Span s{nullptr, static_cast::index_type>(0)}; ASSERT_EQ(s.size(), 0); ASSERT_EQ(s.data(), nullptr); - Span cs {nullptr, static_cast::index_type>(0)}; + Span cs{nullptr, static_cast::index_type>(0)}; ASSERT_EQ(cs.size(), 0); ASSERT_EQ(cs.data(), nullptr); } @@ -91,99 +91,101 @@ TEST(Span, FromNullPtr) { TEST(Span, FromPtrLen) { float arr[16]; - InitializeRange(arr, arr+16); + InitializeRange(arr, arr + 16); // static extent { - Span s (arr, 16); - ASSERT_EQ (s.size(), 16); - ASSERT_EQ (s.data(), arr); + Span s(arr, 16); + ASSERT_EQ(s.size(), 16); + ASSERT_EQ(s.data(), arr); for (Span::index_type i = 0; i < 16; ++i) { - ASSERT_EQ (s[i], arr[i]); + ASSERT_EQ(s[i], arr[i]); } - Span cs (arr, 16); - ASSERT_EQ (cs.size(), 16); - ASSERT_EQ (cs.data(), arr); + Span cs(arr, 16); + ASSERT_EQ(cs.size(), 16); + ASSERT_EQ(cs.data(), arr); for (Span::index_type i = 0; i < 16; ++i) { - ASSERT_EQ (cs[i], arr[i]); + ASSERT_EQ(cs[i], arr[i]); } } // dynamic extent { - Span s (arr, 16); - ASSERT_EQ (s.size(), 16); - ASSERT_EQ (s.data(), arr); + Span s(arr, 16); + ASSERT_EQ(s.size(), 16); + ASSERT_EQ(s.data(), arr); for (size_t i = 0; i < 16; ++i) { - ASSERT_EQ (s[i], arr[i]); + ASSERT_EQ(s[i], arr[i]); } - Span cs (arr, 16); - ASSERT_EQ (cs.size(), 16); - ASSERT_EQ (cs.data(), arr); + Span cs(arr, 16); + ASSERT_EQ(cs.size(), 16); + ASSERT_EQ(cs.data(), arr); for (Span::index_type i = 0; i < 16; ++i) { - ASSERT_EQ (cs[i], arr[i]); + ASSERT_EQ(cs[i], arr[i]); } } } TEST(SpanDeathTest, FromPtrLen) { float arr[16]; - InitializeRange(arr, arr+16); + InitializeRange(arr, arr + 16); { - auto lazy = [=]() {Span tmp (arr, 5);}; + auto lazy = [=]() { + Span tmp(arr, 5); + }; EXPECT_DEATH(lazy(), ""); } } TEST(Span, FromFirstLast) { float arr[16]; - InitializeRange(arr, arr+16); + InitializeRange(arr, arr + 16); // dynamic extent { - Span s (arr, arr + 16); - ASSERT_EQ (s.size(), 16); - ASSERT_EQ (s.data(), arr); - ASSERT_EQ (s.data() + s.size(), arr + 16); + Span s(arr, arr + 16); + ASSERT_EQ(s.size(), 16); + ASSERT_EQ(s.data(), arr); + ASSERT_EQ(s.data() + s.size(), arr + 16); for (size_t i = 0; i < 16; ++i) { - ASSERT_EQ (s[i], arr[i]); + ASSERT_EQ(s[i], arr[i]); } - Span cs (arr, arr + 16); - ASSERT_EQ (cs.size(), 16); - ASSERT_EQ (cs.data(), arr); - ASSERT_EQ (cs.data() + cs.size(), arr + 16); + Span cs(arr, arr + 16); + ASSERT_EQ(cs.size(), 16); + ASSERT_EQ(cs.data(), arr); + ASSERT_EQ(cs.data() + cs.size(), arr + 16); for (size_t i = 0; i < 16; ++i) { - ASSERT_EQ (cs[i], arr[i]); + ASSERT_EQ(cs[i], arr[i]); } } // static extent { - Span s (arr, arr + 16); - ASSERT_EQ (s.size(), 16); - ASSERT_EQ (s.data(), arr); - ASSERT_EQ (s.data() + s.size(), arr + 16); + Span s(arr, arr + 16); + ASSERT_EQ(s.size(), 16); + ASSERT_EQ(s.data(), arr); + ASSERT_EQ(s.data() + s.size(), arr + 16); for (size_t i = 0; i < 16; ++i) { - ASSERT_EQ (s[i], arr[i]); + ASSERT_EQ(s[i], arr[i]); } - Span cs (arr, arr + 16); - ASSERT_EQ (cs.size(), 16); - ASSERT_EQ (cs.data(), arr); - ASSERT_EQ (cs.data() + cs.size(), arr + 16); + Span cs(arr, arr + 16); + ASSERT_EQ(cs.size(), 16); + ASSERT_EQ(cs.data(), arr); + ASSERT_EQ(cs.data() + cs.size(), arr + 16); for (size_t i = 0; i < 16; ++i) { - ASSERT_EQ (cs[i], arr[i]); + ASSERT_EQ(cs[i], arr[i]); } } } @@ -202,8 +204,8 @@ TEST(Span, FromOther) { // default copy constructor { - Span s0 (arr); - Span s1 (s0); + Span s0(arr); + Span s1(s0); ASSERT_EQ(s0.size(), s1.size()); ASSERT_EQ(s0.data(), s1.data()); } @@ -214,7 +216,7 @@ TEST(Span, FromArray) { InitializeRange(arr, arr + 16); { - Span s (arr); + Span s(arr); ASSERT_EQ(&arr[0], s.data()); ASSERT_EQ(s.size(), 16); for (size_t i = 0; i < 16; ++i) { @@ -223,7 +225,7 @@ TEST(Span, FromArray) { } { - Span s (arr); + Span s(arr); ASSERT_EQ(&arr[0], s.data()); ASSERT_EQ(s.size(), 16); for (size_t i = 0; i < 16; ++i) { @@ -233,7 +235,7 @@ TEST(Span, FromArray) { } TEST(Span, FromContainer) { - std::vector vec (16); + std::vector vec(16); InitializeRange(vec.begin(), vec.end()); Span s(vec); @@ -290,7 +292,7 @@ TEST(Span, ElementAccess) { float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); size_t j = 0; for (auto i : s) { ASSERT_EQ(i, arr[j]); @@ -302,7 +304,7 @@ TEST(SpanDeathTest, ElementAccess) { float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); EXPECT_DEATH(s[16], ""); EXPECT_DEATH(s[-1], ""); @@ -318,13 +320,13 @@ TEST(Span, Obversers) { TEST(Span, FrontBack) { { - float arr[4] {0, 1, 2, 3}; + float arr[4]{0, 1, 2, 3}; Span s(arr); ASSERT_EQ(s.front(), 0); ASSERT_EQ(s.back(), 3); } { - std::vector arr {0, 1, 2, 3}; + std::vector arr{0, 1, 2, 3}; Span s(arr); ASSERT_EQ(s.front(), 0); ASSERT_EQ(s.back(), 3); @@ -350,7 +352,7 @@ TEST(Span, FirstLast) { float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); Span first = s.first<4>(); ASSERT_EQ(first.size(), 4); @@ -365,14 +367,14 @@ TEST(Span, FirstLast) { float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); Span last = s.last<4>(); ASSERT_EQ(last.size(), 4); ASSERT_EQ(last.data(), arr + 12); for (size_t i = 0; i < last.size(); ++i) { - ASSERT_EQ(last[i], arr[i+12]); + ASSERT_EQ(last[i], arr[i + 12]); } } @@ -380,7 +382,7 @@ TEST(Span, FirstLast) { { float *arr = new float[16]; InitializeRange(arr, arr + 16); - Span s (arr, 16); + Span s(arr, 16); Span first = s.first(4); ASSERT_EQ(first.size(), 4); @@ -390,13 +392,13 @@ TEST(Span, FirstLast) { ASSERT_EQ(first[i], s[i]); } - delete [] arr; + delete[] arr; } { float *arr = new float[16]; InitializeRange(arr, arr + 16); - Span s (arr, 16); + Span s(arr, 16); Span last = s.last(4); ASSERT_EQ(last.size(), 4); @@ -406,7 +408,7 @@ TEST(Span, FirstLast) { ASSERT_EQ(s[12 + i], last[i]); } - delete [] arr; + delete[] arr; } } @@ -416,7 +418,7 @@ TEST(SpanDeathTest, FirstLast) { float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); auto constexpr kOne = static_cast::index_type>(-1); EXPECT_DEATH(s.first(), ""); EXPECT_DEATH(s.first<17>(), ""); @@ -427,7 +429,7 @@ TEST(SpanDeathTest, FirstLast) { float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); auto constexpr kOne = static_cast::index_type>(-1); EXPECT_DEATH(s.last(), ""); EXPECT_DEATH(s.last<17>(), ""); @@ -438,29 +440,29 @@ TEST(SpanDeathTest, FirstLast) { { float *arr = new float[16]; InitializeRange(arr, arr + 16); - Span s (arr, 16); + Span s(arr, 16); EXPECT_DEATH(s.first(-1), ""); EXPECT_DEATH(s.first(17), ""); EXPECT_DEATH(s.first(32), ""); - delete [] arr; + delete[] arr; } { float *arr = new float[16]; InitializeRange(arr, arr + 16); - Span s (arr, 16); + Span s(arr, 16); EXPECT_DEATH(s.last(-1), ""); EXPECT_DEATH(s.last(17), ""); EXPECT_DEATH(s.last(32), ""); - delete [] arr; + delete[] arr; } } TEST(Span, Subspan) { - int arr[16] {0}; - Span s1 (arr); + int arr[16]{0}; + Span s1(arr); auto s2 = s1.subspan<4>(); ASSERT_EQ(s1.size() - 4, s2.size()); @@ -474,8 +476,8 @@ TEST(Span, Subspan) { } TEST(SpanDeathTest, Subspan) { - int arr[16] {0}; - Span s1 (arr); + int arr[16]{0}; + Span s1(arr); EXPECT_DEATH(s1.subspan(-1, 0), ""); EXPECT_DEATH(s1.subspan(17, 0), ""); @@ -504,7 +506,7 @@ TEST(Span, AsWritableBytes) { TEST(Span, Empty) { { - Span s {nullptr, static_cast::index_type>(0)}; + Span s{nullptr, static_cast::index_type>(0)}; auto res = s.subspan(0); ASSERT_EQ(res.data(), nullptr); ASSERT_EQ(res.size(), 0); @@ -515,7 +517,7 @@ TEST(Span, Empty) { } { - Span s {nullptr, static_cast::index_type>(0)}; + Span s{nullptr, static_cast::index_type>(0)}; auto res = s.subspan(0); ASSERT_EQ(res.data(), nullptr); ASSERT_EQ(res.size(), 0); diff --git a/tests/cpp/common/test_span.cu b/tests/cpp/common/test_span.cu index 9c2bdc65cd34..6e4f050b0838 100644 --- a/tests/cpp/common/test_span.cu +++ b/tests/cpp/common/test_span.cu @@ -7,7 +7,9 @@ #include #include +#include #include // for iota +#include #include "../../../src/common/device_helpers.cuh" #include "test_span.h" @@ -17,29 +19,23 @@ namespace common { struct TestStatus { private: - int *status_; + int* status_; public: - TestStatus () { + TestStatus() { dh::safe_cuda(cudaMalloc(&status_, sizeof(int))); int h_status = 1; - dh::safe_cuda(cudaMemcpy(status_, &h_status, - sizeof(int), cudaMemcpyHostToDevice)); - } - ~TestStatus() { - dh::safe_cuda(cudaFree(status_)); + dh::safe_cuda(cudaMemcpy(status_, &h_status, sizeof(int), cudaMemcpyHostToDevice)); } + ~TestStatus() { dh::safe_cuda(cudaFree(status_)); } int Get() { int h_status; - dh::safe_cuda(cudaMemcpy(&h_status, status_, - sizeof(int), cudaMemcpyDeviceToHost)); + dh::safe_cuda(cudaMemcpy(&h_status, status_, sizeof(int), cudaMemcpyDeviceToHost)); return h_status; } - int* Data() { - return status_; - } + int* Data() { return status_; } }; __global__ void TestFromOtherKernel(Span span) { @@ -64,18 +60,18 @@ __global__ void TestFromOtherKernelConst(Span span) { * \brief Here we just test whether the code compiles. */ TEST(GPUSpan, FromOther) { - thrust::host_vector h_vec (16); + thrust::host_vector h_vec(16); std::iota(h_vec.begin(), h_vec.end(), 0); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); // dynamic extent { - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestFromOtherKernel<<<1, 16>>>(span); } { - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestFromOtherKernelConst<<<1, 16>>>(span); } // static extent @@ -107,11 +103,10 @@ template struct TestEqual { private: T *lhs_, *rhs_; - int *status_; + int* status_; public: - TestEqual(T* _lhs, T* _rhs, int * _status) : - lhs_(_lhs), rhs_(_rhs), status_(_status) {} + TestEqual(T* _lhs, T* _rhs, int* _status) : lhs_(_lhs), rhs_(_rhs), status_(_status) {} XGBOOST_DEVICE void operator()(size_t _idx) { bool res = lhs_[_idx] == rhs_[_idx]; @@ -123,16 +118,16 @@ TEST(GPUSpan, WithTrust) { dh::safe_cuda(cudaSetDevice(0)); // Not adviced to initialize span with host_vector, since h_vec.data() is // a host function. - thrust::host_vector h_vec (16); + thrust::host_vector h_vec(16); std::iota(h_vec.begin(), h_vec.end(), 0); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); // Can't initialize span with device_vector, since d_vec.data() is not raw // pointer { - Span s (d_vec.data().get(), d_vec.size()); + Span s(d_vec.data().get(), d_vec.size()); ASSERT_EQ(d_vec.size(), s.size()); ASSERT_EQ(d_vec.data().get(), s.data()); @@ -140,13 +135,12 @@ TEST(GPUSpan, WithTrust) { { TestStatus status; - thrust::device_vector d_vec1 (d_vec.size()); + thrust::device_vector d_vec1(d_vec.size()); thrust::copy(thrust::device, d_vec.begin(), d_vec.end(), d_vec1.begin()); - Span s (d_vec1.data().get(), d_vec.size()); + Span s(d_vec1.data().get(), d_vec.size()); - dh::LaunchN(16, TestEqual{ - thrust::raw_pointer_cast(d_vec1.data()), - s.data(), status.Data()}); + dh::LaunchN(16, + TestEqual{thrust::raw_pointer_cast(d_vec1.data()), s.data(), status.Data()}); ASSERT_EQ(status.Get(), 1); // FIXME(trivialfis): memory error! @@ -180,13 +174,13 @@ __global__ void TestModifyKernel(Span span) { } TEST(GPUSpan, Modify) { - thrust::host_vector h_vec (16); + thrust::host_vector h_vec(16); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestModifyKernel<<<1, 16>>>(span); @@ -214,7 +208,7 @@ struct TestElementAccess { Span span_; public: - XGBOOST_DEVICE explicit TestElementAccess (Span _span) : span_(_span) {} + XGBOOST_DEVICE explicit TestElementAccess(Span _span) : span_(_span) {} XGBOOST_DEVICE float operator()(size_t _idx) { float tmp = span_[_idx]; @@ -225,13 +219,13 @@ struct TestElementAccess { TEST(GPUSpanDeathTest, ElementAccess) { dh::safe_cuda(cudaSetDevice(0)); auto test_element_access = []() { - thrust::host_vector h_vec (16); + thrust::host_vector h_vec(16); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); dh::LaunchN(17, TestElementAccess{span}); }; @@ -257,13 +251,13 @@ TEST(GPUSpanDeathTest, FirstLast) { // We construct vectors multiple times since thrust can not recover from // death test. auto lambda_first_dy = []() { - thrust::host_vector h_vec (4); + thrust::host_vector h_vec(4); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestFirstDynamicKernel<<<1, 1>>>(span); }; testing::internal::CaptureStdout(); @@ -271,13 +265,13 @@ TEST(GPUSpanDeathTest, FirstLast) { std::string output = testing::internal::GetCapturedStdout(); auto lambda_first_static = []() { - thrust::host_vector h_vec (4); + thrust::host_vector h_vec(4); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestFirstStaticKernel<<<1, 1>>>(span); }; testing::internal::CaptureStdout(); @@ -285,13 +279,13 @@ TEST(GPUSpanDeathTest, FirstLast) { output = testing::internal::GetCapturedStdout(); auto lambda_last_dy = []() { - thrust::host_vector h_vec (4); + thrust::host_vector h_vec(4); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestLastDynamicKernel<<<1, 1>>>(span); }; testing::internal::CaptureStdout(); @@ -299,13 +293,13 @@ TEST(GPUSpanDeathTest, FirstLast) { output = testing::internal::GetCapturedStdout(); auto lambda_last_static = []() { - thrust::host_vector h_vec (4); + thrust::host_vector h_vec(4); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestLastStaticKernel<<<1, 1>>>(span); }; testing::internal::CaptureStdout(); @@ -342,25 +336,19 @@ void TestFrontBack() { } } // namespace -TEST(GPUSpanDeathTest, FrontBack) { - TestFrontBack(); -} +TEST(GPUSpanDeathTest, FrontBack) { TestFrontBack(); } -__global__ void TestSubspanDynamicKernel(Span _span) { - _span.subspan(16, 0); -} -__global__ void TestSubspanStaticKernel(Span _span) { - _span.subspan<16>(); -} +__global__ void TestSubspanDynamicKernel(Span _span) { _span.subspan(16, 0); } +__global__ void TestSubspanStaticKernel(Span _span) { _span.subspan<16>(); } TEST(GPUSpanDeathTest, Subspan) { auto lambda_subspan_dynamic = []() { - thrust::host_vector h_vec (4); + thrust::host_vector h_vec(4); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestSubspanDynamicKernel<<<1, 1>>>(span); }; testing::internal::CaptureStdout(); @@ -368,13 +356,13 @@ TEST(GPUSpanDeathTest, Subspan) { std::string output = testing::internal::GetCapturedStdout(); auto lambda_subspan_static = []() { - thrust::host_vector h_vec (4); + thrust::host_vector h_vec(4); InitializeRange(h_vec.begin(), h_vec.end()); - thrust::device_vector d_vec (h_vec.size()); + thrust::device_vector d_vec(h_vec.size()); thrust::copy(h_vec.begin(), h_vec.end(), d_vec.begin()); - Span span (d_vec.data().get(), d_vec.size()); + Span span(d_vec.data().get(), d_vec.size()); TestSubspanStaticKernel<<<1, 1>>>(span); }; testing::internal::CaptureStdout(); diff --git a/tests/cpp/common/test_span.h b/tests/cpp/common/test_span.h index 11a67caab800..61b7af260a50 100644 --- a/tests/cpp/common/test_span.h +++ b/tests/cpp/common/test_span.h @@ -1,8 +1,8 @@ /*! * Copyright 2018 XGBoost contributors */ -#ifndef XGBOOST_TEST_SPAN_H_ -#define XGBOOST_TEST_SPAN_H_ +#ifndef TESTS_CPP_COMMON_TEST_SPAN_H_ +#define TESTS_CPP_COMMON_TEST_SPAN_H_ #include #include @@ -18,24 +18,22 @@ XGBOOST_DEVICE void InitializeRange(Iter _begin, Iter _end) { namespace xgboost { namespace common { -#define SPAN_ASSERT_TRUE(cond, status) \ - if (!(cond)) { \ - *(status) = -1; \ +#define SPAN_ASSERT_TRUE(cond, status) \ + if (!(cond)) { \ + *(status) = -1; \ } -#define SPAN_ASSERT_FALSE(cond, status) \ - if ((cond)) { \ - *(status) = -1; \ +#define SPAN_ASSERT_FALSE(cond, status) \ + if ((cond)) { \ + *(status) = -1; \ } struct TestTestStatus { - int * status_; + int* status_; - TestTestStatus(int* _status): status_(_status) {} + explicit TestTestStatus(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index SPAN_ASSERT_TRUE(false, status_); } @@ -44,11 +42,9 @@ struct TestTestStatus { struct TestAssignment { int* status_; - TestAssignment(int* _status) : status_(_status) {} + explicit TestAssignment(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index Span s1; @@ -66,38 +62,34 @@ struct TestAssignment { struct TestBeginEnd { int* status_; - TestBeginEnd(int* _status) : status_(_status) {} + explicit TestBeginEnd(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); - Span::iterator beg { s.begin() }; - Span::iterator end { s.end() }; + Span s(arr); + Span::iterator beg{s.begin()}; + Span::iterator end{s.end()}; - SPAN_ASSERT_TRUE(end == beg + 16, status_); + SPAN_ASSERT_TRUE(end == beg + 16, status_); SPAN_ASSERT_TRUE(*beg == arr[0], status_); SPAN_ASSERT_TRUE(*(end - 1) == arr[15], status_); } }; struct TestRBeginREnd { - int * status_; + int* status_; - TestRBeginREnd(int* _status): status_(_status) {} + explicit TestRBeginREnd(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); #if defined(__CUDA_ARCH__) auto rbeg = dh::trbegin(s); @@ -114,17 +106,15 @@ struct TestRBeginREnd { }; struct TestObservers { - int * status_; + int* status_; - TestObservers(int * _status): status_(_status) {} + explicit TestObservers(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index // empty { - float *arr = nullptr; + float* arr = nullptr; Span s(arr, static_cast::index_type>(0)); SPAN_ASSERT_TRUE(s.empty(), status_); } @@ -132,22 +122,20 @@ struct TestObservers { // size, size_types { float* arr = new float[16]; - Span s (arr, 16); + Span s(arr, 16); SPAN_ASSERT_TRUE(s.size() == 16, status_); SPAN_ASSERT_TRUE(s.size_bytes() == 16 * sizeof(float), status_); - delete [] arr; + delete[] arr; } } }; struct TestCompare { - int * status_; + int* status_; - TestCompare(int * _status): status_(_status) {} + explicit TestCompare(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float lhs_arr[16], rhs_arr[16]; InitializeRange(lhs_arr, lhs_arr + 16); @@ -171,13 +159,11 @@ struct TestCompare { }; struct TestIterConstruct { - int * status_; + int* status_; - TestIterConstruct(int * _status): status_(_status) {} + explicit TestIterConstruct(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index. Span::iterator it1; Span::iterator it2; @@ -190,37 +176,33 @@ struct TestIterConstruct { }; struct TestIterRef { - int * status_; + int* status_; - TestIterRef(int * _status): status_(_status) {} + explicit TestIterRef(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); + Span s(arr); SPAN_ASSERT_TRUE(*(s.begin()) == s[0], status_); SPAN_ASSERT_TRUE(*(s.end() - 1) == s[15], status_); } }; struct TestIterCalculate { - int * status_; + int* status_; - TestIterCalculate(int * _status): status_(_status) {} + explicit TestIterCalculate(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); - Span::iterator beg { s.begin() }; + Span s(arr); + Span::iterator beg{s.begin()}; beg += 4; SPAN_ASSERT_TRUE(*beg == 4, status_); @@ -241,19 +223,17 @@ struct TestIterCalculate { }; struct TestIterCompare { - int * status_; + int* status_; - TestIterCompare(int * _status): status_(_status) {} + explicit TestIterCompare(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float arr[16]; InitializeRange(arr, arr + 16); - Span s (arr); - Span::iterator left { s.begin() }; - Span::iterator right { s.end() }; + Span s(arr); + Span::iterator left{s.begin()}; + Span::iterator right{s.end()}; left += 1; right -= 15; @@ -271,23 +251,20 @@ struct TestIterCompare { }; struct TestAsBytes { - int * status_; + int* status_; - TestAsBytes(int * _status): status_(_status) {} + explicit TestAsBytes(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float arr[16]; InitializeRange(arr, arr + 16); { - const Span s {arr}; + const Span s{arr}; const Span bs = as_bytes(s); SPAN_ASSERT_TRUE(bs.size() == s.size_bytes(), status_); - SPAN_ASSERT_TRUE(static_cast(bs.data()) == - static_cast(s.data()), + SPAN_ASSERT_TRUE(static_cast(bs.data()) == static_cast(s.data()), status_); } @@ -297,8 +274,7 @@ struct TestAsBytes { SPAN_ASSERT_TRUE(bs.size() == s.size(), status_); SPAN_ASSERT_TRUE(bs.size() == 0, status_); SPAN_ASSERT_TRUE(bs.size_bytes() == 0, status_); - SPAN_ASSERT_TRUE(static_cast(bs.data()) == - static_cast(s.data()), + SPAN_ASSERT_TRUE(static_cast(bs.data()) == static_cast(s.data()), status_); SPAN_ASSERT_TRUE(bs.data() == nullptr, status_); } @@ -306,13 +282,11 @@ struct TestAsBytes { }; struct TestAsWritableBytes { - int * status_; + int* status_; - TestAsWritableBytes(int * _status): status_(_status) {} + explicit TestAsWritableBytes(int* _status) : status_(_status) {} - XGBOOST_DEVICE void operator()() { - this->operator()(0); - } + XGBOOST_DEVICE void operator()() { this->operator()(0); } XGBOOST_DEVICE void operator()(size_t) { // size_t for CUDA index float arr[16]; InitializeRange(arr, arr + 16); @@ -325,16 +299,14 @@ struct TestAsWritableBytes { SPAN_ASSERT_TRUE(bs.size() == 0, status_); SPAN_ASSERT_TRUE(bs.size_bytes() == 0, status_); SPAN_ASSERT_TRUE(bs.data() == nullptr, status_); - SPAN_ASSERT_TRUE(static_cast(bs.data()) == - static_cast(s.data()), status_); + SPAN_ASSERT_TRUE(static_cast(bs.data()) == static_cast(s.data()), status_); } { - Span s { arr }; - Span bs { as_writable_bytes(s) }; + Span s{arr}; + Span bs{as_writable_bytes(s)}; SPAN_ASSERT_TRUE(s.size_bytes() == bs.size_bytes(), status_); - SPAN_ASSERT_TRUE(static_cast(bs.data()) == - static_cast(s.data()), status_); + SPAN_ASSERT_TRUE(static_cast(bs.data()) == static_cast(s.data()), status_); } } }; @@ -342,4 +314,4 @@ struct TestAsWritableBytes { } // namespace common } // namespace xgboost -#endif +#endif // TESTS_CPP_COMMON_TEST_SPAN_H_ diff --git a/tests/cpp/common/test_stats.cc b/tests/cpp/common/test_stats.cc index 935be1b6adc7..d6eae0104465 100644 --- a/tests/cpp/common/test_stats.cc +++ b/tests/cpp/common/test_stats.cc @@ -7,6 +7,7 @@ #include // for min #include // for thread +#include #include "../../../src/common/linalg_op.h" // for begin, end #include "../../../src/common/stats.h" @@ -239,7 +240,6 @@ TEST(Stats, SampleMeanDist) { TestSampleMeanDistributed(&ctx); } - TEST(Stats, WeightedSampleMean) { Context ctx; TestWeightedSampleMean(&ctx); diff --git a/tests/cpp/common/test_stats.cu b/tests/cpp/common/test_stats.cu index 0b149cb7092d..07f1225197e3 100644 --- a/tests/cpp/common/test_stats.cu +++ b/tests/cpp/common/test_stats.cu @@ -34,7 +34,7 @@ class StatsGPU : public ::testing::Test { } public: - void SetUp() override { ctx_ = MakeCUDACtx(0); } + void SetUp() override { ctx_ = MakeCUDACtx(0); } void WeightedMulti() { // data for one segment @@ -81,9 +81,8 @@ class StatsGPU : public ::testing::Test { dh::MakeTransformIterator(thrust::make_counting_iterator(0ul), [=] XGBOOST_DEVICE(std::size_t i) { return d_arr(i); }); linalg::Tensor weights{{10}, FstCU()}; - linalg::cuda_impl::TransformIdxKernel( - &ctx_, weights.View(DeviceOrd::CUDA(0)), - [=] XGBOOST_DEVICE(std::size_t, float) { return 1.0; }); + linalg::cuda_impl::TransformIdxKernel(&ctx_, weights.View(DeviceOrd::CUDA(0)), + [=] XGBOOST_DEVICE(std::size_t, float) { return 1.0; }); auto w_it = weights.Data()->ConstDevicePointer(); for (auto const& pair : TestSet{{0.0f, 1.0f}, {0.5f, 3.0f}, {1.0f, 5.0f}}) { SegmentedWeightedQuantile(&ctx_, pair.first, key_it, key_it + indptr_.Size(), val_it, diff --git a/tests/cpp/common/test_string_view.cc b/tests/cpp/common/test_string_view.cc index e89689162661..37fa1f89b334 100644 --- a/tests/cpp/common/test_string_view.cc +++ b/tests/cpp/common/test_string_view.cc @@ -22,10 +22,10 @@ TEST(StringView, Basic) { ASSERT_EQ(substr.size(), 2); ASSERT_EQ(StringView{"is"}.size(), 2); - ASSERT_TRUE(substr == "is"); - ASSERT_FALSE(substr != "is"); - ASSERT_FALSE(substr == "foobar"); - ASSERT_FALSE(substr == "i"); + ASSERT_EQ(substr, "is"); + ASSERT_EQ(substr, "is"); + ASSERT_NE(substr, "foobar"); + ASSERT_NE(substr, "i"); ASSERT_TRUE(std::equal(substr.crbegin(), substr.crend(), StringView{"si"}.cbegin())); diff --git a/tests/cpp/common/test_survival_util.cc b/tests/cpp/common/test_survival_util.cc index f54719885dd3..0c842c526e09 100644 --- a/tests/cpp/common/test_survival_util.cc +++ b/tests/cpp/common/test_survival_util.cc @@ -13,22 +13,22 @@ inline static void RobustTestSuite(double y_lower, double y_upper, double sigma) for (int i = 50; i >= -50; --i) { const double y_pred = std::pow(10.0, static_cast(i)); const double z = (std::log(y_lower) - std::log(y_pred)) / sigma; - const double gradient - = AFTLoss::Gradient(y_lower, y_upper, std::log(y_pred), sigma); - const double hessian - = AFTLoss::Hessian(y_lower, y_upper, std::log(y_pred), sigma); - ASSERT_FALSE(std::isnan(gradient)) << "z = " << z << ", y \\in [" - << y_lower << ", " << y_upper << "], y_pred = " << y_pred - << ", dist = " << static_cast(Distribution::Type()); - ASSERT_FALSE(std::isinf(gradient)) << "z = " << z << ", y \\in [" - << y_lower << ", " << y_upper << "], y_pred = " << y_pred - << ", dist = " << static_cast(Distribution::Type()); - ASSERT_FALSE(std::isnan(hessian)) << "z = " << z << ", y \\in [" - << y_lower << ", " << y_upper << "], y_pred = " << y_pred - << ", dist = " << static_cast(Distribution::Type()); - ASSERT_FALSE(std::isinf(hessian)) << "z = " << z << ", y \\in [" - << y_lower << ", " << y_upper << "], y_pred = " << y_pred - << ", dist = " << static_cast(Distribution::Type()); + const double gradient = + AFTLoss::Gradient(y_lower, y_upper, std::log(y_pred), sigma); + const double hessian = + AFTLoss::Hessian(y_lower, y_upper, std::log(y_pred), sigma); + ASSERT_FALSE(std::isnan(gradient)) + << "z = " << z << ", y \\in [" << y_lower << ", " << y_upper << "], y_pred = " << y_pred + << ", dist = " << static_cast(Distribution::Type()); + ASSERT_FALSE(std::isinf(gradient)) + << "z = " << z << ", y \\in [" << y_lower << ", " << y_upper << "], y_pred = " << y_pred + << ", dist = " << static_cast(Distribution::Type()); + ASSERT_FALSE(std::isnan(hessian)) + << "z = " << z << ", y \\in [" << y_lower << ", " << y_upper << "], y_pred = " << y_pred + << ", dist = " << static_cast(Distribution::Type()); + ASSERT_FALSE(std::isinf(hessian)) + << "z = " << z << ", y \\in [" << y_lower << ", " << y_upper << "], y_pred = " << y_pred + << ", dist = " << static_cast(Distribution::Type()); } } diff --git a/tests/cpp/common/test_threading_utils.cc b/tests/cpp/common/test_threading_utils.cc index 6e543dd93f9c..084340852c22 100644 --- a/tests/cpp/common/test_threading_utils.cc +++ b/tests/cpp/common/test_threading_utils.cc @@ -5,6 +5,7 @@ #include #include // for std::size_t +#include #include "../../../src/common/threading_utils.h" // BlockedSpace2d,ParallelFor2d,ParallelFor #include "xgboost/context.h" // Context @@ -22,9 +23,9 @@ TEST(ParallelFor2d, CreateBlockedSpace2d) { for (size_t i = 0; i < kDim1; i++) { for (size_t j = 0; j < kDim2; j++) { - ASSERT_EQ(space.GetFirstDimension(i*kDim2 + j), i); - ASSERT_EQ(j, space.GetRange(i*kDim2 + j).begin()); - ASSERT_EQ(j + kGrainSize, space.GetRange(i*kDim2 + j).end()); + ASSERT_EQ(space.GetFirstDimension(i * kDim2 + j), i); + ASSERT_EQ(j, space.GetRange(i * kDim2 + j).begin()); + ASSERT_EQ(j + kGrainSize, space.GetRange(i * kDim2 + j).end()); } } } @@ -60,10 +61,9 @@ TEST(ParallelFor2d, NonUniform) { // here are quite non-uniform distribution in space // but ParallelFor2d should split them by blocks with max size = kGrainSize // and process in balanced manner (optimal performance) - std::vector dim2 { 1024, 500, 255, 5, 10000 }; - BlockedSpace2d space(kDim1, [&](size_t i) { - return dim2[i]; - }, kGrainSize); + std::vector dim2{1024, 500, 255, 5, 10000}; + BlockedSpace2d space( + kDim1, [&](size_t i) { return dim2[i]; }, kGrainSize); std::vector> working_space(kDim1); for (size_t i = 0; i < kDim1; i++) { diff --git a/tests/cpp/common/test_threading_utils.cu b/tests/cpp/common/test_threading_utils.cu index fc7475698be2..f73346c2b6c9 100644 --- a/tests/cpp/common/test_threading_utils.cu +++ b/tests/cpp/common/test_threading_utils.cu @@ -4,6 +4,8 @@ #include #include // thrust::copy +#include + #include "../../../src/common/device_helpers.cuh" #include "../../../src/common/threading_utils.cuh" #include "../helpers.h" // for MakeCUDACtx diff --git a/tests/cpp/common/test_threadpool.cc b/tests/cpp/common/test_threadpool.cc index 5d863689b556..0c538caa8995 100644 --- a/tests/cpp/common/test_threadpool.cc +++ b/tests/cpp/common/test_threadpool.cc @@ -7,7 +7,9 @@ #include // for size_t #include // for int32_t #include // for future -#include // for sleep_for, thread +#include +#include // for sleep_for, thread +#include #include "../../../src/common/threadpool.h" @@ -54,9 +56,7 @@ TEST(ThreadPool, Basic) { { std::vector> futures; for (std::size_t i = 0; i < static_cast(n_threads) * 16; ++i) { - futures.emplace_back(pool.Submit([=] { - return i; - })); + futures.emplace_back(pool.Submit([=] { return i; })); } for (std::size_t i = 0; i < futures.size(); ++i) { ASSERT_EQ(futures[i].get(), i); diff --git a/tests/cpp/common/test_transform_iterator.cc b/tests/cpp/common/test_transform_iterator.cc index 3ec115f8f3d6..dc5d31bc735a 100644 --- a/tests/cpp/common/test_transform_iterator.cc +++ b/tests/cpp/common/test_transform_iterator.cc @@ -10,7 +10,9 @@ namespace xgboost { namespace common { TEST(IndexTransformIter, Basic) { - auto sqr = [](std::size_t i) { return i * i; }; + auto sqr = [](std::size_t i) { + return i * i; + }; auto iter = MakeIndexTransformIter(sqr); for (std::size_t i = 0; i < 4; ++i) { ASSERT_EQ(iter[i], sqr(i)); diff --git a/tests/cpp/common/test_transform_range.cc b/tests/cpp/common/test_transform_range.cc index 4fc06f63907e..af045f2d160b 100644 --- a/tests/cpp/common/test_transform_range.cc +++ b/tests/cpp/common/test_transform_range.cc @@ -43,9 +43,8 @@ TEST(Transform, DeclareUnifiedTest(Basic)) { HostDeviceVector out_vec{h_out, device}; out_vec.Fill(0); - Transform<>::Init(TestTransformRange{}, - Range{0, static_cast(size)}, AllThreadsForTest(), - TransformDevice()) + Transform<>::Init(TestTransformRange{}, Range{0, static_cast(size)}, + AllThreadsForTest(), TransformDevice()) .Eval(&out_vec, &in_vec); std::vector res = out_vec.HostVector(); diff --git a/tests/cpp/common/test_version.cc b/tests/cpp/common/test_version.cc index d8008be6838b..0aa2886816f0 100644 --- a/tests/cpp/common/test_version.cc +++ b/tests/cpp/common/test_version.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include "../../../src/common/version.h" @@ -14,9 +15,9 @@ namespace xgboost { TEST(Version, Basic) { - Json j_ver { Object() }; + Json j_ver{Object()}; Version::Save(&j_ver); - auto triplet { Version::Load(j_ver) }; + auto triplet{Version::Load(j_ver)}; ASSERT_TRUE(Version::Same(triplet)); common::TemporaryDirectory tempdir; @@ -29,30 +30,30 @@ TEST(Version, Basic) { { std::unique_ptr fi(dmlc::Stream::Create(fname.c_str(), "r")); - auto triplet { Version::Load(fi.get())};; + auto triplet{Version::Load(fi.get())}; ASSERT_TRUE(Version::Same(triplet)); } - std::string str { Version::String(triplet) }; + std::string str{Version::String(triplet)}; - size_t ptr {0}; - XGBoostVersionT v {0}; + size_t ptr{0}; + XGBoostVersionT v{0}; v = std::stoi(str, &ptr); ASSERT_EQ(str.at(ptr), '.'); ASSERT_EQ(v, XGBOOST_VER_MAJOR) << "major: " << v; - str = str.substr(ptr+1); + str = str.substr(ptr + 1); ptr = 0; v = std::stoi(str, &ptr); ASSERT_EQ(str.at(ptr), '.'); - ASSERT_EQ(v, XGBOOST_VER_MINOR) << "minor: " << v;; + ASSERT_EQ(v, XGBOOST_VER_MINOR) << "minor: " << v; - str = str.substr(ptr+1); + str = str.substr(ptr + 1); ptr = 0; v = std::stoi(str, &ptr); - ASSERT_EQ(v, XGBOOST_VER_PATCH) << "patch: " << v;; + ASSERT_EQ(v, XGBOOST_VER_PATCH) << "patch: " << v; str = str.substr(ptr); ASSERT_EQ(str.size(), 0); diff --git a/tests/cpp/data/test_adapter.cc b/tests/cpp/data/test_adapter.cc index cc8728cef7b0..ecfb9b17f586 100644 --- a/tests/cpp/data/test_adapter.cc +++ b/tests/cpp/data/test_adapter.cc @@ -4,8 +4,11 @@ #include #include +#include +#include #include #include +#include #include "../../../src/data/adapter.h" #include "../../../src/data/simple_dmatrix.h" @@ -110,14 +113,14 @@ class CSRIterForTest { 0, 1, 0, 1, 1}; std::vector().offset)>> row_ptr_{ 0, 2, 4, 5, 5}; - size_t iter_ {0}; + size_t iter_{0}; public: - size_t static constexpr kRows { 4 }; // Test for the last row being empty - size_t static constexpr kCols { 13 }; // Test for having some missing columns + size_t static constexpr kRows{4}; // Test for the last row being empty + size_t static constexpr kCols{13}; // Test for having some missing columns XGBoostBatchCSR Next() { - for (auto& v : data_) { + for (auto &v : data_) { v += iter_; } XGBoostBatchCSR batch; @@ -139,10 +142,9 @@ class CSRIterForTest { size_t constexpr CSRIterForTest::kCols; -int CSRSetDataNextForTest(DataIterHandle data_handle, - XGBCallbackSetData *set_function, +int CSRSetDataNextForTest(DataIterHandle data_handle, XGBCallbackSetData *set_function, DataHolderHandle set_function_handle) { - size_t constexpr kIters { 2 }; + size_t constexpr kIters{2}; auto iter = static_cast(data_handle); if (iter->Iter() < kIters) { auto batch = iter->Next(); @@ -156,17 +158,16 @@ int CSRSetDataNextForTest(DataIterHandle data_handle, TEST(Adapter, IteratorAdapter) { CSRIterForTest iter; - data::IteratorAdapter adapter{&iter, CSRSetDataNextForTest}; - constexpr size_t kRows { 8 }; + data::IteratorAdapter adapter{ + &iter, CSRSetDataNextForTest}; + constexpr size_t kRows{8}; - std::unique_ptr data { - DMatrix::Create(&adapter, std::numeric_limits::quiet_NaN(), 1) - }; + std::unique_ptr data{ + DMatrix::Create(&adapter, std::numeric_limits::quiet_NaN(), 1)}; ASSERT_EQ(data->Info().num_col_, CSRIterForTest::kCols); ASSERT_EQ(data->Info().num_row_, kRows); int num_batch = 0; - for (auto const& batch : data->GetBatches()) { + for (auto const &batch : data->GetBatches()) { ASSERT_EQ(batch.offset.HostVector(), std::vector({0, 2, 4, 5, 5, 7, 9, 10, 10})); ++num_batch; } diff --git a/tests/cpp/data/test_array_interface.cc b/tests/cpp/data/test_array_interface.cc index f87932e77749..dcc47939637f 100644 --- a/tests/cpp/data/test_array_interface.cc +++ b/tests/cpp/data/test_array_interface.cc @@ -3,8 +3,14 @@ */ #include #include -#include "../helpers.h" + +#include +#include +#include +#include + #include "../../../src/data/array_interface.h" +#include "../helpers.h" #include "dmlc/logging.h" #include "xgboost/json.h" @@ -32,8 +38,8 @@ TEST(ArrayInterface, Initialize) { TEST(ArrayInterface, Error) { constexpr size_t kRows = 16, kCols = 10; - Json column { Object() }; - std::vector j_shape {Json(Integer(static_cast(kRows)))}; + Json column{Object()}; + std::vector j_shape{Json(Integer(static_cast(kRows)))}; column["shape"] = Array(j_shape); std::vector j_data{Json(Integer(reinterpret_cast(nullptr))), Json(Boolean(false))}; @@ -46,26 +52,21 @@ TEST(ArrayInterface, Error) { EXPECT_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n), dmlc::Error); column["version"] = 3; // missing data - EXPECT_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n), - dmlc::Error); + EXPECT_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n), dmlc::Error); // null data column["data"] = Null{}; - EXPECT_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n), - dmlc::Error); + EXPECT_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n), dmlc::Error); column["data"] = j_data; // missing typestr - EXPECT_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n), - dmlc::Error); + EXPECT_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n), dmlc::Error); column["typestr"] = String(" storage; auto array = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); - j_data = { - Json(Integer(reinterpret_cast(storage.ConstHostPointer()))), - Json(Boolean(false))}; + j_data = {Json(Integer(reinterpret_cast(storage.ConstHostPointer()))), + Json(Boolean(false))}; column["data"] = j_data; EXPECT_NO_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n)); // null data in mask diff --git a/tests/cpp/data/test_array_interface.cu b/tests/cpp/data/test_array_interface.cu index 36cba365b753..e4e1c08132a0 100644 --- a/tests/cpp/data/test_array_interface.cu +++ b/tests/cpp/data/test_array_interface.cu @@ -4,6 +4,8 @@ #include #include +#include + #include "../../../src/data/array_interface.h" #include "../helpers.h" diff --git a/tests/cpp/data/test_array_interface.h b/tests/cpp/data/test_array_interface.h index dfe4f5a3ec5c..0913282fd1ef 100644 --- a/tests/cpp/data/test_array_interface.h +++ b/tests/cpp/data/test_array_interface.h @@ -1,6 +1,8 @@ /** * Copyright 2019-2024, XGBoost Contributors */ +#ifndef TESTS_CPP_DATA_TEST_ARRAY_INTERFACE_H_ +#define TESTS_CPP_DATA_TEST_ARRAY_INTERFACE_H_ #include #include #include // for device @@ -8,14 +10,17 @@ #include #include +#include +#include + namespace xgboost { template Json GenerateDenseColumn(std::string const& typestr, size_t kRows, thrust::device_vector* out_d_data) { auto& d_data = *out_d_data; d_data.resize(kRows); - Json column { Object() }; - std::vector j_shape {Json(Integer(static_cast(kRows)))}; + Json column{Object()}; + std::vector j_shape{Json(Integer(static_cast(kRows)))}; column["shape"] = Array(j_shape); column["strides"] = Array(std::vector{Json(Integer(static_cast(sizeof(T))))}); column["stream"] = nullptr; @@ -25,9 +30,8 @@ Json GenerateDenseColumn(std::string const& typestr, size_t kRows, auto p_d_data = d_data.data().get(); - std::vector j_data { - Json(Integer(reinterpret_cast(p_d_data))), - Json(Boolean(false))}; + std::vector j_data{Json(Integer(reinterpret_cast(p_d_data))), + Json(Boolean(false))}; column["data"] = j_data; column["version"] = 3; @@ -37,10 +41,10 @@ Json GenerateDenseColumn(std::string const& typestr, size_t kRows, template Json GenerateSparseColumn(std::string const& typestr, size_t kRows, - thrust::device_vector* out_d_data) { + thrust::device_vector* out_d_data) { auto& d_data = *out_d_data; - Json column { Object() }; - std::vector j_shape {Json(Integer(static_cast(kRows)))}; + Json column{Object()}; + std::vector j_shape{Json(Integer(static_cast(kRows)))}; column["shape"] = Array(j_shape); column["strides"] = Array(std::vector{Json(Integer(static_cast(sizeof(T))))}); column["stream"] = nullptr; @@ -52,9 +56,8 @@ Json GenerateSparseColumn(std::string const& typestr, size_t kRows, auto p_d_data = d_data.data().get(); - std::vector j_data { - Json(Integer(reinterpret_cast(p_d_data))), - Json(Boolean(false))}; + std::vector j_data{Json(Integer(reinterpret_cast(p_d_data))), + Json(Boolean(false))}; column["data"] = j_data; column["version"] = 3; @@ -64,7 +67,7 @@ Json GenerateSparseColumn(std::string const& typestr, size_t kRows, template Json Generate2dArrayInterface(int rows, int cols, std::string typestr, - thrust::device_vector *p_data) { + thrust::device_vector* p_data) { auto& data = *p_data; thrust::sequence(data.begin(), data.end()); @@ -72,13 +75,15 @@ Json Generate2dArrayInterface(int rows, int cols, std::string typestr, std::vector shape = {Json(static_cast(rows)), Json(static_cast(cols))}; array_interface["shape"] = Array(shape); - std::vector j_data{ - Json(Integer(reinterpret_cast(data.data().get()))), - Json(Boolean(false))}; + std::vector j_data{Json(Integer(reinterpret_cast(data.data().get()))), + Json(Boolean(false))}; array_interface["data"] = j_data; array_interface["version"] = 3; array_interface["typestr"] = String(typestr); array_interface["stream"] = nullptr; return array_interface; } + } // namespace xgboost + +#endif // TESTS_CPP_DATA_TEST_ARRAY_INTERFACE_H_ diff --git a/tests/cpp/data/test_cat_container.cc b/tests/cpp/data/test_cat_container.cc index f59db82872ad..1843de8f9aae 100644 --- a/tests/cpp/data/test_cat_container.cc +++ b/tests/cpp/data/test_cat_container.cc @@ -6,6 +6,8 @@ #include +#include + #include "../encoder/df_mock.h" namespace xgboost { diff --git a/tests/cpp/data/test_cat_container.h b/tests/cpp/data/test_cat_container.h index f869bb3c24c5..b37c0b7071ff 100644 --- a/tests/cpp/data/test_cat_container.h +++ b/tests/cpp/data/test_cat_container.h @@ -5,6 +5,8 @@ #include +#include + #include "../../../src/data/cat_container.h" namespace xgboost { diff --git a/tests/cpp/data/test_data.cc b/tests/cpp/data/test_data.cc index a1545da53b7c..6580f1e3cfab 100644 --- a/tests/cpp/data/test_data.cc +++ b/tests/cpp/data/test_data.cc @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include "../filesystem.h" // TemporaryDirectory @@ -12,7 +14,7 @@ namespace xgboost { TEST(SparsePage, PushCSC) { - std::vector offset {0}; + std::vector offset{0}; std::vector data; SparsePage batch; batch.offset.HostVector() = offset; @@ -55,7 +57,7 @@ TEST(SparsePage, PushCSC) { inst = page[1]; ASSERT_EQ(inst.size(), 6ul); - std::vector indices_sol {1, 2, 3}; + std::vector indices_sol{1, 2, 3}; for (size_t i = 0; i < inst.size(); ++i) { ASSERT_EQ(inst[i].index, indices_sol[i % 3]); } @@ -112,8 +114,8 @@ TEST(SparsePage, SortIndices) { } TEST(DMatrix, Uri) { - auto constexpr kRows {16}; - auto constexpr kCols {8}; + auto constexpr kRows{16}; + auto constexpr kCols{8}; common::TemporaryDirectory tmpdir; auto const path = tmpdir.Path() / "small.csv"; diff --git a/tests/cpp/data/test_device_adapter.cu b/tests/cpp/data/test_device_adapter.cu index f0bb2b7d5401..dbcd56227f85 100644 --- a/tests/cpp/data/test_device_adapter.cu +++ b/tests/cpp/data/test_device_adapter.cu @@ -2,18 +2,22 @@ * Copyright 2019-2024, XGBoost contributors */ #include +#include #include + +#include +#include +#include + #include "../../../src/data/adapter.h" -#include "../helpers.h" -#include #include "../../../src/data/device_adapter.cuh" +#include "../helpers.h" #include "test_array_interface.h" using namespace xgboost; // NOLINT -void TestCudfAdapter() -{ - constexpr size_t kRowsA {16}; - constexpr size_t kRowsB {16}; +void TestCudfAdapter() { + constexpr size_t kRowsA{16}; + constexpr size_t kRowsB{16}; std::vector columns; thrust::device_vector d_data_0(kRowsA); thrust::device_vector d_data_1(kRowsB); @@ -21,7 +25,7 @@ void TestCudfAdapter() columns.emplace_back(GenerateDenseColumn("(" +#include +#include +#include #include +#include #include "../../../src/common/categorical.h" // for AsCat #include "../../../src/common/compressed_iterator.h" // for CompressedByteT @@ -41,22 +45,12 @@ TEST(EllpackPage, BuildGidxDense) { ASSERT_EQ(page->info.row_stride, n_features); std::vector solution = { - 0, 3, 8, 9, 14, 17, 20, 21, - 0, 4, 7, 10, 14, 16, 19, 22, - 1, 3, 7, 11, 14, 15, 19, 21, - 2, 3, 7, 9, 13, 16, 20, 22, - 2, 3, 6, 9, 12, 16, 20, 21, - 1, 5, 6, 10, 13, 16, 20, 21, - 2, 5, 8, 9, 13, 17, 19, 22, - 2, 4, 6, 10, 14, 17, 19, 21, - 2, 5, 7, 9, 13, 16, 19, 22, - 0, 3, 8, 10, 12, 16, 19, 22, - 1, 3, 7, 10, 13, 16, 19, 21, - 1, 3, 8, 10, 13, 17, 20, 22, - 2, 4, 6, 9, 14, 15, 19, 22, - 1, 4, 6, 9, 13, 16, 19, 21, - 2, 4, 8, 10, 14, 15, 19, 22, - 1, 4, 7, 10, 14, 16, 19, 21, + 0, 3, 8, 9, 14, 17, 20, 21, 0, 4, 7, 10, 14, 16, 19, 22, 1, 3, 7, 11, 14, 15, + 19, 21, 2, 3, 7, 9, 13, 16, 20, 22, 2, 3, 6, 9, 12, 16, 20, 21, 1, 5, 6, 10, + 13, 16, 20, 21, 2, 5, 8, 9, 13, 17, 19, 22, 2, 4, 6, 10, 14, 17, 19, 21, 2, 5, + 7, 9, 13, 16, 19, 22, 0, 3, 8, 10, 12, 16, 19, 22, 1, 3, 7, 10, 13, 16, 19, 21, + 1, 3, 8, 10, 13, 17, 20, 22, 2, 4, 6, 9, 14, 15, 19, 22, 1, 4, 6, 9, 13, 16, + 19, 21, 2, 4, 8, 10, 14, 15, 19, 22, 1, 4, 7, 10, 14, 16, 19, 21, }; page->VisitOnHost(&ctx, [&](auto&& h_accessor) { @@ -78,11 +72,9 @@ TEST(EllpackPage, BuildGidxSparse) { ASSERT_EQ(page->info.row_stride, 3); // row_stride = 3, 16 rows, 48 entries for ELLPack - std::vector solution = { - 15, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 20, 24, 24, 24, - 24, 24, 24, 24, 24, 5, 24, 24, 0, 16, 24, 15, 24, 24, 24, 24, - 24, 7, 14, 16, 4, 24, 24, 24, 24, 24, 9, 24, 24, 1, 24, 24 - }; + std::vector solution = {15, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 20, 24, 24, 24, + 24, 24, 24, 24, 24, 5, 24, 24, 0, 16, 24, 15, 24, 24, 24, 24, + 24, 7, 14, 16, 4, 24, 24, 24, 24, 24, 9, 24, 24, 1, 24, 24}; page->VisitOnHost(&ctx, [&](auto&& h_acc) { for (size_t i = 0; i < kNRows * page->info.row_stride; ++i) { ASSERT_EQ(solution[i], h_acc.gidx_iter[i]); diff --git a/tests/cpp/data/test_ellpack_page_raw_format.cu b/tests/cpp/data/test_ellpack_page_raw_format.cu index a0d7405ede20..084c3218ee35 100644 --- a/tests/cpp/data/test_ellpack_page_raw_format.cu +++ b/tests/cpp/data/test_ellpack_page_raw_format.cu @@ -4,11 +4,17 @@ #include #include +#include +#include +#include +#include +#include + +#include "../../../src/data/batch_utils.h" // for AutoHostRatio #include "../../../src/data/ellpack_page.cuh" // for EllpackPage, GetRowStride #include "../../../src/data/ellpack_page_raw_format.h" // for EllpackPageRawFormat #include "../../../src/data/ellpack_page_source.h" // for EllpackFormatStreamPolicy #include "../../../src/tree/param.h" // for TrainParam -#include "../../../src/data/batch_utils.h" // for AutoHostRatio #include "../filesystem.h" // for TemporaryDirectory #include "../helpers.h" diff --git a/tests/cpp/data/test_extmem_quantile_dmatrix.cc b/tests/cpp/data/test_extmem_quantile_dmatrix.cc index 691bcf3369f8..295bcb13bfc3 100644 --- a/tests/cpp/data/test_extmem_quantile_dmatrix.cc +++ b/tests/cpp/data/test_extmem_quantile_dmatrix.cc @@ -7,6 +7,8 @@ #include // for BatchParam #include // for equal +#include +#include #include "../../../src/common/column_matrix.h" // for ColumnMatrix #include "../../../src/data/gradient_index.h" // for GHistIndexMatrix @@ -53,9 +55,9 @@ class ExtMemQuantileDMatrixCpu : public ::testing::TestWithParam { TEST_P(ExtMemQuantileDMatrixCpu, Basic) { this->Run(this->GetParam()); } -INSTANTIATE_TEST_SUITE_P(ExtMemQuantileDMatrix, ExtMemQuantileDMatrixCpu, ::testing::ValuesIn([] { - std::vector sparsities{ - 0.0f, tree::TrainParam::DftSparseThreshold(), 0.4f, 0.8f}; - return sparsities; - }())); +INSTANTIATE_TEST_SUITE_P( + ExtMemQuantileDMatrix, ExtMemQuantileDMatrixCpu, ::testing::ValuesIn([] { + std::vector sparsities{0.0f, tree::TrainParam::DftSparseThreshold(), 0.4f, 0.8f}; + return sparsities; + }())); } // namespace xgboost::data diff --git a/tests/cpp/data/test_extmem_quantile_dmatrix.cu b/tests/cpp/data/test_extmem_quantile_dmatrix.cu index 351acfa31789..18e0df6786bd 100644 --- a/tests/cpp/data/test_extmem_quantile_dmatrix.cu +++ b/tests/cpp/data/test_extmem_quantile_dmatrix.cu @@ -4,8 +4,10 @@ #include #include // for BatchParam -#include // for tuple -#include // for vector +#include // for shared_ptr, unique_ptr +#include // for tuple +#include // for move +#include // for vector #include "../../../src/data/batch_utils.h" // for AutoHostRatio #include "../../../src/data/ellpack_page.cuh" // for EllpackPageImpl diff --git a/tests/cpp/data/test_extmem_quantile_dmatrix.h b/tests/cpp/data/test_extmem_quantile_dmatrix.h index 2d4958010b03..dc7f0b5bc717 100644 --- a/tests/cpp/data/test_extmem_quantile_dmatrix.h +++ b/tests/cpp/data/test_extmem_quantile_dmatrix.h @@ -1,6 +1,8 @@ /** * Copyright 2024, XGBoost Contributors */ +#ifndef TESTS_CPP_DATA_TEST_EXTMEM_QUANTILE_DMATRIX_H_ +#define TESTS_CPP_DATA_TEST_EXTMEM_QUANTILE_DMATRIX_H_ #include #include @@ -72,3 +74,5 @@ void TestExtMemQdmBasic(Context const* ctx, bool on_host, float sparsity, Equal& } } } // namespace xgboost::data + +#endif // TESTS_CPP_DATA_TEST_EXTMEM_QUANTILE_DMATRIX_H_ diff --git a/tests/cpp/data/test_file_iterator.cc b/tests/cpp/data/test_file_iterator.cc index a01eee863c06..337d828abfd9 100644 --- a/tests/cpp/data/test_file_iterator.cc +++ b/tests/cpp/data/test_file_iterator.cc @@ -3,6 +3,7 @@ */ #include +#include #include // for any_cast #include diff --git a/tests/cpp/data/test_gradient_index.cc b/tests/cpp/data/test_gradient_index.cc index 9ceaade72743..8b9167b16630 100644 --- a/tests/cpp/data/test_gradient_index.cc +++ b/tests/cpp/data/test_gradient_index.cc @@ -2,17 +2,17 @@ * Copyright 2021-2024, XGBoost contributors */ #include -#include // for BatchIterator, BatchSet, DMatrix, BatchParam - -#include // for sort, unique -#include // for isnan -#include // for size_t -#include // for numeric_limits -#include // for shared_ptr, __shared_ptr_access, unique_ptr -#include // for string -#include // for make_tuple, tie, tuple -#include // for move -#include // for vector +#include // for BatchIterator, BatchSet, DMatrix, BatchParam + +#include // for sort, unique +#include // for isnan +#include // for size_t +#include // for numeric_limits +#include // for shared_ptr, __shared_ptr_access, unique_ptr +#include // for string +#include // for make_tuple, tie, tuple +#include // for move +#include // for vector #include "../../../src/common/categorical.h" // for AsCat #include "../../../src/common/column_matrix.h" // for ColumnMatrix diff --git a/tests/cpp/data/test_gradient_index_page_raw_format.cc b/tests/cpp/data/test_gradient_index_page_raw_format.cc index 2dae93f0ca12..17ef9c83fb9f 100644 --- a/tests/cpp/data/test_gradient_index_page_raw_format.cc +++ b/tests/cpp/data/test_gradient_index_page_raw_format.cc @@ -6,6 +6,8 @@ #include // for size_t #include // for unique_ptr +#include +#include #include "../../../src/common/column_matrix.h" // for common::ColumnMatrix #include "../../../src/common/io.h" // for MmapResource, AlignedResourceReadStream... diff --git a/tests/cpp/data/test_iterative_dmatrix.cu b/tests/cpp/data/test_iterative_dmatrix.cu index 26ad75e3b472..107aafc6838b 100644 --- a/tests/cpp/data/test_iterative_dmatrix.cu +++ b/tests/cpp/data/test_iterative_dmatrix.cu @@ -3,7 +3,11 @@ */ #include +#include +#include #include // for dynamic_pointer_cast +#include +#include #include "../../../src/common/io.h" // for AlignedFileWriteStream #include "../../../src/data/device_adapter.cuh" diff --git a/tests/cpp/data/test_metainfo.cc b/tests/cpp/data/test_metainfo.cc index a788ab9ec56d..9ad59506163d 100644 --- a/tests/cpp/data/test_metainfo.cc +++ b/tests/cpp/data/test_metainfo.cc @@ -7,8 +7,11 @@ #include #include +#include #include #include +#include +#include #include "../collective/test_worker.h" // for TestDistributedGlobal #include "../filesystem.h" // TemporaryDirectory @@ -60,16 +63,13 @@ TEST(MetaInfo, GetSetFeature) { size_t constexpr kCols = 19; std::vector types(kCols, u8"float"); - std::vector c_types(kCols); + std::vector c_types(kCols); std::transform(types.cbegin(), types.cend(), c_types.begin(), [](auto const &str) { return str.c_str(); }); info.num_col_ = 1; - EXPECT_THROW( - info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size()), - dmlc::Error); + EXPECT_THROW(info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size()), dmlc::Error); info.num_col_ = kCols; - EXPECT_NO_THROW( - info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size())); + EXPECT_NO_THROW(info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size())); // Test clear. info.SetFeatureInfo("feature_type", nullptr, 0); @@ -127,12 +127,12 @@ TEST(MetaInfo, SaveLoadBinary) { xgboost::MetaInfo info; xgboost::Context ctx; - uint64_t constexpr kRows { 64 }, kCols { 32 }; + uint64_t constexpr kRows{64}, kCols{32}; auto generator = []() { - static float f = 0; - return f++; - }; - std::vector values (kRows); + static float f = 0; + return f++; + }; + std::vector values(kRows); std::generate(values.begin(), values.end(), generator); info.SetInfo(ctx, "label", Make1dInterfaceTest(values.data(), kRows)); info.SetInfo(ctx, "weight", Make1dInterfaceTest(values.data(), kRows)); @@ -143,30 +143,26 @@ TEST(MetaInfo, SaveLoadBinary) { auto featname = u8"特征名"; std::vector types(kCols, u8"float"); - std::vector c_types(kCols); + std::vector c_types(kCols); std::transform(types.cbegin(), types.cend(), c_types.begin(), [](auto const &str) { return str.c_str(); }); info.SetFeatureInfo(u8"feature_type", c_types.data(), c_types.size()); std::vector names(kCols, featname); - std::vector c_names(kCols); + std::vector c_names(kCols); std::transform(names.cbegin(), names.cend(), c_names.begin(), [](auto const &str) { return str.c_str(); }); - info.SetFeatureInfo(u8"feature_name", c_names.data(), c_names.size());; + info.SetFeatureInfo(u8"feature_name", c_names.data(), c_names.size()); common::TemporaryDirectory tempdir; const std::string tmp_file = tempdir.Str() + "/metainfo.binary"; { - std::unique_ptr fs { - dmlc::Stream::Create(tmp_file.c_str(), "w") - }; + std::unique_ptr fs{dmlc::Stream::Create(tmp_file.c_str(), "w")}; info.SaveBinary(fs.get()); } { // Round-trip test - std::unique_ptr fs { - dmlc::Stream::Create(tmp_file.c_str(), "r") - }; + std::unique_ptr fs{dmlc::Stream::Create(tmp_file.c_str(), "r")}; xgboost::MetaInfo inforead; inforead.LoadBinary(fs.get()); ASSERT_EQ(inforead.num_row_, kRows); @@ -190,16 +186,12 @@ TEST(MetaInfo, SaveLoadBinary) { inforead.feature_type_names.cend(), [](auto const &str) { return str == u8"float"; })); auto h_ft = inforead.feature_types.HostSpan(); - EXPECT_TRUE(std::all_of(h_ft.cbegin(), h_ft.cend(), [](auto f) { - return f == xgboost::FeatureType::kNumerical; - })); + EXPECT_TRUE(std::all_of(h_ft.cbegin(), h_ft.cend(), + [](auto f) { return f == xgboost::FeatureType::kNumerical; })); EXPECT_EQ(inforead.feature_names.size(), kCols); - EXPECT_TRUE(std::all_of(inforead.feature_names.cbegin(), - inforead.feature_names.cend(), - [=](auto const& str) { - return str == featname; - })); + EXPECT_TRUE(std::all_of(inforead.feature_names.cbegin(), inforead.feature_names.cend(), + [=](auto const &str) { return str == featname; })); } } @@ -226,13 +218,12 @@ TEST(MetaInfo, LoadQid) { std::unique_ptr dmat( xgboost::DMatrix::Load(tmp_file + "?format=libsvm", true, xgboost::DataSplitMode::kRow)); - const xgboost::MetaInfo& info = dmat->Info(); + const xgboost::MetaInfo &info = dmat->Info(); const std::vector expected_group_ptr{0, 4, 8, 12}; CHECK(info.group_ptr_ == expected_group_ptr); - const std::vector expected_offset{ - 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 - }; + const std::vector expected_offset{0, 5, 10, 15, 20, 25, 30, + 35, 40, 45, 50, 55, 60}; const std::vector expected_data{ xgboost::Entry(1, 1), xgboost::Entry(2, 1), xgboost::Entry(3, 0), xgboost::Entry(4, 0.2), xgboost::Entry(5, 0), xgboost::Entry(1, 0), @@ -285,7 +276,7 @@ TEST(MetaInfo, Validate) { info.num_row_ = 10; info.num_nonzero_ = 12; info.num_col_ = 3; - std::vector groups (11); + std::vector groups(11); Context ctx; info.SetInfo(ctx, "group", Make1dInterfaceTest(groups.data(), groups.size())); EXPECT_THROW(info.Validate(FstCU()), dmlc::Error); diff --git a/tests/cpp/data/test_metainfo.cu b/tests/cpp/data/test_metainfo.cu index 4f02dfddc6a0..c3db81f13c7c 100644 --- a/tests/cpp/data/test_metainfo.cu +++ b/tests/cpp/data/test_metainfo.cu @@ -6,6 +6,9 @@ #include #include +#include +#include + #include "../../../src/common/device_helpers.cuh" #include "test_array_interface.h" #include "test_metainfo.h" @@ -13,7 +16,8 @@ namespace xgboost { template -std::string PrepareData(std::string typestr, thrust::device_vector* out, const size_t kRows=16) { +std::string PrepareData(std::string typestr, thrust::device_vector* out, + const size_t kRows = 16) { out->resize(kRows); auto& d_data = *out; @@ -21,9 +25,9 @@ std::string PrepareData(std::string typestr, thrust::device_vector* out, cons d_data[i] = i * 2.0; } - Json column { Object() }; + Json column{Object()}; - std::vector j_shape {Json(Integer(static_cast(kRows)))}; + std::vector j_shape{Json(Integer(static_cast(kRows)))}; column["shape"] = Array(j_shape); column["strides"] = Array(std::vector{Json(Integer{static_cast(sizeof(T))})}); column["version"] = 3; @@ -82,9 +86,7 @@ TEST(MetaInfo, FromInterface) { EXPECT_EQ(info.group_ptr_, expected_group_ptr); } -TEST(MetaInfo, GPUStridedData) { - TestMetaInfoStridedData(DeviceOrd::CUDA(0)); -} +TEST(MetaInfo, GPUStridedData) { TestMetaInfoStridedData(DeviceOrd::CUDA(0)); } TEST(MetaInfo, Group) { cudaSetDevice(0); @@ -139,7 +141,6 @@ TEST(MetaInfo, GPUQid) { } } - TEST(MetaInfo, DeviceExtend) { dh::safe_cuda(cudaSetDevice(0)); size_t const kRows = 100; diff --git a/tests/cpp/data/test_metainfo.h b/tests/cpp/data/test_metainfo.h index 941b6e6577dd..91486be42d7d 100644 --- a/tests/cpp/data/test_metainfo.h +++ b/tests/cpp/data/test_metainfo.h @@ -1,8 +1,8 @@ /** * Copyright 2021-2024, XGBoost Contributors */ -#ifndef XGBOOST_TESTS_CPP_DATA_TEST_METAINFO_H_ -#define XGBOOST_TESTS_CPP_DATA_TEST_METAINFO_H_ +#ifndef TESTS_CPP_DATA_TEST_METAINFO_H_ +#define TESTS_CPP_DATA_TEST_METAINFO_H_ #include #include #include @@ -73,4 +73,4 @@ inline void TestMetaInfoStridedData(DeviceOrd device) { } } } // namespace xgboost -#endif // XGBOOST_TESTS_CPP_DATA_TEST_METAINFO_H_ +#endif // TESTS_CPP_DATA_TEST_METAINFO_H_ diff --git a/tests/cpp/data/test_simple_dmatrix.cc b/tests/cpp/data/test_simple_dmatrix.cc index 1fb8c4e75c39..07db723573f5 100644 --- a/tests/cpp/data/test_simple_dmatrix.cc +++ b/tests/cpp/data/test_simple_dmatrix.cc @@ -6,6 +6,8 @@ #include // std::array #include // std::numeric_limits #include // std::unique_ptr +#include +#include #include "../../../src/data/adapter.h" // ArrayAdapter #include "../../../src/data/simple_dmatrix.h" // SimpleDMatrix @@ -164,8 +166,7 @@ TEST(SimpleDMatrix, FromDense) { int n = 2; std::vector data = {1, 2, 3, 4, 5, 6}; data::DenseAdapter adapter(data.data(), m, n); - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - -1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); EXPECT_EQ(dmat.Info().num_col_, 2); EXPECT_EQ(dmat.Info().num_row_, 3); EXPECT_EQ(dmat.Info().num_nonzero_, 6); @@ -232,8 +233,7 @@ TEST(SimpleDMatrix, FromFile) { auto verify_batch = [kExpectedNumRow](SparsePage const &page) { auto batch = page.GetView(); EXPECT_EQ(batch.Size(), kExpectedNumRow); - EXPECT_EQ(page.offset.HostVector(), - std::vector({0, 3, 6, 9, 12, 15, 15})); + EXPECT_EQ(page.offset.HostVector(), std::vector({0, 3, 6, 9, 12, 15, 15})); EXPECT_EQ(page.base_rowid, 0); for (auto i = 0ull; i < batch.Size() - 1; i++) { @@ -251,8 +251,7 @@ TEST(SimpleDMatrix, FromFile) { constexpr bst_feature_t kCols = 5; data::FileAdapter adapter(parser.get()); - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - 1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), 1); ASSERT_EQ(dmat.Info().num_col_, kCols); for (auto &batch : dmat.GetBatches()) { @@ -261,33 +260,33 @@ TEST(SimpleDMatrix, FromFile) { } TEST(SimpleDMatrix, Slice) { - size_t constexpr kRows {16}; - size_t constexpr kCols {8}; - size_t constexpr kClasses {3}; + size_t constexpr kRows{16}; + size_t constexpr kCols{8}; + size_t constexpr kClasses{3}; auto p_m = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true); - auto& weights = p_m->Info().weights_.HostVector(); + auto &weights = p_m->Info().weights_.HostVector(); weights.resize(kRows); std::iota(weights.begin(), weights.end(), 0.0f); - auto& lower = p_m->Info().labels_lower_bound_.HostVector(); - auto& upper = p_m->Info().labels_upper_bound_.HostVector(); + auto &lower = p_m->Info().labels_lower_bound_.HostVector(); + auto &upper = p_m->Info().labels_upper_bound_.HostVector(); lower.resize(kRows); upper.resize(kRows); std::iota(lower.begin(), lower.end(), 0.0f); std::iota(upper.begin(), upper.end(), 1.0f); - auto& margin = p_m->Info().base_margin_; + auto &margin = p_m->Info().base_margin_; margin = decltype(p_m->Info().base_margin_){{kRows, kClasses}, DeviceOrd::CPU()}; - std::array ridxs {1, 3, 5}; - std::unique_ptr out { p_m->Slice(ridxs) }; + std::array ridxs{1, 3, 5}; + std::unique_ptr out{p_m->Slice(ridxs)}; ASSERT_EQ(out->Info().labels.Size(), ridxs.size()); ASSERT_EQ(out->Info().labels_lower_bound_.Size(), ridxs.size()); ASSERT_EQ(out->Info().labels_upper_bound_.Size(), ridxs.size()); ASSERT_EQ(out->Info().base_margin_.Size(), ridxs.size() * kClasses); - for (auto const& in_batch : p_m->GetBatches()) { + for (auto const &in_batch : p_m->GetBatches()) { auto in_page = in_batch.GetView(); for (auto const &out_batch : out->GetBatches()) { auto out_page = out_batch.GetView(); @@ -334,29 +333,29 @@ TEST(SimpleDMatrix, Slice) { } TEST(SimpleDMatrix, SliceCol) { - size_t constexpr kRows {16}; - size_t constexpr kCols {8}; - size_t constexpr kClasses {3}; + size_t constexpr kRows{16}; + size_t constexpr kCols{8}; + size_t constexpr kClasses{3}; auto p_m = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true); - auto& weights = p_m->Info().weights_.HostVector(); + auto &weights = p_m->Info().weights_.HostVector(); weights.resize(kRows); std::iota(weights.begin(), weights.end(), 0.0f); - auto& lower = p_m->Info().labels_lower_bound_.HostVector(); - auto& upper = p_m->Info().labels_upper_bound_.HostVector(); + auto &lower = p_m->Info().labels_lower_bound_.HostVector(); + auto &upper = p_m->Info().labels_upper_bound_.HostVector(); lower.resize(kRows); upper.resize(kRows); std::iota(lower.begin(), lower.end(), 0.0f); std::iota(upper.begin(), upper.end(), 1.0f); - auto& margin = p_m->Info().base_margin_; + auto &margin = p_m->Info().base_margin_; margin = decltype(p_m->Info().base_margin_){{kRows, kClasses}, DeviceOrd::CPU()}; - auto constexpr kSlices {2}; - auto constexpr kSliceSize {4}; + auto constexpr kSlices{2}; + auto constexpr kSliceSize{4}; for (auto slice = 0; slice < kSlices; slice++) { - std::unique_ptr out { p_m->SliceCol(kSlices, slice) }; + std::unique_ptr out{p_m->SliceCol(kSlices, slice)}; ASSERT_EQ(out->Info().labels.Size(), kRows); ASSERT_EQ(out->Info().labels_lower_bound_.Size(), kRows); ASSERT_EQ(out->Info().labels_upper_bound_.Size(), kRows); @@ -380,7 +379,8 @@ TEST(SimpleDMatrix, SliceCol) { out->Info().labels_lower_bound_.HostVector().at(i)); ASSERT_EQ(p_m->Info().labels_upper_bound_.HostVector().at(i), out->Info().labels_upper_bound_.HostVector().at(i)); - ASSERT_EQ(p_m->Info().weights_.HostVector().at(i), out->Info().weights_.HostVector().at(i)); + ASSERT_EQ(p_m->Info().weights_.HostVector().at(i), + out->Info().weights_.HostVector().at(i)); auto out_margin = out->Info().base_margin_.View(DeviceOrd::CPU()); auto in_margin = margin.View(DeviceOrd::CPU()); @@ -402,12 +402,12 @@ TEST(SimpleDMatrix, SaveLoadBinary) { common::TemporaryDirectory tempdir; const std::string tmp_file = tempdir.Str() + "/simple.libsvm"; CreateSimpleTestData(tmp_file); - xgboost::DMatrix * dmat = xgboost::DMatrix::Load(UriSVM(tmp_file)); - data::SimpleDMatrix *simple_dmat = dynamic_cast(dmat); + xgboost::DMatrix *dmat = xgboost::DMatrix::Load(UriSVM(tmp_file)); + data::SimpleDMatrix *simple_dmat = dynamic_cast(dmat); const std::string tmp_binfile = tempdir.Str() + "/csr_source.binary"; simple_dmat->SaveToLocalFile(tmp_binfile); - xgboost::DMatrix * dmat_read = xgboost::DMatrix::Load(tmp_binfile); + xgboost::DMatrix *dmat_read = xgboost::DMatrix::Load(tmp_binfile); EXPECT_EQ(dmat->Info().num_col_, dmat_read->Info().num_col_); EXPECT_EQ(dmat->Info().num_row_, dmat_read->Info().num_row_); @@ -441,8 +441,8 @@ TEST(SimpleDMatrix, Threads) { namespace { void VerifyColumnSplit() { - size_t constexpr kRows {16}; - size_t constexpr kCols {8}; + size_t constexpr kRows{16}; + size_t constexpr kCols{8}; auto p_fmat = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(false, DataSplitMode::kCol); ASSERT_EQ(p_fmat->Info().num_col_, kCols * collective::GetWorldSize()); diff --git a/tests/cpp/data/test_simple_dmatrix.cu b/tests/cpp/data/test_simple_dmatrix.cu index 6bf76e37acc1..078ab5b03835 100644 --- a/tests/cpp/data/test_simple_dmatrix.cu +++ b/tests/cpp/data/test_simple_dmatrix.cu @@ -1,12 +1,17 @@ // Copyright by Contributors +#include #include -#include "../../../src/data/simple_dmatrix.h" -#include +#include +#include +#include +#include + +#include "../../../src/data/array_interface.h" #include "../../../src/data/device_adapter.cuh" +#include "../../../src/data/simple_dmatrix.h" #include "../helpers.h" #include "test_array_interface.h" -#include "../../../src/data/array_interface.h" using namespace xgboost; // NOLINT @@ -25,8 +30,7 @@ TEST(SimpleDMatrix, FromColumnarDenseBasic) { Json::Dump(column_arr, &str); data::CudfAdapter adapter(str); - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - -1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); EXPECT_EQ(dmat.Info().num_col_, 2); EXPECT_EQ(dmat.Info().num_row_, 16); EXPECT_EQ(dmat.Info().num_nonzero_, 32); @@ -64,8 +68,7 @@ TEST(SimpleDMatrix, FromColumnarDense) { // no missing value { data::CudfAdapter adapter(str); - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - -1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); TestDenseColumn(&dmat, kRows, kCols); } @@ -84,8 +87,7 @@ TEST(SimpleDMatrix, FromColumnarDense) { d_data_0[3] = std::numeric_limits::quiet_NaN(); ASSERT_TRUE(std::isnan(d_data_0[3])); // removes 6.0 data::CudfAdapter adapter(str); - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - -1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); ASSERT_EQ(dmat.Info().num_nonzero_, kRows * kCols - 1); ASSERT_EQ(dmat.Info().num_row_, kRows); ASSERT_EQ(dmat.Info().num_col_, kCols); @@ -98,8 +100,7 @@ TEST(SimpleDMatrix, FromColumnarWithEmptyRows) { std::vector v_columns(kCols); std::vector> columns_data(kCols); - std::vector> column_bitfields( - kCols); + std::vector> column_bitfields(kCols); RBitField8::value_type constexpr kUCOne = 1; @@ -115,9 +116,8 @@ TEST(SimpleDMatrix, FromColumnarWithEmptyRows) { ASSERT_EQ(data.size(), kRows); auto p_d_data = raw_pointer_cast(data.data()); - std::vector j_data{ - Json(Integer(reinterpret_cast(p_d_data))), - Json(Boolean(false))}; + std::vector j_data{Json(Integer(reinterpret_cast(p_d_data))), + Json(Boolean(false))}; col["data"] = j_data; std::vector j_shape{Json(Integer(static_cast(kRows)))}; col["shape"] = Array(j_shape); @@ -144,12 +144,10 @@ TEST(SimpleDMatrix, FromColumnarWithEmptyRows) { } } - j_mask["data"] = std::vector{ - Json( - Integer(reinterpret_cast(mask_storage.data().get()))), - Json(Boolean(false))}; - j_mask["shape"] = Array( - std::vector{Json(Integer(static_cast(kRows)))}); + j_mask["data"] = + std::vector{Json(Integer(reinterpret_cast(mask_storage.data().get()))), + Json(Boolean(false))}; + j_mask["shape"] = Array(std::vector{Json(Integer(static_cast(kRows)))}); j_mask["typestr"] = String("|i1"); } @@ -158,8 +156,7 @@ TEST(SimpleDMatrix, FromColumnarWithEmptyRows) { Json::Dump(column_arr, &str); data::CudfAdapter adapter(str); - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - -1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); for (auto& batch : dmat.GetBatches()) { auto page = batch.GetView(); @@ -189,7 +186,7 @@ TEST(SimpleCSRSource, FromColumnarSparse) { auto& mask = column_bitfields[0]; mask.resize(8); - for (auto && j : mask) { + for (auto&& j : mask) { j = ~0; } // the 2^th entry of first column is invalid @@ -201,7 +198,7 @@ TEST(SimpleCSRSource, FromColumnarSparse) { auto& mask = column_bitfields[1]; mask.resize(8); - for (auto && j : mask) { + for (auto&& j : mask) { j = ~0; } // the 19^th entry of second column is invalid @@ -222,11 +219,10 @@ TEST(SimpleCSRSource, FromColumnarSparse) { column["version"] = 3; column["typestr"] = String(" j_data { - Json(Integer(reinterpret_cast(p_d_data))), - Json(Boolean(false))}; + std::vector j_data{Json(Integer(reinterpret_cast(p_d_data))), + Json(Boolean(false))}; column["data"] = j_data; - std::vector j_shape {Json(Integer(static_cast(kRows)))}; + std::vector j_shape{Json(Integer(static_cast(kRows)))}; column["shape"] = Array(j_shape); column["version"] = 3; column["typestr"] = String("{ - Json(Integer(reinterpret_cast(column_bitfields[c].data().get()))), - Json(Boolean(false))}; + Json(Integer(reinterpret_cast(column_bitfields[c].data().get()))), + Json(Boolean(false))}; j_mask["shape"] = Array(std::vector{Json(Integer(static_cast(kRows)))}); j_mask["typestr"] = String("|i1"); } - Json column_arr {Array(j_columns)}; + Json column_arr{Array(j_columns)}; std::string str; Json::Dump(column_arr, &str); @@ -251,7 +247,7 @@ TEST(SimpleCSRSource, FromColumnarSparse) { data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); ASSERT_EQ(dmat.Info().num_row_, kRows); - ASSERT_EQ(dmat.Info().num_nonzero_, (kRows*kCols)-2); + ASSERT_EQ(dmat.Info().num_nonzero_, (kRows * kCols) - 2); } { @@ -272,8 +268,7 @@ TEST(SimpleCSRSource, FromColumnarSparse) { // no missing value, but has NaN data::CudfAdapter adapter(str); columns_data[0][4] = std::numeric_limits::quiet_NaN(); // 0^th column 4^th row - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - -1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); ASSERT_TRUE(std::isnan(columns_data[0][4])); // Two invalid entries and one NaN, in CSC @@ -283,7 +278,6 @@ TEST(SimpleCSRSource, FromColumnarSparse) { } } - TEST(SimpleDMatrix, FromColumnarSparseBasic) { constexpr size_t kRows{16}; std::vector columns; @@ -299,8 +293,7 @@ TEST(SimpleDMatrix, FromColumnarSparseBasic) { Json::Dump(column_arr, &str); data::CudfAdapter adapter(str); - data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), - -1); + data::SimpleDMatrix dmat(&adapter, std::numeric_limits::quiet_NaN(), -1); EXPECT_EQ(dmat.Info().num_col_, 2); EXPECT_EQ(dmat.Info().num_row_, 16); EXPECT_EQ(dmat.Info().num_nonzero_, 32); @@ -317,11 +310,10 @@ TEST(SimpleDMatrix, FromColumnarSparseBasic) { } } - -TEST(SimpleDMatrix, FromCupy){ +TEST(SimpleDMatrix, FromCupy) { int rows = 50; int cols = 10; - thrust::device_vector< float> data(rows*cols); + thrust::device_vector data(rows * cols); auto json_array_interface = Generate2dArrayInterface(rows, cols, "()) { auto page = batch.GetView(); @@ -343,10 +335,10 @@ TEST(SimpleDMatrix, FromCupy){ } } -TEST(SimpleDMatrix, FromCupySparse){ +TEST(SimpleDMatrix, FromCupySparse) { int rows = 2; int cols = 2; - thrust::device_vector< float> data(rows*cols); + thrust::device_vector data(rows * cols); auto json_array_interface = Generate2dArrayInterface(rows, cols, "::quiet_NaN(); data[2] = std::numeric_limits::quiet_NaN(); diff --git a/tests/cpp/data/test_sparse_page_dmatrix.cc b/tests/cpp/data/test_sparse_page_dmatrix.cc index ad54d3fc8459..a129b4992716 100644 --- a/tests/cpp/data/test_sparse_page_dmatrix.cc +++ b/tests/cpp/data/test_sparse_page_dmatrix.cc @@ -7,7 +7,10 @@ #include // for path #include // for future, async +#include // for numeric_limits +#include // for string #include // for sleep_for +#include // for vector #include "../../../src/common/io.h" #include "../../../src/data/batch_utils.h" // for MatchingPageBytes @@ -307,7 +310,7 @@ TEST(SparsePageDMatrix, ThreadSafetyException) { std::vector> waiting; - std::atomic exception {false}; + std::atomic exception{false}; for (int32_t i = 0; i < threads; ++i) { waiting.emplace_back(std::async(std::launch::async, [&]() { @@ -320,11 +323,10 @@ TEST(SparsePageDMatrix, ThreadSafetyException) { })); } - using namespace std::chrono_literals; + using namespace std::chrono_literals; // NOLINT(build/namespaces) - while (std::any_of(waiting.cbegin(), waiting.cend(), [](auto const &f) { - return f.wait_for(0ms) != std::future_status::ready; - })) { + while (std::any_of(waiting.cbegin(), waiting.cend(), + [](auto const &f) { return f.wait_for(0ms) != std::future_status::ready; })) { std::this_thread::sleep_for(50ms); } diff --git a/tests/cpp/data/test_sparse_page_dmatrix.cu b/tests/cpp/data/test_sparse_page_dmatrix.cu index d3e29ca31cb1..1b5555adbd0e 100644 --- a/tests/cpp/data/test_sparse_page_dmatrix.cu +++ b/tests/cpp/data/test_sparse_page_dmatrix.cu @@ -3,6 +3,10 @@ */ #include // for DMatrix +#include +#include +#include + #include "../../../src/common/compressed_iterator.h" #include "../../../src/data/ellpack_page.cuh" #include "../../../src/data/ellpack_page.h" diff --git a/tests/cpp/data/test_sparse_page_raw_format.cc b/tests/cpp/data/test_sparse_page_raw_format.cc index 7e9dc05fd66f..bca39f9fa914 100644 --- a/tests/cpp/data/test_sparse_page_raw_format.cc +++ b/tests/cpp/data/test_sparse_page_raw_format.cc @@ -14,7 +14,8 @@ #include "xgboost/context.h" // for Context namespace xgboost::data { -template void TestSparsePageRawFormat() { +template +void TestSparsePageRawFormat() { std::unique_ptr> format{CreatePageFormat("raw")}; Context ctx; @@ -38,8 +39,7 @@ template void TestSparsePageRawFormat() { std::make_unique(path.c_str(), 0, n_bytes)}; format->Read(&page, fi.get()); for (size_t i = 0; i < orig.data.Size(); ++i) { - ASSERT_EQ(page.data.HostVector()[i].fvalue, - orig.data.HostVector()[i].fvalue); + ASSERT_EQ(page.data.HostVector()[i].fvalue, orig.data.HostVector()[i].fvalue); ASSERT_EQ(page.data.HostVector()[i].index, orig.data.HostVector()[i].index); } for (size_t i = 0; i < orig.offset.Size(); ++i) { @@ -48,15 +48,9 @@ template void TestSparsePageRawFormat() { ASSERT_EQ(page.base_rowid, orig.base_rowid); } -TEST(SparsePageRawFormat, SparsePage) { - TestSparsePageRawFormat(); -} +TEST(SparsePageRawFormat, SparsePage) { TestSparsePageRawFormat(); } -TEST(SparsePageRawFormat, CSCPage) { - TestSparsePageRawFormat(); -} +TEST(SparsePageRawFormat, CSCPage) { TestSparsePageRawFormat(); } -TEST(SparsePageRawFormat, SortedCSCPage) { - TestSparsePageRawFormat(); -} +TEST(SparsePageRawFormat, SortedCSCPage) { TestSparsePageRawFormat(); } } // namespace xgboost::data diff --git a/tests/cpp/filesystem.cc b/tests/cpp/filesystem.cc index 42a65d646f31..4a0be6b86feb 100644 --- a/tests/cpp/filesystem.cc +++ b/tests/cpp/filesystem.cc @@ -6,6 +6,8 @@ #include #include // for path, temp_directory_path +#include +#include #if !defined(xgboost_IS_WIN) diff --git a/tests/cpp/filesystem.h b/tests/cpp/filesystem.h index 576a6f959afa..15d17e0a7cde 100644 --- a/tests/cpp/filesystem.h +++ b/tests/cpp/filesystem.h @@ -1,10 +1,11 @@ /** * Copyright 2022-2025, XGBoost Contributors */ -#ifndef XGBOOST_TESTS_CPP_FILESYSTEM_H -#define XGBOOST_TESTS_CPP_FILESYSTEM_H +#ifndef TESTS_CPP_FILESYSTEM_H_ +#define TESTS_CPP_FILESYSTEM_H_ #include // for path +#include // for string namespace xgboost::common { class TemporaryDirectory { @@ -22,4 +23,4 @@ class TemporaryDirectory { }; } // namespace xgboost::common -#endif // XGBOOST_TESTS_CPP_FILESYSTEM_H +#endif // TESTS_CPP_FILESYSTEM_H_ diff --git a/tests/cpp/gbm/test_gblinear.cc b/tests/cpp/gbm/test_gblinear.cc index 731c22f172c9..5158f70d50a4 100644 --- a/tests/cpp/gbm/test_gblinear.cc +++ b/tests/cpp/gbm/test_gblinear.cc @@ -5,6 +5,7 @@ #include // for FeatureMap #include +#include #include "../helpers.h" #include "xgboost/context.h" @@ -21,7 +22,7 @@ TEST(GBLinear, JsonIO) { std::unique_ptr gbm{ CreateTrainedGBM("gblinear", Args{}, kRows, kCols, &mparam, &ctx)}; - Json model { Object() }; + Json model{Object()}; gbm->SaveModel(&model); ASSERT_TRUE(IsA(model)); diff --git a/tests/cpp/gbm/test_gblinear.cu b/tests/cpp/gbm/test_gblinear.cu index 94abce062584..3177ebcd1f09 100644 --- a/tests/cpp/gbm/test_gblinear.cu +++ b/tests/cpp/gbm/test_gblinear.cu @@ -7,8 +7,9 @@ #include // for Learner #include // for transform -#include // for string -#include // for swap +#include +#include // for string +#include // for swap #include "../helpers.h" // for RandomDataGenerator diff --git a/tests/cpp/gbm/test_gbtree.cc b/tests/cpp/gbm/test_gbtree.cc index fde9bc3d602f..13e3950c7189 100644 --- a/tests/cpp/gbm/test_gbtree.cc +++ b/tests/cpp/gbm/test_gbtree.cc @@ -7,10 +7,13 @@ #include // for Json, Object #include // for Learner -#include // for numeric_limits +#include // for numeric_limits +#include #include // for shared_ptr #include // for optional #include // for string +#include +#include #include "../../../src/data/proxy_dmatrix.h" // for DMatrixProxy #include "../../../src/gbm/gbtree.h" @@ -26,12 +29,11 @@ TEST(GBTree, SelectTreeMethod) { Context ctx; LearnerModelParam mparam{MakeMP(kCols, .5, 1)}; - std::unique_ptr p_gbm { - GradientBooster::Create("gbtree", &ctx, &mparam)}; - auto& gbtree = dynamic_cast (*p_gbm); + std::unique_ptr p_gbm{GradientBooster::Create("gbtree", &ctx, &mparam)}; + auto& gbtree = dynamic_cast(*p_gbm); // Test if `tree_method` can be set - Args args {{"tree_method", "approx"}}; + Args args{{"tree_method", "approx"}}; gbtree.Configure({args.cbegin(), args.cend()}); gbtree.Configure(args); @@ -59,9 +61,8 @@ TEST(GBTree, PredictionCache) { Context ctx; LearnerModelParam mparam{MakeMP(kCols, .5, 1)}; - std::unique_ptr p_gbm { - GradientBooster::Create("gbtree", &ctx, &mparam)}; - auto& gbtree = dynamic_cast (*p_gbm); + std::unique_ptr p_gbm{GradientBooster::Create("gbtree", &ctx, &mparam)}; + auto& gbtree = dynamic_cast(*p_gbm); gbtree.Configure({{"tree_method", "hist"}}); auto p_m = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(); @@ -112,8 +113,7 @@ TEST(GBTree, WrongUpdater) { learner->SetParams(Args{{"tree_method", "hist"}, {"process_type", "update"}}); ASSERT_THROW(learner->UpdateOneIter(0, p_dmat), dmlc::Error); // Prune can not be used for learning new tree. - learner->SetParams( - Args{{"tree_method", "prune"}, {"process_type", "default"}}); + learner->SetParams(Args{{"tree_method", "prune"}, {"process_type", "default"}}); ASSERT_THROW(learner->UpdateOneIter(0, p_dmat), dmlc::Error); } @@ -344,7 +344,7 @@ TEST(Dart, JsonIO) { std::unique_ptr gbm{ CreateTrainedGBM("dart", Args{}, kRows, kCols, &mparam, &ctx)}; - Json model {Object()}; + Json model{Object()}; model["model"] = Object(); auto& j_model = model["model"]; model["config"] = Object(); @@ -436,15 +436,12 @@ INSTANTIATE_TEST_SUITE_P(PredictorTypes, Dart, testing::Values("CPU", "GPU")); INSTANTIATE_TEST_SUITE_P(PredictorTypes, Dart, testing::Values("CPU")); #endif // defined(XGBOOST_USE_CUDA) - std::pair TestModelSlice(std::string booster) { size_t constexpr kRows = 1000, kCols = 100, kForest = 2, kClasses = 3; auto m = RandomDataGenerator{kRows, kCols, 0}.Classes(kClasses).GenerateDMatrix(true); int32_t kIters = 10; - std::unique_ptr learner { - Learner::Create({m}) - }; + std::unique_ptr learner{Learner::Create({m})}; learner->SetParams(Args{{"booster", booster}, {"tree_method", "hist"}, {"num_parallel_tree", std::to_string(kForest)}, @@ -463,7 +460,7 @@ std::pair TestModelSlice(std::string booster) { bool out_of_bound = false; size_t constexpr kSliceStart = 2, kSliceEnd = 8, kStep = 3; - std::unique_ptr sliced {learner->Slice(kSliceStart, kSliceEnd, kStep, &out_of_bound)}; + std::unique_ptr sliced{learner->Slice(kSliceStart, kSliceEnd, kStep, &out_of_bound)}; Json sliced_model{Object()}; sliced->SaveModel(&sliced_model); @@ -471,14 +468,15 @@ std::pair TestModelSlice(std::string booster) { if (booster == "gbtree") { return get(model["learner"]["gradient_booster"]["model"]["gbtree_model_param"]); } else { - return get(model["learner"]["gradient_booster"]["gbtree"]["model"]["gbtree_model_param"]); + return get( + model["learner"]["gradient_booster"]["gbtree"]["model"]["gbtree_model_param"]); } }; auto const& model_shape = get_shape(sliced_model); CHECK_EQ(get(model_shape.at("num_trees")), std::to_string(2 * kClasses * kForest)); - Json sliced_config {Object()}; + Json sliced_config{Object()}; sliced->SaveConfig(&sliced_config); // Only num trees is changed if (booster == "gbtree") { @@ -505,17 +503,17 @@ std::pair TestModelSlice(std::string booster) { } }; - auto const &sliced_trees = get_trees(sliced_model); + auto const& sliced_trees = get_trees(sliced_model); CHECK_EQ(sliced_trees.size(), 2 * kClasses * kForest); auto constexpr kLayerSize = kClasses * kForest; - auto const &sliced_info = get_info(sliced_model); + auto const& sliced_info = get_info(sliced_model); for (size_t layer = 0; layer < 2; ++layer) { for (size_t j = 0; j < kClasses; ++j) { for (size_t k = 0; k < kForest; ++k) { auto idx = layer * kLayerSize + j * kForest + k; - auto const &group = get(sliced_info.at(idx)); + auto const& group = get(sliced_info.at(idx)); CHECK_EQ(static_cast(group), j); } } @@ -554,15 +552,14 @@ std::pair TestModelSlice(std::string booster) { return std::make_pair(model, sliced_model); } -TEST(GBTree, Slice) { - TestModelSlice("gbtree"); -} +TEST(GBTree, Slice) { TestModelSlice("gbtree"); } TEST(Dart, Slice) { Json model, sliced_model; std::tie(model, sliced_model) = TestModelSlice("dart"); auto const& weights = get(model["learner"]["gradient_booster"]["weight_drop"]); - auto const& trees = get(model["learner"]["gradient_booster"]["gbtree"]["model"]["trees"]); + auto const& trees = + get(model["learner"]["gradient_booster"]["gbtree"]["model"]["trees"]); ASSERT_EQ(weights.size(), trees.size()); } @@ -570,7 +567,7 @@ TEST(GBTree, FeatureScore) { size_t n_samples = 1000, n_features = 10, n_classes = 4; auto m = RandomDataGenerator{n_samples, n_features, 0.5}.Classes(n_classes).GenerateDMatrix(true); - std::unique_ptr learner{ Learner::Create({m}) }; + std::unique_ptr learner{Learner::Create({m})}; learner->SetParam("num_class", std::to_string(n_classes)); learner->Configure(); @@ -686,7 +683,8 @@ TEST(GBTree, InplacePredictionError) { auto test_qdm_err = [&](std::string booster, Context const* ctx) { std::shared_ptr p_fmat; bst_bin_t max_bins = 16; - auto rng = RandomDataGenerator{n_samples, n_features, 0.5f}.Device(ctx->Device()).Bins(max_bins); + auto rng = + RandomDataGenerator{n_samples, n_features, 0.5f}.Device(ctx->Device()).Bins(max_bins); if (ctx->IsCPU()) { p_fmat = rng.GenerateQuantileDMatrix(true); } else { @@ -695,7 +693,7 @@ TEST(GBTree, InplacePredictionError) { #else CHECK(p_fmat); #endif // defined(XGBOOST_USE_CUDA) - }; + } std::unique_ptr learner{Learner::Create({p_fmat})}; learner->SetParams(Args{{"booster", booster}, {"max_bin", std::to_string(max_bins)}, diff --git a/tests/cpp/helpers.cc b/tests/cpp/helpers.cc index b7d54855c67f..fb3f9c37bada 100644 --- a/tests/cpp/helpers.cc +++ b/tests/cpp/helpers.cc @@ -15,6 +15,8 @@ #include // for path #include // for numeric_limits #include // for mt19937 +#include +#include #include "../../src/collective/communicator-inl.h" // for GetRank #include "../../src/data/adapter.h" @@ -34,12 +36,12 @@ // TODO(hcho3): Remove this guard once we require Rapids 25.12+ #if (RMM_VERSION_MAJOR == 25 && RMM_VERSION_MINOR == 12) || RMM_VERSION_MAJOR >= 26 -#include "rmm/mr/per_device_resource.hpp" #include "rmm/mr/cuda_memory_resource.hpp" +#include "rmm/mr/per_device_resource.hpp" #include "rmm/mr/pool_memory_resource.hpp" #else // (RMM_VERSION_MAJOR == 25 && RMM_VERSION_MINOR == 12) || RMM_VERSION_MAJOR >= 26 -#include "rmm/mr/device/per_device_resource.hpp" #include "rmm/mr/device/cuda_memory_resource.hpp" +#include "rmm/mr/device/per_device_resource.hpp" #include "rmm/mr/device/pool_memory_resource.hpp" #endif // (RMM_VERSION_MAJOR == 25 && RMM_VERSION_MINOR == 12) || RMM_VERSION_MAJOR >= 26 @@ -50,9 +52,7 @@ bool FileExists(const std::string& filename) { return stat(filename.c_str(), &st) == 0; } -void CreateSimpleTestData(const std::string& filename) { - CreateBigTestData(filename, 6); -} +void CreateSimpleTestData(const std::string& filename) { CreateBigTestData(filename, 6); } void CreateBigTestData(const std::string& filename, size_t n_entries, bool zero_based) { std::ofstream fo(filename.c_str()); @@ -103,8 +103,7 @@ void CreateTestCSV(std::string const& path, size_t rows, size_t cols) { void CheckObjFunctionImpl(std::unique_ptr const& obj, std::vector preds, std::vector labels, - std::vector weights, - xgboost::MetaInfo const& info, + std::vector weights, xgboost::MetaInfo const& info, std::vector out_grad, std::vector out_hess) { xgboost::HostDeviceVector in_preds(preds); @@ -115,17 +114,16 @@ void CheckObjFunctionImpl(std::unique_ptr const& obj, ASSERT_EQ(gpair.size(), in_preds.Size()); for (int i = 0; i < static_cast(gpair.size()); ++i) { EXPECT_NEAR(gpair[i].GetGrad(), out_grad[i], 0.01) - << "Unexpected grad for pred=" << preds[i] << " label=" << labels[i] - << " weight=" << weights[i]; + << "Unexpected grad for pred=" << preds[i] << " label=" << labels[i] + << " weight=" << weights[i]; EXPECT_NEAR(gpair[i].GetHess(), out_hess[i], 0.01) - << "Unexpected hess for pred=" << preds[i] << " label=" << labels[i] - << " weight=" << weights[i]; + << "Unexpected hess for pred=" << preds[i] << " label=" << labels[i] + << " weight=" << weights[i]; } } void CheckObjFunction(std::unique_ptr const& obj, - std::vector preds, - std::vector labels, + std::vector preds, std::vector labels, std::vector weights, std::vector out_grad, std::vector out_hess) { @@ -140,13 +138,12 @@ void CheckObjFunction(std::unique_ptr const& obj, CheckObjFunctionImpl(obj, preds, labels, weights, info, out_grad, out_hess); } -xgboost::Json CheckConfigReloadImpl(xgboost::Configurable* const configurable, - std::string name) { - xgboost::Json config_0 { xgboost::Object() }; +xgboost::Json CheckConfigReloadImpl(xgboost::Configurable* const configurable, std::string name) { + xgboost::Json config_0{xgboost::Object()}; configurable->SaveConfig(&config_0); configurable->LoadConfig(config_0); - xgboost::Json config_1 { xgboost::Object() }; + xgboost::Json config_1{xgboost::Object()}; configurable->SaveConfig(&config_1); std::string str_0, str_1; @@ -240,8 +237,8 @@ RandomDataGenerator::RandomDataGenerator(bst_idx_t rows, std::size_t cols, float cache_host_ratio_{cuda_impl::AutoHostRatio()} {} void RandomDataGenerator::GenerateLabels(std::shared_ptr p_fmat) const { - RandomDataGenerator{static_cast(p_fmat->Info().num_row_), this->n_targets_, 0.0f}.GenerateDense( - p_fmat->Info().labels.Data()); + RandomDataGenerator{static_cast(p_fmat->Info().num_row_), this->n_targets_, 0.0f} + .GenerateDense(p_fmat->Info().labels.Data()); CHECK_EQ(p_fmat->Info().labels.Size(), this->rows_ * this->n_targets_); p_fmat->Info().labels.Reshape(this->rows_, this->n_targets_); if (device_.IsCUDA()) { @@ -249,15 +246,15 @@ void RandomDataGenerator::GenerateLabels(std::shared_ptr p_fmat) const } } -void RandomDataGenerator::GenerateDense(HostDeviceVector *out) const { +void RandomDataGenerator::GenerateDense(HostDeviceVector* out) const { xgboost::SimpleRealUniformDistribution dist(lower_, upper_); CHECK(out); SimpleLCG lcg{lcg_}; out->Resize(rows_ * cols_, 0); - auto &h_data = out->HostVector(); + auto& h_data = out->HostVector(); float sparsity = sparsity_ * (upper_ - lower_) + lower_; - for (auto &v : h_data) { + for (auto& v : h_data) { auto g = dist(&lcg); if (g < sparsity) { v = std::numeric_limits::quiet_NaN(); @@ -271,14 +268,13 @@ void RandomDataGenerator::GenerateDense(HostDeviceVector *out) const { } } -Json RandomDataGenerator::ArrayInterfaceImpl(HostDeviceVector *storage, - size_t rows, size_t cols) const { +Json RandomDataGenerator::ArrayInterfaceImpl(HostDeviceVector* storage, size_t rows, + size_t cols) const { this->GenerateDense(storage); return GetArrayInterface(storage, rows, cols); } -std::string RandomDataGenerator::GenerateArrayInterface( - HostDeviceVector *storage) const { +std::string RandomDataGenerator::GenerateArrayInterface(HostDeviceVector* storage) const { auto array_interface = this->ArrayInterfaceImpl(storage, rows_, cols_); std::string out; Json::Dump(array_interface, &out); @@ -343,11 +339,11 @@ std::pair, std::string> RandomDataGenerator::GenerateAr } std::string RandomDataGenerator::GenerateColumnarArrayInterface( - std::vector> *data) const { + std::vector>* data) const { CHECK(data); CHECK_EQ(data->size(), cols_); auto& storage = *data; - Json arr { Array() }; + Json arr{Array()}; for (size_t i = 0; i < cols_; ++i) { auto column = this->ArrayInterfaceImpl(&storage[i], rows_, 1); get(arr).emplace_back(column); @@ -357,9 +353,9 @@ std::string RandomDataGenerator::GenerateColumnarArrayInterface( return out; } -void RandomDataGenerator::GenerateCSR( - HostDeviceVector* value, HostDeviceVector* row_ptr, - HostDeviceVector* columns) const { +void RandomDataGenerator::GenerateCSR(HostDeviceVector* value, + HostDeviceVector* row_ptr, + HostDeviceVector* columns) const { auto& h_value = value->HostVector(); auto& h_rptr = row_ptr->HostVector(); auto& h_cols = columns->HostVector(); @@ -713,18 +709,17 @@ ArrayIterForTest::ArrayIterForTest(Context const* ctx, HostDeviceVector c ArrayIterForTest::~ArrayIterForTest() { XGDMatrixFree(proxy_); } -void DMatrixToCSR(DMatrix *dmat, std::vector *p_data, - std::vector *p_row_ptr, - std::vector *p_cids) { - auto &data = *p_data; - auto &row_ptr = *p_row_ptr; - auto &cids = *p_cids; +void DMatrixToCSR(DMatrix* dmat, std::vector* p_data, std::vector* p_row_ptr, + std::vector* p_cids) { + auto& data = *p_data; + auto& row_ptr = *p_row_ptr; + auto& cids = *p_cids; data.resize(dmat->Info().num_nonzero_); cids.resize(data.size()); row_ptr.resize(dmat->Info().num_row_ + 1); SparsePage page; - for (const auto &batch : dmat->GetBatches()) { + for (const auto& batch : dmat->GetBatches()) { page.Push(batch); } @@ -734,13 +729,11 @@ void DMatrixToCSR(DMatrix *dmat, std::vector *p_data, CHECK_EQ(in_offset.size(), row_ptr.size()); std::copy(in_offset.cbegin(), in_offset.cend(), row_ptr.begin()); ASSERT_EQ(in_data.size(), data.size()); - std::transform(in_data.cbegin(), in_data.cend(), data.begin(), [](Entry const& e) { - return e.fvalue; - }); + std::transform(in_data.cbegin(), in_data.cend(), data.begin(), + [](Entry const& e) { return e.fvalue; }); ASSERT_EQ(in_data.size(), cids.size()); - std::transform(in_data.cbegin(), in_data.cend(), cids.begin(), [](Entry const& e) { - return e.index; - }); + std::transform(in_data.cbegin(), in_data.cend(), cids.begin(), + [](Entry const& e) { return e.index; }); } #if defined(XGBOOST_USE_RMM) && XGBOOST_USE_RMM == 1 @@ -765,9 +758,7 @@ class RMMAllocator { ~RMMAllocator() = default; }; -void DeleteRMMResource(RMMAllocator* r) { - delete r; -} +void DeleteRMMResource(RMMAllocator* r) { delete r; } RMMAllocatorPtr SetUpRMMResourceForCppTests(int argc, char** argv) { bool use_rmm_pool = false; @@ -787,7 +778,7 @@ RMMAllocatorPtr SetUpRMMResourceForCppTests(int argc, char** argv) { GlobalConfigThreadLocalStore::Get()->UpdateAllowUnknown(Args{{"use_rmm", "true"}}); return ptr; } -#else // defined(XGBOOST_USE_RMM) && XGBOOST_USE_RMM == 1 +#else // defined(XGBOOST_USE_RMM) && XGBOOST_USE_RMM == 1 class RMMAllocator {}; void DeleteRMMResource(RMMAllocator*) {} @@ -796,4 +787,4 @@ RMMAllocatorPtr SetUpRMMResourceForCppTests(int, char**) { return {nullptr, Dele #endif // !defined(XGBOOST_USE_RMM) || XGBOOST_USE_RMM != 1 std::int32_t DistGpuIdx() { return curt::AllVisibleGPUs() == 1 ? 0 : collective::GetRank(); } -} // namespace xgboost +} // namespace xgboost diff --git a/tests/cpp/helpers.cu b/tests/cpp/helpers.cu index fa7d537ff03b..2f07dd1132e6 100644 --- a/tests/cpp/helpers.cu +++ b/tests/cpp/helpers.cu @@ -6,8 +6,7 @@ #include "helpers.h" namespace xgboost { -CudaArrayIterForTest::CudaArrayIterForTest(float sparsity, size_t rows, - size_t cols, size_t batches) +CudaArrayIterForTest::CudaArrayIterForTest(float sparsity, size_t rows, size_t cols, size_t batches) : ArrayIterForTest{sparsity, rows, cols, batches} { rng_->Device(FstCU()); std::tie(batches_, interface_) = rng_->GenerateArrayInterfaceBatch(&data_, n_batches_); diff --git a/tests/cpp/helpers.h b/tests/cpp/helpers.h index 747d7c450bbf..cfd537d7e05b 100644 --- a/tests/cpp/helpers.h +++ b/tests/cpp/helpers.h @@ -13,23 +13,25 @@ #include // for LearnerModelParam #include // for Configurable +#include #include // std::int32_t #include +#include #include #include +#include #include - #if defined(__CUDACC__) #include "../../src/collective/communicator-inl.h" // for GetRank #include "../../src/common/cuda_rt_utils.h" // for AllVisibleGPUs -#endif // defined(__CUDACC__) +#endif // defined(__CUDACC__) #include "filesystem.h" // for TemporaryDirectory #include "xgboost/linalg.h" #if defined(__CUDACC__) -#define DeclareUnifiedTest(name) GPU ## name +#define DeclareUnifiedTest(name) GPU##name #else #define DeclareUnifiedTest(name) name #endif @@ -41,7 +43,7 @@ #endif #if defined(__CUDACC__) -#define DeclareUnifiedDistributedTest(name) MGPU ## name +#define DeclareUnifiedDistributedTest(name) MGPU##name #else #define DeclareUnifiedDistributedTest(name) name #endif @@ -51,7 +53,7 @@ class ObjFunction; class Metric; struct LearnerModelParam; class GradientBooster; -} +} // namespace xgboost template Float RelError(Float l, Float r) { @@ -82,20 +84,16 @@ void CreateBigTestData(const std::string& filename, size_t n_entries, bool zero_ void CreateTestCSV(std::string const& path, size_t rows, size_t cols); void CheckObjFunction(std::unique_ptr const& obj, - std::vector preds, - std::vector labels, + std::vector preds, std::vector labels, std::vector weights, std::vector out_grad, std::vector out_hess); -xgboost::Json CheckConfigReloadImpl(xgboost::Configurable* const configurable, - std::string name); +xgboost::Json CheckConfigReloadImpl(xgboost::Configurable* const configurable, std::string name); template -xgboost::Json CheckConfigReload(std::unique_ptr const& configurable, - std::string name = "") { - return CheckConfigReloadImpl(dynamic_cast(configurable.get()), - name); +xgboost::Json CheckConfigReload(std::unique_ptr const& configurable, std::string name = "") { + return CheckConfigReloadImpl(dynamic_cast(configurable.get()), name); } void CheckRankingObjFunction(std::unique_ptr const& obj, @@ -107,12 +105,11 @@ void CheckRankingObjFunction(std::unique_ptr const& obj, std::vector out_hess); xgboost::bst_float GetMetricEval( - xgboost::Metric * metric, - xgboost::HostDeviceVector const& preds, - std::vector labels, - std::vector weights = std::vector(), - std::vector groups = std::vector(), - xgboost::DataSplitMode data_split_Mode = xgboost::DataSplitMode::kRow); + xgboost::Metric* metric, xgboost::HostDeviceVector const& preds, + std::vector labels, + std::vector weights = std::vector(), + std::vector groups = std::vector(), + xgboost::DataSplitMode data_split_Mode = xgboost::DataSplitMode::kRow); double GetMultiMetricEval(xgboost::Metric* metric, xgboost::HostDeviceVector const& preds, @@ -179,8 +176,8 @@ class SimpleRealUniformDistribution { template ResultT GenerateCanonical(GeneratorT* rng) const { static_assert(std::is_floating_point_v, "Result type must be floating point."); - long double const r = (static_cast(rng->Max()) - - static_cast(rng->Min())) + 1.0L; + long double const r = + (static_cast(rng->Max()) - static_cast(rng->Min())) + 1.0L; auto const log2r = static_cast(std::log(r) / std::log(2.0L)); size_t m = std::max(1UL, (Bits + log2r - 1UL) / log2r); ResultT sum_value = 0, r_k = 1; @@ -195,13 +192,11 @@ class SimpleRealUniformDistribution { } public: - SimpleRealUniformDistribution(ResultT l, ResultT u) : - lower_{l}, upper_{u} {} + SimpleRealUniformDistribution(ResultT l, ResultT u) : lower_{l}, upper_{u} {} template ResultT operator()(GeneratorT* rng) const { - ResultT tmp = GenerateCanonical::digits, - GeneratorT>(rng); + ResultT tmp = GenerateCanonical::digits, GeneratorT>(rng); auto ret = (tmp * (upper_ - lower_)) + lower_; // Correct floating point error. return std::max(ret, lower_); @@ -410,12 +405,13 @@ inline auto GenerateRandomGradients(Context const* ctx, bst_idx_t n_rows, bst_ta float lower = 0.0f, float upper = 1.0f) { auto g = GenerateRandomGradients(n_rows * n_targets, lower, upper); GradientContainer gpair; - gpair.gpair = linalg::Matrix{{n_rows, static_cast(n_targets)}, ctx->Device()}; + gpair.gpair = + linalg::Matrix{{n_rows, static_cast(n_targets)}, ctx->Device()}; gpair.gpair.Data()->Copy(g); return gpair; } -typedef void *DMatrixHandle; // NOLINT(*); +typedef void* DMatrixHandle; // NOLINT(*); class ArrayIterForTest { protected: @@ -476,19 +472,14 @@ class NumpyArrayIterForTest : public ArrayIterForTest { ~NumpyArrayIterForTest() override = default; }; -void DMatrixToCSR(DMatrix *dmat, std::vector *p_data, - std::vector *p_row_ptr, - std::vector *p_cids); +void DMatrixToCSR(DMatrix* dmat, std::vector* p_data, std::vector* p_row_ptr, + std::vector* p_cids); -typedef void *DataIterHandle; // NOLINT(*) +typedef void* DataIterHandle; // NOLINT(*) -inline void Reset(DataIterHandle self) { - static_cast(self)->Reset(); -} +inline void Reset(DataIterHandle self) { static_cast(self)->Reset(); } -inline int Next(DataIterHandle self) { - return static_cast(self)->Next(); -} +inline int Next(DataIterHandle self) { return static_cast(self)->Next(); } /** * @brief Create an array interface for host vector. @@ -501,7 +492,7 @@ char const* Make1dInterfaceTest(T const* vec, std::size_t len) { } class RMMAllocator; -using RMMAllocatorPtr = std::unique_ptr; +using RMMAllocatorPtr = std::unique_ptr; RMMAllocatorPtr SetUpRMMResourceForCppTests(int argc, char** argv); /* diff --git a/tests/cpp/histogram_helpers.cu b/tests/cpp/histogram_helpers.cu index a04b0b7ba6dc..1ca64e5941f0 100644 --- a/tests/cpp/histogram_helpers.cu +++ b/tests/cpp/histogram_helpers.cu @@ -5,6 +5,7 @@ #include // for FeatureType #include // for Span +#include #include // for make_unique #include // for uniform_real_distribution #include // for vector diff --git a/tests/cpp/histogram_helpers.h b/tests/cpp/histogram_helpers.h index 3d897c2ded80..e01184a4a351 100644 --- a/tests/cpp/histogram_helpers.h +++ b/tests/cpp/histogram_helpers.h @@ -9,6 +9,11 @@ #include // for SparsePage +#include +#include +#include +#include + #include "./helpers.h" // for RandomDataGenerator namespace xgboost { @@ -31,20 +36,15 @@ inline std::unique_ptr BuildEllpackPage(Context const* ctx, int auto cmat = std::make_shared(); cmat->SetPtrs({0, 3, 6, 9, 12, 15, 18, 21, 24}); // 24 cut fields, 3 cut fields for each feature (column). - cmat->SetValues({0.30f, 0.67f, 1.64f, - 0.32f, 0.77f, 1.95f, - 0.29f, 0.70f, 1.80f, - 0.32f, 0.75f, 1.85f, - 0.18f, 0.59f, 1.69f, - 0.25f, 0.74f, 2.00f, - 0.26f, 0.74f, 1.98f, - 0.26f, 0.71f, 1.83f}); + cmat->SetValues({0.30f, 0.67f, 1.64f, 0.32f, 0.77f, 1.95f, 0.29f, 0.70f, + 1.80f, 0.32f, 0.75f, 1.85f, 0.18f, 0.59f, 1.69f, 0.25f, + 0.74f, 2.00f, 0.26f, 0.74f, 1.98f, 0.26f, 0.71f, 1.83f}); cmat->SetMins({0.1f, 0.2f, 0.3f, 0.1f, 0.2f, 0.3f, 0.2f, 0.2f}); bst_idx_t row_stride = 0; - const auto &offset_vec = batch.offset.ConstHostVector(); + const auto& offset_vec = batch.offset.ConstHostVector(); for (size_t i = 1; i < offset_vec.size(); ++i) { - row_stride = std::max(row_stride, offset_vec[i] - offset_vec[i-1]); + row_stride = std::max(row_stride, offset_vec[i] - offset_vec[i - 1]); } auto page = std::unique_ptr( diff --git a/tests/cpp/linear/test_json_io.h b/tests/cpp/linear/test_json_io.h index c423448e2ac4..af41a51e44f3 100644 --- a/tests/cpp/linear/test_json_io.h +++ b/tests/cpp/linear/test_json_io.h @@ -1,19 +1,22 @@ /*! * Copyright 2020 XGBoost contributors */ -#ifndef XGBOOST_TEST_JSON_IO_H_ -#define XGBOOST_TEST_JSON_IO_H_ +#ifndef TESTS_CPP_LINEAR_TEST_JSON_IO_H_ +#define TESTS_CPP_LINEAR_TEST_JSON_IO_H_ -#include #include +#include + +#include #include -#include "../helpers.h" + #include "../../../src/gbm/gblinear_model.h" +#include "../helpers.h" namespace xgboost { inline void TestUpdaterJsonIO(std::string updater_str) { Context ctx{MakeCUDACtx(GPUIDX)}; - Json config_0 {Object() }; + Json config_0{Object()}; { auto updater = @@ -26,16 +29,15 @@ inline void TestUpdaterJsonIO(std::string updater_str) { auto updater = std::unique_ptr(xgboost::LinearUpdater::Create(updater_str, &ctx)); updater->LoadConfig(config_0); - Json config_1 { Object() }; + Json config_1{Object()}; updater->SaveConfig(&config_1); ASSERT_EQ(config_0, config_1); auto eta = atof(get(config_1["linear_train_param"]["eta"]).c_str()); ASSERT_NEAR(eta, 3.14, kRtEps); } - } } // namespace xgboost -#endif // XGBOOST_TEST_JSON_IO_H_ +#endif // TESTS_CPP_LINEAR_TEST_JSON_IO_H_ diff --git a/tests/cpp/linear/test_linear.cc b/tests/cpp/linear/test_linear.cc index 8f81428b3b4b..3c58892aa8c4 100644 --- a/tests/cpp/linear/test_linear.cc +++ b/tests/cpp/linear/test_linear.cc @@ -1,12 +1,14 @@ /*! * Copyright 2018-2019 by Contributors */ -#include #include +#include + +#include +#include "../../../src/gbm/gblinear_model.h" #include "../helpers.h" #include "test_json_io.h" -#include "../../../src/gbm/gblinear_model.h" #include "xgboost/base.h" namespace xgboost { @@ -33,15 +35,13 @@ TEST(Linear, Shotgun) { ASSERT_EQ(model.Bias()[0], 5.0f); } { - auto updater = std::unique_ptr( - xgboost::LinearUpdater::Create("shotgun", &ctx)); + auto updater = + std::unique_ptr(xgboost::LinearUpdater::Create("shotgun", &ctx)); EXPECT_ANY_THROW(updater->Configure({{"feature_selector", "random"}})); } } -TEST(Shotgun, JsonIO) { - TestUpdaterJsonIO("shotgun"); -} +TEST(Shotgun, JsonIO) { TestUpdaterJsonIO("shotgun"); } TEST(Linear, coordinate) { size_t constexpr kRows = 10; @@ -64,8 +64,6 @@ TEST(Linear, coordinate) { ASSERT_EQ(model.Bias()[0], 5.0f); } -TEST(Coordinate, JsonIO){ - TestUpdaterJsonIO("coord_descent"); -} +TEST(Coordinate, JsonIO) { TestUpdaterJsonIO("coord_descent"); } } // namespace xgboost diff --git a/tests/cpp/linear/test_linear.cu b/tests/cpp/linear/test_linear.cu index 8475116bc954..02485023febe 100644 --- a/tests/cpp/linear/test_linear.cu +++ b/tests/cpp/linear/test_linear.cu @@ -1,12 +1,14 @@ /** * Copyright 2018-2023, XGBoost Contributors */ -#include #include +#include + +#include +#include "../../../src/gbm/gblinear_model.h" #include "../helpers.h" #include "test_json_io.h" -#include "../../../src/gbm/gblinear_model.h" namespace xgboost { @@ -30,7 +32,5 @@ TEST(Linear, GPUCoordinate) { ASSERT_EQ(model.Bias()[0], 5.0f); } -TEST(GPUCoordinate, JsonIO) { - TestUpdaterJsonIO("gpu_coord_descent"); -} +TEST(GPUCoordinate, JsonIO) { TestUpdaterJsonIO("gpu_coord_descent"); } } // namespace xgboost diff --git a/tests/cpp/metric/test_auc.h b/tests/cpp/metric/test_auc.h index dc99ab2e95ef..ffa9b002134b 100644 --- a/tests/cpp/metric/test_auc.h +++ b/tests/cpp/metric/test_auc.h @@ -5,6 +5,10 @@ #include +#include +#include +#include + #include "../helpers.h" namespace xgboost::metric { @@ -97,9 +101,9 @@ inline void VerifyMultiClassAUC(DataSplitMode data_split_mode, DeviceOrd device) // invalid dataset float auc = GetMetricEval(metric, { - 1.0f, 0.0f, 0.0f, // p_0 - 0.0f, 1.0f, 0.0f, // p_1 - 0.0f, 0.0f, 1.0f // p_2 + 1.0f, 0.0f, 0.0f, // p_0 + 0.0f, 1.0f, 0.0f, // p_1 + 0.0f, 0.0f, 1.0f // p_2 }, {0, 1, 1}, {}, {}, data_split_mode); // no class 2. EXPECT_TRUE(std::isnan(auc)) << auc; diff --git a/tests/cpp/metric/test_distributed_metric.cc b/tests/cpp/metric/test_distributed_metric.cc index c9012dbd4b35..f73c711d684c 100644 --- a/tests/cpp/metric/test_distributed_metric.cc +++ b/tests/cpp/metric/test_distributed_metric.cc @@ -10,6 +10,7 @@ #include // for function #include // for string #include // for thread +#include #include "../collective/test_worker.h" // for TestDistributedGlobal #include "test_auc.h" diff --git a/tests/cpp/metric/test_elementwise_metric.h b/tests/cpp/metric/test_elementwise_metric.h index 03e4126d4b13..ef6891935aa0 100644 --- a/tests/cpp/metric/test_elementwise_metric.h +++ b/tests/cpp/metric/test_elementwise_metric.h @@ -5,8 +5,11 @@ #include #include +#include +#include #include #include // for iota +#include #include "../helpers.h" @@ -19,7 +22,7 @@ inline void CheckDeterministicMetricElementWise(StringView name, int32_t device) size_t n_samples = 2048; auto p_fmat = EmptyDMatrix(); - MetaInfo& info = p_fmat->Info(); + MetaInfo &info = p_fmat->Info(); info.labels.Reshape(n_samples, 1); info.num_row_ = n_samples; auto &h_labels = info.labels.Data()->HostVector(); @@ -44,27 +47,22 @@ inline void CheckDeterministicMetricElementWise(StringView name, int32_t device) inline void VerifyRMSE(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("rmse", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("rmse", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "rmse"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.6403f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 0.6403f, 0.001f); auto expected = 2.8284f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected = sqrt(8.0f * collective::GetWorldSize()); } - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {-1, 1, 9, -9}, {}, + data_split_mode), expected, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 0.6708f, 0.001f); delete metric; @@ -73,27 +71,22 @@ inline void VerifyRMSE(DataSplitMode data_split_mode, DeviceOrd device) { inline void VerifyRMSLE(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("rmsle", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("rmsle", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "rmsle"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, - {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, {}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, + {}, {}, data_split_mode), 0.4063f, 1e-4); auto expected = 0.6212f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected = sqrt(0.3859f * collective::GetWorldSize()); } - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, - {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, - { 0, -1, 1, -9, 9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, + {0, -1, 1, -9, 9}, {}, data_split_mode), expected, 1e-4); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, - {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, - { 0, 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, + {0, 1, 2, 9, 8}, {}, data_split_mode), 0.2415f, 1e-4); delete metric; @@ -102,27 +95,22 @@ inline void VerifyRMSLE(DataSplitMode data_split_mode, DeviceOrd device) { inline void VerifyMAE(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("mae", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("mae", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "mae"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.5f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), 0.5f, + 0.001f); auto expected = 8.0f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected *= collective::GetWorldSize(); } - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {-1, 1, 9, -9}, {}, + data_split_mode), expected, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 0.54f, 0.001f); delete metric; @@ -131,27 +119,22 @@ inline void VerifyMAE(DataSplitMode data_split_mode, DeviceOrd device) { inline void VerifyMAPE(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("mape", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("mape", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "mape"); EXPECT_NEAR(GetMetricEval(metric, {150, 300}, {100, 200}, {}, {}, data_split_mode), 0.5f, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {50, 400, 500, 4000}, - {100, 200, 500, 1000}, {}, {}, data_split_mode), - 1.125f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {50, 400, 500, 4000}, {100, 200, 500, 1000}, {}, {}, data_split_mode), + 1.125f, 0.001f); auto expected = -26.5f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected *= collective::GetWorldSize(); } - EXPECT_NEAR(GetMetricEval(metric, - {50, 400, 500, 4000}, - {100, 200, 500, 1000}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {50, 400, 500, 4000}, {100, 200, 500, 1000}, {-1, 1, 9, -9}, {}, + data_split_mode), expected, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {50, 400, 500, 4000}, - {100, 200, 500, 1000}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {50, 400, 500, 4000}, {100, 200, 500, 1000}, {1, 2, 9, 8}, {}, + data_split_mode), 1.3250f, 0.001f); delete metric; @@ -164,62 +147,49 @@ inline void VerifyMPHE(DataSplitMode data_split_mode, DeviceOrd device) { metric->Configure({}); ASSERT_STREQ(metric->Name(), "mphe"); EXPECT_NEAR(GetMetricEval(metric.get(), {0, 1}, {0, 1}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric.get(), - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.1751f, 1e-4); + EXPECT_NEAR( + GetMetricEval(metric.get(), {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 0.1751f, 1e-4); auto expected = 3.40375f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected *= collective::GetWorldSize(); } - EXPECT_NEAR(GetMetricEval(metric.get(), - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric.get(), {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {-1, 1, 9, -9}, + {}, data_split_mode), expected, 1e-4); - EXPECT_NEAR(GetMetricEval(metric.get(), - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric.get(), {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 0.1922f, 1e-4); CheckDeterministicMetricElementWise(StringView{"mphe"}, device.ordinal); metric->Configure({{"huber_slope", "0.1"}}); - EXPECT_NEAR(GetMetricEval(metric.get(), - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric.get(), {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 0.0461686f, 1e-4); } inline void VerifyLogLoss(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("logloss", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("logloss", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "logloss"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.5f, 1e-17f, 1.0f+1e-17f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.5f, 1e-17f, 1.0f + 1e-17f, 0.9f}, {0, 0, 1, 1}, {}, {}, + data_split_mode), 0.1996f, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 1.2039f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 1.2039f, 0.001f); auto expected = 21.9722f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected *= collective::GetWorldSize(); } - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {-1, 1, 9, -9}, {}, + data_split_mode), expected, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 1.3138f, 0.001f); delete metric; @@ -228,27 +198,22 @@ inline void VerifyLogLoss(DataSplitMode data_split_mode, DeviceOrd device) { inline void VerifyError(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("error", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("error", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "error"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.5f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), 0.5f, + 0.001f); auto expected = 10.0f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected *= collective::GetWorldSize(); } - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {-1, 1, 9, -9}, {}, + data_split_mode), expected, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 0.55f, 0.001f); EXPECT_ANY_THROW(xgboost::Metric::Create("error@abc", &ctx)); @@ -265,23 +230,18 @@ inline void VerifyError(DataSplitMode data_split_mode, DeviceOrd device) { ASSERT_STREQ(metric->Name(), "error@0.1"); EXPECT_STREQ(metric->Name(), "error@0.1"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {-0.1f, -0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.25f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {-0.1f, -0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 0.25f, 0.001f); expected = 9.0f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected *= collective::GetWorldSize(); } - EXPECT_NEAR(GetMetricEval(metric, - {-0.1f, -0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {-0.1f, -0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {-1, 1, 9, -9}, {}, + data_split_mode), expected, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {-0.1f, -0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {-0.1f, -0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 0.45f, 0.001f); delete metric; @@ -290,31 +250,25 @@ inline void VerifyError(DataSplitMode data_split_mode, DeviceOrd device) { inline void VerifyPoissonNegLogLik(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("poisson-nloglik", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("poisson-nloglik", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "poisson-nloglik"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 0.5f, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.5f, 1e-17f, 1.0f+1e-17f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.5f, 1e-17f, 1.0f + 1e-17f, 0.9f}, {0, 0, 1, 1}, {}, {}, + data_split_mode), 0.6263f, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 1.1019f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 1.1019f, 0.001f); auto expected = 13.3750f; if (collective::IsDistributed() && data_split_mode == DataSplitMode::kRow) { expected *= collective::GetWorldSize(); } - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { -1, 1, 9, -9}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {-1, 1, 9, -9}, {}, + data_split_mode), expected, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, - { 1, 2, 9, 8}, {}, data_split_mode), + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {1, 2, 9, 8}, {}, + data_split_mode), 1.5783f, 0.001f); delete metric; @@ -349,10 +303,8 @@ inline void VerifyQuantile(DataSplitMode data_split_mode, DeviceOrd device) { HostDeviceVector predts{0.1f, 0.9f, 0.1f, 0.9f}; HostDeviceVector predts_2{0.2f, 0.6f, 0.4f, 0.6f, 0.5f, 1.2f, 0.0f, 0.4f}; - HostDeviceVector predts_3{0.2f, 0.4f, 0.6f, - 0.4f, 0.5f, 0.6f, - 0.5f, 0.8f, 1.2f, - 0.0f, 0.3f, 0.4f}; + HostDeviceVector predts_3{0.2f, 0.4f, 0.6f, 0.4f, 0.5f, 0.6f, + 0.5f, 0.8f, 1.2f, 0.0f, 0.3f, 0.4f}; std::vector labels{0.5f, 0.5f, 0.9f, 0.1f}; std::vector weights{0.2f, 0.4f, 0.6f, 0.8f}; diff --git a/tests/cpp/metric/test_multiclass_metric.h b/tests/cpp/metric/test_multiclass_metric.h index 002e38cb1e21..33bba672ac95 100644 --- a/tests/cpp/metric/test_multiclass_metric.h +++ b/tests/cpp/metric/test_multiclass_metric.h @@ -1,5 +1,10 @@ // Copyright by Contributors +#ifndef TESTS_CPP_METRIC_TEST_MULTICLASS_METRIC_H_ +#define TESTS_CPP_METRIC_TEST_MULTICLASS_METRIC_H_ + #include + +#include #include #include "../helpers.h" @@ -13,7 +18,7 @@ inline void CheckDeterministicMetricMultiClass(StringView name, int32_t device) HostDeviceVector predts; auto p_fmat = EmptyDMatrix(); - MetaInfo& info = p_fmat->Info(); + MetaInfo &info = p_fmat->Info(); auto &h_predts = predts.HostVector(); SimpleLCG lcg; @@ -46,14 +51,14 @@ inline void CheckDeterministicMetricMultiClass(StringView name, int32_t device) inline void TestMultiClassError(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("merror", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("merror", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "merror"); EXPECT_ANY_THROW(GetMetricEval(metric, {0}, {0, 0}, {}, {}, data_split_mode)); - EXPECT_NEAR(GetMetricEval( - metric, {1, 0, 0, 0, 1, 0, 0, 0, 1}, {0, 1, 2}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f}, + EXPECT_NEAR( + GetMetricEval(metric, {1, 0, 0, 0, 1, 0, 0, 0, 1}, {0, 1, 2}, {}, {}, data_split_mode), 0, + 1e-10); + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f}, {0, 1, 2}, {}, {}, data_split_mode), 0.666f, 0.001f); delete metric; @@ -66,14 +71,14 @@ inline void VerifyMultiClassError(DataSplitMode data_split_mode, DeviceOrd devic inline void TestMultiClassLogLoss(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - xgboost::Metric * metric = xgboost::Metric::Create("mlogloss", &ctx); + xgboost::Metric *metric = xgboost::Metric::Create("mlogloss", &ctx); metric->Configure({}); ASSERT_STREQ(metric->Name(), "mlogloss"); EXPECT_ANY_THROW(GetMetricEval(metric, {0}, {0, 0}, {}, {}, data_split_mode)); - EXPECT_NEAR(GetMetricEval( - metric, {1, 0, 0, 0, 1, 0, 0, 0, 1}, {0, 1, 2}, {}, {}, data_split_mode), 0, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f}, + EXPECT_NEAR( + GetMetricEval(metric, {1, 0, 0, 0, 1, 0, 0, 0, 1}, {0, 1, 2}, {}, {}, data_split_mode), 0, + 1e-10); + EXPECT_NEAR(GetMetricEval(metric, {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f}, {0, 1, 2}, {}, {}, data_split_mode), 2.302f, 0.001f); @@ -87,3 +92,5 @@ inline void VerifyMultiClassLogLoss(DataSplitMode data_split_mode, DeviceOrd dev } // namespace metric } // namespace xgboost + +#endif // TESTS_CPP_METRIC_TEST_MULTICLASS_METRIC_H_ diff --git a/tests/cpp/metric/test_rank_metric.h b/tests/cpp/metric/test_rank_metric.h index bb409628868a..6d6f832e9080 100644 --- a/tests/cpp/metric/test_rank_metric.h +++ b/tests/cpp/metric/test_rank_metric.h @@ -2,15 +2,15 @@ * Copyright 2016-2023 by XGBoost Contributors */ #pragma once -#include // for Test, EXPECT_NEAR, ASSERT_STREQ -#include // for Context -#include // for MetaInfo, DMatrix -#include // for Matrix -#include // for Metric +#include // for Test, EXPECT_NEAR, ASSERT_STREQ +#include // for Context +#include // for MetaInfo, DMatrix +#include // for Matrix +#include // for Metric -#include // for max -#include // for unique_ptr -#include // for vector +#include // for max +#include // for unique_ptr +#include // for vector #include "../helpers.h" // for GetMetricEval, CreateEmptyGe... #include "xgboost/base.h" // for bst_float, kRtEps @@ -45,107 +45,99 @@ inline void VerifyPrecision(DataSplitMode data_split_mode, DeviceOrd device) { inline void VerifyNDCG(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - Metric * metric = xgboost::Metric::Create("ndcg", &ctx); + Metric* metric = xgboost::Metric::Create("ndcg", &ctx); ASSERT_STREQ(metric->Name(), "ndcg"); EXPECT_ANY_THROW(GetMetricEval(metric, {0, 1}, {}, {}, {}, data_split_mode)); - ASSERT_NEAR(GetMetricEval(metric, - xgboost::HostDeviceVector{}, - {}, {}, {}, data_split_mode), 1, 1e-10); + ASSERT_NEAR(GetMetricEval(metric, xgboost::HostDeviceVector{}, {}, {}, {}, + data_split_mode), + 1, 1e-10); ASSERT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 1, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.6509f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 0.6509f, 0.001f); delete metric; metric = xgboost::Metric::Create("ndcg@2", &ctx); ASSERT_STREQ(metric->Name(), "ndcg@2"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 1, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.3868f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 0.3868f, 0.001f); delete metric; metric = xgboost::Metric::Create("ndcg@-", &ctx); ASSERT_STREQ(metric->Name(), "ndcg-"); - EXPECT_NEAR(GetMetricEval(metric, - xgboost::HostDeviceVector{}, - {}, {}, {}, data_split_mode), 0, 1e-10); + EXPECT_NEAR(GetMetricEval(metric, xgboost::HostDeviceVector{}, {}, {}, {}, + data_split_mode), + 0, 1e-10); ASSERT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 1.f, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.6509f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 0.6509f, 0.001f); delete metric; metric = xgboost::Metric::Create("ndcg-", &ctx); ASSERT_STREQ(metric->Name(), "ndcg-"); - EXPECT_NEAR(GetMetricEval(metric, - xgboost::HostDeviceVector{}, - {}, {}, {}, data_split_mode), 0, 1e-10); + EXPECT_NEAR(GetMetricEval(metric, xgboost::HostDeviceVector{}, {}, {}, {}, + data_split_mode), + 0, 1e-10); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 1.f, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.6509f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 0.6509f, 0.001f); delete metric; metric = xgboost::Metric::Create("ndcg@2-", &ctx); ASSERT_STREQ(metric->Name(), "ndcg@2-"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 1.f, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 1.f - 0.3868f, 1.f - 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), + 1.f - 0.3868f, 1.f - 0.001f); delete metric; } inline void VerifyMAP(DataSplitMode data_split_mode, DeviceOrd device) { auto ctx = MakeCUDACtx(device.ordinal); - Metric * metric = xgboost::Metric::Create("map", &ctx); + Metric* metric = xgboost::Metric::Create("map", &ctx); ASSERT_STREQ(metric->Name(), "map"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 1, kRtEps); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.5f, 0.001f); - EXPECT_NEAR(GetMetricEval(metric, - xgboost::HostDeviceVector{}, - std::vector{}, {}, {}, data_split_mode), 1, 1e-10); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), 0.5f, + 0.001f); + EXPECT_NEAR(GetMetricEval(metric, xgboost::HostDeviceVector{}, + std::vector{}, {}, {}, data_split_mode), + 1, 1e-10); // Rank metric with group info - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.2f, 0.8f, 0.4f, 1.7f}, - {1, 1, 1, 0, 1, 0}, // Labels - {}, // Weights - {0, 2, 5, 6}, // Group info - data_split_mode), - 0.8611f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.2f, 0.8f, 0.4f, 1.7f}, {1, 1, 1, 0, 1, 0}, // Labels + {}, // Weights + {0, 2, 5, 6}, // Group info + data_split_mode), + 0.8611f, 0.001f); delete metric; metric = xgboost::Metric::Create("map@-", &ctx); ASSERT_STREQ(metric->Name(), "map-"); - EXPECT_NEAR(GetMetricEval(metric, - xgboost::HostDeviceVector{}, - {}, {}, {}, data_split_mode), 0, 1e-10); + EXPECT_NEAR(GetMetricEval(metric, xgboost::HostDeviceVector{}, {}, {}, {}, + data_split_mode), + 0, 1e-10); delete metric; metric = xgboost::Metric::Create("map-", &ctx); ASSERT_STREQ(metric->Name(), "map-"); - EXPECT_NEAR(GetMetricEval(metric, - xgboost::HostDeviceVector{}, - {}, {}, {}, data_split_mode), 0, 1e-10); + EXPECT_NEAR(GetMetricEval(metric, xgboost::HostDeviceVector{}, {}, {}, {}, + data_split_mode), + 0, 1e-10); delete metric; metric = xgboost::Metric::Create("map@2", &ctx); ASSERT_STREQ(metric->Name(), "map@2"); EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}, {}, {}, data_split_mode), 1, 1e-10); - EXPECT_NEAR(GetMetricEval(metric, - {0.1f, 0.9f, 0.1f, 0.9f}, - { 0, 0, 1, 1}, {}, {}, data_split_mode), - 0.25f, 0.001f); + EXPECT_NEAR( + GetMetricEval(metric, {0.1f, 0.9f, 0.1f, 0.9f}, {0, 0, 1, 1}, {}, {}, data_split_mode), 0.25f, + 0.001f); delete metric; } diff --git a/tests/cpp/metric/test_survival_metric.cc b/tests/cpp/metric/test_survival_metric.cc index 1b02fd7e7b02..0a2ca4892f2d 100644 --- a/tests/cpp/metric/test_survival_metric.cc +++ b/tests/cpp/metric/test_survival_metric.cc @@ -2,16 +2,17 @@ * Copyright 2020-2023, XGBoost Contributors */ #include + +#include +#include #include -#include #include -#include -#include +#include -#include "xgboost/metric.h" -#include "xgboost/logging.h" -#include "../helpers.h" #include "../../../src/common/survival_util.h" +#include "../helpers.h" +#include "xgboost/logging.h" +#include "xgboost/metric.h" // CUDA conditional compile trick. #include "test_survival_metric.cu" @@ -32,19 +33,17 @@ namespace xgboost::common { // Generate prediction value ranging from 2**1 to 2**15, using grid points in log scale // Then check prediction against the reference values template -static inline void CheckLossOverGridPoints( - double true_label_lower_bound, - double true_label_upper_bound, - const std::vector& reference_values) { +static inline void CheckLossOverGridPoints(double true_label_lower_bound, + double true_label_upper_bound, + const std::vector& reference_values) { const int num_point = 20; const double log_y_low = 1.0; const double log_y_high = 15.0; CHECK_EQ(num_point, reference_values.size()); for (int i = 0; i < num_point; ++i) { - const double y_pred - = std::pow(2.0, i * (log_y_high - log_y_low) / (num_point - 1) + log_y_low); + const double y_pred = std::pow(2.0, i * (log_y_high - log_y_low) / (num_point - 1) + log_y_low); const double loss_val = AFTLoss::Loss( - true_label_lower_bound, true_label_upper_bound, std::log(y_pred), 1.0); + true_label_lower_bound, true_label_upper_bound, std::log(y_pred), 1.0); EXPECT_NEAR(loss_val, reference_values[i], 1e-4); } } @@ -54,15 +53,18 @@ TEST(AFTLoss, Uncensored) { const double true_label_lower_bound = 100.0; const double true_label_upper_bound = true_label_lower_bound; - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 13.1761, 11.3085, 9.7017, 8.3558, 7.2708, 6.4466, 5.8833, 5.5808, 5.5392, 5.7585, 6.2386, - 6.9795, 7.9813, 9.2440, 10.7675, 12.5519, 14.5971, 16.9032, 19.4702, 22.2980 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 8.5568, 8.0720, 7.6038, 7.1620, 6.7612, 6.4211, 6.1659, 6.0197, 5.9990, 6.1064, 6.3293, - 6.6450, 7.0289, 7.4594, 7.9205, 8.4008, 8.8930, 9.3926, 9.8966, 10.4033 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 27.6310, 27.6310, 19.7177, 13.0281, 9.2183, 7.1365, 6.0916, 5.6688, 5.6195, 5.7941, 6.1031, - 6.4929, 6.9310, 7.3981, 7.8827, 8.3778, 8.8791, 9.3842, 9.8916, 10.40033 }); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {13.1761, 11.3085, 9.7017, 8.3558, 7.2708, 6.4466, 5.8833, 5.5808, 5.5392, 5.7585, + 6.2386, 6.9795, 7.9813, 9.2440, 10.7675, 12.5519, 14.5971, 16.9032, 19.4702, 22.2980}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {8.5568, 8.0720, 7.6038, 7.1620, 6.7612, 6.4211, 6.1659, 6.0197, 5.9990, 6.1064, + 6.3293, 6.6450, 7.0289, 7.4594, 7.9205, 8.4008, 8.8930, 9.3926, 9.8966, 10.4033}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {27.6310, 27.6310, 19.7177, 13.0281, 9.2183, 7.1365, 6.0916, 5.6688, 5.6195, 5.7941, + 6.1031, 6.4929, 6.9310, 7.3981, 7.8827, 8.3778, 8.8791, 9.3842, 9.8916, 10.40033}); } TEST(AFTLoss, LeftCensored) { @@ -70,15 +72,18 @@ TEST(AFTLoss, LeftCensored) { const double true_label_lower_bound = 0.0; const double true_label_upper_bound = 20.0; - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 0.0107, 0.0373, 0.1054, 0.2492, 0.5068, 0.9141, 1.5003, 2.2869, 3.2897, 4.5196, 5.9846, - 7.6902, 9.6405, 11.8385, 14.2867, 16.9867, 19.9399, 23.1475, 26.6103, 27.6310 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 0.0953, 0.1541, 0.2451, 0.3804, 0.5717, 0.8266, 1.1449, 1.5195, 1.9387, 2.3902, 2.8636, - 3.3512, 3.8479, 4.3500, 4.8556, 5.3632, 5.8721, 6.3817, 6.8918, 7.4021 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 0.0000, 0.0025, 0.0277, 0.1225, 0.3195, 0.6150, 0.9862, 1.4094, 1.8662, 2.3441, 2.8349, - 3.3337, 3.8372, 4.3436, 4.8517, 5.3609, 5.8707, 6.3808, 6.8912, 7.4018 }); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {0.0107, 0.0373, 0.1054, 0.2492, 0.5068, 0.9141, 1.5003, 2.2869, 3.2897, 4.5196, + 5.9846, 7.6902, 9.6405, 11.8385, 14.2867, 16.9867, 19.9399, 23.1475, 26.6103, 27.6310}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {0.0953, 0.1541, 0.2451, 0.3804, 0.5717, 0.8266, 1.1449, 1.5195, 1.9387, 2.3902, + 2.8636, 3.3512, 3.8479, 4.3500, 4.8556, 5.3632, 5.8721, 6.3817, 6.8918, 7.4021}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {0.0000, 0.0025, 0.0277, 0.1225, 0.3195, 0.6150, 0.9862, 1.4094, 1.8662, 2.3441, + 2.8349, 3.3337, 3.8372, 4.3436, 4.8517, 5.3609, 5.8707, 6.3808, 6.8912, 7.4018}); } TEST(AFTLoss, RightCensored) { @@ -86,15 +91,18 @@ TEST(AFTLoss, RightCensored) { const double true_label_lower_bound = 60.0; const double true_label_upper_bound = std::numeric_limits::infinity(); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 8.0000, 6.2537, 4.7487, 3.4798, 2.4396, 1.6177, 0.9993, 0.5638, 0.2834, 0.1232, 0.0450, - 0.0134, 0.0032, 0.0006, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 3.4340, 2.9445, 2.4683, 2.0125, 1.5871, 1.2041, 0.8756, 0.6099, 0.4083, 0.2643, 0.1668, - 0.1034, 0.0633, 0.0385, 0.0233, 0.0140, 0.0084, 0.0051, 0.0030, 0.0018 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 27.6310, 18.0015, 10.8018, 6.4817, 3.8893, 2.3338, 1.4004, 0.8403, 0.5042, 0.3026, 0.1816, - 0.1089, 0.0654, 0.0392, 0.0235, 0.0141, 0.0085, 0.0051, 0.0031, 0.0018 }); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {8.0000, 6.2537, 4.7487, 3.4798, 2.4396, 1.6177, 0.9993, 0.5638, 0.2834, 0.1232, + 0.0450, 0.0134, 0.0032, 0.0006, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {3.4340, 2.9445, 2.4683, 2.0125, 1.5871, 1.2041, 0.8756, 0.6099, 0.4083, 0.2643, + 0.1668, 0.1034, 0.0633, 0.0385, 0.0233, 0.0140, 0.0084, 0.0051, 0.0030, 0.0018}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {27.6310, 18.0015, 10.8018, 6.4817, 3.8893, 2.3338, 1.4004, 0.8403, 0.5042, 0.3026, + 0.1816, 0.1089, 0.0654, 0.0392, 0.0235, 0.0141, 0.0085, 0.0051, 0.0031, 0.0018}); } TEST(AFTLoss, IntervalCensored) { @@ -102,14 +110,17 @@ TEST(AFTLoss, IntervalCensored) { const double true_label_lower_bound = 16.0; const double true_label_upper_bound = 200.0; - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 3.9746, 2.8415, 1.9319, 1.2342, 0.7335, 0.4121, 0.2536, 0.2470, 0.3919, 0.6982, 1.1825, - 1.8622, 2.7526, 3.8656, 5.2102, 6.7928, 8.6183, 10.6901, 13.0108, 15.5826 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 2.2906, 1.8578, 1.4667, 1.1324, 0.8692, 0.6882, 0.5948, 0.5909, 0.6764, 0.8499, 1.1061, - 1.4348, 1.8215, 2.2511, 2.7104, 3.1891, 3.6802, 4.1790, 4.6825, 5.1888 }); - CheckLossOverGridPoints(true_label_lower_bound, true_label_upper_bound, - { 8.0000, 4.8004, 2.8805, 1.7284, 1.0372, 0.6231, 0.3872, 0.3031, 0.3740, 0.5839, 0.8995, - 1.2878, 1.7231, 2.1878, 2.6707, 3.1647, 3.6653, 4.1699, 4.6770, 5.1856 }); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {3.9746, 2.8415, 1.9319, 1.2342, 0.7335, 0.4121, 0.2536, 0.2470, 0.3919, 0.6982, + 1.1825, 1.8622, 2.7526, 3.8656, 5.2102, 6.7928, 8.6183, 10.6901, 13.0108, 15.5826}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {2.2906, 1.8578, 1.4667, 1.1324, 0.8692, 0.6882, 0.5948, 0.5909, 0.6764, 0.8499, + 1.1061, 1.4348, 1.8215, 2.2511, 2.7104, 3.1891, 3.6802, 4.1790, 4.6825, 5.1888}); + CheckLossOverGridPoints( + true_label_lower_bound, true_label_upper_bound, + {8.0000, 4.8004, 2.8805, 1.7284, 1.0372, 0.6231, 0.3872, 0.3031, 0.3740, 0.5839, + 0.8995, 1.2878, 1.7231, 2.1878, 2.6707, 3.1647, 3.6653, 4.1699, 4.6770, 5.1856}); } } // namespace xgboost::common diff --git a/tests/cpp/metric/test_survival_metric.cu b/tests/cpp/metric/test_survival_metric.cu index ead8d11f2a3b..c071477aa59a 100644 --- a/tests/cpp/metric/test_survival_metric.cu +++ b/tests/cpp/metric/test_survival_metric.cu @@ -2,6 +2,9 @@ * Copyright (c) by Contributors 2020 */ #include + +#include + #include "test_survival_metric.h" #include "xgboost/metric.h" @@ -15,7 +18,7 @@ TEST(AFTNegLogLikMetric, DeclareUnifiedTest(Configuration)) { metric->Configure({{"aft_loss_distribution", "normal"}, {"aft_loss_distribution_scale", "10"}}); // Configuration round-trip test - Json j_obj{ Object() }; + Json j_obj{Object()}; metric->SaveConfig(&j_obj); auto aft_param_json = j_obj["aft_loss_param"]; EXPECT_EQ(get(aft_param_json["aft_loss_distribution"]), "normal"); diff --git a/tests/cpp/metric/test_survival_metric.h b/tests/cpp/metric/test_survival_metric.h index 902c9aa6bbcf..ac835a98d24e 100644 --- a/tests/cpp/metric/test_survival_metric.h +++ b/tests/cpp/metric/test_survival_metric.h @@ -5,6 +5,10 @@ #include #include +#include +#include +#include +#include #include "../../../src/common/survival_util.h" #include "../helpers.h" @@ -20,7 +24,7 @@ inline void CheckDeterministicMetricElementWise(StringView name, int32_t device) HostDeviceVector predts; auto p_fmat = EmptyDMatrix(); MetaInfo& info = p_fmat->Info(); - auto &h_predts = predts.HostVector(); + auto& h_predts = predts.HostVector(); SimpleLCG lcg; SimpleRealUniformDistribution dist{0.0f, 1.0f}; @@ -32,8 +36,8 @@ inline void CheckDeterministicMetricElementWise(StringView name, int32_t device) h_predts[i] = dist(&lcg); } - auto &h_upper = info.labels_upper_bound_.HostVector(); - auto &h_lower = info.labels_lower_bound_.HostVector(); + auto& h_upper = info.labels_upper_bound_.HostVector(); + auto& h_lower = info.labels_lower_bound_.HostVector(); h_lower.resize(n_samples); h_upper.resize(n_samples); for (size_t i = 0; i < n_samples; ++i) { @@ -57,10 +61,9 @@ inline void VerifyAFTNegLogLik(DataSplitMode data_split_mode, DeviceOrd device) auto p_fmat = EmptyDMatrix(); MetaInfo& info = p_fmat->Info(); info.num_row_ = 4; - info.labels_lower_bound_.HostVector() - = { 100.0f, 0.0f, 60.0f, 16.0f }; - info.labels_upper_bound_.HostVector() - = { 100.0f, 20.0f, std::numeric_limits::infinity(), 200.0f }; + info.labels_lower_bound_.HostVector() = {100.0f, 0.0f, 60.0f, 16.0f}; + info.labels_upper_bound_.HostVector() = {100.0f, 20.0f, + std::numeric_limits::infinity(), 200.0f}; info.weights_.HostVector() = std::vector(); info.data_split_mode = data_split_mode; HostDeviceVector preds(4, std::log(64)); @@ -69,11 +72,11 @@ inline void VerifyAFTNegLogLik(DataSplitMode data_split_mode, DeviceOrd device) std::string dist_type; bst_float reference_value; }; - for (const auto& test_case : std::vector{ {"normal", 2.1508f}, {"logistic", 2.1804f}, - {"extreme", 2.0706f} }) { + for (const auto& test_case : + std::vector{{"normal", 2.1508f}, {"logistic", 2.1804f}, {"extreme", 2.0706f}}) { std::unique_ptr metric(Metric::Create("aft-nloglik", &ctx)); - metric->Configure({ {"aft_loss_distribution", test_case.dist_type}, - {"aft_loss_distribution_scale", "1.0"} }); + metric->Configure( + {{"aft_loss_distribution", test_case.dist_type}, {"aft_loss_distribution_scale", "1.0"}}); EXPECT_NEAR(metric->Evaluate(preds, p_fmat), test_case.reference_value, 1e-4); } } @@ -84,8 +87,8 @@ inline void VerifyIntervalRegressionAccuracy(DataSplitMode data_split_mode, Devi auto p_fmat = EmptyDMatrix(); MetaInfo& info = p_fmat->Info(); info.num_row_ = 4; - info.labels_lower_bound_.HostVector() = { 20.0f, 0.0f, 60.0f, 16.0f }; - info.labels_upper_bound_.HostVector() = { 80.0f, 20.0f, 80.0f, 200.0f }; + info.labels_lower_bound_.HostVector() = {20.0f, 0.0f, 60.0f, 16.0f}; + info.labels_upper_bound_.HostVector() = {80.0f, 20.0f, 80.0f, 200.0f}; info.weights_.HostVector() = std::vector(); info.data_split_mode = data_split_mode; HostDeviceVector preds(4, std::log(60.0f)); diff --git a/tests/cpp/objective/test_aft_obj.cc b/tests/cpp/objective/test_aft_obj.cc index cd031b6bcdf5..a1735c9408a5 100644 --- a/tests/cpp/objective/test_aft_obj.cc +++ b/tests/cpp/objective/test_aft_obj.cc @@ -1,25 +1,28 @@ /** * Copyright 2020-2024, XGBoost Contributors */ +#include "test_aft_obj.h" + #include + +#include +#include #include +#include #include -#include -#include -#include "xgboost/objective.h" -#include "xgboost/logging.h" #include "../helpers.h" -#include "test_aft_obj.h" +#include "xgboost/logging.h" +#include "xgboost/objective.h" namespace xgboost::common { void TestAFTObjConfiguration(const Context* ctx) { std::unique_ptr objective(ObjFunction::Create("survival:aft", ctx)); - objective->Configure({ {"aft_loss_distribution", "logistic"}, - {"aft_loss_distribution_scale", "5"} }); + objective->Configure( + {{"aft_loss_distribution", "logistic"}, {"aft_loss_distribution_scale", "5"}}); // Configuration round-trip test - Json j_obj{ Object() }; + Json j_obj{Object()}; objective->SaveConfig(&j_obj); EXPECT_EQ(get(j_obj["name"]), "survival:aft"); auto aft_param_json = j_obj["aft_loss_param"]; @@ -35,27 +38,22 @@ void TestAFTObjConfiguration(const Context* ctx) { // Generate prediction value ranging from 2**1 to 2**15, using grid points in log scale // Then check prediction against the reference values -static inline void CheckGPairOverGridPoints( - ObjFunction* obj, - bst_float true_label_lower_bound, - bst_float true_label_upper_bound, - const std::string& dist_type, - const std::vector& expected_grad, - const std::vector& expected_hess, - float ftol = 1e-4f) { +static inline void CheckGPairOverGridPoints(ObjFunction* obj, bst_float true_label_lower_bound, + bst_float true_label_upper_bound, + const std::string& dist_type, + const std::vector& expected_grad, + const std::vector& expected_hess, + float ftol = 1e-4f) { const int num_point = 20; const double log_y_low = 1.0; const double log_y_high = 15.0; - obj->Configure({ {"aft_loss_distribution", dist_type}, - {"aft_loss_distribution_scale", "1"} }); + obj->Configure({{"aft_loss_distribution", dist_type}, {"aft_loss_distribution_scale", "1"}}); MetaInfo info; info.num_row_ = num_point; - info.labels_lower_bound_.HostVector() - = std::vector(num_point, true_label_lower_bound); - info.labels_upper_bound_.HostVector() - = std::vector(num_point, true_label_upper_bound); + info.labels_lower_bound_.HostVector() = std::vector(num_point, true_label_lower_bound); + info.labels_upper_bound_.HostVector() = std::vector(num_point, true_label_upper_bound); info.weights_.HostVector() = std::vector(); std::vector preds(num_point); for (int i = 0; i < num_point; ++i) { @@ -76,90 +74,102 @@ static inline void CheckGPairOverGridPoints( void TestAFTObjGPairUncensoredLabels(const Context* ctx) { std::unique_ptr obj(ObjFunction::Create("survival:aft", ctx)); - CheckGPairOverGridPoints(obj.get(), 100.0f, 100.0f, "normal", - { -3.9120f, -3.4013f, -2.8905f, -2.3798f, -1.8691f, -1.3583f, -0.8476f, -0.3368f, 0.1739f, - 0.6846f, 1.1954f, 1.7061f, 2.2169f, 2.7276f, 3.2383f, 3.7491f, 4.2598f, 4.7706f, 5.2813f, - 5.7920f }, - { 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, - 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f }); - CheckGPairOverGridPoints(obj.get(), 100.0f, 100.0f, "logistic", - { -0.9608f, -0.9355f, -0.8948f, -0.8305f, -0.7327f, -0.5910f, -0.4001f, -0.1668f, 0.0867f, - 0.3295f, 0.5354f, 0.6927f, 0.8035f, 0.8773f, 0.9245f, 0.9540f, 0.9721f, 0.9832f, 0.9899f, - 0.9939f }, - { 0.0384f, 0.0624f, 0.0997f, 0.1551f, 0.2316f, 0.3254f, 0.4200f, 0.4861f, 0.4962f, 0.4457f, - 0.3567f, 0.2601f, 0.1772f, 0.1152f, 0.0726f, 0.0449f, 0.0275f, 0.0167f, 0.0101f, 0.0061f }); + CheckGPairOverGridPoints( + obj.get(), 100.0f, 100.0f, "normal", + {-3.9120f, -3.4013f, -2.8905f, -2.3798f, -1.8691f, -1.3583f, -0.8476f, + -0.3368f, 0.1739f, 0.6846f, 1.1954f, 1.7061f, 2.2169f, 2.7276f, + 3.2383f, 3.7491f, 4.2598f, 4.7706f, 5.2813f, 5.7920f}, + {1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, + 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f, 1.0000f}); + CheckGPairOverGridPoints( + obj.get(), 100.0f, 100.0f, "logistic", + {-0.9608f, -0.9355f, -0.8948f, -0.8305f, -0.7327f, -0.5910f, -0.4001f, + -0.1668f, 0.0867f, 0.3295f, 0.5354f, 0.6927f, 0.8035f, 0.8773f, + 0.9245f, 0.9540f, 0.9721f, 0.9832f, 0.9899f, 0.9939f}, + {0.0384f, 0.0624f, 0.0997f, 0.1551f, 0.2316f, 0.3254f, 0.4200f, 0.4861f, 0.4962f, 0.4457f, + 0.3567f, 0.2601f, 0.1772f, 0.1152f, 0.0726f, 0.0449f, 0.0275f, 0.0167f, 0.0101f, 0.0061f}); CheckGPairOverGridPoints(obj.get(), 100.0f, 100.0f, "extreme", - { -15.0000f, -15.0000f, -15.0000f, -9.8028f, -5.4822f, -2.8897f, -1.3340f, -0.4005f, 0.1596f, - 0.4957f, 0.6974f, 0.8184f, 0.8910f, 0.9346f, 0.9608f, 0.9765f, 0.9859f, 0.9915f, 0.9949f, - 0.9969f }, - { 15.0000f, 15.0000f, 15.0000f, 10.8028f, 6.4822f, 3.8897f, 2.3340f, 1.4005f, 0.8404f, 0.5043f, - 0.3026f, 0.1816f, 0.1090f, 0.0654f, 0.0392f, 0.0235f, 0.0141f, 0.0085f, 0.0051f, 0.0031f }); + {-15.0000f, -15.0000f, -15.0000f, -9.8028f, -5.4822f, -2.8897f, -1.3340f, + -0.4005f, 0.1596f, 0.4957f, 0.6974f, 0.8184f, 0.8910f, 0.9346f, + 0.9608f, 0.9765f, 0.9859f, 0.9915f, 0.9949f, 0.9969f}, + {15.0000f, 15.0000f, 15.0000f, 10.8028f, 6.4822f, 3.8897f, 2.3340f, + 1.4005f, 0.8404f, 0.5043f, 0.3026f, 0.1816f, 0.1090f, 0.0654f, + 0.0392f, 0.0235f, 0.0141f, 0.0085f, 0.0051f, 0.0031f}); } void TestAFTObjGPairLeftCensoredLabels(const Context* ctx) { std::unique_ptr obj(ObjFunction::Create("survival:aft", ctx)); - CheckGPairOverGridPoints(obj.get(), 0.0f, 20.0f, "normal", - { 0.0285f, 0.0832f, 0.1951f, 0.3804f, 0.6403f, 0.9643f, 1.3379f, 1.7475f, 2.1828f, 2.6361f, - 3.1023f, 3.5779f, 4.0603f, 4.5479f, 5.0394f, 5.5340f, 6.0309f, 6.5298f, 7.0303f, 7.5326f }, - { 0.0663f, 0.1559f, 0.2881f, 0.4378f, 0.5762f, 0.6878f, 0.7707f, 0.8300f, 0.8719f, 0.9016f, - 0.9229f, 0.9385f, 0.9501f, 0.9588f, 0.9656f, 0.9709f, 0.9751f, 0.9785f, 0.9813f, 0.9877f }); - CheckGPairOverGridPoints(obj.get(), 0.0f, 20.0f, "logistic", - { 0.0909f, 0.1428f, 0.2174f, 0.3164f, 0.4355f, 0.5625f, 0.6818f, 0.7812f, 0.8561f, 0.9084f, - 0.9429f, 0.9650f, 0.9787f, 0.9871f, 0.9922f, 0.9953f, 0.9972f, 0.9983f, 0.9990f, 0.9994f }, - { 0.0826f, 0.1224f, 0.1701f, 0.2163f, 0.2458f, 0.2461f, 0.2170f, 0.1709f, 0.1232f, 0.0832f, - 0.0538f, 0.0338f, 0.0209f, 0.0127f, 0.0077f, 0.0047f, 0.0028f, 0.0017f, 0.0010f, 0.0006f }); - CheckGPairOverGridPoints(obj.get(), 0.0f, 20.0f, "extreme", - { 0.0005f, 0.0149f, 0.1011f, 0.2815f, 0.4881f, 0.6610f, 0.7847f, 0.8665f, 0.9183f, 0.9504f, - 0.9700f, 0.9820f, 0.9891f, 0.9935f, 0.9961f, 0.9976f, 0.9986f, 0.9992f, 0.9995f, 0.9997f }, - { 0.0041f, 0.0747f, 0.2731f, 0.4059f, 0.3829f, 0.2901f, 0.1973f, 0.1270f, 0.0793f, 0.0487f, - 0.0296f, 0.0179f, 0.0108f, 0.0065f, 0.0039f, 0.0024f, 0.0014f, 0.0008f, 0.0005f, 0.0003f }); + CheckGPairOverGridPoints( + obj.get(), 0.0f, 20.0f, "normal", + {0.0285f, 0.0832f, 0.1951f, 0.3804f, 0.6403f, 0.9643f, 1.3379f, 1.7475f, 2.1828f, 2.6361f, + 3.1023f, 3.5779f, 4.0603f, 4.5479f, 5.0394f, 5.5340f, 6.0309f, 6.5298f, 7.0303f, 7.5326f}, + {0.0663f, 0.1559f, 0.2881f, 0.4378f, 0.5762f, 0.6878f, 0.7707f, 0.8300f, 0.8719f, 0.9016f, + 0.9229f, 0.9385f, 0.9501f, 0.9588f, 0.9656f, 0.9709f, 0.9751f, 0.9785f, 0.9813f, 0.9877f}); + CheckGPairOverGridPoints( + obj.get(), 0.0f, 20.0f, "logistic", + {0.0909f, 0.1428f, 0.2174f, 0.3164f, 0.4355f, 0.5625f, 0.6818f, 0.7812f, 0.8561f, 0.9084f, + 0.9429f, 0.9650f, 0.9787f, 0.9871f, 0.9922f, 0.9953f, 0.9972f, 0.9983f, 0.9990f, 0.9994f}, + {0.0826f, 0.1224f, 0.1701f, 0.2163f, 0.2458f, 0.2461f, 0.2170f, 0.1709f, 0.1232f, 0.0832f, + 0.0538f, 0.0338f, 0.0209f, 0.0127f, 0.0077f, 0.0047f, 0.0028f, 0.0017f, 0.0010f, 0.0006f}); + CheckGPairOverGridPoints( + obj.get(), 0.0f, 20.0f, "extreme", + {0.0005f, 0.0149f, 0.1011f, 0.2815f, 0.4881f, 0.6610f, 0.7847f, 0.8665f, 0.9183f, 0.9504f, + 0.9700f, 0.9820f, 0.9891f, 0.9935f, 0.9961f, 0.9976f, 0.9986f, 0.9992f, 0.9995f, 0.9997f}, + {0.0041f, 0.0747f, 0.2731f, 0.4059f, 0.3829f, 0.2901f, 0.1973f, 0.1270f, 0.0793f, 0.0487f, + 0.0296f, 0.0179f, 0.0108f, 0.0065f, 0.0039f, 0.0024f, 0.0014f, 0.0008f, 0.0005f, 0.0003f}); } void TestAFTObjGPairRightCensoredLabels(const Context* ctx) { std::unique_ptr obj(ObjFunction::Create("survival:aft", ctx)); - CheckGPairOverGridPoints(obj.get(), 60.0f, std::numeric_limits::infinity(), "normal", - { -3.6583f, -3.1815f, -2.7135f, -2.2577f, -1.8190f, -1.4044f, -1.0239f, -0.6905f, -0.4190f, - -0.2209f, -0.0973f, -0.0346f, -0.0097f, -0.0021f, -0.0004f, -0.0000f, -0.0000f, -0.0000f, - -0.0000f, -0.0000f }, - { 0.9407f, 0.9259f, 0.9057f, 0.8776f, 0.8381f, 0.7821f, 0.7036f, 0.5970f, 0.4624f, 0.3128f, - 0.1756f, 0.0780f, 0.0265f, 0.0068f, 0.0013f, 0.0002f, 0.0000f, 0.0000f, 0.0000f, 0.0000f }); - CheckGPairOverGridPoints(obj.get(), 60.0f, std::numeric_limits::infinity(), "logistic", - { -0.9677f, -0.9474f, -0.9153f, -0.8663f, -0.7955f, -0.7000f, -0.5834f, -0.4566f, -0.3352f, - -0.2323f, -0.1537f, -0.0982f, -0.0614f, -0.0377f, -0.0230f, -0.0139f, -0.0084f, -0.0051f, - -0.0030f, -0.0018f }, - { 0.0312f, 0.0499f, 0.0776f, 0.1158f, 0.1627f, 0.2100f, 0.2430f, 0.2481f, 0.2228f, 0.1783f, - 0.1300f, 0.0886f, 0.0576f, 0.0363f, 0.0225f, 0.0137f, 0.0083f, 0.0050f, 0.0030f, 0.0018f }); + CheckGPairOverGridPoints( + obj.get(), 60.0f, std::numeric_limits::infinity(), "normal", + {-3.6583f, -3.1815f, -2.7135f, -2.2577f, -1.8190f, -1.4044f, -1.0239f, + -0.6905f, -0.4190f, -0.2209f, -0.0973f, -0.0346f, -0.0097f, -0.0021f, + -0.0004f, -0.0000f, -0.0000f, -0.0000f, -0.0000f, -0.0000f}, + {0.9407f, 0.9259f, 0.9057f, 0.8776f, 0.8381f, 0.7821f, 0.7036f, 0.5970f, 0.4624f, 0.3128f, + 0.1756f, 0.0780f, 0.0265f, 0.0068f, 0.0013f, 0.0002f, 0.0000f, 0.0000f, 0.0000f, 0.0000f}); + CheckGPairOverGridPoints( + obj.get(), 60.0f, std::numeric_limits::infinity(), "logistic", + {-0.9677f, -0.9474f, -0.9153f, -0.8663f, -0.7955f, -0.7000f, -0.5834f, + -0.4566f, -0.3352f, -0.2323f, -0.1537f, -0.0982f, -0.0614f, -0.0377f, + -0.0230f, -0.0139f, -0.0084f, -0.0051f, -0.0030f, -0.0018f}, + {0.0312f, 0.0499f, 0.0776f, 0.1158f, 0.1627f, 0.2100f, 0.2430f, 0.2481f, 0.2228f, 0.1783f, + 0.1300f, 0.0886f, 0.0576f, 0.0363f, 0.0225f, 0.0137f, 0.0083f, 0.0050f, 0.0030f, 0.0018f}); CheckGPairOverGridPoints(obj.get(), 60.0f, std::numeric_limits::infinity(), "extreme", - { -15.0000f, -15.0000f, -10.8018f, -6.4817f, -3.8893f, -2.3338f, -1.4004f, -0.8403f, -0.5042f, - -0.3026f, -0.1816f, -0.1089f, -0.0654f, -0.0392f, -0.0235f, -0.0141f, -0.0085f, -0.0051f, - -0.0031f, -0.0018f }, - { 15.0000f, 15.0000f, 10.8018f, 6.4817f, 3.8893f, 2.3338f, 1.4004f, 0.8403f, 0.5042f, 0.3026f, - 0.1816f, 0.1089f, 0.0654f, 0.0392f, 0.0235f, 0.0141f, 0.0085f, 0.0051f, 0.0031f, 0.0018f }); + {-15.0000f, -15.0000f, -10.8018f, -6.4817f, -3.8893f, -2.3338f, -1.4004f, + -0.8403f, -0.5042f, -0.3026f, -0.1816f, -0.1089f, -0.0654f, -0.0392f, + -0.0235f, -0.0141f, -0.0085f, -0.0051f, -0.0031f, -0.0018f}, + {15.0000f, 15.0000f, 10.8018f, 6.4817f, 3.8893f, 2.3338f, 1.4004f, + 0.8403f, 0.5042f, 0.3026f, 0.1816f, 0.1089f, 0.0654f, 0.0392f, + 0.0235f, 0.0141f, 0.0085f, 0.0051f, 0.0031f, 0.0018f}); } void TestAFTObjGPairIntervalCensoredLabels(const Context* ctx) { std::unique_ptr obj(ObjFunction::Create("survival:aft", ctx)); - CheckGPairOverGridPoints(obj.get(), 16.0f, 200.0f, "normal", - { -2.4435f, -1.9965f, -1.5691f, -1.1679f, -0.7990f, -0.4649f, -0.1596f, 0.1336f, 0.4370f, - 0.7682f, 1.1340f, 1.5326f, 1.9579f, 2.4035f, 2.8639f, 3.3351f, 3.8143f, 4.2995f, 4.7891f, - 5.2822f }, - { 0.8909f, 0.8579f, 0.8134f, 0.7557f, 0.6880f, 0.6221f, 0.5789f, 0.5769f, 0.6171f, 0.6818f, - 0.7500f, 0.8088f, 0.8545f, 0.8884f, 0.9131f, 0.9312f, 0.9446f, 0.9547f, 0.9624f, 0.9684f }); - CheckGPairOverGridPoints(obj.get(), 16.0f, 200.0f, "logistic", - { -0.8790f, -0.8112f, -0.7153f, -0.5893f, -0.4375f, -0.2697f, -0.0955f, 0.0800f, 0.2545f, - 0.4232f, 0.5768f, 0.7054f, 0.8040f, 0.8740f, 0.9210f, 0.9513f, 0.9703f, 0.9820f, 0.9891f, - 0.9934f }, - { 0.1086f, 0.1588f, 0.2176f, 0.2745f, 0.3164f, 0.3374f, 0.3433f, 0.3434f, 0.3384f, 0.3191f, - 0.2789f, 0.2229f, 0.1637f, 0.1125f, 0.0737f, 0.0467f, 0.0290f, 0.0177f, 0.0108f, 0.0065f }); - CheckGPairOverGridPoints(obj.get(), 16.0f, 200.0f, "extreme", - { -8.0000f, -4.8004f, -2.8805f, -1.7284f, -1.0371f, -0.6168f, -0.3140f, -0.0121f, 0.2841f, - 0.5261f, 0.6989f, 0.8132f, 0.8857f, 0.9306f, 0.9581f, 0.9747f, 0.9848f, 0.9909f, 0.9945f, - 0.9967f }, - { 8.0000f, 4.8004f, 2.8805f, 1.7284f, 1.0380f, 0.6567f, 0.5727f, 0.6033f, 0.5384f, 0.4051f, - 0.2757f, 0.1776f, 0.1110f, 0.0682f, 0.0415f, 0.0251f, 0.0151f, 0.0091f, 0.0055f, 0.0033f }); + CheckGPairOverGridPoints( + obj.get(), 16.0f, 200.0f, "normal", + {-2.4435f, -1.9965f, -1.5691f, -1.1679f, -0.7990f, -0.4649f, -0.1596f, + 0.1336f, 0.4370f, 0.7682f, 1.1340f, 1.5326f, 1.9579f, 2.4035f, + 2.8639f, 3.3351f, 3.8143f, 4.2995f, 4.7891f, 5.2822f}, + {0.8909f, 0.8579f, 0.8134f, 0.7557f, 0.6880f, 0.6221f, 0.5789f, 0.5769f, 0.6171f, 0.6818f, + 0.7500f, 0.8088f, 0.8545f, 0.8884f, 0.9131f, 0.9312f, 0.9446f, 0.9547f, 0.9624f, 0.9684f}); + CheckGPairOverGridPoints( + obj.get(), 16.0f, 200.0f, "logistic", + {-0.8790f, -0.8112f, -0.7153f, -0.5893f, -0.4375f, -0.2697f, -0.0955f, + 0.0800f, 0.2545f, 0.4232f, 0.5768f, 0.7054f, 0.8040f, 0.8740f, + 0.9210f, 0.9513f, 0.9703f, 0.9820f, 0.9891f, 0.9934f}, + {0.1086f, 0.1588f, 0.2176f, 0.2745f, 0.3164f, 0.3374f, 0.3433f, 0.3434f, 0.3384f, 0.3191f, + 0.2789f, 0.2229f, 0.1637f, 0.1125f, 0.0737f, 0.0467f, 0.0290f, 0.0177f, 0.0108f, 0.0065f}); + CheckGPairOverGridPoints( + obj.get(), 16.0f, 200.0f, "extreme", + {-8.0000f, -4.8004f, -2.8805f, -1.7284f, -1.0371f, -0.6168f, -0.3140f, + -0.0121f, 0.2841f, 0.5261f, 0.6989f, 0.8132f, 0.8857f, 0.9306f, + 0.9581f, 0.9747f, 0.9848f, 0.9909f, 0.9945f, 0.9967f}, + {8.0000f, 4.8004f, 2.8805f, 1.7284f, 1.0380f, 0.6567f, 0.5727f, 0.6033f, 0.5384f, 0.4051f, + 0.2757f, 0.1776f, 0.1110f, 0.0682f, 0.0415f, 0.0251f, 0.0151f, 0.0091f, 0.0055f, 0.0033f}); } } // namespace xgboost::common diff --git a/tests/cpp/objective/test_aft_obj.h b/tests/cpp/objective/test_aft_obj.h index 3f959dc6eb52..e7b82fa46164 100644 --- a/tests/cpp/objective/test_aft_obj.h +++ b/tests/cpp/objective/test_aft_obj.h @@ -1,8 +1,8 @@ /** * Copyright 2020-2024 by XGBoost Contributors */ -#ifndef XGBOOST_TEST_AFT_OBJ_H_ -#define XGBOOST_TEST_AFT_OBJ_H_ +#ifndef TESTS_CPP_OBJECTIVE_TEST_AFT_OBJ_H_ +#define TESTS_CPP_OBJECTIVE_TEST_AFT_OBJ_H_ #include // for Context @@ -20,4 +20,4 @@ void TestAFTObjGPairIntervalCensoredLabels(const Context* ctx); } // namespace xgboost::common -#endif // XGBOOST_TEST_AFT_OBJ_H_ +#endif // TESTS_CPP_OBJECTIVE_TEST_AFT_OBJ_H_ diff --git a/tests/cpp/objective/test_aft_obj_cpu.cc b/tests/cpp/objective/test_aft_obj_cpu.cc index f457e4f97f3b..1947905ad104 100644 --- a/tests/cpp/objective/test_aft_obj_cpu.cc +++ b/tests/cpp/objective/test_aft_obj_cpu.cc @@ -2,15 +2,16 @@ * Copyright 2020-2024, XGBoost Contributors */ #include + +#include +#include #include #include -#include -#include -#include "xgboost/objective.h" -#include "xgboost/logging.h" #include "../helpers.h" #include "test_aft_obj.h" +#include "xgboost/logging.h" +#include "xgboost/objective.h" namespace xgboost::common { TEST(Objective, DeclareUnifiedTest(AFTObjConfiguration)) { diff --git a/tests/cpp/objective/test_hinge.cc b/tests/cpp/objective/test_hinge.cc index d7c6e3d89548..4e4c8e122e02 100644 --- a/tests/cpp/objective/test_hinge.cc +++ b/tests/cpp/objective/test_hinge.cc @@ -1,13 +1,17 @@ /** * Copyright 2018-2023, XGBoost Contributors */ -#include +#include "test_hinge.h" + #include +#include + #include +#include +#include -#include "../helpers.h" -#include "test_hinge.h" #include "../../../src/common/linalg_op.h" +#include "../helpers.h" namespace xgboost { void TestHingeObj(const Context* ctx) { @@ -15,7 +19,7 @@ void TestHingeObj(const Context* ctx) { float eps = std::numeric_limits::min(); std::vector predt{-1.0f, -0.5f, 0.5f, 1.0f, -1.0f, -0.5f, 0.5f, 1.0f}; - std::vector label{ 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f}; + std::vector label{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f}; std::vector grad{0.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 0.0f}; std::vector hess{eps, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, eps}; diff --git a/tests/cpp/objective/test_hinge.cu b/tests/cpp/objective/test_hinge.cu index a5aacd2f86e2..cf5c1dbbe722 100644 --- a/tests/cpp/objective/test_hinge.cu +++ b/tests/cpp/objective/test_hinge.cu @@ -1 +1,4 @@ +/** + * Copyright 2026, XGBoost contributors + */ #include "test_hinge_cpu.cc" diff --git a/tests/cpp/objective/test_hinge.h b/tests/cpp/objective/test_hinge.h index 8534877596d7..f8c45907f904 100644 --- a/tests/cpp/objective/test_hinge.h +++ b/tests/cpp/objective/test_hinge.h @@ -1,8 +1,8 @@ /** * Copyright 2020-2024 by XGBoost Contributors */ -#ifndef XGBOOST_TEST_HINGE_H_ -#define XGBOOST_TEST_HINGE_H_ +#ifndef TESTS_CPP_OBJECTIVE_TEST_HINGE_H_ +#define TESTS_CPP_OBJECTIVE_TEST_HINGE_H_ #include // for Context @@ -12,4 +12,4 @@ void TestHingeObj(const Context* ctx); } // namespace xgboost -#endif // XGBOOST_TEST_REGRESSION_OBJ_H_ +#endif // TESTS_CPP_OBJECTIVE_TEST_HINGE_H_ diff --git a/tests/cpp/objective/test_hinge_cpu.cc b/tests/cpp/objective/test_hinge_cpu.cc index f993eebc7c10..0a55c5646d2d 100644 --- a/tests/cpp/objective/test_hinge_cpu.cc +++ b/tests/cpp/objective/test_hinge_cpu.cc @@ -1,13 +1,14 @@ /** * Copyright 2018-2023, XGBoost Contributors */ -#include #include +#include + #include +#include "../../../src/common/linalg_op.h" #include "../helpers.h" #include "test_hinge.h" -#include "../../../src/common/linalg_op.h" namespace xgboost { diff --git a/tests/cpp/objective/test_lambdarank_obj.cc b/tests/cpp/objective/test_lambdarank_obj.cc index 36dcab5bc065..770ab2000b89 100644 --- a/tests/cpp/objective/test_lambdarank_obj.cc +++ b/tests/cpp/objective/test_lambdarank_obj.cc @@ -7,6 +7,7 @@ #include // for sort #include // for size_t +#include // for greater #include // for initializer_list #include // for unique_ptr, shared_ptr, make_shared #include // for iota @@ -37,25 +38,15 @@ void TestNDCGGPair(Context const* ctx) { CheckConfigReload(obj, "rank:ndcg"); // No gain in swapping 2 documents. - CheckRankingObjFunction(obj, - {1, 1, 1, 1}, - {1, 1, 1, 1}, - {1.0f, 1.0f}, - {0, 2, 4}, - {0.0f, -0.0f, 0.0f, 0.0f}, - {0.0f, 0.0f, 0.0f, 0.0f}); + CheckRankingObjFunction(obj, {1, 1, 1, 1}, {1, 1, 1, 1}, {1.0f, 1.0f}, {0, 2, 4}, + {0.0f, -0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}); } { std::unique_ptr obj{xgboost::ObjFunction::Create("rank:ndcg", ctx)}; obj->Configure(Args{{"lambdarank_pair_method", "topk"}}); // Test with setting sample weight to second query group - CheckRankingObjFunction(obj, - {0, 0.1f, 0, 0.1f}, - {0, 1, 0, 1}, - {2.0f, 0.0f}, - {0, 2, 4}, - {2.06611f, -2.06611f, 0.0f, 0.0f}, - {2.169331f, 2.169331f, 0.0f, 0.0f}); + CheckRankingObjFunction(obj, {0, 0.1f, 0, 0.1f}, {0, 1, 0, 1}, {2.0f, 0.0f}, {0, 2, 4}, + {2.06611f, -2.06611f, 0.0f, 0.0f}, {2.169331f, 2.169331f, 0.0f, 0.0f}); } { std::unique_ptr obj{xgboost::ObjFunction::Create("rank:ndcg", ctx)}; @@ -63,7 +54,9 @@ void TestNDCGGPair(Context const* ctx) { float weight_norm = 0.5; // n_groups / sum_weights std::vector out_grad{2.06611f, -2.06611f, 2.06611f, -2.06611f}; std::vector out_hess{2.169331f, 2.169331f, 2.169331f, 2.169331f}; - auto norm = [=](auto v) { return v * weight_norm; }; + auto norm = [=](auto v) { + return v * weight_norm; + }; std::transform(out_grad.begin(), out_grad.end(), out_grad.begin(), norm); std::transform(out_hess.begin(), out_hess.end(), out_hess.begin(), norm); CheckRankingObjFunction(obj, {0, 0.1f, 0, 0.1f}, {0, 1, 0, 1}, {2.0f, 2.0f}, {0, 2, 4}, diff --git a/tests/cpp/objective/test_lambdarank_obj.cu b/tests/cpp/objective/test_lambdarank_obj.cu index c80ec20fc63d..273bf9d91e52 100644 --- a/tests/cpp/objective/test_lambdarank_obj.cu +++ b/tests/cpp/objective/test_lambdarank_obj.cu @@ -2,10 +2,12 @@ * Copyright 2023 by XGBoost Contributors */ #include -#include // for Context +#include // for Context -#include // for uint32_t -#include // for vector +#include // for uint32_t +#include +#include // for make_pair +#include // for vector #include "../../../src/common/cuda_context.cuh" // for CUDAContext #include "../../../src/objective/lambdarank_obj.cuh" @@ -137,7 +139,9 @@ void RankItemCountImpl(std::vector const &sorted_items, CountFunc TEST(LambdaRank, RankItemCountOnLeft) { // Items sorted descendingly std::vector sorted_items{10, 10, 6, 4, 4, 4, 4, 1, 1, 1, 1, 1, 0}; - auto wrapper = [](auto const &...args) { return cuda_impl::CountNumItemsToTheLeftOf(args...); }; + auto wrapper = [](auto const &...args) { + return cuda_impl::CountNumItemsToTheLeftOf(args...); + }; RankItemCountImpl(sorted_items, wrapper, 10, static_cast(0)); RankItemCountImpl(sorted_items, wrapper, 6, static_cast(2)); RankItemCountImpl(sorted_items, wrapper, 4, static_cast(3)); @@ -148,7 +152,9 @@ TEST(LambdaRank, RankItemCountOnLeft) { TEST(LambdaRank, RankItemCountOnRight) { // Items sorted descendingly std::vector sorted_items{10, 10, 6, 4, 4, 4, 4, 1, 1, 1, 1, 1, 0}; - auto wrapper = [](auto const &...args) { return cuda_impl::CountNumItemsToTheRightOf(args...); }; + auto wrapper = [](auto const &...args) { + return cuda_impl::CountNumItemsToTheRightOf(args...); + }; RankItemCountImpl(sorted_items, wrapper, 10, static_cast(11)); RankItemCountImpl(sorted_items, wrapper, 6, static_cast(10)); RankItemCountImpl(sorted_items, wrapper, 4, static_cast(6)); diff --git a/tests/cpp/objective/test_lambdarank_obj.h b/tests/cpp/objective/test_lambdarank_obj.h index 4383a44d1a75..6fcc823ccf88 100644 --- a/tests/cpp/objective/test_lambdarank_obj.h +++ b/tests/cpp/objective/test_lambdarank_obj.h @@ -1,18 +1,18 @@ /** * Copyright 2023-2025, XGBoost Contributors */ -#ifndef XGBOOST_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_ -#define XGBOOST_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_ +#ifndef TESTS_CPP_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_ +#define TESTS_CPP_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_ #include -#include // for MetaInfo -#include // for HostDeviceVector -#include // for All -#include // for ObjFunction +#include // for MetaInfo +#include // for HostDeviceVector +#include // for All +#include // for ObjFunction -#include // for shared_ptr, make_shared +#include // for shared_ptr, make_shared -#include "../../../src/common/ranking_utils.h" // for LambdaRankParam, MAPCache -#include "../helpers.h" // for EmptyDMatrix +#include "../../../src/common/ranking_utils.h" // for LambdaRankParam, MAPCache +#include "../helpers.h" // for EmptyDMatrix namespace xgboost::obj { void TestMAPStat(Context const* ctx); @@ -43,4 +43,4 @@ void TestMAPGPair(Context const* ctx); */ void InitMakePairTest(Context const* ctx, MetaInfo* out_info, HostDeviceVector* out_predt); } // namespace xgboost::obj -#endif // XGBOOST_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_ +#endif // TESTS_CPP_OBJECTIVE_TEST_LAMBDARANK_OBJ_H_ diff --git a/tests/cpp/objective/test_multiclass_obj.cc b/tests/cpp/objective/test_multiclass_obj.cc index ae3427b61398..f3ee491e3aee 100644 --- a/tests/cpp/objective/test_multiclass_obj.cc +++ b/tests/cpp/objective/test_multiclass_obj.cc @@ -1,35 +1,38 @@ /** * Copyright 2018-2025, XGBoost contributors */ -#include +#include "test_multiclass_obj.h" + #include +#include + +#include +#include +#include +#include + #include "../helpers.h" -#include "test_multiclass_obj.h" namespace xgboost { void TestSoftmaxMultiClassObjGPair(const Context* ctx) { - std::vector> args {{"num_class", "3"}}; - std::unique_ptr obj { - ObjFunction::Create("multi:softmax", ctx) - }; + std::vector> args{{"num_class", "3"}}; + std::unique_ptr obj{ObjFunction::Create("multi:softmax", ctx)}; obj->Configure(args); CheckConfigReload(obj, "multi:softmax"); - CheckObjFunction(obj, - {1.0f, 0.0f, 2.0f, 2.0f, 0.0f, 1.0f}, // preds - {1.0f, 0.0f}, // labels - {1.0f, 1.0f}, // weights - {0.24f, -0.91f, 0.66f, -0.33f, 0.09f, 0.24f}, // grad - {0.36f, 0.16f, 0.44f, 0.45f, 0.16f, 0.37f}); // hess + CheckObjFunction(obj, {1.0f, 0.0f, 2.0f, 2.0f, 0.0f, 1.0f}, // preds + {1.0f, 0.0f}, // labels + {1.0f, 1.0f}, // weights + {0.24f, -0.91f, 0.66f, -0.33f, 0.09f, 0.24f}, // grad + {0.36f, 0.16f, 0.44f, 0.45f, 0.16f, 0.37f}); // hess - CheckObjFunction(obj, - {1.0f, 0.0f, 2.0f, 2.0f, 0.0f, 1.0f}, // preds - {1.0f, 0.0f}, // labels - {}, // weights - {0.24f, -0.91f, 0.66f, -0.33f, 0.09f, 0.24f}, // grad - {0.36f, 0.16f, 0.44f, 0.45f, 0.16f, 0.37f}); // hess + CheckObjFunction(obj, {1.0f, 0.0f, 2.0f, 2.0f, 0.0f, 1.0f}, // preds + {1.0f, 0.0f}, // labels + {}, // weights + {0.24f, -0.91f, 0.66f, -0.33f, 0.09f, 0.24f}, // grad + {0.36f, 0.16f, 0.44f, 0.45f, 0.16f, 0.37f}); // hess ASSERT_NO_THROW({ [[maybe_unused]] auto _ = obj->DefaultEvalMetric(); }); } @@ -42,8 +45,7 @@ void TestSoftmaxMultiClassBasic(const Context* ctx) { obj->Configure(args); CheckConfigReload(obj, "multi:softmax"); - HostDeviceVector io_preds = {2.0f, 0.0f, 1.0f, - 1.0f, 0.0f, 2.0f}; + HostDeviceVector io_preds = {2.0f, 0.0f, 1.0f, 1.0f, 0.0f, 2.0f}; std::vector out_preds = {0.0f, 2.0f}; obj->PredTransform(&io_preds); @@ -55,16 +57,14 @@ void TestSoftmaxMultiClassBasic(const Context* ctx) { } void TestSoftprobMultiClassBasic(const Context* ctx) { - std::vector> args { - std::pair("num_class", "3")}; + std::vector> args{ + std::pair("num_class", "3")}; - std::unique_ptr obj { - ObjFunction::Create("multi:softprob", ctx) - }; + std::unique_ptr obj{ObjFunction::Create("multi:softprob", ctx)}; obj->Configure(args); CheckConfigReload(obj, "multi:softprob"); - HostDeviceVector io_preds = {2.0f, 0.0f, 1.0f}; + HostDeviceVector io_preds = {2.0f, 0.0f, 1.0f}; std::vector out_preds = {0.66524096f, 0.09003057f, 0.24472847f}; obj->PredTransform(&io_preds); diff --git a/tests/cpp/objective/test_multiclass_obj.h b/tests/cpp/objective/test_multiclass_obj.h index bf6f9258c6ab..197ee1c78227 100644 --- a/tests/cpp/objective/test_multiclass_obj.h +++ b/tests/cpp/objective/test_multiclass_obj.h @@ -1,8 +1,8 @@ /** * Copyright 2020-2023 by XGBoost Contributors */ -#ifndef XGBOOST_TEST_MULTICLASS_OBJ_H_ -#define XGBOOST_TEST_MULTICLASS_OBJ_H_ +#ifndef TESTS_CPP_OBJECTIVE_TEST_MULTICLASS_OBJ_H_ +#define TESTS_CPP_OBJECTIVE_TEST_MULTICLASS_OBJ_H_ #include // for Context @@ -16,4 +16,4 @@ void TestSoftprobMultiClassBasic(const Context* ctx); } // namespace xgboost -#endif // XGBOOST_TEST_MULTICLASS_OBJ_H_ +#endif // TESTS_CPP_OBJECTIVE_TEST_MULTICLASS_OBJ_H_ diff --git a/tests/cpp/objective/test_multiclass_obj_gpu.cu b/tests/cpp/objective/test_multiclass_obj_gpu.cu index f80f07ce8c72..6c7c679580df 100644 --- a/tests/cpp/objective/test_multiclass_obj_gpu.cu +++ b/tests/cpp/objective/test_multiclass_obj_gpu.cu @@ -1 +1,4 @@ +/** + * Copyright 2026, XGBoost contributors + */ #include "test_multiclass_obj_cpu.cc" diff --git a/tests/cpp/objective/test_objective.cc b/tests/cpp/objective/test_objective.cc index 20260b893c38..824a647068d4 100644 --- a/tests/cpp/objective/test_objective.cc +++ b/tests/cpp/objective/test_objective.cc @@ -5,6 +5,10 @@ #include #include +#include +#include +#include + #include "../helpers.h" #include "../objective_helpers.h" @@ -101,4 +105,4 @@ INSTANTIATE_TEST_SUITE_P(Objective, TestDefaultObjConfig, [](const ::testing::TestParamInfo& info) { return ObjTestNameGenerator(info); }); -} // namespace xgboost +} // namespace xgboost diff --git a/tests/cpp/objective/test_objective_helpers.h b/tests/cpp/objective/test_objective_helpers.h index 4d049772b98f..f3b4692005ef 100644 --- a/tests/cpp/objective/test_objective_helpers.h +++ b/tests/cpp/objective/test_objective_helpers.h @@ -8,6 +8,7 @@ #include // for HostDeviceVector #include // for iota +#include // for vector namespace xgboost { // Generate node position for two nodes. diff --git a/tests/cpp/objective/test_quantile_obj.cc b/tests/cpp/objective/test_quantile_obj.cc index 1a6eb79cbf8a..f52bb61670c1 100644 --- a/tests/cpp/objective/test_quantile_obj.cc +++ b/tests/cpp/objective/test_quantile_obj.cc @@ -13,7 +13,7 @@ #include // for unique_ptr #include // for vector -#include "../helpers.h" // CheckConfigReload,MakeCUDACtx,DeclareUnifiedTest +#include "../helpers.h" // CheckConfigReload,MakeCUDACtx,DeclareUnifiedTest namespace xgboost { void TestQuantile(Context const* ctx) { diff --git a/tests/cpp/objective/test_quantile_obj.h b/tests/cpp/objective/test_quantile_obj.h index 63c3cfd3442c..a5bff6d46a7f 100644 --- a/tests/cpp/objective/test_quantile_obj.h +++ b/tests/cpp/objective/test_quantile_obj.h @@ -1,8 +1,8 @@ /** * Copyright 2020-2026, XGBoost Contributors */ -#ifndef XGBOOST_TEST_QUANTILE_OBJ_H_ -#define XGBOOST_TEST_QUANTILE_OBJ_H_ +#ifndef TESTS_CPP_OBJECTIVE_TEST_QUANTILE_OBJ_H_ +#define TESTS_CPP_OBJECTIVE_TEST_QUANTILE_OBJ_H_ #include // for Context @@ -14,4 +14,4 @@ void TestQuantileIntercept(Context const* ctx); } // namespace xgboost -#endif // XGBOOST_TEST_REGRESSION_OBJ_H_ +#endif // TESTS_CPP_OBJECTIVE_TEST_QUANTILE_OBJ_H_ diff --git a/tests/cpp/objective/test_quantile_obj_cpu.cc b/tests/cpp/objective/test_quantile_obj_cpu.cc index 6db11c1e409d..3d9ce2520f23 100644 --- a/tests/cpp/objective/test_quantile_obj_cpu.cc +++ b/tests/cpp/objective/test_quantile_obj_cpu.cc @@ -4,6 +4,8 @@ #include #include +#include + #include "../helpers.h" #include "test_quantile_obj.h" #include "test_regression_obj.h" // for TestVectorLeafObj diff --git a/tests/cpp/objective/test_regression_obj.cc b/tests/cpp/objective/test_regression_obj.cc index 9f63f85af4a8..9187a0f19b8e 100644 --- a/tests/cpp/objective/test_regression_obj.cc +++ b/tests/cpp/objective/test_regression_obj.cc @@ -9,7 +9,10 @@ #include #include // for RegTree +#include #include // for iota +#include +#include #include "../../../src/common/linalg_op.h" // for begin, end #include "../../../src/tree/param.h" // for TrainParam @@ -39,18 +42,12 @@ void TestLinearRegressionGPair(const Context* ctx) { std::unique_ptr obj{ObjFunction::Create(obj_name, ctx)}; obj->Configure(args); - CheckObjFunction(obj, - {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - {0, 0, 0, 0, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1}, - {0, 0.1f, 0.9f, 1.0f, -1.0f, -0.9f, -0.1f, 0}, - {1, 1, 1, 1, 1, 1, 1, 1}); - CheckObjFunction(obj, - {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - {0, 0, 0, 0, 1, 1, 1, 1}, + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, 0, 0, 0, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1, 1}, {0, 0.1f, 0.9f, 1.0f, -1.0f, -0.9f, -0.1f, 0}, + {1, 1, 1, 1, 1, 1, 1, 1}); + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, 0, 0, 0, 1, 1, 1, 1}, {}, // empty weight - {0, 0.1f, 0.9f, 1.0f, -1.0f, -0.9f, -0.1f, 0}, - {1, 1, 1, 1, 1, 1, 1, 1}); + {0, 0.1f, 0.9f, 1.0f, -1.0f, -0.9f, -0.1f, 0}, {1, 1, 1, 1, 1, 1, 1, 1}); ASSERT_NO_THROW({ [[maybe_unused]] auto _ = obj->DefaultEvalMetric(); }); } @@ -63,18 +60,16 @@ void TestSquaredLog(const Context* ctx) { obj->Configure(args); CheckConfigReload(obj, obj_name); - CheckObjFunction(obj, - {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred - {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels - {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // weights + CheckObjFunction(obj, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred + {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels + {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // weights {-0.5435f, -0.4257f, -0.25475f, -0.05855f, 0.1009f}, - { 1.3205f, 1.0492f, 0.69215f, 0.34115f, 0.1091f}); - CheckObjFunction(obj, - {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred - {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels - {}, // empty weights + {1.3205f, 1.0492f, 0.69215f, 0.34115f, 0.1091f}); + CheckObjFunction(obj, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred + {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels + {}, // empty weights {-0.5435f, -0.4257f, -0.25475f, -0.05855f, 0.1009f}, - { 1.3205f, 1.0492f, 0.69215f, 0.34115f, 0.1091f}); + {1.3205f, 1.0492f, 0.69215f, 0.34115f, 0.1091f}); ASSERT_EQ(obj->DefaultEvalMetric(), std::string{"rmsle"}); } @@ -86,12 +81,11 @@ void TestLogisticRegressionGPair(const Context* ctx) { obj->Configure(args); CheckConfigReload(obj, obj_name); - CheckObjFunction(obj, - { 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, // preds - { 0, 0, 0, 0, 1, 1, 1, 1}, // labels - { 1, 1, 1, 1, 1, 1, 1, 1}, // weights - { 0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f}, // out_grad - {0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f}); // out_hess + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, // preds + {0, 0, 0, 0, 1, 1, 1, 1}, // labels + {1, 1, 1, 1, 1, 1, 1, 1}, // weights + {0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f}, // out_grad + {0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f}); // out_hess } void TestLogisticRegressionBasic(const Context* ctx) { @@ -104,7 +98,7 @@ void TestLogisticRegressionBasic(const Context* ctx) { // test label validation EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {10}, {1}, {0}, {0})) - << "Expected error when label not in range [0,1f] for LogisticRegression"; + << "Expected error when label not in range [0,1f] for LogisticRegression"; // test ProbToMargin CheckProbaToMargin(obj, 0.1f, -2.197f); @@ -125,52 +119,41 @@ void TestLogisticRegressionBasic(const Context* ctx) { void TestsLogisticRawGPair(const Context* ctx) { std::string obj_name = "binary:logitraw"; std::vector> args; - std::unique_ptr obj {ObjFunction::Create(obj_name, ctx)}; + std::unique_ptr obj{ObjFunction::Create(obj_name, ctx)}; obj->Configure(args); - CheckObjFunction(obj, - { 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - { 0, 0, 0, 0, 1, 1, 1, 1}, - { 1, 1, 1, 1, 1, 1, 1, 1}, - { 0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f}, - {0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f}); + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, 0, 0, 0, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1, 1}, + {0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f}, + {0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f}); } void TestPoissonRegressionGPair(const Context* ctx) { std::vector> args; - std::unique_ptr obj { - ObjFunction::Create("count:poisson", ctx) - }; + std::unique_ptr obj{ObjFunction::Create("count:poisson", ctx)}; args.emplace_back("max_delta_step", "0.1f"); obj->Configure(args); - CheckObjFunction(obj, - { 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - { 0, 0, 0, 0, 1, 1, 1, 1}, - { 1, 1, 1, 1, 1, 1, 1, 1}, - { 1, 1.10f, 2.45f, 2.71f, 0, 0.10f, 1.45f, 1.71f}, + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, 0, 0, 0, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1.10f, 2.45f, 2.71f, 0, 0.10f, 1.45f, 1.71f}, {1.10f, 1.22f, 2.71f, 3.00f, 1.10f, 1.22f, 2.71f, 3.00f}); - CheckObjFunction(obj, - { 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - { 0, 0, 0, 0, 1, 1, 1, 1}, + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, 0, 0, 0, 1, 1, 1, 1}, {}, // Empty weight - { 1, 1.10f, 2.45f, 2.71f, 0, 0.10f, 1.45f, 1.71f}, + {1, 1.10f, 2.45f, 2.71f, 0, 0.10f, 1.45f, 1.71f}, {1.10f, 1.22f, 2.71f, 3.00f, 1.10f, 1.22f, 2.71f, 3.00f}); } void TestPoissonRegressionBasic(const Context* ctx) { std::vector> args; - std::unique_ptr obj { - ObjFunction::Create("count:poisson", ctx) - }; + std::unique_ptr obj{ObjFunction::Create("count:poisson", ctx)}; obj->Configure(args); CheckConfigReload(obj, "count:poisson"); // test label validation EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0})) - << "Expected error when label < 0 for PoissonRegression"; + << "Expected error when label < 0 for PoissonRegression"; // test ProbToMargin CheckProbaToMargin(obj, 0.1f, -2.30f); @@ -189,23 +172,16 @@ void TestPoissonRegressionBasic(const Context* ctx) { void TestGammaRegressionGPair(const Context* ctx) { std::vector> args; - std::unique_ptr obj { - ObjFunction::Create("reg:gamma", ctx) - }; + std::unique_ptr obj{ObjFunction::Create("reg:gamma", ctx)}; obj->Configure(args); - CheckObjFunction(obj, - {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - {2, 2, 2, 2, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1}, - {-1, -0.809, 0.187, 0.264, 0, 0.09f, 0.59f, 0.63f}, - {2, 1.809, 0.813, 0.735, 1, 0.90f, 0.40f, 0.36f}); - CheckObjFunction(obj, - {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - {2, 2, 2, 2, 1, 1, 1, 1}, + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {2, 2, 2, 2, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1, 1}, {-1, -0.809, 0.187, 0.264, 0, 0.09f, 0.59f, 0.63f}, + {2, 1.809, 0.813, 0.735, 1, 0.90f, 0.40f, 0.36f}); + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {2, 2, 2, 2, 1, 1, 1, 1}, {}, // Empty weight - {-1, -0.809, 0.187, 0.264, 0, 0.09f, 0.59f, 0.63f}, - {2, 1.809, 0.813, 0.735, 1, 0.90f, 0.40f, 0.36f}); + {-1, -0.809, 0.187, 0.264, 0, 0.09f, 0.59f, 0.63f}, + {2, 1.809, 0.813, 0.735, 1, 0.90f, 0.40f, 0.36f}); } void TestGammaRegressionBasic(const Context* ctx) { @@ -217,9 +193,9 @@ void TestGammaRegressionBasic(const Context* ctx) { // test label validation EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {0}, {1}, {0}, {0})) - << "Expected error when label = 0 for GammaRegression"; + << "Expected error when label = 0 for GammaRegression"; EXPECT_ANY_THROW(CheckObjFunction(obj, {-1}, {-1}, {1}, {-1}, {-3})) - << "Expected error when label < 0 for GammaRegression"; + << "Expected error when label < 0 for GammaRegression"; // test ProbToMargin CheckProbaToMargin(obj, 0.1f, -2.30f); @@ -243,17 +219,12 @@ void TestTweedieRegressionGPair(const Context* ctx) { args.emplace_back("tweedie_variance_power", "1.1f"); obj->Configure(args); - CheckObjFunction(obj, - { 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - { 0, 0, 0, 0, 1, 1, 1, 1}, - { 1, 1, 1, 1, 1, 1, 1, 1}, - { 1, 1.09f, 2.24f, 2.45f, 0, 0.10f, 1.33f, 1.55f}, + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, 0, 0, 0, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1.09f, 2.24f, 2.45f, 0, 0.10f, 1.33f, 1.55f}, {0.89f, 0.98f, 2.02f, 2.21f, 1, 1.08f, 2.11f, 2.30f}); - CheckObjFunction(obj, - { 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - { 0, 0, 0, 0, 1, 1, 1, 1}, + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, 0, 0, 0, 1, 1, 1, 1}, {}, // Empty weight. - { 1, 1.09f, 2.24f, 2.45f, 0, 0.10f, 1.33f, 1.55f}, + {1, 1.09f, 2.24f, 2.45f, 0, 0.10f, 1.33f, 1.55f}, {0.89f, 0.98f, 2.02f, 2.21f, 1, 1.08f, 2.11f, 2.30f}); ASSERT_EQ(obj->DefaultEvalMetric(), std::string{"tweedie-nloglik@1.1"}); } @@ -267,7 +238,7 @@ void TestTweedieRegressionBasic(const Context* ctx) { // test label validation EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0})) - << "Expected error when label < 0 for TweedieRegression"; + << "Expected error when label < 0 for TweedieRegression"; // test ProbToMargin CheckProbaToMargin(obj, 0.1f, -2.30f); @@ -289,12 +260,9 @@ void TestCoxRegressionGPair(const Context* ctx) { std::unique_ptr obj{ObjFunction::Create("survival:cox", ctx)}; obj->Configure(args); - CheckObjFunction(obj, - { 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, - { 0, -2, -2, 2, 3, 5, -10, 100}, - { 1, 1, 1, 1, 1, 1, 1, 1}, - { 0, 0, 0, -0.799f, -0.788f, -0.590f, 0.910f, 1.006f}, - { 0, 0, 0, 0.160f, 0.186f, 0.348f, 0.610f, 0.639f}); + CheckObjFunction(obj, {0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, {0, -2, -2, 2, 3, 5, -10, 100}, + {1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, -0.799f, -0.788f, -0.590f, 0.910f, 1.006f}, + {0, 0, 0, 0.160f, 0.186f, 0.348f, 0.610f, 0.639f}); } void TestAbsoluteError(const Context* ctx) { diff --git a/tests/cpp/objective/test_regression_obj.h b/tests/cpp/objective/test_regression_obj.h index 39a941ff20e3..3461a8bb164d 100644 --- a/tests/cpp/objective/test_regression_obj.h +++ b/tests/cpp/objective/test_regression_obj.h @@ -1,11 +1,14 @@ /** * Copyright 2020-2026, XGBoost Contributors */ -#ifndef XGBOOST_TEST_REGRESSION_OBJ_H_ -#define XGBOOST_TEST_REGRESSION_OBJ_H_ +#ifndef TESTS_CPP_OBJECTIVE_TEST_REGRESSION_OBJ_H_ +#define TESTS_CPP_OBJECTIVE_TEST_REGRESSION_OBJ_H_ #include // for Context +#include +#include + namespace xgboost { void TestLinearRegressionGPair(const Context* ctx); @@ -44,4 +47,4 @@ void TestPseudoHuber(const Context* ctx); } // namespace xgboost -#endif // XGBOOST_TEST_REGRESSION_OBJ_H_ +#endif // TESTS_CPP_OBJECTIVE_TEST_REGRESSION_OBJ_H_ diff --git a/tests/cpp/objective/test_regression_obj_cpu.cc b/tests/cpp/objective/test_regression_obj_cpu.cc index fa0424ea161e..8496d2d048ec 100644 --- a/tests/cpp/objective/test_regression_obj_cpu.cc +++ b/tests/cpp/objective/test_regression_obj_cpu.cc @@ -5,6 +5,9 @@ #include #include +#include +#include + #include "../../../src/objective/adaptive.h" #include "../helpers.h" #include "test_regression_obj.h" @@ -87,7 +90,7 @@ TEST(Objective, CPU_vs_CUDA) { info.labels.Reshape(kRows); auto& h_labels = info.labels.Data()->HostVector(); for (size_t i = 0; i < h_labels.size(); ++i) { - h_labels[i] = 1 / static_cast(i+1); + h_labels[i] = 1 / static_cast(i + 1); } { diff --git a/tests/cpp/objective_helpers.cc b/tests/cpp/objective_helpers.cc index b037b160fb9d..ea7014f00c49 100644 --- a/tests/cpp/objective_helpers.cc +++ b/tests/cpp/objective_helpers.cc @@ -3,6 +3,9 @@ */ #include "objective_helpers.h" +#include +#include + #include "../../src/common/linalg_op.h" // for begin, end #include "helpers.h" // for RandomDataGenerator diff --git a/tests/cpp/objective_helpers.h b/tests/cpp/objective_helpers.h index 6e41ecd8e185..4c34a43ba615 100644 --- a/tests/cpp/objective_helpers.h +++ b/tests/cpp/objective_helpers.h @@ -9,6 +9,7 @@ #include // for transform #include // for back_insert_iterator, back_inserter +#include // for shared_ptr #include // for string #include // for vector @@ -30,7 +31,7 @@ inline std::string ObjTestNameGenerator(const ::testing::TestParamInfo #include // for Span -#include // for array +#include // for min, transform +#include // for array +#include // for make_shared, shared_ptr +#include // for string +#include // for vector #include "../../../../src/common/type.h" // for EraseType #include "../../collective/test_worker.h" // for SocketTest diff --git a/tests/cpp/plugin/federated/test_federated_coll.cu b/tests/cpp/plugin/federated/test_federated_coll.cu index 67bf0ebc66e2..90aa859dbe31 100644 --- a/tests/cpp/plugin/federated/test_federated_coll.cu +++ b/tests/cpp/plugin/federated/test_federated_coll.cu @@ -5,6 +5,9 @@ #include #include // for Result +#include // for make_shared, shared_ptr +#include // for vector + #include "../../../../src/collective/allreduce.h" #include "../../../../src/common/cuda_rt_utils.h" // for AllVisibleGPUs #include "../../../../src/common/device_helpers.cuh" // for device_vector diff --git a/tests/cpp/plugin/federated/test_federated_comm.cc b/tests/cpp/plugin/federated/test_federated_comm.cc index a82e9c2c429e..d5243a477716 100644 --- a/tests/cpp/plugin/federated/test_federated_comm.cc +++ b/tests/cpp/plugin/federated/test_federated_comm.cc @@ -4,6 +4,7 @@ #include #include +#include #include // for string #include // for thread diff --git a/tests/cpp/plugin/federated/test_federated_data.cc b/tests/cpp/plugin/federated/test_federated_data.cc index c3c3fd1c0f81..47065018637b 100644 --- a/tests/cpp/plugin/federated/test_federated_data.cc +++ b/tests/cpp/plugin/federated/test_federated_data.cc @@ -4,6 +4,9 @@ #include #include +#include +#include + #include "../../../../src/collective/communicator-inl.h" #include "../../filesystem.h" // for test_federated_data #include "../../helpers.h" diff --git a/tests/cpp/plugin/federated/test_federated_learner.cc b/tests/cpp/plugin/federated/test_federated_learner.cc index b7b6a6e27c1b..a0792991f015 100644 --- a/tests/cpp/plugin/federated/test_federated_learner.cc +++ b/tests/cpp/plugin/federated/test_federated_learner.cc @@ -8,6 +8,10 @@ #include #include +#include +#include +#include + #include "../../../../src/collective/communicator-inl.h" #include "../../../../src/common/linalg_op.h" // for begin, end #include "../../helpers.h" diff --git a/tests/cpp/plugin/federated/test_worker.h b/tests/cpp/plugin/federated/test_worker.h index 5f53965da5dd..42e60deddac8 100644 --- a/tests/cpp/plugin/federated/test_worker.h +++ b/tests/cpp/plugin/federated/test_worker.h @@ -7,7 +7,9 @@ #include // for ms, seconds #include // for shared_ptr +#include // for string #include // for thread +#include // for vector #include "../../../../plugin/federated/federated_tracker.h" #include "../../../../src/collective/comm_group.h" @@ -36,7 +38,7 @@ void TestFederatedImpl(std::int32_t n_workers, WorkerFn&& fn) { auto fut = tracker.Run(); std::vector workers; - using namespace std::chrono_literals; + using namespace std::chrono_literals; // NOLINT(build/namespaces) auto rc = tracker.WaitUntilReady(); SafeColl(rc); std::int32_t port = tracker.Port(); diff --git a/tests/cpp/plugin/sycl_helpers.h b/tests/cpp/plugin/sycl_helpers.h index d28ee464ecf1..47af0b4483b7 100644 --- a/tests/cpp/plugin/sycl_helpers.h +++ b/tests/cpp/plugin/sycl_helpers.h @@ -3,27 +3,30 @@ */ #pragma once -#include "../helpers.h" -#include "../../plugin/sycl/device_manager.h" +#include + #include "../../plugin/sycl/data.h" +#include "../../plugin/sycl/device_manager.h" +#include "../helpers.h" namespace xgboost::sycl { -template +template void TransformOnDeviceData(DeviceOrd device, T* device_data, size_t n_data, Fn&& fn) { sycl::DeviceManager device_manager; ::sycl::queue* qu = device_manager.GetQueue(device); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(n_data), [=](::sycl::item<1> nid) { - const size_t i = nid.get_id(0); - device_data[i] = fn(device_data[i]); - }); - }).wait(); + cgh.parallel_for<>(::sycl::range<1>(n_data), [=](::sycl::item<1> nid) { + const size_t i = nid.get_id(0); + device_data[i] = fn(device_data[i]); + }); + }).wait(); } -template -void VerifyOnDeviceData(DeviceOrd device, const T* device_data, const T* host_data, size_t n_data, T eps = T()) { +template +void VerifyOnDeviceData(DeviceOrd device, const T* device_data, const T* host_data, size_t n_data, + T eps = T()) { sycl::DeviceManager device_manager; ::sycl::queue* qu = device_manager.GetQueue(device); @@ -34,7 +37,7 @@ void VerifyOnDeviceData(DeviceOrd device, const T* device_data, const T* host_da } } -template +template void VerifySyclVector(const USMVector& sycl_vector, const Container& host_vector, T eps = T()) { ASSERT_EQ(sycl_vector.Size(), host_vector.size()); @@ -45,9 +48,9 @@ void VerifySyclVector(const USMVector& sycl_vector, } } -template -void VerifySyclVector(const std::vector& sycl_vector, - const Container& host_vector, T eps = T()) { +template +void VerifySyclVector(const std::vector& sycl_vector, const Container& host_vector, + T eps = T()) { ASSERT_EQ(sycl_vector.size(), host_vector.size()); size_t size = sycl_vector.size(); diff --git a/tests/cpp/plugin/test_example_objective.cc b/tests/cpp/plugin/test_example_objective.cc index ccb83c781fc1..24c8c8962e60 100644 --- a/tests/cpp/plugin/test_example_objective.cc +++ b/tests/cpp/plugin/test_example_objective.cc @@ -1,6 +1,11 @@ +/** + * Copyright 2026, XGBoost contributors + */ #include #include + #include + #include "../helpers.h" namespace xgboost { diff --git a/tests/cpp/plugin/test_sycl_ghist_builder.cc b/tests/cpp/plugin/test_sycl_ghist_builder.cc index 95e38a61adf5..2662ecabbcbb 100644 --- a/tests/cpp/plugin/test_sycl_ghist_builder.cc +++ b/tests/cpp/plugin/test_sycl_ghist_builder.cc @@ -6,14 +6,16 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" #pragma GCC diagnostic ignored "-W#pragma-messages" -#include "../../../src/data/gradient_index.h" // for GHistIndexMatrix +#include "../../../src/data/gradient_index.h" // for GHistIndexMatrix #pragma GCC diagnostic pop +#include + #include "../../../plugin/sycl/common/hist_util.h" #include "../../../plugin/sycl/device_manager.h" #include "../../../plugin/sycl/tree/hist_dispatcher.h" -#include "sycl_helpers.h" #include "../helpers.h" +#include "sycl_helpers.h" namespace xgboost::sycl::common { @@ -42,23 +44,27 @@ void GHistBuilderTest(float sparsity, bool force_atomic_use) { size_t* p_row_indices = row_indices.Data(); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(num_rows), - [p_row_indices](::sycl::item<1> pid) { - const size_t idx = pid.get_id(0); - p_row_indices[idx] = idx; - }); - }).wait_and_throw(); + cgh.parallel_for<>(::sycl::range<1>(num_rows), [p_row_indices](::sycl::item<1> pid) { + const size_t idx = pid.get_id(0); + p_row_indices[idx] = idx; + }); + }).wait_and_throw(); row_set_collection.Init(); auto builder = GHistBuilder(qu, n_bins); - HostDeviceVector gpair({ - {0.1f, 0.2f}, {0.3f, 0.4f}, {0.5f, 0.6f}, {0.7f, 0.8f}, - {0.9f, 0.1f}, {0.2f, 0.3f}, {0.4f, 0.5f}, {0.6f, 0.7f}}, - ctx.Device()); + HostDeviceVector gpair({{0.1f, 0.2f}, + {0.3f, 0.4f}, + {0.5f, 0.6f}, + {0.7f, 0.8f}, + {0.9f, 0.1f}, + {0.2f, 0.3f}, + {0.4f, 0.5f}, + {0.6f, 0.7f}}, + ctx.Device()); CHECK_EQ(gpair.Size(), num_rows); - std::vector hist_host(2*n_bins); + std::vector hist_host(2 * n_bins); GHistRow hist(qu, 2 * n_bins); ::sycl::event event; @@ -69,21 +75,20 @@ void GHistBuilderTest(float sparsity, bool force_atomic_use) { InitHist(qu, &hist_buffer, hist_buffer.Size(), &event); DeviceProperties device_prop(qu->get_device()); - event = builder.BuildHist(gpair, row_set_collection[0], gmat_sycl, &hist, - sparsity < eps , &hist_buffer, device_prop, event, force_atomic_use); - qu->memcpy(hist_host.data(), hist.Data(), - 2 * n_bins * sizeof(GradientSumT), event); + event = builder.BuildHist(gpair, row_set_collection[0], gmat_sycl, &hist, sparsity < eps, + &hist_buffer, device_prop, event, force_atomic_use); + qu->memcpy(hist_host.data(), hist.Data(), 2 * n_bins * sizeof(GradientSumT), event); qu->wait_and_throw(); // Build hist on host to compare - std::vector hist_desired(2*n_bins); + std::vector hist_desired(2 * n_bins); for (size_t rid = 0; rid < num_rows; ++rid) { const size_t ibegin = gmat.row_ptr[rid]; const size_t iend = gmat.row_ptr[rid + 1]; for (size_t i = ibegin; i < iend; ++i) { const size_t bin_idx = gmat.index[i]; - hist_desired[2*bin_idx] += gpair.HostVector()[rid].GetGrad(); - hist_desired[2*bin_idx+1] += gpair.HostVector()[rid].GetHess(); + hist_desired[2 * bin_idx] += gpair.HostVector()[rid].GetGrad(); + hist_desired[2 * bin_idx + 1] += gpair.HostVector()[rid].GetHess(); } } @@ -104,19 +109,16 @@ void GHistSubtractionTest() { ::sycl::event event; std::vector hist1_host = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}; GHistType hist1(qu, 2 * n_bins); - event = qu->memcpy(hist1.Data(), hist1_host.data(), - 2 * n_bins * sizeof(GradientSumT), event); + event = qu->memcpy(hist1.Data(), hist1_host.data(), 2 * n_bins * sizeof(GradientSumT), event); std::vector hist2_host = {0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1}; GHistType hist2(qu, 2 * n_bins); - event = qu->memcpy(hist2.Data(), hist2_host.data(), - 2 * n_bins * sizeof(GradientSumT), event); + event = qu->memcpy(hist2.Data(), hist2_host.data(), 2 * n_bins * sizeof(GradientSumT), event); std::vector hist3_host(2 * n_bins); GHistType hist3(qu, 2 * n_bins); event = SubtractionHist(qu, &hist3, hist1, hist2, n_bins, event); - qu->memcpy(hist3_host.data(), hist3.Data(), - 2 * n_bins * sizeof(GradientSumT), event); + qu->memcpy(hist3_host.data(), hist3.Data(), 2 * n_bins * sizeof(GradientSumT), event); qu->wait_and_throw(); std::vector hist3_desired(2 * n_bins); diff --git a/tests/cpp/plugin/test_sycl_gradient_index.cc b/tests/cpp/plugin/test_sycl_gradient_index.cc index cfa625080254..9f0a9ca9ed90 100644 --- a/tests/cpp/plugin/test_sycl_gradient_index.cc +++ b/tests/cpp/plugin/test_sycl_gradient_index.cc @@ -5,13 +5,15 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" #pragma GCC diagnostic ignored "-W#pragma-messages" -#include "../../../src/data/gradient_index.h" // for GHistIndexMatrix +#include "../../../src/data/gradient_index.h" // for GHistIndexMatrix #pragma GCC diagnostic pop +#include + #include "../../../plugin/sycl/data/gradient_index.h" #include "../../../plugin/sycl/device_manager.h" -#include "sycl_helpers.h" #include "../helpers.h" +#include "sycl_helpers.h" namespace xgboost::sycl::data { @@ -37,9 +39,7 @@ TEST(SyclGradientIndex, Init) { ASSERT_EQ(gmat_sycl.nfeatures, n_columns); } - { - VerifySyclVector(gmat_sycl.hit_count.ConstHostVector(), gmat.hit_count); - } + { VerifySyclVector(gmat_sycl.hit_count.ConstHostVector(), gmat.hit_count); } { std::vector feature_count_sycl(n_columns, 0); diff --git a/tests/cpp/plugin/test_sycl_hist_updater.cc b/tests/cpp/plugin/test_sycl_hist_updater.cc index 69c5047d045c..9052d7ed7cb8 100644 --- a/tests/cpp/plugin/test_sycl_hist_updater.cc +++ b/tests/cpp/plugin/test_sycl_hist_updater.cc @@ -3,13 +3,14 @@ */ #include +#include #include +#include +#include -#include "../../../plugin/sycl/tree/hist_updater.h" #include "../../../plugin/sycl/device_manager.h" - +#include "../../../plugin/sycl/tree/hist_updater.h" #include "../../../src/tree/common_row_partitioner.h" - #include "../helpers.h" namespace xgboost::sycl::tree { @@ -18,12 +19,9 @@ namespace xgboost::sycl::tree { template class TestHistUpdater : public HistUpdater { public: - TestHistUpdater(const Context* ctx, - ::sycl::queue* qu, - const xgboost::tree::TrainParam& param, - FeatureInteractionConstraintHost int_constraints_, - DMatrix const* fmat) : HistUpdater(ctx, qu, param, - int_constraints_, fmat) {} + TestHistUpdater(const Context* ctx, ::sycl::queue* qu, const xgboost::tree::TrainParam& param, + FeatureInteractionConstraintHost int_constraints_, DMatrix const* fmat) + : HistUpdater(ctx, qu, param, int_constraints_, fmat) {} void TestInitSampling(const HostDeviceVector& gpair, USMVector* row_indices) { @@ -31,61 +29,51 @@ class TestHistUpdater : public HistUpdater { } auto* TestInitData(const common::GHistIndexMatrix& gmat, - const HostDeviceVector& gpair, - const DMatrix& fmat, + const HostDeviceVector& gpair, const DMatrix& fmat, const RegTree& tree) { HistUpdater::InitData(gmat, gpair, fmat, tree); return &(HistUpdater::row_set_collection_); } - const auto* TestBuildHistogramsLossGuide(ExpandEntry entry, - const common::GHistIndexMatrix &gmat, - RegTree *p_tree, - const HostDeviceVector& gpair) { + const auto* TestBuildHistogramsLossGuide(ExpandEntry entry, const common::GHistIndexMatrix& gmat, + RegTree* p_tree, + const HostDeviceVector& gpair) { HistUpdater::BuildHistogramsLossGuide(entry, gmat, p_tree, gpair); return &(HistUpdater::hist_); } - auto TestInitNewNode(int nid, - const common::GHistIndexMatrix& gmat, - const HostDeviceVector& gpair, - const RegTree& tree) { + auto TestInitNewNode(int nid, const common::GHistIndexMatrix& gmat, + const HostDeviceVector& gpair, const RegTree& tree) { HistUpdater::InitNewNode(nid, gmat, gpair, tree); return HistUpdater::snode_host_[nid]; } auto TestEvaluateSplits(const std::vector& nodes_set, - const common::GHistIndexMatrix& gmat, - const RegTree& tree) { + const common::GHistIndexMatrix& gmat, const RegTree& tree) { HistUpdater::EvaluateSplits(nodes_set, gmat, tree); return HistUpdater::snode_host_; } - void TestApplySplit(const std::vector nodes, - const common::GHistIndexMatrix& gmat, + void TestApplySplit(const std::vector nodes, const common::GHistIndexMatrix& gmat, RegTree* p_tree) { HistUpdater::ApplySplit(nodes, gmat, p_tree); } - auto TestExpandWithLossGuide(const common::GHistIndexMatrix& gmat, - DMatrix *p_fmat, - RegTree* p_tree, - const HostDeviceVector& gpair) { + auto TestExpandWithLossGuide(const common::GHistIndexMatrix& gmat, DMatrix* p_fmat, + RegTree* p_tree, const HostDeviceVector& gpair) { HistUpdater::ExpandWithLossGuide(gmat, p_tree, gpair); } - auto TestExpandWithDepthWise(const common::GHistIndexMatrix& gmat, - DMatrix *p_fmat, - RegTree* p_tree, - const HostDeviceVector& gpair) { + auto TestExpandWithDepthWise(const common::GHistIndexMatrix& gmat, DMatrix* p_fmat, + RegTree* p_tree, const HostDeviceVector& gpair) { HistUpdater::ExpandWithDepthWise(gmat, p_tree, gpair); } }; -void GenerateRandomGPairs(::sycl::queue* qu, GradientPair* gpair_ptr, size_t num_rows, bool has_neg_hess) { +void GenerateRandomGPairs(::sycl::queue* qu, GradientPair* gpair_ptr, size_t num_rows, + bool has_neg_hess) { qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(num_rows)), - [=](::sycl::item<1> pid) { + cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(num_rows)), [=](::sycl::item<1> pid) { uint64_t i = pid.get_linear_id(); constexpr uint32_t seed = 777; @@ -121,7 +109,7 @@ void TestHistUpdaterSampling(const xgboost::tree::TrainParam& param) { GenerateRandomGPairs(qu, gpair.DevicePointer(), num_rows, true); updater.TestInitSampling(gpair, &row_indices_0); - + size_t n_samples = row_indices_0.Size(); // Half of gpairs have neg hess ASSERT_LT(n_samples, num_rows * 0.5 * param.subsample * 1.2); @@ -179,7 +167,8 @@ void TestHistUpdaterInitData(const xgboost::tree::TrainParam& param, bool has_ne auto& row_indices = row_set_collection->Data(); std::vector row_indices_host(row_indices.Size()); - qu->memcpy(row_indices_host.data(), row_indices.DataConst(), row_indices.Size()*sizeof(size_t)).wait(); + qu->memcpy(row_indices_host.data(), row_indices.DataConst(), row_indices.Size() * sizeof(size_t)) + .wait(); if (!has_neg_hess) { for (size_t i = 0; i < num_rows; ++i) { @@ -200,7 +189,8 @@ void TestHistUpdaterInitData(const xgboost::tree::TrainParam& param, bool has_ne } template -void TestHistUpdaterBuildHistogramsLossGuide(const xgboost::tree::TrainParam& param, float sparsity) { +void TestHistUpdaterBuildHistogramsLossGuide(const xgboost::tree::TrainParam& param, + float sparsity) { const size_t num_rows = 1u << 8; const size_t num_columns = 1; const size_t n_bins = 32; @@ -247,14 +237,19 @@ void TestHistUpdaterBuildHistogramsLossGuide(const xgboost::tree::TrainParam& pa std::vector> hist0_host(n_bins); std::vector> hist1_host(n_bins); std::vector> hist2_host(n_bins); - qu->memcpy(hist0_host.data(), (*hist)[0].DataConst(), sizeof(xgboost::detail::GradientPairInternal) * n_bins); - qu->memcpy(hist1_host.data(), (*hist)[1].DataConst(), sizeof(xgboost::detail::GradientPairInternal) * n_bins); - qu->memcpy(hist2_host.data(), (*hist)[2].DataConst(), sizeof(xgboost::detail::GradientPairInternal) * n_bins); + qu->memcpy(hist0_host.data(), (*hist)[0].DataConst(), + sizeof(xgboost::detail::GradientPairInternal) * n_bins); + qu->memcpy(hist1_host.data(), (*hist)[1].DataConst(), + sizeof(xgboost::detail::GradientPairInternal) * n_bins); + qu->memcpy(hist2_host.data(), (*hist)[2].DataConst(), + sizeof(xgboost::detail::GradientPairInternal) * n_bins); qu->wait(); for (size_t idx_bin = 0; idx_bin < n_bins; ++idx_bin) { - EXPECT_NEAR(hist0_host[idx_bin].GetGrad(), hist1_host[idx_bin].GetGrad() + hist2_host[idx_bin].GetGrad(), 1e-6); - EXPECT_NEAR(hist0_host[idx_bin].GetHess(), hist1_host[idx_bin].GetHess() + hist2_host[idx_bin].GetHess(), 1e-6); + EXPECT_NEAR(hist0_host[idx_bin].GetGrad(), + hist1_host[idx_bin].GetGrad() + hist2_host[idx_bin].GetGrad(), 1e-6); + EXPECT_NEAR(hist0_host[idx_bin].GetHess(), + hist1_host[idx_bin].GetHess() + hist2_host[idx_bin].GetHess(), 1e-6); } } @@ -299,15 +294,15 @@ void TestHistUpdaterInitNewNode(const xgboost::tree::TrainParam& param, float sp { ::sycl::buffer> buff(&grad_stat, 1); qu->submit([&](::sycl::handler& cgh) { - auto buff_acc = buff.template get_access<::sycl::access::mode::read_write>(cgh); - cgh.single_task<>([=]() { - for (size_t i = 0; i < num_rows; ++i) { - size_t row_idx = row_idxs_ptr[i]; - buff_acc[0] += GradStats(gpair_ptr[row_idx].GetGrad(), - gpair_ptr[row_idx].GetHess()); - } - }); - }).wait_and_throw(); + auto buff_acc = buff.template get_access<::sycl::access::mode::read_write>(cgh); + cgh.single_task<>([=]() { + for (size_t i = 0; i < num_rows; ++i) { + size_t row_idx = row_idxs_ptr[i]; + buff_acc[0] += + GradStats(gpair_ptr[row_idx].GetGrad(), gpair_ptr[row_idx].GetHess()); + } + }); + }).wait_and_throw(); } EXPECT_NEAR(snode.stats.GetGrad(), grad_stat.GetGrad(), 1e-6 * grad_stat.GetGrad()); @@ -367,29 +362,31 @@ void TestHistUpdaterEvaluateSplits(const xgboost::tree::TrainParam& param) { { ::sycl::buffer best_loss_chg_buff(best_loss_chg_des.data(), 1); qu->submit([&](::sycl::handler& cgh) { - auto best_loss_chg_acc = best_loss_chg_buff.template get_access<::sycl::access::mode::read_write>(cgh); - cgh.single_task<>([=]() { - for (size_t i = 1; i < size; ++i) { - GradStats left(0, 0); - GradStats right = stats - left; - for (size_t j = cut_ptr[i-1]; j < cut_ptr[i]; ++j) { - auto loss_change = evaluator.CalcSplitGain(0, i - 1, left, right) - root_gain; - if (loss_change > best_loss_chg_acc[0]) { - best_loss_chg_acc[0] = loss_change; + auto best_loss_chg_acc = + best_loss_chg_buff.template get_access<::sycl::access::mode::read_write>(cgh); + cgh.single_task<>([=]() { + for (size_t i = 1; i < size; ++i) { + GradStats left(0, 0); + GradStats right = stats - left; + for (size_t j = cut_ptr[i - 1]; j < cut_ptr[i]; ++j) { + auto loss_change = evaluator.CalcSplitGain(0, i - 1, left, right) - root_gain; + if (loss_change > best_loss_chg_acc[0]) { + best_loss_chg_acc[0] = loss_change; + } + left.Add(hist_ptr[j].GetGrad(), hist_ptr[j].GetHess()); + right = stats - left; } - left.Add(hist_ptr[j].GetGrad(), hist_ptr[j].GetHess()); - right = stats - left; } - } - }); - }).wait(); + }); + }).wait(); } ASSERT_NEAR(best_loss_chg_des[0], best_loss_chg, 1e-4); } template -void TestHistUpdaterApplySplit(const xgboost::tree::TrainParam& param, float sparsity, int max_bins) { +void TestHistUpdaterApplySplit(const xgboost::tree::TrainParam& param, float sparsity, + int max_bins) { const size_t num_rows = 1024; const size_t num_columns = 2; @@ -420,14 +417,17 @@ void TestHistUpdaterApplySplit(const xgboost::tree::TrainParam& param, float spa // Copy indexes to host std::vector row_indices_host(num_rows); - qu->memcpy(row_indices_host.data(), row_set_collection->Data().Data(), sizeof(size_t)*num_rows).wait(); + qu->memcpy(row_indices_host.data(), row_set_collection->Data().Data(), sizeof(size_t) * num_rows) + .wait(); // Reference Implementation std::vector row_indices_desired_host(num_rows); size_t n_left, n_right; { - TestHistUpdater updater4verification(&ctx, qu, param, int_constraints, p_fmat.get()); - auto* row_set_collection4verification = updater4verification.TestInitData(gmat, gpair, *p_fmat, tree); + TestHistUpdater updater4verification(&ctx, qu, param, int_constraints, + p_fmat.get()); + auto* row_set_collection4verification = + updater4verification.TestInitData(gmat, gpair, *p_fmat, tree); size_t n_nodes = nodes.size(); std::vector split_conditions(n_nodes); @@ -441,8 +441,8 @@ void TestHistUpdaterApplySplit(const xgboost::tree::TrainParam& param, float spa }); ::sycl::event event; - partition_builder.Partition(gmat, nodes, (*row_set_collection4verification), - split_conditions, &tree, &event); + partition_builder.Partition(gmat, nodes, (*row_set_collection4verification), split_conditions, + &tree, &event); qu->wait_and_throw(); for (size_t node_in_set = 0; node_in_set < n_nodes; node_in_set++) { @@ -456,10 +456,12 @@ void TestHistUpdaterApplySplit(const xgboost::tree::TrainParam& param, float spa n_left = partition_builder.GetNLeftElems(0); n_right = partition_builder.GetNRightElems(0); - row_set_collection4verification->AddSplit(nid, tree[nid].LeftChild(), - tree[nid].RightChild(), n_left, n_right); + row_set_collection4verification->AddSplit(nid, tree[nid].LeftChild(), tree[nid].RightChild(), + n_left, n_right); - qu->memcpy(row_indices_desired_host.data(), row_set_collection4verification->Data().Data(), sizeof(size_t)*num_rows).wait(); + qu->memcpy(row_indices_desired_host.data(), row_set_collection4verification->Data().Data(), + sizeof(size_t) * num_rows) + .wait(); } std::sort(row_indices_desired_host.begin(), row_indices_desired_host.begin() + n_left); @@ -503,11 +505,12 @@ void TestHistUpdaterExpandWithLossGuide(const xgboost::tree::TrainParam& param) const auto& nodes = tree.GetNodes(DeviceOrd::CPU()); std::vector ans(data.size()); for (size_t data_idx = 0; data_idx < data.size(); ++data_idx) { - size_t node_idx = 0; - while (!nodes[node_idx].IsLeaf()) { - node_idx = data[data_idx] < nodes[node_idx].SplitCond() ? nodes[node_idx].LeftChild() : nodes[node_idx].RightChild(); - } - ans[data_idx] = nodes[node_idx].LeafValue(); + size_t node_idx = 0; + while (!nodes[node_idx].IsLeaf()) { + node_idx = data[data_idx] < nodes[node_idx].SplitCond() ? nodes[node_idx].LeftChild() + : nodes[node_idx].RightChild(); + } + ans[data_idx] = nodes[node_idx].LeafValue(); } ASSERT_NEAR(ans[0], -0.15, 1e-6); @@ -515,7 +518,6 @@ void TestHistUpdaterExpandWithLossGuide(const xgboost::tree::TrainParam& param) ASSERT_NEAR(ans[2], -0.15, 1e-6); } - template void TestHistUpdaterExpandWithDepthWise(const xgboost::tree::TrainParam& param) { const size_t num_rows = 3; @@ -547,11 +549,12 @@ void TestHistUpdaterExpandWithDepthWise(const xgboost::tree::TrainParam& param) const auto& nodes = tree.GetNodes(DeviceOrd::CPU()); std::vector ans(data.size()); for (size_t data_idx = 0; data_idx < data.size(); ++data_idx) { - size_t node_idx = 0; - while (!nodes[node_idx].IsLeaf()) { - node_idx = data[data_idx] < nodes[node_idx].SplitCond() ? nodes[node_idx].LeftChild() : nodes[node_idx].RightChild(); - } - ans[data_idx] = nodes[node_idx].LeafValue(); + size_t node_idx = 0; + while (!nodes[node_idx].IsLeaf()) { + node_idx = data[data_idx] < nodes[node_idx].SplitCond() ? nodes[node_idx].LeftChild() + : nodes[node_idx].RightChild(); + } + ans[data_idx] = nodes[node_idx].LeafValue(); } ASSERT_NEAR(ans[0], -0.15, 1e-6); @@ -619,17 +622,16 @@ TEST(SyclHistUpdater, ApplySplitDence) { param.UpdateAllowUnknown(Args{{"max_depth", "3"}}); TestHistUpdaterApplySplit(param, 0.0, 256); - TestHistUpdaterApplySplit(param, 0.0, 256+1); + TestHistUpdaterApplySplit(param, 0.0, 256 + 1); TestHistUpdaterApplySplit(param, 0.0, (1u << 16) + 1); TestHistUpdaterApplySplit(param, 0.0, 256); - TestHistUpdaterApplySplit(param, 0.0, 256+1); + TestHistUpdaterApplySplit(param, 0.0, 256 + 1); TestHistUpdaterApplySplit(param, 0.0, (1u << 16) + 1); } TEST(SyclHistUpdater, ExpandWithLossGuide) { xgboost::tree::TrainParam param; - param.UpdateAllowUnknown(Args{{"max_depth", "2"}, - {"grow_policy", "lossguide"}}); + param.UpdateAllowUnknown(Args{{"max_depth", "2"}, {"grow_policy", "lossguide"}}); TestHistUpdaterExpandWithLossGuide(param); TestHistUpdaterExpandWithLossGuide(param); diff --git a/tests/cpp/plugin/test_sycl_host_device_vector.cc b/tests/cpp/plugin/test_sycl_host_device_vector.cc index 7044ae38efc8..b44324bb36fa 100644 --- a/tests/cpp/plugin/test_sycl_host_device_vector.cc +++ b/tests/cpp/plugin/test_sycl_host_device_vector.cc @@ -2,7 +2,10 @@ * Copyright 2018-2024, XGBoost contributors */ #include + #include +#include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-W#pragma-messages" #pragma GCC diagnostic ignored "-Wtautological-constant-compare" @@ -14,7 +17,7 @@ namespace xgboost::common { namespace { -void InitHostDeviceVector(size_t n, DeviceOrd device, HostDeviceVector *v) { +void InitHostDeviceVector(size_t n, DeviceOrd device, HostDeviceVector* v) { // create the vector v->SetDevice(device); v->Resize(n); @@ -39,16 +42,14 @@ void InitHostDeviceVector(size_t n, DeviceOrd device, HostDeviceVector *v) std::iota(data_h.begin(), data_h.end(), 0); } -void PlusOne(HostDeviceVector *v) { +void PlusOne(HostDeviceVector* v) { auto device = v->Device(); - sycl::TransformOnDeviceData(v->Device(), v->DevicePointer(), v->Size(), [=](size_t a){ return a + 1; }); + sycl::TransformOnDeviceData(v->Device(), v->DevicePointer(), v->Size(), + [=](size_t a) { return a + 1; }); ASSERT_TRUE(v->DeviceCanWrite()); } -void CheckDevice(HostDeviceVector* v, - size_t size, - unsigned int first, - GPUAccess access) { +void CheckDevice(HostDeviceVector* v, size_t size, unsigned int first, GPUAccess access) { ASSERT_EQ(v->Size(), size); std::vector desired_data(size); @@ -67,9 +68,9 @@ void CheckDevice(HostDeviceVector* v, ASSERT_FALSE(v->HostCanWrite()); } -void CheckHost(HostDeviceVector *v, GPUAccess access) { - const std::vector& data_h = access == GPUAccess::kNone ? - v->HostVector() : v->ConstHostVector(); +void CheckHost(HostDeviceVector* v, GPUAccess access) { + const std::vector& data_h = + access == GPUAccess::kNone ? v->HostVector() : v->ConstHostVector(); for (size_t i = 0; i < v->Size(); ++i) { ASSERT_EQ(data_h.at(i), i + 1); } @@ -154,22 +155,23 @@ TEST(SyclHostDeviceVector, Extend) { v0.Extend(v1); { - std::vector desired_data(n0+n1, val); - sycl::VerifyOnDeviceData(v0.Device(), v0.ConstDevicePointer(), desired_data.data(), n0+n1); + std::vector desired_data(n0 + n1, val); + sycl::VerifyOnDeviceData(v0.Device(), v0.ConstDevicePointer(), desired_data.data(), n0 + n1); } v1.Extend(v0); { - std::vector desired_data(n0+2*n1, val); - sycl::VerifyOnDeviceData(v1.Device(), v1.ConstDevicePointer(), desired_data.data(), n0+2*n1); + std::vector desired_data(n0 + 2 * n1, val); + sycl::VerifyOnDeviceData(v1.Device(), v1.ConstDevicePointer(), desired_data.data(), + n0 + 2 * n1); } } TEST(SyclHostDeviceVector, SetDevice) { - std::vector h_vec (2345); + std::vector h_vec(2345); for (size_t i = 0; i < h_vec.size(); ++i) { h_vec[i] = i; } - HostDeviceVector vec (h_vec); + HostDeviceVector vec(h_vec); auto device = DeviceOrd::SyclDefault(); vec.SetDevice(device); @@ -185,7 +187,7 @@ TEST(SyclHostDeviceVector, SetDevice) { } TEST(SyclHostDeviceVector, Span) { - HostDeviceVector vec {1.0f, 2.0f, 3.0f, 4.0f}; + HostDeviceVector vec{1.0f, 2.0f, 3.0f, 4.0f}; vec.SetDevice(DeviceOrd::SyclDefault()); auto span = vec.DeviceSpan(); ASSERT_EQ(vec.Size(), span.size()); @@ -205,8 +207,8 @@ TEST(SyclHostDeviceVector, Span) { } TEST(SyclHostDeviceVector, Empty) { - HostDeviceVector vec {1.0f, 2.0f, 3.0f, 4.0f}; - HostDeviceVector another { std::move(vec) }; + HostDeviceVector vec{1.0f, 2.0f, 3.0f, 4.0f}; + HostDeviceVector another{std::move(vec)}; ASSERT_FALSE(another.Empty()); ASSERT_TRUE(vec.Empty()); } @@ -247,5 +249,5 @@ TEST(SyclHostDeviceVector, Resize) { check(vec); } } -} +} // namespace } // namespace xgboost::common diff --git a/tests/cpp/plugin/test_sycl_lambdarank_obj.cc b/tests/cpp/plugin/test_sycl_lambdarank_obj.cc index 2129d8b3e4c3..2814b937eb20 100644 --- a/tests/cpp/plugin/test_sycl_lambdarank_obj.cc +++ b/tests/cpp/plugin/test_sycl_lambdarank_obj.cc @@ -42,4 +42,3 @@ TEST(SyclObjective, LambdaRankMAPGPair) { } } // namespace xgboost::obj - diff --git a/tests/cpp/plugin/test_sycl_linalg.cc b/tests/cpp/plugin/test_sycl_linalg.cc index 2827aa34fbb3..e47b981ec120 100644 --- a/tests/cpp/plugin/test_sycl_linalg.cc +++ b/tests/cpp/plugin/test_sycl_linalg.cc @@ -2,11 +2,12 @@ * Copyright 2017-2025 XGBoost contributors */ #include + #include #include -#include "../../src/common/linalg_op.h" #include "../../../src/common/optional_weight.h" // for MakeOptionalWeights +#include "../../src/common/linalg_op.h" #include "sycl_helpers.h" namespace xgboost::sycl::linalg { @@ -22,7 +23,7 @@ TEST(SyclLinalg, SmallHistogram) { values.SetDevice(ctx.Device()); float* values_host_ptr = values.HostPointer(); for (std::size_t i = 0; i < n_bins; ++i) { - std::fill(values_host_ptr + i * cnt, values_host_ptr + (i + 1) * cnt, i); + std::fill(values_host_ptr + i * cnt, values_host_ptr + (i + 1) * cnt, i); } std::mt19937 rng; @@ -30,18 +31,19 @@ TEST(SyclLinalg, SmallHistogram) { std::shuffle(values_host_ptr, values_host_ptr + cnt * n_bins, rng); float* values_device_ptr = values.DevicePointer(); - xgboost::linalg::MatrixView indices = - xgboost::linalg::MakeTensorView(&ctx, xgboost::common::Span(values_device_ptr, cnt * n_bins), - cnt * n_bins, 1); + xgboost::linalg::MatrixView indices = xgboost::linalg::MakeTensorView( + &ctx, xgboost::common::Span(values_device_ptr, cnt * n_bins), cnt * n_bins, 1); HostDeviceVector bins(n_bins, 0); bins.SetDevice(ctx.Device()); HostDeviceVector weights; - xgboost::linalg::SmallHistogram(&ctx, indices, xgboost::common::MakeOptionalWeights(ctx.Device(), weights), - xgboost::linalg::MakeTensorView(&ctx, xgboost::common::Span(bins.DevicePointer(), n_bins), n_bins)); + xgboost::linalg::SmallHistogram( + &ctx, indices, xgboost::common::MakeOptionalWeights(ctx.Device(), weights), + xgboost::linalg::MakeTensorView(&ctx, xgboost::common::Span(bins.DevicePointer(), n_bins), + n_bins)); for (std::size_t i = 0; i < n_bins; ++i) { ASSERT_EQ(bins.HostVector()[i], cnt); } } -} // namespace xgboost::linalg \ No newline at end of file +} // namespace xgboost::sycl::linalg diff --git a/tests/cpp/plugin/test_sycl_partition_builder.cc b/tests/cpp/plugin/test_sycl_partition_builder.cc index 584b5c26fb72..69a5bd371e4c 100644 --- a/tests/cpp/plugin/test_sycl_partition_builder.cc +++ b/tests/cpp/plugin/test_sycl_partition_builder.cc @@ -33,12 +33,11 @@ void TestPartitioning(float sparsity, int max_bins) { size_t* p_row_indices = row_indices.Data(); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(num_rows), - [p_row_indices](::sycl::item<1> pid) { - const size_t idx = pid.get_id(0); - p_row_indices[idx] = idx; - }); - }).wait_and_throw(); + cgh.parallel_for<>(::sycl::range<1>(num_rows), [p_row_indices](::sycl::item<1> pid) { + const size_t idx = pid.get_id(0); + p_row_indices[idx] = idx; + }); + }).wait_and_throw(); row_set_collection.Init(); RegTree tree; @@ -46,17 +45,14 @@ void TestPartitioning(float sparsity, int max_bins) { const size_t n_nodes = row_set_collection.Size(); PartitionBuilder partition_builder; - partition_builder.Init(qu, n_nodes, [&](size_t nid) { - return row_set_collection[nid].Size(); - }); + partition_builder.Init(qu, n_nodes, [&](size_t nid) { return row_set_collection[nid].Size(); }); std::vector nodes; nodes.emplace_back(tree::ExpandEntry(0, tree.GetDepth(0))); ::sycl::event event; std::vector split_conditions = {2}; - partition_builder.Partition(gmat, nodes, row_set_collection, - split_conditions, &tree, &event); + partition_builder.Partition(gmat, nodes, row_set_collection, split_conditions, &tree, &event); qu->wait_and_throw(); size_t* data_result = const_cast(row_set_collection[0].begin); @@ -67,7 +63,7 @@ void TestPartitioning(float sparsity, int max_bins) { std::vector ridx_left(num_rows, 0); std::vector ridx_right(num_rows, 0); - for (auto &batch : p_fmat->GetBatches()) { + for (auto& batch : p_fmat->GetBatches()) { const auto& data_vec = batch.data.HostVector(); const auto& offset_vec = batch.offset.HostVector(); @@ -92,14 +88,14 @@ void TestPartitioning(float sparsity, int max_bins) { begin = end; } } - auto n_left = std::accumulate(ridx_left.begin(), ridx_left.end(), 0); + auto n_left = std::accumulate(ridx_left.begin(), ridx_left.end(), 0); auto n_right = std::accumulate(ridx_right.begin(), ridx_right.end(), 0); std::vector row_indices_host(num_rows); qu->memcpy(row_indices_host.data(), row_indices.Data(), num_rows * sizeof(size_t)); qu->wait_and_throw(); - ASSERT_EQ(n_left, partition_builder.GetNLeftElems(0)); + ASSERT_EQ(n_left, partition_builder.GetNLeftElems(0)); for (size_t i = 0; i < n_left; ++i) { auto idx = row_indices_host[i]; ASSERT_EQ(ridx_left[idx], 1); @@ -115,28 +111,26 @@ void TestPartitioning(float sparsity, int max_bins) { TEST(SyclPartitionBuilder, BasicTest) { constexpr size_t kNodes = 5; // Number of rows for each node - std::vector rows = { 5, 5, 10, 1, 2 }; + std::vector rows = {5, 5, 10, 1, 2}; DeviceManager device_manager; auto qu = device_manager.GetQueue(DeviceOrd::SyclDefault()); PartitionBuilder builder; - builder.Init(qu, kNodes, [&](size_t i) { - return rows[i]; - }); + builder.Init(qu, kNodes, [&](size_t i) { return rows[i]; }); // We test here only the basics, thus syntetic partition builder is adopted // Number of rows to go left for each node. - std::vector rows_for_left_node = { 2, 0, 7, 1, 2 }; + std::vector rows_for_left_node = {2, 0, 7, 1, 2}; size_t first_row_id = 0; - for(size_t nid = 0; nid < kNodes; ++nid) { + for (size_t nid = 0; nid < kNodes; ++nid) { size_t n_rows_nodes = rows[nid]; auto rid_buff = builder.GetData(nid); size_t rid_buff_size = rid_buff.size(); auto* rid_buff_ptr = rid_buff.data(); - size_t n_left = rows_for_left_node[nid]; + size_t n_left = rows_for_left_node[nid]; size_t n_right = rows[nid] - n_left; qu->submit([&](::sycl::handler& cgh) { @@ -165,42 +159,34 @@ TEST(SyclPartitionBuilder, BasicTest) { ::sycl::event event; std::vector v(*std::max_element(rows.begin(), rows.end())); size_t row_id = 0; - for(size_t nid = 0; nid < kNodes; ++nid) { + for (size_t nid = 0; nid < kNodes; ++nid) { builder.MergeToArray(nid, v.data(), &event); qu->wait(); // Check that row_id for left side are correct - for(size_t j = 0; j < rows_for_left_node[nid]; ++j) { - ASSERT_EQ(v[j], row_id++); + for (size_t j = 0; j < rows_for_left_node[nid]; ++j) { + ASSERT_EQ(v[j], row_id++); } // Check that row_id for right side are correct - for(size_t j = 0; j < rows[nid] - rows_for_left_node[nid]; ++j) { + for (size_t j = 0; j < rows[nid] - rows_for_left_node[nid]; ++j) { ASSERT_EQ(v[rows[nid] - j - 1], row_id++); } // Check that number of left/right rows are correct - size_t n_left = builder.GetNLeftElems(nid); + size_t n_left = builder.GetNLeftElems(nid); size_t n_right = builder.GetNRightElems(nid); ASSERT_EQ(n_left, rows_for_left_node[nid]); ASSERT_EQ(n_right, (rows[nid] - rows_for_left_node[nid])); } } -TEST(SyclPartitionBuilder, PartitioningSparce) { - TestPartitioning(0.3, 256); -} +TEST(SyclPartitionBuilder, PartitioningSparce) { TestPartitioning(0.3, 256); } -TEST(SyclPartitionBuilder, PartitioningDence8Bits) { - TestPartitioning(0.0, 256); -} +TEST(SyclPartitionBuilder, PartitioningDence8Bits) { TestPartitioning(0.0, 256); } -TEST(SyclPartitionBuilder, PartitioningDence16Bits) { - TestPartitioning(0.0, 256 + 1); -} +TEST(SyclPartitionBuilder, PartitioningDence16Bits) { TestPartitioning(0.0, 256 + 1); } -TEST(SyclPartitionBuilder, PartitioningDence32Bits) { - TestPartitioning(0.0, (1u << 16) + 1); -} +TEST(SyclPartitionBuilder, PartitioningDence32Bits) { TestPartitioning(0.0, (1u << 16) + 1); } -} // namespace xgboost::common +} // namespace xgboost::sycl::common diff --git a/tests/cpp/plugin/test_sycl_predictor.cc b/tests/cpp/plugin/test_sycl_predictor.cc old mode 100755 new mode 100644 index 04df03e29bc4..eff1016ae3a2 --- a/tests/cpp/plugin/test_sycl_predictor.cc +++ b/tests/cpp/plugin/test_sycl_predictor.cc @@ -8,6 +8,10 @@ #include #pragma GCC diagnostic pop +#include +#include +#include + #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtautological-constant-compare" #include "../../../src/data/adapter.h" diff --git a/tests/cpp/plugin/test_sycl_quantile_hist_builder.cc b/tests/cpp/plugin/test_sycl_quantile_hist_builder.cc index 4bf7bd962750..6ac552d73145 100644 --- a/tests/cpp/plugin/test_sycl_quantile_hist_builder.cc +++ b/tests/cpp/plugin/test_sycl_quantile_hist_builder.cc @@ -8,7 +8,10 @@ #pragma GCC diagnostic ignored "-W#pragma-messages" #include #include -#include "../../../plugin/sycl/tree/updater_quantile_hist.h" // for QuantileHistMaker + +#include + +#include "../../../plugin/sycl/tree/updater_quantile_hist.h" // for QuantileHistMaker #pragma GCC diagnostic pop namespace xgboost::sycl::tree { @@ -17,7 +20,8 @@ TEST(SyclQuantileHistMaker, Basic) { ctx.UpdateAllowUnknown(Args{{"device", "sycl"}}); ObjInfo task{ObjInfo::kRegression}; - std::unique_ptr updater{TreeUpdater::Create("grow_quantile_histmaker_sycl", &ctx, &task)}; + std::unique_ptr updater{ + TreeUpdater::Create("grow_quantile_histmaker_sycl", &ctx, &task)}; ASSERT_EQ(updater->Name(), "grow_quantile_histmaker_sycl"); } @@ -27,19 +31,21 @@ TEST(SyclQuantileHistMaker, JsonIO) { ctx.UpdateAllowUnknown(Args{{"device", "sycl"}}); ObjInfo task{ObjInfo::kRegression}; - Json config {Object()}; + Json config{Object()}; { - std::unique_ptr updater{TreeUpdater::Create("grow_quantile_histmaker_sycl", &ctx, &task)}; + std::unique_ptr updater{ + TreeUpdater::Create("grow_quantile_histmaker_sycl", &ctx, &task)}; updater->Configure({{"max_depth", std::to_string(42)}}); updater->Configure({{"single_precision_histogram", std::to_string(true)}}); updater->SaveConfig(&config); } { - std::unique_ptr updater{TreeUpdater::Create("grow_quantile_histmaker_sycl", &ctx, &task)}; + std::unique_ptr updater{ + TreeUpdater::Create("grow_quantile_histmaker_sycl", &ctx, &task)}; updater->LoadConfig(config); - Json new_config {Object()}; + Json new_config{Object()}; updater->SaveConfig(&new_config); ASSERT_EQ(config, new_config); @@ -47,9 +53,10 @@ TEST(SyclQuantileHistMaker, JsonIO) { auto max_depth = atoi(get(new_config["train_param"]["max_depth"]).c_str()); ASSERT_EQ(max_depth, 42); - auto single_precision_histogram = atoi(get(new_config["sycl_hist_train_param"]["single_precision_histogram"]).c_str()); + auto single_precision_histogram = + atoi(get(new_config["sycl_hist_train_param"]["single_precision_histogram"]) + .c_str()); ASSERT_EQ(single_precision_histogram, 1); } - } } // namespace xgboost::sycl::tree diff --git a/tests/cpp/plugin/test_sycl_regression_obj.cc b/tests/cpp/plugin/test_sycl_regression_obj.cc index d80fc0fb03f9..719aa66e871f 100644 --- a/tests/cpp/plugin/test_sycl_regression_obj.cc +++ b/tests/cpp/plugin/test_sycl_regression_obj.cc @@ -108,13 +108,11 @@ TEST(SyclObjective, DeclareUnifiedTest(PseudoHuber)) { TEST(SyclObjective, CPUvsSycl) { Context ctx_sycl; ctx_sycl.UpdateAllowUnknown(Args{{"device", "sycl"}}); - ObjFunction * obj_sycl = - ObjFunction::Create("reg:squarederror", &ctx_sycl); + ObjFunction* obj_sycl = ObjFunction::Create("reg:squarederror", &ctx_sycl); Context ctx_cpu; ctx_cpu.UpdateAllowUnknown(Args{{"device", "cpu"}}); - ObjFunction * obj_cpu = - ObjFunction::Create("reg:squarederror", &ctx_cpu); + ObjFunction* obj_cpu = ObjFunction::Create("reg:squarederror", &ctx_cpu); linalg::Matrix cpu_out_preds; linalg::Matrix sycl_out_preds; @@ -133,7 +131,7 @@ TEST(SyclObjective, CPUvsSycl) { info.labels.Reshape(kRows, 1); auto& h_labels = info.labels.Data()->HostVector(); for (size_t i = 0; i < h_labels.size(); ++i) { - h_labels[i] = 1 / static_cast(i+1); + h_labels[i] = 1 / static_cast(i + 1); } { diff --git a/tests/cpp/plugin/test_sycl_row_set_collection.cc b/tests/cpp/plugin/test_sycl_row_set_collection.cc index cefa24b166bd..377cb75a2384 100644 --- a/tests/cpp/plugin/test_sycl_row_set_collection.cc +++ b/tests/cpp/plugin/test_sycl_row_set_collection.cc @@ -25,12 +25,11 @@ TEST(SyclRowSetCollection, AddSplits) { size_t* p_row_indices = row_indices.Data(); qu->submit([&](::sycl::handler& cgh) { - cgh.parallel_for<>(::sycl::range<1>(num_rows), - [p_row_indices](::sycl::item<1> pid) { - const size_t idx = pid.get_id(0); - p_row_indices[idx] = idx; - }); - }).wait_and_throw(); + cgh.parallel_for<>(::sycl::range<1>(num_rows), [p_row_indices](::sycl::item<1> pid) { + const size_t idx = pid.get_id(0); + p_row_indices[idx] = idx; + }); + }).wait_and_throw(); row_set_collection.Init(); CHECK_EQ(row_set_collection.Size(), 1); @@ -39,7 +38,7 @@ TEST(SyclRowSetCollection, AddSplits) { auto& elem = row_set_collection[nid_test]; CHECK_EQ(elem.begin, row_indices.Begin()); CHECK_EQ(elem.end, row_indices.End()); - CHECK_EQ(elem.node_id , 0); + CHECK_EQ(elem.node_id, 0); } size_t nid = 0; @@ -55,7 +54,7 @@ TEST(SyclRowSetCollection, AddSplits) { auto& elem = row_set_collection[nid_test]; CHECK_EQ(elem.begin, nullptr); CHECK_EQ(elem.end, nullptr); - CHECK_EQ(elem.node_id , -1); + CHECK_EQ(elem.node_id, -1); } { @@ -63,7 +62,7 @@ TEST(SyclRowSetCollection, AddSplits) { auto& elem = row_set_collection[nid_test]; CHECK_EQ(elem.begin, row_indices.Begin()); CHECK_EQ(elem.end, row_indices.Begin() + n_left); - CHECK_EQ(elem.node_id , nid_test); + CHECK_EQ(elem.node_id, nid_test); } { @@ -71,8 +70,7 @@ TEST(SyclRowSetCollection, AddSplits) { auto& elem = row_set_collection[nid_test]; CHECK_EQ(elem.begin, row_indices.Begin() + n_left); CHECK_EQ(elem.end, row_indices.End()); - CHECK_EQ(elem.node_id , nid_test); + CHECK_EQ(elem.node_id, nid_test); } - } } // namespace xgboost::sycl::common diff --git a/tests/cpp/plugin/test_sycl_split_evaluator.cc b/tests/cpp/plugin/test_sycl_split_evaluator.cc index 507490fd17e1..794b7baba3bb 100644 --- a/tests/cpp/plugin/test_sycl_split_evaluator.cc +++ b/tests/cpp/plugin/test_sycl_split_evaluator.cc @@ -2,6 +2,8 @@ * Copyright 2020-2024 by XGBoost contributors */ #include + +#include #include #pragma GCC diagnostic push @@ -15,7 +17,7 @@ namespace xgboost::sycl::tree { -template +template void BasicTestSplitEvaluator(const std::string& monotone_constraints, bool has_constrains) { const size_t n_columns = 2; @@ -43,7 +45,7 @@ void BasicTestSplitEvaluator(const std::string& monotone_constraints, bool has_c } } -template +template void TestSplitEvaluator(const std::string& monotone_constraints) { const size_t n_columns = 2; @@ -88,7 +90,7 @@ void TestSplitEvaluator(const std::string& monotone_constraints) { bst_node_t nidx = 0; bst_feature_t fidx = 0; - GradientSumT wleft = split_evaluator.CalcWeight(nidx, left); + GradientSumT wleft = split_evaluator.CalcWeight(nidx, left); // wleft = -grad/hess = -0.1/0.2 EXPECT_NEAR(wleft, -0.5, eps); GradientSumT wright = split_evaluator.CalcWeight(nidx, right); diff --git a/tests/cpp/plugin/test_sycl_transform_range.cc b/tests/cpp/plugin/test_sycl_transform_range.cc index bfae073ac7d3..eff36f31a86f 100644 --- a/tests/cpp/plugin/test_sycl_transform_range.cc +++ b/tests/cpp/plugin/test_sycl_transform_range.cc @@ -32,14 +32,13 @@ TEST(SyclTransform, DeclareUnifiedTest(Basic)) { std::vector h_sol(size); std::iota(h_sol.begin(), h_sol.end(), 0); - auto device = DeviceOrd::SyclDefault(); + auto device = DeviceOrd::SyclDefault(); HostDeviceVector const in_vec{h_in, device}; HostDeviceVector out_vec{h_out, device}; out_vec.Fill(0); - Transform<>::Init(TestTransformRange{}, - Range{0, static_cast(size)}, 1, - device) + Transform<>::Init(TestTransformRange{}, Range{0, static_cast(size)}, + 1, device) .Eval(&out_vec, &in_vec); std::vector res = out_vec.HostVector(); diff --git a/tests/cpp/predictor/test_cpu_predictor.cc b/tests/cpp/predictor/test_cpu_predictor.cc index 40926cc4a39a..aab34116d662 100644 --- a/tests/cpp/predictor/test_cpu_predictor.cc +++ b/tests/cpp/predictor/test_cpu_predictor.cc @@ -4,13 +4,18 @@ #include #include +#include +#include +#include +#include + #include "../../../src/collective/communicator-inl.h" #include "../../../src/data/adapter.h" #include "../../../src/data/proxy_dmatrix.h" -#include "../../../src/predictor/array_tree_layout.h" -#include "../../../src/tree/tree_view.h" #include "../../../src/gbm/gbtree.h" #include "../../../src/gbm/gbtree_model.h" +#include "../../../src/predictor/array_tree_layout.h" +#include "../../../src/tree/tree_view.h" #include "../collective/test_worker.h" // for TestDistributedGlobal #include "../helpers.h" #include "test_predictor.h" @@ -234,9 +239,7 @@ TEST(CPUPredictor, GHistIndexTraining) { TestTrainingPrediction(&ctx, kRows, kBins, p_full, p_hist); } -TEST(CPUPredictor, CategoricalPrediction) { - TestCategoricalPrediction(false, false); -} +TEST(CPUPredictor, CategoricalPrediction) { TestCategoricalPrediction(false, false); } TEST(CPUPredictor, CategoricalPredictionColumnSplit) { auto constexpr kWorldSize = 2; diff --git a/tests/cpp/predictor/test_gpu_predictor.cu b/tests/cpp/predictor/test_gpu_predictor.cu index eaab01ca7cd5..6f7698b7c994 100644 --- a/tests/cpp/predictor/test_gpu_predictor.cu +++ b/tests/cpp/predictor/test_gpu_predictor.cu @@ -7,7 +7,10 @@ #include #include +#include +#include #include +#include #include "../../../src/data/device_adapter.cuh" #include "../../../src/data/proxy_dmatrix.h" @@ -243,9 +246,7 @@ TEST_F(MGPUPredictorTest, IterationRangeColumnSplit) { TestIterationRangeColumnSplit(curt::AllVisibleGPUs(), true); } -TEST(GPUPredictor, CategoricalPrediction) { - TestCategoricalPrediction(true, false); -} +TEST(GPUPredictor, CategoricalPrediction) { TestCategoricalPrediction(true, false); } TEST_F(MGPUPredictorTest, CategoricalPredictionColumnSplit) { this->DoTest([] { TestCategoricalPrediction(true, true); }, true); diff --git a/tests/cpp/predictor/test_predictor.cc b/tests/cpp/predictor/test_predictor.cc index 0cfdac5732b0..56a1d746cfeb 100644 --- a/tests/cpp/predictor/test_predictor.cc +++ b/tests/cpp/predictor/test_predictor.cc @@ -11,8 +11,9 @@ #include // for PredictionCacheEntry, Predictor, Predic... #include // for StringView -#include // for numeric_limits -#include // for shared_ptr +#include // for numeric_limits +#include // for shared_ptr +#include #include // for unordered_map #include // for move #include // for vector @@ -30,7 +31,7 @@ #include "xgboost/tree_model.h" // for RegTree namespace xgboost { -void TestBasic(DMatrix* dmat, Context const *ctx) { +void TestBasic(DMatrix *dmat, Context const *ctx) { auto predictor = std::unique_ptr(CreatePredictorForTest(ctx)); size_t const kCols = dmat->Info().num_col_; @@ -45,7 +46,7 @@ void TestBasic(DMatrix* dmat, Context const *ctx) { predictor->InitOutPredictions(dmat->Info(), &out_predictions.predictions, model); predictor->PredictBatch(dmat, &out_predictions, model, 0); - std::vector& out_predictions_h = out_predictions.predictions.HostVector(); + std::vector &out_predictions_h = out_predictions.predictions.HostVector(); for (size_t i = 0; i < out_predictions.predictions.Size(); i++) { ASSERT_EQ(out_predictions_h[i], 1.5); } @@ -53,7 +54,7 @@ void TestBasic(DMatrix* dmat, Context const *ctx) { // Test predict leaf HostDeviceVector leaf_out_predictions; predictor->PredictLeaf(dmat, &leaf_out_predictions, model); - auto const& h_leaf_out_predictions = leaf_out_predictions.ConstHostVector(); + auto const &h_leaf_out_predictions = leaf_out_predictions.ConstHostVector(); for (auto v : h_leaf_out_predictions) { ASSERT_EQ(v, 0); } @@ -125,13 +126,11 @@ void TestTrainingPrediction(Context const *ctx, size_t rows, size_t bins, void TestInplacePrediction(Context const *ctx, std::shared_ptr x, bst_idx_t rows, bst_feature_t cols) { - std::size_t constexpr kClasses { 4 }; + std::size_t constexpr kClasses{4}; auto gen = RandomDataGenerator{rows, cols, 0.5}.Device(ctx->Device()).Classes(kClasses); std::shared_ptr m = gen.GenerateDMatrix(true); - std::unique_ptr learner { - Learner::Create({m}) - }; + std::unique_ptr learner{Learner::Create({m})}; learner->SetParam("num_parallel_tree", "4"); learner->SetParam("num_class", std::to_string(kClasses)); @@ -159,15 +158,15 @@ void TestInplacePrediction(Context const *ctx, std::shared_ptr x, bst_i HostDeviceVector predict_1(p_out_predictions_1->Size()); predict_1.Copy(*p_out_predictions_1); - HostDeviceVector* p_out_predictions{nullptr}; + HostDeviceVector *p_out_predictions{nullptr}; learner->InplacePredict(x, PredictionType::kMargin, std::numeric_limits::quiet_NaN(), &p_out_predictions, 0, 4); - auto& h_pred = p_out_predictions->HostVector(); - auto& h_pred_0 = predict_0.HostVector(); - auto& h_pred_1 = predict_1.HostVector(); + auto &h_pred = p_out_predictions->HostVector(); + auto &h_pred_0 = predict_0.HostVector(); + auto &h_pred_1 = predict_1.HostVector(); - Json config {Object{}}; + Json config{Object{}}; learner->SaveConfig(&config); auto base_score = GetBaseScore(config); @@ -277,22 +276,20 @@ void TestPredictionWithLesserFeaturesColumnSplit(bool use_gpu) { VerifyPredictionWithLesserFeatures(learner.get(), kRows, sliced_test, sliced_invalid); } -void GBTreeModelForTest(gbm::GBTreeModel *model, uint32_t split_ind, - bst_cat_t split_cat, float left_weight, - float right_weight) { +void GBTreeModelForTest(gbm::GBTreeModel *model, uint32_t split_ind, bst_cat_t split_cat, + float left_weight, float right_weight) { PredictionCacheEntry out_predictions; std::vector> trees; trees.push_back(std::unique_ptr(new RegTree)); - auto& p_tree = trees.front(); + auto &p_tree = trees.front(); std::vector split_cats(LBitField32::ComputeStorageSize(split_cat)); LBitField32 cats_bits(split_cats); cats_bits.Set(split_cat); - p_tree->ExpandCategorical(0, split_ind, split_cats, true, 1.5f, - left_weight, right_weight, - 3.0f, 2.2f, 7.0f, 9.0f); + p_tree->ExpandCategorical(0, split_ind, split_cats, true, 1.5f, left_weight, right_weight, 3.0f, + 2.2f, 7.0f, 9.0f); model->CommitModelGroup(std::move(trees), 0); } @@ -382,7 +379,7 @@ void TestCategoricalPredictLeaf(Context const *ctx, bool is_column_split) { ASSERT_EQ(out_predictions.predictions.HostVector()[0], 1); } -void TestIterationRange(Context const* ctx) { +void TestIterationRange(Context const *ctx) { size_t constexpr kRows = 1000, kCols = 20, kClasses = 4, kForest = 3, kIters = 10; auto dmat = RandomDataGenerator(kRows, kCols, 0) .Device(ctx->Device()) @@ -688,41 +685,41 @@ void TestVectorLeafPrediction(Context const *ctx) { gbm::GBTreeModel model{&mparam, ctx}; model.CommitModelGroup(std::move(trees), 0); - auto test_batch = [&](float expected, HostDeviceVector const*p_data) { - auto p_fmat = GetDMatrixFromData(p_data->ConstHostVector(), kRows, kCols); - PredictionCacheEntry predt_cache; - predictor->InitOutPredictions(p_fmat->Info(), &predt_cache.predictions, model); - ASSERT_EQ(predt_cache.predictions.Size(), kRows * mparam.LeafLength()); - predictor->PredictBatch(p_fmat.get(), &predt_cache, model, 0, 1); - auto const &h_predt = predt_cache.predictions.HostVector(); - for (auto v : h_predt) { - ASSERT_EQ(v, expected); - } + auto test_batch = [&](float expected, HostDeviceVector const *p_data) { + auto p_fmat = GetDMatrixFromData(p_data->ConstHostVector(), kRows, kCols); + PredictionCacheEntry predt_cache; + predictor->InitOutPredictions(p_fmat->Info(), &predt_cache.predictions, model); + ASSERT_EQ(predt_cache.predictions.Size(), kRows * mparam.LeafLength()); + predictor->PredictBatch(p_fmat.get(), &predt_cache, model, 0, 1); + auto const &h_predt = predt_cache.predictions.HostVector(); + for (auto v : h_predt) { + ASSERT_EQ(v, expected); + } }; - auto test_inplace = [&](float expected, HostDeviceVector const*p_data) { - PredictionCacheEntry predt_cache; - std::shared_ptr p_fmat = GetDMatrixFromData(p_data->ConstHostVector(), kRows, kCols); - predictor->InitOutPredictions(p_fmat->Info(), &predt_cache.predictions, model); - if (ctx->IsCUDA()) { - // pull data to device. - p_data->SetDevice(ctx->Device()); - p_data->ConstDeviceSpan(); - } - auto arr = GetArrayInterface(p_data, kRows, kCols); - std::string str; - Json::Dump(arr, &str); - auto proxy = std::shared_ptr(new data::DMatrixProxy{}); - if (ctx->IsCUDA()) { - dynamic_cast(proxy.get())->SetCudaArray(str.c_str()); - } else { - dynamic_cast(proxy.get())->SetArray(str.c_str()); - } - predictor->InplacePredict(proxy, model, std::numeric_limits::quiet_NaN(), &predt_cache, - 0, 1); - auto const &h_predt = predt_cache.predictions.HostVector(); - for (auto v : h_predt) { - ASSERT_EQ(v, expected); - } + auto test_inplace = [&](float expected, HostDeviceVector const *p_data) { + PredictionCacheEntry predt_cache; + std::shared_ptr p_fmat = GetDMatrixFromData(p_data->ConstHostVector(), kRows, kCols); + predictor->InitOutPredictions(p_fmat->Info(), &predt_cache.predictions, model); + if (ctx->IsCUDA()) { + // pull data to device. + p_data->SetDevice(ctx->Device()); + p_data->ConstDeviceSpan(); + } + auto arr = GetArrayInterface(p_data, kRows, kCols); + std::string str; + Json::Dump(arr, &str); + auto proxy = std::shared_ptr(new data::DMatrixProxy{}); + if (ctx->IsCUDA()) { + dynamic_cast(proxy.get())->SetCudaArray(str.c_str()); + } else { + dynamic_cast(proxy.get())->SetArray(str.c_str()); + } + predictor->InplacePredict(proxy, model, std::numeric_limits::quiet_NaN(), &predt_cache, + 0, 1); + auto const &h_predt = predt_cache.predictions.HostVector(); + for (auto v : h_predt) { + ASSERT_EQ(v, expected); + } }; auto test_ghist = [&](float expected, HostDeviceVector *p_data) { // ghist diff --git a/tests/cpp/predictor/test_predictor.h b/tests/cpp/predictor/test_predictor.h index 34979cc23200..72501378040d 100644 --- a/tests/cpp/predictor/test_predictor.h +++ b/tests/cpp/predictor/test_predictor.h @@ -48,7 +48,7 @@ inline auto CreatePredictorForTest(Context const* ctx) { template void TestPredictionFromGradientIndex(Context const* ctx, size_t rows, size_t cols, std::shared_ptr p_hist) { - constexpr size_t kClasses { 3 }; + constexpr size_t kClasses{3}; LearnerModelParam mparam{MakeMP(cols, .5, kClasses, ctx->Device())}; auto cuda_ctx = MakeCUDACtx(0); @@ -89,7 +89,7 @@ void TestPredictionFromGradientIndex(Context const* ctx, size_t rows, size_t col } } -void TestBasic(DMatrix* dmat, Context const * ctx); +void TestBasic(DMatrix* dmat, Context const* ctx); // p_full and p_hist should come from the same data set. void TestTrainingPrediction(Context const* ctx, size_t rows, size_t bins, @@ -106,7 +106,7 @@ void TestCategoricalPrediction(bool use_gpu, bool is_column_split); void TestPredictionWithLesserFeaturesColumnSplit(bool use_gpu); -void TestCategoricalPredictLeaf(Context const *ctx, bool is_column_split); +void TestCategoricalPredictLeaf(Context const* ctx, bool is_column_split); void TestIterationRange(Context const* ctx); diff --git a/tests/cpp/predictor/test_shap.cc b/tests/cpp/predictor/test_shap.cc index 9368ec1ee0d8..c7c17c7196f6 100644 --- a/tests/cpp/predictor/test_shap.cc +++ b/tests/cpp/predictor/test_shap.cc @@ -11,6 +11,8 @@ #include // for unique_ptr #include // for to_string +#include +#include #include "../helpers.h" @@ -99,10 +101,8 @@ std::vector BuildShapTestCases(Context const* ctx) { { // multi-class dense training DMatrix, medium depth bst_target_t n_classes{3}; - auto dmat = RandomDataGenerator(256, 8, 0.0) - .Classes(n_classes) - .Device(device) - .GenerateDMatrix(true); + auto dmat = + RandomDataGenerator(256, 8, 0.0).Classes(n_classes).Device(device).GenerateDMatrix(true); SetLabels(dmat.get(), n_classes); auto args = BaseParams(ctx, "multi:softprob", "4"); args.emplace_back("num_class", std::to_string(n_classes)); @@ -123,7 +123,8 @@ void CheckShapOutput(DMatrix* dmat, Args const& model_args) { size_t const kRows = dmat->Info().num_row_; size_t const kCols = dmat->Info().num_col_; - std::shared_ptr p_dmat{dmat, [](DMatrix*) {}}; + std::shared_ptr p_dmat{dmat, [](DMatrix*) { + }}; std::unique_ptr learner{Learner::Create({p_dmat})}; learner->SetParams(model_args); learner->Configure(); @@ -142,8 +143,7 @@ void CheckShapOutput(DMatrix* dmat, Args const& model_args) { HostDeviceVector shap_interactions; learner->Predict(p_dmat, false, &shap_interactions, 0, 0, false, false, false, false, true); - ASSERT_EQ(shap_interactions.HostVector().size(), - kRows * (kCols + 1) * (kCols + 1) * n_outputs); + ASSERT_EQ(shap_interactions.HostVector().size(), kRows * (kCols + 1) * (kCols + 1) * n_outputs); CheckShapAdditivity(kRows, kCols, shap_interactions, margin_predt); } diff --git a/tests/cpp/test_cache.cc b/tests/cpp/test_cache.cc index cd4b28b0536b..98b0ed62baea 100644 --- a/tests/cpp/test_cache.cc +++ b/tests/cpp/test_cache.cc @@ -5,11 +5,13 @@ #include #include // for DMatrix -#include // for size_t -#include // for uint32_t -#include // for thread +#include // for size_t +#include // for uint32_t +#include +#include // for thread +#include -#include "helpers.h" // for RandomDataGenerator +#include "helpers.h" // for RandomDataGenerator namespace xgboost { namespace { diff --git a/tests/cpp/test_context.cc b/tests/cpp/test_context.cc index 4eb765c93c92..a657e072b139 100644 --- a/tests/cpp/test_context.cc +++ b/tests/cpp/test_context.cc @@ -6,6 +6,7 @@ #include #include +#include namespace xgboost { TEST(Context, CPU) { diff --git a/tests/cpp/test_context.cu b/tests/cpp/test_context.cu index 7f205ab0f849..728e572aff98 100644 --- a/tests/cpp/test_context.cu +++ b/tests/cpp/test_context.cu @@ -2,12 +2,12 @@ * Copyright 2023-2025, XGBoost Contributors */ #include -#include // for thread #include // for Args #include #include // for FromJson, ToJson #include // for string, to_string +#include // for thread #include "../../src/common/cuda_rt_utils.h" // for AllVisibleGPUs diff --git a/tests/cpp/test_helpers.cc b/tests/cpp/test_helpers.cc index bfc2100dfe49..9afaefd84a77 100644 --- a/tests/cpp/test_helpers.cc +++ b/tests/cpp/test_helpers.cc @@ -2,16 +2,19 @@ * Copyright 2020-2025, XGBoost Contributors */ #include + #include +#include +#include -#include "helpers.h" -#include "filesystem.h" // for TemporaryDirectory #include "../../src/data/array_interface.h" +#include "filesystem.h" // for TemporaryDirectory +#include "helpers.h" namespace xgboost { TEST(RandomDataGenerator, DMatrix) { - size_t constexpr kRows { 16 }, kCols { 32 }; - float constexpr kSparsity { 0.4f }; + size_t constexpr kRows{16}, kCols{32}; + float constexpr kSparsity{0.4f}; auto p_dmatrix = RandomDataGenerator{kRows, kCols, kSparsity}.GenerateDMatrix(); HostDeviceVector csr_value; @@ -22,9 +25,8 @@ TEST(RandomDataGenerator, DMatrix) { HostDeviceVector dense_data; RandomDataGenerator{kRows, kCols, kSparsity}.GenerateDense(&dense_data); - auto it = std::copy_if( - dense_data.HostVector().begin(), dense_data.HostVector().end(), - dense_data.HostVector().begin(), [](float v) { return !std::isnan(v); }); + auto it = std::copy_if(dense_data.HostVector().begin(), dense_data.HostVector().end(), + dense_data.HostVector().begin(), [](float v) { return !std::isnan(v); }); CHECK_EQ(p_dmatrix->Info().num_row_, kRows); CHECK_EQ(p_dmatrix->Info().num_col_, kCols); @@ -47,21 +49,19 @@ TEST(RandomDataGenerator, DMatrix) { } TEST(RandomDataGenerator, GenerateArrayInterfaceBatch) { - size_t constexpr kRows { 937 }, kCols { 100 }, kBatches { 13 }; - float constexpr kSparsity { 0.4f }; + size_t constexpr kRows{937}, kCols{100}, kBatches{13}; + float constexpr kSparsity{0.4f}; HostDeviceVector storage; std::string array; std::vector batches; std::tie(batches, array) = - RandomDataGenerator{kRows, kCols, kSparsity}.GenerateArrayInterfaceBatch( - &storage, kBatches); + RandomDataGenerator{kRows, kCols, kSparsity}.GenerateArrayInterfaceBatch(&storage, kBatches); CHECK_EQ(batches.size(), kBatches); size_t rows = 0; - for (auto const &interface_str : batches) { - Json j_interface = - Json::Load({interface_str.c_str(), interface_str.size()}); + for (auto const& interface_str : batches) { + Json j_interface = Json::Load({interface_str.c_str(), interface_str.size()}); ArrayInterfaceHandler::Validate(get(j_interface)); CHECK_EQ(get(j_interface["shape"][1]), kCols); rows += get(j_interface["shape"][0]); diff --git a/tests/cpp/test_histogram_helpers.cu b/tests/cpp/test_histogram_helpers.cu index 8f2ce06bf3d6..db5cce47b42a 100644 --- a/tests/cpp/test_histogram_helpers.cu +++ b/tests/cpp/test_histogram_helpers.cu @@ -1,6 +1,11 @@ +/** + * Copyright 2026, XGBoost contributors + */ #include #include +#include + #include "histogram_helpers.h" namespace xgboost { diff --git a/tests/cpp/test_learner.cc b/tests/cpp/test_learner.cc index 0beeabcc5a74..58a15ca59f43 100644 --- a/tests/cpp/test_learner.cc +++ b/tests/cpp/test_learner.cc @@ -50,7 +50,6 @@ TEST(Learner, Basic) { auto learner = std::unique_ptr(Learner::Create({mat_ptr})); learner->SetParams(args); - auto major = XGBOOST_VER_MAJOR; auto minor = XGBOOST_VER_MINOR; auto patch = XGBOOST_VER_PATCH; @@ -96,7 +95,7 @@ TEST(Learner, CheckGroup) { group[1] = 3; group[2] = 7; group[3] = 5; - std::vector labels (kNumRows); + std::vector labels(kNumRows); for (size_t i = 0; i < kNumRows; ++i) { labels[i] = i % 2; } @@ -110,10 +109,10 @@ TEST(Learner, CheckGroup) { learner->SetParams({Arg{"objective", "rank:pairwise"}}); EXPECT_NO_THROW(learner->UpdateOneIter(0, p_mat)); - group.resize(kNumGroups+1); + group.resize(kNumGroups + 1); group[3] = 4; group[4] = 1; - p_mat->SetInfo("group", Make1dInterfaceTest(group.data(), kNumGroups+1)); + p_mat->SetInfo("group", Make1dInterfaceTest(group.data(), kNumGroups + 1)); EXPECT_ANY_THROW(learner->UpdateOneIter(0, p_mat)); } @@ -131,7 +130,7 @@ TEST(Learner, CheckMultiBatch) { TEST(Learner, Configuration) { std::string const emetric = "eval_metric"; { - std::unique_ptr learner { Learner::Create({nullptr}) }; + std::unique_ptr learner{Learner::Create({nullptr})}; learner->SetParam(emetric, "auc"); learner->SetParam(emetric, "rmsle"); learner->SetParam("foo", "bar"); @@ -144,7 +143,7 @@ TEST(Learner, Configuration) { } { - std::unique_ptr learner { Learner::Create({nullptr}) }; + std::unique_ptr learner{Learner::Create({nullptr})}; learner->SetParams({{"foo", "bar"}, {emetric, "auc"}, {emetric, "entropy"}, {emetric, "KL"}}); auto attr_names = learner->GetConfigurationArguments(); ASSERT_EQ(attr_names.size(), 1ul); @@ -162,14 +161,14 @@ TEST(Learner, JsonModelIO) { CHECK_NE(p_dmat->Info().num_col_, 0); { - std::unique_ptr learner { Learner::Create({p_dmat}) }; + std::unique_ptr learner{Learner::Create({p_dmat})}; learner->Configure(); - Json out { Object() }; + Json out{Object()}; learner->SaveModel(&out); common::TemporaryDirectory tmpdir; - std::ofstream fout (tmpdir.Path() / "model.json"); + std::ofstream fout(tmpdir.Path() / "model.json"); fout << out; fout.close(); @@ -179,23 +178,23 @@ TEST(Learner, JsonModelIO) { learner->LoadModel(loaded); learner->Configure(); - Json new_in { Object() }; + Json new_in{Object()}; learner->SaveModel(&new_in); ASSERT_EQ(new_in, out); } { - std::unique_ptr learner { Learner::Create({p_dmat}) }; + std::unique_ptr learner{Learner::Create({p_dmat})}; for (int32_t iter = 0; iter < kIters; ++iter) { learner->UpdateOneIter(iter, p_dmat); } learner->SetAttr("best_score", "15.2"); - Json out { Object() }; + Json out{Object()}; learner->SaveModel(&out); learner->LoadModel(out); - Json new_in { Object() }; + Json new_in{Object()}; learner->Configure(); learner->SaveModel(&new_in); @@ -268,17 +267,17 @@ TEST(Learner, MultiThreadedPredict) { for (decltype(n_threads) thread_id = 0; thread_id < n_threads; ++thread_id) { threads.emplace_back([learner, p_data] { size_t constexpr kIters = 10; - auto &entry = learner->GetThreadLocal().prediction_entry; + auto& entry = learner->GetThreadLocal().prediction_entry; HostDeviceVector predictions; for (size_t iter = 0; iter < kIters; ++iter) { learner->Predict(p_data, false, &entry.predictions, 0, 0); - learner->Predict(p_data, false, &predictions, 0, 0, false, true); // leaf + learner->Predict(p_data, false, &predictions, 0, 0, false, true); // leaf learner->Predict(p_data, false, &predictions, 0, 0, false, false, true); // contribs } }); } - for (auto &thread : threads) { + for (auto& thread : threads) { thread.join(); } } @@ -289,7 +288,7 @@ TEST(Learner, GPUConfiguration) { using Arg = std::pair; size_t constexpr kRows = 10; auto p_dmat = RandomDataGenerator(kRows, 10, 0).GenerateDMatrix(); - std::vector> mat {p_dmat}; + std::vector> mat{p_dmat}; std::vector labels(kRows); for (size_t i = 0; i < labels.size(); ++i) { labels[i] = i; @@ -312,14 +311,14 @@ TEST(Learner, GPUConfiguration) { ASSERT_EQ(learner->Ctx()->Device(), DeviceOrd::CUDA(0)); } { - std::unique_ptr learner {Learner::Create(mat)}; + std::unique_ptr learner{Learner::Create(mat)}; learner->SetParams({Arg{"tree_method", "hist"}, Arg{"device", "cuda"}}); learner->UpdateOneIter(0, p_dmat); ASSERT_EQ(learner->Ctx()->Device(), DeviceOrd::CUDA(0)); } { // with CPU algorithm - std::unique_ptr learner {Learner::Create(mat)}; + std::unique_ptr learner{Learner::Create(mat)}; learner->SetParams({Arg{"tree_method", "hist"}}); learner->UpdateOneIter(0, p_dmat); ASSERT_EQ(learner->Ctx()->Device(), DeviceOrd::CPU()); @@ -329,23 +328,19 @@ TEST(Learner, GPUConfiguration) { TEST(Learner, Seed) { auto m = RandomDataGenerator{10, 10, 0}.GenerateDMatrix(); - std::unique_ptr learner { - Learner::Create({m}) - }; + std::unique_ptr learner{Learner::Create({m})}; auto seed = std::numeric_limits::max(); learner->SetParam("seed", std::to_string(seed)); learner->Configure(); - Json config { Object() }; + Json config{Object()}; learner->SaveConfig(&config); - ASSERT_EQ(std::to_string(seed), - get(config["learner"]["generic_param"]["seed"])); + ASSERT_EQ(std::to_string(seed), get(config["learner"]["generic_param"]["seed"])); seed = std::numeric_limits::min(); learner->SetParam("seed", std::to_string(seed)); learner->Configure(); learner->SaveConfig(&config); - ASSERT_EQ(std::to_string(seed), - get(config["learner"]["generic_param"]["seed"])); + ASSERT_EQ(std::to_string(seed), get(config["learner"]["generic_param"]["seed"])); } TEST(Learner, ConstantSeed) { diff --git a/tests/cpp/test_logging.cc b/tests/cpp/test_logging.cc index 90daa12d1b28..92eb6a0434e3 100644 --- a/tests/cpp/test_logging.cc +++ b/tests/cpp/test_logging.cc @@ -1,12 +1,16 @@ -#include - +/** + * Copyright 2026, XGBoost contributors + */ #include #include +#include +#include + namespace xgboost { TEST(Logging, Basic) { - std::map args {}; + std::map args{}; std::string output; args["verbosity"] = "0"; // silent diff --git a/tests/cpp/test_multi_target.cc b/tests/cpp/test_multi_target.cc index cf926ae26563..baf64d3aaa10 100644 --- a/tests/cpp/test_multi_target.cc +++ b/tests/cpp/test_multi_target.cc @@ -2,23 +2,23 @@ * Copyright 2023-2025, XGBoost Contributors */ #include -#include // for Args, bst_target_t -#include // for DMatrix, MetaInfo -#include // for Json, get, Object, String -#include // for Learner - -#include // for copy -#include // for size_t -#include // for shared_ptr, allocator, __shared_ptr_access -#include // for accumulate -#include // for stod, string -#include // for vector - -#include "../../src/common/linalg_op.h" // for begin, cbegin, cend -#include "../../src/common/stats.h" // for Median -#include "helpers.h" // for RandomDataGenerator -#include "xgboost/host_device_vector.h" // for HostDeviceVector -#include "xgboost/linalg.h" // for Tensor, All, TensorView, Vector +#include // for Args, bst_target_t +#include // for DMatrix, MetaInfo +#include // for Json, get, Object, String +#include // for Learner + +#include // for copy +#include // for size_t +#include // for shared_ptr, allocator, __shared_ptr_access +#include // for accumulate +#include // for stod, string +#include // for vector + +#include "../../src/common/linalg_op.h" // for begin, cbegin, cend +#include "../../src/common/stats.h" // for Median +#include "helpers.h" // for RandomDataGenerator +#include "xgboost/host_device_vector.h" // for HostDeviceVector +#include "xgboost/linalg.h" // for Tensor, All, TensorView, Vector namespace xgboost { class TestL1MultiTarget : public ::testing::Test { diff --git a/tests/cpp/test_serialization.cc b/tests/cpp/test_serialization.cc index a033ab72d4cf..7d12d40dba06 100644 --- a/tests/cpp/test_serialization.cc +++ b/tests/cpp/test_serialization.cc @@ -10,7 +10,10 @@ #include // for Json #include +#include #include +#include +#include #include "../../src/common/io.h" #include "../../src/common/random.h" @@ -30,103 +33,103 @@ void CompareIntArray(Json l, Json r) { void CompareJSON(Json l, Json r) { switch (l.GetValue().Type()) { - case Value::ValueKind::kString: { - ASSERT_EQ(l, r); - break; - } - case Value::ValueKind::kNumber: { - ASSERT_NEAR(get(l), get(r), kRtEps); - break; - } - case Value::ValueKind::kInteger: { - ASSERT_EQ(l, r); - break; - } - case Value::ValueKind::kObject: { - auto const &l_obj = get(l); - auto const &r_obj = get(r); - ASSERT_EQ(l_obj.size(), r_obj.size()); - - for (auto const& kv : l_obj) { - ASSERT_NE(r_obj.find(kv.first), r_obj.cend()); - // Floating point array saved as a string. - if (kv.first == "base_score") { - auto l_v = Json::Load(get(l_obj.at(kv.first))); - auto r_v = Json::Load(get(r_obj.at(kv.first))); - CompareJSON(l_v, r_v); - } else { - CompareJSON(l_obj.at(kv.first), r_obj.at(kv.first)); + case Value::ValueKind::kString: { + ASSERT_EQ(l, r); + break; + } + case Value::ValueKind::kNumber: { + ASSERT_NEAR(get(l), get(r), kRtEps); + break; + } + case Value::ValueKind::kInteger: { + ASSERT_EQ(l, r); + break; + } + case Value::ValueKind::kObject: { + auto const& l_obj = get(l); + auto const& r_obj = get(r); + ASSERT_EQ(l_obj.size(), r_obj.size()); + + for (auto const& kv : l_obj) { + ASSERT_NE(r_obj.find(kv.first), r_obj.cend()); + // Floating point array saved as a string. + if (kv.first == "base_score") { + auto l_v = Json::Load(get(l_obj.at(kv.first))); + auto r_v = Json::Load(get(r_obj.at(kv.first))); + CompareJSON(l_v, r_v); + } else { + CompareJSON(l_obj.at(kv.first), r_obj.at(kv.first)); + } } + break; } - break; - } - case Value::ValueKind::kArray: { - auto const& l_arr = get(l); - auto const& r_arr = get(r); - ASSERT_EQ(l_arr.size(), r_arr.size()); - for (size_t i = 0; i < l_arr.size(); ++i) { - CompareJSON(l_arr[i], r_arr[i]); + case Value::ValueKind::kArray: { + auto const& l_arr = get(l); + auto const& r_arr = get(r); + ASSERT_EQ(l_arr.size(), r_arr.size()); + for (size_t i = 0; i < l_arr.size(); ++i) { + CompareJSON(l_arr[i], r_arr[i]); + } + break; } - break; - } - case Value::ValueKind::kF32Array: { - auto const& l_arr = get(l); - auto const& r_arr = get(r); - ASSERT_EQ(l_arr.size(), r_arr.size()); - for (size_t i = 0; i < l_arr.size(); ++i) { - ASSERT_NEAR(l_arr[i], r_arr[i], kRtEps); + case Value::ValueKind::kF32Array: { + auto const& l_arr = get(l); + auto const& r_arr = get(r); + ASSERT_EQ(l_arr.size(), r_arr.size()); + for (size_t i = 0; i < l_arr.size(); ++i) { + ASSERT_NEAR(l_arr[i], r_arr[i], kRtEps); + } + break; } - break; - } - case Value::ValueKind::kF64Array: { - auto const& l_arr = get(l); - auto const& r_arr = get(r); - ASSERT_EQ(l_arr.size(), r_arr.size()); - for (size_t i = 0; i < l_arr.size(); ++i) { - ASSERT_NEAR(l_arr[i], r_arr[i], kRtEps); + case Value::ValueKind::kF64Array: { + auto const& l_arr = get(l); + auto const& r_arr = get(r); + ASSERT_EQ(l_arr.size(), r_arr.size()); + for (size_t i = 0; i < l_arr.size(); ++i) { + ASSERT_NEAR(l_arr[i], r_arr[i], kRtEps); + } + break; + } + case Value::ValueKind::kI8Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kU8Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kI16Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kU16Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kI32Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kU32Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kI64Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kU64Array: { + CompareIntArray(l, r); + break; + } + case Value::ValueKind::kBoolean: { + ASSERT_EQ(l, r); + break; + } + case Value::ValueKind::kNull: { + ASSERT_EQ(l, r); + break; } - break; - } - case Value::ValueKind::kI8Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kU8Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kI16Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kU16Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kI32Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kU32Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kI64Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kU64Array: { - CompareIntArray(l, r); - break; - } - case Value::ValueKind::kBoolean: { - ASSERT_EQ(l, r); - break; - } - case Value::ValueKind::kNull: { - ASSERT_EQ(l, r); - break; - } } } @@ -149,7 +152,7 @@ void TestLearnerSerialization(Args args, FeatureMap const& fmap, std::shared_ptr // Train for kIters. { std::unique_ptr fo(dmlc::Stream::Create(fname.c_str(), "w")); - std::unique_ptr learner {Learner::Create({p_dmat})}; + std::unique_ptr learner{Learner::Create({p_dmat})}; learner->SetParams(args); for (int32_t iter = 0; iter < kIters; ++iter) { learner->UpdateOneIter(iter, p_dmat); @@ -165,7 +168,7 @@ void TestLearnerSerialization(Args args, FeatureMap const& fmap, std::shared_ptr std::vector dumped_1; { std::unique_ptr fi(dmlc::Stream::Create(fname.c_str(), "r")); - std::unique_ptr learner {Learner::Create({p_dmat})}; + std::unique_ptr learner{Learner::Create({p_dmat})}; learner->Load(fi.get()); learner->Configure(); dumped_1 = learner->DumpModel(fmap, true, "json"); @@ -179,8 +182,7 @@ void TestLearnerSerialization(Args args, FeatureMap const& fmap, std::shared_ptr std::string continued_model; { // Continue the previous training with another kIters - std::unique_ptr fi( - dmlc::Stream::Create(fname.c_str(), "r")); + std::unique_ptr fi(dmlc::Stream::Create(fname.c_str(), "r")); std::unique_ptr learner{Learner::Create({p_dmat})}; learner->Load(fi.get()); learner->Configure(); @@ -191,7 +193,7 @@ void TestLearnerSerialization(Args args, FeatureMap const& fmap, std::shared_ptr learner->Save(&mem_out); ASSERT_EQ(model_at_kiter, serialised_model_tmp); - for (auto &batch : p_dmat->GetBatches()) { + for (auto& batch : p_dmat->GetBatches()) { batch.data.HostVector(); batch.offset.HostVector(); } @@ -251,7 +253,7 @@ void TestLearnerSerialization(Args args, FeatureMap const& fmap, std::shared_ptr } // Pull data to device - for (auto &batch : p_dmat->GetBatches()) { + for (auto& batch : p_dmat->GetBatches()) { batch.data.SetDevice(DeviceOrd::CUDA(0)); batch.data.DeviceSpan(); batch.offset.SetDevice(DeviceOrd::CUDA(0)); @@ -294,7 +296,9 @@ class SerializationTest : public ::testing::Test { xgboost::SimpleLCG gen(0); SimpleRealUniformDistribution dis(0.0f, 1.0f); - for (auto& v : h_labels) { v = dis(&gen); } + for (auto& v : h_labels) { + v = dis(&gen); + } for (size_t i = 0; i < kCols; ++i) { std::string name = "feat_" + std::to_string(i); @@ -382,11 +386,9 @@ TEST_F(SerializationTest, Hist) { } TEST_F(SerializationTest, CPUCoordDescent) { - TestLearnerSerialization({{"booster", "gblinear"}, - {"seed", "0"}, - {"nthread", "1"}, - {"updater", "coord_descent"}}, - fmap_, p_dmat_); + TestLearnerSerialization( + {{"booster", "gblinear"}, {"seed", "0"}, {"nthread", "1"}, {"updater", "coord_descent"}}, + fmap_, p_dmat_); } #if defined(XGBOOST_USE_CUDA) @@ -455,7 +457,7 @@ TEST_F(SerializationTest, ConfigurationCount) { size_t pos = 0; // Should run configuration exactly 2 times, one for each learner. while ((pos = output.find("[GPU Hist]: Configure", pos)) != std::string::npos) { - occureences ++; + occureences++; pos += target.size(); } ASSERT_EQ(occureences, 2ul); @@ -527,7 +529,9 @@ class LogitSerializationTest : public SerializationTest { auto& rnd = common::GlobalRandom(); rnd.seed(0); - for (auto& v : h_labels) { v = flip(rnd); } + for (auto& v : h_labels) { + v = flip(rnd); + } for (size_t i = 0; i < kCols; ++i) { std::string name = "feat_" + std::to_string(i); @@ -591,11 +595,9 @@ TEST_F(LogitSerializationTest, Hist) { } TEST_F(LogitSerializationTest, CPUCoordDescent) { - TestLearnerSerialization({{"booster", "gblinear"}, - {"seed", "0"}, - {"nthread", "1"}, - {"updater", "coord_descent"}}, - fmap_, p_dmat_); + TestLearnerSerialization( + {{"booster", "gblinear"}, {"seed", "0"}, {"nthread", "1"}, {"updater", "coord_descent"}}, + fmap_, p_dmat_); } #if defined(XGBOOST_USE_CUDA) @@ -649,13 +651,15 @@ class MultiClassesSerializationTest : public SerializationTest { std::shared_ptr p_dmat{p_dmat_}; p_dmat->Info().labels.Reshape(kRows); - auto &h_labels = p_dmat->Info().labels.Data()->HostVector(); + auto& h_labels = p_dmat->Info().labels.Data()->HostVector(); std::uniform_int_distribution categorical(0, kClasses - 1); auto& rnd = common::GlobalRandom(); rnd.seed(0); - for (auto& v : h_labels) { v = categorical(rnd); } + for (auto& v : h_labels) { + v = categorical(rnd); + } for (size_t i = 0; i < kCols; ++i) { std::string name = "feat_" + std::to_string(i); @@ -737,11 +741,9 @@ TEST_F(MultiClassesSerializationTest, Hist) { } TEST_F(MultiClassesSerializationTest, CPUCoordDescent) { - TestLearnerSerialization({{"booster", "gblinear"}, - {"seed", "0"}, - {"nthread", "1"}, - {"updater", "coord_descent"}}, - fmap_, p_dmat_); + TestLearnerSerialization( + {{"booster", "gblinear"}, {"seed", "0"}, {"nthread", "1"}, {"updater", "coord_descent"}}, + fmap_, p_dmat_); } #if defined(XGBOOST_USE_CUDA) @@ -792,4 +794,4 @@ TEST_F(MultiClassesSerializationTest, GPUCoordDescent) { fmap_, p_dmat_); } #endif // defined(XGBOOST_USE_CUDA) -} // namespace xgboost +} // namespace xgboost diff --git a/tests/cpp/tree/gpu_hist/test_driver.cu b/tests/cpp/tree/gpu_hist/test_driver.cu index 659e1ba34fd9..49b43fe8c49c 100644 --- a/tests/cpp/tree/gpu_hist/test_driver.cu +++ b/tests/cpp/tree/gpu_hist/test_driver.cu @@ -2,6 +2,7 @@ * Copyright 2020-2025, XGBoost contributors */ #include + #include "../../../../src/tree/driver.h" #include "../../../../src/tree/gpu_hist/expand_entry.cuh" @@ -56,7 +57,7 @@ TEST(GpuHist, DriverLossGuided) { Driver driver(p); EXPECT_TRUE(driver.Pop().empty()); - GPUExpandEntry root(0, 0, high_gain, 2.0f, 1.0f, 1.0f ); + GPUExpandEntry root(0, 0, high_gain, 2.0f, 1.0f, 1.0f); driver.Push({root}); EXPECT_EQ(driver.Pop().front().nidx, 0); // Select high gain first diff --git a/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu b/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu index 690e877ecb03..c2e02e7c64a5 100644 --- a/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu +++ b/tests/cpp/tree/gpu_hist/test_evaluate_splits.cu @@ -4,6 +4,8 @@ #include #include +#include + #include "../../../../src/tree/gpu_hist/evaluate_splits.cuh" #include "../../collective/test_worker.h" // for BaseMGPUTest #include "../../helpers.h" diff --git a/tests/cpp/tree/gpu_hist/test_histogram.cu b/tests/cpp/tree/gpu_hist/test_histogram.cu index a3f49b3035d0..cd8b5f85db01 100644 --- a/tests/cpp/tree/gpu_hist/test_histogram.cu +++ b/tests/cpp/tree/gpu_hist/test_histogram.cu @@ -4,6 +4,8 @@ #include #include // for Context +#include +#include #include // for unique_ptr #include // for tuple #include // for vector @@ -226,7 +228,7 @@ void TestGPUHistogramCategorical(size_t num_categories) { /** * Generate hist with cat data. */ - for (auto const &batch : cat_m->GetBatches(&ctx, batch_param)) { + for (auto const& batch : cat_m->GetBatches(&ctx, batch_param)) { auto* page = batch.Impl(); FeatureGroups single_group(page->Cuts()); DeviceHistogramBuilder builder; @@ -243,7 +245,7 @@ void TestGPUHistogramCategorical(size_t num_categories) { auto x_encoded = OneHotEncodeFeature(x, num_categories); auto encode_m = GetDMatrixFromData(x_encoded, kRows, num_categories); dh::device_vector encode_hist(2 * num_categories); - for (auto const &batch : encode_m->GetBatches(&ctx, batch_param)) { + for (auto const& batch : encode_m->GetBatches(&ctx, batch_param)) { auto* page = batch.Impl(); FeatureGroups single_group(page->Cuts()); DeviceHistogramBuilder builder; @@ -259,8 +261,7 @@ void TestGPUHistogramCategorical(size_t num_categories) { std::vector h_encode_hist(encode_hist.size()); thrust::copy(encode_hist.begin(), encode_hist.end(), h_encode_hist.begin()); - ValidateCategoricalHistogram(num_categories, - common::Span{h_encode_hist}, + ValidateCategoricalHistogram(num_categories, common::Span{h_encode_hist}, common::Span{h_cat_hist}); } @@ -277,13 +278,13 @@ TEST(Histogram, GPUHistCategorical) { namespace { // Atomic add as type cast for test. -XGBOOST_DEV_INLINE int64_t atomicAdd(int64_t *dst, int64_t src) { // NOLINT +XGBOOST_DEV_INLINE int64_t atomicAdd(int64_t* dst, int64_t src) { // NOLINT uint64_t* u_dst = reinterpret_cast(dst); uint64_t u_src = *reinterpret_cast(&src); uint64_t ret = ::atomicAdd(u_dst, u_src); return *reinterpret_cast(&ret); } -} +} // namespace void TestAtomicAdd() { size_t n_elements = 1024; @@ -312,8 +313,7 @@ void TestAtomicAdd() { /** * Test for positive values that don't fit into 32 bit integer. */ - thrust::fill(inputs.begin(), inputs.end(), - (std::numeric_limits::max() / 2)); + thrust::fill(inputs.begin(), inputs.end(), (std::numeric_limits::max() / 2)); thrust::fill(result_a.begin(), result_a.end(), 0); thrust::fill(result_b.begin(), result_b.end(), 0); dh::LaunchN(n_elements, [=] __device__(size_t i) { @@ -327,8 +327,7 @@ void TestAtomicAdd() { /** * Test for negative values that don't fit into 32 bit integer. */ - thrust::fill(inputs.begin(), inputs.end(), - (std::numeric_limits::min() / 2)); + thrust::fill(inputs.begin(), inputs.end(), (std::numeric_limits::min() / 2)); thrust::fill(result_a.begin(), result_a.end(), 0); thrust::fill(result_b.begin(), result_b.end(), 0); dh::LaunchN(n_elements, [=] __device__(size_t i) { @@ -340,9 +339,7 @@ void TestAtomicAdd() { CHECK_EQ(thrust::reduce(inputs.begin(), inputs.end(), int64_t(0)), result_a[0]); } -TEST(Histogram, AtomicAddInt64) { - TestAtomicAdd(); -} +TEST(Histogram, AtomicAddInt64) { TestAtomicAdd(); } TEST(Histogram, Quantiser) { auto ctx = MakeCUDACtx(0); diff --git a/tests/cpp/tree/gpu_hist/test_multi_evaluate_splits.cu b/tests/cpp/tree/gpu_hist/test_multi_evaluate_splits.cu index 8136976d0ebb..38ace78828ef 100644 --- a/tests/cpp/tree/gpu_hist/test_multi_evaluate_splits.cu +++ b/tests/cpp/tree/gpu_hist/test_multi_evaluate_splits.cu @@ -3,6 +3,9 @@ */ #include +#include +#include + #include "../../../../src/tree/gpu_hist/evaluate_splits.cuh" #include "../../../../src/tree/gpu_hist/multi_evaluate_splits.cuh" #include "../../helpers.h" diff --git a/tests/cpp/tree/gpu_hist/test_multi_histogram.cu b/tests/cpp/tree/gpu_hist/test_multi_histogram.cu index 067818b23ef7..ca6af3a05ccc 100644 --- a/tests/cpp/tree/gpu_hist/test_multi_histogram.cu +++ b/tests/cpp/tree/gpu_hist/test_multi_histogram.cu @@ -5,6 +5,9 @@ #include #include +#include +#include +#include #include "../../../../src/tree/gpu_hist/histogram.cuh" #include "../../helpers.h" @@ -43,8 +46,7 @@ class MultiHistTest this->page = MakeEllpackForTest(&ctx, n_samples, n_features, n_bins); this->cuts = page->CutsShared(); - this->p_fg = - std::make_unique(*cuts, true, DftMtHistShmemBytes(ctx.Ordinal())); + this->p_fg = std::make_unique(*cuts, true, DftMtHistShmemBytes(ctx.Ordinal())); this->gpairs = linalg::Constant(&ctx, GradientPair{1.0f, 1.0f}, n_samples, n_targets); this->quantizers = MakeDummyQuantizers(n_targets); diff --git a/tests/cpp/tree/hist/test_evaluate_splits.cc b/tests/cpp/tree/hist/test_evaluate_splits.cc index e3123058c38d..f37252df2bb1 100644 --- a/tests/cpp/tree/hist/test_evaluate_splits.cc +++ b/tests/cpp/tree/hist/test_evaluate_splits.cc @@ -10,9 +10,12 @@ #include // for CHECK_EQ #include // for RegTree, RTreeNodeStat +#include +#include #include // for make_shared, shared_ptr, addressof #include // for iota #include // for make_tuple +#include #include "../../../../src/common/hist_util.h" // for HistCollection, HistogramCuts #include "../../../../src/common/random.h" // for ColumnSampler @@ -97,7 +100,8 @@ void TestPartitionBasedSplit::SetUp() { void TestEvaluateSplits(bool force_read_by_column) { Context ctx; ctx.nthread = 4; - int static constexpr kRows = 8, kCols = 16; + static constexpr int kRows = 8; + static constexpr int kCols = 16; auto sampler = std::make_shared(1u); TrainParam param; @@ -107,11 +111,11 @@ void TestEvaluateSplits(bool force_read_by_column) { auto evaluator = HistEvaluator{&ctx, ¶m, dmat->Info(), sampler}; BoundedHistCollection hist; - std::vector row_gpairs = { - {1.23f, 0.24f}, {0.24f, 0.25f}, {0.26f, 0.27f}, {2.27f, 0.28f}, - {0.27f, 0.29f}, {0.37f, 0.39f}, {-0.47f, 0.49f}, {0.57f, 0.59f}}; + std::vector row_gpairs = {{1.23f, 0.24f}, {0.24f, 0.25f}, {0.26f, 0.27f}, + {2.27f, 0.28f}, {0.27f, 0.29f}, {0.37f, 0.39f}, + {-0.47f, 0.49f}, {0.57f, 0.59f}}; - size_t constexpr kMaxBins = 4; + static constexpr size_t kMaxBins = 4; // dense, no missing values GHistIndexMatrix gmat(&ctx, dmat.get(), kMaxBins, 0.5, false); common::RowSetCollection row_set_collection; @@ -141,21 +145,19 @@ void TestEvaluateSplits(bool force_read_by_column) { evaluator.InitRoot(GradStats{total_gpair}); evaluator.EvaluateSplits(hist, gmat.cut, {}, tree, &entries); - auto best_loss_chg = - evaluator.Evaluator().CalcSplitGain( - param, 0, entries.front().split.SplitIndex(), - entries.front().split.left_sum, entries.front().split.right_sum) - - evaluator.Stats().front().root_gain; + auto best_loss_chg = evaluator.Evaluator().CalcSplitGain( + param, 0, entries.front().split.SplitIndex(), + entries.front().split.left_sum, entries.front().split.right_sum) - + evaluator.Stats().front().root_gain; ASSERT_EQ(entries.front().split.loss_chg, best_loss_chg); ASSERT_GT(entries.front().split.loss_chg, 16.2f); // Assert that's the best split for (size_t i = 1; i < gmat.cut.Ptrs().size(); ++i) { GradStats left, right; - for (size_t j = gmat.cut.Ptrs()[i-1]; j < gmat.cut.Ptrs()[i]; ++j) { - auto loss_chg = - evaluator.Evaluator().CalcSplitGain(param, 0, i - 1, left, right) - - evaluator.Stats().front().root_gain; + for (size_t j = gmat.cut.Ptrs()[i - 1]; j < gmat.cut.Ptrs()[i]; ++j) { + auto loss_chg = evaluator.Evaluator().CalcSplitGain(param, 0, i - 1, left, right) - + evaluator.Stats().front().root_gain; ASSERT_GE(best_loss_chg, loss_chg); left.Add(hist[0][j].GetGrad(), hist[0][j].GetHess()); right.SetSubstract(GradStats{total_gpair}, left); @@ -239,7 +241,8 @@ TEST(HistEvaluator, Apply) { Context ctx; ctx.nthread = 4; RegTree tree; - int static constexpr kNRows = 8, kNCols = 16; + static constexpr int kNRows = 8; + static constexpr int kNCols = 16; TrainParam param; param.UpdateAllowUnknown(Args{{"min_child_weight", "0"}, {"reg_lambda", "0.0"}}); auto dmat = RandomDataGenerator(kNRows, kNCols, 0).Seed(3).GenerateDMatrix(); @@ -283,7 +286,8 @@ TEST_F(TestPartitionBasedSplit, CPUHist) { namespace { auto CompareOneHotAndPartition(bool onehot) { Context ctx; - int static constexpr kRows = 128, kCols = 1; + static constexpr int kRows = 128; + static constexpr int kCols = 1; std::vector ft(kCols, FeatureType::kCategorical); TrainParam param; diff --git a/tests/cpp/tree/hist/test_expand_entry.cc b/tests/cpp/tree/hist/test_expand_entry.cc index 23da825205bd..2c0831419e94 100644 --- a/tests/cpp/tree/hist/test_expand_entry.cc +++ b/tests/cpp/tree/hist/test_expand_entry.cc @@ -5,6 +5,8 @@ #include // for Json #include // for RegTree +#include + #include "../../../../src/common/categorical.h" // for CatBitField #include "../../../../src/tree/hist/expand_entry.h" diff --git a/tests/cpp/tree/hist/test_histogram.cc b/tests/cpp/tree/hist/test_histogram.cc index 36da8209d098..26aad4587480 100644 --- a/tests/cpp/tree/hist/test_histogram.cc +++ b/tests/cpp/tree/hist/test_histogram.cc @@ -38,7 +38,8 @@ namespace xgboost::tree { namespace { -void InitRowPartitionForTest(common::RowSetCollection *row_set, size_t n_samples, size_t base_rowid = 0) { +void InitRowPartitionForTest(common::RowSetCollection *row_set, size_t n_samples, + size_t base_rowid = 0) { auto &row_indices = *row_set->Data(); row_indices.resize(n_samples); std::iota(row_indices.begin(), row_indices.end(), base_rowid); @@ -81,7 +82,6 @@ void TestAddHistRows(bool is_distributed) { } } - TEST(CPUHistogram, AddRows) { TestAddHistRows(true); TestAddHistRows(false); @@ -227,11 +227,11 @@ TEST(CPUHistogram, SyncHist) { TestSyncHist(false); } -void TestBuildHistogram(Context const* ctx, bool is_distributed, bool force_read_by_column, bool is_col_split) { +void TestBuildHistogram(Context const *ctx, bool is_distributed, bool force_read_by_column, + bool is_col_split) { size_t constexpr kNRows = 8, kNCols = 16; int32_t constexpr kMaxBins = 4; - auto p_fmat = - RandomDataGenerator(kNRows, kNCols, 0.8).Seed(3).GenerateDMatrix(); + auto p_fmat = RandomDataGenerator(kNRows, kNCols, 0.8).Seed(3).GenerateDMatrix(); if (is_col_split) { p_fmat = std::shared_ptr{ p_fmat->SliceCol(collective::GetWorldSize(), collective::GetRank())}; @@ -241,9 +241,9 @@ void TestBuildHistogram(Context const* ctx, bool is_distributed, bool force_read uint32_t total_bins = gmat.cut.Ptrs().back(); static double constexpr kEps = 1e-6; - std::vector gpair = { - {0.23f, 0.24f}, {0.24f, 0.25f}, {0.26f, 0.27f}, {0.27f, 0.28f}, - {0.27f, 0.29f}, {0.37f, 0.39f}, {0.47f, 0.49f}, {0.57f, 0.59f}}; + std::vector gpair = {{0.23f, 0.24f}, {0.24f, 0.25f}, {0.26f, 0.27f}, + {0.27f, 0.28f}, {0.27f, 0.29f}, {0.37f, 0.39f}, + {0.47f, 0.49f}, {0.57f, 0.59f}}; bst_node_t nid = 0; HistogramBuilder histogram; @@ -315,8 +315,7 @@ TEST(CPUHistogram, BuildHistColumnSplit) { namespace { template -void ValidateCategoricalHistogram(size_t n_categories, - common::Span onehot, +void ValidateCategoricalHistogram(size_t n_categories, common::Span onehot, common::Span cat) { auto cat_sum = std::accumulate(cat.cbegin(), cat.cend(), GradientPairPrecise{}); for (size_t c = 0; c < n_categories; ++c) { diff --git a/tests/cpp/tree/test_column_split.cc b/tests/cpp/tree/test_column_split.cc index 998017a4f589..5c3fa4f804af 100644 --- a/tests/cpp/tree/test_column_split.cc +++ b/tests/cpp/tree/test_column_split.cc @@ -7,6 +7,8 @@ #include // for RegTree #include // for TreeUpdater +#include +#include // for string #include // for vector #include "../../../src/tree/param.h" // for TrainParam diff --git a/tests/cpp/tree/test_column_split.h b/tests/cpp/tree/test_column_split.h index c1edbc811ad7..0387b9ce0369 100644 --- a/tests/cpp/tree/test_column_split.h +++ b/tests/cpp/tree/test_column_split.h @@ -3,13 +3,14 @@ */ #pragma once -#include // for FeatureType, DMatrix +#include // for FeatureType, DMatrix #include // for size_t #include // for shared_ptr +#include // for string #include // for vector -#include "../helpers.h" // for RandomDataGenerator +#include "../helpers.h" // for RandomDataGenerator namespace xgboost::tree { inline std::shared_ptr GenerateCatDMatrix(std::size_t rows, std::size_t cols, diff --git a/tests/cpp/tree/test_common_partitioner.cc b/tests/cpp/tree/test_common_partitioner.cc index 83d2b1f7ce2f..71391731733b 100644 --- a/tests/cpp/tree/test_common_partitioner.cc +++ b/tests/cpp/tree/test_common_partitioner.cc @@ -2,16 +2,16 @@ * Copyright 2022-2024, XGBoost contributors. */ #include -#include // for bst_node_t -#include // for Context +#include // for bst_node_t +#include // for Context -#include // for transform -#include // for distance -#include // for vector +#include // for transform +#include // for distance +#include // for vector -#include "../../../src/common/numeric.h" // for ==RunLengthEncode -#include "../../../src/common/row_set.h" // for RowSetCollection -#include "../../../src/data/gradient_index.h" // for GHistIndexMatrix +#include "../../../src/common/numeric.h" // for ==RunLengthEncode +#include "../../../src/common/row_set.h" // for RowSetCollection +#include "../../../src/data/gradient_index.h" // for GHistIndexMatrix #include "../../../src/tree/common_row_partitioner.h" #include "../../../src/tree/hist/expand_entry.h" // for CPUExpandEntry #include "../helpers.h" // for RandomDataGenerator diff --git a/tests/cpp/tree/test_constraints.cc b/tests/cpp/tree/test_constraints.cc index 4f810102d1a2..d1fa8e4f8624 100644 --- a/tests/cpp/tree/test_constraints.cc +++ b/tests/cpp/tree/test_constraints.cc @@ -7,6 +7,8 @@ #include #include +#include +#include #include "../../../src/tree/constraints.h" #include "../../../src/tree/hist/evaluate_splits.h" @@ -24,8 +26,9 @@ TEST(CPUFeatureInteractionConstraint, Empty) { // no-op constraints.Split(/*node_id=*/0, /*feature_id=*/2, /*left_id=*/1, /*right_id=*/2); - std::vector h_input_feature_list {0, 1, 2, 3, 4, 5}; - common::Span s_input_feature_list = common::Span{h_input_feature_list}; + std::vector h_input_feature_list{0, 1, 2, 3, 4, 5}; + common::Span s_input_feature_list = + common::Span{h_input_feature_list}; for (auto f : h_input_feature_list) { constraints.Query(f, 1); @@ -39,7 +42,7 @@ TEST(CPUFeatureInteractionConstraint, Basic) { std::string const constraints_str = R"constraint([[1, 2], [2, 3, 4]])constraint"; std::vector> args{ - {"interaction_constraints", constraints_str}}; + {"interaction_constraints", constraints_str}}; TrainParam param; param.interaction_constraints = constraints_str; bst_feature_t constexpr kFeatures = 6; diff --git a/tests/cpp/tree/test_constraints.cu b/tests/cpp/tree/test_constraints.cu index 0d19e2325c95..3ae8a5257260 100644 --- a/tests/cpp/tree/test_constraints.cu +++ b/tests/cpp/tree/test_constraints.cu @@ -8,6 +8,7 @@ #include #include #include +#include #include "../../../src/common/device_helpers.cuh" #include "../../../src/tree/constraints.cuh" @@ -21,15 +22,11 @@ struct FConstraintWrapper : public FeatureInteractionConstraintDevice { common::Span GetNodeConstraints() { return FeatureInteractionConstraintDevice::s_node_constraints_; } - FConstraintWrapper(tree::TrainParam param, bst_feature_t n_features) : - FeatureInteractionConstraintDevice(param, n_features) {} + FConstraintWrapper(tree::TrainParam param, bst_feature_t n_features) + : FeatureInteractionConstraintDevice(param, n_features) {} - dh::device_vector const& GetDSets() const { - return d_sets_; - } - dh::device_vector const& GetDSetsPtr() const { - return d_sets_ptr_; - } + dh::device_vector const& GetDSets() const { return d_sets_; } + dh::device_vector const& GetDSetsPtr() const { return d_sets_ptr_; } }; std::string GetConstraintsStr() { @@ -46,12 +43,11 @@ tree::TrainParam GetParameter() { void CompareBitField(LBitField64 d_field, std::set positions) { std::vector h_field_storage(d_field.Bits().size()); - thrust::copy(thrust::device_ptr(d_field.Bits().data()), - thrust::device_ptr( - d_field.Bits().data() + d_field.Bits().size()), - h_field_storage.data()); - LBitField64 h_field{ {h_field_storage.data(), - h_field_storage.data() + h_field_storage.size()} }; + thrust::copy( + thrust::device_ptr(d_field.Bits().data()), + thrust::device_ptr(d_field.Bits().data() + d_field.Bits().size()), + h_field_storage.data()); + LBitField64 h_field{{h_field_storage.data(), h_field_storage.data() + h_field_storage.size()}}; for (size_t i = 0; i < h_field.Capacity(); ++i) { if (positions.find(i) != positions.cend()) { @@ -64,7 +60,6 @@ void CompareBitField(LBitField64 d_field, std::set positions) { } // anonymous namespace - TEST(GPUFeatureInteractionConstraint, Init) { { int32_t constexpr kFeatures = 6; @@ -75,12 +70,10 @@ TEST(GPUFeatureInteractionConstraint, Init) { for (LBitField64 const& d_node : s_nodes_constraints) { std::vector h_node_storage(d_node.Bits().size()); thrust::copy(thrust::device_ptr(d_node.Bits().data()), - thrust::device_ptr( - d_node.Bits().data() + d_node.Bits().size()), + thrust::device_ptr(d_node.Bits().data() + + d_node.Bits().size()), h_node_storage.data()); - LBitField64 h_node { - {h_node_storage.data(), h_node_storage.data() + h_node_storage.size()} - }; + LBitField64 h_node{{h_node_storage.data(), h_node_storage.data() + h_node_storage.size()}}; // no feature is attached to node. for (size_t i = 0; i < h_node.Capacity(); ++i) { ASSERT_FALSE(h_node.Check(i)); @@ -94,8 +87,8 @@ TEST(GPUFeatureInteractionConstraint, Init) { tree::TrainParam param = GetParameter(); param.interaction_constraints = R"([[0, 1, 3], [3, 5, 6]])"; FConstraintWrapper constraints(param, kFeatures); - std::vector h_sets {0, 0, 0, 1, 1, 1}; - std::vector h_sets_ptr {0, 1, 2, 2, 4, 4, 5, 6}; + std::vector h_sets{0, 0, 0, 1, 1, 1}; + std::vector h_sets_ptr{0, 1, 2, 2, 4, 4, 5, 6}; auto d_sets = constraints.GetDSets(); ASSERT_EQ(h_sets.size(), d_sets.size()); auto d_sets_ptr = constraints.GetDSetsPtr(); @@ -120,7 +113,7 @@ TEST(GPUFeatureInteractionConstraint, Init) { auto _128_end = d_sets_ptr[128 + 1]; ASSERT_EQ(_128_end - _128_beg, 2); ASSERT_EQ(d_sets[_128_beg], 1); - ASSERT_EQ(d_sets[_128_end-1], 2); + ASSERT_EQ(d_sets[_128_end - 1], 2); } } @@ -165,12 +158,11 @@ TEST(GPUFeatureInteractionConstraint, QueryNode) { } { - constraints.Split(/*node_id=*/ 0, /*feature_id=*/ 1, 1, 2); + constraints.Split(/*node_id=*/0, /*feature_id=*/1, 1, 2); auto span = constraints.QueryNode(&ctx, 0); - std::vector h_result (span.size()); + std::vector h_result(span.size()); thrust::copy(thrust::device_ptr(span.data()), - thrust::device_ptr(span.data() + span.size()), - h_result.begin()); + thrust::device_ptr(span.data() + span.size()), h_result.begin()); ASSERT_EQ(h_result.size(), 2); ASSERT_EQ(h_result[0], 1); ASSERT_EQ(h_result[1], 2); @@ -179,10 +171,9 @@ TEST(GPUFeatureInteractionConstraint, QueryNode) { { constraints.Split(1, /*feature_id=*/0, 3, 4); auto span = constraints.QueryNode(&ctx, 1); - std::vector h_result (span.size()); + std::vector h_result(span.size()); thrust::copy(thrust::device_ptr(span.data()), - thrust::device_ptr(span.data() + span.size()), - h_result.begin()); + thrust::device_ptr(span.data() + span.size()), h_result.begin()); ASSERT_EQ(h_result.size(), 3); ASSERT_EQ(h_result[0], 0); ASSERT_EQ(h_result[1], 1); @@ -192,8 +183,7 @@ TEST(GPUFeatureInteractionConstraint, QueryNode) { span = constraints.QueryNode(&ctx, 3); h_result.resize(span.size()); thrust::copy(thrust::device_ptr(span.data()), - thrust::device_ptr(span.data() + span.size()), - h_result.begin()); + thrust::device_ptr(span.data() + span.size()), h_result.begin()); ASSERT_EQ(h_result.size(), 3); ASSERT_EQ(h_result[0], 0); ASSERT_EQ(h_result[1], 1); @@ -206,10 +196,9 @@ TEST(GPUFeatureInteractionConstraint, QueryNode) { FConstraintWrapper large_features(large_param, 256); large_features.Split(0, 139, 1, 2); auto span = large_features.QueryNode(&ctx, 0); - std::vector h_result (span.size()); + std::vector h_result(span.size()); thrust::copy(thrust::device_ptr(span.data()), - thrust::device_ptr(span.data() + span.size()), - h_result.begin()); + thrust::device_ptr(span.data() + span.size()), h_result.begin()); ASSERT_EQ(h_result.size(), 3); ASSERT_EQ(h_result[0], 1); ASSERT_EQ(h_result[1], 139); @@ -234,8 +223,8 @@ TEST(GPUFeatureInteractionConstraint, Query) { tree::TrainParam param = GetParameter(); bst_feature_t constexpr kFeatures = 6; FConstraintWrapper constraints(param, kFeatures); - std::vector h_input_feature_list {0, 1, 2, 3, 4, 5}; - dh::device_vector d_input_feature_list (h_input_feature_list); + std::vector h_input_feature_list{0, 1, 2, 3, 4, 5}; + dh::device_vector d_input_feature_list(h_input_feature_list); common::Span s_input_feature_list = dh::ToSpan(d_input_feature_list); auto s_output = constraints.Query(s_input_feature_list, 0); @@ -263,8 +252,8 @@ TEST(GPUFeatureInteractionConstraint, Query) { * */ - std::vector h_input_feature_list {0, 1, 2, 3, 4, 5}; - dh::device_vector d_input_feature_list (h_input_feature_list); + std::vector h_input_feature_list{0, 1, 2, 3, 4, 5}; + dh::device_vector d_input_feature_list(h_input_feature_list); common::Span s_input_feature_list = dh::ToSpan(d_input_feature_list); auto s_output = constraints.Query(s_input_feature_list, 1); @@ -291,8 +280,8 @@ TEST(GPUFeatureInteractionConstraint, Query) { FConstraintWrapper constraints(param, kFeatures); constraints.Split(/*node_id=*/0, /*feature_id=*/2, /*left_id=*/1, /*right_id=*/2); - std::vector h_input_feature_list {0, 1, 2, 3, 4, 5}; - dh::device_vector d_input_feature_list (h_input_feature_list); + std::vector h_input_feature_list{0, 1, 2, 3, 4, 5}; + dh::device_vector d_input_feature_list(h_input_feature_list); common::Span s_input_feature_list = dh::ToSpan(d_input_feature_list); auto s_output = constraints.Query(s_input_feature_list, 1); @@ -306,8 +295,8 @@ TEST(GPUFeatureInteractionConstraint, Query) { std::string const constraints_str = R"constraint([[0, 1]])constraint"; param.interaction_constraints = constraints_str; FConstraintWrapper constraints(param, kFeatures); - std::vector h_input_feature_list {0, 1, 2, 3, 4, 5}; - dh::device_vector d_input_feature_list (h_input_feature_list); + std::vector h_input_feature_list{0, 1, 2, 3, 4, 5}; + dh::device_vector d_input_feature_list(h_input_feature_list); common::Span s_input_feature_list = dh::ToSpan(d_input_feature_list); constraints.Split(/*node_id=*/0, /*feature_id=*/2, /*left_id=*/1, /*right_id=*/2); auto s_output = constraints.Query(s_input_feature_list, 1); diff --git a/tests/cpp/tree/test_evaluate_splits.h b/tests/cpp/tree/test_evaluate_splits.h index bc4b70946d16..3e055af99389 100644 --- a/tests/cpp/tree/test_evaluate_splits.h +++ b/tests/cpp/tree/test_evaluate_splits.h @@ -1,6 +1,8 @@ /** * Copyright 2022-2024, XGBoost Contributors */ +#ifndef TESTS_CPP_TREE_TEST_EVALUATE_SPLITS_H_ +#define TESTS_CPP_TREE_TEST_EVALUATE_SPLITS_H_ #include #include // for GradientPairInternal, GradientPairPrecise #include // for MetaInfo @@ -89,4 +91,7 @@ class TestCategoricalSplitWithMissing : public testing::Test { ASSERT_EQ(right_sum.GetHess(), parent_sum_.GetHess() - left_sum.GetHess()); } }; + } // namespace xgboost::tree + +#endif // TESTS_CPP_TREE_TEST_EVALUATE_SPLITS_H_ diff --git a/tests/cpp/tree/test_gpu_approx.cu b/tests/cpp/tree/test_gpu_approx.cu index 295d79c0a477..93494327649b 100644 --- a/tests/cpp/tree/test_gpu_approx.cu +++ b/tests/cpp/tree/test_gpu_approx.cu @@ -8,6 +8,9 @@ #include // for RegTree #include // for TreeUpdater +#include +#include + #include "../../../src/tree/param.h" // for TrainParam #include "../collective/test_worker.h" // for BaseMGPUTest #include "../helpers.h" // for GenerateRandomGradients diff --git a/tests/cpp/tree/test_gpu_hist.cu b/tests/cpp/tree/test_gpu_hist.cu index 2ffdc12f1cc1..44cdec8b2b3d 100644 --- a/tests/cpp/tree/test_gpu_hist.cu +++ b/tests/cpp/tree/test_gpu_hist.cu @@ -22,8 +22,8 @@ namespace xgboost::tree { namespace { -void UpdateTree(Context const* ctx, GradientContainer* gpair, DMatrix* dmat, - RegTree* tree, HostDeviceVector* preds, float subsample, +void UpdateTree(Context const* ctx, GradientContainer* gpair, DMatrix* dmat, RegTree* tree, + HostDeviceVector* preds, float subsample, const std::string& sampling_method, bst_bin_t max_bin) { Args args{ {"max_depth", "2"}, @@ -217,13 +217,13 @@ TEST(GpuHist, MaxDepth) { learner->SetParam("max_depth", "32"); learner->Configure(); - ASSERT_THROW({learner->UpdateOneIter(0, p_mat);}, dmlc::Error); + ASSERT_THROW({ learner->UpdateOneIter(0, p_mat); }, dmlc::Error); } namespace { RegTree GetHistTree(Context const* ctx, DMatrix* dmat) { ObjInfo task{ObjInfo::kRegression}; - std::unique_ptr hist_maker {TreeUpdater::Create("grow_gpu_hist", ctx, &task)}; + std::unique_ptr hist_maker{TreeUpdater::Create("grow_gpu_hist", ctx, &task)}; hist_maker->Configure(Args{}); TrainParam param; diff --git a/tests/cpp/tree/test_multi_target_tree_model.cc b/tests/cpp/tree/test_multi_target_tree_model.cc index 88118ca31136..72c4fcaa5619 100644 --- a/tests/cpp/tree/test_multi_target_tree_model.cc +++ b/tests/cpp/tree/test_multi_target_tree_model.cc @@ -11,6 +11,8 @@ #include // for unique_ptr #include // for iota +#include // for string +#include // for vector #include "../../../src/tree/tree_view.h" diff --git a/tests/cpp/tree/test_param.cc b/tests/cpp/tree/test_param.cc index d4194bb74c58..9ff9aec2e506 100644 --- a/tests/cpp/tree/test_param.cc +++ b/tests/cpp/tree/test_param.cc @@ -1,7 +1,10 @@ // Copyright by Contributors +#include + +#include + #include "../../../src/tree/param.h" #include "../helpers.h" -#include TEST(Param, VectorIOStream) { std::vector vals = {3, 2, 1}; @@ -14,7 +17,10 @@ TEST(Param, VectorIOStream) { ss >> vals_in; EXPECT_EQ(vals_in, vals); - vals.clear(); ss.flush(); ss.clear(); ss.str(""); + vals.clear(); + ss.flush(); + ss.clear(); + ss.str(""); vals = {1}; ss << vals; EXPECT_EQ(ss.str(), "(1,)"); @@ -25,57 +31,90 @@ TEST(Param, VectorStreamRead) { std::stringstream ss; std::vector vals_in; - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << "(3, 2, 1)"; ss >> vals_in; EXPECT_EQ(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << "(3L,2L,1L)"; ss >> vals_in; EXPECT_EQ(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << " (3,2,1,)"; ss >> vals_in; EXPECT_EQ(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << " ( 3, 2,1 )"; ss >> vals_in; EXPECT_EQ(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << " ( 3, 2,1 ) "; ss >> vals_in; EXPECT_EQ(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << " 321 "; ss >> vals_in; EXPECT_EQ(vals_in[0], 321); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << "(3.0,2,1)"; ss >> vals_in; EXPECT_NE(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << "1a"; ss >> vals_in; EXPECT_NE(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << "abcde"; ss >> vals_in; EXPECT_NE(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); ss << "(3,2,1"; ss >> vals_in; EXPECT_NE(vals_in, vals); - vals_in.clear(); ss.flush(); ss.clear(); ss.str(""); + vals_in.clear(); + ss.flush(); + ss.clear(); + ss.str(""); vals_in.emplace_back(3); ss << "( )"; ss >> vals_in; @@ -88,15 +127,14 @@ TEST(Param, SplitEntry) { xgboost::tree::SplitEntry se2; EXPECT_FALSE(se1.Update(se2)); - EXPECT_FALSE(se2.Update(-1, 100, 0, true, false, xgboost::tree::GradStats(), - xgboost::tree::GradStats())); - ASSERT_TRUE(se2.Update(1, 100, 0, true, false, xgboost::tree::GradStats(), - xgboost::tree::GradStats())); + EXPECT_FALSE( + se2.Update(-1, 100, 0, true, false, xgboost::tree::GradStats(), xgboost::tree::GradStats())); + ASSERT_TRUE( + se2.Update(1, 100, 0, true, false, xgboost::tree::GradStats(), xgboost::tree::GradStats())); ASSERT_TRUE(se1.Update(se2)); xgboost::tree::SplitEntry se3; - se3.Update(2, 101, 0, false, false, xgboost::tree::GradStats(), - xgboost::tree::GradStats()); + se3.Update(2, 101, 0, false, false, xgboost::tree::GradStats(), xgboost::tree::GradStats()); xgboost::tree::SplitEntry::Reduce(se2, se3); EXPECT_EQ(se2.SplitIndex(), 101); EXPECT_FALSE(se2.DefaultLeft()); diff --git a/tests/cpp/tree/test_partitioner.h b/tests/cpp/tree/test_partitioner.h index 4afeb540670d..92f554ce6175 100644 --- a/tests/cpp/tree/test_partitioner.h +++ b/tests/cpp/tree/test_partitioner.h @@ -1,14 +1,14 @@ /** * Copyright 2021-2026 by XGBoost contributors. */ -#ifndef XGBOOST_TESTS_CPP_TREE_TEST_PARTITIONER_H_ -#define XGBOOST_TESTS_CPP_TREE_TEST_PARTITIONER_H_ -#include // for Context -#include // for Constant, Vector -#include // for CHECK -#include // for RegTree +#ifndef TESTS_CPP_TREE_TEST_PARTITIONER_H_ +#define TESTS_CPP_TREE_TEST_PARTITIONER_H_ +#include // for Context +#include // for Constant, Vector +#include // for CHECK +#include // for RegTree -#include // for vector +#include // for vector #include "../../../src/tree/hist/expand_entry.h" // for CPUExpandEntry, MultiExpandEntry @@ -44,4 +44,4 @@ inline void GetMultiSplitForTest(RegTree *tree, float split_value, tree->GetMultiTargetTree()->SetLeaves(); } } // namespace xgboost::tree -#endif // XGBOOST_TESTS_CPP_TREE_TEST_PARTITIONER_H_ +#endif // TESTS_CPP_TREE_TEST_PARTITIONER_H_ diff --git a/tests/cpp/tree/test_prediction_cache.cc b/tests/cpp/tree/test_prediction_cache.cc index cd1aeef7f706..fb4b5725ff65 100644 --- a/tests/cpp/tree/test_prediction_cache.cc +++ b/tests/cpp/tree/test_prediction_cache.cc @@ -1,10 +1,10 @@ /** * Copyright 2021-2023 by XGBoost contributors */ -#include - #include "test_prediction_cache.h" +#include + namespace xgboost { TEST_F(TestPredictionCache, Approx) { Context ctx; diff --git a/tests/cpp/tree/test_prediction_cache.h b/tests/cpp/tree/test_prediction_cache.h index 39073360de32..d3be815b8b84 100644 --- a/tests/cpp/tree/test_prediction_cache.h +++ b/tests/cpp/tree/test_prediction_cache.h @@ -4,15 +4,16 @@ #pragma once #include - #include #include #include +#include +#include #include "../../../src/tree/param.h" // for TrainParam #include "../helpers.h" -#include "xgboost/task.h" // for ObjInfo +#include "xgboost/task.h" // for ObjInfo namespace xgboost { class TestPredictionCache : public ::testing::Test { diff --git a/tests/cpp/tree/test_prune.cc b/tests/cpp/tree/test_prune.cc index 0b8e3258c9cd..64acc8ca27cb 100644 --- a/tests/cpp/tree/test_prune.cc +++ b/tests/cpp/tree/test_prune.cc @@ -26,14 +26,21 @@ TEST(Updater, Prune) { // These data are just place holders. GradientContainer gpair; - gpair.gpair = linalg::Matrix - {{ {0.50f, 0.25f}, {0.50f, 0.25f}, {0.50f, 0.25f}, {0.50f, 0.25f}, - {0.25f, 0.24f}, {0.25f, 0.24f}, {0.25f, 0.24f}, {0.25f, 0.24f} }, {8, 1}, ctx.Device()}; + gpair.gpair = linalg::Matrix{{{0.50f, 0.25f}, + {0.50f, 0.25f}, + {0.50f, 0.25f}, + {0.50f, 0.25f}, + {0.25f, 0.24f}, + {0.25f, 0.24f}, + {0.25f, 0.24f}, + {0.25f, 0.24f}}, + {8, 1}, + ctx.Device()}; std::shared_ptr p_dmat{RandomDataGenerator{32, 10, 0}.GenerateDMatrix()}; // prepare tree RegTree tree = RegTree{1u, kCols}; - std::vector trees {&tree}; + std::vector trees{&tree}; // prepare pruner TrainParam param; param.UpdateAllowUnknown(cfg); @@ -64,12 +71,10 @@ TEST(Updater, Prune) { // Test depth // loss_chg > min_split_loss - tree.ExpandNode(tree[0].LeftChild(), - 0, 0.5f, true, 0.3, 0.4, 0.5, + tree.ExpandNode(tree[0].LeftChild(), 0, 0.5f, true, 0.3, 0.4, 0.5, /*loss_chg=*/18.0f, 0.0f, /*left_sum=*/0.0f, /*right_sum=*/0.0f); - tree.ExpandNode(tree[0].RightChild(), - 0, 0.5f, true, 0.3, 0.4, 0.5, + tree.ExpandNode(tree[0].RightChild(), 0, 0.5f, true, 0.3, 0.4, 0.5, /*loss_chg=*/19.0f, 0.0f, /*left_sum=*/0.0f, /*right_sum=*/0.0f); @@ -78,8 +83,7 @@ TEST(Updater, Prune) { pruner->Update(¶m, &gpair, p_dmat.get(), position, trees); ASSERT_EQ(tree.NumExtraNodes(), 2); - tree.ExpandNode(tree[0].LeftChild(), - 0, 0.5f, true, 0.3, 0.4, 0.5, + tree.ExpandNode(tree[0].LeftChild(), 0, 0.5f, true, 0.3, 0.4, 0.5, /*loss_chg=*/18.0f, 0.0f, /*left_sum=*/0.0f, /*right_sum=*/0.0f); cfg.emplace_back("min_split_loss", "0"); diff --git a/tests/cpp/tree/test_refresh.cc b/tests/cpp/tree/test_refresh.cc index 01052861ab03..6bd6049bd67d 100644 --- a/tests/cpp/tree/test_refresh.cc +++ b/tests/cpp/tree/test_refresh.cc @@ -33,11 +33,9 @@ TEST(Updater, Refresh) { ctx.Device()}; std::shared_ptr p_dmat{ - RandomDataGenerator{kRows, kCols, 0.4f}.Seed(3).GenerateDMatrix()}; + RandomDataGenerator{kRows, kCols, 0.4f}.Seed(3).GenerateDMatrix()}; std::vector> cfg{ - {"reg_alpha", "0.0"}, - {"num_feature", std::to_string(kCols)}, - {"reg_lambda", "1"}}; + {"reg_alpha", "0.0"}, {"num_feature", std::to_string(kCols)}, {"reg_lambda", "1"}}; RegTree tree = RegTree{1u, kCols}; std::vector trees{&tree}; diff --git a/tests/cpp/tree/test_regen.cc b/tests/cpp/tree/test_regen.cc index 837159329761..f0834d2339b5 100644 --- a/tests/cpp/tree/test_regen.cc +++ b/tests/cpp/tree/test_regen.cc @@ -3,6 +3,10 @@ */ #include +#include +#include +#include + #include "../../../src/data/adapter.h" #include "../../../src/data/simple_dmatrix.h" #include "../helpers.h" diff --git a/tests/cpp/tree/test_tree_model.cc b/tests/cpp/tree/test_tree_model.cc index 0d8e05e8f183..74572c5e1dd0 100644 --- a/tests/cpp/tree/test_tree_model.cc +++ b/tests/cpp/tree/test_tree_model.cc @@ -3,7 +3,10 @@ */ #include +#include #include // for stack +#include +#include #include "../../../src/common/bitfield.h" #include "../../../src/common/categorical.h" @@ -78,8 +81,8 @@ TEST(Tree, ExpandCategoricalFeature) { { RegTree tree; bst_cat_t cat = 33; - std::vector split_cats(LBitField32::ComputeStorageSize(cat+1)); - LBitField32 bitset {split_cats}; + std::vector split_cats(LBitField32::ComputeStorageSize(cat + 1)); + LBitField32 bitset{split_cats}; bitset.Set(cat); tree.ExpandCategorical(0, 0, split_cats, true, 1.0, 2.0, 3.0, 11.0, 2.0, /*left_sum=*/3.0, /*right_sum=*/4.0); @@ -126,8 +129,7 @@ void GrowTree(RegTree* p_tree) { bst_feature_t f = feat(&lcg); if (is_cat) { bst_cat_t cat = common::AsCat(split_cat(&lcg)); - std::vector split_cats( - LBitField32::ComputeStorageSize(cat + 1)); + std::vector split_cats(LBitField32::ComputeStorageSize(cat + 1)); LBitField32 bitset{split_cats}; bitset.Set(cat); tree.ExpandCategorical(node, f, split_cats, true, 1.0, 2.0, 3.0, 11.0, 2.0, @@ -143,7 +145,7 @@ void GrowTree(RegTree* p_tree) { } } -void CheckReload(RegTree const &tree) { +void CheckReload(RegTree const& tree) { Json out{Object()}; tree.SaveModel(&out); @@ -207,16 +209,16 @@ RegTree ConstructTreeCat(std::vector* cond) { cond->push_back(14); cond->push_back(32); - tree.ExpandCategorical(0, /*split_index=*/0, cats_storage, true, 0.0f, 2.0, - 3.00, 11.0, 2.0, 3.0, 4.0); + tree.ExpandCategorical(0, /*split_index=*/0, cats_storage, true, 0.0f, 2.0, 3.00, 11.0, 2.0, 3.0, + 4.0); auto left = tree[0].LeftChild(); auto right = tree[0].RightChild(); tree.ExpandNode( /*nid=*/left, /*split_index=*/1, /*split_value=*/1.0f, /*default_left=*/false, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, /*left_sum=*/0.0f, /*right_sum=*/0.0f); - tree.ExpandCategorical(right, /*split_index=*/0, cats_storage, true, 0.0f, - 2.0, 3.00, 11.0, 2.0, 3.0, 4.0); + tree.ExpandCategorical(right, /*split_index=*/0, cats_storage, true, 0.0f, 2.0, 3.00, 11.0, 2.0, + 3.0, 4.0); return tree; } @@ -286,14 +288,11 @@ TEST(Tree, DumpJson) { str = tree.DumpModel(fmap, false, "json"); ASSERT_EQ(str.find("cover"), std::string::npos); - auto j_tree = Json::Load({str.c_str(), str.size()}); ASSERT_EQ(get(j_tree["children"]).size(), 2ul); } -TEST(Tree, DumpJsonCategorical) { - TestCategoricalTreeDump("json", ", "); -} +TEST(Tree, DumpJsonCategorical) { TestCategoricalTreeDump("json", ", "); } TEST(Tree, DumpText) { auto tree = ConstructTree(); @@ -330,9 +329,7 @@ TEST(Tree, DumpText) { ASSERT_EQ(str.find("cover"), std::string::npos); } -TEST(Tree, DumpTextCategorical) { - TestCategoricalTreeDump("text", ","); -} +TEST(Tree, DumpTextCategorical) { TestCategoricalTreeDump("text", ","); } TEST(Tree, DumpDot) { auto tree = ConstructTree(); @@ -372,9 +369,7 @@ TEST(Tree, DumpDot) { ASSERT_NE(str.find(R"(1 -> 4 [label="no, missing")"), std::string::npos); } -TEST(Tree, DumpDotCategorical) { - TestCategoricalTreeDump("dot", ","); -} +TEST(Tree, DumpDotCategorical) { TestCategoricalTreeDump("dot", ","); } TEST(Tree, JsonIO) { RegTree tree; diff --git a/tests/cpp/tree/test_tree_policy.cc b/tests/cpp/tree/test_tree_policy.cc index 6baaf9635a77..91c95aeb4efa 100644 --- a/tests/cpp/tree/test_tree_policy.cc +++ b/tests/cpp/tree/test_tree_policy.cc @@ -6,6 +6,7 @@ #include // for Context #include +#include #include // for unique_ptr #include // for string diff --git a/tests/cpp/tree/test_tree_stat.cc b/tests/cpp/tree/test_tree_stat.cc index 0acd987b4871..b7c01f50e819 100644 --- a/tests/cpp/tree/test_tree_stat.cc +++ b/tests/cpp/tree/test_tree_stat.cc @@ -9,6 +9,8 @@ #include // for TreeUpdater #include // for unique_ptr +#include +#include #include "../../../src/tree/io_utils.h" // for DftBadValue #include "../../../src/tree/param.h" // for TrainParam @@ -415,7 +417,8 @@ class TestMaxDeltaStep : public ::testing::Test { RegTree tree_0{static_cast(gpairs.gpair.Shape(1)), static_cast(p_fmat->Info().num_col_)}; - BuildTree(ctx, p_fmat.get(), &gpairs, updater, Args{{"max_delta_step", std::to_string(0.5)}}, &tree_0); + BuildTree(ctx, p_fmat.get(), &gpairs, updater, Args{{"max_delta_step", std::to_string(0.5)}}, + &tree_0); ASSERT_EQ(tree_0.NumNodes(), 1); } }; diff --git a/tests/test_distributed/test_gpu_with_dask/__init__.py b/tests/test_distributed/test_gpu_with_dask/__init__.py index 8b137891791f..e69de29bb2d1 100644 --- a/tests/test_distributed/test_gpu_with_dask/__init__.py +++ b/tests/test_distributed/test_gpu_with_dask/__init__.py @@ -1 +0,0 @@ - diff --git a/tests/test_distributed/test_with_dask/__init__.py b/tests/test_distributed/test_with_dask/__init__.py index 8b137891791f..e69de29bb2d1 100644 --- a/tests/test_distributed/test_with_dask/__init__.py +++ b/tests/test_distributed/test_with_dask/__init__.py @@ -1 +0,0 @@ -