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

@NimaSarajpoor

NimaSarajpoor commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

... vs MATLAB SDP

NOTE:
In the following figures, the baseline is "MATLAB SDP"


Summary:
I used MATLAB ONLINE to get the timing for the following cases:

Code:

  • sdp_matlab_vs_python_1thread.zip
  • Instruction to run: Upload files in MATLAB, and run the file run_matlab_and_python.m
  • Expected output: Once done, all timing will be stored in a csv file, and its structure is the same as the file that is generated by timing.sh.

Performance:

  • Note that the baseline (y=1) in the following figures is "MATLAB SDP"
  • If a data point is above 1, it means that it outperforms MATLAB SDP
  • Higher is better
  • output: timing_1Thread.csv
image

Observation: ( X > Y means X performs better than Y)

  • For all cases: pyfftw_sdp > MATLAB SDP (seems to be aligned with the observations reported in pyFFTW vs MATLAB FFT #19)
  • For almost all cases: challenger_sdp > scipy_oaconvolve_sdp
  • No clear winner between challenger_sdp and pocketfft_r2c_c2r_sdp. Having said that, challenger_sdp significantly outperforms pocketfft_r2c_c2r_sdp when len(T) / len(Q) >= 2^4. This is somewhat aligned with the factor lambertw(-1 / (2 * (m - 1) * math.e), k=-1).real used in challenger_sdp for calculating the optimal size of circular convolution

Bonus: Checking performance for # threads > 1

Regarding the following figures:

  • The baseline (y=1) in the following figures is "MATLAB SDP with 'automatic' number of threads (<8)"
  • MATLAB SDP uses 'automatic' number for number of threads (< 8). challenger_sdp, in overlap-add, uses workers=2 when calculating r2c of chunks, and also works=2 when calculating c2r. No change in other SDPs, meaning they are single-threaded still
  • Code: sdp_matlab_vs_python_autothread.zip
  • output: timing_autothread.csv
image

Observation: ( X > Y means X performs better than Y)

  • For all cases: pyfftw_sdp > MATLAB SDP (pyfftw_sdpstill performs better even when it is single-threaded)
  • For almost all cases: challenger_sdp > scipy_oaconvolve_sdp AND challenger_sdp > pocketfft_r2c_c2r_sdp

Conclusion

  • pyfftw_sdp has a very good performance and it outperforms MATLAB SDP.
  • pocketfft_r2c_c2r_sdp has a good performance and is close to the performance of MATLAB SDP
  • challenger_sdp seems to outperform scipy_oaconvolve_sdp (in almost all cases) and pocketfft_r2c_c2r_sdp (in many cases, particularly when len(T)/len(Q) > 2^3)

@seanlaw

seanlaw commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Conclusion

pyfftw_sdp has a very good performance and it outperforms MATLAB SDP.
pocketfft_r2c_c2r_sdp has a good performance and is close to the performance of MATLAB SDP
challenger_sdp seems to outperform scipy_oaconvolve_sdp (in almost all cases) and > pocketfft_r2c_c2r_sdp (in many cases, particularly when len(T)/len(Q) > 2^3)

@NimaSarajpoor This is great! However, it's been so long that I've lost sight of what the overarching goal is. Can you state the goal simply? And, with this goal, what was the plan to reach that goal? Finally, how close/far away are we from accomplishing our goal?

@NimaSarajpoor

NimaSarajpoor commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

@seanlaw

Can you state the goal simply?

The main goal is to find SDP method(s) that:

  • have good performance (say.... >= 0.8 MATLAB SDP)
  • use Scipy/Numpy, the STUMPY's existing dependencies, if possible

what was the plan to reach that goal?

To compare methods' performances with MATLAB SDP
Note: We compared MATLAB SDP with PYFFTW SDP, and we decided to change the baseline to PYFFTW SDP.

how close/far away are we from accomplishing our goal?

I think pyfftw sdp gives us the "good performance" part of the goal. However, it requires new dependency. As the next best method(s), we have two options:

  • use pocketfft_r2c_c2r_sdp.py
  • use challenger.py of this PR (which seems to perform better than scipy_oaconvolve_sdp.py and pocketfft_r2c_c2r_sdp.py)

The goal of this PR was to see if a "challenger' should replace scipy_oaconvolve_sdp.py, one of the existing SDP modules. Maybe I should have reported the ... vs MATLAB SDP results separately and not in this PR. In contrast to scipy_oaconvolve_sdp.py, the challenger.py uses the private functions r2c and c2r. So, we either need to think about a fallback or see how the challenger_sdp.py performs if we replace private functions with public ones.

@seanlaw

seanlaw commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Thank you. What are our next steps? Need anything from me?

@NimaSarajpoor

NimaSarajpoor commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

@seanlaw

Thank you. What are our next steps? Need anything from me?

(1) I think it would be great if you could review the notebook in #43 . I'll request another review there. I believe it's worthwhile to at least have a document that helps us understand (overlap-add) convolution, regardless of whether this PR gets merged.

(2) Then, once you become comfortable with the logic of overlap-add and you see no issue there, it would be great if you could review this PR. If it is good from code, logic, and performance, we can go ahead and replace existing scipy_oaconvolve with the one proposed in the challenger_sdp.py of this PR. And, of course, closing PR is an option. I do not want to attach myself to the work and prefer to not just merge something to repo that does not make sense.

(3) Then I do not see anything else for SDP repo. We have pyfftw, and now we have a good alternative choice whose performance is good for small and large inputs.

What do you think? please let me know if something is missing.

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