-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathmla_decoding_split_kv.py
More file actions
224 lines (184 loc) · 6.84 KB
/
Copy pathmla_decoding_split_kv.py
File metadata and controls
224 lines (184 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: MIT
"""
CUDA Tile C++ MLA Decoding with Split-KV Implementation
Implements Multi-head Latent Attention with split-KV for efficient parallel processing.
Optimized to use tensor_span + partition_view with contiguous tensor layouts.
"""
import math
from pathlib import Path
from typing import Optional
import numpy as np
import torch
from tilegym.backend import register_impl
from tilegym.ops.tilecpp.splitk_reduce import splitk_reduce as _tilecpp_splitk_reduce
from tilegym.ops.tilecpp.utils._cuda_utils import TileCppKernel
from tilegym.ops.tilecpp.utils._cuda_utils import get_dtype_info
from tilegym.ops.tilecpp.utils._dump_types import dump_kernel_types
def next_power_of_2(n: int) -> int:
"""Return the smallest power of 2 >= n."""
if n <= 0:
return 1
n -= 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
return n + 1
def cdiv(a: int, b: int) -> int:
"""Ceiling division."""
return (a + b - 1) // b
# =============================================================================
# Kernel Definitions
# =============================================================================
_mla_decode_kernel = TileCppKernel(
source_path=Path(__file__).parent / "mla_decoding_split_kv.cuh",
kernel_name="naive_absorb_mla_transpose",
)
def _get_cpp_type(dtype: torch.dtype) -> str:
"""Get C++ type string for a torch dtype."""
type_map = {
torch.float32: "float",
torch.float16: "__half",
torch.bfloat16: "__nv_bfloat16",
}
return type_map.get(dtype, "float")
def _launch_mla_decode_kernel(
Q: torch.Tensor,
QPE: torch.Tensor,
K: torch.Tensor,
V: torch.Tensor,
KPE: torch.Tensor,
Att_Out: torch.Tensor,
LSE_Out: torch.Tensor,
sm_scale: float,
B: int,
NUM_HEADS: int,
S_kv: int,
kv_len_per_split: int,
TILE_D: int,
TILE_H: int,
TILE_N: int,
TILE_KPE: int,
NUM_KV_SPLITS: int,
):
"""Launch the MLA decode split-KV kernel with all dimensions as template params."""
dtype = Q.dtype
cpp_type = _get_cpp_type(dtype)
dump_kernel_types("naive_absorb_mla_transpose", Q, QPE, K, V, KPE)
# All dimensions as template parameters for optimal code generation
# Template: T, B, NUM_HEADS, S_KV, TILE_D, TILE_H, TILE_N, TILE_KPE, NUM_KV_SPLITS, KV_LEN_PER_SPLIT, EVEN_N
EVEN_N = S_kv % TILE_N == 0
template_params = [B, NUM_HEADS, S_kv, TILE_D, TILE_H, TILE_N, TILE_KPE, NUM_KV_SPLITS, kv_len_per_split, EVEN_N]
kernel, _, _ = _mla_decode_kernel.get_kernel(
dtype=dtype,
template_params=template_params,
signature=f"const {cpp_type}*, const {cpp_type}*, const {cpp_type}*, const {cpp_type}*, const {cpp_type}*, {cpp_type}*, float*, float",
)
num_head_groups = cdiv(NUM_HEADS, TILE_H)
grid = (num_head_groups, B, NUM_KV_SPLITS)
_mla_decode_kernel.launch(
grid=grid,
kernel=kernel,
args=[
np.uint64(Q.data_ptr()),
np.uint64(QPE.data_ptr()),
np.uint64(K.data_ptr()),
np.uint64(V.data_ptr()),
np.uint64(KPE.data_ptr()),
np.uint64(Att_Out.data_ptr()),
np.uint64(LSE_Out.data_ptr()),
np.float32(sm_scale),
],
)
# =============================================================================
# Autograd Function
# =============================================================================
class _mla_decoding_split_kv(torch.autograd.Function):
@staticmethod
def forward(ctx, Q, QPE, KV, KPE, sm_scale, kv_len_per_split=None):
"""
MLA Decoding with Split-KV Forward.
Args:
Q: Query tensor [B, NUM_HEADS, HEAD_DIM]
QPE: Query positional embedding [B, NUM_HEADS, KPE_DIM]
KV: Key-Value tensor [B, S_kv, HEAD_DIM]
KPE: Key positional embedding [B, S_kv, KPE_DIM]
sm_scale: Softmax scale
kv_len_per_split: KV length per split (optional)
Returns:
O: Output tensor [B, NUM_HEADS, HEAD_DIM]
"""
B, NUM_HEADS, HEAD_DIM = Q.shape
TILE_KPE = QPE.shape[-1]
S_kv = KV.shape[1]
TILE_D = HEAD_DIM
TILE_H = 16 # Heads per tile
TILE_N = 128 # KV sequence per tile
# per-split KV span down/up through next_power_of_2(S_kv // estimate).
if kv_len_per_split is None:
NUM_SMS = torch.cuda.get_device_properties("cuda").multi_processor_count
num_split_kv_estimated = max(1, NUM_SMS // B)
kv_len_per_split = next_power_of_2(S_kv // num_split_kv_estimated)
kv_len_per_split = max(kv_len_per_split, TILE_N)
assert kv_len_per_split == next_power_of_2(kv_len_per_split)
assert kv_len_per_split >= TILE_N
NUM_KV_SPLITS = cdiv(S_kv, kv_len_per_split)
# Allocate intermediate tensors with contiguous layout
Att_Out = torch.empty(
(B, NUM_HEADS, NUM_KV_SPLITS, TILE_D),
device=Q.device,
dtype=Q.dtype,
)
LSE_Out = torch.empty(
(B, NUM_HEADS, NUM_KV_SPLITS),
device=Q.device,
dtype=torch.float32,
)
O = torch.empty_like(Q)
# Launch decode kernel
_launch_mla_decode_kernel(
Q,
QPE,
KV,
KV,
KPE, # K=V=KV
Att_Out,
LSE_Out,
sm_scale,
B,
NUM_HEADS,
S_kv,
kv_len_per_split,
TILE_D,
TILE_H,
TILE_N,
TILE_KPE,
NUM_KV_SPLITS,
)
_tilecpp_splitk_reduce(Att_Out, LSE_Out, O, S_kv)
return O.reshape((B, NUM_HEADS, TILE_D))
@staticmethod
def backward(ctx, do):
raise NotImplementedError("MLA Decoding Split-KV backward is not implemented yet")
@register_impl("mla_decoding_split_kv", backend="tilecpp")
def mla_decoding_split_kv(q, qpe, kv, kpe, sm_scale=None, kv_len_per_split=None, **kwargs):
"""
MLA Decoding with Split-KV interface
Args:
q: Query tensor [batch_size, seq_len, head_dim]
qpe: Query positional embedding [batch_size, seq_len, kpe_dim]
kv: Key-Value tensor [batch_size, kv_seq_len, head_dim]
kpe: Key positional embedding [batch_size, kv_seq_len, kpe_dim]
sm_scale: Softmax scale (defaults to 1/sqrt(head_dim + kpe_dim))
kv_len_per_split: kv_len_per_split
**kwargs: Additional arguments for backend-specific configurations
Returns:
o: Output tensor [batch_size, seq_len, head_dim]
"""
if sm_scale is None:
sm_scale = 1.0 / (math.sqrt(q.size(-1) + qpe.size(-1)))
o = _mla_decoding_split_kv.apply(q, qpe, kv, kpe, sm_scale, kv_len_per_split)
return o