[split from #3093] NestedTensor SDPA on XPU has no viable backend (jagged path)
- Dominant language
- Python
- Stars
- 113
- Forks
- 128
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 107
Description
### 🐛 Describe the bug
test_cases:
- op_ut,third_party.torch-xpu-ops.test.xpu.test_nestedtensor_xpu.TestNestedTensorSubclassXPU,test_sdpa_with_packed_in_proj_xpu_bfloat16
---
## Parent Issue
Split from #3093 — XPU does not support NestedTensor for SDPA operations.
## Affected Test Cases
- `test_nestedtensor_xpu.py::TestNestedTensorSubclassXPU::test_sdpa_with_packed_in_proj_xpu_bfloat16`
(The closely-related `test_dummy_mha_with_nt_use_legacy_api_{True,False}_xpu` cases mentioned in the parent issue are already skipped on XPU since torch-xpu-ops commit `7625ae9b` via `@skipXPUIf(True, "XPU does not support NestedTensor for SDPA operations.")`. They no longer reproduce.)
## Root Cause
`F.scaled_dot_product_attention` on jagged-NestedTensor inputs is dispatched to `jagged_scaled_dot_product_attention` in `torch/nested/_internal/sdpa.py`, which calls `_select_sdp_backend` (line 293). On XPU all four candidate backends are rejected:
| Backend | Why it fails on XPU |
|---|---|
| `CUDNN_ATTENTION` | `torch._C._can_use_cudnn_attention` is guarded `#ifdef USE_CUDA` only in `torch/csrc/Module.cpp` (always returns `False` on XPU). |
| `FLASH_ATTENTION` | `aten/src/ATen/native/transformers/xpu/sdp_utils.cpp::can_use_flash_attention` includes `check_nested_tensor` (in `sdp_utils_cpp.h`), which unconditionally rejects nested inputs. CUDA's version splits constraints into a separate nested branch and accepts them. |
| `EFFICIENT_ATTENTION` | `torch._C._can_use_mem_efficient_attention` is guarded `#ifdef USE_CUDA` only. |
| `MATH` | This specific test produces non-contiguous q/k/v (`# these are non-contiguous to trigger _is_safe_to_get_storage_as_tensor()`); `_can_use_math_sdpa_jagged` rejects them via the `transpose(1,2).is_contiguous()` check. |
The test does **not** wrap the call in any `sdpa_kernel(...)` context, so the failure mode here differs from the vmap `backend3` cases (sub-issue 1): math is enabled but rejected by the contiguity check, not by user-disabling.
Even when `_can_use_math_sdpa_jagged` accepts inputs, the broader gap remains: XPU has no XPU-specific `_flash_attention_forward` (CUDA-only dispatch in `aten/src/ATen/native/native_functions.yaml`), no efficient/cuDNN attention, and no jagged constraint relaxation. The in-flight work pytorch/pytorch#180857 + intel/torch-xpu-ops#3405 (blocked on intel/torch-xpu-ops#3140) is intended to add an XPU varlen flash kernel and the jagged-NT plumbing.
## Reproducer
```bash
cd third_party/torch-xpu-ops/test/xpu
python -m pytest -sxv test_nestedtensor_xpu.py \
-k "test_sdpa_with_packed_in_proj_xpu_bfloat16"
```
## Representative Error
```
W0430 08:28:17.712000 sdpa.py:327] Memory efficient kernel not used because:
W0430 08:28:17.713000 sdpa.py:330] Flash attention kernel not used because:
W0430 08:28:17.713000 sdpa.py:333] Math attention kernel not used because:
W0430 08:28:17.713000 sdpa.py:280] If inputs are nested tensors they must be contiguous after transposing.
W0430 08:28:17.713000 sdpa.py:335] cuDNN attention kernel not used because:
.../test_nestedtensor_xpu.py:7193: UserWarning: Both fused kernels of cpp version currently do not support Nested Tensor inputs. (Triggered internally at .../aten/src/ATen/native/transformers/sdp_utils_cpp.h:230.)
output = F.scaled_dot_product_attention(q, k, v, attn_mask=None)
FAILED
=================================== FAILURES ===================================
____ TestNestedTensorSubclassXPU.test_sdpa_with_packed_in_proj_xpu_bfloat16 ____
Traceback (most recent call last):
File ".../test/xpu/test_nestedtensor_xpu.py", line 7193, in test_sdpa_with_packed_in_proj
output = F.scaled_dot_product_attention(q, k, v, attn_mask=None)
File ".../torch/nested/_internal/nested_tensor.py", line 396, in __torch_function__
return jagged_torch_function(func, *args, **kwargs)
File ".../torch/nested/_internal/ops.py", line 390, in jagged_torch_function
return jagged_scaled_dot_product_attention(*args, **kwargs)
File ".../torch/nested/_internal/sdpa.py", line 934, in jagged_scaled_dot_product_attention
raise RuntimeError(
RuntimeError: No viable backend for scaled_dot_product_attention was found.
```
## Recommended Fix Direction
- **Short term**: add `@skipXPUIf(True, "XPU does not support NestedTensor for SDPA operations.")` to `test_sdpa_with_packed_in_proj` (mirroring the existing decorator on `test_dummy_mha_with_nt`).
- **Long term**: rely on pytorch/pytorch#180857 + intel/torch-xpu-ops#3405 to land XPU varlen FlashAttention, then either drop `check_nested_tensor` from XPU `can_use_flash_attention` and add an XPU dispatch entry for `_flash_attention_forward`, or implement an `OVERRIDEABLE` SDPA backend selector for XPU.
## Shared Context
- Reproduced 2026-04-30 on `pytorch` HEAD (current branch `main`) with `torch-xpu-ops` pinned at `d67a87783002c17786e9501c8c67b360479f6bb7`.
- Hardware: Intel(R) Data Center GPU Max 1550, 2 devices.
### Versions
PyTorch version: 2.13.0a0+git9b87139
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A
OS: Ubuntu 24.04.4 LTS (x86_64)
GCC version: (Ubuntu 14.2.0-4ubuntu2~24.04.1) 14.2.0
Clang version: Could not collect
CMake version: version 3.31.6
Libc version: glibc-2.39
Python version: 3.12.3 (main, Mar 3 2026, 12:15:18) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.17.0-14-generic-x86_64-with-glibc2.39
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
Is XPU available: True
XPU used to build PyTorch: 20250303
Intel GPU driver version:
* libze1: 1.24.0.0-1146~24.04
* intel-opencl-icd: 25.18.33578.51-1146~24.04
Intel GPU models onboard:
* Intel(R) Data Center GPU Max 1550
Intel GPU models detected:
* [0] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD5, uuid=8680d50b-2f00-0000-8c00-000000000001, driver_version='1.6.33578+51', total_memory=65520MB, local_mem_size=128KB, max_compute_units=512, memory_clock_rate=3200MHz, memory_bus_width=64-bit, gpu_eu_count=512, gpu_subslice_count=64, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [1] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD5, uuid=8680d50b-2f00-0000-8c00-000000000002, driver_version='1.6.33578+51', total_memory=65520MB, local_mem_size=128KB, max_compute_units=512, memory_clock_rate=3200MHz, memory_bus_width=64-bit, gpu_eu_count=512, gpu_subslice_count=64, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A
CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 57 bits virtual
Byte Order: Little Endian
CPU(s): 64
On-line CPU(s) list: 0-63
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Platinum 8352Y CPU @ 2.20GHz
CPU family: 6
Model: 106
Thread(s) per core: 2
Core(s) per socket: 32
Socket(s): 1
Stepping: 6
CPU(s) scaling MHz: 24%
CPU max MHz: 3400.0000
CPU min MHz: 800.0000
BogoMIPS: 4400.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 1.5 MiB (32 instances)
L1i cache: 1 MiB (32 instances)
L2 cache: 40 MiB (32 instances)
L3 cache: 48 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-63
Vulnerability Gather data sampling: Vulnerable
Vulnerability Ghostwrite: Not affected
Vulnerability Indirect target selection: Mitigation; Aligned branch/return thunks
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Old microcode: Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop
Vulnerability Srbds: Not affected
Vulnerability Tsa: Not affected
Vulnerability Tsx async abort: Not affected
Vulnerability Vmscape: Not affected
Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.15.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] intel-cmplr-lib-ur==2025.3.3
[pip3] intel-openmp==2025.3.3
[pip3] mkl==2024.2.0
[pip3] mkl-include==2024.2.0
[pip3] mkl-static==2024.2.0
[pip3] mypy==1.13.0
[pip3] mypy_extensions==1.1.0
[pip3] numpy==2.1.0
[pip3] onemkl-license==2025.3.1
[pip3] onnx==1.20.0
[pip3] onnx-ir==0.1.16
[pip3] onnxscript==0.6.2
[pip3] optree==0.13.0
[pip3] tbb==2021.13.1
[pip3] tbb-devel==2022.3.1
[pip3] tcmlib==1.4.1
[pip3] torch==2.13.0a0+git9b87139
[pip3] triton==3.7.0+git9c288bc5
[pip3] triton-xpu==3.7.1+git21033c4e
[pip3] umf==1.0.3
Contributor guide
Assessment
This issue has not been assessed yet.