|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import math |
| 16 | + |
15 | 17 | import pytest |
16 | 18 | import torch |
| 19 | +import torch.nn.functional as F |
17 | 20 | import triton |
18 | 21 | from packaging.version import Version |
19 | 22 |
|
|
22 | 25 | from . import accuracy_utils as utils |
23 | 26 | from . import conftest as cfg |
24 | 27 |
|
| 28 | +try: |
| 29 | + from scipy.linalg import hadamard as scipy_hadamard |
| 30 | +except ImportError: # pragma: no cover |
| 31 | + scipy_hadamard = None |
| 32 | + |
25 | 33 | _TRITON_VERSION = Version(triton.__version__.split("+")[0]) |
26 | 34 | _SKIP_JOIN_BUG = _TRITON_VERSION < Version("3.5.0") |
27 | 35 | _skip_if_join_bug = pytest.mark.skipif( |
28 | 36 | _SKIP_JOIN_BUG, |
29 | 37 | reason=f"triton {triton.__version__} has tt.join layout bug (fixed in 3.5.0)", |
30 | 38 | ) |
31 | 39 |
|
| 40 | +# Dao-style dims for standard FHT (exclude 16384/32768: scipy full matrix too large) |
| 41 | +if cfg.QUICK_MODE: |
| 42 | + HADAMARD_DIMS = [64, 137, 256, 512, 1024] |
| 43 | +else: |
| 44 | + HADAMARD_DIMS = [ |
| 45 | + 1, |
| 46 | + 2, |
| 47 | + 4, |
| 48 | + 8, |
| 49 | + 16, |
| 50 | + 32, |
| 51 | + 64, |
| 52 | + 128, |
| 53 | + 256, |
| 54 | + 512, |
| 55 | + 137, |
| 56 | + 1024, |
| 57 | + 2048, |
| 58 | + 4096, |
| 59 | + 8192, |
| 60 | + ] |
| 61 | + |
32 | 62 | if cfg.QUICK_MODE: |
33 | 63 | HADAMARD_MN_CASES = [ |
34 | 64 | (1536, 3, "12N"), |
|
57 | 87 | } |
58 | 88 |
|
59 | 89 |
|
| 90 | +def _hadamard_transform_ref(x, scale=1.0): |
| 91 | + """Reference matching Dao-AILab fast_hadamard_transform_interface.hadamard_transform_ref.""" |
| 92 | + if scipy_hadamard is None: |
| 93 | + raise ImportError("Please install scipy") |
| 94 | + x_shape = x.shape |
| 95 | + dim = x.shape[-1] |
| 96 | + x = x.reshape(-1, dim) |
| 97 | + log_dim = math.ceil(math.log2(dim)) if dim > 0 else 0 |
| 98 | + dim_padded = 1 << log_dim if dim > 0 else 1 |
| 99 | + if dim != dim_padded: |
| 100 | + x = F.pad(x, (0, dim_padded - dim)) |
| 101 | + out = F.linear( |
| 102 | + x, |
| 103 | + torch.tensor( |
| 104 | + scipy_hadamard(dim_padded, dtype=float), |
| 105 | + dtype=x.dtype, |
| 106 | + device=x.device, |
| 107 | + ), |
| 108 | + ) |
| 109 | + out = out * scale |
| 110 | + return out[..., :dim].reshape(*x_shape) |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.hadamard_transform |
| 114 | +@_skip_if_join_bug |
| 115 | +@pytest.mark.parametrize("dtype", utils.FLOAT_DTYPES) |
| 116 | +@pytest.mark.parametrize("dim", HADAMARD_DIMS) |
| 117 | +def test_hadamard_transform(dim, dtype): |
| 118 | + """Dao-style accuracy test vs scipy Hadamard matrix multiply (fp32 ground truth).""" |
| 119 | + if scipy_hadamard is None: |
| 120 | + pytest.skip("scipy is required for hadamard_transform_ref") |
| 121 | + |
| 122 | + atol = 3e-3 if dtype == torch.float32 else 5e-3 |
| 123 | + if dtype == torch.bfloat16: |
| 124 | + atol = 5e-2 |
| 125 | + |
| 126 | + torch.random.manual_seed(0) |
| 127 | + batch_size = 15 |
| 128 | + device = flag_gems.device |
| 129 | + x = torch.randn(batch_size, dim, device=device, dtype=dtype).requires_grad_() |
| 130 | + x_ref = x.detach().clone().requires_grad_() |
| 131 | + x_pt = x.detach().clone().requires_grad_() |
| 132 | + scale = 1 / math.sqrt(dim) |
| 133 | + |
| 134 | + out = flag_gems.hadamard_transform(x, scale=scale) |
| 135 | + out_ref = _hadamard_transform_ref(x_ref.float(), scale=scale) |
| 136 | + out_pt = _hadamard_transform_ref(x_pt, scale=scale) |
| 137 | + |
| 138 | + out_err = (out.float() - out_ref).abs().max().item() |
| 139 | + pt_err = (out_pt.float() - out_ref).abs().max().item() |
| 140 | + assert ( |
| 141 | + out_err < 2 * pt_err + atol |
| 142 | + ), f"forward dim={dim} dtype={dtype}: out_err={out_err}, pt_err={pt_err}, atol={atol}" |
| 143 | + |
| 144 | + g = torch.randn_like(out) |
| 145 | + out.backward(g) |
| 146 | + out_ref.backward(g) |
| 147 | + out_pt.backward(g) |
| 148 | + |
| 149 | + dx_err = (x.grad.float() - x_ref.grad.float()).abs().max().item() |
| 150 | + dx_pt_err = (x_pt.grad.float() - x_ref.grad.float()).abs().max().item() |
| 151 | + assert ( |
| 152 | + dx_err < 2 * dx_pt_err + atol |
| 153 | + ), f"backward dim={dim} dtype={dtype}: dx_err={dx_err}, dx_pt_err={dx_pt_err}, atol={atol}" |
| 154 | + |
| 155 | + |
60 | 156 | def _ref_mn(x: torch.Tensor, M: int) -> torch.Tensor: |
61 | 157 | """Reference: 2-kernel version (H_M column transform in fp32 + standard FHT).""" |
62 | 158 | *leading, dim = x.shape |
@@ -93,7 +189,10 @@ def _ref_mn(x: torch.Tensor, M: int) -> torch.Tensor: |
93 | 189 |
|
94 | 190 | ym = torch.stack(rows, dim=1).reshape(batch * M, n_cols) # keep fp32 |
95 | 191 | ym = flag_gems.hadamard_transform(ym) # FHT in fp32 |
96 | | - return ym.to(orig_dtype).reshape(*leading, dim) |
| 192 | + out = ym.to(orig_dtype).reshape(*leading, dim) |
| 193 | + if cfg.TO_CPU: |
| 194 | + out = out.cpu() |
| 195 | + return out |
97 | 196 |
|
98 | 197 |
|
99 | 198 | @pytest.mark.hadamard_transform_mn |
|
0 commit comments