-
Notifications
You must be signed in to change notification settings - Fork 496
Add fft 1d OP #1243
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
Closed
Closed
Add fft 1d OP #1243
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
96d0b83
add triton FFT
huangyiqun 4901944
update fft_1d
huangyiqun c06c88f
update fft_1d
huangyiqun 5c6d21d
fix fft_1d
huangyiqun 1716f6f
fix format
huangyiqun 1c0f066
fix code review
huangyiqun a877aa1
format code
huangyiqun 0e9d11a
unable fft_1d
huangyiqun 39a0dde
update comments
huangyiqun 9f3329e
revert the computation logic for first_idx
huangyiqun 7bde431
keep the list sorted
huangyiqun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import math | ||
|
|
||
| import torch | ||
| import torch.profiler | ||
| import triton | ||
| import triton.language as tl | ||
|
|
||
|
|
||
| @triton.jit | ||
| def bit_reverse_kernel(real_in, imag_in, real_out, imag_out, n): | ||
| """do reverse first: input[i] -> output[bit_reverse(i)]""" | ||
| tid = tl.program_id(0) | ||
| if tid >= n: | ||
| return | ||
|
|
||
| # compute bits & reverse | ||
| temp_n = n | ||
| idx = tid | ||
| rev_idx = 0 | ||
| temp_idx = idx | ||
| while temp_n > 1: | ||
| temp_n //= 2 | ||
| rev_idx = (rev_idx << 1) | (temp_idx & 1) | ||
| temp_idx = temp_idx >> 1 | ||
|
huangyiqun marked this conversation as resolved.
|
||
|
|
||
| val_real = tl.load(real_in + idx) | ||
| val_imag = tl.load(imag_in + idx) | ||
| tl.store(real_out + rev_idx, val_real) | ||
| tl.store(imag_out + rev_idx, val_imag) | ||
|
|
||
|
|
||
| @triton.jit | ||
| def fft_stage_kernel(real_ptr, imag_ptr, n, stage): | ||
| """iterate the FFT stage""" | ||
| PI = math.pi | ||
| tid = tl.program_id(0) | ||
|
|
||
| if tid >= n // 2: | ||
| return | ||
|
|
||
| # compute current parameter | ||
| half_block = 1 << (stage - 1) # 2^stage | ||
|
|
||
| # Each thread processes one butterfly pair | ||
| butterfly_group = tid // half_block | ||
| pos_in_group = tid % half_block | ||
|
|
||
| # compute the index of two elements in butterfly pair | ||
| first_idx = butterfly_group * half_block * 2 + pos_in_group | ||
| second_idx = first_idx + half_block | ||
|
|
||
| if second_idx >= n: | ||
| return | ||
|
|
||
| # load | ||
| a_real = tl.load(real_ptr + first_idx) | ||
| a_imag = tl.load(imag_ptr + first_idx) | ||
| b_real = tl.load(real_ptr + second_idx) | ||
| b_imag = tl.load(imag_ptr + second_idx) | ||
|
|
||
| # calculate complex amplitude | ||
| angle = PI * pos_in_group / half_block | ||
| w_real = tl.cos(-angle) | ||
| w_imag = tl.sin(-angle) | ||
|
|
||
| tw_real = b_real * w_real - b_imag * w_imag | ||
| tw_imag = b_real * w_imag + b_imag * w_real | ||
|
|
||
| # butterfly | ||
| result_a_real = a_real + tw_real | ||
| result_a_imag = a_imag + tw_imag | ||
| result_b_real = a_real - tw_real | ||
| result_b_imag = a_imag - tw_imag | ||
|
|
||
| # store | ||
| tl.store(real_ptr + first_idx, result_a_real) | ||
| tl.store(imag_ptr + first_idx, result_a_imag) | ||
| tl.store(real_ptr + second_idx, result_b_real) | ||
| tl.store(imag_ptr + second_idx, result_b_imag) | ||
|
|
||
|
|
||
| def fft_1d(x: torch.Tensor, output: torch.Tensor) -> torch.Tensor: | ||
| N = x.shape[0] | ||
| # make sure N is an integer power of 2 | ||
| assert N > 0 and (N & (N - 1)) == 0 | ||
|
|
||
| x_real = x.real.clone() | ||
| x_imag = x.imag.clone() | ||
|
|
||
| temp_real = torch.zeros_like(x_real) | ||
| temp_imag = torch.zeros_like(x_imag) | ||
| bit_reverse_kernel[(N,)](x_real, x_imag, temp_real, temp_imag, N) | ||
|
|
||
| x_real.copy_(temp_real) | ||
| x_imag.copy_(temp_imag) | ||
|
|
||
| log2n = N.bit_length() - 1 | ||
| for stage in range(1, log2n + 1): | ||
| fft_stage_kernel[(N // 2,)](x_real, x_imag, N, stage) | ||
|
|
||
| output.real.copy_(x_real) | ||
| output.imag.copy_(x_imag) | ||
| return output | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.