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

Function benchmark

benchmarks/ops/benchmark_abc.py:39–73  ·  view source on GitHub ↗
(T, provider)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected