Repository navigation
[CUDA] Support FP16 KV cache and FP16 query/key in DynamicSparseAttention<BFloat16> and FP32 rotary cache / FP16 output in RMSNormRotary - #33997
Conversation
…tion<BFloat16> and FP32 rotary cache / FP16 output in RMSNormRotary - RMSNormRotary (bert_defs.cc, rms_norm_rotary.h/.cc/.cu): - Extend TCos type constraint to allow tensor(float) in addition to tensor(bfloat16) so 256K RoPE cos/sin caches can be kept in FP32. - Add output_fp16 attribute and TOut type constraint (tensor(bfloat16), tensor(float16)) and instantiate RMSNormRotaryExactKernel<kFp32Cos, kOutputFp16>, computing RMSNorm + RoPE in exact FP32 without intermediate BFloat16 rounding and outputting full 10-bit mantissa FP16 Q/K tensors. - DynamicSparseAttention (bert_defs.cc, dynamic_sparse_attention.cc, dynamic_sparse_attention_impl.h/.cu, dynamic_sparse_attention_prefill.cuh): - Extend TCache to support tensor(float16) and TQuery to support tensor(float16) in DynamicSparseAttention<BFloat16>. - Instantiate Tensor Core MmaFp16 prefill kernel (DynamicSparseAttentionPrefillKernel<false, true>) and grouped decode kernel (GroupedDecodeDynamicSparseAttentionKernel<..., kFp16Cache=true>) for BFloat16 activations with FP16 KV cache. - Skip PrepareQueryKernel when query is already in the target attention dtype (need_prepare_query == false). - Add unit tests in rms_norm_rotary_test.cc and dynamic_sparse_attention_op_test.cc. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Grouped attention can overflow BF16 auxiliary values when converting to FP16, while the schema change permits unsupported mixed types that generate invalid WebGPU shaders.
4 open issues
What changed in this PR
This PR extends ONNX Runtime’s CUDA contrib operators for long-context models, adding mixed-precision sparse attention and higher-precision RMS normalization with rotary embeddings.
Changes:
- Add FP16 query/key and KV-cache support to DynamicSparseAttention, including prefill/decode kernels and a query-preparation bypass.
- Add FP32 rotary caches and optional FP16 output to RMSNormRotary using FP32 intermediate arithmetic.
- Update operator schemas, output type inference, and CUDA parity tests.
| File | Description |
|---|---|
| onnxruntime/test/contrib_ops/rms_norm_rotary_test.cc | Adds FP32-cache and FP16-output parity coverage. |
| onnxruntime/test/contrib_ops/dynamic_sparse_attention_op_test.cc | Adds mixed query/cache dtype coverage. |
| onnxruntime/core/graph/contrib_ops/bert_defs.cc | Extends schemas and updates output type inference. |
| onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.h | Extends launch parameters and output configuration. |
| onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.cu | Implements the FP32-intermediate normalization/rotary kernel. |
| onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.cc | Registers new types and dispatches cache/output modes. |
| onnxruntime/contrib_ops/cuda/bert/dynamic_sparse_attention.cc | Registers and validates mixed-precision inputs. |
| onnxruntime/contrib_ops/cuda/bert/dynamic_sparse_attention_prefill.cuh | Adds FP16 Tensor Core prefill support. |
| onnxruntime/contrib_ops/cuda/bert/dynamic_sparse_attention_impl.h | Adds mixed-precision execution flags. |
| onnxruntime/contrib_ops/cuda/bert/dynamic_sparse_attention_impl.cu | Updates preparation, cache writes, attention kernels, and dispatch. |
Review effort: Balanced
…nd FP16 X input in RMSNormRotary Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Preserve floating auxiliary K/V with FP16 main caches and keep fallback workspace sizing consistent with dispatch. Separate query/value schema types, reject unsupported WebGPU mixed inputs, and preserve the BF16 RMSNormRotary output default. Add mixed-precision regressions and regenerate operator documentation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Feedback fixes pushed in d09014c095. Targeted validation
94 targeted tests passed. The new auxiliary-range, FP16-cache norm/RoPE, WebGPU mixed-type rejection, and FP16-input/default-BF16-output regressions pass. The existing FP32 rotary-cache/FP16-output precision grid also passes. Nsight confirms mixed auxiliary inputs use generic fused/split kernels while main-cache-only cases still exercise grouped kernels. The remaining Contrib schema docs match fresh generation under CI comparison rules. The two CUDA rows and WebGPU DSA row exactly match documentation generated from real rebuilt registries; unrelated provider sections are unchanged. Targeted lintrunner and whitespace checks pass. |
…Attention<BFloat16> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…V cache kernels Avoid truncating FP16 query to BFloat16 in PrepareQueryKernel when quantized_cache is enabled, and read FP16 query directly in DynamicSparseAttentionInt8TcKernel, DynamicSparseAttentionPrefillKernel, GroupedDecodeDynamicSparseAttentionKernel, FusedDynamicSparseAttentionKernel, and SplitDynamicSparseAttentionKernel. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>



