Download code/flash-linear-attention/tests/modules/test_l2norm.py from amonshano/echo: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/amonshano/echo/resolve/main/code/flash-linear-attention/tests/modules/test_l2norm.py
- Command line
-
hf download hf://amonshano/echo/code/flash-linear-attention/tests/modules/test_l2norm.py
-
curl -L -o test_l2norm.py https://huggingface.co/amonshano/echo/resolve/main/code/flash-linear-attention/tests/modules/test_l2norm.py
1.12 kB
| import pytest | |
| import torch | |
| import torch.nn.functional as F | |
| from fla.modules.l2norm import l2_norm | |
| from fla.utils import assert_close, device | |
| def test_l2norm(B: int, T: int, H: int, D: int, dtype: torch.dtype): | |
| torch.manual_seed(42) | |
| x = torch.randn(B, T, H, D, dtype=dtype).to(device).requires_grad_(True) | |
| x = x * 0.5 + 0.3 | |
| ref = F.normalize(x, dim=-1, p=2) | |
| tri = l2_norm(x) | |
| ref_dx = torch.autograd.grad(ref.sum(), x)[0] | |
| tri_dx = torch.autograd.grad(tri.sum(), x)[0] | |
| assert_close('y', ref, tri, 0.005) | |
| assert_close('dx', ref_dx, tri_dx, 0.005) | |