Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coremltools/converters/mil/frontend/torch/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8749,6 +8749,12 @@ def fft_ifftn(context, node):
def fft_irfft(context, node):
"""Lowers torch.fft.irfft by the dialect op `complex_irfft` from complex_dialect_ops.py."""
input_data, n, dim, norm = _get_inputs(context, node, expected=[4])
# torch.fft.irfft accepts a real input and treats it as a complex tensor with a zero
# imaginary part. The complex_irfft dialect op only takes complex64, so promote a real
# input to complex here before lowering.
if not types.is_complex(input_data.dtype):
imag_data = mb.fill(shape=mb.shape(x=input_data), value=0.0)
input_data = mb.complex(real_data=input_data, imag_data=imag_data)
irfft_res = mb.complex_irfft(data=input_data, n=n, dim=dim, norm=norm)
context.add(irfft_res, node.name)

Expand Down
21 changes: 21 additions & 0 deletions coremltools/converters/mil/frontend/torch/test/test_torch_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -12973,6 +12973,27 @@ def forward(self, x):
(2, 3, 4), FftModel(), backend=backend, compute_unit=compute_unit
)

@pytest.mark.parametrize(
"compute_unit, backend, n, dim, norm",
itertools.product(
compute_units,
backends,
[None, 1, 5],
[0, 1, -1],
[None, "forward", "backward", "ortho"],
),
)
def test_fft_irfft_real_input(self, compute_unit: ct.ComputeUnit, backend, n, dim, norm):
# torch.fft.irfft accepts a real input (treated as complex with a zero imaginary part).
# The input is not wrapped with torch.complex here, unlike test_fft_basic. See GH #2130.
class FftModel(torch.nn.Module):
def forward(self, x):
return torch.fft.irfft(x, n=n, dim=dim, norm=norm)

TorchBaseTest.run_compare_torch(
(2, 3, 4), FftModel(), backend=backend, compute_unit=compute_unit
)

@pytest.mark.parametrize(
"compute_unit, backend",
itertools.product(
Expand Down