-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathattention_mha.py
More file actions
573 lines (512 loc) · 19.7 KB
/
attention_mha.py
File metadata and controls
573 lines (512 loc) · 19.7 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
from typing import Optional
import aiter
import torch
from aiter import fused_qk_norm_rope_cache_quant_shuffle
from aiter.ops.triton.fused_kv_cache import fused_qk_rope_reshape_and_cache
from aiter.ops.triton.gluon.pa_decode_gluon import get_recommended_splits
from aiter.ops.triton.unified_attention import unified_attention
from atom.config import get_current_atom_config
from atom.utils.forward_context import ForwardContext, get_forward_context
from torch import nn
from .attention_mla import MLAModules
import logging
from atom.plugin.prepare import is_plugin_mode, is_vllm
from atom.plugin.attention_mha import PagedAttentionImplDecoratorForPluginMode
from atom.model_ops.base_attention import cp_mha_gather_cache
logger = logging.getLogger("atom")
@PagedAttentionImplDecoratorForPluginMode
class PagedAttentionImpl(nn.Module):
"""
Attention paged implementation
"""
def __init__(
self,
num_heads,
head_dim,
scale,
num_kv_heads,
alibi_slopes: list[float] | None,
sliding_window: Optional[int] = None,
kv_cache_dtype="bf16",
logits_soft_cap: float | None = None,
attn_type=None,
kv_sharing_target_layer_name: int | None = None,
layer_num=0,
mla_modules: Optional[MLAModules] = None,
sinks: Optional[nn.Parameter] = None,
rotary_emb: Optional[torch.nn.Module] = None,
q_norm: Optional[torch.nn.Module] = None,
k_norm: Optional[torch.nn.Module] = None,
**kwargs,
):
super().__init__()
self.num_heads = num_heads
self.head_dim = head_dim
# for upper framework, it uses head_size in built-in methods
self.head_size = head_dim
self.scale = scale
self.num_kv_heads = num_kv_heads
self.alibi_slopes = alibi_slopes
self.k_cache = self.v_cache = torch.tensor([])
self.kv_cache_dtype = kv_cache_dtype
self.max_model_len = 0
self.k_scale = self.v_scale = None
self.device = "cuda:" + str(torch.cuda.current_device())
self.layer_num = layer_num
self.kv_scale_float = (
torch.finfo(torch.float8_e4m3fn).max / torch.finfo(aiter.dtypes.fp8).max
if self.kv_cache_dtype == "fp8"
else 1.0
)
self.kv_scale = torch.tensor(self.kv_scale_float, dtype=torch.float32)
self.sinks = sinks
self.sliding_window = sliding_window if sliding_window is not None else -1
self.rotary_emb = rotary_emb
self.q_norm = q_norm
self.k_norm = k_norm
# for plugin mode(vllm), the query quant is disabled for now
if is_vllm():
self.supports_quant_query_input = False
def forward_impl_server_mode(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
position: torch.Tensor = None,
q_scale: torch.Tensor = None,
qkv: torch.Tensor = None,
):
fwd_ctx: ForwardContext = get_forward_context()
# dummy run will skip attention in cuda graph capture phase
if fwd_ctx.context.is_dummy_run:
o = torch.empty_like(q)
return o
o: torch.Tensor
q = q.view(-1, self.num_heads, self.head_dim)
k = k.view(-1, self.num_kv_heads, self.head_dim)
v = v.view(-1, self.num_kv_heads, self.head_dim)
# rope cache
q, k, v, k_cache, v_cache, k_scale, v_scale = self.rope_cache(
q, k, v, qkv, position, fwd_ctx
)
attn_impl = self.dispatch_backend(fwd_ctx)
o = attn_impl(q, k, v, k_cache, v_cache, k_scale, v_scale, fwd_ctx)
o = o.view(-1, self.num_heads * self.head_dim)
return o
def rope_cache(self, q, k, v, qkv, position, fwd_ctx: ForwardContext):
attn_metadata = fwd_ctx.attn_metadata
kv_cache_data = fwd_ctx.kv_cache_data
k_cache = kv_cache_data[f"layer_{self.layer_num}"].k_cache
v_cache = kv_cache_data[f"layer_{self.layer_num}"].v_cache
k_scale = kv_cache_data[f"layer_{self.layer_num}"].k_scale
v_scale = kv_cache_data[f"layer_{self.layer_num}"].v_scale
use_triton_attn = self.sliding_window != -1 or self.head_dim != 128
self.use_triton_attn = use_triton_attn
if (
self.rotary_emb is not None
and self.q_norm is not None
and self.k_norm is not None
):
# fused_qk_norm_rope_cache_quant_shuffle expects V cache layout
# [num_blocks, num_kv_heads, block_size//x, head_size, x], not [n, nh, hd, bs]
x = 16 // k_cache.element_size()
if k_cache.dim() == 5 and v_cache.dim() == 4:
n, nh, hd, bs = v_cache.shape
v_cache_shuffle = v_cache.view(n, nh, bs // x, hd, x)
else:
v_cache_shuffle = v_cache
fused_qk_norm_rope_cache_quant_shuffle(
qkv,
num_heads_q=self.num_heads,
num_heads_k=self.num_kv_heads,
num_heads_v=self.num_kv_heads,
head_dim=self.head_dim,
eps=self.q_norm.eps,
qw=self.q_norm.weight,
kw=self.k_norm.weight,
cos_sin_cache=self.rotary_emb.cos_sin_cache,
is_neox_style=self.rotary_emb.is_neox_style,
pos_ids=position,
k_cache=k_cache,
v_cache=v_cache_shuffle,
slot_mapping=attn_metadata.slot_mapping,
kv_cache_dtype=(
"auto" if self.kv_cache_dtype == "bf16" else self.kv_cache_dtype
),
k_scale=k_scale,
v_scale=v_scale,
)
qkv = qkv.view(qkv.shape[0], -1, self.head_dim)
q, k, v = qkv.split(
[self.num_heads, self.num_kv_heads, self.num_kv_heads], dim=1
)
elif use_triton_attn and self.rotary_emb is not None:
k_scale = v_scale = self.kv_scale
q, k, k_cache, v_cache = fused_qk_rope_reshape_and_cache(
q,
k,
v,
k_cache,
v_cache,
attn_metadata.slot_mapping,
position,
self.rotary_emb.cos_cache,
self.rotary_emb.sin_cache,
k_scale,
v_scale,
self.rotary_emb.is_neox_style,
flash_layout=False,
apply_scale=self.kv_cache_dtype.startswith("fp8"),
offs=None,
q_out=q,
k_out=k,
output_zeros=False,
)
else:
# for asm paged attention
asm_layout = True
if use_triton_attn:
asm_layout = False
if self.rotary_emb is not None:
assert position is not None
q, k = self.rotary_emb(position, q, k)
if self.q_norm is not None:
q = self.q_norm(q)
if self.k_norm is not None:
k = self.k_norm(k)
if self.kv_cache_dtype == "fp8":
aiter.reshape_and_cache_with_pertoken_quant(
k,
v,
k_cache,
v_cache,
k_scale,
v_scale,
attn_metadata.slot_mapping,
asm_layout=asm_layout,
)
else:
aiter.reshape_and_cache(
k,
v,
k_cache,
v_cache,
attn_metadata.slot_mapping,
kv_cache_dtype="auto",
k_scale=None,
v_scale=None,
asm_layout=asm_layout,
)
# Prefix cache hit: gather cached KV from paged cache and concat with new tokens
if attn_metadata.has_cached:
q, k, v, k_cache, v_cache, k_scale, v_scale = (
self._gather_prefix_and_concat_kv(
q, k, v, k_cache, v_cache, k_scale, v_scale, attn_metadata
)
)
return q, k, v, k_cache, v_cache, k_scale, v_scale
def _gather_prefix_and_concat_kv(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
k_cache: torch.Tensor,
v_cache: torch.Tensor,
k_scale: torch.Tensor,
v_scale: torch.Tensor,
attn_metadata,
):
"""
When prefix cache hits, gather full KV (cached + new) from paged cache in
one pass. New tokens are already written by fused_qk_rope_reshape_and_cache.
Same flow as gather_kv_b_proj: write new first, then read cached+new together.
token_to_batch, seq_starts are built in prepare_prefill.
"""
cu_seqlens_k = attn_metadata.cu_seqlens_k
total_tokens = cu_seqlens_k[-1].item()
token_to_batch = attn_metadata.token_to_batch
seq_starts = attn_metadata.seq_starts
num_kv_heads = k.shape[1]
head_dim = k.shape[2]
device = k.device
dtype = k.dtype
k_full = torch.empty(
(total_tokens, num_kv_heads, head_dim), dtype=dtype, device=device
)
v_full = torch.empty(
(total_tokens, num_kv_heads, head_dim), dtype=dtype, device=device
)
# Convert cache for cp_mha_gather_cache
# fused_qk_norm_rope_cache_quant_shuffle: K [n, nh, hd//x, bs, x], V [n, nh, bs//x, hd, x] (SHUFFLE)
# fused_qk_rope_reshape_and_cache: K [n, nh, hd//x, bs, x], V [n, nh, hd, bs] -> NHD
if k_cache.dim() == 5:
x = 16 // k_cache.element_size()
n, nh, _, block_size, _ = k_cache.shape
if v_cache.dim() == 4:
# fused_qk_norm_rope_cache_quant_shuffle: V data in [n, nh, bs//x, hd, x] layout
use_shuffle = True
k_cache_gather = k_cache
v_cache_gather = v_cache.view(n, nh, block_size // x, head_dim, x)
else:
# fused_qk_rope_reshape_and_cache: V [n, nh, hd, bs] -> NHD
use_shuffle = False
k_cache_gather = (
k_cache.permute(0, 3, 1, 2, 4)
.contiguous()
.view(n, block_size, nh, head_dim)
)
v_cache_gather = v_cache.permute(0, 3, 1, 2).contiguous()
else:
use_shuffle = False
k_cache_gather = k_cache
v_cache_gather = v_cache
block_size = k_cache.shape[1]
block_tables = attn_metadata.block_tables
cp_mha_gather_cache(
key_cache=k_cache_gather,
value_cache=v_cache_gather,
key=k_full,
value=v_full,
block_tables=block_tables,
k_scales=k_scale,
v_scales=v_scale,
cu_seqlens_kv=cu_seqlens_k,
token_to_batch=token_to_batch,
seq_starts=seq_starts,
dequant=self.kv_cache_dtype.startswith("fp8"),
kv_cache_layout="SHUFFLE" if use_shuffle else "NHD",
total_tokens=total_tokens,
)
return q, k_full, v_full, k_cache, v_cache, k_scale, v_scale
def paged_attention_triton(
self, q, k, v, k_cache, v_cache, k_scale, v_scale, fwd_ctx: ForwardContext
):
attn_metadata = fwd_ctx.attn_metadata
o = torch.empty_like(q)
num_seqs = attn_metadata.context_lens.shape[0]
_, num_q_heads_total, head_size = q.shape
num_blocks, num_kv_heads, _, block_size, _ = k_cache.shape
# assume all query have same length
query_group_size = attn_metadata.max_seqlen_q * (
num_q_heads_total // num_kv_heads
)
assert num_q_heads_total % num_kv_heads == 0
max_context_partition_num = get_recommended_splits(num_seqs, num_kv_heads)
context_partition_size = 256
if self.sliding_window > 0:
max_context_partition_num = 1
context_partition_size = 128
# Output buffers (same as Triton)
intermediate_shape = (
num_seqs,
num_kv_heads,
max_context_partition_num,
query_group_size,
)
exp_sums = torch.empty(intermediate_shape, dtype=torch.float32, device=q.device)
max_logits = torch.empty(
intermediate_shape, dtype=torch.float32, device=q.device
)
temporary_output = torch.empty(
*intermediate_shape,
head_size,
dtype=q.dtype,
device=q.device,
)
if k_scale is not None and k_scale.numel() > 1:
k_scale = k_scale.unsqueeze(-1)
v_scale = v_scale.unsqueeze(-1)
compute_type = (
torch.bfloat16
if self.kv_cache_dtype == "bf16" # or per_tensor
else aiter.dtypes.fp8
)
torch.ops.aiter.pa_decode_gluon(
o,
q,
k_cache,
v_cache,
attn_metadata.context_lens,
attn_metadata.block_tables,
self.scale,
attn_metadata.max_seqlen_q,
max_context_partition_num,
context_partition_size,
compute_type,
None, # q_scale
None if self.kv_cache_dtype == "bf16" else k_scale,
None if self.kv_cache_dtype == "bf16" else v_scale,
exp_sums=exp_sums,
max_logits=max_logits,
temporary_output=temporary_output,
alibi_slopes=None,
sinks=self.sinks,
sliding_window=self.sliding_window,
ps=True,
)
return o
def paged_attention_asm(
self, q, k, v, k_cache, v_cache, k_scale, v_scale, fwd_ctx: ForwardContext
):
attn_metadata = fwd_ctx.attn_metadata
o = aiter.pa_fwd_asm(
q,
k_cache,
v_cache,
attn_metadata.block_tables,
attn_metadata.context_lens,
attn_metadata.block_tables.stride(0),
max_qlen=attn_metadata.max_seqlen_q,
K_QScale=k_scale,
V_QScale=v_scale,
out_=None,
qo_indptr=attn_metadata.cu_seqlens_q,
high_precision=0,
)
return o
def paged_attention_persistent_asm(
self, q, k, v, k_cache, v_cache, k_scale, v_scale, fwd_ctx: ForwardContext
):
attn_metadata = fwd_ctx.attn_metadata
output = torch.empty_like(q)
aiter.pa_persistent_fwd(
Q=q,
K=k_cache,
V=v_cache,
output=output,
max_qlen=attn_metadata.max_seqlen_q,
qo_indptr=attn_metadata.cu_seqlens_q,
kv_indptr=attn_metadata.kv_indptr,
kv_indices=attn_metadata.kv_indices,
context_lens=attn_metadata.context_lens,
K_QScale=k_scale,
V_QScale=v_scale,
work_indptr=attn_metadata.work_indptr,
work_info=attn_metadata.work_info_set,
reduce_indptr=attn_metadata.reduce_indptr,
reduce_final_map=attn_metadata.reduce_final_map,
reduce_partial_map=attn_metadata.reduce_partial_map,
softmax_scale=self.scale,
mask=1,
)
return output
def prefill_attention(
self, q, k, v, k_cache, v_cache, k_scale, v_scale, fwd_ctx: ForwardContext
):
# variable lenth attention use key value as input
attn_metadata = fwd_ctx.attn_metadata
sliding_window = (
(self.sliding_window, 0, 0)
if self.sliding_window is not None
else (-1, -1, 0)
)
o = aiter.flash_attn_varlen_func(
q,
k,
v,
cu_seqlens_q=attn_metadata.cu_seqlens_q,
cu_seqlens_k=attn_metadata.cu_seqlens_k,
max_seqlen_q=attn_metadata.max_seqlen_q,
max_seqlen_k=attn_metadata.max_seqlen_k,
min_seqlen_q=attn_metadata.min_seqlen_q,
dropout_p=attn_metadata.dropout_p,
softmax_scale=self.scale,
causal=True,
window_size=sliding_window,
sink_ptr=self.sinks,
)
return o
def prefill_attention_triton(
self, q, k, v, k_cache, v_cache, k_scale, v_scale, fwd_ctx: ForwardContext
):
# the unified_attention supports both prefill attention and decode attention, but it only support
# flash-layout kv_cache.
#
# key_cache: [num_blocks, block_size, num_kv_heads, head_size]
# value_cache: [num_blocks, num_kv_heads, head_size, block_size]
#
# if the paged_attention supports only non-flash-layout kv_cache and kv_cache is also cached as
# non-flash-layout in rope_cache phase, the unified_attention should use key and value as kv_cache
# with block_size 1 and fake block_table.
#
# key: [num_blocks, 1, num_kv_heads, head_size]
# value: [num_blocks, 1, num_kv_heads, head_size]
attn_metadata = fwd_ctx.attn_metadata
block_tables = attn_metadata.block_tables
o = torch.empty_like(q)
descale_shape = (attn_metadata.cu_seqlens_q.shape[0] - 1, k.shape[1])
sliding_window = (
(self.sliding_window - 1, 0)
if self.sliding_window is not None
else (-1, -1)
)
unified_attention(
q,
k_cache,
v_cache,
o,
cu_seqlens_q=attn_metadata.cu_seqlens_q,
seqused_k=attn_metadata.context_lens,
max_seqlen_q=attn_metadata.max_seqlen_q,
max_seqlen_k=attn_metadata.max_seqlen_k,
softmax_scale=self.scale,
causal=True,
alibi_slopes=None,
window_size=sliding_window,
block_table=block_tables,
softcap=0,
q_descale=None,
k_descale=self.kv_scale.expand(descale_shape),
v_descale=self.kv_scale.expand(descale_shape),
sinks=self.sinks,
)
return o
def dispatch_backend(self, fwd_ctx: ForwardContext):
ctx = fwd_ctx.context
if ctx.is_prefill:
return self.prefill_attention
else:
if self.use_triton_attn:
return self.paged_attention_triton
else:
# Only use pa persistent when block_size == 1024
atom_config = get_current_atom_config()
if atom_config.kv_cache_block_size == 1024:
return self.paged_attention_persistent_asm
return self.paged_attention_asm
def forward(
self,
layer: torch.nn.Module,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
kv_cache: torch.Tensor = None,
attn_metadata=None,
position: torch.Tensor = None,
q_scale: Optional[torch.Tensor] = None,
qkv: torch.Tensor = None,
output: torch.Tensor = None,
**kwargs,
):
if is_plugin_mode():
# forward impl method are added by the decorator
# PagedAttentionImplDecoratorForPluginMode
return self.forward_impl_plugin_mode(
layer=layer,
query=query,
key=key,
value=value,
kv_cache=kv_cache,
attn_metadata=attn_metadata,
position=position,
q_scale=q_scale,
qkv=qkv,
)
else:
# only for server mode, keep the original method
o = self.forward_impl_server_mode(
q=query, k=key, v=value, position=position, q_scale=q_scale, qkv=qkv
)
return o