/
parallel-processing.Rmd
196 lines (153 loc) · 5.17 KB
/
parallel-processing.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: "Parallel Processing"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Parallel Processing}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
## Rules of thumb
- When tuning: `doParallel::registerDoParallel(8)` then `tune_grid()`/`tune_bayes()`
- When fitting: `set_engine("catboost", nthread = 8)` then `fit()`/`last_fit()`
## Packages and data
```{r data, eval = TRUE, message=FALSE}
library(tidymodels)
library(treesnip)
big_mtcars <- mtcars %>% sample_n(100000, replace = TRUE)
```
## Forking (workers)
Set `doParallel::registerDoParallel(8)` before `tune_grid()` or `tune_bayes()` and you are good to go. Just a reminder to watch out the memory usage. Forking can make your machine run out of memory.
```{r forking, cache=TRUE, eval = FALSE}
# model spec
model <- boost_tree(tree_depth = tune()) %>%
set_mode("regression") %>%
set_engine("catboost")
# set_engine("lightgbm")
# recipe
rec <- recipe(mpg ~ ., data = big_mtcars)
# workflows
wf <- workflow() %>% add_recipe(rec) %>% add_model(model)
# parallel tune grid
doParallel::registerDoParallel(8)
tg <- tune_grid(wf, vfold_cv(big_mtcars, 5))
```
## Threading
In summary, all you have to do is set `nthread = x` where `x` is the number of threads to use, usually the number of CPU cores to let the engine use from you own computer. E.g. `set_engine("lightgbm", nthread = 8)`.
Parsnip principles states that *parallel processing must be explictly requested by the user*^[https://tidymodels.github.io/model-implementation-principles/parallel-processing.html], so if `nthread` were not specified, just a single thread will be used.
`
```{r threading, eval = FALSE, message=FALSE}
model <- boost_tree(tree_depth = tune()) %>%
set_mode("regression") %>%
set_engine("catboost", nthread = 8) %>%
finalize_model(select_best(tg, "rmse"))
wf <- workflow() %>% add_recipe(rec) %>% add_model(model)
definitive_fit <- fit(wf, big_mtcars)
```
PS: originally, there is no `nthread` parameter for {catboost} but {treesnip} made `nthread` an alias for `thread_count` for convenience.
## Tune Grid Benchmark (speed)
### Findings:
- Threading is better for small datasets
- Forking is better for large datasets
- Memory is an issue when forking
### Parameters (all combinations of...):
- **the size of the data.frame:** 1.000 vs 1.000.000 rows
- **the number of models trained in grid search:** 48 vs 1875
- **threads/workers:** 1/8, 2/4, 4/2 and 8/1 (there was a constraint of `workers*threads = 8`)
### Machine:
- Intel Core i7-8565U
- CPU @ 1.80GHz × 8 cores
- Dell Inspiron
- RAM 16GB
- Linux Ubuntu 20.04 64bit
**further reading:** [Parallel Processing from Applied Predictive Modeling (Kuhn and Johnson, 2014)](http://appliedpredictivemodeling.com/blog/2018/1/17/parallel-processing)
### Code:
```{r code, cache=TRUE, eval = FALSE}
library(tidymodels)
library(treesnip)
# main function to tune_grid with given benchmark parameters
execute_tune_grid <- function(df, threads, workers, cv_folds, tune_grid, engine = "lightgbm") {
##############################################################
df_splits <- vfold_cv(df, v = cv_folds)
df_rec <- recipe(mpg ~ ., data = df)
df_model <- boost_tree(
mtry = 3,
trees = 600,
sample_size = 0.7,
min_n = tune(),
tree_depth = tune(),
learn_rate = tune(),
loss_reduction = tune()
) %>%
set_mode("regression") %>%
set_engine(as.character(engine), nthread = threads)
df_wf <- workflow() %>%
add_model(df_model) %>%
add_recipe(df_rec)
##############################################################
doParallel::registerDoParallel(workers)
tg <- tune_grid(df_wf, df_splits, grid = tune_grid)
tg
}
# parameters
parameters <- expand.grid(
engine = "lightgbm",
cv_folds = 8,
tune_grid = c(2, 5, 10),
nrow = c(1e2, 1e4, 1e6),
threads = c(1, 2, 4, 8)
) %>%
mutate(
workers = 8 %/% threads
) %>%
bind_rows(
expand.grid(
engine = "lightgbm",
cv_folds = 8,
tune_grid = c(2, 5, 10),
nrow = c(1e2, 1e4, 1e6),
threads = 1,
workers = 1
)
)
# bench::mark
set.seed(1)
bm <- bench::press(
{
Sys.sleep(2)
df <- mtcars %>% sample_n(nrow, replace = TRUE)
bench::mark(
execute_tune_grid(df, threads, workers, cv_folds, tune_grid, engine),
check = FALSE,
iterations = 3,
memory = FALSE,
filter_gc = FALSE,
time_unit = "m"
)
},
.grid = parameters
)
```
### Results:
```{r, include = FALSE}
bm <- readr::read_rds(system.file("benchmark_parallel_processing_vignette.rds", package = "treesnip"))
```
```{r result, message=FALSE, warning=FALSE, fig.width=8}
bm %>%
mutate(
nrow = paste("nrows:", nrow),
models_trained = (tune_grid ^ 4) * cv_folds,
max = map_dbl(time, max)/60,
threads = glue::glue("Threads: {threads} (workers: {workers})")
) %>%
ggplot(aes(x = models_trained, y = median, colour = factor(threads))) +
facet_wrap(~nrow, scales = "free_y") +
geom_pointrange(aes(ymin = min, ymax = max)) +
geom_line() +
labs(x = "Models trained", y = "Time elapsed (minutes)", colour = "Threads (Workers)")
```