diff --git a/R-package/R/callback.R b/R-package/R/callback.R index 50f36be4a2be..e428dfb79eea 100644 --- a/R-package/R/callback.R +++ b/R-package/R/callback.R @@ -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) { @@ -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) { @@ -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)) } @@ -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)) } } @@ -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 { diff --git a/R-package/R/lgb.Booster.R b/R-package/R/lgb.Booster.R index 949038fde622..8598ed6488b3 100644 --- a/R-package/R/lgb.Booster.R +++ b/R-package/R/lgb.Booster.R @@ -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)) { @@ -928,6 +945,7 @@ NULL #' , metric = "l2" #' , min_data = 1L #' , learning_rate = 1.0 +#' , num_threads = 2L #' ) #' valids <- list(test = dtest) #' model <- lgb.train( @@ -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 @@ -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( @@ -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( @@ -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( @@ -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( diff --git a/R-package/R/lgb.cv.R b/R-package/R/lgb.cv.R index cf88100db399..16f10073b6cc 100644 --- a/R-package/R/lgb.cv.R +++ b/R-package/R/lgb.cv.R @@ -65,6 +65,7 @@ CVBooster <- R6::R6Class( #' , data = dtrain #' , nrounds = 5L #' , nfold = 3L +#' , num_threads = 2L #' ) #' } #' @importFrom data.table data.table setorderv diff --git a/R-package/R/lgb.importance.R b/R-package/R/lgb.importance.R index c05c6628be34..5a58770553f9 100644 --- a/R-package/R/lgb.importance.R +++ b/R-package/R/lgb.importance.R @@ -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 diff --git a/R-package/R/lgb.interprete.R b/R-package/R/lgb.interprete.R index 70aac8760485..e0de0b781079 100644 --- a/R-package/R/lgb.interprete.R +++ b/R-package/R/lgb.interprete.R @@ -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 diff --git a/R-package/R/lgb.model.dt.tree.R b/R-package/R/lgb.model.dt.tree.R index 871f8f1d24bf..8b0d8d81e2e8 100644 --- a/R-package/R/lgb.model.dt.tree.R +++ b/R-package/R/lgb.model.dt.tree.R @@ -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) #' diff --git a/R-package/R/lgb.plot.importance.R b/R-package/R/lgb.plot.importance.R index 66eafd73a731..fc59ebd0efec 100644 --- a/R-package/R/lgb.plot.importance.R +++ b/R-package/R/lgb.plot.importance.R @@ -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( diff --git a/R-package/R/lgb.plot.interpretation.R b/R-package/R/lgb.plot.interpretation.R index 86d8b682725f..a88f14bf83f0 100644 --- a/R-package/R/lgb.plot.interpretation.R +++ b/R-package/R/lgb.plot.interpretation.R @@ -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 diff --git a/R-package/R/lgb.restore_handle.R b/R-package/R/lgb.restore_handle.R index dcb167608888..4de93d46c96a 100644 --- a/R-package/R/lgb.restore_handle.R +++ b/R-package/R/lgb.restore_handle.R @@ -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) #' diff --git a/R-package/R/lgb.train.R b/R-package/R/lgb.train.R index 8cf3a95eaf2e..4260f81cd3fe 100644 --- a/R-package/R/lgb.train.R +++ b/R-package/R/lgb.train.R @@ -30,6 +30,7 @@ #' , metric = "l2" #' , min_data = 1L #' , learning_rate = 1.0 +#' , num_threads = 2L #' ) #' valids <- list(test = dtest) #' model <- lgb.train( diff --git a/R-package/R/readRDS.lgb.Booster.R b/R-package/R/readRDS.lgb.Booster.R index a995d804adc5..a8abac642c24 100644 --- a/R-package/R/readRDS.lgb.Booster.R +++ b/R-package/R/readRDS.lgb.Booster.R @@ -23,6 +23,7 @@ #' , metric = "l2" #' , min_data = 1L #' , learning_rate = 1.0 +#' , num_threads = 2L #' ) #' valids <- list(test = dtest) #' model <- lgb.train( diff --git a/R-package/R/saveRDS.lgb.Booster.R b/R-package/R/saveRDS.lgb.Booster.R index eb71e7a2f08c..5d3af097301f 100644 --- a/R-package/R/saveRDS.lgb.Booster.R +++ b/R-package/R/saveRDS.lgb.Booster.R @@ -33,6 +33,7 @@ #' , metric = "l2" #' , min_data = 1L #' , learning_rate = 1.0 +#' , num_threads = 2L #' ) #' valids <- list(test = dtest) #' model <- lgb.train( diff --git a/R-package/man/lgb.configure_fast_predict.Rd b/R-package/man/lgb.configure_fast_predict.Rd index a228aad42e21..39fe6afa6b18 100644 --- a/R-package/man/lgb.configure_fast_predict.Rd +++ b/R-package/man/lgb.configure_fast_predict.Rd @@ -119,7 +119,10 @@ data(mtcars) 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 diff --git a/R-package/man/lgb.cv.Rd b/R-package/man/lgb.cv.Rd index 0e6db2e2cb0f..d16e53f75dcf 100644 --- a/R-package/man/lgb.cv.Rd +++ b/R-package/man/lgb.cv.Rd @@ -166,6 +166,7 @@ model <- lgb.cv( , data = dtrain , nrounds = 5L , nfold = 3L + , num_threads = 2L ) } } diff --git a/R-package/man/lgb.dump.Rd b/R-package/man/lgb.dump.Rd index c9b242a812e3..f4e90242fd75 100644 --- a/R-package/man/lgb.dump.Rd +++ b/R-package/man/lgb.dump.Rd @@ -31,6 +31,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/man/lgb.get.eval.result.Rd b/R-package/man/lgb.get.eval.result.Rd index cb54217bc42d..9c2293a0f909 100644 --- a/R-package/man/lgb.get.eval.result.Rd +++ b/R-package/man/lgb.get.eval.result.Rd @@ -45,6 +45,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/man/lgb.importance.Rd b/R-package/man/lgb.importance.Rd index 2fd5d4938de5..89a3d4e6b5b7 100644 --- a/R-package/man/lgb.importance.Rd +++ b/R-package/man/lgb.importance.Rd @@ -35,6 +35,7 @@ params <- list( , max_depth = -1L , min_data_in_leaf = 1L , min_sum_hessian_in_leaf = 1.0 + , num_threads = 2L ) model <- lgb.train( params = params diff --git a/R-package/man/lgb.interprete.Rd b/R-package/man/lgb.interprete.Rd index 6431a5011f48..c1166b2c1cc9 100644 --- a/R-package/man/lgb.interprete.Rd +++ b/R-package/man/lgb.interprete.Rd @@ -48,6 +48,7 @@ params <- list( , max_depth = -1L , min_data_in_leaf = 1L , min_sum_hessian_in_leaf = 1.0 + , num_threads = 2L ) model <- lgb.train( params = params diff --git a/R-package/man/lgb.load.Rd b/R-package/man/lgb.load.Rd index 6031ff8e55bb..c1a00a20974b 100644 --- a/R-package/man/lgb.load.Rd +++ b/R-package/man/lgb.load.Rd @@ -31,6 +31,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/man/lgb.model.dt.tree.Rd b/R-package/man/lgb.model.dt.tree.Rd index c5c88156ff4d..4d02ede9a001 100644 --- a/R-package/man/lgb.model.dt.tree.Rd +++ b/R-package/man/lgb.model.dt.tree.Rd @@ -51,6 +51,7 @@ params <- list( , max_depth = -1L , min_data_in_leaf = 1L , min_sum_hessian_in_leaf = 1.0 + , num_threads = 2L ) model <- lgb.train(params, dtrain, 10L) diff --git a/R-package/man/lgb.plot.importance.Rd b/R-package/man/lgb.plot.importance.Rd index 4b915e35fc86..302f46460e3f 100644 --- a/R-package/man/lgb.plot.importance.Rd +++ b/R-package/man/lgb.plot.importance.Rd @@ -47,6 +47,7 @@ params <- list( , learning_rate = 0.1 , min_data_in_leaf = 1L , min_sum_hessian_in_leaf = 1.0 + , num_threads = 2L ) model <- lgb.train( diff --git a/R-package/man/lgb.plot.interpretation.Rd b/R-package/man/lgb.plot.interpretation.Rd index 2d7416561f23..a914071e896f 100644 --- a/R-package/man/lgb.plot.interpretation.Rd +++ b/R-package/man/lgb.plot.interpretation.Rd @@ -58,6 +58,7 @@ params <- list( , max_depth = -1L , min_data_in_leaf = 1L , min_sum_hessian_in_leaf = 1.0 + , num_threads = 2L ) model <- lgb.train( params = params diff --git a/R-package/man/lgb.restore_handle.Rd b/R-package/man/lgb.restore_handle.Rd index bbe6f70c85de..95cbdc64485d 100644 --- a/R-package/man/lgb.restore_handle.Rd +++ b/R-package/man/lgb.restore_handle.Rd @@ -34,7 +34,9 @@ model <- lightgbm( , agaricus.train$label , params = list(objective = "binary") , nrounds = 5L - , verbose = 0) + , verbose = 0 + , num_threads = 2L +) fname <- tempfile(fileext="rds") saveRDS(model, fname) diff --git a/R-package/man/lgb.save.Rd b/R-package/man/lgb.save.Rd index 0736c26ab3f6..efd110c7d816 100644 --- a/R-package/man/lgb.save.Rd +++ b/R-package/man/lgb.save.Rd @@ -33,6 +33,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/man/lgb.train.Rd b/R-package/man/lgb.train.Rd index 30589ef34e54..0f2961edc415 100644 --- a/R-package/man/lgb.train.Rd +++ b/R-package/man/lgb.train.Rd @@ -141,6 +141,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/man/predict.lgb.Booster.Rd b/R-package/man/predict.lgb.Booster.Rd index f8043767be43..2df13b9bc374 100644 --- a/R-package/man/predict.lgb.Booster.Rd +++ b/R-package/man/predict.lgb.Booster.Rd @@ -132,6 +132,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/man/readRDS.lgb.Booster.Rd b/R-package/man/readRDS.lgb.Booster.Rd index 5a1c647a0f74..6a8e4c80ca91 100644 --- a/R-package/man/readRDS.lgb.Booster.Rd +++ b/R-package/man/readRDS.lgb.Booster.Rd @@ -34,6 +34,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/man/saveRDS.lgb.Booster.Rd b/R-package/man/saveRDS.lgb.Booster.Rd index e730f36b2caf..a8664243dce2 100644 --- a/R-package/man/saveRDS.lgb.Booster.Rd +++ b/R-package/man/saveRDS.lgb.Booster.Rd @@ -57,6 +57,7 @@ params <- list( , metric = "l2" , min_data = 1L , learning_rate = 1.0 + , num_threads = 2L ) valids <- list(test = dtest) model <- lgb.train( diff --git a/R-package/tests/testthat.R b/R-package/tests/testthat.R index 4752952740b2..0ff30d1f4294 100644 --- a/R-package/tests/testthat.R +++ b/R-package/tests/testthat.R @@ -1,6 +1,10 @@ library(testthat) library(lightgbm) +options( + "lightgbm.cran.testing.threads" = 2.0 +) + test_check( package = "lightgbm" , stop_on_failure = TRUE diff --git a/R-package/tests/testthat/test_lgb.Booster.R b/R-package/tests/testthat/test_lgb.Booster.R index 8ccfdaa336ae..6ebc2a36521f 100644 --- a/R-package/tests/testthat/test_lgb.Booster.R +++ b/R-package/tests/testthat/test_lgb.Booster.R @@ -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 @@ -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 @@ -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 ) ) @@ -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) diff --git a/R-package/tests/testthat/test_weighted_loss.R b/R-package/tests/testthat/test_weighted_loss.R index d00399548560..15bd27753941 100644 --- a/R-package/tests/testthat/test_weighted_loss.R +++ b/R-package/tests/testthat/test_weighted_loss.R @@ -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)