Skip to content

Commit af99abb

Browse files
Merge branch 'FlagOpen:master' into fill(cpp-wrapper)
2 parents d680ed5 + 1a679d2 commit af99abb

38 files changed

Lines changed: 1537 additions & 848 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ bin-release/
99
__pycache__/
1010
.vscode/
1111

12+
# Python venv directory
13+
.venv
14+
1215
# Distribution files
1316
build/
1417
*.egg-info/

benchmark/core_shapes.yaml

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ outer:
44
- [1024, 1024]
55
- [4096, 4096]
66
- [8192, 8192]
7-
- [10240, 10240] #from perf
7+
- [10240, 10240] #from perf
88

99
single_dim_shapes: &single_dim_shapes
1010
shapes:
@@ -23,31 +23,31 @@ randperm:
2323

2424
gelu backward:
2525
shapes:
26-
- [1048576,] # 1024 * 1024
26+
- [1048576] # 1024 * 1024
2727
- [64, 64]
2828
- [4096, 4096]
2929
- [64, 512, 512]
3030
- [256, 1024, 1024]
3131

3232
softmax backward:
3333
shapes:
34-
- [1048576,] # 1024 * 1024
34+
- [1048576] # 1024 * 1024
3535
- [64, 64]
3636
- [4096, 4096]
3737
- [64, 512, 512]
3838
- [256, 1024, 1024]
3939

4040
diag:
4141
shapes:
42-
- [64,]
43-
- [1024,]
42+
- [64]
43+
- [1024]
4444
- [64, 64]
4545
- [256, 1024]
4646
- [1024, 1024]
4747

4848
quantile:
4949
shapes:
50-
- [1048576,] # 1024 * 1024
50+
- [1048576] # 1024 * 1024
5151
- [64, 64]
5252
- [4096, 256]
5353
- [64, 512, 128]
@@ -60,7 +60,7 @@ BlasBenchmark:
6060
- [16, 1024, 1024, 1024]
6161
- [16, 2048, 2048, 2048]
6262
- [16, 4096, 4096, 4096]
63-
shape_desc: "B, M, N, K" # shapes are defined as (B, M, N, K)
63+
shape_desc: "B, M, N, K" # shapes are defined as (B, M, N, K)
6464

6565
# NORM shapes can be either 3D or 4D:
6666
# - 3D shapes are represented as [batch_size, channels, hidden_size]
@@ -75,7 +75,6 @@ NormBenchmark:
7575
- [20, 6, 65536] # from perf
7676
shape_desc: "N, C, *"
7777

78-
7978
TensorSelectBenchmark:
8079
shapes:
8180
- [64, 64]
@@ -130,8 +129,8 @@ GenericBenchmarkExcluse1D:
130129

131130
GenericBenchmarkExcluse3D:
132131
shapes:
133-
- [1048576,] # 1024 * 1024
134-
- [1073741824,] # 1024 * 1024 * 1024
132+
- [1048576] # 1024 * 1024
133+
- [1073741824] # 1024 * 1024 * 1024
135134
- [1024, 512]
136135
- [1024, 1024]
137136
- [4096, 4096]
@@ -147,15 +146,15 @@ GenericBenchmark2DOnly:
147146

148147
UnaryReductionBenchmark:
149148
shapes:
150-
- [1048576,] # 1024 * 1024
149+
- [1048576] # 1024 * 1024
151150
- [64, 64]
152151
- [4096, 4096]
153152
- [64, 512, 512]
154153
- [1024, 1024, 1024]
155154

