[Model][Hardware][AMD][Kernel] Part 2/2 -> Enable e2e QK Norm + RoPE + KV Cache runtime fusion for Qwen3-30B-A3B on ROCM_ATTN (#42755)
Signed-off-by: Jack Hu <Jack.Hu@amd.com> Signed-off-by: Jack Hu <jingxujaster@hotmail.com> Signed-off-by: Jack Hu <jack.hu@amd.com> Co-authored-by: Douglas Lehr <91553416+dllehr-amd@users.noreply.github.com>
This commit is contained in:
+24
-9
@@ -11,6 +11,7 @@ import torch.distributed as dist
|
||||
from torch._ops import OpOverload
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm import _custom_ops as ops
|
||||
from vllm.logger import init_logger
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.utils.torch_utils import direct_register_custom_op
|
||||
@@ -3887,17 +3888,29 @@ class rocm_aiter_ops:
|
||||
v_scale: torch.Tensor,
|
||||
kv_cache_dtype: str,
|
||||
use_shuffle_layout: bool,
|
||||
return_kv: bool,
|
||||
) -> None:
|
||||
"""Run the fused QK-norm+RoPE+KV-cache op on already-split k/v caches.
|
||||
|
||||
Shared by the AITER FA and unified-attention impls. The caller splits
|
||||
Shared by the AITER FA, ROCM ATTN, and unified-attention impls. The caller splits
|
||||
kv_cache, since the unbind dim depends on the layout (e.g. the unified
|
||||
encoder-decoder path is K/V-first), and passes use_shuffle_layout
|
||||
(unified reads NHD and must pass False).
|
||||
(unified reads NHD and must pass False). k_scale/v_scale are the
|
||||
per-tensor CPU scalars. return_kv says whether the impl consumes
|
||||
k_out (FA and ROCM_ATTN prefill do; unified reads K from the cache and
|
||||
passes False, so the kernel skips that store). For fp8 caches the
|
||||
kernel emits k_out in the cache dtype, so a returned k_out is
|
||||
converted to the activation dtype.
|
||||
"""
|
||||
if kv_cache_dtype.startswith("fp8"):
|
||||
key_cache = key_cache.view(current_platform.fp8_dtype())
|
||||
value_cache = value_cache.view(current_platform.fp8_dtype())
|
||||
is_fp8_cache = kv_cache_dtype.startswith("fp8")
|
||||
dequant_k_out = return_kv and is_fp8_cache
|
||||
kernel_k_out = k_out
|
||||
if is_fp8_cache:
|
||||
fp8_dtype = current_platform.fp8_dtype()
|
||||
key_cache = key_cache.view(fp8_dtype)
|
||||
value_cache = value_cache.view(fp8_dtype)
|
||||
if dequant_k_out:
|
||||
kernel_k_out = torch.empty_like(k_out, dtype=fp8_dtype)
|
||||
# Partial-rotary support (e.g. GLM-4.7 applies rotary to only a prefix
|
||||
# of each head's channel dim).
|
||||
rotary_dim = cos_sin_cache.shape[-1]
|
||||
@@ -3920,14 +3933,16 @@ class rocm_aiter_ops:
|
||||
slot_mapping=slot_mapping,
|
||||
k_scale=k_scale,
|
||||
v_scale=v_scale,
|
||||
k_out=k_out,
|
||||
k_out=kernel_k_out,
|
||||
v_out=None,
|
||||
return_kv=True,
|
||||
return_kv=return_kv,
|
||||
use_shuffle_layout=use_shuffle_layout,
|
||||
block_size=key_cache.shape[1],
|
||||
x=16 // key_cache.element_size(),
|
||||
rotary_dim=kernel_rotary_dim,
|
||||
)
|
||||
if dequant_k_out:
|
||||
ops.convert_fp8(k_out, kernel_k_out, float(k_scale), kv_cache_dtype)
|
||||
|
||||
@staticmethod
|
||||
def fused_qk_norm_mrope_and_cache(
|
||||
@@ -4506,8 +4521,8 @@ class rocm_aiter_ops:
|
||||
scale: float,
|
||||
K_QScale_hip: torch.Tensor,
|
||||
V_QScale_hip: torch.Tensor,
|
||||
K_QScale_asm: torch.Tensor,
|
||||
V_QScale_asm: torch.Tensor,
|
||||
K_QScale_asm: torch.Tensor | None,
|
||||
V_QScale_asm: torch.Tensor | None,
|
||||
out_: torch.Tensor,
|
||||
kv_cache_dtype: str,
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user