|
202 | 202 | from aphrodite._aiter_ops import rocm_aiter_ops |
203 | 203 | from aphrodite.compilation.breakable_cudagraph import eager_break_during_capture |
204 | 204 | from aphrodite.config import ( |
| 205 | + AphroditeConfig, |
205 | 206 | CacheConfig, |
206 | 207 | ModelConfig, |
207 | | - AphroditeConfig, |
208 | 208 | get_current_aphrodite_config, |
209 | 209 | get_current_aphrodite_config_or_none, |
210 | 210 | ) |
@@ -325,7 +325,7 @@ def _canonicalize_sparse_mla_kv_cache_dtype( |
325 | 325 | kv_cache_dtype: CacheDType, |
326 | 326 | ) -> CacheDType: |
327 | 327 | backend_name = attn_backend.get_name() |
328 | | - if backend_name == "FLASHMLA_SPARSE" and is_quantized_kv_cache(kv_cache_dtype): |
| 328 | + if backend_name in ("FLASHMLA_SPARSE", "SM89_MLA_SPARSE") and is_quantized_kv_cache(kv_cache_dtype): |
329 | 329 | return "fp8_ds_mla" |
330 | 330 | if backend_name == "FLASHINFER_MLA_SPARSE_SM120" and kv_cache_dtype in ( |
331 | 331 | "auto", |
@@ -696,6 +696,24 @@ def forward_impl( |
696 | 696 | # Sparse MLA impls only support forward_mqa (decode-style attention) |
697 | 697 | is_sparse_impl = isinstance(self.impl, SparseMLAAttentionImpl) |
698 | 698 |
|
| 699 | + # sm89 sparse local-prefill under DCP. When every prefill request is a |
| 700 | + # fresh prompt, its full KV is computable on-rank from the replicated |
| 701 | + # activations, so prefill tokens attend locally (no q all-gather, no |
| 702 | + # LSE combine) and only decode tokens take the DCP path. The metadata |
| 703 | + # builder sets prefill_local only for Sm89MLASparseImpl batches that |
| 704 | + # qualify; other impls/metadata lack the attribute. |
| 705 | + sparse_dcp_local_prefill = ( |
| 706 | + is_sparse_impl |
| 707 | + and self.impl.dcp_world_size > 1 |
| 708 | + and getattr(attn_metadata, "prefill_local", None) is not None |
| 709 | + ) |
| 710 | + if sparse_dcp_local_prefill: |
| 711 | + # Build the per-forward shadow pool of this batch's fresh prompt |
| 712 | + # KV before the attention kernels below consume it. |
| 713 | + self.impl.build_local_prefill_pool( # type: ignore[attr-defined] |
| 714 | + k_c_normed, k_pe, attn_metadata, self._k_scale |
| 715 | + ) |
| 716 | + |
699 | 717 | if is_sparse_impl: |
700 | 718 | num_mqa_tokens = q.size(0) |
701 | 719 | num_mha_tokens = 0 |
@@ -799,37 +817,85 @@ def forward_impl( |
799 | 817 | ) |
800 | 818 | else: |
801 | 819 | mqa_q = (mqa_ql_nope, mqa_q_pe) |
| 820 | + mqa_prefill_q: torch.Tensor | None = None |
802 | 821 | if self.impl.dcp_world_size > 1: |
803 | 822 | if isinstance(mqa_q, tuple): |
804 | 823 | # concatenate mqa_ql_nope and mqa_q_pe -> (B, N, L + P) |
805 | 824 | mqa_q = torch.cat(mqa_q, dim=-1) |
806 | | - # mqa_q do allgather in head dim. |
807 | | - mqa_q = get_dcp_group().all_gather(mqa_q, dim=1) |
| 825 | + if sparse_dcp_local_prefill: |
| 826 | + # Peel off the fresh-prompt prefill tokens (batch tail, |
| 827 | + # after the decode tokens) before the all-gather; they |
| 828 | + # attend locally with this rank's heads only. The gate is |
| 829 | + # batch-uniform across DCP ranks, so collective |
| 830 | + # participation stays aligned. |
| 831 | + num_dcp_decode_tokens = attn_metadata.num_decode_tokens |
| 832 | + mqa_prefill_q = mqa_q[num_dcp_decode_tokens:] |
| 833 | + mqa_q = mqa_q[:num_dcp_decode_tokens] |
| 834 | + if num_dcp_decode_tokens > 0: |
| 835 | + mqa_q = get_dcp_group().all_gather(mqa_q, dim=1) |
| 836 | + else: |
| 837 | + # all-gather q along the head dim |
| 838 | + mqa_q = get_dcp_group().all_gather(mqa_q, dim=1) |
808 | 839 |
|
809 | 840 | # call decode attn |
810 | 841 | if not is_sparse_impl: |
811 | 842 | assert attn_metadata.decode is not None |
812 | | - attn_out, lse = self.impl.forward_mqa(mqa_q, kv_cache, attn_metadata, self) # type: ignore[attr-defined] |
813 | 843 |
|
814 | | - # correct dcp attn_out with lse. |
815 | | - if self.impl.dcp_world_size > 1: |
816 | | - if self.dcp_a2a: |
817 | | - attn_out = dcp_a2a_lse_reduce( |
818 | | - attn_out, |
819 | | - lse, |
820 | | - get_dcp_group(), |
821 | | - is_lse_base_on_e=self.impl.lse_base_on_e, |
| 844 | + if sparse_dcp_local_prefill: |
| 845 | + assert mqa_prefill_q is not None |
| 846 | + num_dcp_decode_tokens = attn_metadata.num_decode_tokens |
| 847 | + if num_dcp_decode_tokens > 0: |
| 848 | + # Decode tokens keep the normal DCP path, all-gathered |
| 849 | + # heads on the local KV shard then cross-rank LSE combine. |
| 850 | + attn_out, lse = self.impl.forward_mqa( # type: ignore[attr-defined] |
| 851 | + mqa_q, kv_cache, attn_metadata, self |
822 | 852 | ) |
823 | | - else: |
824 | | - attn_out = cp_lse_ag_out_rs( |
825 | | - attn_out, |
826 | | - lse, |
827 | | - get_dcp_group(), |
828 | | - is_lse_base_on_e=self.impl.lse_base_on_e, |
| 853 | + if self.dcp_a2a: |
| 854 | + attn_out = dcp_a2a_lse_reduce( |
| 855 | + attn_out, |
| 856 | + lse, |
| 857 | + get_dcp_group(), |
| 858 | + is_lse_base_on_e=self.impl.lse_base_on_e, |
| 859 | + ) |
| 860 | + else: |
| 861 | + attn_out = cp_lse_ag_out_rs( |
| 862 | + attn_out, |
| 863 | + lse, |
| 864 | + get_dcp_group(), |
| 865 | + is_lse_base_on_e=self.impl.lse_base_on_e, |
| 866 | + ) |
| 867 | + self._v_up_proj( |
| 868 | + attn_out, out=mqa_output_slice[:num_dcp_decode_tokens] |
| 869 | + ) |
| 870 | + if mqa_prefill_q.shape[0] > 0: |
| 871 | + prefill_attn_out = self.impl.forward_mqa_local_prefill( # type: ignore[attr-defined] |
| 872 | + mqa_prefill_q, attn_metadata |
| 873 | + ) |
| 874 | + self._v_up_proj( |
| 875 | + prefill_attn_out, |
| 876 | + out=mqa_output_slice[num_dcp_decode_tokens:], |
829 | 877 | ) |
| 878 | + else: |
| 879 | + attn_out, lse = self.impl.forward_mqa(mqa_q, kv_cache, attn_metadata, self) # type: ignore[attr-defined] |
| 880 | + |
| 881 | + # correct dcp attn_out with lse. |
| 882 | + if self.impl.dcp_world_size > 1: |
| 883 | + if self.dcp_a2a: |
| 884 | + attn_out = dcp_a2a_lse_reduce( |
| 885 | + attn_out, |
| 886 | + lse, |
| 887 | + get_dcp_group(), |
| 888 | + is_lse_base_on_e=self.impl.lse_base_on_e, |
| 889 | + ) |
| 890 | + else: |
| 891 | + attn_out = cp_lse_ag_out_rs( |
| 892 | + attn_out, |
| 893 | + lse, |
| 894 | + get_dcp_group(), |
| 895 | + is_lse_base_on_e=self.impl.lse_base_on_e, |
| 896 | + ) |
830 | 897 |
|
831 | | - # v_up projection |
832 | | - self._v_up_proj(attn_out, out=mqa_output_slice) |
| 898 | + self._v_up_proj(attn_out, out=mqa_output_slice) |
833 | 899 |
|
834 | 900 | if quant_key is not None: |
835 | 901 | quant_idx = num_mqa_tokens if mha_use_quant_output else num_actual_toks |
|
0 commit comments