156155
UnaryPointwiseBenchmark:
157156
shapes:
158-
- [1073741824,] # 1024 * 1024 * 1024
157+
- [1073741824] # 1024 * 1024 * 1024
159158
- [64, 64]
160159
- [4096, 4096]
161160
- [64, 512, 512]
@@ -165,7 +164,7 @@ UnaryPointwiseBenchmark:
165164
# the default shapes info
166165
Benchmark:
167166
shapes:
168-
- [1073741824,] # 1024 * 1024 * 1024
167+
- [1073741824] # 1024 * 1024 * 1024
169168
- [64, 64]
170169
- [4096, 4096]
171170
- [64, 512, 512]
@@ -174,16 +173,16 @@ Benchmark:
174173
# default Ni Ci Hi WI Co Hk Wk stride padding groups
175174
ConvBenchmark:
176175
shapes:
177-
- [32, 64, 128, 128, 32, 3, 3, 1, 1, 1]
178-
- [32, 64, 128, 128, 32, 3, 3, 1, 2, 1]
179-
- [104, 16, 32, 32, 32, 4, 4, 1, 0, 1]
180-
- [64, 32, 18, 18, 32, 5, 5, 2, 1, 1]
181-
- [64, 32, 210, 210, 64, 5, 5, 2, 1, 1]
182-
- [32, 64, 210, 210, 16, 5, 5, 2, 1, 1]
183-
- [16, 32, 12, 12, 24, 3, 3, 2, 1, 1]
184-
- [16, 32, 24, 24, 24, 3, 3, 1, 1, 2]
185-
- [16, 32, 24, 24, 24, 3, 3, 2, 2, 2]
186-
- [16, 32, 24, 24, 24, 3, 3, 1, 2, 2]
176+
- [32, 64, 128, 128, 32, 3, 3, 1, 1, 1]
177+
- [32, 64, 128, 128, 32, 3, 3, 1, 2, 1]
178+
- [104, 16, 32, 32, 32, 4, 4, 1, 0, 1]
179+
- [64, 32, 18, 18, 32, 5, 5, 2, 1, 1]
180+
- [64, 32, 210, 210, 64, 5, 5, 2, 1, 1]
181+
- [32, 64, 210, 210, 16, 5, 5, 2, 1, 1]
182+
- [16, 32, 12, 12, 24, 3, 3, 2, 1, 1]
183+
- [16, 32, 24, 24, 24, 3, 3, 1, 1, 2]
184+
- [16, 32, 24, 24, 24, 3, 3, 2, 2, 2]
185+
- [16, 32, 24, 24, 24, 3, 3, 1, 2, 2]
187186

188187
AttentionBenchmark:
189188
shapes:
@@ -195,40 +194,48 @@ AttentionBenchmark:
195194

196195
KronBenchmark:
197196
shapes:
198-
- [16,16]
199-
- [64,64]
200-
- [128,128]
201-
- [256,256]
202-
- [4, 8, 16, 32]
197+
- [16, 16]
198+
- [64, 64]
199+
- [128, 128]
200+
- [256, 256]
201+
- [4, 8, 16, 32]
203202

204203
IndexPutAccFalseBenchmark:
205204
shapes:
206-
- [[268435456,], [[65536,],], [65536,]]
207-
- [[32, 32], [[8,], [2, 8]], [8,]]
208-
- [[1024, 1024], [[4, 64],], [1024,]]
209-
- [[512, 512, 512], [[2, 128], [128,], [128,]], [128,]]
210-
- [[512, 512, 512], [[2, 128],], [512,]]
205+
- [[268435456], [[65536]], [65536]]
206+
- [[32, 32], [[8], [2, 8]], [8]]
207+
- [[1024, 1024], [[4, 64]], [1024]]
208+
- [[512, 512, 512], [[2, 128], [128], [128]], [128]]
209+
- [[512, 512, 512], [[2, 128]], [512]]
211210

212211
IndexPutAccTrueBenchmark:
213212
shapes:
214-
- [[268435456,], [[65536,],], [65536,]]
215-
- [[32, 32], [[8,], [8,]], [8,]]
216-
- [[1024, 1024], [[64,], [64,]], [64,]]
217-
- [[512, 512, 512], [[128,], [128,], [128,]], [128,]]
218-
- [[512, 512, 512], [[2, 128], [2, 128], [2, 128]], [2, 128]]
213+
- [[268435456], [[65536]], [65536]]
214+
- [[32, 32], [[8], [8]], [8]]
215+
- [[1024, 1024], [[64], [64]], [64]]
216+
- [[512, 512, 512], [[128], [128], [128]], [128]]
217+
- [[512, 512, 512], [[2, 128], [2, 128], [2, 128]], [2, 128]]
219218

220219
glu:
221220
shapes:
222-
- [4, 8, 512, 128]
223-
- [4, 8, 1024, 128]
224-
- [4, 8, 2048, 128]
225-
- [4, 8, 3072, 128]
226-
- [4, 8, 4096, 128]
221+
- [4, 8, 512, 128]
222+
- [4, 8, 1024, 128]
223+
- [4, 8, 2048, 128]
224+
- [4, 8, 3072, 128]
225+
- [4, 8, 4096, 128]
227226

