Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PRF Ridge - centering trick for cholesky solver - #34793

Open
cakedev0 wants to merge 17 commits into
scikit-learn:mainfrom
cakedev0:ridge/optim_cholesky
Open

PRF Ridge - centering trick for cholesky solver#34793
cakedev0 wants to merge 17 commits into
scikit-learn:mainfrom
cakedev0:ridge/optim_cholesky

Conversation

@cakedev0

@cakedev0 cakedev0 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

None, I started investigated for a speed gain while comparing speed/profiles with the scikit-learn-intelex (oneDAL) implementation.

What does this implement/fix? Explain your changes.

  • It uses an algebra trick to avoid the need for re-centering in Cholesky
  • It refactors a bit the validation so to avoid a double pass of check_array (and to avoid a copy in the algebraic centering Cholesky path).

Those relatively simple changes provide a speedups ranging from ~1.2x to 2x.

AI usage disclosure

I used AI assistance for:

  • Code generation (many iterations)
  • Comments
  • Research and understanding

Benchmarks

Benchmarks are run through the PR: probabl-ai/scikit-learn-benchmarks#31 (see bot comments with links to dashboards)

The benchmark cases are the same than the ones used to compare with scikit-learn-intelex, they are a mix of synthetic and real-world datasets.

On the intel-laptop, one case show a regression but it's a benchmark artifact: this case uses the dual path, which is not affected by this PR. And if you look at the granular timings, you'll see in both branches (main and this one) oscillate a lot.

@cakedev0 cakedev0 changed the title PRF Ridge - centering trick & fast mean & fast assert finite [WIP] PRF Ridge - centering trick & fast mean & fast assert finite Aug 21, 2026
… to svd

use_no_center_cholesky matched on solver in ("auto", "cholesky") but didn't
check the array namespace. With array API dispatch to a non-numpy namespace,
solver="auto" silently resolves to "svd" instead of "cholesky" (see
resolve_solver), which needs X to actually be centered. This left X
uncentered while running svd, causing
test_cross_val_predict_array_api_compliance[...-Ridge] failures on
array_api_strict and torch in CI.
@cakedev0 cakedev0 changed the title [WIP] PRF Ridge - centering trick & fast mean & fast assert finite [WIP] PRF Ridge - centering trick Aug 25, 2026
Comment on lines +396 to +399
elif is_X_sparse and fit_intercept:
# when `fit_intercept=True`, `mean_variance_axis` will be called on X
# and it requires csr/csc format
return ["csr", "csc"]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: this addition is needed because I now pass check_input=False to _preprocess_data which would have done this logic.

@cakedev0 cakedev0 changed the title [WIP] PRF Ridge - centering trick PRF Ridge - centering trick for cholesky solver Aug 27, 2026
@cakedev0
cakedev0 marked this pull request as ready for review August 27, 2026 06:55
@cakedev0

Copy link
Copy Markdown
Contributor Author

cc @antoinebaker, I was told you're the specialist of this kind of tricks 😉

@cakedev0 cakedev0 moved this to PR waiting for reviews in Labs Aug 31, 2026
@cakedev0 cakedev0 added this to Labs Aug 31, 2026
@cakedev0

cakedev0 commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

I checked memory usage and I did see it saving 2GB of RAM of a X of 2GB (which is expected as it avoids a copy). I guess this is a nice improvement, as memory might be a limit more often than run time for this model that usually is very fast.

Edit: Hum... I just had an OOM caused by Ridge on main while running benchmarks... 😄

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The centering and validation optimizations look good to me but I don't understand the change related to the solvers.

Also please add a changelog entry.

Comment thread sklearn/linear_model/_ridge.py Outdated
Comment thread sklearn/linear_model/_ridge.py
@ogrisel ogrisel moved this from PR waiting for reviews to In progress in Labs Sep 7, 2026
@cakedev0 cakedev0 moved this from In progress to PR waiting for reviews in Labs Sep 7, 2026
@cakedev0
cakedev0 requested a review from ogrisel September 9, 2026 06:56

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ogrisel

ogrisel commented Sep 11, 2026

Copy link
Copy Markdown
Member

cc @antoinebaker.

BTW similar work should be done for RidgeCV as a followup to this PR:

  • the input validation happens twice there as well (_RidgeGCV.fit calls validate_data and then _preprocess_data that does it again);
  • we could skip the pre-centering on dense data for the eigen GCV solver: the algebraic post-centering trick is already implemented for the sparse case, so we could repurpose the code to also use it for the dense case.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants