Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Distance module n_jobs support #2545

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

chrisholder
Copy link
Contributor

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
  • I've added myself to the list of contributors. Alternatively, you can use the @all-contributors bot to do this for you after the PR has been merged.
  • The PR title starts with either [ENH], [MNT], [DOC], [BUG], [REF], [DEP] or [GOV] indicating whether the PR topic is related to enhancement, maintenance, documentation, bugs, refactoring, deprecation or governance.
For new estimators and functions
  • I've added the estimator/function to the online API documentation.
  • (OPTIONAL) I've added myself as a __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
  • (OPTIONAL) I've updated aeon's CODEOWNERS to receive notifications about future changes to these files.

@aeon-actions-bot aeon-actions-bot bot added distances Distances package enhancement New feature, improvement request or other non-bug code enhancement labels Feb 18, 2025
@aeon-actions-bot
Copy link
Contributor

Thank you for contributing to aeon

I have added the following labels to this PR based on the title: [ $\color{#FEF1BE}{\textsf{enhancement}}$ ].
I have added the following labels to this PR based on the changes made: [ $\color{#5209C9}{\textsf{distances}}$ ]. Feel free to change these if they do not properly represent the PR.

The Checks tab will show the status of our automated tests. You can click on individual test runs in the tab or "Details" in the panel below to see more information if there is a failure.

If our pre-commit code quality check fails, any trivial fixes will automatically be pushed to your PR unless it is a draft.

Don't hesitate to ask questions on the aeon Slack channel if you have any.

PR CI actions

These checkboxes will add labels to enable/disable CI functionality for this PR. This may not take effect immediately, and a new commit may be required to run the new configuration.

  • Run pre-commit checks for all files
  • Run mypy typecheck tests
  • Run all pytest tests and configurations
  • Run all notebook example tests
  • Run numba-disabled codecov tests
  • Stop automatic pre-commit fixes (always disabled for drafts)
  • Disable numba cache loading
  • Push an empty commit to re-run CI checks

Copy link
Member

@baraline baraline left a comment

Choose a reason for hiding this comment

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

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

baraline
baraline previously approved these changes Feb 18, 2025
Copy link
Member

@MatthewMiddlehurst MatthewMiddlehurst left a comment

Choose a reason for hiding this comment

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

Would be nice to benchmark and see if this actually speeds it up.

@@ -188,6 +196,17 @@ def sbd_pairwise_distance(
[0.36754447, 0. , 0.29289322],
[0.5527864 , 0.29289322, 0. ]])
"""
n_jobs = check_n_jobs(n_jobs)
set_num_threads(n_jobs)
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this should set it back to the original value after?

Maybe its possible to use a decorator in these cases similar to https://github.com/aeon-toolkit/aeon/blob/v1.0.0/aeon/testing/utils/output_suppression.py

@baraline
Copy link
Member

baraline commented Mar 1, 2025

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)
199 ms ± 7.05 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
82.7 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Seconds Matthew's comment to revert the number of threads back to previously setted value (using get_num_threads).

@chrisholder
Copy link
Contributor Author

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:

    X = make_example_3d_numpy(10000, 1, 100, return_y=False, random_state=42)
    start = time.time()
    pairwise_distance(X, method="dtw", n_jobs=1)
    print(f"1 Job Time: {time.time() - start}")

    start = time.time()
    pairwise_distance(X, method="dtw", n_jobs=14)
    print(f"14 Job Time: {time.time() - start}")
Time: 1620.196809053421
Time: 255.28647780418396

@chrisholder
Copy link
Contributor Author

I also decided to add the threading with a custom distance function.

@chrisholder
Copy link
Contributor Author

I followed your suggestion @MatthewMiddlehurst and defined a decorator to handle setting and resetting threading.

@chrisholder chrisholder added the no numba cache Disable numba cache loading on a PR label Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distances Distances package enhancement New feature, improvement request or other non-bug code enhancement no numba cache Disable numba cache loading on a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants