fft-conv-pytorch
Implementation of 1D, 2D, and 3D FFT convolutions in PyTorch.
- Faster than direct convolution for large kernels.
- Much slower than direct convolution for small kernels.
- In my local tests, FFT convolution is faster when the kernel has >100 or so elements.
- Dependent on machine and PyTorch version.
- Also see benchmarks below.
Install
Using pip:
pip install fft-conv-pytorch
From source:
git clone https://github.com/fkodom/fft-conv-pytorch.git
cd fft-conv-pytorch
pip install .
Example Usage
import torch
from fft_conv_pytorch import fft_conv, FFTConv1d
# Create dummy data.
# Data shape: (batch, channels, length)
# Kernel shape: (out_channels, in_channels, kernel_size)
# Bias shape: (out channels, )
# For ordinary 1D convolution, simply set batch=1.
signal = torch.randn(3, 3, 1024 * 1024)
kernel = torch.randn(2, 3, 128)
bias = torch.randn(2)
# Functional execution. (Easiest for generic use cases.)
out = fft_conv(signal, kernel, bias=bias)
# Object-oriented execution. (Requires some extra work, since the
# defined classes were designed for use in neural networks.)
fft_conv = FFTConv1d(3, 2, 128, bias=True)
fft_conv.weight = torch.nn.Parameter(kernel)
fft_conv.bias = torch.nn.Parameter(bias)
out = fft_conv(signal)
Benchmarks
Benchmarking FFT convolution against the direct convolution from PyTorch in 1D, 2D, and 3D. The exact times are heavily dependent on your local machine, but relative scaling with kernel size is always the same.
| Dimensions | Input Size | Input Channels | Output Channels | Bias | Padding | Stride | Dilation |
|---|---|---|---|---|---|---|---|
| 1 | (4096) | 4 | 4 | True | 0 | 1 | 1 |
| 2 | (512, 512) | 4 | 4 | True | 0 | 1 | 1 |
| 3 | (64, 64, 64) | 4 | 4 | True | 0 | 1 | 1 |
Metadata
Release files for fft-conv-pytorch 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fft-conv-pytorch-1.2.0.tar.gz | 7.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fft_conv_pytorch-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.2 kB
Release files / fft-conv-pytorch-1.2.0.tar.gz
| Download URL | fft-conv-pytorch-1.2.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a061383176fa72cb2d8815d0c9ae67d03f7f1cea182ec9b9f5e869168582adb
|
|
BLAKE2b-256 checksum How to use checksums |
e233bebb4a0c01aa18513331b6a19d1a100b5762ce3678fefae862d2f12673e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / fft_conv_pytorch-1.2.0-py3-none-any.whl
| Download URL | fft_conv_pytorch-1.2.0-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
17b9bd616df86da25e4820473698eb4831c2f2f6e73906961901e6c278098f3c
|
|
BLAKE2b-256 checksum How to use checksums |
3496bbbf3761c6969defddb3c3268e2ab1a0524a7a16378afbdf06985cccce26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|