[ENH] Distance module n_jobs support#2545
Conversation
Thank you for contributing to
|
baraline
left a comment
There was a problem hiding this comment.
LGTM! The only thing that would be missing from the PR to fix the issue is to include n_jobs in the distance params in our KNeighborsTimeSeriesClassifier and we would be good to go
MatthewMiddlehurst
left a comment
There was a problem hiding this comment.
Would be nice to benchmark and see if this actually speeds it up.
|
Can confirm that this does indeed provide some speed ups : import numpy as np
from aeon.distances import euclidean_pairwise_distance
X = np.random.rand(1000, 1000)
Y = np.random.rand(1000, 1000)
euclidean_pairwise_distance(X, Y)
%timeit euclidean_pairwise_distance(X, Y)
%timeit euclidean_pairwise_distance(X, Y, n_jobs=-1)Seconds Matthew's comment to revert the number of threads back to previously setted value (using get_num_threads). |
|
With distance measures like Euclidean I found unless you have a huge number of very long time series threading is almost never going to be faster just due to the cost of spawning a thread. This is why I added the warning as I can't really see a scenario where running pairwise euclidean distance thread will ever achieve a meaningful speed up. However, for something like DTW I get huge speed ups: |
|
I also decided to add the threading with a custom distance function. |
|
I followed your suggestion @MatthewMiddlehurst and defined a decorator to handle setting and resetting threading. |
TonyBagnall
left a comment
There was a problem hiding this comment.
LGTM but will wait to see if @baraline or @MatthewMiddlehurst want to comment
baraline
left a comment
There was a problem hiding this comment.
The use of the decorator and how you handle number of threads seems to be the good solution here, so I'm good with it, makes the handling of the n_jobs straightforward for those functions without having to mess with them.
|
I've had to disable the CanonicalIntervalForestRegressor "check_regressor_against_expected_results" test. I believe this has started to fail due to instability with numba in the module. I'm not really sure at all how this PR is impacting this test my best guess is switching the number of threads is causing unstable behaviour in tests that occur after certain distance tests run. I have also noticed that two other regressors RDST and RIST Regressor also have excluded tests in the aeon/testing/testing_config.py EXCLUDED_TESTS so I would assume this is a similar issue. I have had this problem both with numba caches on and off. I think this is a deeper issue with the regression module itself. I could change the expected results as it seems to consistently now resolve to 0.14 instead of 0.16 (fourth result in the expected array) however, I don't feel comfortable changing this not knowing if this is a bug or not. I don't think I can fix this myself so instead after this PR is merged I'll open an issue documenting the bug so we can resolve it in the future. Here is a CI run: https://github.com/aeon-toolkit/aeon/actions/runs/16759105314/job/47458932638?pr=2545 |
MatthewMiddlehurst
left a comment
There was a problem hiding this comment.
in testing config we have a variable called MULTITHREAD_TESTING, can you use that for tests which play with threads, It runs it on a workflow without pytest-xdist (the tool we use to thread testing)
|
Ill make that change now |
Reference Issues/PRs
#1886 #1797
What does this implement/fix? Explain your changes.
This PR adds parallelism to the distance module. Specifically to pairwise distance computations.
To do this the n_job parameter can now be specified to any pairwise distance (aside mpdist which was excluded due to complexity) and it will use that many threads.
This PR just updates the distance module. In a follow up PR I will update all our models that use pairwise_distance and n_jobs to make use of this.
Does your contribution introduce a new dependency? If yes, which one?
Any other comments?
PR checklist
For all contributions
For new estimators and functions
__maintainer__at the top of relevant files and want to be contacted regarding its maintenance. Unmaintained files may be removed. This is for the full file, and you should not add yourself if you are just making minor changes or do not want to help maintain its contents.For developers with write access