Skip to content

PERF Limit OpenMP and BLAS in threaded backend as well - #1871

Open
itamarst wants to merge 78 commits into
joblib:mainfrom
itamarst:combined-over-saturation-prevention
Open

itamarst wants to merge 78 commits into
joblib:mainfrom
itamarst:combined-over-saturation-prevention

Conversation

@itamarst

@itamarst itamarst commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1846

Assumes joblib/threadpoolctl#257 will get merged; if not, or if API changes, this will need to change too. It is compatible with older threadpoolctl though.

itamarst added 30 commits July 22, 2026 11:29
@itamarst

itamarst commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Local testing suggests that once joblib/threadpoolctl#300 is merged, and combined with fixes I've added to this branch, the race conditions should be fixed and the joblib reproducer above should pass (I turned it into a test.)

@itamarst

itamarst commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

The main issue with this branch is that part of the correctness for OpenBLAS-pthreads resides here (_get_threadpoolctl_limits and its usage), and not in threadpoolctl. It's possible that joblib usage is more complex than average, and so that having it here is OK. But if not maybe should think about moving that, somehow, into threadpoolctl. Would probably require a different abstraction...

@itamarst

itamarst commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Test failures will (hopefully) turn to success when run against threadpoolctl 3.8.1 due to joblib/threadpoolctl#300

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some more feedback:

Comment thread joblib/test/test_parallel.py Resolved
Comment thread joblib/test/test_parallel.py Resolved
Comment thread joblib/_parallel_backends.py Resolved

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tiny suggestions to improve readability from the calling sites.

Comment thread joblib/_parallel_backends.py Resolved
Comment thread joblib/_parallel_backends.py Resolved
Comment thread joblib/parallel.py Resolved
@ogrisel ogrisel moved this from PR in progress to PR waiting for reviews in Labs Nov 16, 2026
@itamarst

Copy link
Copy Markdown
Contributor Author

I've updated this to work with latest version of APIs in joblib/threadpoolctl#300

@itamarst
itamarst requested a review from ogrisel November 16, 2026 20:56

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Besides the following detail, LGTM.

Comment thread joblib/test/test_parallel.py Resolved
@ogrisel

ogrisel commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Note that we might want to wait for the threadpoolctl 3.9.0 release before merging (even though I tested the PR against the dev branch already). For information, threadpoolctl 3.9.0 will probably also include joblib/threadpoolctl#309.

@itamarst

Copy link
Copy Markdown
Contributor Author

I suggest not merging this until there is a PR with scikit-learn docs (I'm working on it right now), since that will make clear how this impacts users. I'll submit that tomorrow morning my time hopefully.

@itamarst

Copy link
Copy Markdown
Contributor Author

Here's a first pass; the main differences from user perspective are lack of env variable overrides, and the fact that things Just Work mostly.

scikit-learn/scikit-learn#35521

@itamarst

Copy link
Copy Markdown
Contributor Author

(Merging that PR is blocked on joblib release, just worth reading it before proceeding to make sure we're happy with the impact on users.)

@ogrisel

ogrisel commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

I did a joint review of this PR with cursor and it founds several important things.

https://gist.github.com/ogrisel/372a9445655616c0e995cdb7d1d4f1c7

One of the main points (already raised by @cakedev0 during a meeting IIRC) is that calling the limiter in the worker number of threads can actually increase the number of threads limitted by an previous call (env var, default MKL SMT handling, outer threadpool_limit). I think this is worth fixing because it can be very surprising.

I don't know if we should fix this on the joblib side or on the threadpoolctl side.

The review also highlights problems related to double-terminate and handling of exceptions at pre-dispatch time that can result in ownership leaks.

There are many more things to look at in more details.

@itamarst

itamarst commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Going through the gist, I've so far addressed locally:

Part 1

TODO: We decided to have a limit per library, set initially to minimum of LibController.get_num_threads() and joblib.cpu_count(), and then split up the way Python threads cpu count is split up. This means env vars are useful again: for cases where no Python threads are used, they behave as normal. For Python thread pools, they act as a cap.

Part 2

  • 2.1: Fixed
  • 2.2: Fixed
  • 2.3: This only happens if the developer passed in bad parameters, i.e. it's a coding bug. I don't think ending up in a bad state if you have a bug that raises an expansion is worth fixing.
  • 2.4: I'll think about it, but calling __enter__/__exit__ directly doesn't add much clarity, and with variant is difficult to code given these abstractions. So probably not worth addressing.
  • 2.5: Increasing minimum dependencies is project policy question, my impression was that it wasn't desired.
  • Other part 2 small issues: Fixed.

Part 3

TODO, need to review.

Part 4

  • 4.1: Already fixed by previous PR.
  • ... TODO need to review ...

@ogrisel ogrisel moved this from PR waiting for reviews to Issue in progress in Labs Dec 1, 2026
@ogrisel

ogrisel commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

2.5: Increasing minimum dependencies is project policy question, my impression was that it wasn't desired.

I agree.

@ogrisel

ogrisel commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

The default-backend-threading CI seems to be failing because of an ownership leak.

This branch has not been deployed

No deployments
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.

Prevent oversaturation of OpenMP/BLAS when using Python threads

2 participants