Skip to content

Fixed #34 Add overlap-add convolve - #35

Open
NimaSarajpoor wants to merge 38 commits into
mainfrom
oaconvolve
Open

NimaSarajpoor wants to merge 38 commits into
mainfrom
oaconvolve

Conversation

@NimaSarajpoor

Copy link
Copy Markdown
Collaborator

This PR is to address #34.

@gitnotebooks

gitnotebooks Bot commented Jan 8, 2026

Copy link
Copy Markdown

@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author
./timing.py -timeout 1.0 -pmin 7 -pmax 24 pyfftw pocketfft_r2c_c2r scipy_oaconvolve challenger > timing.csv

# in timing.py, I change timeout to 5.0 when `len(T) >= 2^20`

The challenger is the customized version of scipy's oaconvolve.

customized_oaconvolve

Observations:

  • The challenger outperforms scipy's oaconvolve
  • For len(Q) <= 2 ^16 (and len(Q)>= 2^7), challenger outperforms the others for the most part.
  • For len(Q) > 2^16, pocketfft outperforms the others for the most part.

For me, the important one is the first bullet point. Of the four optimization opportunities mentioned in this comment, I've addressed 1, 2, and 3 in this PR. The last item, which is about adjusting the number of multiplication for real-valued arrays, can be explored next.

Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
@seanlaw

seanlaw commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

As a gentle reminder, even if we can do things faster, we will never (??) remove the public scipy convolution functions from STUMPY because they should be our last resort fallback (in case the alternatives, that may use private functions, raise an error). Does that make sense?

@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author

Good reminder. It makes sense!!

Comment thread test.py


def test_oaconvolve_sdp_blocksize():
from sdp.challenger_sdp import sliding_dot_product

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This line needs to be modified if, at a later time, we decide to move the proposal to a new file (module).

Comment thread sdp/challenger_sdp.py Resolved
@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author
./timing.py -timeout 1.0 -pmin 7 -pmax 24 pyfftw pocketfft_r2c_c2r scipy_oaconvolve challenger > timing.csv

# in timing.py, I change timeout to 5.0 when `len(T) >= 2^20`
customized_oaconvolve_performance

@seanlaw
"Challenger" seems to be the winner for most cases, and I think it is worth it to include it. What do you think? Also, can you please review the script? I've made major changes. The private objects are now only r2c and c2r. IMO, the script looks cleaner now.

@NimaSarajpoor
NimaSarajpoor requested a review from seanlaw January 9, 2026 20:39
Comment thread sdp/challenger_sdp.py Resolved

@seanlaw seanlaw left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@NimaSarajpoor I've left some comments but would still like another pass after you've cleaned things up further

I do agree that, for the most part, things look clean. I think it still lacks clarity as to what is happening or why the logic is coded in this way

Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
Comment thread sdp/challenger_sdp.py Resolved
@NimaSarajpoor

NimaSarajpoor commented Aug 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

@seanlaw
I think this is ready for another round of review. Note that:

(1) I intentionally left a few things to be addressed in a different PR later for the function _compute_block_size. This can help us see the impact of those changes once the current PR is merged.

(2) I thought about refactoring '_pocketfft_circular_convolve_block' (in challenger_sdp.py) and '_pocketfft_valid_convolve' (in the changed file pocketfft_r2c_c2r_sdp.py). However, I couldn't find a clean way to do it. Maybe we need to create a new function and those both functions need to call that common function (??)

@NimaSarajpoor
NimaSarajpoor requested a review from seanlaw August 23, 2026 23:10
@seanlaw

seanlaw commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

I think this is ready for another round of review

@NimaSarajpoor Was there anything in particular that you wanted me to focus on. Frankly, I don't have enough details or context to follow most/all the oaconvolve/block size logic. It feels like I can only attempt to spot issues with programming logic but not issues with algorithmic logic (not unless you can spell out the problem in clear detail).

Maybe we need to create a new function and those both functions need to call that common function (??)

I'll try to take a look but it's not clear to me what this is currently referencing as I do not have sufficient context

@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author

@seanlaw
Thanks for pointing that out. I would like to bring this to a point where it could be a good candidate for eventually being added to STUMPY/sdp.py. So, making both the algorithmic logic and the code easier to follow would be valuable.

Perhaps I should add some more context to this PR (?) For example, I can create a notebook that walks through the overlap-add approach mathematically and shows why, in our particular case, it produces the same result as the direct valid convolution.

Would something like that make the algorithmic logic easier to review? Or is there another kind of context that you think would be more useful?

@seanlaw

seanlaw commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

@NimaSarajpoor Yes, I think a notebook would be valuable. In most/all of the matrix profile work, we nearly always had a published paper to reference (and their code) and so it was easier to start from the same shared context and then we could evolve from there together. Here, I'm purely following you and the context that you give me. So, I think a detailed notebook (even with naive code snippets) would be quite helpful especially to "see" where the hard problem(s) are that need to be solved

