[ROCm][DSv4] Functional fixes for DeepSeek V4 on MI300X/MI325X (#45681)
Signed-off-by: ganyi <ygan@amd.com> Signed-off-by: Markus Hartikainen <markus.hartikainen@amd.com> Signed-off-by: Tuukka Sarvi <tuukka.sarvi@amd.com> Co-authored-by: ganyi <ygan@amd.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Markus Hartikainen <markus.hartikainen@amd.com> Co-authored-by: Jin Tao <jintao12@amd.com>
This commit is contained in:
co-authored by
ganyi
Cursor
Markus Hartikainen
Jin Tao
parent
8d4f54966c
commit
afdcbd5d39
@@ -504,7 +504,13 @@ def fp8_mqa_logits_torch(
|
||||
)
|
||||
mask = mask_lo & mask_hi
|
||||
|
||||
score = torch.einsum("mhd,nd->hmn", q, k).float() * scale
|
||||
# ``score`` is [H, M, N]; ``scale`` is the per-KV-token scale, which
|
||||
# vLLM callers hand us as ``[N, 1]`` (a ``[N, 4]`` uint8 buffer cast
|
||||
# to fp32). PyTorch right-aligns dimensions for broadcasting, so a
|
||||
# naked ``score * scale`` would align ``scale``'s leading dim with
|
||||
# ``score``'s M dim and raise a shape mismatch. Flatten to ``[N]`` so
|
||||
# broadcasting lines up with the last dim of ``score``.
|
||||
score = torch.einsum("mhd,nd->hmn", q, k).float() * scale.reshape(-1)
|
||||
logits = (score.relu() * weights.unsqueeze(-1).transpose(0, 1)).sum(dim=0)
|
||||
logits = logits.masked_fill(~mask, float("-inf"))
|
||||
|
||||
@@ -557,13 +563,26 @@ def rocm_fp8_mqa_logits(
|
||||
# path after aiter merge this kernel into main
|
||||
from vllm._aiter_ops import rocm_aiter_ops
|
||||
|
||||
k_fp8, scale = kv
|
||||
|
||||
# Temporarily route gfx942 to the vendored ROCm/aiter#3257 workaround.
|
||||
# Remove this branch once vLLM bumps AITER to a version that includes
|
||||
# ROCm/aiter#3257.
|
||||
if _ON_GFX942 and rocm_aiter_ops.is_enabled():
|
||||
from vllm.v1.attention.ops.triton_fp8_mqa_logits import (
|
||||
fp8_mqa_logits_gfx942,
|
||||
)
|
||||
|
||||
return fp8_mqa_logits_gfx942(
|
||||
q, k_fp8, scale, weights, cu_seqlen_ks, cu_seqlen_ke
|
||||
)
|
||||
|
||||
aiter_mqa_logits_module = None
|
||||
if rocm_aiter_ops.is_enabled():
|
||||
aiter_mqa_logits_module = mqa_logits_module()
|
||||
|
||||
if aiter_mqa_logits_module is not None:
|
||||
fp8_mqa_logits = aiter_mqa_logits_module.fp8_mqa_logits
|
||||
k_fp8, scale = kv
|
||||
return fp8_mqa_logits(q, k_fp8, scale, weights, cu_seqlen_ks, cu_seqlen_ke)
|
||||
else:
|
||||
return fp8_mqa_logits_torch(q, kv, weights, cu_seqlen_ks, cu_seqlen_ke)
|
||||
@@ -1249,7 +1268,10 @@ def _sparse_attn_decode_ragged_kernel(
|
||||
NOPE_DIM: tl.constexpr,
|
||||
NOPE_BLOCK: tl.constexpr,
|
||||
ROPE_DIM: tl.constexpr,
|
||||
IS_FNUZ: tl.constexpr,
|
||||
# SWA K-cache (main): C++ encoder writes FNUZ on gfx942, OCP on gfx950.
|
||||
# Compressed K-cache (extra): Triton encoder writes OCP everywhere.
|
||||
IS_FNUZ_MAIN: tl.constexpr,
|
||||
IS_FNUZ_EXTRA: tl.constexpr,
|
||||
BLOCK_H: tl.constexpr,
|
||||
BLOCK_K: tl.constexpr,
|
||||
):
|
||||
@@ -1306,8 +1328,8 @@ def _sparse_attn_decode_ragged_kernel(
|
||||
mask=valid[:, None] & nope_mask[None, :],
|
||||
other=0,
|
||||
)
|
||||
if IS_FNUZ:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b15, bitcast=True)
|
||||
if IS_FNUZ_MAIN:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b8, bitcast=True)
|
||||
else:
|
||||
x_fp8 = x_uint8.to(tl.float8e4nv, bitcast=True)
|
||||
encoded_scales = tl.load(
|
||||
@@ -1374,8 +1396,8 @@ def _sparse_attn_decode_ragged_kernel(
|
||||
mask=valid[:, None] & nope_mask[None, :],
|
||||
other=0,
|
||||
)
|
||||
if IS_FNUZ:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b15, bitcast=True)
|
||||
if IS_FNUZ_EXTRA:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b8, bitcast=True)
|
||||
else:
|
||||
x_fp8 = x_uint8.to(tl.float8e4nv, bitcast=True)
|
||||
encoded_scales = tl.load(
|
||||
@@ -1485,7 +1507,12 @@ def _sparse_attn_decode_partial_kernel(
|
||||
NOPE_DIM: tl.constexpr,
|
||||
NOPE_BLOCK: tl.constexpr,
|
||||
ROPE_DIM: tl.constexpr,
|
||||
IS_FNUZ: tl.constexpr,
|
||||
# `main_cache` is the SWA K-cache (written by the C++ encoder, FNUZ on
|
||||
# gfx942 / OCP on gfx950). `extra_cache` is the compressed K-cache
|
||||
# (Triton encoder, OCP on every platform). Reading both with the same
|
||||
# `IS_FNUZ` would decode one of them with the wrong FNUZ/OCP scale ratio.
|
||||
IS_FNUZ_MAIN: tl.constexpr,
|
||||
IS_FNUZ_EXTRA: tl.constexpr,
|
||||
BLOCK_H: tl.constexpr,
|
||||
BLOCK_K: tl.constexpr,
|
||||
NUM_SPLITS: tl.constexpr,
|
||||
@@ -1551,8 +1578,8 @@ def _sparse_attn_decode_partial_kernel(
|
||||
mask=valid[:, None] & nope_mask[None, :],
|
||||
other=0,
|
||||
)
|
||||
if IS_FNUZ:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b15, bitcast=True)
|
||||
if IS_FNUZ_MAIN:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b8, bitcast=True)
|
||||
else:
|
||||
x_fp8 = x_uint8.to(tl.float8e4nv, bitcast=True)
|
||||
encoded_scales = tl.load(
|
||||
@@ -1622,8 +1649,8 @@ def _sparse_attn_decode_partial_kernel(
|
||||
mask=valid[:, None] & nope_mask[None, :],
|
||||
other=0,
|
||||
)
|
||||
if IS_FNUZ:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b15, bitcast=True)
|
||||
if IS_FNUZ_EXTRA:
|
||||
x_fp8 = x_uint8.to(tl.float8e4b8, bitcast=True)
|
||||
else:
|
||||
x_fp8 = x_uint8.to(tl.float8e4nv, bitcast=True)
|
||||
encoded_scales = tl.load(
|
||||
@@ -2095,7 +2122,8 @@ def _rocm_sparse_attn_decode_ragged_triton(
|
||||
NOPE_DIM=nope_head_dim,
|
||||
NOPE_BLOCK=nope_block,
|
||||
ROPE_DIM=rope_head_dim,
|
||||
IS_FNUZ=is_fnuz,
|
||||
IS_FNUZ_MAIN=is_fnuz,
|
||||
IS_FNUZ_EXTRA=False,
|
||||
BLOCK_H=block_h,
|
||||
BLOCK_K=block_k,
|
||||
num_warps=8,
|
||||
@@ -2153,7 +2181,12 @@ def _rocm_sparse_attn_decode_ragged_triton(
|
||||
NOPE_DIM=nope_head_dim,
|
||||
NOPE_BLOCK=nope_block,
|
||||
ROPE_DIM=rope_head_dim,
|
||||
IS_FNUZ=is_fnuz,
|
||||
# main_cache = swa_k_cache (C++ encoder, FNUZ on gfx942 / OCP on gfx950).
|
||||
# extra_cache = compressed kv_cache (Triton encoder, OCP everywhere).
|
||||
# Reading both with a single IS_FNUZ would decode one of them with the
|
||||
# wrong FNUZ/OCP scale ratio (~1.87×).
|
||||
IS_FNUZ_MAIN=is_fnuz,
|
||||
IS_FNUZ_EXTRA=False,
|
||||
BLOCK_H=block_h,
|
||||
BLOCK_K=block_k,
|
||||
NUM_SPLITS=num_splits,
|
||||
|
||||
Reference in New Issue
Block a user