Skip to content

FIX Prevent segfault in TSNE with PCA init on constant data - #32789

Merged
lesteve merged 2 commits into
scikit-learn:mainfrom
nandan2003:fix_tsne_segfault_32788
Oct 13, 2026
Merged

lesteve merged 2 commits into
scikit-learn:mainfrom
nandan2003:fix_tsne_segfault_32788

Conversation

@nandan2003

@nandan2003 nandan2003 commented Nov 25, 2025 •

Copy link
Copy Markdown
Contributor

Reference Issue

Fixes #28368 (ref: #32788 which is a duplicate issue)

What does this implement/fix? Explain your changes.

This PR fixes a bug where TSNE with init='pca' produces NaN values (leading to potential segmentation faults in the C++ extension) when the input data has zero variance (constant values).

The Issue:
In _t_sne.py, the PCA initialization step attempts to normalize the first principal component by dividing by its standard deviation: X_embedded / np.std(...). When the input data is constant, the standard deviation is 0.0, resulting in NaN propagation.

The Fix:
I added a safe-guard check for the standard deviation.

  • If std < 1e-12 (effectively zero), the division is skipped, and the embedding is only scaled by the default factor (1e-4).
  • Otherwise, the standard normalization logic is applied.

Verification:
Added a regression test test_tsne_pca_init_constant_data in sklearn/manifold/tests/test_t_sne.py which asserts that fitting on a constant matrix (np.ones) produces finite values and does not crash.

Other comments

The fix uses a tolerance check (< 1e-12) rather than strict equality (== 0) to handle floating-point nuances, ensuring robustness for near-constant data.

@github-actions

github-actions Bot commented Nov 25, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

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

Generated for commit: 4039cf4. Link to the linter CI: here

@github-actions github-actions Bot added CI:Linter failure The linter CI is failing on this PR and removed CI:Linter failure The linter CI is failing on this PR labels Nov 25, 2025
@nandan2003

Copy link
Copy Markdown
Contributor Author

I've updated the PR to address the CI failures. This commit adds the missing changelog entry and resolves the linting errors (including the manual regex fixes in the test file).

Comment thread sklearn/manifold/_t_sne.py Resolved
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Nov 25, 2025
@nandan2003
nandan2003 force-pushed the fix_tsne_segfault_32788 branch from dae1860 to 67b1aa0 Compare November 25, 2025 17:49
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Nov 25, 2025
@nandan2003
nandan2003 requested a review from dkobak November 25, 2025 18:22
Comment thread sklearn/manifold/_t_sne.py Resolved
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Nov 25, 2025
@nandan2003
nandan2003 force-pushed the fix_tsne_segfault_32788 branch from b24f064 to 4039cf4 Compare November 25, 2025 18:50
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Nov 25, 2025
@nandan2003
nandan2003 requested a review from dkobak December 2, 2025 07:10

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

Looks good to me!

@lesteve

lesteve commented Dec 3, 2025

Copy link
Copy Markdown
Member

@nandan2003 thanks for the PR, I did not have time to look at it but set the label "Waiting for reviewer" hoping another maintainer will have a closer look.

In the mean time, please:

@dkobak thanks a lot for the first review, this is very useful!

@adrinjalali adrinjalali added this to Labs Dec 11, 2025
@adrinjalali adrinjalali moved this to In progress in Labs Dec 11, 2025
@dkobak

dkobak commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

@nandan2003 Do you want to do the small changes requested above by @lesteve? Also, please mark our conversations in my review as "resolved".

@nandan2003

Copy link
Copy Markdown
Contributor Author

@dkobak @lesteve thanks for the guidance. Those unnecessary changes like adding r is due to linting checks. Reverted those changes anyways. Please review it :)