@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author

@NimaSarajpoor Yes, I think a notebook would be valuable. In most/all of the matrix profile work, we nearly always had a published paper to reference (and their code) and so it was easier to start from the same shared context and then we could evolve from there together. Here, I'm purely following you and the context that you give me. So, I think a detailed notebook (even with naive code snippets) would be quite helpful especially to "see" where the hard problem(s) are that need to be solved

I implemented the overlap-add method from scratch, based on what I learned from the notebook developed in PR #43, and pushed the changes.

Notes:

  • What is the overlap-add method? It is a divide-and-conquer approach to computing the convolution of two arrays, Q and T. T is split into smaller chunks, and each chunk is convolved with Q. The result for the chunk T[start:stop] has length L = len(chunk) + len(Q) - 1 (the length of the full convolution) and is added to the output slice out[start : start + L].

  • How can performance be improved?

    • Use fixed-size chunks, so the loop can be vectorized later (done: the chunks now have a fixed size, oa_chunk_size, except possibly the last one).
    • Replace np.convolve with an FFT-based circular convolution.
    • Find a good chunk size when the user does not supply one.

@NimaSarajpoor NimaSarajpoor changed the title Fixed #34 Customizing scipy's oaconvolve Fixed #34 Add overlap-add convolve Nov 4, 2026
@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author

@seanlaw
I should confess that I did not fully understand the overlap-add before! The notebook developed in PR #43 was very helpful and I now understand the logic. So, I decided to start from scratch rather than customizing Scipy's code. My plan is to take it from here and optimize it.

What do you think?

@seanlaw

seanlaw commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

I should confess that I did not fully understand the overlap-add before! The notebook developed in PR #43 was very helpful and I now understand the logic. So, I decided to start from scratch rather than customizing Scipy's code.

You've put in a lot of hard work and effort into this and it really shows. I fully support you!

My plan is to take it from here and optimize it.

The only thing I'll ask is:

  1. Favor readability/understandability over minor hyper-optimizations
  2. Measure and compare with SciPy's performance

Otherwise, I am excited to see what you do!

@NimaSarajpoor

NimaSarajpoor commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator Author

As of the latest commit, the challenger is outperformed by scipy_oaconvolve_sdp.py (see image below). This is because the size of chunks in overlap-add is automatically set to n//2. As the next step, we focus on the chunk size in overlap-add.

timing_challenger_vs_oaconvolve

@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author

In commit 5fb0b08, I added a new notebook that digs into the topic of "chunk size" in overlap-add. Although there might be only a few candidates of chunk size for overlap-add on a given Q and T, it is still expensive to find the optimal value empirically. As shown in the added notebook, a mathematical model can provide an approximate cost of computation, and one can calculate an estimate of the optimal chunk size by finding the minima of the cost function.

The relevant piece was changed in challenger_sdp, and the impact can be seen in the following figure obtained by running timing.sh with ./timing.py -timeout 5.0 -pmin 7 -pmax 20 pyfftw scipy_oaconvolve challenger > timing.csv. As observed, challenger_sdp.py now outperforms scipy_oaconvolve_sdp.py in almost all cases.

image

@seanlaw

seanlaw commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

That's wonderful! How does this compare with Matlab or is that not important?

@NimaSarajpoor

Copy link
Copy Markdown
Collaborator Author

That's wonderful!

Thanks! Took me some time to figure things out and write them into a notebook.... but it was good! Still need to review the notebook and polish it again later with fresh eyes. I also need to add some references.

Two key points are:
(1) Use real=True when computing next_fast_len
(2) Since the number of chunks is computed via ceiling operator, we should be careful to avoid ending up with a solution where a more efficient one exists. Example: Suppose n=100. If chunk's size is 40, we end up with 3 chunks. However, for 3 chunks, the smallest chunk size is 34. So, we should go with 34 as it can potentially result in smaller size for circular convolution!

How does this compare with Matlab or is that not important?

Three items come to my mind:

(1) vs MATLAB's SDP via FFT/IFFT: Since the baseline in our plots is based on pyfftw, I think there is no need to compare with MATLAB again as we already compared pyfftw's performance with MATLAB before. IIRC, they were close.

(2) vs MATLAB's SDP via fftfilt: I just found out about this MATLAB function, which performs overlap-add convolution. We can compare this PR's challenger_sdp.py with that. However, I did not notice it in matrix profile.

(3) vs MATLAB's SDP via MASS V3: It uses the logic behind overlap-save; however, it does not use the logic to perform divide and conquer on the convolution problem. It uses it to perform divide and conquer directly on the SDP problem. In other words, it breaks down the time series T into smaller chunks... then compute SDP between each chunk and Q.

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.

2 participants