diff --git a/python/sglang/srt/layers/attention/dsa_backend.py b/python/sglang/srt/layers/attention/dsa_backend.py index fa8fb3ca57..a7ab6f1c5a 100644 --- a/python/sglang/srt/layers/attention/dsa_backend.py +++ b/python/sglang/srt/layers/attention/dsa_backend.py @@ -2582,6 +2582,28 @@ class DeepseekSparseAttnBackend( logit_cap: float, page_size: int, ) -> torch.Tensor: + # Sparse DSA represents each query token as a one-token FA3 sequence. + # MoE synchronization can pad length metadata, while the prefill graph + # attention bridge narrows Q and the page table to real query tokens. + # Remove only trailing metadata padding to match that narrowed layout. + if max_seqlen_q == 1 and q_nope.ndim == 3: + num_queries = q_nope.shape[0] + if cache_seqlens.shape[0] > num_queries: + if ( + page_table.shape[0] != num_queries + or cu_seqlens_q.shape[0] != cache_seqlens.shape[0] + 1 + or cu_seqlens_k.shape[0] != cache_seqlens.shape[0] + 1 + ): + raise ValueError( + "Inconsistent padded DSA FA3 metadata: " + f"q={tuple(q_nope.shape)}, page_table={tuple(page_table.shape)}, " + f"cache_seqlens={tuple(cache_seqlens.shape)}, " + f"cu_q={tuple(cu_seqlens_q.shape)}, " + f"cu_k={tuple(cu_seqlens_k.shape)}" + ) + cache_seqlens = cache_seqlens[:num_queries] + cu_seqlens_q = cu_seqlens_q[: num_queries + 1] + cu_seqlens_k = cu_seqlens_k[: num_queries + 1] k_rope_cache = kv_cache[:, :, v_head_dim:] c_kv_cache = kv_cache[:, :, :v_head_dim] qk_rope_dim = k_rope_cache.shape[-1]