Skip to content

[Inductor][FFT] Support f16/bf16 inputs on CUDA/XPU via promotion to float32 - #180766

Closed
Alokksinha00 wants to merge 9 commits into
pytorch:mainfrom
Alokksinha00:fix/fft-bfloat16-xpu-cuda-support
Closed

Alokksinha00 wants to merge 9 commits into
pytorch:mainfrom
Alokksinha00:fix/fft-bfloat16-xpu-cuda-support

Conversation

@Alokksinha00

@Alokksinha00 Alokksinha00 commented Apr 19, 2026 •

Copy link
Copy Markdown

[FFT] Support float16/bfloat16 inputs on CUDA/XPU

float16 and bfloat16 have no native XPU FFT kernel, but can be
supported by silently promoting to float32 before the transform —
analogous to how complex32 (chalf) is promoted to complex64. The
promotion is device-gated to XPU only. CPU continues to reject both
with 'Unsupported dtype'.

On CUDA, bfloat16 is supported natively by cuFFT on SM_80+ (Ampere)
devices via CUDA_R_16BF/CUDA_C_16BF transform types, with the same
power-of-two size constraint as the existing float16 (CUDA_R_16F) path.
On pre-SM80 CUDA and ROCm, bfloat16 falls back to float32 promotion.
float16 on CUDA continues to use the pre-existing native cuFFT path.

Root cause: promote_type_fft() in SpectralOps.cpp did not handle
kBFloat16 or kHalf on XPU, causing them to fall through to the
TORCH_CHECK and fail with 'Unsupported dtype'. This broke rfft, irfft,
fft, stft, and all n-dimensional FFT variants when called with these
dtypes under torch.compile / torchinductor on XPU devices.

Changes:

  • SpectralOps.cpp: promote kBFloat16 and kHalf to kFloat on XPU in
    promote_type_fft(); pass both through unchanged on CUDA to let the
    cuFFT planner handle them natively or fall back
  • SpectralOps.cpp: add kComplexHalf -> kComplexFloat upcast for complex
    input path (analogous fix for complex32)
  • cuda/CuFFTPlanCache.h: wire in CUDA_R_16BF/CUDA_C_16BF cuFFT types
    for bfloat16 on SM_80+ CUDA; guarded with #if !defined(USE_ROCM)
    since hipFFT does not support these types
  • cuda/SpectralOps.cpp: native bfloat16 R2C path for SM_80+ allocates a
    ComplexHalf proxy buffer (same 4-byte layout as CUDA_C_16BF) and
    upcasts to ComplexFloat; falls back to float32 promotion on ROCm,
    pre-SM80 CUDA, and multi-dim non-optimised paths
  • torch/_refs/fft.py: mirror both dtype allowances in _promote_type_fft
  • test/test_spectral_ops.py: update test_fft_half_and_bfloat16_errors
    to skip bfloat16 on CUDA/XPU (no longer errors); add new positive
    test test_fft_bfloat16_promoted_on_cuda asserting bfloat16 input
    produces complex64 output on CUDA

Fixes: torch.fft.rfft/irfft/stft with float16/bfloat16 input on XPU/CUDA

cc @mruberry @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

@pytorch-bot

pytorch-bot Bot commented Apr 19, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/180766

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Unclassified Failure

