(T, provider)
| 31 | ) |
| 32 | ) |
| 33 | def benchmark(T, provider): |
| 34 | device = 'cuda' |
| 35 | dtype = torch.bfloat16 |
| 36 | requires_grad = True |
| 37 | B, D = 16, 1024 |
| 38 | |
| 39 | x = torch.randn(B * T, D, device=device, requires_grad=requires_grad, dtype=dtype) |
| 40 | |
| 41 | quantiles = [0.5, 0.2, 0.8] |
| 42 | results = 0, 0, 0 |
| 43 | if provider.startswith('naive_ln'): |
| 44 | norm = nn.LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) |
| 45 | results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) |
| 46 | if provider.startswith('fused_ln'): |
| 47 | norm = LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) |
| 48 | results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) |
| 49 | if provider.startswith('naive_gn'): |
| 50 | norm = nn.GroupNorm(4, D).to(device=device, dtype=dtype) |
| 51 | results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) |
| 52 | if provider.startswith('fused_gn'): |
| 53 | norm = GroupNorm(4, D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) |
| 54 | results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) |
| 55 | if provider.startswith('naive_ln_bwd'): |
| 56 | norm = nn.LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) |
| 57 | results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) |
| 58 | if provider.startswith('fused_ln_bwd'): |
| 59 | norm = LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) |
| 60 | results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) |
| 61 | if provider.startswith('naive_gn_bwd'): |
| 62 | norm = nn.GroupNorm(4, D).to(device=device, dtype=dtype) |
| 63 | results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) |
| 64 | if provider.startswith('fused_gn_bwd'): |
| 65 | norm = GroupNorm(4, D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) |
| 66 | results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) |
| 67 | return results |
| 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
nothing calls this directly
no outgoing calls
no test coverage detected