Skip to content

Multithreaded kneighbors_graph in TSNE - #15082

Merged
jnothman merged 6 commits into
scikit-learn:masterfrom
rth:parallel-nn-tsne
Oct 2, 2019
Merged

jnothman merged 6 commits into
scikit-learn:masterfrom
rth:parallel-nn-tsne

Conversation

@rth

@rth rth commented Sep 24, 2019 •

Copy link
Copy Markdown
Member

Partially addresses #10044
Partially addresses #10023

Computing the kneighbors_graph is one of the bottlenecks in TSNE. Actually we computing it in parallel, including for kd_tree and ball_tree has been supported for a while. This adds the n_jobs parameter to TSNE, which will pass it to NearestNeighbours.

On the first 20k samples of MNIST

Details
import numpy as np
from sklearn.manifold import TSNE
from sklearn.datasets import fetch_openml
from joblib import Memory


memory = Memory('/tmp/joblib_cache')

mnist = memory.cache(fetch_openml)(data_id=41063)

TSNE(verbose=5, n_jobs=8).fit_transform(mnist.data[:20000])

we can get up to a 2.5x run time speedup when running with n_jobs=8,

MNIST n_samples=20k / n_jobs 1 4 8 12
kneighbors_graph (s) 340 130 86 80
total run time (s) 462 246 189 184

The strong scaling limit seem to be around 10 CPU cores in this example.

For the full MNIST (60k samples), k-neighbours search takes proportionally even longer, and therefore multi-core scalability is better, with a x3.0 speed up at n_jobs=8 (and x3.7 at n_jobs=12) ,

MNIST n_samples=60k / n_jobs 1 8 12
kneighbors_graph (min) 61 15 10
total run time (min) 68 22.4 18.5

#10044 (comment) proposed smarter improvements that are still worth investigating, but in any case NN search is a bottleneck and we can't use annoy or similar unlike other TSNE libraries.

Not sure if this needs additional tests given that n_jobs in NearesNeigbours is already extensively tested.

@rth rth mentioned this pull request Sep 24, 2019
@rth rth changed the title Parallel kneighbors_graph in TSNE Multi-threaded kneighbors_graph in TSNE Sep 24, 2019
@rth rth changed the title Multi-threaded kneighbors_graph in TSNE Multithreaded kneighbors_graph in TSNE Sep 24, 2019
@rth

rth commented Sep 24, 2019

Copy link
Copy Markdown
Member Author

In the end using the algorithm='brute' on MNIST computes NN in 1min, and runs fully in 9min. We really need better heuristics for algorithm='auto' (#8213).

Though, this PR is still relevant, as for less high dimensional data kd_tree or ball_tree could be the right choice.

@jnothman

jnothman commented Sep 24, 2019 via email

Copy link
Copy Markdown
Member

@rth

rth commented Sep 24, 2019

Copy link
Copy Markdown
Member Author

We could also present an example of how to use annoy?

You mean with precomputed distance sparse distance matrices? #10482 added examples/neighbors/approximate_nearest_neighbors.py with exactly that.

The point of this PR is however that we can be significantly faster than what we are now for TSNE even without using external dependencies.

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

Smoke test??

Comment thread sklearn/manifold/t_sne.py
The number of parallel jobs to run for neighbors search.
``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
``-1`` means using all processors. See :term:`Glossary <n_jobs>`
for more details.

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.

Worth noting that it's unused if metric is precomputed?

Comment thread sklearn/manifold/t_sne.py Outdated
distances = pairwise_distances(X, metric=self.metric,
squared=True)
squared=True,
n_jobs=self.n_jobs)

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.

for euclidean metric, having n_jobs > 1 unless blas num threads has been limited.
Could you rerun your benchmark with this n_jobs removed ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course you are right, somehow I missed that in this branch the metric is always euclidean. Reverted the change here. The n_jobs is only useful in pairwise_distances for other computationally expensive metrics. In fact I think we should raise a warning when pairwise_distances is used with an euclidean metric and n_jobs>1.

The benchmarks I have done above are only for method="barnes_hut" (default). I have added the n_jobs for pairwise_distances in method="exact" on general grounds as there are probably some metrics where it's helpful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benchmarks I have done above are only for method="barnes_hut" (default). I have added the n_jobs for pairwise_distances in method="exact" on general grounds as there are probably some metrics where it's helpful.

My point is that this has no impact on benchmarks above.

@rth

rth commented Sep 26, 2019

Copy link
Copy Markdown
Member Author

OK, clarified the docstring, added a basic test, and a what's new.

@rth

rth commented Sep 30, 2019

Copy link
Copy Markdown
Member Author

Should be good to merge unless there are other comments?

@jnothman
jnothman merged commit 4ca6ee4 into scikit-learn:master Oct 2, 2019
@jnothman

jnothman commented Oct 2, 2019

Copy link
Copy Markdown
Member

Thanks

@rth
rth deleted the parallel-nn-tsne branch March 9, 2021 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants