MCPcopy Create free account
hub / github.com/OpenSparseLLMs/MoM / benchmark

Function benchmark

benchmarks/ops/benchmark_gsa.py:41–78  ·  view source on GitHub ↗
(T, provider)

Source from the content-addressed store, hash-verified

39 )
40)
41def benchmark(T, provider):
42 device = 'cuda'
43 dtype = torch.bfloat16
44 requires_grad = True
45 B, H, D, M = 16, 4, 128, 64
46
47 q = torch.randn(B, H, T, D, device=device, requires_grad=requires_grad, dtype=dtype)
48 k = torch.randn(B, H, T, D, device=device, requires_grad=requires_grad, dtype=dtype)
49 v = torch.randn(B, H, T, D, device=device, requires_grad=requires_grad, dtype=dtype)
50 if provider.startswith('gsa'):
51 f = F.logsigmoid(torch.randn(B, H, T, M, device=device, dtype=dtype))
52 s = (1 - f.exp()).to(f.dtype)
53 if provider.startswith('gla'):
54 g = F.logsigmoid(torch.randn(B, H, T, D, device=device, dtype=dtype))
55 g = g.clamp_min(-5).requires_grad_(requires_grad)
56 if provider.startswith('flash'):
57 q = torch.randn(B, T, H, D, device=device, requires_grad=requires_grad, dtype=dtype)
58 k = torch.randn(B, T, H, D, device=device, requires_grad=requires_grad, dtype=dtype)
59 v = torch.randn(B, T, H, D, device=device, requires_grad=requires_grad, dtype=dtype)
60 do = torch.ones_like(v, dtype=dtype)
61
62 quantiles = [0.5, 0.2, 0.8]
63 if provider == 'gsa_recurrent':
64 return triton.testing.do_bench(lambda: fused_recurrent_gsa(q, k, v, s, f), quantiles=quantiles)
65 if provider == 'gsa_chunk':
66 return triton.testing.do_bench(lambda: chunk_gsa(q, k, v, s, f), quantiles=quantiles)
67 elif provider == 'gla':
68 return triton.testing.do_bench(lambda: chunk_gla(q, k, v, g), quantiles=quantiles)
69 elif provider == 'gsa_recurrent_bwd':
70 return triton.testing.do_bench(lambda: fused_recurrent_gsa(q, k, v, s, f)[0].backward(do), quantiles=quantiles)
71 elif provider == 'gsa_chunk_bwd':
72 return triton.testing.do_bench(lambda: chunk_gsa(q, k, v, s, f)[0].backward(do), quantiles=quantiles)
73 elif provider == 'gla_bwd':
74 return triton.testing.do_bench(lambda: chunk_gla(q, k, v, g)[0].backward(do), quantiles=quantiles)
75 elif provider == 'retention_bwd':
76 return triton.testing.do_bench(lambda: chunk_retention(q, k, v)[0].backward(do), quantiles=quantiles)
77 elif provider == 'flash_bwd':
78 return triton.testing.do_bench(lambda: flash_attn_func(q, k, v, causal=True).backward(do), quantiles=quantiles)
79
80
81if __name__ == '__main__':

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected