Parallelize 'mlr3' functions
Henrik Bengtsson
Source:vignettes/futurize-81-mlr3.md
futurize-81-mlr3.Rmd
+
=

The futurize package allows you to easily turn
sequential code into parallel code by piping the sequential code to the
futurize() function. Easy!
Introduction
This vignette demonstrates how to use this approach to parallelize
mlr3
functions such as resample() and
benchmark().
The mlr3 package is a modern, object-oriented framework for machine learning in R. It provides a unified interface to many learning algorithms, together with tools for resampling, benchmarking, and model evaluation. Resampling and benchmarking fit and evaluate a model once per resampling iteration, which can be computationally intensive. Since these iterations are independent of each other, they are ideal candidates for parallelization.
Example: Cross-validating a learner
The resample() function fits a learner on each training
set of a resampling strategy and evaluates it on the corresponding test
set. For example, 10-fold cross-validation of a classification tree
results in ten independent model fits:
library(futurize)
library(mlr3)
task <- tsk("penguins")
learner <- lrn("classif.rpart")
resampling <- rsmp("cv", folds = 10)
rr <- resample(task, learner, resampling) |> futurize()
rr$aggregate(msr("classif.ce"))The iterations are processed in parallel by the workers defined by
your current plan(). For example, to parallelize on your
local machine:
plan(multisession)Example: Benchmarking multiple learners
The benchmark() function runs multiple resampling
experiments, e.g. to compare several learners on one or more tasks:
library(futurize)
library(mlr3)
design <- benchmark_grid(
tasks = tsk("penguins"),
learners = lrns(c("classif.rpart", "classif.featureless")),
resamplings = rsmp("cv", folds = 10)
)
bmr <- benchmark(design) |> futurize()
bmr$aggregate(msr("classif.ce"))All resampling iterations across all experiments are processed in parallel.
Details
The mlr3 package already parallelizes
resample() and benchmark() via future.apply,
using the future backend set by plan(). It is also possible
to configure mlr3 to parallelize via mirai
instead, by setting up mirai daemons for the
"mlr3_parallelization" compute profile. When you pipe one
of these functions to futurize(), mlr3 is
guaranteed to parallelize via futures, even if mirai daemons have been
set up for mlr3, e.g. by other code in your R
session.
The mlr3 package itself decides which globals and
packages to export to the parallel workers, and it always uses parallel
random number generation (rng = TRUE). Because of this,
futurize() options globals and
packages are ignored for mlr3 functions.
Other future options, e.g. chunk_size and
stdout, are passed on as-is, e.g.
Supported Functions
The following mlr3 functions are supported by
futurize():
-
benchmark()withrng = TRUEas the default -
resample()withrng = TRUEas the default