[Perf][DSV4.1] Overlap mHC coefficients for small TP batches (#57603)

Signed-off-by: Woosuk Kwon <woosuk@inferact.ai>
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Woosuk Kwon
2026-09-20 17:33:17 -07:00
committed by GitHub
co-authored by Codex
parent 17e50b9b76
commit 9b49f92344
6 changed files with 357 additions and 46 deletions
+60 -16
View File
@@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import typing
from collections.abc import Callable, Iterable
from functools import partial
from itertools import islice
import regex as re
@@ -17,7 +18,11 @@ from vllm.distributed import (
get_tensor_model_parallel_rank,
get_tensor_model_parallel_world_size,
)
from vllm.forward_context import get_forward_context, is_forward_context_available
from vllm.forward_context import (
get_forward_context,
in_piecewise_cudagraph,
is_forward_context_available,
)
from vllm.logger import init_logger
from vllm.model_executor.kernels.mhc.tilelang import (
mhc_post_tilelang,
@@ -84,6 +89,11 @@ from ..common.engram import EngramLayout, NgramHashState
from ..common.mm_preprocess import IMAGE_SENTINEL_BASE_ID, image_sentinel_mask
from .engram import Engram, gather_engram_hashes
from .ops.mega_mhc import mhc_shifted_post_pre
from .ops.mhc import (
MHC_OVERLAP_MAX_TOKENS,
mhc_pre_delayed_overlap,
supports_mhc_overlap,
)
if typing.TYPE_CHECKING:
from vllm.v1.attention.backends.mla.sparse_swa import DeepseekSparseSWAMetadata
@@ -188,11 +198,13 @@ 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,
mhc_stream: torch.cuda.Stream | None = None,
):
super().__init__()
config = vllm_config.model_config.hf_config
self.hidden_size = config.hidden_size
self.mhc_stream = mhc_stream
self.use_sequence_parallel = _use_sequence_parallel(vllm_config)
self.engram: Engram | None = None
@@ -269,19 +281,30 @@ class DeepseekV4DecoderLayer(nn.Module):
variants.append(
{**hc_stream, "use_pre_mix_in": True, "write_aux": True}
)
# Shifted mHC splits the epilogue in two: the input collapse on the
# caller stream and coefficient generation on a side stream.
split_modes = (
("fused", "stats", "input") if mhc_stream is not None else ("fused",)
)
for variant in variants:
MHC_PRE_NORM_KERNEL.register_warmup(
max_tokens=max_tokens,
hidden_size=self.hidden_size,
rms_eps=self.rms_norm_eps,
hc_pre_eps=self.hc_eps,
hc_sinkhorn_eps=self.hc_eps,
hc_post_mult_value=self.hc_post_alpha,
sinkhorn_repeat=self.hc_sinkhorn_iters,
norm_eps=self.rms_norm_eps,
hc_mult=self.hc_mult,
**variant,
)
for split_mode in split_modes:
MHC_PRE_NORM_KERNEL.register_warmup(
split_mode=split_mode,
max_tokens=(
max_tokens
if split_mode == "fused"
else min(max_tokens, MHC_OVERLAP_MAX_TOKENS)
),
hidden_size=self.hidden_size,
rms_eps=self.rms_norm_eps,
hc_pre_eps=self.hc_eps,
hc_sinkhorn_eps=self.hc_eps,
hc_post_mult_value=self.hc_post_alpha,
sinkhorn_repeat=self.hc_sinkhorn_iters,
norm_eps=self.rms_norm_eps,
hc_mult=self.hc_mult,
**variant,
)
mix_hc = (2 + self.hc_mult) * self.hc_mult
hc_dim = self.hc_mult * self.hidden_size
self.hc_attn_fn = nn.Parameter(
@@ -351,6 +374,18 @@ class DeepseekV4DecoderLayer(nn.Module):
torch.Tensor | None,
]:
previous_aux: torch.Tensor | None = None
mhc_stream = self.mhc_stream
if mhc_stream is not None and (
in_piecewise_cudagraph()
or not 0 < positions.shape[0] <= MHC_OVERLAP_MAX_TOKENS
):
# A side stream cannot remain unjoined across breakable graph segments.
mhc_stream = None
mhc_pre = (
partial(mhc_pre_delayed_overlap, stream=mhc_stream)
if mhc_stream is not None
else mhc_pre_delayed_tilelang
)
# The reference collapses each sublayer's input with the *previous*
# sublayer's pre-mix: attention uses the pre-mix carried in (identity
# for the first layer), the FFN uses this layer's attention pre-mix.
@@ -360,7 +395,7 @@ class DeepseekV4DecoderLayer(nn.Module):
# copies and the identity pre-mix selects copy 0.
assert self.hc_attn_fn_broadcast is not None
residual = x.unsqueeze(1).expand(-1, self.hc_mult, -1).contiguous()
post_mix, res_mix, x, attn_pre = mhc_pre_delayed_tilelang(
post_mix, res_mix, x, attn_pre = mhc_pre(
residual,
self.hc_attn_fn_broadcast,
self.hc_attn_scale,
@@ -376,7 +411,7 @@ class DeepseekV4DecoderLayer(nn.Module):
)
else:
residual = x
post_mix, res_mix, x, attn_pre = mhc_pre_delayed_tilelang(
post_mix, res_mix, x, attn_pre = mhc_pre(
residual,
self.hc_attn_fn,
self.hc_attn_scale,
@@ -403,7 +438,7 @@ class DeepseekV4DecoderLayer(nn.Module):
engram_hashes[:, self.engram.layer_hash_index],
engram_mask,
)
post_mix, res_mix, x, attn_pre = mhc_pre_delayed_tilelang(
post_mix, res_mix, x, attn_pre = mhc_pre(
residual,
self.hc_attn_fn,
self.hc_attn_scale,
@@ -437,6 +472,7 @@ class DeepseekV4DecoderLayer(nn.Module):
norm_weight=self.attn_norm.weight,
norm_eps=self.attn_norm.variance_epsilon,
capture_aux=capture_previous_aux,
stream=mhc_stream,
)
if capture_previous_aux:
previous_aux = aux
@@ -448,6 +484,8 @@ class DeepseekV4DecoderLayer(nn.Module):
if self.use_sequence_parallel:
x = sp_reduce_scatter(x)
if mhc_stream is not None:
torch.cuda.current_stream().wait_stream(mhc_stream)
residual, post_mix, res_mix, x, ffn_pre, _ = mhc_shifted_post_pre(
x,
residual,
@@ -464,8 +502,11 @@ class DeepseekV4DecoderLayer(nn.Module):
pre_mix=attn_pre,
norm_weight=self.ffn_norm.weight,
norm_eps=self.ffn_norm.variance_epsilon,
stream=mhc_stream,
)
x = self.ffn(x, input_ids, mega_gate_metadata)
if mhc_stream is not None:
torch.cuda.current_stream().wait_stream(mhc_stream)
return x, residual, post_mix, res_mix, ffn_pre, previous_aux
@@ -497,6 +538,8 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
# (compressor kv_score, indexer.weights_proj). fused_wqa_wkv stays on
# the default stream.
aux_stream_list = [torch.cuda.Stream() for _ in range(3)]
# Keep mHC independent of the streams used inside attention.
mhc_stream = torch.cuda.Stream() if supports_mhc_overlap(vllm_config) else None
# Reserved topk indices buffer for all Indexer layers to reuse.
self.topk_indices_buffer = torch.empty(
@@ -541,6 +584,7 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
aux_stream_list=aux_stream_list,
candidate_block_buffer=self.candidate_block_buffer,
engram_layout=self.engram_layout,
mhc_stream=mhc_stream,
),
prefix=f"{prefix}.layers",
)