-
Notifications
You must be signed in to change notification settings - Fork 988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fast Metal FFT for all N #981
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 tasks
Do you mind rebasing this @barronalex ? |
barronalex
force-pushed
the
ab-metal-2dfft
branch
from
April 12, 2024 11:49
7be2e7c
to
fa8047b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proposed changes
A feature-complete Metal FFT that's faster than both CPU and PyTorch MPS in the majority of 1D cases.
Fully functional, but still needs some clean up.
Resolves #399.
Supports
fft
,ifft
,rfft
,irfft
fft2
,ifft2
,rfft2
,irfft2
,fftn
,ifftn
,rfftn
,irfftn
Performance
For
N < 1024
, 1D FFTs on my M1 Max:We're only behind MPS on some multiples of 7 and all multiples of 11 and 13:
Our Bluestein's implementation is significantly more efficient for N < 1024:
Note: For the sake of time, I ran at a slightly lower batch size than is required to max out the bandwidth for the powers of 2. I'll run a full one shortly, but in my experiments so far the relative speeds seem to hold.
Implementation Details
For
N <= 2048
whose prime factors are all<= 7
:For all other
N <= 1024
:For
N > 1024
:> 1024
, we use a manual version of Bluestein's implemented with MLX opsRFFT:
Areas for Improvement
Codelet optimizations and additions
The radix codelets are extremely naive currently and could be replaced with hand-tuned or compiled ones that perform fewer than O(N^2) operations. We should also add radix11 and radix13 codelets to match MPS and VkFFT.
Performance on ND and four step FFT cases
These have quite a few unnecessary copies currently. A fused implementation incorporating the transpose and twiddle factors would bring us closer to the max bandwidth.
Accuracy
Accuracy is comparable to MPS' implementation but about an order of magnitude behind
pocketfft
. More careful twiddle factor computation inspired bypocketfft
could help here. Precision also suffers a bit on very large N. Computing the twiddle factors in float64 as we do with Bluestein's would help.IRFFT
irfft
on GPU currently only works for outputs ofrfft
(there are a couple exceptions in the tests to account for this).Convolution theorem
The fused Bluestein's implementation contains a convolution implemented with FFTs via the convolution theorem. For larger kernel sizes we might want to adapt this and add it to the main convolution implementation as suggested in #811.
Checklist
Put an
x
in the boxes that apply.pre-commit run --all-files
to format my code / installed pre-commit prior to committing changes