项目作者: fkodom

项目描述 :
Implementation of 1D, 2D, and 3D FFT convolutions in PyTorch. Much faster than direct convolutions for large kernel sizes.
高级语言: Python
项目地址: git://github.com/fkodom/fft-conv-pytorch.git
创建时间: 2019-07-30T14:05:34Z
项目社区:https://github.com/fkodom/fft-conv-pytorch

开源协议:

下载


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:

  1. pip install fft-conv-pytorch

From source:

  1. git clone https://github.com/fkodom/fft-conv-pytorch.git
  2. cd fft-conv-pytorch
  3. pip install .

Example Usage

  1. import torch
  2. from fft_conv_pytorch import fft_conv, FFTConv1d
  3. # Create dummy data.
  4. # Data shape: (batch, channels, length)
  5. # Kernel shape: (out_channels, in_channels, kernel_size)
  6. # Bias shape: (out channels, )
  7. # For ordinary 1D convolution, simply set batch=1.
  8. signal = torch.randn(3, 3, 1024 * 1024)
  9. kernel = torch.randn(2, 3, 128)
  10. bias = torch.randn(2)
  11. # Functional execution. (Easiest for generic use cases.)
  12. out = fft_conv(signal, kernel, bias=bias)
  13. # Object-oriented execution. (Requires some extra work, since the
  14. # defined classes were designed for use in neural networks.)
  15. fft_conv = FFTConv1d(3, 2, 128, bias=True)
  16. fft_conv.weight = torch.nn.Parameter(kernel)
  17. fft_conv.bias = torch.nn.Parameter(bias)
  18. 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

Benchmark Plot