Download code/flash-linear-attention/benchmarks/modules/benchmark_l2norm.py from amonshano/echo: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/amonshano/echo/resolve/main/code/flash-linear-attention/benchmarks/modules/benchmark_l2norm.py
- Command line
-
hf download hf://amonshano/echo/code/flash-linear-attention/benchmarks/modules/benchmark_l2norm.py
-
curl -L -o benchmark_l2norm.py https://huggingface.co/amonshano/echo/resolve/main/code/flash-linear-attention/benchmarks/modules/benchmark_l2norm.py
2.57 kB
| from functools import partial | |
| import torch | |
| import torch.nn.functional as F | |
| import triton | |
| from fla.modules.l2norm import l2norm | |
| def benchmark(B, H, D, T, provider): | |
| from fla.utils import device | |
| dtype = torch.bfloat16 | |
| requires_grad = True | |
| 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'): | |
| norm = partial(F.normalize, dim=-1, p=2) | |
| results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) | |
| if provider.startswith('compiled'): | |
| norm = torch.compile(partial(F.normalize, dim=-1, p=2)) | |
| results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) | |
| if provider.startswith('fused'): | |
| norm = l2norm | |
| results = triton.testing.do_bench(lambda: norm(x), quantiles=quantiles) | |
| if provider.startswith('naive_bwd'): | |
| norm = partial(F.normalize, dim=-1, p=2) | |
| results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) | |
| if provider.startswith('compiled_bwd'): | |
| norm = torch.compile(partial(F.normalize, dim=-1, p=2)) | |
| results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) | |
| if provider.startswith('fused_bwd'): | |
| norm = l2norm | |
| results = triton.testing.do_bench(lambda: norm(x).backward(x), quantiles=quantiles) | |
| return results | |
| if __name__ == '__main__': | |
| benchmark.run(print_data=True) | |