[ROCm][MLA] Fuse MLA q/kv RMSNorm + FP8 per-token quant in the FP8 attention path (#44977)
Signed-off-by: Xavier Aguilar <xavier.aguilarfruto@amd.com> Signed-off-by: Xavier Aguilar <Xavier.AguilarFruto@amd.com>
This commit is contained in:
@@ -1355,6 +1355,63 @@ def _fused_mla_dual_rms_norm_fake(
|
||||
return (torch.empty_like(x1), torch.empty_like(x2))
|
||||
|
||||
|
||||
def _fused_mla_dual_rms_norm_per_token_quant_impl(
|
||||
q: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
kv: torch.Tensor,
|
||||
kv_weight: torch.Tensor,
|
||||
q_epsilon: float,
|
||||
kv_epsilon: float,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
"""Fused MLA q/kv RMSNorm (+ FP8 per-token quant on q) via AITER.
|
||||
|
||||
Backs the ``fused_mla_dual_rms_norm_per_token_quant`` custom op used by the
|
||||
MLA FP8 attention fusion when the q latent is quantized *per token* (a single
|
||||
``(M, 1)`` scale). Only the *q* latent is FP8 quantized (it feeds the
|
||||
FP8 ``q_b_proj`` GEMM); the *kv* latent is RMS-normed and consumed by attention as bf16.
|
||||
"""
|
||||
from aiter.ops.fused_qk_rmsnorm_group_quant import (
|
||||
fused_qk_rmsnorm_per_token_quant,
|
||||
)
|
||||
|
||||
mq, nq = q.shape
|
||||
q_out = torch.empty((mq, nq), dtype=FP8_DTYPE, device=q.device)
|
||||
q_scale = torch.empty((mq, 1), dtype=torch.float32, device=q.device)
|
||||
kv_normed = torch.empty(kv.shape, dtype=kv.dtype, device=kv.device)
|
||||
|
||||
# q -> RMSNorm + FP8 per-token quant (q slot); kv -> RMSNorm only (k slot).
|
||||
# `split` views are accepted directly (unit inner stride); the kernel
|
||||
# handles strided inputs, matching the aiter op-test usage.
|
||||
fused_qk_rmsnorm_per_token_quant(
|
||||
q_out_quantized=q_out,
|
||||
q_out_scale=q_scale,
|
||||
q=q,
|
||||
q_weight=q_weight,
|
||||
q_epsilon=q_epsilon,
|
||||
k_out=kv_normed,
|
||||
k=kv,
|
||||
k_weight=kv_weight,
|
||||
k_epsilon=kv_epsilon,
|
||||
gemma_norm=False,
|
||||
)
|
||||
return q_out, q_scale, kv_normed
|
||||
|
||||
|
||||
def _fused_mla_dual_rms_norm_per_token_quant_fake(
|
||||
q: torch.Tensor,
|
||||
q_weight: torch.Tensor,
|
||||
kv: torch.Tensor,
|
||||
kv_weight: torch.Tensor,
|
||||
q_epsilon: float,
|
||||
kv_epsilon: float,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
mq, nq = q.shape
|
||||
q_out = torch.empty((mq, nq), dtype=FP8_DTYPE, device=q.device)
|
||||
q_scale = torch.empty((mq, 1), dtype=torch.float32, device=q.device)
|
||||
kv_normed = torch.empty(kv.shape, dtype=kv.dtype, device=kv.device)
|
||||
return q_out, q_scale, kv_normed
|
||||
|
||||
|
||||
def _rocm_aiter_gemm_a8wfp4_impl(
|
||||
x: torch.Tensor,
|
||||
w: torch.Tensor,
|
||||
@@ -2046,6 +2103,13 @@ class rocm_aiter_ops:
|
||||
fake_impl=_fused_mla_dual_rms_norm_fake,
|
||||
)
|
||||
|
||||
direct_register_custom_op(
|
||||
op_name="fused_mla_dual_rms_norm_per_token_quant",
|
||||
op_func=_fused_mla_dual_rms_norm_per_token_quant_impl,
|
||||
mutates_args=[],
|
||||
fake_impl=_fused_mla_dual_rms_norm_per_token_quant_fake,
|
||||
)
|
||||
|
||||
_OPS_REGISTERED = True
|
||||
|
||||
@staticmethod
|
||||
@@ -2120,6 +2184,10 @@ class rocm_aiter_ops:
|
||||
def get_fused_mla_dual_rms_norm_op() -> OpOverload:
|
||||
return torch.ops.vllm.fused_mla_dual_rms_norm.default
|
||||
|
||||
@staticmethod
|
||||
def get_fused_mla_dual_rms_norm_per_token_quant_op() -> OpOverload:
|
||||
return torch.ops.vllm.fused_mla_dual_rms_norm_per_token_quant.default
|
||||
|
||||
@staticmethod
|
||||
def w8a8_gemm(
|
||||
A: torch.Tensor,
|
||||
|
||||
Reference in New Issue
Block a user