[Feature] support dpsk v32 flashinfer decode#15546
[Feature] support dpsk v32 flashinfer decode#15546DarkSharpness wants to merge 2 commits intosgl-project:mainfrom
Conversation
Summary of ChangesHello @DarkSharpness, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces and integrates Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request integrates flashinfer for sparse MLA decode on Blackwell GPUs, which is a great performance enhancement. The implementation looks solid, but I have a few suggestions to improve code quality. Specifically, I've pointed out a debug print statement that should be removed or replaced with logging and some hardcoded model-specific parameters that should be dynamically retrieved from the model configuration for better maintainability.
| self.workspace_buffer = global_workspace_buffer | ||
| else: | ||
| self.workspace_buffer = None | ||
| print(f"{self.nsa_prefill_impl = } {self.nsa_decode_impl = }") |
| return self._forward_flashinfer( | ||
| q_all=q_all, | ||
| kv_cache=kv_cache, | ||
| page_table_1=page_table_1, | ||
| sm_scale=layer.scaling, | ||
| metadata=metadata, | ||
| ) |
There was a problem hiding this comment.
To avoid hardcoding model-specific parameters in _forward_flashinfer, please pass the layer object to it. This will allow accessing the model configuration dynamically in the next step.
| return self._forward_flashinfer( | |
| q_all=q_all, | |
| kv_cache=kv_cache, | |
| page_table_1=page_table_1, | |
| sm_scale=layer.scaling, | |
| metadata=metadata, | |
| ) | |
| return self._forward_flashinfer( | |
| q_all=q_all, | |
| kv_cache=kv_cache, | |
| page_table_1=page_table_1, | |
| sm_scale=layer.scaling, | |
| metadata=metadata, | |
| layer=layer, | |
| ) |
| def _forward_flashinfer( | ||
| self, | ||
| q_all: torch.Tensor, | ||
| kv_cache: torch.Tensor, | ||
| page_table_1: torch.Tensor, | ||
| sm_scale: float, | ||
| metadata: NSAMetadata, | ||
| ) -> torch.Tensor: | ||
| import flashinfer | ||
|
|
||
| assert self.workspace_buffer is not None | ||
| return flashinfer.decode.trtllm_batch_decode_with_kv_cache_mla( | ||
| query=q_all.unsqueeze(1), # TODO(dark): support MTP | ||
| kv_cache=kv_cache.view(-1, 1, self.real_page_size, self.kv_cache_dim), | ||
| workspace_buffer=self.workspace_buffer, | ||
| qk_nope_head_dim=128, | ||
| kv_lora_rank=512, | ||
| qk_rope_head_dim=64, | ||
| block_tables=page_table_1.unsqueeze(1), # NOTE: 1 is MTP length | ||
| seq_lens=metadata.nsa_seqlens_expanded, | ||
| max_seq_len=metadata.nsa_max_seqlen_q, | ||
| sparse_mla_top_k=self.nsa_index_topk, | ||
| bmm1_scale=sm_scale, | ||
| enable_pdl=True, | ||
| ) |
There was a problem hiding this comment.
Instead of hardcoding values for qk_nope_head_dim, kv_lora_rank, and qk_rope_head_dim, please retrieve them from the layer.model_config. This makes the implementation more generic and avoids magic numbers. This change depends on passing the layer object from forward_decode.
def _forward_flashinfer(
self,
q_all: torch.Tensor,
kv_cache: torch.Tensor,
page_table_1: torch.Tensor,
sm_scale: float,
metadata: NSAMetadata,
layer: "RadixAttention",
) -> torch.Tensor:
import flashinfer
assert self.workspace_buffer is not None
return flashinfer.decode.trtllm_batch_decode_with_kv_cache_mla(
query=q_all.unsqueeze(1), # TODO(dark): support MTP
kv_cache=kv_cache.view(-1, 1, self.real_page_size, self.kv_cache_dim),
workspace_buffer=self.workspace_buffer,
qk_nope_head_dim=layer.model_config.qk_nope_head_dim,
kv_lora_rank=layer.model_config.kv_lora_rank,
qk_rope_head_dim=layer.model_config.qk_rope_head_dim,
block_tables=page_table_1.unsqueeze(1), # NOTE: 1 is MTP length
seq_lens=metadata.nsa_seqlens_expanded,
max_seq_len=metadata.nsa_max_seqlen_q,
sparse_mla_top_k=self.nsa_index_topk,
bmm1_scale=sm_scale,
enable_pdl=True,
)| self.workspace_buffer = global_workspace_buffer | ||
| else: | ||
| self.workspace_buffer = None | ||
| print(f"{self.nsa_prefill_impl = } {self.nsa_decode_impl = }") |
There was a problem hiding this comment.
| print(f"{self.nsa_prefill_impl = } {self.nsa_decode_impl = }") |
| ) | ||
| elif self.nsa_decode_impl == "flashinfer": | ||
| if q_rope is not None: | ||
| q_all = torch.cat([q_nope, q_rope], dim=-1) |
There was a problem hiding this comment.
Can we apply _concat_mla_absorb_q_general(q_nope, q_rope), which might be faster
|
/tag-and-rerun-ci |
|
This Pr is covered by #16758 |
Motivation
In next flashinfer release, there will be support for sparse MLA decode on Blackwell.
Modifications
Support for fp8 is working in progress.
Accuracy Tests
Benchmarking and Profiling
8 * B200.
Before (
flashmla_sparsefor decode):After (
flashinferfor decode)Checklist