[ROCm][Perf][GLM-5.3-Flash] Remove redundant copy after ragged sparse MLA (#58569)
Signed-off-by: simondanielsson <simon.danielsson99@hotmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
5688a4dd4a
commit
751fa96a49
@@ -2190,6 +2190,7 @@ def _sparse_attn_prefill_ragged_kernel(
|
||||
num_kv,
|
||||
scale,
|
||||
HAS_ATTN_SINK: tl.constexpr,
|
||||
OUT_DV: tl.constexpr,
|
||||
BLOCK_H: tl.constexpr,
|
||||
BLOCK_D: tl.constexpr,
|
||||
BLOCK_K: tl.constexpr,
|
||||
@@ -2280,7 +2281,7 @@ def _sparse_attn_prefill_ragged_kernel(
|
||||
+ head_offsets[:, None] * out_stride_h
|
||||
+ dim_offsets[None, :] * out_stride_d,
|
||||
out,
|
||||
mask=head_mask[:, None] & dim_mask[None, :],
|
||||
mask=head_mask[:, None] & (dim_offsets[None, :] < OUT_DV),
|
||||
)
|
||||
|
||||
|
||||
@@ -3442,6 +3443,7 @@ def _rocm_sparse_attn_prefill_ragged_triton(
|
||||
attn_sink: torch.Tensor | None,
|
||||
nope_head_dim: int,
|
||||
rope_head_dim: int,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> torch.Tensor:
|
||||
assert q.ndim == 3, f"expected q=[sq,h,d], got {q.shape}"
|
||||
assert kv.ndim == 2, f"expected kv=[skv,d], got {kv.shape}"
|
||||
@@ -3472,7 +3474,15 @@ def _rocm_sparse_attn_prefill_ragged_triton(
|
||||
block_d = triton.next_power_of_2(head_dim)
|
||||
block_k = 16 if head_dim >= 256 else 32
|
||||
num_warps = 4
|
||||
out = torch.empty_like(q)
|
||||
if out is None:
|
||||
out = torch.empty_like(q)
|
||||
else:
|
||||
assert out.shape[:2] == q.shape[:2], (
|
||||
f"expected out=[{num_queries},{num_heads},*], got {out.shape}"
|
||||
)
|
||||
assert out.shape[-1] in (nope_head_dim, head_dim), (
|
||||
f"expected out width {nope_head_dim} or {head_dim}, got {out.shape[-1]}"
|
||||
)
|
||||
_sparse_attn_prefill_ragged_kernel[(num_queries, triton.cdiv(num_heads, block_h))](
|
||||
q,
|
||||
kv,
|
||||
@@ -3493,6 +3503,7 @@ def _rocm_sparse_attn_prefill_ragged_triton(
|
||||
kv.shape[0],
|
||||
float(scale),
|
||||
HAS_ATTN_SINK=has_attn_sink,
|
||||
OUT_DV=out.shape[-1],
|
||||
BLOCK_H=block_h,
|
||||
BLOCK_D=block_d,
|
||||
BLOCK_K=block_k,
|
||||
@@ -3510,6 +3521,7 @@ def _rocm_sparse_attn_prefill_triton(
|
||||
nope_head_dim: int,
|
||||
rope_head_dim: int,
|
||||
topk_length: torch.Tensor | None = None,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> torch.Tensor:
|
||||
ragged_indices, ragged_indptr = build_ragged_indices_from_dense(
|
||||
indices,
|
||||
@@ -3527,6 +3539,7 @@ def _rocm_sparse_attn_prefill_triton(
|
||||
attn_sink=attn_sink,
|
||||
nope_head_dim=nope_head_dim,
|
||||
rope_head_dim=rope_head_dim,
|
||||
out=out,
|
||||
)
|
||||
|
||||
|
||||
@@ -4180,7 +4193,7 @@ def rocm_sparse_attn_prefill(
|
||||
return
|
||||
|
||||
if ragged_indices is not None and ragged_indptr is not None:
|
||||
output_chunk = _rocm_sparse_attn_prefill_ragged_triton(
|
||||
_rocm_sparse_attn_prefill_ragged_triton(
|
||||
q=q,
|
||||
kv=kv.squeeze(1),
|
||||
indices=ragged_indices,
|
||||
@@ -4189,11 +4202,12 @@ def rocm_sparse_attn_prefill(
|
||||
attn_sink=None if attn_sink is None else attn_sink[: q.shape[1]],
|
||||
nope_head_dim=nope_head_dim,
|
||||
rope_head_dim=rope_head_dim,
|
||||
out=output,
|
||||
)
|
||||
else:
|
||||
assert indices is not None
|
||||
indices_2d = indices.reshape(indices.shape[0], -1)
|
||||
output_chunk = _rocm_sparse_attn_prefill_triton(
|
||||
_rocm_sparse_attn_prefill_triton(
|
||||
q=q,
|
||||
kv=kv.squeeze(1),
|
||||
indices=indices_2d,
|
||||
@@ -4202,8 +4216,8 @@ def rocm_sparse_attn_prefill(
|
||||
nope_head_dim=nope_head_dim,
|
||||
rope_head_dim=rope_head_dim,
|
||||
topk_length=topk_length,
|
||||
out=output,
|
||||
)
|
||||
output.copy_(output_chunk[..., : output.shape[-1]].to(output.dtype))
|
||||
|
||||
|
||||
def rocm_sparse_attn_decode(
|
||||
|
||||
Reference in New Issue
Block a user