Skip to content

perf(fitting): scale features in one step instead of column by column - #60

Closed
amcim wants to merge 1 commit into
amrml-rewritefrom
recipe-speedup
Closed

amcim wants to merge 1 commit into
amrml-rewritefrom
recipe-speedup

Conversation

@amcim

@amcim amcim commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

perf(fitting): scale features in one step instead of column by column

What changes

file change
R/recipe-scale.R (new) .stepScale(), a recipe step with the same statistics as recipes::step_scale() and step_normalize(). It applies them to all columns at once.
R/fit-models.R .buildRecipe() uses .stepScale() in place of step_scale() (sparse) and step_normalize() (dense). Two lines.
NAMESPACE Registers the step's prep, bake and print methods (from roxygen).
tests/testthat/test-recipe-scale.R (new) Checks that .stepScale() bakes identical() to step_scale() (sparse) and step_normalize() (dense), with the same RNG state afterwards.

recipes scaling steps is really slow on very wide tables

recipes' scaling steps assign each scaled column back into the tibble one at a time. Every
assignment costs time proportional to the number of columns. This is extremely problematic for struct in particular given its size. The step is quadratic:

struct columns step_scale prep + bake the sd arithmetic itself
2,000 0.6 s 0.01 s
8,000 5.9 s 0.03 s
30,764 (SFL) about 1.8 min per prep 0.5 s

recipes' step_scale() and step_normalize() write each scaled column back into the tibble one at a time, and each write copies and re-checks the tibble's full list of columns. With n columns that's n writes of cost ~n each, so time grows with n²; .stepScale() writes all columns back in one assignment.

A task runs the recipe 6 times (5 folds plus the final fit). So a struct task spent minutes
scaling

Results don't change

The step computes exactly what recipes computes:

  • each column's sd is sqrt(stats::var(x));
  • an sd below .Machine$double.eps is set to 1;
  • the means come from colMeans();
  • each value is divided by the same double.

It draws the same random step ID as step_scale()/step_normalize(), so the folds drawn
after the recipe are unchanged.

Checked against amrml-rewrite

check result
Baked (training data after the recipe steps applied) data, 8,000 struct columns (sparse) and 3,000 (dense) identical(), same RNG state afterwards
S. argenteus test ORB, full workflow, every feature type, 130 tasks tasks, performance, importance and predictions all identical()
S. flexneri struct, 30,764 columns, MDR target, real and shuffled labels performance, importance and predictions identical() (except date)
Test suite with NOT_CRAN=true 432 passed, 0 failed, 1 skipped
Applied on #59 (hpc-parallelization), NAMESPACE regenerated 766 passed, 0 failed
The new test, run against two deliberately broken versions of the step (sd dividing by n instead of n − 1; each column divided by another column's sd) fails on both, so test shows it matches the recipe

Speed

Each pair ran side by side on the same machine:

before after
One S. flexneri struct task 966 s 90 s (10.7×)
Full S. argenteus workflow (small tables) 723 s 681 s

I did not do a full benchmark but I am hoping for pretty meaningful speedup here for larger bugs.

Merging with the stack

The R and test changes apply cleanly to #53 (8fdef4d), #57 and #59. There will be merging conflicts with NAMESPACE across the stack though

Other speedup possibilties that are less easy and can be done if this isn't enough

These are more speedups for wide tables. Each one changes results or is a larger refactor:

  • Collapsing duplicate columns: S. flexneri struct has 2,923 distinct column patterns
    among 31,643 columns. This changes elastic-net coefficients and importance attribution.
  • Calling glmnet directly instead of tune.
  • Sharing a matrix's prepared data across its seed and label tasks.
  • Filtering explicit zeros when reading parquet: 95% of struct's rows are zeros.

@amcim

amcim commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

https://github.com/tidymodels /recipes/issues/1543

The good news is that the issue I found was definitely real and led to massive speedup, the bad news is that I was using an old version of recipes and this was implemented in the newest version. So closing this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant