|
| 1 | +import unittest |
| 2 | + |
| 3 | +import jax |
| 4 | +import jax.numpy as jnp |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from tpu_inference.distributed.cache_util import jitted_insert_kv_cache_slices |
| 8 | + |
| 9 | + |
| 10 | +def original_jitted_insert_kv_cache_slices( |
| 11 | + block_size, |
| 12 | + kv_caches: list[jax.Array], |
| 13 | + kv_cache_slices: list[jax.Array], |
| 14 | + block_numbers: jax.Array, |
| 15 | +) -> list[jax.Array]: |
| 16 | + """ |
| 17 | + This is the original implementation that expects concatenated slices. |
| 18 | + It reshapes the single slice per layer into multiple blocks. |
| 19 | + """ |
| 20 | + |
| 21 | + def _update_layer(cache, slices): |
| 22 | + """The function to apply to each layer's cache and slices.""" |
| 23 | + # Original method reshapes a large slice into blocks |
| 24 | + num_blocks = len(block_numbers) |
| 25 | + reshaped_slices = slices.reshape( |
| 26 | + (num_blocks, 1, block_size, *slices.shape[1:])) |
| 27 | + for i, block_idx in enumerate(block_numbers): |
| 28 | + cache = jax.lax.dynamic_update_slice_in_dim(cache, |
| 29 | + reshaped_slices[i], |
| 30 | + block_idx, |
| 31 | + axis=0) |
| 32 | + return cache |
| 33 | + |
| 34 | + return jax.tree.map(_update_layer, kv_caches, kv_cache_slices) |
| 35 | + |
| 36 | + |
| 37 | +class TestCacheInsertion(unittest.TestCase): |
| 38 | + |
| 39 | + def setUp(self): |
| 40 | + """Set up common parameters for the tests.""" |
| 41 | + self.num_layers = 2 |
| 42 | + self.num_blocks_total = 32 |
| 43 | + self.block_size = 16 |
| 44 | + self.num_kv_heads = 4 |
| 45 | + self.head_dim = 128 |
| 46 | + |
| 47 | + # We will load 3 new blocks |
| 48 | + self.num_blocks_to_load = 3 |
| 49 | + |
| 50 | + # Shape for a single block in the main KV cache |
| 51 | + self.kv_cache_shape = ( |
| 52 | + self.num_blocks_total, |
| 53 | + self.block_size, |
| 54 | + self.num_kv_heads, |
| 55 | + 2, |
| 56 | + self.head_dim, |
| 57 | + ) |
| 58 | + |
| 59 | + # Shape for one chunk/slice of tokens to be inserted |
| 60 | + self.slice_shape = ( |
| 61 | + self.block_size, |
| 62 | + self.num_kv_heads, |
| 63 | + 2, |
| 64 | + self.head_dim, |
| 65 | + ) |
| 66 | + |
| 67 | + # Destination block indices in the main KV cache |
| 68 | + self.dst_blocks = jnp.array([5, 12, 21]) |
| 69 | + |
| 70 | + # --- Test Data --- |
| 71 | + |
| 72 | + # 1. Initial (empty) KV caches for all layers |
| 73 | + key = jax.random.PRNGKey(0) |
| 74 | + self.initial_kv_caches1 = [ |
| 75 | + jnp.zeros(self.kv_cache_shape, dtype=jnp.float32) |
| 76 | + for _ in range(self.num_layers) |
| 77 | + ] |
| 78 | + |
| 79 | + self.initial_kv_caches2 = [ |
| 80 | + jnp.zeros(self.kv_cache_shape, dtype=jnp.float32) |
| 81 | + for _ in range(self.num_layers) |
| 82 | + ] |
| 83 | + |
| 84 | + # 2. The raw, chunked KV data (input for the new method) |
| 85 | + # This is a list of lists: List[layer -> List[chunk]] |
| 86 | + self.raw_chunked_kv = [] |
| 87 | + for _ in range(self.num_layers): |
| 88 | + key, subkey = jax.random.split(key) |
| 89 | + layer_chunks = [ |
| 90 | + jax.random.normal(subkey, self.slice_shape) |
| 91 | + for _ in range(self.num_blocks_to_load) |
| 92 | + ] |
| 93 | + self.raw_chunked_kv.append(layer_chunks) |
| 94 | + |
| 95 | + # 3. The concatenated KV data (input for the original method) |
| 96 | + # This is a list of arrays: List[layer -> concatenated_array] |
| 97 | + self.concatenated_kv = [ |
| 98 | + jax.lax.concatenate(layer_chunks, dimension=0) |
| 99 | + for layer_chunks in self.raw_chunked_kv |
| 100 | + ] |
| 101 | + |
| 102 | + def test_jitted_insert_kv_cache_slices_equivalence(self): |
| 103 | + """ |
| 104 | + Verify that the new and original methods for inserting KV cache slices |
| 105 | + produce identical results. |
| 106 | + """ |
| 107 | + # --- Approach 1: Original Method --- |
| 108 | + # This method takes concatenated slices. |
| 109 | + original_output = original_jitted_insert_kv_cache_slices( |
| 110 | + self.block_size, self.initial_kv_caches1, self.concatenated_kv, |
| 111 | + self.dst_blocks) |
| 112 | + |
| 113 | + # --- Approach 2: New Method --- |
| 114 | + # This method takes a list of chunked slices. |
| 115 | + new_output = jitted_insert_kv_cache_slices(self.block_size, |
| 116 | + self.initial_kv_caches2, |
| 117 | + self.raw_chunked_kv, |
| 118 | + self.dst_blocks) |
| 119 | + |
| 120 | + # --- Verification --- |
| 121 | + # Check that the outputs for each layer are identical. |
| 122 | + for i in range(self.num_layers): |
| 123 | + np.testing.assert_array_equal(np.array(original_output[i]), |
| 124 | + np.array(new_output[i])) |
| 125 | + print("\nTest passed: Both methods produce identical KV caches.") |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + unittest.main() |
0 commit comments