Skip to content

Speed issues in t-SNE #10044

Closed
Closed

Description

@amueller

There seem to be some odd speed issues in our t-SNE:
DmitryUlyanov/Multicore-TSNE#29 (comment)

Benchmarks for comparison are here:
https://github.com/DmitryUlyanov/Multicore-TSNE/blob/master/MulticoreTSNE/examples/test_sklearn.py
https://github.com/DmitryUlyanov/Multicore-TSNE/blob/master/MulticoreTSNE/examples/test_py_bh_tsne.py

Todo: ensure these are sensible benchmarks to compare, run them, report numbers, look for bottlenecks.

The second iteration is a factor of 20 slower than the reference implementation according to the readme in that repo.

Activity

  1. added
    Easy
    on Oct 30, 2017
  2. jnothman commented on Oct 30, 2017

    @jnothman
    Member

    @ogrisel does this match your earlier benchmarks?

  3. thechargedneutron commented on Oct 31, 2017

    @thechargedneutron
    Contributor

    I want to take this issue. Will look into the issue and will ask in case of doubt.

  4. jnothman commented on Oct 31, 2017

    @jnothman
    Member
  5. siftikha commented on Nov 17, 2017

    @siftikha
    Contributor

    This still of interest to you all?

  6. amueller commented on Nov 17, 2017

    @amueller
    MemberAuthor

    for sure! Maybe talk to @thechargedneutron to see if they came up with something.

  7. thechargedneutron commented on Nov 17, 2017

    @thechargedneutron
    Contributor

    I am a bit busy with my semester end examinations. Not any significant progress on this issue. Will be free after 21st. @siftikha may work on it if possible before 21.

  8. rmitsch commented on Jan 29, 2018

    @rmitsch

    @thechargedneutron @siftikha Did you make any progress?

  9. lmcinnes commented on Jan 30, 2018

    @lmcinnes
    Contributor

    I can confirm some issues. I benchmarked all the python t-SNE implementations I could find on the MNIST dataset. Most implementations are based off the original code by Laurens van der Maaten, but there were some interesting differences anyway. You can see the results here.

    Summary:

    There is, of course, quite a bit of noise in timing based on random seeds etc., but there are some clear winners and losers here.

    MulticoreTSNE has superior NN computation (note surprising as it does approx NN via annoy I believe), coming in at 799s to complete the NN search. Interestingly the tsne implementation (which is largely a wrapper around Van der Maaten's code as I understand) is far worse at NN, taking 1104s, but makes up for it via faster gradient descent (306s vs the 654s of MulticoreTSNE). That's not just a sooner finish; in general it performs twice as fast for each batch of 50 iterations (according to the logging).

    Both bhtsne and Van der Maaten's original bh_tsne fare less well at the NN phase (taking 3833s for bhtsne's NN for example), but reproduce similar gradient descent times as MulticoreTSNE.

    Finally the bad news is the sklearn version, which fares badly in NN computation (5270s; probably due to kd-trees vs. vp-trees and some lack of tuning), and equally badly on gradient descent, averaging around 3-4 times slower than most of the other implementations (per batch of 50 iterations, and globally). This is a little disconcerting. I am pretty confident I have seen sklearn's t-SNE run much faster than this on MNIST (I seem to recall running it and getting similar timings to bh_tsne), but I certainly can't reproduce that now. Perhaps there has been a performance regression lately?

    With regard to bottlenecks -- I did some profiling and it seems like _kl_divergence_bh is taking the time (as it should), but is apparently slower than it needs to be. To get deeper requires digging in and setting debug flags on the cython, which I haven't done yet.

    I do believe the NN performance could be improved. I suspect taking advantage of a dual-tree approach would help with this much data that is this high dimensional. A breadth-first search strategy may also help. I'll look into that and try to get back to you with more details on timings.

  10. jnothman commented on Jan 30, 2018

    @jnothman
    Member
  11. jnothman commented on Jan 30, 2018

    @jnothman
    Member

    Does @tomMoral have any ideas?

  12. lmcinnes commented on Jan 30, 2018

    @lmcinnes
    Contributor

    No the pre-0.19 version (with the issues) was quite slow -- there was a noticeable performance boost moving on from that. If performance has degraded it is between 0.19.0 and 0.19.1, but I'm not sure anything has changed.

  13. jnothman commented on Jan 30, 2018

    @jnothman
    Member
  14. lmcinnes commented on Jan 30, 2018

    @lmcinnes
    Contributor

    I haven't actually verified that -- I'm just remembering older runs, but many thing have changed since then (including OS upgrades to deal with meltdown) which could have impacted performance. I'm not actually sure there is any difference.

    Update: verified that 0.19.0 on my current machine gives similar performance to 0.19.1. It may simply be that my recollection is poor :-(

  15. 6 remaining items

  16. TomDLT commented on Sep 25, 2019

    @TomDLT
    Member

    Finally the bad news is the sklearn version, which fares badly in NN computation [...], and equally badly on gradient descent, averaging around 3-4 times slower than most of the other implementations.

    Note that two speed improvements on the TSNE gradient descent have been merged in Feb 2018, (-40%) #10593, and (-25%) #10610 (total speed up -55%).

  17. lmcinnes commented on Sep 25, 2019

    @lmcinnes
    Contributor

    It seems like the merged PRs @TomDLT mentions, in combination with #10482, should significantly narrow the gap. It is probably worth benchmarking again to see how things look now. Of course OpenTSNE should probably be a point of comparison as well. I'll try to do this if I get some time, but other are certainly welcome to try as well.

  18. dkobak commented on Mar 9, 2021

    @dkobak
    Contributor

    Given

    #17148
    #15082
    #13264
    #10593
    #10610

    I believe this could be closed now?

  19. cmarmo commented on Mar 9, 2021

    @cmarmo
    Contributor

    Thanks @dkobak , do you think you would be able to quickly benchmarking the current version using scripts referenced in this issue?

  20. dkobak commented on Mar 9, 2021

    @dkobak
    Contributor

    @cmarmo Hmm. Embedding full MNIST without PCA pre-processing (n=70000, d=784) as @lmcinnes did above, on my 8-core laptop:

    • openTSNE(n_jobs=-1): 3min 6s [kNN search: 49s]
    • MulticoreTSNE(n_jobs=-1): 13min 56s [kNN: 6min 27s]
    • openTSNE(n_jobs=-1, neighbors='exact', negative_gradient_method='bh'): 29min 24s [kNN: 25 min]
    • SklearnTSNE(n_jobs=-1): 48min 18s [kNN: 40 min]

    I did not run it single-threaded yet because it takes annoyingly long.

    In terms of gradient descent speed, all Barnes-Hut implementations are similar. openTSNE is the fastest, then MulticoreTSNe, then sklearn.

    But BY FAR the largest difference in performance is due to kNN search. MulticoreTSNE uses Vantage Point trees. I think those are not available in sklearn, so this cannot really be addressed. openTSNE uses Ball Trees from sklearn. SklearnTSNE uses brute-force search (because that's what algorithm='auto' does). Brute-force is quite a bit slower than ball trees! Not sure why brute force used by default. It was implemented here #17148.

  21. rth commented on Mar 9, 2021

    @rth
    Member

    @dkobak Are you sure you have installed the latest version of scikit-learn? What is your OS?

    With scikit-learn 0.24.1 on a Linux desktop restricting calculation to 6 CPU* (with n_jobs=6 and export OMP_NUM_THREADS=6),

    • MulticoreTSNE(n_jobs=6): 8min 32s
    • SklearnTSNE(n_jobs=6): 4min 18s (kNN: 100s)

    So the scikit-learn implementation is currently faster than MulticoreTSNE for me.

    SklearnTSNE uses brute-force search (because that's what algorithm='auto' does). Brute-force is quite a bit slower than ball trees! Not sure why brute force used by default. It was implemented here #17148.

    In depends on the data dimensionality. For n_features=784 brute-force is likely to be faster than ball trees, and so it is correctly used there.

    *more CPUs could potentially be used as a result of multi-processing creating threads, but it any case that wouldn't account for a 10x difference in performance you reported.

    Edit: also for scikit-learn implementation could you please report the full output of %%time? I get,

    CPU times: user 15min 14s, sys: 1min 23s, total: 16min 37s
    Wall time: 4min 18s
    
  22. dkobak commented on Mar 9, 2021

    @dkobak
    Contributor

    @rth Hmm

    Are you sure you have installed the latest version of scikit-learn? What is your OS?

    I have sklearn 0.23.2 on Ubuntu.

    In depends on the data dimensionality. For n_features=784 brute-force is likely to be faster than ball trees, and so it is correctly used there.

    Well, it's not what I observe:

    neigh = NearestNeighbors(n_neighbors=90, algorithm='ball_tree', n_jobs=-1)
    %time neigh.fit(X)
    %time neigh.kneighbors_graph(mode='distance')
    
    neigh = NearestNeighbors(n_neighbors=90, algorithm='auto', n_jobs=-1)
    %time neigh.fit(X)
    %time neigh.kneighbors_graph(mode='distance')
    

    gives

    CPU times: user 25.8 s, sys: 75.7 ms, total: 25.9 s
    Wall time: 25.8 s
    CPU times: user 3h 3min 28s, sys: 7.13 s, total: 3h 3min 35s
    Wall time: 23min 46s
    CPU times: user 23 s, sys: 83.8 ms, total: 23.1 s
    Wall time: 23 s
    CPU times: user 4h 42min 24s, sys: 12.2 s, total: 4h 42min 36s
    Wall time: 37min 2s
    

    Can it be hardware-dependent?? It seems my CPU is several times slower than your CPU.

  23. dkobak commented on Mar 9, 2021

    @dkobak
    Contributor

    @rth Wow, I installed 0.24.1 and look:

    CPU times: user 23.3 s, sys: 83.6 ms, total: 23.4 s
    Wall time: 23.3 s
    CPU times: user 3h 9min 11s, sys: 11 s, total: 3h 9min 22s
    Wall time: 25min 18s
    CPU times: user 26.2 ms, sys: 0 ns, total: 26.2 ms
    Wall time: 25.9 ms
    CPU times: user 14min 21s, sys: 1min 40s, total: 16min 1s
    Wall time: 3min 48s
    

    What has changed in 0.24?? It's amazing.

  24. rth commented on Mar 9, 2021

    @rth
    Member

    No it shouldn't depend too much on hardware. It sounds like an issue of CPU oversubscription or BLAS that was fixed (or something about pre-computed sparse distance matrices calculation).
    Could you please provide the output of

    • sklearn.show_versions()
    • python -m threadpoolctl -i numpy scipy.linalg

    ?

  25. rth commented on Mar 9, 2021

    @rth
    Member

    I'm not fully sure which commit is responsible for the difference in performance you saw, but in any case it does look like the performance of v0.24.1 is good when compared to other TSNE implementations.

    So I'm going to close this issue, but don't hesitate to comment if you see other things.

  26. dkobak commented on Mar 9, 2021

    @dkobak
    Contributor

    @rth Very cool. I just re-ran t-SNE benchmark, and I got 10min 54s with sklearn which is indeed faster than MulticoreTSNE.

    I'll let @pavlinpolicar know that we should switch openTSNE's exact kNN search to algorithm='auto'.

  27. TomDLT commented on Mar 9, 2021

    @TomDLT
    Member

    I'm not fully sure which commit is responsible for the difference in performance

    It is most likely #17148

  28. dkobak commented on Mar 9, 2021

    @dkobak
    Contributor

    @TomDLT Ah, so in 0.23 "auto" was not running brute-force but something else? Makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions