forked from ROCm/flashinfer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_decode_example.py
More file actions
158 lines (146 loc) · 5.07 KB
/
Copy pathbatch_decode_example.py
File metadata and controls
158 lines (146 loc) · 5.07 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
# SPDX-FileCopyrightText : 2025 Advanced Micro Devices, Inc.
#
# SPDX-License-Identifier : Apache-2.0
import torch
import flashinfer
def verify_tensors(tensor1, tensor2, rtol=1e-3, atol=1e-3):
for i in range(tensor1.shape[0]):
for j in range(tensor1.shape[1]):
if torch.abs(tensor1[i][j] - tensor2[i][j]) > atol + rtol * torch.abs(
tensor2[i][j]
):
print(f"Error at {i}, {j}")
print(f"Expected: {tensor2[i][j]}")
print(f"Got: {tensor1[i][j]}")
return False
return True
def batch_decode_with_paged_kv_cache_example(
batch_size,
kv_len,
page_size,
num_kv_heads,
num_qo_heads,
head_dim,
kv_layout,
pos_encoding_mode,
logits_soft_cap,
return_lse,
q_dtype,
kv_dtype,
contiguous_kv,
):
q = torch.randn(batch_size, num_qo_heads, head_dim, device="cuda:0", dtype=q_dtype)
num_pages_per_seq = (kv_len + page_size - 1) // page_size
total_num_pages = num_pages_per_seq * batch_size
result = 0
if kv_layout == "HND":
kv_shape = [total_num_pages, 2, num_kv_heads, page_size, head_dim]
else:
kv_shape = [total_num_pages, 2, page_size, num_kv_heads, head_dim]
if not contiguous_kv:
tmp = [kv_shape[0]]
for v in kv_shape[1:]:
tmp.append(2)
tmp.append(v)
kv_shape = tmp
kv_data_fp32 = torch.randn(*kv_shape, dtype=torch.float32, device="cuda:0")
kv_data = kv_data_fp32.to(kv_dtype)
kv_data = kv_data[:, 1, :, 1, :, 1, :, 1, :]
kv_data_fp32 = kv_data_fp32[:, 1, :, 1, :, 1, :, 1, :]
# actual data is stored in non-contiguous memory
assert (
kv_data.stride(-4)
!= kv_data.shape[-3] * kv_data.shape[-2] * kv_data.shape[-1]
)
else:
kv_data_fp32 = torch.randn(*kv_shape, dtype=torch.float32, device="cuda:0")
kv_data = kv_data_fp32.to(kv_dtype)
kv_indptr = (
torch.arange(0, batch_size + 1, device="cuda:0", dtype=torch.int32)
* num_pages_per_seq
)
kv_indices = torch.arange(0, total_num_pages, device="cuda:0", dtype=torch.int32)
kv_last_page_len = torch.full(
(batch_size,), (kv_len - 1) % page_size + 1, dtype=torch.int32, device="cuda:0"
)
workspace_buffer = torch.empty(32 * 1024 * 1024, dtype=torch.int8, device="cuda:0")
wrapper = flashinfer.decode.BatchDecodeWithPagedKVCacheWrapper(
workspace_buffer, kv_layout
)
wrapper.plan(
kv_indptr,
kv_indices,
kv_last_page_len,
num_qo_heads,
num_kv_heads,
head_dim,
page_size,
logits_soft_cap=logits_soft_cap,
pos_encoding_mode=pos_encoding_mode,
data_type=kv_dtype,
q_data_type=q_dtype,
)
if return_lse:
o, _ = wrapper.run(q, kv_data, return_lse=True)
else:
o = wrapper.run(q, kv_data)
for i in range(batch_size):
perm_dims = [0, 2, 1, 3] if kv_layout == "HND" else [0, 1, 2, 3]
perm_dims_last = [1, 0, 2] if kv_layout == "HND" else [0, 1, 2]
qi = q[i]
ki = torch.cat(
[
kv_data_fp32[kv_indptr[i] : kv_indptr[i + 1] - 1, 0]
.permute(*perm_dims)
.reshape(-1, num_kv_heads, head_dim),
(
kv_data_fp32[kv_indptr[i + 1] - 1, 0, :, : kv_last_page_len[i]]
if kv_layout == "HND"
else kv_data_fp32[kv_indptr[i + 1] - 1, 0, : kv_last_page_len[i], :]
)
.permute(*perm_dims_last)
.reshape(-1, num_kv_heads, head_dim),
],
dim=0,
).to(kv_dtype)
vi = torch.cat(
[
kv_data_fp32[kv_indptr[i] : kv_indptr[i + 1] - 1, 1]
.permute(*perm_dims)
.reshape(-1, num_kv_heads, head_dim),
(
kv_data_fp32[kv_indptr[i + 1] - 1, 1, :, : kv_last_page_len[i]]
if kv_layout == "HND"
else kv_data_fp32[kv_indptr[i + 1] - 1, 1, : kv_last_page_len[i], :]
)
.permute(*perm_dims_last)
.reshape(-1, num_kv_heads, head_dim),
],
dim=0,
).to(kv_dtype)
o_ref_i = flashinfer.single_decode_with_kv_cache(qi, ki, vi)
result += verify_tensors(o[i], o_ref_i, rtol=1e-3, atol=1e-3)
# test user-allocated output
o_buffer = torch.empty_like(o)
wrapper.run(q, kv_data, out=o_buffer)
torch.testing.assert_close(o, o_buffer, rtol=1e-3, atol=1e-3)
if result == batch_size:
print("PASS")
else:
print("FAIL")
if __name__ == "__main__":
batch_decode_with_paged_kv_cache_example(
batch_size=256,
kv_len=8192,
page_size=8,
num_kv_heads=4,
num_qo_heads=32,
head_dim=256,
kv_layout="NHD",
pos_encoding_mode="NONE",
logits_soft_cap=0.0,
return_lse=False,
q_dtype=torch.float16,
kv_dtype=torch.float16,
contiguous_kv=True,
)