Repository navigation
Open
Labels
Needs InvestigationIssue requires investigationfree-threadingPRs and issues related to support for free-threaded CPython (a.k.a. nogil or no-GIL, PEP 703)
Description
Warning
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
Look at a few logs and it consistently takes ~60 minutes which is quite slow. Part of the slowness is because we use pytest-run-parallel to stress-test free-threaded but still ...
From build log, see slowed tests below. The partial dependence taking each 2 minutes would be a start.
126.23s call inspection/tests/test_partial_dependence.py::test_partial_dependence_non_null_weight_idx[1-estimator2]
125.97s call inspection/tests/test_partial_dependence.py::test_partial_dependence_non_null_weight_idx[-1-estimator2]
125.65s call inspection/tests/test_partial_dependence.py::test_partial_dependence_non_null_weight_idx[0-estimator2]
20.70s call preprocessing/tests/test_target_encoder.py::test_fit_transform_not_associated_with_y_if_ordinal_categorical_is_not[42]
16.11s call feature_selection/tests/test_rfe.py::test_rfe_cv_groups
15.51s call cluster/tests/test_mean_shift.py::test_mean_shift[float64-1.2-False-4--1]
15.47s call cluster/tests/test_mean_shift.py::test_max_iter[100]
15.40s call cluster/tests/test_mean_shift.py::test_mean_shift[float64-1.2-True-3-0]
13.67s call ensemble/_hist_gradient_boosting/tests/test_binning.py::test_subsampled_weighted_vs_repeated_equivalence[5-0]
13.21s call ensemble/_hist_gradient_boosting/tests/test_binning.py::test_subsampled_weighted_vs_repeated_equivalence[3-1]
12.89s call ensemble/_hist_gradient_boosting/tests/test_binning.py::test_subsampled_weighted_vs_repeated_equivalence[5-42]
12.89s call ensemble/_hist_gradient_boosting/tests/test_binning.py::test_subsampled_weighted_vs_repeated_equivalence[3-0]
12.76s call ensemble/_hist_gradient_boosting/tests/test_binning.py::test_subsampled_weighted_vs_repeated_equivalence[3-42]
12.75s call ensemble/tests/test_forest.py::test_memory_layout[float32-ExtraTreesRegressor]
12.74s call ensemble/_hist_gradient_boosting/tests/test_binning.py::test_subsampled_weighted_vs_repeated_equivalence[5-1]
12.60s call ensemble/tests/test_forest.py::test_memory_layout[float32-RandomForestRegressor]
12.12s call ensemble/tests/test_forest.py::test_memory_layout[float64-ExtraTreesRegressor]
11.86s call ensemble/tests/test_forest.py::test_memory_layout[float64-RandomForestRegressor]
11.22s call ensemble/tests/test_forest.py::test_memory_layout[float32-ExtraTreesClassifier]
10.75s call ensemble/tests/test_forest.py::test_memory_layout[float64-ExtraTreesClassifier]
Metadata
Metadata
Assignees
Labels
Needs InvestigationIssue requires investigationfree-threadingPRs and issues related to support for free-threaded CPython (a.k.a. nogil or no-GIL, PEP 703)
Activity
lesteve commentedon Oct 8, 2026
Initial investigation with codex:
There is a slowdown, but the big jump is August 25 → 26, 2026. These are whole-job durations, including setup and compilation:
be5a8a853302ffa456b32The regular
pylatest_pip_scipy_devjob stayed essentially unchanged: 10m 58s, 10m 57s, and 10m 53s in those runs.I followed the commit history between the runs:
contingency_matrixArray API support, GitHub authentication scripts, other environments’ lock files, documentation, and a progress-bar fix. The free-threaded lock file did not change. The main computational change was #34785, which changedcontingency_matrixand added tests. That is a possible lead, but insufficient evidence to explain the extra 3m 32s.I could not inspect the requested
--durationsoutput. The browser exposes summaries but requires sign-in for logs, and the authenticated CLI fails because this environment cannot resolveapi.github.com. Consequently, I can confirm the job slowdown and identify relevant changes, but cannot yet identify the slowest tests or establish a cause.Please attach the downloaded free-threaded logs for these three runs; their duration tables and setup/build timings will let me distinguish a test regression from compilation or environment overhead.
lesteve commentedon Oct 8, 2026
I can reproduce locally a similar behaviour, running a single test with
--parallel-threads 4take 40s, whereas it takes 3s without--parallel-threadsand 5s with--parallel-threads 2pytest 'sklearn/inspection/tests/test_partial_dependence.py::test_partial_dependence_non_null_weight_idx[1-estimator2]'\ --parallel-threads 4 --durations=10lesteve commentedon Oct 8, 2026
The slowdown is much smaller with Python 3.14.6
lesteve commentedon Oct 8, 2026
Similar slowdown with
uvso this is not specific to the conda package:lesteve commentedon Oct 8, 2026
I checked that applying the diff below to scikit-learn
delayedwrapper does remove the slowdown with--parallel-threads 4from ~45s to ~2.5s:Here is what codex had to say about it (lightly edited to remove minor details):
Python 3.14.7 changes how function attributes are modified. Setting
__name__,__qualname__,__module__, or__doc__now calls_PyEval_StopTheWorld()before the update and_PyEval_StartTheWorld()afterwards. This pauses all threads executing Python, even when modifying a newly created function. These pauses are absent from the corresponding setters in 3.14.6; they appear in 3.14.7.That connects directly to this test:
delayed()(sklearn/utils/parallel.py:121) creates a function decorated withfunctools.wraps.wraps()copies those four attributes, triggering four global pauses.delayed()calls per test execution: potentially about 82,000 pauses, multiplied across concurrent test executions.The other candidates I examined are much weaker:
My leading explanation is therefore repeated global pauses caused by
functools.wrapsinsidedelayed().lesteve commentedon Oct 8, 2026
cc @cakedev0 in case you have noticed something similar in your performance investigation with free-threaded recently.
Note that this is running on my laptop so this one is not an issue only specific to many-cores server.
cakedev0 commentedon Oct 8, 2026
It's a bug on its own that was introduced between 3.14.6 and 3.14.7 (my agent agrees with yours ^^).
The issue I reported (python/cpython#158901) seems to make it worse, but my agent tried your reproducer with a fixed CPython it built (fixed for my bug), and your bug can still be quite bad it seems. I'll post my agent detailed findings when it's fully wrapped up.
I think you can open an issue on CPython then 😄
cakedev0 commentedon Oct 8, 2026
My agent findings (I read it, it's pretty informative):
I can reproduce on my laptop (Intel Core Ultra 5 225H, 14 cores), conda-forge free-threaded Python, same numpy/scipy/Cython in both envs, scikit-learn
main(14e0bae). Whole pytest run oftest_partial_dependence_non_null_weight_idx[1-estimator2]:--parallel-threadsYour diagnosis is right: the slowdown comes from the stop-the-world pauses that 3.14.7 added to the function attribute setters. It reproduces with the standard library only:
On the same laptop (µs per
functools.wrapscall):So no cost with a single thread, but already ~100x slower as soon as there is a second thread.
Where it comes from. Two use-after-free fixes for the free-threaded build, merged on main in late July and backported to 3.14:
__name__,__qualname__,__code__,__defaults__,__kwdefaults__,__closure__. 3.14.6 already paused in some of these setters, but only for functions that had already run, which a freshly created wrapper never has.sentinel()construction racesfunc.__module__reassignment incaller()under free-threading python/cpython#153531 (fix: gh-153531: fix thread safety of setting func.__doc__ and func.__module__ python/cpython#154851, 3.14 backport [3.14] gh-153531: fix thread safety of setting func.__doc__ and func.__module__ (GH-154851) python/cpython#154857): same for__doc__and__module__. The PR says a stop-the-world is needed because some C-API functions return borrowed references to these attributes.functools.wrapssets 4 of them (__module__,__name__,__qualname__,__doc__), so everydelayed()call pauses all the other threads 4 times. I found no discussion of the performance impact on these PRs. The 3.14.8 tag andmainstill have the pauses.It gets much worse on many-core machines, because of the
PyMutexfair handoff. Stop-the-world requests are serialized by aPyMutex(stw->mutexinPython/pystate.c), which has the 1 ms fair handoff I reported in python/cpython#158901. On a 2 x 86-core Xeon 6787P, with CPython built from source with--disable-gil, stock vsTIME_TO_BE_FAIR_NSraised to 100 ms (µs perfunctools.wrapscall, same idea as the script above with a decorator function, best of 3):Raising the threshold makes no difference on 3.14.6. Same machine, your test:
--parallel-threadsSo the handoff turns an already slow path into a collapse past ~8 threads, but even without it, 3.14.7 is ~5x slower than 3.14.6 at 8 threads.
What could be done.
delayed()patch removes the cost, whatever CPython does. I think it's worth merging regardless.mainalready protects the generator__name__setter by deferring the decref of the old value (_PyObject_XSetRefDelayed, the old value is freed once all threads have passed a quiescent state). As far as I can tell, this protects borrowed references as well as a stop-the-world does, but I may be missing a case. Skipping the pause when no other thread can see the function (thefunctools.wrapscase) would also work, but it's narrower.lesteve commentedon Oct 20, 2026
It seems like the
functools.wrapslowdown has been reported in CPython already 1 week ago and is in the process of being fixed:__name__/__doc__stops the world;functools.wrapsfrom multiple threads is orders of magnitude slower since 3.14.7 python/cpython#159136cakedev0 commentedon Oct 20, 2026
Nice! 🎉
lesteve commentedon Nov 4, 2026
The fix has been back-ported in 3.14 and 3.15 branch (see previous message for links) so the fix should make it in Python 3.14.11 and 3.15.3 expected December 1st 2026, see Python 3.14 release schedule and Python 3.15 release schedule.
I don't think it's worth doing a temporary work-around pinning to
python<=3.14.6to avoid the performance regression since the slow free-threaded CI doesn't annoy us too much right now. If you disagree, I will happily review a PR 😉.While I was at it I asked an agent to find some info about the future Python 3.15 free-threaded migration on conda-forge and the tl;dr is that it hasn't started yet and there is no precise start date in mind, see agent session below (TIL about
/exportin my agent ...)User
Can you find some public comment on GitHub indicating when the conda-forge python3.15 free-threaded will start or when numpy plans to have a conda-forge package?
Assistant
Yes—there is a public plan, but I couldn’t find a firm date for Python 3.15 free-threaded packages or NumPy’s 3.15t build.
The most relevant sources are:
conda-forge migration PR #8891, opened August 26, 2026. @h-vetinari explicitly proposes keeping the 3.14t migration running and then “remigrate for 3.15t,” following the earlier 3.13t → 3.14t process. No start date is given.
conda-forge’s September 3 announcement, on GitHub states:
A more recent PR, October 20 says “conda-forge has no 3.15t migration yet.” This is supporting evidence from a package contributor; the description labels itself AI-generated.
For NumPy specifically, I found PR #397, “Rebuild for python 3.15”, but that concerns the regular build. I found no NumPy-specific public commitment for 3.15t availability. The conda-forge migration plan above is the clearest indication of what it is waiting for.
h-vetinari commentedon Nov 4, 2026
I think we're ready to discuss migrating 3.15t; you can open an issue in https://github.com/conda-forge/conda-forge.github.io for that
lesteve commentedon Nov 4, 2026
Ooops didn't mean to ping you @h-vetinari 😅 1, thanks for your answer, I created an issue conda-forge/conda-forge.github.io#2982.
Footnotes
I blindly copied and pasted an agent conversation without thinking enough about the consequences 😱 ↩