Skip to content

FIX svm-newrand-31883 - #35092

Open
Higgs32584 wants to merge 12 commits into
scikit-learn:mainfrom
Higgs32584:fix-svm-newrand-31883
Open

Higgs32584 wants to merge 12 commits into
scikit-learn:mainfrom
Higgs32584:fix-svm-newrand-31883

Conversation

@Higgs32584

@Higgs32584 Higgs32584 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #31883

What does this implement/fix? Explain your changes.

mt_rand in sklearn/svm/src/newrand/newrand.h was a process-wide global. The liblinear solvers draw from it to shuffle the sample order while the GIL is released, so concurrent fits in different threads reseeded and consumed the same generator, giving timing-dependent coefficients.

This PR declares mt_rand as thread_local. Nothing else changes in the C/C++ or Cython code. This is sufficient because set_seed() is always called from the thread that then runs the solver:

  • liblinear: train_wrap calls set_parameter (which seeds) and then train in the same call.
  • libsvm: svm_train and svm_cross_validation seed at their start.

Tests:

  • Added test_liblinear_concurrent_fit, which compares sequential fits with fits run in 4 threads for LinearSVC and LinearSVR with dual=True.
  • Removed the thread_unsafe markers that referenced this issue in test_svm.py and test_logistic.py.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

This pull request includes code written with the assistance of AI.
The code has been reviewed and verified by me.

Any other comments?

@Higgs32584 Higgs32584 changed the title fix-svm-newrand-31883 FIX svm-newrand-31883 Oct 12, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fitting different instances of LinearSVR is not thread-safe

1 participant