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

Skip to content

Fix low level crash in BisectingKMeans.predict on rescaled data - #27167

Merged
OmarManzoor merged 9 commits into
scikit-learn:mainfrom
ogrisel:fix-bisect-kmeans-segfault
Aug 28, 2023
Merged

Fix low level crash in BisectingKMeans.predict on rescaled data#27167
OmarManzoor merged 9 commits into
scikit-learn:mainfrom
ogrisel:fix-bisect-kmeans-segfault

Conversation

@ogrisel

@ogrisel ogrisel commented Aug 25, 2023

Copy link
Copy Markdown
Member

Fixes #27081.

I will first open this PR with the non-regression test that should hopefully make the macOS Intel CI workers segfault (for some reason, this bug seems to stay silent on other platforms)...

Then I will push a fix for the underlying root cause.

@github-actions

github-actions Bot commented Aug 25, 2023

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: a4a5abf. Link to the linter CI: here

@ogrisel ogrisel changed the title TST non-regression test for segfault in BisectingKMeans.predict Fix low level crash in BisectingKMeans.predict on rescaled data Aug 25, 2023
@ogrisel

ogrisel commented Aug 25, 2023

Copy link
Copy Markdown
Member Author

Victory! The non-regression test is actually causing a crash on the macOS intel CI as expected:

https://dev.azure.com/scikit-learn/scikit-learn/_build/results?buildId=58341&view=logs&j=97641769-79fb-5590-9088-a30ce9b850b9&t=4745baa1-36b5-56c8-9a8e-6480742db1a6

Current thread 0x0000000107bdae00 (most recent call first):
  File "/Users/runner/work/1/s/sklearn/cluster/_kmeans.py", line 796 in _labels_inertia
  File "/Users/runner/work/1/s/sklearn/cluster/_kmeans.py", line 820 in _labels_inertia_threadpool_limit
  File "/Users/runner/work/1/s/sklearn/cluster/_bisect_k_means.py", line 504 in _predict_recursive
  File "/Users/runner/work/1/s/sklearn/cluster/_bisect_k_means.py", line 520 in _predict_recursive
  File "/Users/runner/work/1/s/sklearn/cluster/_bisect_k_means.py", line 471 in predict
  File "/Users/runner/work/1/s/sklearn/cluster/tests/test_bisect_k_means.py", line 148 in test_non_regression_segfault_on_empty_bisections

Let's now check that the fix works as expected.

@ogrisel

ogrisel commented Aug 25, 2023

Copy link
Copy Markdown
Member Author

Note: this PR implements the fix both in the Python caller and the Cython callee:

  • sklearn/cluster/_bisect_k_means.py
  • sklearn/cluster/_k_means_lloyd.pyx

In theory, only one of those two fixes is actually needed, but I am not sure where it's best to fix this bug so I decided to fix it at both locations.

EDIT: I changed my mind and decided to make to make it explicit in the Cython code that it's expected to call the no-op case. This makes the Python code of bisecting k-means simpler as a result.

Comment thread sklearn/cluster/tests/test_bisect_k_means.py Outdated

@jjerphan jjerphan 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.

Thank you, @ogrisel. I have a few suggestions and a question.

Comment thread sklearn/cluster/_bisect_k_means.py Outdated
Comment thread sklearn/cluster/_k_means_lloyd.pyx
Comment thread sklearn/cluster/_k_means_lloyd.pyx
Comment thread sklearn/cluster/tests/test_bisect_k_means.py Outdated
@ogrisel

ogrisel commented Aug 25, 2023

Copy link
Copy Markdown
Member Author

The tests pass! I will generalize the fix to Elkan next week as suggested by @jjerphan. Have a nice WE.

@ogrisel

ogrisel commented Aug 26, 2023

Copy link
Copy Markdown
Member Author

In the end I decided to only keep the Cython side of the fix for the sake of simplicity. I updated the comments accordingly.

Comment thread sklearn/cluster/tests/test_bisect_k_means.py Outdated

@jjerphan jjerphan 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.

Thank you, @ogrisel.

Comment thread sklearn/cluster/_k_means_lloyd.pyx Outdated
Comment thread sklearn/cluster/_k_means_elkan.pyx Outdated
Comment thread sklearn/cluster/_k_means_elkan.pyx Outdated
Comment thread sklearn/cluster/_k_means_lloyd.pyx Outdated
@ogrisel ogrisel added Quick Review For PRs that are quick to review Waiting for Second Reviewer First reviewer is done, need a second one! labels Aug 28, 2023

@OmarManzoor OmarManzoor 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.

LGTM. Thanks @ogrisel

@OmarManzoor OmarManzoor removed the Waiting for Second Reviewer First reviewer is done, need a second one! label Aug 28, 2023
@OmarManzoor
OmarManzoor merged commit aae5837 into scikit-learn:main Aug 28, 2023
@jjerphan
jjerphan deleted the fix-bisect-kmeans-segfault branch August 28, 2023 09:56
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Aug 29, 2023
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:cluster Quick Review For PRs that are quick to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BisectingKMeans floating point exception fatal error on OSX

3 participants