Skip to content

torch.fft.fftfreq behaves unexpectedly when run on MPS backend #135223

@corn-m

Description

@corn-m

🐛 Describe the bug

When generating a frequency axis using torch.fft.fftfreq(N) on the MPS backend, the generated output is different from what the CPU produces. The result should be a tensor of length N, which starts at 0.0 and linearly increases to 0.5 at index N/2, then jumps to -0.5 and increases linearly again until -0.0001.

The below example demonstrates the different outputs when being run on CPU vs MPS.

import torch

device = torch.device('cpu')
freq_cpu = torch.fft.fftfreq(10**4, device=device).to('mps').cpu()
device = torch.device('mps')
freq_mps = torch.fft.fftfreq(10**4, device=device).cpu()

print('CPU: fftfreq ranges from %f to %f' % (freq_cpu.min(), freq_cpu.max()) )
print('MPS: fftfreq ranges from %f to %f' % (freq_mps.min(), freq_mps.max()) )

Versions

PyTorch version: 2.4.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 14.2.1 (arm64)
GCC version: Could not collect
Clang version: 15.0.0 (clang-1500.1.0.2.5)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.6 (default, Nov 10 2023, 13:38:27) [Clang 15.0.0 (clang-1500.1.0.2.5)] (64-bit runtime)
Python platform: macOS-14.2.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M1

Versions of relevant libraries:
[pip3] numpy==2.0.1
[pip3] torch==2.4.0
[pip3] torchaudio==2.4.0
[conda] Could not collect

cc @mruberry @kulinseth @albanD @malfet @DenisVieriu97 @jhavukainen

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: fftmodule: mpsRelated to Apple Metal Performance Shaders frameworktriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions