Repository navigation
FIX Prevent segfault in TSNE with PCA init on constant data - #32789
Conversation
|
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). |
dae1860 to
67b1aa0
Compare
b24f064 to
4039cf4
Compare
|
@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! |
|
@nandan2003 Do you want to do the small changes requested above by @lesteve? Also, please mark our conversations in my review as "resolved". |
|
The changelog file needs to be called As indicated here the PR number needs to be in the changelog file not the issue number. |
|
My bad! |
|
@antoinebaker if you happen to fancy having a look here, it seems it could be an easy review potentially (not sure) |
antoinebaker
left a comment
There was a problem hiding this comment.
Thanks a lot @nandan2003 for the PR ! Here a few comments.
|
I confirm that a constant |
|
Hi @nandan2003, are you still working on this PR ? |
…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.
4b69bc8 to
6df306b
Compare
…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.
6df306b to
58dc7e6
Compare
|
Hi @antoinebaker, apologies for the delay! Yes, I'm still on it and have addressed all your review comments
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
left a comment
There was a problem hiding this comment.
Thanks @nandan2003, LGTM !
@dkobak would you mind giving a quick look ?
| # PCA init on constant or near-constant data must trigger a warning, | ||
| # fall back to random initialization, and not crash. |
| # 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: |
There was a problem hiding this comment.
Here also, add a comment like "To prevent crashing, see issue #28368"
|
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 What do you think? |
|
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. |
|
@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. |
There was a problem hiding this comment.
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).
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
TSNEwithinit='pca'producesNaNvalues (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 is0.0, resulting inNaNpropagation.The Fix:
I added a safe-guard check for the standard deviation.
std < 1e-12(effectively zero), the division is skipped, and the embedding is only scaled by the default factor (1e-4).Verification:
Added a regression test
test_tsne_pca_init_constant_datainsklearn/manifold/tests/test_t_sne.pywhich 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.