Skip to content

Commit

Permalink
[R-package] limit number of threads used in tests and examples (fixes #…
Browse files Browse the repository at this point in the history
…5102, fixes #5987)
  • Loading branch information
jameslamb committed Jul 18, 2023
1 parent 7d4d897 commit 934f0d0
Show file tree
Hide file tree
Showing 31 changed files with 75 additions and 10 deletions.
12 changes: 6 additions & 6 deletions R-package/R/callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CB_ENV <- R6::R6Class(
)

# Format the evaluation metric string
format.eval.string <- function(eval_res, eval_err) {
.format_eval_string <- function(eval_res, eval_err) {

# Check for empty evaluation string
if (is.null(eval_res) || length(eval_res) == 0L) {
Expand All @@ -40,7 +40,7 @@ format.eval.string <- function(eval_res, eval_err) {

}

merge.eval.string <- function(env) {
.merge_eval_string <- function(env) {

# Check length of evaluation list
if (length(env$eval_list) <= 0L) {
Expand All @@ -63,7 +63,7 @@ merge.eval.string <- function(env) {
}

# Set error message
msg <- c(msg, format.eval.string(eval_res = env$eval_list[[j]], eval_err = eval_err))
msg <- c(msg, .format_eval_string(eval_res = env$eval_list[[j]], eval_err = eval_err))

}

Expand All @@ -86,11 +86,11 @@ cb_print_evaluation <- function(period) {
if ((i - 1L) %% period == 0L || is.element(i, c(env$begin_iteration, env$end_iteration))) {

# Merge evaluation string
msg <- merge.eval.string(env = env)
msg <- .merge_eval_string(env = env)

# Check if message is existing
if (nchar(msg) > 0L) {
print(merge.eval.string(env = env))
print(.merge_eval_string(env = env))
}

}
Expand Down Expand Up @@ -270,7 +270,7 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {

# Prepare to print if verbose
if (verbose) {
best_msg[[i]] <<- as.character(merge.eval.string(env = env))
best_msg[[i]] <<- as.character(.merge_eval_string(env = env))
}

} else {
Expand Down
27 changes: 26 additions & 1 deletion R-package/R/lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ Booster <- R6::R6Class(
modelfile = NULL,
model_str = NULL) {

# LightGBM-internal fix to comply with CRAN policy of only using up to 2 threads in tests and example.
#
# per https://cran.r-project.org/web/packages/policies.html:
#
# "If running a package uses multiple threads/cores it must never use more than two simultaneously:
# the check farm is a shared resource and will typically be running many checks simultaneously."
#
# This mechanism could be removed at any time, and isn't considered part of the public API.
#
threads_from_opts <- options("lightgbm.cran.testing.threads")[[1L]]
if (!is.null(threads_from_opts)) {
# put an upper limit on num_threads
params[["num_threads"]] <- min(params[["num_threads"]], as.integer(threads_from_opts))
# handle the case where 0 is passed to mean "use OpenMP default number of threads"
params[["num_threads"]] <- max(params[["num_threads"]], 1L)
}

handle <- NULL

if (!is.null(train_set)) {
Expand Down Expand Up @@ -928,6 +945,7 @@ NULL
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down Expand Up @@ -1086,7 +1104,10 @@ predict.lgb.Booster <- function(object,
#' X <- as.matrix(mtcars[, -1L])
#' y <- mtcars[, 1L]
#' dtrain <- lgb.Dataset(X, label = y, params = list(max_bin = 5L))
#' params <- list(min_data_in_leaf = 2L)
#' params <- list(
#' min_data_in_leaf = 2L
#' , num_threads = 2L
#' )
#' model <- lgb.train(
#' params = params
#' , data = dtrain
Expand Down Expand Up @@ -1231,6 +1252,7 @@ summary.lgb.Booster <- function(object, ...) {
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down Expand Up @@ -1296,6 +1318,7 @@ lgb.load <- function(filename = NULL, model_str = NULL) {
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down Expand Up @@ -1351,6 +1374,7 @@ lgb.save <- function(booster, filename, num_iteration = NULL) {
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down Expand Up @@ -1401,6 +1425,7 @@ lgb.dump <- function(booster, num_iteration = NULL) {
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down
1 change: 1 addition & 0 deletions R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CVBooster <- R6::R6Class(
#' , data = dtrain
#' , nrounds = 5L
#' , nfold = 3L
#' , num_threads = 2L
#' )
#' }
#' @importFrom data.table data.table setorderv
Expand Down
1 change: 1 addition & 0 deletions R-package/R/lgb.importance.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#' , max_depth = -1L
#' , min_data_in_leaf = 1L
#' , min_sum_hessian_in_leaf = 1.0
#' , num_threads = 2L
#' )
#' model <- lgb.train(
#' params = params
Expand Down
1 change: 1 addition & 0 deletions R-package/R/lgb.interprete.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#' , max_depth = -1L
#' , min_data_in_leaf = 1L
#' , min_sum_hessian_in_leaf = 1.0
#' , num_threads = 2L
#' )
#' model <- lgb.train(
#' params = params
Expand Down
1 change: 1 addition & 0 deletions R-package/R/lgb.model.dt.tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#' , max_depth = -1L
#' , min_data_in_leaf = 1L
#' , min_sum_hessian_in_leaf = 1.0
#' , num_threads = 2L
#' )
#' model <- lgb.train(params, dtrain, 10L)
#'
Expand Down
1 change: 1 addition & 0 deletions R-package/R/lgb.plot.importance.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#' , learning_rate = 0.1
#' , min_data_in_leaf = 1L
#' , min_sum_hessian_in_leaf = 1.0
#' , num_threads = 2L
#' )
#'
#' model <- lgb.train(
Expand Down
1 change: 1 addition & 0 deletions R-package/R/lgb.plot.interpretation.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#' , max_depth = -1L
#' , min_data_in_leaf = 1L
#' , min_sum_hessian_in_leaf = 1.0
#' , num_threads = 2L
#' )
#' model <- lgb.train(
#' params = params
Expand Down
4 changes: 3 additions & 1 deletion R-package/R/lgb.restore_handle.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#' , agaricus.train$label
#' , params = list(objective = "binary")
#' , nrounds = 5L
#' , verbose = 0)
#' , verbose = 0
#' , num_threads = 2L
#' )
#' fname <- tempfile(fileext="rds")
#' saveRDS(model, fname)
#'
Expand Down
1 change: 1 addition & 0 deletions R-package/R/lgb.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down
1 change: 1 addition & 0 deletions R-package/R/readRDS.lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down
1 change: 1 addition & 0 deletions R-package/R/saveRDS.lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#' , metric = "l2"
#' , min_data = 1L
#' , learning_rate = 1.0
#' , num_threads = 2L
#' )
#' valids <- list(test = dtest)
#' model <- lgb.train(
Expand Down
5 changes: 4 additions & 1 deletion R-package/man/lgb.configure_fast_predict.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.cv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.dump.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.get.eval.result.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.importance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.interprete.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.load.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.model.dt.tree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.plot.importance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.plot.interpretation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion R-package/man/lgb.restore_handle.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.save.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/lgb.train.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/predict.lgb.Booster.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/readRDS.lgb.Booster.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions R-package/man/saveRDS.lgb.Booster.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions R-package/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
library(testthat)
library(lightgbm)

options(
"lightgbm.cran.testing.threads" = 2.0
)

test_check(
package = "lightgbm"
, stop_on_failure = TRUE
Expand Down
4 changes: 4 additions & 0 deletions R-package/tests/testthat/test_lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ test_that("Booster should store parameters and Booster$reset_parameter() should
, boosting = "gbdt"
, num_class = 5L
, verbose = VERBOSITY
, num_threads = 2L
)
bst <- Booster$new(
params = params
Expand Down Expand Up @@ -658,6 +659,7 @@ test_that("Booster$params should include dataset params, before and after Booste
, max_depth = 4L
, bagging_fraction = 0.8
, verbose = VERBOSITY
, num_threads = 2L
)
bst <- Booster$new(
params = params
Expand All @@ -670,6 +672,7 @@ test_that("Booster$params should include dataset params, before and after Booste
, max_depth = 4L
, bagging_fraction = 0.8
, verbose = VERBOSITY
, num_threads = 2L
, max_bin = 17L
)
)
Expand All @@ -681,6 +684,7 @@ test_that("Booster$params should include dataset params, before and after Booste
, max_depth = 4L
, bagging_fraction = 0.9
, verbose = VERBOSITY
, num_threads = 2L
, max_bin = 17L
)
expect_identical(ret_bst$params, expected_params)
Expand Down
2 changes: 2 additions & 0 deletions R-package/tests/testthat/test_weighted_loss.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ VERBOSITY <- as.integer(
)

test_that("Gamma regression reacts on 'weight'", {

stop(options("lightgbm.cran.testing.threads")[[1L]])
n <- 100L
set.seed(87L)
X <- matrix(runif(2L * n), ncol = 2L)
Expand Down

0 comments on commit 934f0d0

Please sign in to comment.