|
| 1 | +from typing import Iterable, Union |
| 2 | + |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | +import torch.nn.functional as f |
| 6 | + |
| 7 | +from fft_conv_pytorch.fft_conv import _FFTConv, fft_conv, to_ntuple |
| 8 | +from tests.utils import _assert_almost_equal, _gcd |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("in_channels", [1, 2, 3]) |
| 13 | +@pytest.mark.parametrize("out_channels", [1, 2, 3]) |
| 14 | +@pytest.mark.parametrize("groups", [1, 2, 3]) |
| 15 | +@pytest.mark.parametrize("kernel_size", [1, 2, 3]) |
| 16 | +@pytest.mark.parametrize("padding", [0, 1]) |
| 17 | +@pytest.mark.parametrize("stride", [1, 2, 3]) |
| 18 | +@pytest.mark.parametrize("dilation", [1, 2, 3]) |
| 19 | +@pytest.mark.parametrize("bias", [True, False]) |
| 20 | +@pytest.mark.parametrize("ndim", [1, 2, 3]) |
| 21 | +@pytest.mark.parametrize("input_size", [7, 8]) |
| 22 | +def test_fft_conv_functional( |
| 23 | + in_channels: int, |
| 24 | + out_channels: int, |
| 25 | + kernel_size: Union[int, Iterable[int]], |
| 26 | + padding: Union[int, Iterable[int]], |
| 27 | + stride: Union[int, Iterable[int]], |
| 28 | + dilation: Union[int, Iterable[int]], |
| 29 | + groups: int, |
| 30 | + bias: bool, |
| 31 | + ndim: int, |
| 32 | + input_size: int, |
| 33 | +): |
| 34 | + torch_conv = getattr(f, f"conv{ndim}d") |
| 35 | + groups = _gcd(in_channels, _gcd(out_channels, groups)) |
| 36 | + |
| 37 | + batch_size = 2 # TODO: Make this non-constant? |
| 38 | + dims = ndim * [input_size] |
| 39 | + signal = torch.randn(batch_size, in_channels, *dims) |
| 40 | + kwargs = dict( |
| 41 | + bias=torch.randn(out_channels) if bias else None, |
| 42 | + padding=padding, |
| 43 | + stride=stride, |
| 44 | + dilation=dilation, |
| 45 | + groups=groups, |
| 46 | + ) |
| 47 | + |
| 48 | + kernel_size = to_ntuple(kernel_size, n=signal.ndim - 2) |
| 49 | + w0 = torch.randn(out_channels, in_channels // groups, *kernel_size, |
| 50 | + requires_grad=True) |
| 51 | + w1 = w0.detach().clone().requires_grad() |
| 52 | + |
| 53 | + y0 = fft_conv(signal, w0, **kwargs) |
| 54 | + y1 = torch_conv(signal, w1, **kwargs) |
| 55 | + |
| 56 | + _assert_almost_equal(y0, y1) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize("in_channels", [1, 2, 3]) |
| 60 | +@pytest.mark.parametrize("out_channels", [1, 2, 3]) |
| 61 | +@pytest.mark.parametrize("groups", [1, 2, 3]) |
| 62 | +@pytest.mark.parametrize("kernel_size", [1, 2, 3]) |
| 63 | +@pytest.mark.parametrize("padding", [0, 1]) |
| 64 | +@pytest.mark.parametrize("stride", [1, 2, 3]) |
| 65 | +@pytest.mark.parametrize("dilation", [1, 2, 3]) |
| 66 | +@pytest.mark.parametrize("bias", [True, False]) |
| 67 | +@pytest.mark.parametrize("ndim", [1, 2, 3]) |
| 68 | +@pytest.mark.parametrize("input_size", [7, 8]) |
| 69 | +def test_fft_conv_backward_functional( |
| 70 | + in_channels: int, |
| 71 | + out_channels: int, |
| 72 | + kernel_size: Union[int, Iterable[int]], |
| 73 | + padding: Union[int, Iterable[int]], |
| 74 | + stride: Union[int, Iterable[int]], |
| 75 | + dilation: Union[int, Iterable[int]], |
| 76 | + groups: int, |
| 77 | + bias: bool, |
| 78 | + ndim: int, |
| 79 | + input_size: int, |
| 80 | +): |
| 81 | + torch_conv = getattr(f, f"conv{ndim}d") |
| 82 | + groups = _gcd(in_channels, _gcd(out_channels, groups)) |
| 83 | + |
| 84 | + batch_size = 2 # TODO: Make this non-constant? |
| 85 | + dims = ndim * [input_size] |
| 86 | + signal = torch.randn(batch_size, in_channels, *dims) |
| 87 | + |
| 88 | + kernel_size = to_ntuple(kernel_size, n=signal.ndim - 2) |
| 89 | + w0 = torch.randn(out_channels, in_channels // groups, *kernel_size, |
| 90 | + requires_grad=True) |
| 91 | + w1 = w0.detach().clone().requires_grad_() |
| 92 | + |
| 93 | + b0 = torch.randn(out_channels, requires_grad=True) if bias else None |
| 94 | + b1 = b0.detach().clone().requires_grad_() if bias else None |
| 95 | + |
| 96 | + kwargs = dict( |
| 97 | + padding=padding, |
| 98 | + stride=stride, |
| 99 | + dilation=dilation, |
| 100 | + groups=groups, |
| 101 | + ) |
| 102 | + |
| 103 | + y0 = fft_conv(signal, w0, bias=b0, **kwargs) |
| 104 | + y1 = torch_conv(signal, w1, bias=b1, **kwargs) |
| 105 | + |
| 106 | + # Compute pseudo-loss and gradient |
| 107 | + y0.sum().backward() |
| 108 | + y1.sum().backward() |
| 109 | + |
| 110 | + _assert_almost_equal(w0.grad, w1.grad) |
| 111 | + |
| 112 | + if bias: |
| 113 | + _assert_almost_equal(b0.grad, b1.grad) |
0 commit comments