Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING_CHANGE: predict timings are cumulative #1078

Merged
merged 5 commits into from
Aug 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
* refactor: Optimize runtime of setting row roles.
* refactor: Optimize runtime of marshalling.
* refactor: Optimize runtime of `Task$col_info`
* BREAKING CHANGE: the predict time of the learner now stores the cumulative duration for
all predict sets (#992).
* feat: `$internal_valid_task` can now be set to an `integer` vector.
* deprecated the `$divide()` method
* fix: `Task$cbind()` now works with non-standard primary keys
13 changes: 12 additions & 1 deletion R/Learner.R
Original file line number Diff line number Diff line change
@@ -191,6 +191,9 @@ Learner = R6Class("Learner",
#' This currently only works for methods `Learner$predict()` and `Learner$predict_newdata()`,
#' and has no effect during [resample()] or [benchmark()] where you have other means
#' to parallelize.
#'
#' Note that the recorded time required for prediction reports the time required to predict
#' is not properly defined and depends on the parallelization backend.
parallel_predict = FALSE,

#' @field timeout (named `numeric(2)`)\cr
@@ -346,6 +349,10 @@ Learner = R6Class("Learner",
}
}, add = TRUE)

# reset learner predict time; this is only cumulative for multiple predict sets,
# not for multiple calls to predict / predict_newdata
self$state$predict_time = 0

# we only have to marshal here for the parallel prediction case, because learner_predict() handles the
# marshaling for call-r encapsulation itself
if (isTRUE(self$parallel_predict) && nbrOfWorkers() > 1L) {
@@ -360,7 +367,6 @@ Learner = R6Class("Learner",
pdata = learner_predict(self, task, row_ids)
}


if (is.null(pdata)) {
return(NULL)
} else {
@@ -461,6 +467,11 @@ Learner = R6Class("Learner",

#' @field timings (named `numeric(2)`)\cr
#' Elapsed time in seconds for the steps `"train"` and `"predict"`.
#'
#' When predictions for multiple predict sets were made during [resample()] or [benchmark()],
#' the predict time shows the cumulative duration of all predictions.
#' If `learner$predict()` is called manually, the last predict time gets overwritten.
#'
#' Measured via [mlr3misc::encapsulate()].
timings = function(rhs) {
assert_ro_binding(rhs)
4 changes: 4 additions & 0 deletions R/MeasureElapsedTime.R
Original file line number Diff line number Diff line change
@@ -12,6 +12,10 @@
#' Aggregation of elapsed time defaults to mean but can be configured via the field `aggregator` of the
#' [Measure].
#'
#' When predictions for multiple predict sets were made during [resample()] or [benchmark()],
#' the predict time shows the cumulative duration of all predictions.
#' If `learner$predict()` is called manually, the last predict time gets overwritten.
#'
#' @template param_id
#' @templateVar id time_train
#' @template measure
2 changes: 1 addition & 1 deletion R/worker.R
Original file line number Diff line number Diff line change
@@ -210,7 +210,7 @@ learner_predict = function(learner, task, row_ids = NULL) {

pdata = result$result
learner$state$log = append_log(learner$state$log, "predict", result$log$class, result$log$msg)
learner$state$predict_time = result$elapsed
learner$state$predict_time = sum(learner$state$predict_time, result$elapsed)

lg$debug("Learner '%s' returned an object of class '%s'",
learner$id, class(pdata)[1L], learner = learner$clone(), prediction_data = pdata, messages = result$log$msg)
10 changes: 9 additions & 1 deletion man/Learner.Rd

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

4 changes: 4 additions & 0 deletions man/mlr_measures_elapsed_time.Rd

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

15 changes: 13 additions & 2 deletions tests/testthat/test_Learner.R
Original file line number Diff line number Diff line change
@@ -490,7 +490,7 @@ test_that("compatability check on validation task", {
task$internal_valid_task$col_roles$target = "credit_history"
expect_error(learner$train(task), "has different target")
})

test_that("model is marshaled during parallel predict", {
# by setting check_pid = TRUE, we ensure that unmarshal_model() sets the process id to the current
# id. LearnerClassifDebug then checks during `.predict()`, whether the marshal_id of the model is equal to the current process id and errs if this is not the case.
@@ -540,7 +540,7 @@ test_that("learner state contains internal valid task information", {
expect_string(rr$learners[[1L]]$state$internal_valid_task_hash)

# 1. manual
learner$train(task)
learner$train(task)
expect_string(learner$state$internal_valid_task_hash)
})

@@ -550,3 +550,14 @@ test_that("validation task with 0 observations", {
task$internal_valid_task = integer(0)
expect_error({learner$train(task)}, "has 0 observations")
})

test_that("predict time is cumulative", {
learner = lrn("classif.debug", sleep_predict = function() 0.05)
task = tsk("iris")
learner$train(task)$predict(task)
t1 = learner$timings["predict"]
learner$param_set$values$sleep_predict = function() 0.01
learner$predict(task)
t2 = learner$timings["predict"]
expect_true(t1 > t2)
})