228227
index:
229228
shapes:
230-
- [[268435456,], [[65536,],]]
231-
- [[32, 32], [[8,], [2, 8]]]
232-
- [[1024, 1024], [[64,], [64,]]]
233-
- [[512, 512, 512], [[2, 128],]]
234-
- [[64, 64, 64], [[2, 8], [2, 8]]]
229+
- [[268435456], [[65536]]]
230+
- [[32, 32], [[8], [2, 8]]]
231+
- [[1024, 1024], [[64], [64]]]
232+
- [[512, 512, 512], [[2, 128]]]
233+
- [[64, 64, 64], [[2, 8], [2, 8]]]
234+
235+
FlashMLABenchmark:
236+
shapes:
237+
- [1024]
238+
- [2048]
239+
- [4096]
240+
- [8192]
241+
- [16384]

benchmark/performance_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ def get_latency(self, op, *args, **kwargs):
258258
if self.is_backward:
259259
out = fn()
260260
dout = torch.randn_like(out)
261-
fn = lambda: out.backward(dout, retain_graph=True)
261+
# fn = lambda: out.backward(dout, retain_graph=True)
262+
xs = list(filter(lambda x: torch.is_tensor(x) and x.requires_grad, args))
263+
fn = lambda: torch.autograd.grad(
264+
(out,), xs, grad_outputs=(dout,), retain_graph=True
265+
)
262266
if Config.cpu_mode:
263267
for i in range(Config.warm_up):
264268
fn()
@@ -280,6 +284,7 @@ def get_latency(self, op, *args, **kwargs):
280284
warmup=Config.warm_up,
281285
rep=Config.repetition,
282286
return_mode="median",
287+
grad_to_none=xs if self.is_backward else None,
283288
)
284289
# average latency in ms
285290
return latency

benchmark/test_attention_perf.py

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import math
2+
13
import pytest
24
import torch
5+
import triton
36

47
import flag_gems
58