@lesteve lesteve changed the title FIX Prevent segfault in TSNE with PCA init on constant data (#32788) FIX Prevent segfault in TSNE with PCA init on constant data Jan 8, 2026
@lesteve

lesteve commented Jan 8, 2026 •

Copy link
Copy Markdown
Member

The changelog file needs to be called doc/whats_new/upcoming_changes/sklearn.manifold/32789.fix.rst, please rename it 🙏! This is why the CI is red.

As indicated here the PR number needs to be in the changelog file not the issue number.

@nandan2003

nandan2003 commented Jan 8, 2026 •

Copy link
Copy Markdown
Contributor Author

My bad!
Thankyou @lesteve

@adrinjalali adrinjalali moved this from In progress to Todo in Labs Mar 2, 2026
@adrinjalali

Copy link
Copy Markdown
Member

@antoinebaker if you happen to fancy having a look here, it seems it could be an easy review potentially (not sure)

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

Thanks a lot @nandan2003 for the PR ! Here a few comments.

Comment thread sklearn/manifold/_t_sne.py Resolved
@antoinebaker

Copy link
Copy Markdown
Member

I confirm that a constant X (as in test_tsne_pca_init_constant_data) crashes TSNE on main, and is fixed in this PR.

@antoinebaker

Copy link
Copy Markdown
Member

Hi @nandan2003, are you still working on this PR ?

@jeremiedbb jeremiedbb moved this from Todo to Stalled in Labs Jun 29, 2026
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Sep 20, 2026
nandan2003 added a commit to nandan2003/scikit-learn that referenced this pull request Sep 20, 2026
…earn#32789)

- Fall back to random initialization when PCA initialization produces
  zero or near-zero variance on constant input data, preventing NaN
  propagation and subsequent Barnes-Hut segmentation faults (scikit-learn#32788).
- Emit RuntimeWarning explaining the fallback.
- Update TSNE init docstring and changelog.
- Add comprehensive regression tests for constant and near-constant data.
@nandan2003
nandan2003 force-pushed the fix_tsne_segfault_32788 branch from 4b69bc8 to 6df306b Compare September 20, 2026 05:16
…earn#32789)

- Fall back to random initialization when PCA initialization produces
  zero or near-zero variance on constant input data, preventing NaN
  propagation and subsequent Barnes-Hut segmentation faults (scikit-learn#32788).
- Emit RuntimeWarning explaining the fallback.
- Update TSNE init docstring and changelog.
- Add comprehensive regression tests for constant and near-constant data.
@nandan2003
nandan2003 force-pushed the fix_tsne_segfault_32788 branch from 6df306b to 58dc7e6 Compare September 20, 2026 05:18
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Sep 20, 2026
@nandan2003

Copy link
Copy Markdown
Contributor Author

Hi @antoinebaker, apologies for the delay! Yes, I'm still on it and have addressed all your review comments

  1. Fixed string concatenation spacing in the runtime warning.
  2. Documented the constant-data fallback under init in the TSNE docstring.
  3. Updated the changelog entry in 32789.fix.rst.
  4. Strengthened the regression test to cover both barnes_hut and exact methods, zero/constant/near-constant data, and reproducibility.

The branch has also been rebased onto latest main with a clean squashed commit, and CI is now completely green. Thanks for your patience and for following up!

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

Thanks @nandan2003, LGTM !

@dkobak would you mind giving a quick look ?

Comment on lines +1150 to +1151
# PCA init on constant or near-constant data must trigger a warning,
# fall back to random initialization, and not crash.

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.

Maybe add "nonregression test for issue #28368"

# the default value for random initialization. See issue #18018.
X_embedded = X_embedded / np.std(X_embedded[:, 0]) * 1e-4
std_pc1 = np.std(X_embedded[:, 0])
if std_pc1 < 1e-12:

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.

Here also, add a comment like "To prevent crashing, see issue #28368"

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.

Yup, done!

@dkobak

dkobak commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

I added one comment.

But in general, I just want to say here that if the input vectors are all identical, then the theoretically optimal t-SNE output is also all identical vectors. This achieves KL loss of zero. And PCA initialization does give you a set of identical points in this case. So switching to random initialization, while "fixes" the issue that tSNE is crashing, is not really fully satisfying to me :) Feels like a hack.

IMHO it would be better if tSNE optimizer simply returned this initialization (which is a set of zero vectors) as the optimal tSNE solution... Not sure what's the best way to achieve this though. Can we just immediately return the set of zero vectors if if std_pc1 < 1e-12: is TRUE?

What do you think?

@antoinebaker

Copy link
Copy Markdown
Member

I agree it's a hack, but I think it's good enough. For me it's more important to prevent the segfault than solve an edge (almost pathological) case.

@dkobak

dkobak commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

@antoinebaker Right, and it's OK with me, but what do you think about this alternative suggestion above: if we detect that PC1 of PCA initialization is constant, then we print a warning and immediately return constant embedding vectors as tSNE result?

@antoinebaker

Copy link
Copy Markdown
Member

@antoinebaker Right, and it's OK with me, but what do you think about this alternative suggestion above: if we detect that PC1 of PCA initialization is constant, then we print a warning and immediately return constant embedding vectors as tSNE result?

I would be fine with it.

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

Thanks @dkobak and @antoinebaker for your inputs and reviews.

I kind of agree it's a hack, but a hack that fixes a real issue, so overall I am fine with it.

@dkobak (or anyone else really) feel free to implement the suggested approach in #32789 (comment).

@lesteve
lesteve merged commit 94305cc into scikit-learn:main Oct 13, 2026
35 checks passed
@github-project-automation github-project-automation Bot moved this from PR Stalled to Done in Labs Oct 13, 2026
@nandan2003
nandan2003 deleted the fix_tsne_segfault_32788 branch October 13, 2026 14:17
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.

Crash in T-SNE

5 participants