Skip to content

Commit

Permalink
Merge branch 'master' into fix/vs-omp
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Jan 6, 2024
2 parents 95508b9 + 48e3629 commit 742d4a4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 51 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ jobs:
action:
runs-on: ubuntu-latest
steps:
- uses: dessant/lock-threads@v4
- uses: dessant/lock-threads@v5
with:
github-token: ${{ github.token }}
# after how many days of inactivity should a closed issue/PR be locked?
issue-inactive-days: '90'
pr-inactive-days: '90'
issue-inactive-days: '365'
pr-inactive-days: '365'
# do not close feature request issues...
# we close those but track them in https://github.com/microsoft/LightGBM/issues/2302
exclude-any-issue-labels: '"feature request"'
exclude-any-issue-labels: 'feature request'
# what labels should be removed prior to locking?
remove-issue-labels: 'awaiting response,awaiting review,blocking,in progress'
remove-pr-labels: 'awaiting response,awaiting review,blocking,in progress'
Expand All @@ -42,3 +42,4 @@ jobs:
# what shoulld the locking status be?
issue-lock-reason: 'resolved'
pr-lock-reason: 'resolved'
process-only: 'issues, prs'
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ endif()

project(lightgbm LANGUAGES C CXX)

if(BUILD_CPP_TEST)
set(CMAKE_CXX_STANDARD 14)
else()
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")

