[ROCm] Fuse MLA dual RMSNorm + FP8 group quant for DeepSeek-R1 (#54857)
Signed-off-by: Ethan Ky <ethan.ky@amd.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
4516896626
commit
29f955cce0
@@ -1074,16 +1074,148 @@ class MLADualRMSPerTokenQuantPattern(
|
||||
return _replacement
|
||||
|
||||
|
||||
class MLADualRMSGroupQuantPattern(
|
||||
VllmPatternReplacement[
|
||||
...,
|
||||
tuple[
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
],
|
||||
]
|
||||
):
|
||||
"""Fuse the MLA FP8 *block-scale* attention path -- q-latent RMSNorm + FP8
|
||||
group quant plus kv-latent RMSNorm -- into AITER's
|
||||
``fused_qk_rmsnorm_group_quant``.
|
||||
|
||||
With a block-scale FP8 ``q_b_proj`` (DeepSeek fp8), the earlier
|
||||
``RocmAiterRMSNormQuantFusionPass`` folds the q side into
|
||||
``rocm_aiter_rmsnorm_fp8_group_quant`` (the Triton rms+group-quant kernel)
|
||||
and leaves the kv side a plain ``vllm_ir.rms_norm``. This pattern matches
|
||||
that asymmetric pair::
|
||||
|
||||
gemm -> split_with_sizes([q_dim, kv_dim])
|
||||
+-- q_c -> rocm_aiter_rmsnorm_fp8_group_quant -> (q_fp8, q_scale)
|
||||
+-- kv_lora -> split_with_sizes([kv_c_dim, k_pe_dim])
|
||||
+-- kv_c -> vllm_ir.rms_norm -> kv_normed (bf16)
|
||||
+-- k_pe
|
||||
"""
|
||||
|
||||
GROUP_QUANT_OP = rocm_aiter_ops.get_rmsnorm_group_fused_quant_op()
|
||||
FUSED_OP = rocm_aiter_ops.get_fused_mla_dual_rms_norm_group_quant_op()
|
||||
|
||||
def __init__(self, epsilon: float) -> None:
|
||||
self._epsilon = epsilon
|
||||
|
||||
def get_inputs(self) -> list[torch.Tensor]:
|
||||
q_dim, kv_c_dim, k_pe_dim = 256, 128, 64
|
||||
return [
|
||||
self.empty_bf16(5, q_dim + kv_c_dim + k_pe_dim),
|
||||
self.empty_bf16(q_dim),
|
||||
self.empty_bf16(kv_c_dim),
|
||||
]
|
||||
|
||||
@property
|
||||
def pattern(
|
||||
self,
|
||||
) -> Callable[
|
||||
...,
|
||||
tuple[
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
],
|
||||
]:
|
||||
eps = self._epsilon
|
||||
group_quant_op = self.GROUP_QUANT_OP
|
||||
|
||||
def _pattern(
|
||||
projected: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
kv_weight: torch.Tensor,
|
||||
) -> tuple[
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
]:
|
||||
q_dim = q_weight.shape[0]
|
||||
kv_dim = projected.shape[-1] - q_dim
|
||||
kv_c_dim = kv_weight.shape[0]
|
||||
k_pe_dim = kv_dim - kv_c_dim
|
||||
q_c, kv_lora = projected.split([q_dim, kv_dim], dim=-1)
|
||||
kv_c, k_pe = kv_lora.split([kv_c_dim, k_pe_dim], dim=-1)
|
||||
quant_kwargs = dict(
|
||||
x=q_c,
|
||||
weight=q_weight,
|
||||
variance_epsilon=eps,
|
||||
group_size=128,
|
||||
)
|
||||
q_quant = group_quant_op(**quant_kwargs)
|
||||
kv_normed = vllm.ir.ops.rms_norm(kv_c, kv_weight, eps)
|
||||
return q_quant[0], q_quant[1], kv_normed, k_pe
|
||||
|
||||
return _pattern
|
||||
|
||||
@property
|
||||
def replacement(
|
||||
self,
|
||||
) -> Callable[
|
||||
...,
|
||||
tuple[
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
],
|
||||
]:
|
||||
eps = self._epsilon
|
||||
fused_op = self.FUSED_OP
|
||||
|
||||
def _replacement(
|
||||
projected: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
kv_weight: torch.Tensor,
|
||||
) -> tuple[
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
torch.Tensor,
|
||||
]:
|
||||
q_dim = q_weight.shape[0]
|
||||
kv_dim = projected.shape[-1] - q_dim
|
||||
kv_c_dim = kv_weight.shape[0]
|
||||
k_pe_dim = kv_dim - kv_c_dim
|
||||
q_c, kv_lora = projected.split([q_dim, kv_dim], dim=-1)
|
||||
kv_c, k_pe = kv_lora.split([kv_c_dim, k_pe_dim], dim=-1)
|
||||
at = fused_op(
|
||||
q_c,
|
||||
q_weight,
|
||||
kv_c,
|
||||
kv_weight,
|
||||
eps,
|
||||
eps,
|
||||
128,
|
||||
False,
|
||||
)
|
||||
# q_fp8, q_scale, kv_normed, k_pe
|
||||
return at[0], at[1], at[2], k_pe
|
||||
|
||||
return _replacement
|
||||
|
||||
|
||||
class MLADualRMSNormFusionPass(VllmFusionPatternMatcherPass):
|
||||
"""Post-grad PatternMatcher pass that fuses paired q / kv RMS norms in
|
||||
MLA attention into ``fused_mla_dual_rms_norm`` backed by aiter's
|
||||
``fused_qk_rmsnorm`` HIP kernel.
|
||||
|
||||
The FP8 attention path is also handled via
|
||||
:class:`MLADualRMSPerTokenQuantPattern`, which fuses the q-latent RMSNorm +
|
||||
FP8 per-token quant together with the kv-latent RMSNorm into
|
||||
``fused_mla_dual_rms_norm_per_token_quant`` backed by aiter's
|
||||
``fused_qk_rmsnorm_per_token_quant`` HIP kernel.
|
||||
:class:`MLADualRMSPerTokenQuantPattern` or :class:`MLADualRMSGroupQuantPattern`,
|
||||
which fuse the q-latent RMSNorm + FP8 per-token/group quant together with
|
||||
the kv-latent RMSNorm into ``fused_mla_dual_rms_norm_<per_token|group>_quant``
|
||||
backed by aiter's ``fused_qk_rmsnorm_<per_token|group>_quant`` HIP kernel.
|
||||
"""
|
||||
|
||||
def __init__(self, config: VllmConfig) -> None:
|
||||
@@ -1092,3 +1224,4 @@ class MLADualRMSNormFusionPass(VllmFusionPatternMatcherPass):
|
||||
for epsilon in [1e-5, 1e-6]:
|
||||
self.register(MLADualRMSNormPattern(epsilon))
|
||||
self.register(MLADualRMSPerTokenQuantPattern(epsilon))
|
||||
self.register(MLADualRMSGroupQuantPattern(epsilon))
|
||||
|
||||
Reference in New Issue
Block a user