As of commit 6390770 with merge base 9ac037d (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

  • xpu / linux-noble-xpu-n-py3.10 / test (default, 1, 12, linux.idc.xpu) (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    User: arn:aws:sts::308535385114:assumed-role/gha_workflow_s3_and_ecr_read_only/gha-ecr-login is not authorized to perform: s3:PutObject on resource: "arn:aws:s3:::gha-artifacts/pytorch/pytorch/28593701914/3/artifact/logs-test-default-1-12-linux.idc.xpu_84938345036.zip" because no identity-based policy allows the s3:PutObject action

    AI verdict: AI verdict: not related

    The failure is an AWS IAM permissions error preventing S3 artifact upload, completely unrelated to the code change which modified spectral ops for ROCm compatibility.

    Full reasoning on HUD →

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@Alokksinha00

Copy link
Copy Markdown
Author

@pytorchbot label "module: fft"

@Alokksinha00

Copy link
Copy Markdown
Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Apr 19, 2026
@Skylion007

Copy link
Copy Markdown
Collaborator

CuFFT supports bfloat16 kernels on SM80+? https://docs.nvidia.com/cuda/cufft/ just wire in the missing templates

@EikanWang

Copy link
Copy Markdown
Collaborator

@Alokksinha00 , besides bfloat16, should this PR add float16 support as well?

@zou3519 zou3519 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Apr 20, 2026
@zou3519
zou3519 requested a review from EikanWang April 20, 2026 13:22
@Alokksinha00

Copy link
Copy Markdown
Author

@Alokksinha00 , besides bfloat16, should this PR add float16 support as well?
CUDA (<SM80): bfloat16 falls back to float32 promotion.float16 continues to work on SM53+ — unchanged.
CUDA (SM80+): will update in the next patch
XPU: Both bfloat16 and float16 now promote to float32 in promote_type_fft()
CPU: Both correctly error as before.

@Alokksinha00

Copy link
Copy Markdown
Author

CuFFT supports bfloat16 kernels on SM80+? https://docs.nvidia.com/cuda/cufft/ just wire in the missing templates

updating in the next patch

@Alokksinha00
Alokksinha00 force-pushed the fix/fft-bfloat16-xpu-cuda-support branch from 60ae0f5 to 6af0a98 Compare April 20, 2026 17:55
@Alokksinha00 Alokksinha00 changed the title [FFT] Support bfloat16 inputs on CUDA/XPU via promotion to float32 [FFT] Support f16/bf16 inputs on CUDA/XPU via promotion to float32 Apr 20, 2026
@pytorch-bot

pytorch-bot Bot commented Apr 21, 2026

Copy link
Copy Markdown

❌ 🤖 pytorchbot command failed:

@pytorchbot: error: argument command: invalid choice: 'ci' (choose from 'merge', 'revert', 'rebase', 'label', 'drci', 'lint', 'fix-lint', 'apply-lint', 'cherry-pick')

usage: @pytorchbot [-h]
                   
                   {merge,revert,rebase,label,drci,lint,fix-lint,apply-lint,cherry-pick}
                   ...

Try @pytorchbot --help for more info.

@Alokksinha00

Alokksinha00 commented May 12, 2026 •

Copy link
Copy Markdown
Author

@EikanWang , @guangyey , could you please approve the pending CI workflows to unblock the run and help in merge ?

@Alokksinha00

Copy link
Copy Markdown
Author

@pytorchbot label "module: inductor"

@Alokksinha00 Alokksinha00 changed the title [FFT] Support f16/bf16 inputs on CUDA/XPU via promotion to float32 [Inductor][FFT] Support f16/bf16 inputs on CUDA/XPU via promotion to float32 May 13, 2026
@guangyey guangyey added the ciflow/trunk Trigger trunk jobs on your pull request label May 14, 2026
@pytorch-bot

pytorch-bot Bot commented May 14, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@guangyey

Copy link
Copy Markdown
Collaborator

The code base is out-of-date. Just rebase it.
@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased fix/fft-bfloat16-xpu-cuda-support onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix/fft-bfloat16-xpu-cuda-support && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the fix/fft-bfloat16-xpu-cuda-support branch from 6af0a98 to 5f5166e Compare May 14, 2026 05:39
@guangyey
guangyey requested a review from jansel July 3, 2026 05:52
@guangyey

guangyey commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Hi @jansel, the failures are resolved. Could you please help take another look.

@guangyey

guangyey commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: xpu / linux-noble-xpu-n-py3.10 / test (default, 1, 12, linux.idc.xpu)

Details for Dev Infra team Raised by workflow job

@guangyey

guangyey commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: xpu / linux-noble-xpu-n-py3.10 / test (default, 1, 12, linux.idc.xpu)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Jul 16, 2026
…float32 (pytorch#180766)

[FFT] Support float16/bfloat16 inputs on CUDA/XPU

float16 and bfloat16 have no native XPU FFT kernel, but can be
supported by silently promoting to float32 before the transform —
analogous to how complex32 (chalf) is promoted to complex64. The
promotion is device-gated to XPU only. CPU continues to reject both
with 'Unsupported dtype'.

On CUDA, bfloat16 is supported natively by cuFFT on SM_80+ (Ampere)
devices via CUDA_R_16BF/CUDA_C_16BF transform types, with the same
power-of-two size constraint as the existing float16 (CUDA_R_16F) path.
On pre-SM80 CUDA and ROCm, bfloat16 falls back to float32 promotion.
float16 on CUDA continues to use the pre-existing native cuFFT path.

Root cause: promote_type_fft() in SpectralOps.cpp did not handle
kBFloat16 or kHalf on XPU, causing them to fall through to the
TORCH_CHECK and fail with 'Unsupported dtype'. This broke rfft, irfft,
fft, stft, and all n-dimensional FFT variants when called with these
dtypes under torch.compile / torchinductor on XPU devices.

Changes:
- SpectralOps.cpp: promote kBFloat16 and kHalf to kFloat on XPU in
  promote_type_fft(); pass both through unchanged on CUDA to let the
  cuFFT planner handle them natively or fall back
- SpectralOps.cpp: add kComplexHalf -> kComplexFloat upcast for complex
  input path (analogous fix for complex32)
- cuda/CuFFTPlanCache.h: wire in CUDA_R_16BF/CUDA_C_16BF cuFFT types
  for bfloat16 on SM_80+ CUDA; guarded with #if !defined(USE_ROCM)
  since hipFFT does not support these types
- cuda/SpectralOps.cpp: native bfloat16 R2C path for SM_80+ allocates a
  ComplexHalf proxy buffer (same 4-byte layout as CUDA_C_16BF) and
  upcasts to ComplexFloat; falls back to float32 promotion on ROCm,
  pre-SM80 CUDA, and multi-dim non-optimised paths
- torch/_refs/fft.py: mirror both dtype allowances in _promote_type_fft
- test/test_spectral_ops.py: update test_fft_half_and_bfloat16_errors
  to skip bfloat16 on CUDA/XPU (no longer errors); add new positive
  test test_fft_bfloat16_promoted_on_cuda asserting bfloat16 input
  produces complex64 output on CUDA

Fixes: torch.fft.rfft/irfft/stft with float16/bfloat16 input on XPU/CUDA

Pull Request resolved: pytorch#180766
Approved by: https://github.com/EikanWang, https://github.com/guangyey, https://github.com/jansel

Co-authored-by: sachinkademane <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
DamJanusz pushed a commit to DamJanusz/pytorch that referenced this pull request Jul 21, 2026
…float32 (pytorch#180766)

[FFT] Support float16/bfloat16 inputs on CUDA/XPU

float16 and bfloat16 have no native XPU FFT kernel, but can be
supported by silently promoting to float32 before the transform —
analogous to how complex32 (chalf) is promoted to complex64. The
promotion is device-gated to XPU only. CPU continues to reject both
with 'Unsupported dtype'.

On CUDA, bfloat16 is supported natively by cuFFT on SM_80+ (Ampere)
devices via CUDA_R_16BF/CUDA_C_16BF transform types, with the same
power-of-two size constraint as the existing float16 (CUDA_R_16F) path.
On pre-SM80 CUDA and ROCm, bfloat16 falls back to float32 promotion.
float16 on CUDA continues to use the pre-existing native cuFFT path.

Root cause: promote_type_fft() in SpectralOps.cpp did not handle
kBFloat16 or kHalf on XPU, causing them to fall through to the
TORCH_CHECK and fail with 'Unsupported dtype'. This broke rfft, irfft,
fft, stft, and all n-dimensional FFT variants when called with these
dtypes under torch.compile / torchinductor on XPU devices.

Changes:
- SpectralOps.cpp: promote kBFloat16 and kHalf to kFloat on XPU in
  promote_type_fft(); pass both through unchanged on CUDA to let the
  cuFFT planner handle them natively or fall back
- SpectralOps.cpp: add kComplexHalf -> kComplexFloat upcast for complex
  input path (analogous fix for complex32)
- cuda/CuFFTPlanCache.h: wire in CUDA_R_16BF/CUDA_C_16BF cuFFT types
  for bfloat16 on SM_80+ CUDA; guarded with #if !defined(USE_ROCM)
  since hipFFT does not support these types
- cuda/SpectralOps.cpp: native bfloat16 R2C path for SM_80+ allocates a
  ComplexHalf proxy buffer (same 4-byte layout as CUDA_C_16BF) and
  upcasts to ComplexFloat; falls back to float32 promotion on ROCm,
  pre-SM80 CUDA, and multi-dim non-optimised paths
- torch/_refs/fft.py: mirror both dtype allowances in _promote_type_fft
- test/test_spectral_ops.py: update test_fft_half_and_bfloat16_errors
  to skip bfloat16 on CUDA/XPU (no longer errors); add new positive
  test test_fft_bfloat16_promoted_on_cuda asserting bfloat16 input
  produces complex64 output on CUDA

Fixes: torch.fft.rfft/irfft/stft with float16/bfloat16 input on XPU/CUDA

Pull Request resolved: pytorch#180766
Approved by: https://github.com/EikanWang, https://github.com/guangyey, https://github.com/jansel

Co-authored-by: sachinkademane <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
torchxpubot pushed a commit to intel/torch-xpu-ops that referenced this pull request Jul 24, 2026
Issues reported here: #4268
After changes were made here:
pytorch/pytorch#180766

According to documentation
([meta.md](https://github.com/pytorch/pytorch/blob/main/docs/source/meta.md)):

In some cases, not all device types (e.g., CPU and CUDA) have exactly
the same output metadata for an operation; we typically prefer
representing the CUDA behavior faithfully in this situation.

Disabled test cases for FFT with float16 in test_meta_xpu.py
Reported test cases in test_ops_xpu.py and test_spectral_ops_xpu.py
cannot be disabled because those files are not fully copied to XPU,
running native pytorch tests.
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…float32 (pytorch#180766)

[FFT] Support float16/bfloat16 inputs on CUDA/XPU

float16 and bfloat16 have no native XPU FFT kernel, but can be
supported by silently promoting to float32 before the transform —
analogous to how complex32 (chalf) is promoted to complex64. The
promotion is device-gated to XPU only. CPU continues to reject both
with 'Unsupported dtype'.

On CUDA, bfloat16 is supported natively by cuFFT on SM_80+ (Ampere)
devices via CUDA_R_16BF/CUDA_C_16BF transform types, with the same
power-of-two size constraint as the existing float16 (CUDA_R_16F) path.
On pre-SM80 CUDA and ROCm, bfloat16 falls back to float32 promotion.
float16 on CUDA continues to use the pre-existing native cuFFT path.

Root cause: promote_type_fft() in SpectralOps.cpp did not handle
kBFloat16 or kHalf on XPU, causing them to fall through to the
TORCH_CHECK and fail with 'Unsupported dtype'. This broke rfft, irfft,
fft, stft, and all n-dimensional FFT variants when called with these
dtypes under torch.compile / torchinductor on XPU devices.

Changes:
- SpectralOps.cpp: promote kBFloat16 and kHalf to kFloat on XPU in
  promote_type_fft(); pass both through unchanged on CUDA to let the
  cuFFT planner handle them natively or fall back
- SpectralOps.cpp: add kComplexHalf -> kComplexFloat upcast for complex
  input path (analogous fix for complex32)
- cuda/CuFFTPlanCache.h: wire in CUDA_R_16BF/CUDA_C_16BF cuFFT types
  for bfloat16 on SM_80+ CUDA; guarded with #if !defined(USE_ROCM)
  since hipFFT does not support these types
- cuda/SpectralOps.cpp: native bfloat16 R2C path for SM_80+ allocates a
  ComplexHalf proxy buffer (same 4-byte layout as CUDA_C_16BF) and
  upcasts to ComplexFloat; falls back to float32 promotion on ROCm,
  pre-SM80 CUDA, and multi-dim non-optimised paths
- torch/_refs/fft.py: mirror both dtype allowances in _promote_type_fft
- test/test_spectral_ops.py: update test_fft_half_and_bfloat16_errors
  to skip bfloat16 on CUDA/XPU (no longer errors); add new positive
  test test_fft_bfloat16_promoted_on_cuda asserting bfloat16 input
  produces complex64 output on CUDA

Fixes: torch.fft.rfft/irfft/stft with float16/bfloat16 input on XPU/CUDA

Pull Request resolved: pytorch#180766
Approved by: https://github.com/EikanWang, https://github.com/guangyey, https://github.com/jansel

Co-authored-by: sachinkademane <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
astachowiczhabana added a commit to astachowiczhabana/pytorch that referenced this pull request Sep 11, 2026
oneMKL provides no half-precision DFT, so torch-xpu-ops emulates one: it
upcasts Half to Float, runs both the transform and the normalization in
float32, and rounds to complex32 once at the end. Separately, pytorch#171231 added
"xpu" to maybe_support_half in torch/_refs/fft.py, which tells the reference
implementation that XPU supports half natively. The ref consequently stays in
float16 and rounds at every prim boundary, while the eager path it is compared
against quietly computed in float32.

test_python_ref, test_python_ref_torch_fallback and test_python_ref_executor
assert ref_distance <= torch_distance against a float64 baseline with no
tolerance, so the ref loses that comparison: 1.18x worse for ihfftn and 1.02x
for ihfft2, on 1 of 10 OpInfo samples. The XPU kernel is not wrong here; it is
more accurate than true float16 would be. No user-visible path is affected,
because inductor promotes float16 FFT to float32 (pytorch#180766) rather than lowering
through these refs, and compiled output is bit-identical to eager.

This adds five xpu-scoped, float16-only skips beside the existing cuda ones for
_refs.fft.ihfft2 and _refs.fft.ihfftn. It is a pure addition and leaves CUDA
behaviour untouched.

Three routes were available. Widening the existing decorators to
device_type=("cuda", "xpu") is terser, but it would attach comments describing
CUDA's native-float16 rounding to a structurally different XPU cause, so the
skips are added as separate entries carrying their own reason - which also
matches the pattern established by pytorch#176688 and pytorch#178849. Adding a tolerance to
the assertion itself would treat the cause and let CUDA, ROCm and XPU all drop
skips, but that single assertion guards every test_python_ref case on every
backend and dtype, so it needs primTorch ownership rather than an XPU change.
Making the refs compute in float32 and cast back to complex32 is the only route
that preserves coverage, but it puts device-conditional numerics inside a
deliberately device-agnostic reference library; that is left for discussion on
the linked issue.

These three tests are currently unreachable on XPU because of the @skipXPU
decorators on them in test/test_ops.py, which are being removed incrementally
(pytorch#159944 introduced them, pytorch#176688 and pytorch#178849 have removed most). Declaring the
skips here is the prerequisite for removing the remaining ones without turning
XPU CI red.

Test Plan

Verified on Intel Arc B570 Graphics (Battlemage). With both skip sources
disabled, exactly five cases fail; with this change they are skipped and the
rest of the selection still passes.

```
cd third_party/torch-xpu-ops
python -m pytest test/xpu/test_ops_xpu.py \
  -k "python_ref and (ihfft2 or ihfftn)" -v --timeout=600
```

```
before (both skip sources disabled):  5 failed, 11 passed
after  (this change):                 0 failed, 68 passed, 14 skipped
```

The skip set was additionally cross-checked by evaluating
DecorateInfo.is_active over both ops, all three tests and all ten instantiated
dtypes for device_type in (cuda, xpu): it matches the five measured failures
exactly, and CUDA's set is unchanged.

```
lintrunner -a torch/testing/_internal/opinfo/definitions/fft.py
ok No lint issues.
```

Authored with the assistance of an AI coding agent; the change and the test
output above come from a run on Intel GPU hardware and were reviewed by a human
before submission.

AI-Assisted: Claude Code (xpu-workspace ai_assisted agent, issue pytorch#174)
Co-Authored-By: Claude Opus 5 <[email protected]>
minmingzhu pushed a commit to minmingzhu/torch-xpu-ops that referenced this pull request Sep 15, 2026
Issues reported here: intel#4268
After changes were made here:
pytorch/pytorch#180766

According to documentation
([meta.md](https://github.com/pytorch/pytorch/blob/main/docs/source/meta.md)):

In some cases, not all device types (e.g., CPU and CUDA) have exactly
the same output metadata for an operation; we typically prefer
representing the CUDA behavior faithfully in this situation.

Disabled test cases for FFT with float16 in test_meta_xpu.py
Reported test cases in test_ops_xpu.py and test_spectral_ops_xpu.py
cannot be disabled because those files are not fully copied to XPU,
running native pytorch tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks Merged module: fft module: inductor open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

10 participants