#-- Sanitizer
Expand Down Expand Up @@ -329,7 +336,7 @@ endif()
if(UNIX OR MINGW OR CYGWIN)
set(
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Wextra -Wall -Wno-ignored-attributes -Wno-unknown-pragmas -Wno-return-type"
"${CMAKE_CXX_FLAGS} -pthread -Wextra -Wall -Wno-ignored-attributes -Wno-unknown-pragmas -Wno-return-type"
)
if(MINGW)
# ignore this warning: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95353
Expand Down Expand Up @@ -630,7 +637,7 @@ if(BUILD_CPP_TEST)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
GIT_TAG v1.14.0
)
FetchContent_MakeAvailable(googletest)
add_library(GTest::GTest ALIAS gtest)
Expand Down
61 changes: 21 additions & 40 deletions R-package/src/lightgbm_R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,19 @@ SEXP LGBM_DatasetGetSubset_R(SEXP handle,
_AssertDatasetHandleNotNull(handle);
SEXP ret = PROTECT(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue));
int32_t len = static_cast<int32_t>(Rf_asInteger(len_used_row_indices));
std::vector<int32_t> idxvec(len);
std::unique_ptr<int32_t[]> idxvec(new int32_t[len]);
// convert from one-based to zero-based index
const int *used_row_indices_ = INTEGER(used_row_indices);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (len >= 1024)
#ifndef _MSC_VER
#pragma omp simd
#endif
for (int32_t i = 0; i < len; ++i) {
idxvec[i] = static_cast<int32_t>(used_row_indices_[i] - 1);
}
const char* parameters_ptr = CHAR(PROTECT(Rf_asChar(parameters)));
DatasetHandle res = nullptr;
CHECK_CALL(LGBM_DatasetGetSubset(R_ExternalPtrAddr(handle),
idxvec.data(), len, parameters_ptr,
idxvec.get(), len, parameters_ptr,
&res));
R_SetExternalPtrAddr(ret, res);
R_RegisterCFinalizerEx(ret, _DatasetFinalizer, TRUE);
Expand All @@ -248,13 +250,13 @@ SEXP LGBM_DatasetSetFeatureNames_R(SEXP handle,
R_API_BEGIN();
_AssertDatasetHandleNotNull(handle);
auto vec_names = Split(CHAR(PROTECT(Rf_asChar(feature_names))), '\t');
std::vector<const char*> vec_sptr;
int len = static_cast<int>(vec_names.size());
std::unique_ptr<const char*[]> vec_sptr(new const char*[len]);
for (int i = 0; i < len; ++i) {
vec_sptr.push_back(vec_names[i].c_str());
vec_sptr[i] = vec_names[i].c_str();
}
CHECK_CALL(LGBM_DatasetSetFeatureNames(R_ExternalPtrAddr(handle),
vec_sptr.data(), len));
vec_sptr.get(), len));
UNPROTECT(1);
return R_NilValue;
R_API_END();
Expand Down Expand Up @@ -339,23 +341,13 @@ SEXP LGBM_DatasetSetField_R(SEXP handle,
int len = Rf_asInteger(num_element);
const char* name = CHAR(PROTECT(Rf_asChar(field_name)));
if (!strcmp("group", name) || !strcmp("query", name)) {
std::vector<int32_t> vec(len);
const int *field_data_ = INTEGER(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (len >= 1024)
for (int i = 0; i < len; ++i) {
vec[i] = static_cast<int32_t>(field_data_[i]);
}
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, vec.data(), len, C_API_DTYPE_INT32));
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, INTEGER(field_data), len, C_API_DTYPE_INT32));
} else if (!strcmp("init_score", name)) {
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, REAL(field_data), len, C_API_DTYPE_FLOAT64));
} else {
std::vector<float> vec(len);
const double *field_data_ = REAL(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (len >= 1024)
for (int i = 0; i < len; ++i) {
vec[i] = static_cast<float>(field_data_[i]);
}
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, vec.data(), len, C_API_DTYPE_FLOAT32));
std::unique_ptr<float[]> vec(new float[len]);
std::copy(REAL(field_data), REAL(field_data) + len, vec.get());
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, vec.get(), len, C_API_DTYPE_FLOAT32));
}
UNPROTECT(1);
return R_NilValue;
Expand All @@ -376,24 +368,18 @@ SEXP LGBM_DatasetGetField_R(SEXP handle,
auto p_data = reinterpret_cast<const int32_t*>(res);
// convert from boundaries to size
int *field_data_ = INTEGER(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (out_len >= 1024)
#ifndef _MSC_VER
#pragma omp simd
#endif
for (int i = 0; i < out_len - 1; ++i) {
field_data_[i] = p_data[i + 1] - p_data[i];
}
} else if (!strcmp("init_score", name)) {
auto p_data = reinterpret_cast<const double*>(res);
double *field_data_ = REAL(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (out_len >= 1024)
for (int i = 0; i < out_len; ++i) {
field_data_[i] = p_data[i];
}
std::copy(p_data, p_data + out_len, REAL(field_data));
} else {
auto p_data = reinterpret_cast<const float*>(res);
double *field_data_ = REAL(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (out_len >= 1024)
for (int i = 0; i < out_len; ++i) {
field_data_[i] = p_data[i];
}
std::copy(p_data, p_data + out_len, REAL(field_data));
}
UNPROTECT(1);
return R_NilValue;
Expand Down Expand Up @@ -616,15 +602,10 @@ SEXP LGBM_BoosterUpdateOneIterCustom_R(SEXP handle,
_AssertBoosterHandleNotNull(handle);
int is_finished = 0;
int int_len = Rf_asInteger(len);
std::vector<float> tgrad(int_len), thess(int_len);
const double *grad_ = REAL(grad);
const double *hess_ = REAL(hess);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (int_len >= 1024)
for (int j = 0; j < int_len; ++j) {
tgrad[j] = static_cast<float>(grad_[j]);
thess[j] = static_cast<float>(hess_[j]);
}
CHECK_CALL(LGBM_BoosterUpdateOneIterCustom(R_ExternalPtrAddr(handle), tgrad.data(), thess.data(), &is_finished));
std::unique_ptr<float[]> tgrad(new float[int_len]), thess(new float[int_len]);
std::copy(REAL(grad), REAL(grad) + int_len, tgrad.get());
std::copy(REAL(hess), REAL(hess) + int_len, thess.get());
CHECK_CALL(LGBM_BoosterUpdateOneIterCustom(R_ExternalPtrAddr(handle), tgrad.get(), thess.get(), &is_finished));
return R_NilValue;
R_API_END();
}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ External (Unofficial) Repositories
Projects listed here offer alternative ways to use LightGBM.
They are not maintained or officially endorsed by the `LightGBM` development team.

LightGBMLSS (An extension of LightGBM to probabilistic modelling from which prediction intervals and quantiles can be derived): https://github.com/StatMixedML/LightGBMLSS

FLAML (AutoML library for hyperparameter optimization): https://github.com/microsoft/FLAML

Optuna (hyperparameter optimization framework): https://github.com/optuna/optuna
Expand Down
10 changes: 5 additions & 5 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def _data_from_pandas(
feature_name: _LGBM_FeatureNameConfiguration,
categorical_feature: _LGBM_CategoricalFeatureConfiguration,
pandas_categorical: Optional[List[List]]
) -> Tuple[np.ndarray, List[str], List[str], List[List]]:
) -> Tuple[np.ndarray, List[str], Union[List[str], List[int]], List[List]]:
if len(data.shape) != 2 or data.shape[0] < 1:
raise ValueError('Input data must be 2 dimensional and non empty.')

Expand All @@ -800,7 +800,7 @@ def _data_from_pandas(

# determine categorical features
cat_cols = [col for col, dtype in zip(data.columns, data.dtypes) if isinstance(dtype, pd_CategoricalDtype)]
cat_cols_not_ordered = [col for col in cat_cols if not data[col].cat.ordered]
cat_cols_not_ordered: List[str] = [col for col in cat_cols if not data[col].cat.ordered]
if pandas_categorical is None: # train dataset
pandas_categorical = [list(data[col].cat.categories) for col in cat_cols]
else:
Expand All @@ -811,10 +811,10 @@ def _data_from_pandas(
data[col] = data[col].cat.set_categories(category)
if len(cat_cols): # cat_cols is list
data[cat_cols] = data[cat_cols].apply(lambda x: x.cat.codes).replace({-1: np.nan})
if categorical_feature == 'auto': # use cat cols from DataFrame

# use cat cols from DataFrame
if categorical_feature == 'auto':
categorical_feature = cat_cols_not_ordered
else: # use cat cols specified by user
categorical_feature = list(categorical_feature) # type: ignore[assignment]

df_dtypes = [dtype.type for dtype in data.dtypes]
# so that the target dtype considers floats
Expand Down

0 comments on commit 742d4a4

Please sign in to comment.