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

Function benchmark

benchmarks/ops/benchmark_retention.py:42–85  ·  view source on GitHub ↗
(T, provider)

Source from the content-addressed store, hash-verified

40 )
41)
42def benchmark(T, provider):
43 device = 'cuda'
44 dtype = torch.bfloat16
45 requires_grad = True
46 B, H, D = 4, 8, 256
47 os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
48
49 if provider == 'flash' or provider == 'flash_bwd':
50 q = torch.randn(B, T, H, D, device=device, requires_grad=requires_grad, dtype=dtype)
51 k = torch.randn(B, T, H, D, device=device, requires_grad=requires_grad, dtype=dtype)
52 v = torch.randn(B, T, H, D, device=device, requires_grad=requires_grad, dtype=dtype)
53 else:
54 q = torch.randn(B, H, T, D, device=device, requires_grad=requires_grad, dtype=dtype)
55 k = torch.randn(B, H, T, D, device=device, requires_grad=requires_grad, dtype=dtype)
56 v = torch.randn(B, H, T, D, device=device, requires_grad=requires_grad, dtype=dtype)
57 do = torch.ones_like(q, dtype=dtype)
58
59 quantiles = [0.5, 0.2, 0.8]
60 results = 0, 0, 0
61 if provider == 'torch':
62 if T > 2048:
63 return results
64 results = triton.testing.do_bench(lambda: naive_retention(q, k, v), quantiles=quantiles)
65 elif provider == 'recurrent':
66 results = triton.testing.do_bench(lambda: fused_recurrent_retention(q, k, v), quantiles=quantiles)
67 elif provider == 'chunk':
68 results = triton.testing.do_bench(lambda: chunk_retention(q, k, v), quantiles=quantiles)
69 elif provider == 'parallel':
70 results = triton.testing.do_bench(lambda: parallel_retention(q, k, v), quantiles=quantiles)
71 elif provider == 'torch_bwd':
72 if T > 2048:
73 return results
74 results = triton.testing.do_bench(lambda: naive_retention(q, k, v).backward(do), quantiles=quantiles)
75 elif provider == 'recurrent_bwd':
76 results = triton.testing.do_bench(lambda: fused_recurrent_retention(q, k, v)[0].backward(do), quantiles=quantiles)
77 elif provider == 'chunk_bwd':
78 results = triton.testing.do_bench(lambda: chunk_retention(q, k, v)[0].backward(do), quantiles=quantiles)
79 elif provider == 'parallel_bwd':
80 results = triton.testing.do_bench(lambda: parallel_retention(q, k, v)[0].backward(do), quantiles=quantiles)
81 elif provider == 'flash':
82 results = triton.testing.do_bench(lambda: flash_attn_func(q, k, v, causal=True), quantiles=quantiles)
83 elif provider == 'flash_bwd':
84 results = triton.testing.do_bench(lambda: flash_attn_func(q, k, v, causal=True).backward(do), quantiles=quantiles)
85 return results
86
87
88if __name__ == '__main__':

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected