[ROCm][Model][Bugfix] Enable GLM-5.2-MXFP4 on the deepseek_v32 path and fix sparse attention correctness (#51915)
Signed-off-by: Jack Hu <Jack.Hu@amd.com> Signed-off-by: Jack Hu <jack.hu@amd.com> Signed-off-by: Douglas Lehr <Doug.Lehr@amd.com> Co-authored-by: James E T Smith <jamesETsmith@users.noreply.github.com> Co-authored-by: Douglas Lehr <91553416+dllehr-amd@users.noreply.github.com> Co-authored-by: Douglas Lehr <Doug.Lehr@amd.com>
This commit is contained in:
co-authored by
James E T Smith
Douglas Lehr
Douglas Lehr
parent
9646f53064
commit
1c0eee919d
@@ -3,12 +3,14 @@
|
||||
|
||||
import torch
|
||||
|
||||
from vllm._aiter_ops import rocm_aiter_ops
|
||||
from vllm.compilation.breakable_cudagraph import eager_break_during_capture
|
||||
from vllm.forward_context import get_forward_context
|
||||
from vllm.model_executor.layers.sparse_attn_indexer import SparseAttnIndexer
|
||||
from vllm.model_executor.models.deepseek_v2 import DeepseekV32IndexerCache
|
||||
from vllm.models.deepseek_v32.attention import DeepseekV32Attention, DeepseekV32Indexer
|
||||
from vllm.models.deepseek_v32.common.kernels import fused_norm_rope, fused_q
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.utils.torch_utils import is_quantized_kv_cache
|
||||
from vllm.v1.attention.backends.mla.indexer import DeepseekV32IndexerBackend
|
||||
from vllm.v1.attention.backends.mla.rocm_aiter_mla_sparse import (
|
||||
@@ -16,21 +18,15 @@ from vllm.v1.attention.backends.mla.rocm_aiter_mla_sparse import (
|
||||
)
|
||||
|
||||
|
||||
class DeepseekV32MLASparseBackend(ROCMAiterMLASparseBackend):
|
||||
@staticmethod
|
||||
def get_supported_kernel_block_sizes() -> list:
|
||||
return [16, 32]
|
||||
|
||||
|
||||
class DeepseekV32ROCmIndexerBackend(DeepseekV32IndexerBackend):
|
||||
@staticmethod
|
||||
def get_supported_kernel_block_sizes() -> list:
|
||||
return [16, 32]
|
||||
|
||||
|
||||
class DeepseekV32ROCmIndexerCache(DeepseekV32IndexerCache):
|
||||
def get_attn_backend(self):
|
||||
return DeepseekV32ROCmIndexerBackend
|
||||
return DeepseekV32IndexerBackend
|
||||
|
||||
@property
|
||||
def uses_shuffled_layout(self) -> bool:
|
||||
# aiter's gather/insert pair shuffles the cache above block size 1:
|
||||
# [n_blocks, blk/16, head_dim/16, 16, 16] instead of [n_blocks, blk, head_dim].
|
||||
return self.kv_cache.ndim == 3 and self.kv_cache.shape[1] != 1
|
||||
|
||||
|
||||
class DeepseekV32ROCmIndexer(DeepseekV32Indexer):
|
||||
@@ -46,7 +42,7 @@ class DeepseekV32MLAAttention(DeepseekV32Attention):
|
||||
config,
|
||||
prefix,
|
||||
topk_indices_buffer,
|
||||
attn_backend=DeepseekV32MLASparseBackend,
|
||||
attn_backend=ROCMAiterMLASparseBackend,
|
||||
)
|
||||
|
||||
self.indexer_op: SparseAttnIndexer | None = None
|
||||
@@ -100,14 +96,10 @@ class DeepseekV32MLAAttention(DeepseekV32Attention):
|
||||
q_nope = q_nope.transpose(0, 1) # (N, tokens, P)
|
||||
|
||||
if self.is_aiter_triton_fp4_bmm_enabled:
|
||||
from aiter.ops.triton.batched_gemm_a16wfp4 import batched_gemm_a16wfp4
|
||||
|
||||
ql_nope = batched_gemm_a16wfp4(
|
||||
ql_nope = rocm_aiter_ops.batched_gemm_a16wfp4(
|
||||
q_nope, self.W_K, self.W_K_scale, transpose_bm=True, prequant=True
|
||||
)
|
||||
elif self.is_aiter_triton_fp8_bmm_enabled:
|
||||
from vllm._aiter_ops import rocm_aiter_ops
|
||||
|
||||
ql_nope = rocm_aiter_ops.triton_fp8_bmm(
|
||||
q_nope, self.W_K, self.W_K_scale, group_size=128, transpose_bm=True
|
||||
)
|
||||
@@ -152,14 +144,10 @@ class DeepseekV32MLAAttention(DeepseekV32Attention):
|
||||
)
|
||||
|
||||
if self.is_aiter_triton_fp4_bmm_enabled:
|
||||
from aiter.ops.triton.batched_gemm_a16wfp4 import batched_gemm_a16wfp4
|
||||
|
||||
batched_gemm_a16wfp4(
|
||||
rocm_aiter_ops.batched_gemm_a16wfp4(
|
||||
x, self.W_V, self.W_V_scale, out_view, transpose_bm=True, prequant=True
|
||||
)
|
||||
elif self.is_aiter_triton_fp8_bmm_enabled:
|
||||
from vllm._aiter_ops import rocm_aiter_ops
|
||||
|
||||
rocm_aiter_ops.triton_fp8_bmm(
|
||||
x,
|
||||
self.W_V,
|
||||
@@ -202,6 +190,7 @@ class DeepseekV32MLAAttention(DeepseekV32Attention):
|
||||
indexer_k_norm_eps = self.indexer.k_norm.eps
|
||||
indexer_k_rope_cos_sin_cache = self.indexer_rope_emb.cos_sin_cache
|
||||
indexer_k_cache = self.indexer.k_cache.kv_cache
|
||||
indexer_cache_shuffled = self.indexer.k_cache.uses_shuffled_layout
|
||||
indexer_softmax_scale = self.indexer.softmax_scale
|
||||
indexer_n_head_scale = self.indexer.n_head**-0.5
|
||||
else:
|
||||
@@ -211,6 +200,7 @@ class DeepseekV32MLAAttention(DeepseekV32Attention):
|
||||
indexer_k_norm_eps = 1e-6
|
||||
indexer_k_rope_cos_sin_cache = None
|
||||
indexer_k_cache = None
|
||||
indexer_cache_shuffled = False
|
||||
indexer_softmax_scale = 0.0
|
||||
indexer_n_head_scale = 0.0
|
||||
|
||||
@@ -241,6 +231,7 @@ class DeepseekV32MLAAttention(DeepseekV32Attention):
|
||||
self.topk_indices_buffer,
|
||||
slot_mapping=mla_slot,
|
||||
indexer_k_cache=indexer_k_cache,
|
||||
indexer_cache_shuffled=indexer_cache_shuffled,
|
||||
mla_kv_cache=mla_kv_cache,
|
||||
mla_kv_cache_dtype=self.kv_cache_dtype,
|
||||
mla_k_scale=mla_k_scale,
|
||||
@@ -282,7 +273,7 @@ class DeepseekV32MLAAttention(DeepseekV32Attention):
|
||||
num_actual = attn_metadata.num_actual_tokens # type: ignore[attr-defined]
|
||||
kv_cache = self.kv_cache
|
||||
if self._fp8_kv_needs_view:
|
||||
kv_cache = kv_cache.view(torch.float8_e4m3fn)
|
||||
kv_cache = kv_cache.view(current_platform.fp8_dtype())
|
||||
|
||||
q_for_attn = self._build_q_for_attn(ql_nope, mqa_q, num_actual)
|
||||
attn_out, _ = self.impl.forward_mqa( # type: ignore[attr-defined]
|
||||
|
||||
Reference in New Issue
Block a user