Description
Add native
FP16(MLFloat16) KV cache andFP16query/key support toDynamicSparseAttention<BFloat16>andFP32rotary cache /FP16output support toRMSNormRotaryfor long-context Qwen3.8-Flash-Next models:com.microsoft::RMSNormRotary(bert_defs.cc,rms_norm_rotary.h/.cc/.cu):TCostype constraint to allowtensor(float)in addition totensor(bfloat16)so 256K RoPEcos_cache/sin_cachecan be stored in exactFP32.output_fp16attribute (0defaultBFloat16,1MLFloat16) and extendTOuttype constraint totensor(bfloat16)andtensor(float16).RMSNormRotaryExactKernel<kFp32Cos, kOutputFp16>which computes RMSNorm + RoPE in exactFP32without intermediateBFloat16rounding before converting to the output type, reducing Q/K relative RMSE by 9.4x (0.1954% -> 0.0207%) and@32Kattention logit RMSE by 9.5x (0.2789% -> 0.0293%).com.microsoft::DynamicSparseAttention(bert_defs.cc,dynamic_sparse_attention.cc,dynamic_sparse_attention_impl.h/.cu,dynamic_sparse_attention_prefill.cuh):TCacheto supporttensor(float16)andTQueryto supporttensor(float16)inDynamicSparseAttention<BFloat16>, while fixing output 0 (output) type inference to propagate from input 2 (value) when present.MmaFp16prefill kernel (DynamicSparseAttentionPrefillKernel<false, true>,mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32) and grouped decode kernel (GroupedDecodeDynamicSparseAttentionKernel<..., kFp16Cache=true>) forBFloat16activations withFP16KV cache.PrepareQueryKernelwhenqueryalready matches the target attention dtype (need_prepare_query == false), making theFP16KV cache path slightly faster thanBF16(143 usvs152 usper layer atS=8, L=32K..192K).Unit Tests:
RMSNormRotaryFp32CosAndFp16OutputParityinonnxruntime/test/contrib_ops/rms_norm_rotary_test.cc.SelectedOnlyFp16CacheBFloat16_CUDAandMLFloat16cache/query coverage inSelectedOnlyPrefillBFloat16_CUDA,FloatingCacheGroupedReferenceParity_CUDA, andGroupedDecodeRatio12ReferenceParity_CUDAinonnxruntime/test/contrib_ops/dynamic_sparse_attention_op_test.cc.fp32_fp16kv_v2(--qsa-qkv-dtype v_rc5_fp16+MLFloat16valueinDynamicSparseAttention<BFloat16>)In
v1(*_fp32_fp16kv),V(Input(2, "value")) enteringDynamicSparseAttention<BFloat16>originated frommerged_proj(SplitQsa) inBFloat16(7-bit mantissa), leaving the lower 3 mantissa bits ofpresent_value(FP16) zero-padded (0.1658%truncation error). ExtendingDynamicSparseAttention<BFloat16>to acceptMLFloat16value(output_bf16=1), extendingRMSNormRotaryto acceptMLFloat16orBFloat16inputX, and computing QSAv_projfrom the bit-exactCutlassFpAIntBdequantized weightw_bf16_deqinFLOAT16(--qsa-qkv-dtype v_rc5_fp16,+30.0 MiBacross 12 layers,0.000000%weight shift vs.rc5calibration,5.7xlowerVrounding error0.1658% -> 0.0291%) with nativeINT8PLE (--precision-profile fp32_fp16kv_v2) yields:Strict)N=175)N=175)N=175)rc5_top16_fp32_fp16kv_v269.2084.00%(1008/1200)73.14%(128/175,+5.71ppvs Base)87.43%(153/175)88.00%(154/175l,+1.14ppvs_fp32,+2.29ppvsv1)280.3/244.2rc5_all_int4_fp32_fp16kv_v280.9286.00%(1032/1200) (strict 85.33% = 1024/1200)69.71%(122/175)81.71%(143/175)84.57%(148/175,+0.57ppvsv1)272.6/248.4rc5_2b375_fp32_fp16kv_v255.1283.50%(1002/1200,+0.58ppvs Base,+1.08ppvs_fp32)68.57%(120/175,+4.00ppvs Base,+4.57ppvs_fp32)82.29%(144/175,+2.29ppvs Base,+6.29ppvs_fp32)84.57%(148/175,+1.71ppvs Base,+5.71ppvs_fp32)311.7/255.6