|
| 1 | +"""Tests for splash attention mask. |
| 2 | +
|
| 3 | +This code is adapted from the jax_ml/jax library, specifically from the |
| 4 | +https://github.com/jax-ml/jax/blob/9bcfac6542a330b77f29d5cc5dcf4a57f55b2947/tests/pallas/tpu_splash_attention_mask_test.py |
| 5 | +TODO(dhwang2): Delete this file once JAX is upgraded and LocalMask becomes a computable mask. |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from absl.testing import absltest, parameterized |
| 10 | +from jax.experimental.pallas.ops.tpu.splash_attention import splash_attention_mask as mask_lib |
| 11 | + |
| 12 | +from axlearn.common.attention_bias import causal_mask, sliding_window_causal_mask |
| 13 | +from axlearn.common.flash_attention.splash_attention_mask import ComputableMask |
| 14 | +from axlearn.common.test_utils import TestCase |
| 15 | + |
| 16 | + |
| 17 | +class SplashAttentionMaskTest(TestCase): |
| 18 | + @parameterized.parameters( |
| 19 | + [ |
| 20 | + ((256, 256), (1024, 1024)), |
| 21 | + ((256, 128), (1024, 1024)), |
| 22 | + ((128, 256), (1024, 1024)), |
| 23 | + ((256, 256), (1024, 2048)), |
| 24 | + ((256, 128), (1024, 2048)), |
| 25 | + ((128, 256), (1024, 2048)), |
| 26 | + ((256, 256), (2048, 1024)), |
| 27 | + ((256, 128), (2048, 1024)), |
| 28 | + ((128, 256), (2048, 1024)), |
| 29 | + ] |
| 30 | + ) |
| 31 | + def test_causal_mask_fn(self, block_size, shape): |
| 32 | + """Test ComputableMask with causal_mask function from attention_bias.py.""" |
| 33 | + # Create expected dense causal mask |
| 34 | + q_len, kv_len = shape |
| 35 | + q_ids = np.arange(q_len)[:, None] |
| 36 | + kv_ids = np.arange(kv_len)[None, :] |
| 37 | + dense_mask = causal_mask(q_ids, kv_ids) |
| 38 | + |
| 39 | + # Create ComputableMask with mask_fn |
| 40 | + lazy_mask = ComputableMask(shape=shape, mask_fn=causal_mask) |
| 41 | + |
| 42 | + self._compare_masks(dense_mask, lazy_mask, block_size) |
| 43 | + |
| 44 | + @parameterized.parameters( |
| 45 | + [ |
| 46 | + ((256, 256), (1024, 1024), 128), |
| 47 | + ((256, 128), (1024, 1024), 128), |
| 48 | + ((128, 256), (1024, 1024), 128), |
| 49 | + ((256, 256), (1024, 2048), 256), |
| 50 | + ((256, 128), (1024, 2048), 256), |
| 51 | + ((128, 256), (1024, 2048), 256), |
| 52 | + ((256, 256), (2048, 1024), 512), |
| 53 | + ((256, 128), (2048, 1024), 512), |
| 54 | + ((128, 256), (2048, 1024), 512), |
| 55 | + ] |
| 56 | + ) |
| 57 | + def test_sliding_window_causal_mask_fn(self, block_size, shape, window_size): |
| 58 | + """Test ComputableMask with sliding_window_causal_mask from attention_bias.py.""" |
| 59 | + # Create expected dense sliding window causal mask |
| 60 | + q_len, kv_len = shape |
| 61 | + q_ids = np.arange(q_len)[:, None] |
| 62 | + kv_ids = np.arange(kv_len)[None, :] |
| 63 | + mask_fn = sliding_window_causal_mask(sliding_window_size=window_size) |
| 64 | + dense_mask = mask_fn(q_ids, kv_ids) |
| 65 | + |
| 66 | + # Create ComputableMask with mask_fn |
| 67 | + lazy_mask = ComputableMask(shape=shape, mask_fn=mask_fn) |
| 68 | + |
| 69 | + self._compare_masks(dense_mask, lazy_mask, block_size) |
| 70 | + |
| 71 | + def _compare_masks( |
| 72 | + self, |
| 73 | + dense_mask: np.ndarray, |
| 74 | + lazy_mask: mask_lib.Mask, |
| 75 | + block_size: tuple[int, int], |
| 76 | + ): |
| 77 | + self.assertEqual(dense_mask.shape, lazy_mask.shape) |
| 78 | + |
| 79 | + *prefix, width, height = dense_mask.shape |
| 80 | + |
| 81 | + assert width % block_size[0] == 0 |
| 82 | + assert height % block_size[1] == 0 |
| 83 | + |
| 84 | + full_lazy_mask = lazy_mask[(*[slice(p) for p in prefix], slice(None), slice(None))] |
| 85 | + self.assertNestedEqual(dense_mask, full_lazy_mask) |
| 86 | + for i, j in np.ndindex(width // block_size[0], height // block_size[1]): |
| 87 | + indexer = ( |
| 88 | + *[slice(p) for p in prefix], |
| 89 | + slice(i * block_size[0], (i + 1) * block_size[0]), |
| 90 | + slice(j * block_size[1], (j + 1) * block_size[1]), |
| 91 | + ) |
| 92 | + dense_chunk = dense_mask[indexer] |
| 93 | + lazy_chunk = lazy_mask[indexer] |
| 94 | + self.assertNestedEqual(dense_chunk, lazy_chunk) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + absltest.main() |
0 commit comments