Repository navigation
[varlen] Add fp8 descales to varlen_attn and varlen_attn_out, and run fp8 SDPA on FA4 - #206590
felipemello1 wants to merge 6 commits into
Conversation
…rd ops `_flash_attention_forward.quantized` (the fp8 flash attention forward) now takes the kwarg-only `block_table` and `num_splits` that its default overload already has. A new overload, `_flash_attention_forward_no_dropout_inplace.quantized`, is the fp8 form of the `out=` op behind `varlen_attn_out`. Both CUDA kernels stay stubs that raise until FA3 or FA4 is activated, as the existing `.quantized` op does. Their meta functions give a bfloat16 output for float8_e4m3fn and float8_e5m2. The AOTI C shim gets a v2 of the first op and the new op, both since 2.16. The new arguments are kwarg-only with defaults, so existing callers keep working. The schema BC check passes against a schema dump of main. Test Plan: python test/forward_backward_compatibility/dump_all_function_schemas.py --filename main_schemas.txt # on main python test/forward_backward_compatibility/check_forward_backward_compatibility.py --existing-schemas main_schemas.txt python -m pytest test/test_decomp.py::HasDecompTest::test_has_decomposition Written with Claude Code (an AI assistant).
FA4's bridge now registers `_flash_attention_forward.quantized` and `_flash_attention_forward_no_dropout_inplace.quantized`. They take float8_e4m3fn or float8_e5m2 query, key and value with float32 descales of shape (batch, num_kv_heads), and write a bfloat16 output. FA4 runs the fp8 forward on SM100-family GPUs only. The descales reach `_flash_attn_fwd` as `scales=TensorwiseScales(...)` on flash-attn-4 4.0.0b39 and later, and as `q_descale`, `k_descale` and `v_descale` on older releases. The default overloads keep the float16 and bfloat16 rule. FA3's bridge registers the new `out=` overload with the function it already uses for the default one, which now passes the descales through. Test Plan: the varlen tests in the next commit run these ops. Written with Claude Code (an AI assistant).
`varlen_attn` and `varlen_attn_out` take kwarg-only `q_descale`, `k_descale` and `v_descale`. With fp8 query, key and value they call the `.quantized` flash attention ops, so paged KV (`block_table`, `seqused_k`) and `num_splits` work in fp8 too. The output is bfloat16. The inputs are checked before any kernel runs: descales need an fp8 query; query, key and value share one fp8 dtype; each descale is float32 on CUDA with shape (batch, num_kv_heads); `out` is bfloat16; and no input or descale requires grad, since fp8 runs forward only. `_validate_descale` and `DescaleType` move from the experimental quantized SDPA module to `torch/nn/attention/_utils.py`, so the varlen module does not import from `experimental`. Test Plan: python -m pytest test/test_varlen_attention.py -k "fp8 or batch_invariance" python -m pytest test/test_varlen_attention.py Both on a GB300 with flash-attn-4 4.0.0b31 and 4.0.0b40. Written with Claude Code (an AI assistant).
FA4's bridge now registers `_scaled_dot_product_flash_attention.quantized`, the op behind the experimental `_scaled_dot_product_attention_quantized`. It reuses the fp8 path of the flash ops: fp8 query, key and value (float8_e4m3fn or float8_e5m2) with float32 descales of shape (batch, num_kv_heads), and a bfloat16 output in the query's layout. The op's meta now maps float8_e5m2 to a bfloat16 output too. The C++ stub's error and the experimental docstring name FA4 next to FA3. Test Plan: ```bash python -m pytest test/nn/attention/test_fa4.py -k fp8_sdpa ``` Run on a GB300 with flash-attn-4 4.0.0b31 and 4.0.0b40. Written with Claude Code (an AI assistant).
…ials The fp8 dtype tuple now lives once in `torch/nn/attention/_utils.py`, used by the varlen API, the FA4 bridge and the two `.quantized` metas. FA4's two flash attention impls take the descales right after `return_debug_mask`, where the `.quantized` schemas put them, so each `.quantized` overload is a `functools.partial(impl, quantized=True)` instead of a wrapper. The comment on the pre-4.0.0b39 descale form now states which flash-attn-4 releases take it. `test_fp8_descales` now compiles `varlen_attn_out` too, the fp8 tests skip with `IS_SM10X`, the `varlen_attn_out` docstring says fp8 inputs need a bfloat16 `out`, and the descale batch error says "number of sequences". Test Plan: ```bash python -m pytest test/test_varlen_attention.py -k "fp8 or batch_invariance" python -m pytest test/nn/attention/test_fa4.py -k fp8_sdpa python -m pytest test/test_varlen_attention.py test/nn/attention/test_fa4.py ``` Run on a GB300 with flash-attn-4 4.0.0b31 and 4.0.0b40. Written with Claude Code (an AI assistant).
The comment on FA4's older descale form no longer repeats CI's flash-attn-4 pin, which lives in another file. The `DescaleType` docstring says that `varlen_attn` always uses PER_HEAD, since it takes no descale-type argument. Test Plan: comment and docstring only. Lint: ```bash lintrunner torch/nn/attention/_fa4.py torch/nn/attention/_utils.py ``` Written with Claude Code (an AI assistant).
Attention! native_functions.yaml was changedIf you are adding a new function or defaulted argument to native_functions.yaml, you cannot use it from pre-existing Python frontend code until our FC window passes (two weeks). Split your PR into two PRs, one which adds the new C++ functionality, and one that makes use of it from Python, and land them two weeks apart. See https://github.com/pytorch/pytorch/wiki/PyTorch's-Python-Frontend-Backward-and-Forward-Compatibility-Policy#forwards-compatibility-fc for more info. Attention! PyTorch one of the C-stable API file was changedYou MUST NOT change existing function declarations in this, as this header defines a stable C ABI. If you need to change the signature for a function, introduce a new v2 version of the function and modify code generation to target the new version of the function. |
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/206590
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 02b2744 with merge base 8d5a301 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Linked issue or supporting maintainer
Supported by @drisspg
BC-breaking?
No.
Summary (human written only)
Agent details (if applicable)
Written with Claude Code: the analysis, code, measurements and test plan below.