Skip to content

Convolve1d rewrites #1319

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

Open
ricardoV94 opened this issue Mar 24, 2025 · 0 comments
Open

Convolve1d rewrites #1319

ricardoV94 opened this issue Mar 24, 2025 · 0 comments

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Mar 24, 2025

Description

Follow up to #1318

Some of these may apply to higher-dimensional convolutions.

Sum of convolution as product of sum of inputs

If we have convolve1d(x, y, mode="full").sum(), we can rewrite it as x.sum() * y.sum()

import numpy as np

rng = np.random.default_rng(37)
x = rng.normal(size=(5))
y = rng.normal(size=(9))
np.testing.assert_allclose(np.convolve(x, y).sum(), x.sum() * y.sum())

convolve constant kernels

If we have two sequential applications of convolve, with constant inputs we can convolve the constant inputs first, reducing the number of runtime convolutions

import numpy as np

rng = np.random.default_rng(37)
x = rng.normal(size=(5))
y1 = rng.normal(size=(9))
y2 = rng.normal(size=(3,))

r1 = np.convolve(np.convolve(x, y1), y2)
r2 = np.convolve(x, np.convolve(y1, y2))
np.testing.assert_allclose(r1, r2)

Merge convolutions with flipped inputs

Convolutions give the same output regardless of order of inputs, so if we see two with the same inputs but in different order we can merge them. In general, we may want to have an Op property that tells us when the output is invariant to the order of inputs to apply such merge automatically. This Applies to Add, Mul, ...

import numpy as np

rng = np.random.default_rng(37)
x = rng.normal(size=(5))
y = rng.normal(size=(9))

r1 = np.convolve(x, y)
r2 = np.convolve(y, x)
np.testing.assert_allclose(r1, r2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant