math icon indicating copy to clipboard operation
math copied to clipboard

Discrete 1D convolutions [FR]

Open adamhaber opened this issue 4 years ago • 1 comments

I’m working on a project whose main bottleneck is a series of 1d discrete convolutions, basically a lot of dot products within a for loop. Similar computations also came up in the forums a couple of times (for example here).

I’m wondering if there is a way to make them more efficient. Would it be helpful to add something like a conv1d function to stan-math? Is there a way to implement conv1d(x, kernel) such that it is more efficient than:

for (i in T:n) {
    y[i] = dot_product(kernel, x[(i-T+1):i]);
}

I guess there are many subtleties here that I'm not aware of (numpy convolve has 3 modes - "full", "valid" and "same" - to handle various padding and border scenarios), just curious if this is relevant/interesting.

adamhaber avatar Jul 13 '21 20:07 adamhaber

Would the row/col-wise framework work for this? https://github.com/stan-dev/design-docs/pull/35#issuecomment-821812405

spinkney avatar Jul 15 '21 13:07 spinkney