|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +import torch |
| 10 | + |
| 11 | +from executorch.examples.models.llama.source_transformation.attention_sink import ( |
| 12 | + KVCacheWithAttentionSink, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class KVCacheWithAttentionSinkTest(unittest.TestCase): |
| 17 | + |
| 18 | + def _init_cache(self): |
| 19 | + self.kv_cache = KVCacheWithAttentionSink( |
| 20 | + max_batch_size=self.max_batch_size, |
| 21 | + window_size=self.window_size, |
| 22 | + sink_size=self.sink_size, |
| 23 | + n_heads=self.n_heads, |
| 24 | + head_dim=self.head_dim, |
| 25 | + transpose_cache=self.transpose_cache, |
| 26 | + dtype=self.dtype, |
| 27 | + ) |
| 28 | + |
| 29 | + def setUp(self): |
| 30 | + torch.manual_seed(42) |
| 31 | + self.max_batch_size = 1 |
| 32 | + self.window_size = 28 |
| 33 | + self.sink_size = 4 |
| 34 | + self.n_heads = 8 |
| 35 | + self.head_dim = 16 |
| 36 | + self.transpose_cache = False |
| 37 | + self.dtype = torch.float32 |
| 38 | + self._init_cache() |
| 39 | + |
| 40 | + def test_update_empty_cache(self): |
| 41 | + # KV cache is empty, update will fill sink tokens |
| 42 | + input_pos = torch.tensor([0], dtype=torch.int32) |
| 43 | + k = torch.ones((1, 1, 8, 16), dtype=self.dtype) |
| 44 | + v = torch.ones((1, 1, 8, 16), dtype=self.dtype) |
| 45 | + |
| 46 | + k_out, v_out = self.kv_cache.update(input_pos, k, v) |
| 47 | + |
| 48 | + expected_k_out = torch.cat( |
| 49 | + [ |
| 50 | + torch.ones((1, 1, 8, 16), dtype=self.dtype), |
| 51 | + torch.zeros((1, 31, 8, 16), dtype=self.dtype), |
| 52 | + ], |
| 53 | + dim=1, |
| 54 | + ) |
| 55 | + expected_v_out = torch.cat( |
| 56 | + [ |
| 57 | + torch.ones((1, 1, 8, 16), dtype=self.dtype), |
| 58 | + torch.zeros((1, 31, 8, 16), dtype=self.dtype), |
| 59 | + ], |
| 60 | + dim=1, |
| 61 | + ) |
| 62 | + |
| 63 | + torch.testing.assert_close(k_out, expected_k_out) |
| 64 | + torch.testing.assert_close(v_out, expected_v_out) |
| 65 | + |
| 66 | + def test_update_without_shift(self): |
| 67 | + # KV cache has enough spaces for new tokens, no shift |
| 68 | + input_pos = torch.tensor([0], dtype=torch.int32) |
| 69 | + k = torch.ones((1, 5, 8, 16), dtype=self.dtype) |
| 70 | + v = torch.ones((1, 5, 8, 16), dtype=self.dtype) |
| 71 | + |
| 72 | + self.kv_cache.update(input_pos, k, v) |
| 73 | + |
| 74 | + input_pos = torch.tensor([5], dtype=torch.int32) |
| 75 | + k = torch.full((1, 5, 8, 16), 2, dtype=self.dtype) |
| 76 | + v = torch.full((1, 5, 8, 16), 2, dtype=self.dtype) |
| 77 | + |
| 78 | + k_out, v_out = self.kv_cache.update(input_pos, k, v) |
| 79 | + |
| 80 | + expected_k_out = torch.cat( |
| 81 | + [ |
| 82 | + torch.ones((1, 5, 8, 16), dtype=self.dtype), |
| 83 | + torch.full((1, 5, 8, 16), 2, dtype=self.dtype), |
| 84 | + torch.zeros((1, 22, 8, 16), dtype=self.dtype), |
| 85 | + ], |
| 86 | + dim=1, |
| 87 | + ) |
| 88 | + expected_v_out = torch.cat( |
| 89 | + [ |
| 90 | + torch.ones((1, 5, 8, 16), dtype=self.dtype), |
| 91 | + torch.full((1, 5, 8, 16), 2, dtype=self.dtype), |
| 92 | + torch.zeros((1, 22, 8, 16), dtype=self.dtype), |
| 93 | + ], |
| 94 | + dim=1, |
| 95 | + ) |
| 96 | + |
| 97 | + torch.testing.assert_close(k_out, expected_k_out) |
| 98 | + torch.testing.assert_close(v_out, expected_v_out) |
| 99 | + |
| 100 | + def test_update_with_some_shift(self): |
| 101 | + # KV cache has some spaces for new tokens but not all, shift some tokens |
| 102 | + input_pos = torch.tensor([0], dtype=torch.int32) |
| 103 | + k = torch.ones((1, 5, 8, 16), dtype=self.dtype) |
| 104 | + v = torch.ones((1, 5, 8, 16), dtype=self.dtype) |
| 105 | + |
| 106 | + self.kv_cache.update(input_pos, k, v) |
| 107 | + |
| 108 | + input_pos = torch.tensor([5], dtype=torch.int32) |
| 109 | + k = torch.full((1, 5, 8, 16), 2, dtype=self.dtype) |
| 110 | + v = torch.full((1, 5, 8, 16), 2, dtype=self.dtype) |
| 111 | + |
| 112 | + self.kv_cache.update(input_pos, k, v) |
| 113 | + |
| 114 | + input_pos = torch.tensor([10], dtype=torch.int32) |
| 115 | + k = torch.full((1, 24, 8, 16), 3, dtype=self.dtype) |
| 116 | + v = torch.full((1, 24, 8, 16), 3, dtype=self.dtype) |
| 117 | + |
| 118 | + k_out, v_out = self.kv_cache.update(input_pos, k, v) |
| 119 | + |
| 120 | + expected_k_out = torch.cat( |
| 121 | + [ |
| 122 | + torch.ones((1, 4, 8, 16), dtype=self.dtype), |
| 123 | + torch.full((1, 4, 8, 16), 2, dtype=self.dtype), |
| 124 | + torch.full((1, 24, 8, 16), 3, dtype=self.dtype), |
| 125 | + ], |
| 126 | + dim=1, |
| 127 | + ) |
| 128 | + expected_v_out = torch.cat( |
| 129 | + [ |
| 130 | + torch.ones((1, 4, 8, 16), dtype=self.dtype), |
| 131 | + torch.full((1, 4, 8, 16), 2, dtype=self.dtype), |
| 132 | + torch.full((1, 24, 8, 16), 3, dtype=self.dtype), |
| 133 | + ], |
| 134 | + dim=1, |
| 135 | + ) |
| 136 | + |
| 137 | + torch.testing.assert_close(k_out, expected_k_out) |
| 138 | + torch.testing.assert_close(v_out, expected_v_out) |
| 139 | + |
| 140 | + def test_update_with_all_shift(self): |
| 141 | + # KV cache has no spaces for new tokens, shift all tokens |
| 142 | + input_pos = torch.tensor([0], dtype=torch.int32) |
| 143 | + k = torch.ones((1, 5, 8, 16), dtype=self.dtype) |
| 144 | + v = torch.ones((1, 5, 8, 16), dtype=self.dtype) |
| 145 | + |
| 146 | + self.kv_cache.update(input_pos, k, v) |
| 147 | + |
| 148 | + input_pos = torch.tensor([5], dtype=torch.int32) |
| 149 | + k = torch.full((1, 28, 8, 16), 2, dtype=self.dtype) |
| 150 | + v = torch.full((1, 28, 8, 16), 2, dtype=self.dtype) |
| 151 | + |
| 152 | + self.kv_cache.update(input_pos, k, v) |
| 153 | + |
| 154 | + input_pos = torch.tensor([33], dtype=torch.int32) |
| 155 | + k = torch.full((1, 6, 8, 16), 3, dtype=self.dtype) |
| 156 | + v = torch.full((1, 6, 8, 16), 3, dtype=self.dtype) |
| 157 | + |
| 158 | + k_out, v_out = self.kv_cache.update(input_pos, k, v) |
| 159 | + |
| 160 | + expected_k_out = torch.cat( |
| 161 | + [ |
| 162 | + torch.ones((1, 4, 8, 16), dtype=self.dtype), |
| 163 | + torch.full((1, 22, 8, 16), 2, dtype=self.dtype), |
| 164 | + torch.full((1, 6, 8, 16), 3, dtype=self.dtype), |
| 165 | + ], |
| 166 | + dim=1, |
| 167 | + ) |
| 168 | + expected_v_out = torch.cat( |
| 169 | + [ |
| 170 | + torch.ones((1, 4, 8, 16), dtype=self.dtype), |
| 171 | + torch.full((1, 22, 8, 16), 2, dtype=self.dtype), |
| 172 | + torch.full((1, 6, 8, 16), 3, dtype=self.dtype), |
| 173 | + ], |
| 174 | + dim=1, |
| 175 | + ) |
| 176 | + |
| 177 | + torch.testing.assert_close(k_out, expected_k_out) |
| 178 | + torch.testing.assert_close(v_out, expected_v_out) |
0 commit comments