Download code/flash-linear-attention/benchmarks/modules/benchmark_layernorm.py from amonshano/echo: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/amonshano/echo/resolve/main/code/flash-linear-attention/benchmarks/modules/benchmark_layernorm.py
- Command line
-
hf download hf://amonshano/echo/code/flash-linear-attention/benchmarks/modules/benchmark_layernorm.py
-
curl -L -o benchmark_layernorm.py https://huggingface.co/amonshano/echo/resolve/main/code/flash-linear-attention/benchmarks/modules/benchmark_layernorm.py
3.32 kB
| import torch | |
| import torch.nn as nn | |
| import triton | |
| from fla.modules import GroupNorm, LayerNorm | |
| def benchmark(T, provider): | |
| from fla.utils import device | |
| dtype = torch.bfloat16 | |
| requires_grad = True | |
| B, D = 16, 1024 | |
| x = torch.randn(B * T, D, device=device, requires_grad=requires_grad, dtype=dtype) | |
| quantiles = [0.5, 0.2, 0.8] | |
| results = 0, 0, 0 | |
| if provider.startswith('naive_ln'): | |
| norm = nn.LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) | |
| if provider.startswith('fused_ln'): | |
| norm = LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) | |
| if provider.startswith('naive_gn'): | |
| norm = nn.GroupNorm(4, D).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) | |
| if provider.startswith('fused_gn'): | |
| norm = GroupNorm(4, D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) | |
| if provider.startswith('naive_ln_bwd'): | |
| norm = nn.LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) | |
| if provider.startswith('fused_ln_bwd'): | |
| norm = LayerNorm(D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) | |
| if provider.startswith('naive_gn_bwd'): | |
| norm = nn.GroupNorm(4, D).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) | |
| if provider.startswith('fused_gn_bwd'): | |
| norm = GroupNorm(4, D, elementwise_affine=True, bias=True).to(device=device, dtype=dtype) | |
| results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) | |
| return results | |
| if __name__ == '__main__': | |
| benchmark.run(print_data=True) | |