@@ -21,7 +24,7 @@ def set_more_shapes(self):
2124
@pytest.mark.skipif(
2225
flag_gems.device == "musa" or vendor_name == "hygon", reason="RuntimeError"
2326
)
24-
@pytest.mark.attention
27+
@pytest.mark.scaled_dot_product_attention
2528
@pytest.mark.parametrize("dropout_p", [0.0, 0.25])
2629
@pytest.mark.parametrize("is_causal", [True, False])
2730
def test_perf_scaled_dot_product_attention(dropout_p, is_causal):
@@ -55,3 +58,112 @@ def sdpa_flash(query, key, value, dropout_p=dropout_p, is_causal=is_causal):
5558
],
5659
)
5760
bench.run()
61+
62+
63+
class FlashMLABenchmark(GenericBenchmark):
64+
"""
65+
benchmark for flash_mla
66+
"""
67+
68+
def set_more_shapes(self):
69+
# self.shapes is a list of tuples, each containing three elements:
70+
# (batch, num_heads, seq_len, head_size).
71+
return None
72+
73+
74+
@pytest.mark.skipif(vendor_name == "kunlunxin", reason="RESULT TODOFIX")
75+
@pytest.mark.skipif(
76+
flag_gems.device == "musa" or vendor_name == "hygon", reason="RuntimeError"
77+
)
78+
@pytest.mark.flash_mla
79+
def test_perf_flash_mla():
80+
def flash_mla_kwargs(shape, dtype, device):
81+
seqlen = shape[0]
82+
b = 128
83+
s_q = 1
84+
h_q = 128
85+
h_kv = 1
86+
d = 576
87+
dv = 512
88+
causal = True
89+
block_size = 64
90+
cache_seqlens = torch.tensor(
91+
[seqlen + 2 * i for i in range(b)], dtype=torch.int32, device=device
92+
)
93+
max_seqlen = cache_seqlens.max().item()
94+
max_seqlen_pad = triton.cdiv(max_seqlen, 256) * 256
95+
96+
q = torch.randn([b, s_q, h_q, d], dtype=dtype, device=device)
97+
block_table = torch.arange(
98+
b * max_seqlen_pad // block_size, dtype=torch.int32, device=device
99+
).view(b, max_seqlen_pad // block_size)
100+
blocked_k = torch.randn(
101+
[block_table.numel(), block_size, h_kv, d], dtype=dtype, device=device
102+
)
103+
yield q, block_table, blocked_k, max_seqlen_pad, block_size, b, s_q, cache_seqlens, h_q, h_kv, d, dv, causal
104+
105+
def scaled_dot_product_attention(query, key, value, h_q, h_kv, is_causal=False):
106+
query = query.float()
107+
key = key.float()
108+
value = value.float()
109+
key = key.repeat_interleave(h_q // h_kv, dim=0)
110+
value = value.repeat_interleave(h_q // h_kv, dim=0)
111+
attn_weight = query @ key.transpose(-2, -1) / math.sqrt(query.size(-1))
112+
if is_causal:
113+
s_q = query.shape[-2]
114+
s_k = key.shape[-2]
115+
attn_bias = torch.zeros(s_q, s_k, dtype=query.dtype, device=query.device)
116+
temp_mask = torch.ones(
117+
s_q, s_k, dtype=torch.bool, device=query.device
118+
).tril(diagonal=s_k - s_q)
119+
attn_bias.masked_fill_(temp_mask.logical_not(), float("-inf"))
120+
attn_bias.to(query.dtype)
121+
attn_weight += attn_bias
122+
lse = attn_weight.logsumexp(dim=-1)
123+
attn_weight = torch.softmax(attn_weight, dim=-1, dtype=torch.float32)
124+
return attn_weight @ value, lse
125+
126+
def ref_mla(
127+
q,
128+
block_table,
129+
blocked_k,
130+
max_seqlen_pad,
131+
block_size,
132+
b,
133+
s_q,
134+
cache_seqlens,
135+
h_q,
136+
h_kv,
137+
d,
138+
dv,
139+
causal,
140+
):
141+
device = q.device
142+
blocked_v = blocked_k[..., :dv]
143+
out = torch.empty(b, s_q, h_q, dv, dtype=torch.float32, device=device)
144+
lse = torch.empty(b, h_q, s_q, dtype=torch.float32, device=device)
145+
for i in range(b):
146+
begin = i * max_seqlen_pad
147+
end = begin + cache_seqlens[i]
148+
O, LSE = scaled_dot_product_attention(
149+
q[i].transpose(0, 1),
150+
blocked_k.view(-1, h_kv, d)[begin:end].transpose(0, 1),
151+
blocked_v.view(-1, h_kv, dv)[begin:end].transpose(0, 1),
152+
h_q=h_q,
153+
h_kv=h_kv,
154+
is_causal=causal,
155+
)
156+
out[i] = O.transpose(0, 1)
157+
lse[i] = LSE
158+
return out, lse
159+
160+
bench = FlashMLABenchmark(
161+
op_name="flash_mla",
162+
input_fn=flash_mla_kwargs,
163+
torch_op=ref_mla,
164+
dtypes=[
165+
torch.bfloat16,
166+
],
167+
)
168+
bench.set_gems(flag_gems.flash_mla)
169+
bench.run()

benchmark/test_binary_pointwise_perf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def get_tflops(self, op, *args, **kwargs):
4949
("mul", torch.mul, FLOAT_DTYPES),
5050
("sub", torch.sub, FLOAT_DTYPES),
5151
("pow", torch.pow, FLOAT_DTYPES),
52-
("rsub", torch.rsub, FLOAT_DTYPES),
5352
*(
5453
[
5554
("polar", torch.polar, [torch.float32]),
@@ -75,7 +74,6 @@ def get_tflops(self, op, *args, **kwargs):
7574
# Bitwise operations
7675
("bitwise_and", torch.bitwise_and, INT_DTYPES + BOOL_DTYPES),
7776
("bitwise_or", torch.bitwise_or, INT_DTYPES + BOOL_DTYPES),
78-
("or_", torch.bitwise_or, INT_DTYPES + BOOL_DTYPES),
7977
# Numerical Checks
8078
("isclose", torch.isclose, FLOAT_DTYPES + INT_DTYPES),
8179
("allclose", torch.allclose, FLOAT_DTYPES + INT_DTYPES),

0 commit comments

Comments
 (0)