[Kernel][DSV4.1] Fuse MXFP8 wo_b GEMM with sequence-parallel reduce-scatter (#57428)
Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg> Signed-off-by: Canlin <canlinguosdu@gmail.com> Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg> Co-authored-by: OpenAI Codex <codex@openai.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Jee Jee Li <pandaleefree@gmail.com>
This commit is contained in:
co-authored by
Thien Tran
OpenAI Codex
Claude Fable 5.1
Jee Jee Li
parent
a6c47fbbf4
commit
f92b78f6ef
@@ -26,6 +26,7 @@ from vllm.model_executor.layers.linear import (
|
||||
from vllm.model_executor.layers.sparse_attn_indexer import SparseAttnIndexer
|
||||
from vllm.model_executor.layers.sparse_mqa_indexer import SparseMQAIndexer
|
||||
from vllm.models.common.ops import fused_q_kv_rmsnorm
|
||||
from vllm.models.common.ops.sequence_parallel import sp_reduce_scatter
|
||||
from vllm.models.deepseek_v41.common.ops import (
|
||||
MXFP4_BLOCK_SIZE,
|
||||
fused_indexer_q_rope_quant,
|
||||
@@ -33,6 +34,7 @@ from vllm.models.deepseek_v41.common.ops import (
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.model_executor.kernels.linear.cute_dsl.gemm_rs_ar import GemmRsAr
|
||||
from vllm.v1.attention.backends.mla.sparse_swa import (
|
||||
DeepseekSparseSWAMetadata,
|
||||
)
|
||||
@@ -389,6 +391,9 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
|
||||
return_bias=False,
|
||||
prefix=f"{prefix}.wo_b",
|
||||
)
|
||||
# Set by ``bind_gemm_rs`` when the decoder layer runs sequence
|
||||
# parallel and the fused GEMM + reduce-scatter kernel accepts wo_b.
|
||||
self.gemm_rs: GemmRsAr | None = None
|
||||
|
||||
# Initialize rotary embedding before the indexer/compressor consume it.
|
||||
self.rotary_emb = build_deepseek_v4_rope(
|
||||
@@ -672,6 +677,41 @@ class DeepseekV4Attention(nn.Module, AttentionLayerBase, ABC):
|
||||
)
|
||||
return self._o_proj(attn_out, positions)
|
||||
|
||||
def bind_gemm_rs(self) -> None:
|
||||
"""Fuse ``wo_b`` with the sequence-parallel TP reduce-scatter.
|
||||
|
||||
The decoder layer calls this after the model initialized the
|
||||
process-wide GEMM-RS workspace and before weights load: the
|
||||
eligibility check inspects the projection's linear kernel and weight
|
||||
shape, which online quantization may later re-layout.
|
||||
"""
|
||||
from vllm.model_executor.kernels.linear.cute_dsl.gemm_rs_ar import (
|
||||
get_gemm_rs_ar,
|
||||
)
|
||||
|
||||
# The layer owns the reduction under sequence parallel.
|
||||
assert not self.wo_b.reduce_results
|
||||
gemm_rs = get_gemm_rs_ar()
|
||||
if gemm_rs.can_run(self.wo_b):
|
||||
self.gemm_rs = gemm_rs
|
||||
else:
|
||||
gemm_rs.warn_incompatible_projection()
|
||||
|
||||
def _wo_b_proj(self, z: torch.Tensor) -> torch.Tensor:
|
||||
"""Apply ``wo_b``; with GEMM-RS bound, also reduce-scatter the result.
|
||||
|
||||
Every ``_o_proj`` implementation projects through this so the decoder
|
||||
layer sees one contract: when ``gemm_rs`` is bound the output is
|
||||
already the local sequence-parallel shard, otherwise it is the
|
||||
unreduced TP partial.
|
||||
"""
|
||||
if self.gemm_rs is None:
|
||||
return self.wo_b(z)
|
||||
if self.gemm_rs.should_run(z):
|
||||
return self.gemm_rs.apply(z, self.wo_b)
|
||||
# Small batches stay on the unfused path, which is faster there.
|
||||
return sp_reduce_scatter(self.wo_b(z))
|
||||
|
||||
def _alloc_attn_out(
|
||||
self, num_tokens: int, hidden_states: torch.Tensor
|
||||
) -> "torch.Tensor | QuantizedActivation":
|
||||
|
||||
@@ -58,6 +58,7 @@ from .model import (
|
||||
_linear_scale_param_name,
|
||||
_use_sequence_parallel,
|
||||
make_deepseek_v4_expert_params_mapping,
|
||||
maybe_init_gemm_rs,
|
||||
prepare_mega_gate_routing_metadata,
|
||||
)
|
||||
|
||||
@@ -113,12 +114,18 @@ class DSparkDeepseekV4Model(nn.Module):
|
||||
)
|
||||
|
||||
current_vllm_config = get_current_vllm_config()
|
||||
# The target model already holds the GEMM-RS workspace (same hidden
|
||||
# size, same TP group); this only re-checks and binds the draft layers.
|
||||
run_gemm_rs = maybe_init_gemm_rs(
|
||||
current_vllm_config, self.use_sequence_parallel
|
||||
)
|
||||
self.layers = nn.ModuleList(
|
||||
[
|
||||
DeepseekV4DecoderLayer(
|
||||
current_vllm_config,
|
||||
prefix=maybe_prefix(prefix, f"layers.{self.num_hidden_layers + i}"),
|
||||
topk_indices_buffer=self.topk_indices_buffer,
|
||||
run_gemm_rs=run_gemm_rs,
|
||||
)
|
||||
for i in range(self.num_dspark_layers)
|
||||
]
|
||||
|
||||
@@ -225,7 +225,7 @@ class DeepseekV4MegaAttnAttention(DeepseekV4FlashMLAAttention):
|
||||
z,
|
||||
recipe=self._einsum_recipe,
|
||||
)
|
||||
return self.wo_b(z.flatten(1))
|
||||
return self._wo_b_proj(z.flatten(1))
|
||||
|
||||
# ---- weights -----------------------------------------------------------
|
||||
|
||||
|
||||
@@ -248,7 +248,7 @@ class DeepseekV4FlashInferMLAAttention(DeepseekV4Attention):
|
||||
positions,
|
||||
self.rotary_emb.cos_sin_cache,
|
||||
self.wo_a,
|
||||
self.wo_b,
|
||||
self._wo_b_proj,
|
||||
n_groups=self.n_local_groups,
|
||||
heads_per_group=self.n_local_heads // self.n_local_groups,
|
||||
nope_dim=self.nope_head_dim,
|
||||
@@ -627,7 +627,7 @@ class DeepseekV4FlashInferSM120Attention(DeepseekV4Attention):
|
||||
positions,
|
||||
self.rotary_emb.cos_sin_cache,
|
||||
self.wo_a,
|
||||
self.wo_b,
|
||||
self._wo_b_proj,
|
||||
n_groups=self.n_local_groups,
|
||||
heads_per_group=self.n_local_heads // self.n_local_groups,
|
||||
nope_dim=self.nope_head_dim,
|
||||
|
||||
@@ -65,7 +65,7 @@ class DeepseekV4FlashMLAAttention(DeepseekV4Attention):
|
||||
positions,
|
||||
self.rotary_emb.cos_sin_cache,
|
||||
self.wo_a,
|
||||
self.wo_b,
|
||||
self._wo_b_proj,
|
||||
n_groups=self.n_local_groups,
|
||||
heads_per_group=self.n_local_heads // self.n_local_groups,
|
||||
nope_dim=self.nope_head_dim,
|
||||
|
||||
@@ -193,6 +193,30 @@ def _use_sequence_parallel(vllm_config: VllmConfig) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def maybe_init_gemm_rs(vllm_config: VllmConfig, use_sequence_parallel: bool) -> bool:
|
||||
"""Set up the fused ``wo_b`` GEMM + reduce-scatter when opted in.
|
||||
|
||||
Sequence parallel is the only topology where ``wo_b`` ends in a
|
||||
reduce-scatter, so the kernel is bound to RS mode. The workspace is a
|
||||
process-wide singleton shared with any other model code in this worker
|
||||
(the DSpark drafter reuses it), and its NVLink multicast rendezvous is
|
||||
collective, so every TP rank must take the same decision here.
|
||||
"""
|
||||
if not (use_sequence_parallel and envs.VLLM_ENABLE_GEMM_RS):
|
||||
return False
|
||||
|
||||
# The kernel module pulls in cute_dsl, so import it only once opted in.
|
||||
from vllm.model_executor.kernels.linear.cute_dsl.gemm_rs_ar import (
|
||||
maybe_init_gemm_rs_ar,
|
||||
)
|
||||
|
||||
hidden_size = vllm_config.model_config.hf_config.hidden_size
|
||||
if not maybe_init_gemm_rs_ar(vllm_config, N=hidden_size, all_reduce=False):
|
||||
return False
|
||||
logger.info_once("To disable DeepSeek-V4.1 GEMM-RS, set VLLM_ENABLE_GEMM_RS=0.")
|
||||
return True
|
||||
|
||||
|
||||
class DeepseekV4DecoderLayer(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -202,6 +226,7 @@ class DeepseekV4DecoderLayer(nn.Module):
|
||||
aux_stream_list: list[torch.cuda.Stream] | None = None,
|
||||
candidate_block_buffer: torch.Tensor | None = None,
|
||||
engram_layout: EngramLayout | None = None,
|
||||
run_gemm_rs: bool = False,
|
||||
mhc_stream: torch.cuda.Stream | None = None,
|
||||
fuse_mhc_all_reduce: bool = False,
|
||||
):
|
||||
@@ -236,6 +261,10 @@ class DeepseekV4DecoderLayer(nn.Module):
|
||||
)
|
||||
if self.use_sequence_parallel or fuse_mhc_all_reduce:
|
||||
self.attn.wo_b.reduce_results = False
|
||||
if run_gemm_rs:
|
||||
# Binds only when wo_b's kernel and shape qualify; otherwise
|
||||
# forward keeps the separate reduce-scatter below.
|
||||
self.attn.bind_gemm_rs()
|
||||
self.ffn = DeepseekV4MoE(
|
||||
vllm_config,
|
||||
prefix=f"{prefix}.ffn",
|
||||
@@ -492,7 +521,9 @@ class DeepseekV4DecoderLayer(nn.Module):
|
||||
x = sp_all_gather(x)[: positions.shape[0]]
|
||||
|
||||
x = self.attn(positions, x, None)
|
||||
if self.use_sequence_parallel:
|
||||
# With GEMM-RS bound, the attention output is already this rank's
|
||||
# sequence-parallel shard (see DeepseekV4Attention._wo_b_proj).
|
||||
if self.use_sequence_parallel and self.attn.gemm_rs is None:
|
||||
x = sp_reduce_scatter(x)
|
||||
|
||||
if mhc_stream is not None:
|
||||
@@ -597,6 +628,10 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
|
||||
):
|
||||
engram_config.dp_shared_memory = can_share_engram_tables(self.engram_layout)
|
||||
|
||||
# GEMM-RS uses NCCL symmetric-memory multicast, which requires all TP
|
||||
# ranks to belong to one NVLink domain. Collective: run before layers.
|
||||
self.run_gemm_rs = maybe_init_gemm_rs(vllm_config, self.use_sequence_parallel)
|
||||
|
||||
self.start_layer, self.end_layer, self.layers = make_layers(
|
||||
config.num_hidden_layers,
|
||||
lambda prefix: DeepseekV4DecoderLayer(
|
||||
@@ -606,6 +641,7 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
|
||||
aux_stream_list=aux_stream_list,
|
||||
candidate_block_buffer=self.candidate_block_buffer,
|
||||
engram_layout=self.engram_layout,
|
||||
run_gemm_rs=self.run_gemm_rs,
|
||||
mhc_stream=mhc_stream,
|
||||
fuse_mhc_all_reduce=self.fuse_mhc_all_reduce,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user