MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / tests / test_prefix_cache.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Tests for PrefixCache. |
| 3 | |
| 4 | Run: OMP_NUM_THREADS=32 python3 -u test_prefix_cache.py |
| 5 | """ |
| 6 | import sys, os |
| 7 | sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 8 | |
| 9 | from mojollama.prefix_cache import PrefixCache |
| 10 | |
| 11 | |
| 12 | def test_basic_put_get(): |
| 13 | """Basic put followed by get with matching prefix.""" |
| 14 | cache = PrefixCache(max_entries=16, prefix_len=4) |
| 15 | tokens_a = (1, 100, 200, 300, 400, 500) |
| 16 | cache.put(tokens_a, "snap_a") |
| 17 | # Full match |
| 18 | assert cache.get((1, 100, 200, 300, 400, 500, 600)) == "snap_a" |
| 19 | # Partial suffix different but prefix matches |
| 20 | assert cache.get((1, 100, 200, 300, 999, 888)) == "snap_a" |
| 21 | print(" [PASS] test_basic_put_get") |
| 22 | |
| 23 | |
| 24 | def test_cache_miss(): |
| 25 | """Cache miss when prefix doesn't match.""" |
| 26 | cache = PrefixCache(max_entries=16, prefix_len=4) |
| 27 | cache.put((10, 20, 30, 40, 50), "snap_x") |
| 28 | assert cache.get((99, 98, 97, 96)) is None |
| 29 | assert cache.get((10, 20, 30, 41)) is None # last token differs |
| 30 | print(" [PASS] test_cache_miss") |
| 31 | |
| 32 | |
| 33 | def test_lru_eviction(): |
| 34 | """LRU eviction when cache is full.""" |
| 35 | cache = PrefixCache(max_entries=3, prefix_len=2) |
| 36 | cache.put((1, 10, 100), "A") |
| 37 | cache.put((2, 20, 200), "B") |
| 38 | cache.put((3, 30, 300), "C") |
| 39 | assert cache.size == 3 |
| 40 | |
| 41 | # Access A to make it MRU |
| 42 | cache.get((1, 10, 42)) |
| 43 | # Add D — should evict B (LRU) |
| 44 | cache.put((4, 40, 400), "D") |
| 45 | assert cache.size == 3 |
| 46 | assert cache.get((2, 20, 42)) is None # B evicted |
| 47 | assert cache.get((1, 10, 42)) == "A" # A still there |
| 48 | assert cache.get((4, 40, 42)) == "D" # D added |
| 49 | |
| 50 | # Exhaustive: fill and evict one by one |
| 51 | cache2 = PrefixCache(max_entries=2, prefix_len=2) |
| 52 | cache2.put((5, 50, 500), "E") |
| 53 | cache2.put((6, 60, 600), "F") |
| 54 | cache2.put((7, 70, 700), "G") # evicts E |
| 55 | assert cache2.size == 2 |
| 56 | assert cache2.get((5, 50, 42)) is None # E evicted |
| 57 | assert cache2.get((7, 70, 42)) == "G" |
| 58 | print(" [PASS] test_lru_eviction") |
| 59 | |
| 60 | |
| 61 | def test_partial_prefix_match(): |
| 62 | """Partial prefix matching — request shorter than prefix_len.""" |
| 63 | cache = PrefixCache(max_entries=16, prefix_len=8) |
| 64 | # Store an entry with 12 tokens (prefix_len=8) |
| 65 | long_tokens = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) |
| 66 | cache.put(long_tokens, "snap_long") |
| 67 | |
| 68 | # A shorter sequence whose first N tokens match |
| 69 | # (where N < prefix_len) should still match |
| 70 | short_tokens = (1, 2, 3, 4) # only 4 tokens |
| 71 | # The cache should match as long as the available tokens are a prefix |
| 72 | match = cache.get(short_tokens) |
| 73 | assert match == "snap_long", f"Expected snap_long, got {match}" |
| 74 | print(" [PASS] test_partial_prefix_match") |
| 75 | |
| 76 | |
| 77 | def test_multiple_prefix_lengths(): |
| 78 | """Multiple cache entries with different prefix lengths.""" |
| 79 | cache = PrefixCache(max_entries=16, prefix_len=8) |
| 80 | # Entry A: first 8 tokens = 1-8 |
| 81 | cache.put((1, 2, 3, 4, 5, 6, 7, 8, 100, 200), "A") |
| 82 | # Entry B: first 8 tokens = 10-18 |
| 83 | cache.put((10, 11, 12, 13, 14, 15, 16, 17, 300), "B") |
| 84 | # Entry C: same first 8 tokens as A but different continuation |
| 85 | cache.put((1, 2, 3, 4, 5, 6, 7, 8, 999, 888), "C") |
| 86 | |
| 87 | # A and C share same 8-token prefix → get should return one of them |
| 88 | # (the most recently accessed) |
| 89 | match = cache.get((1, 2, 3, 4, 5, 6, 7, 8, 42, 43)) |
| 90 | assert match is not None, "Should match either A or C" |
| 91 | |
| 92 | # B should match its own prefix |
| 93 | match_b = cache.get((10, 11, 12, 13, 14, 15, 16, 17, 55)) |
| 94 | assert match_b == "B" |
| 95 | |
| 96 | # Non-matching prefix |
| 97 | assert cache.get((99, 98, 97, 96, 95, 94, 93, 92)) is None |
| 98 | print(" [PASS] test_multiple_prefix_lengths") |
| 99 | |
| 100 | |
| 101 | def test_invalidate(): |
| 102 | """invalidate removes specific entry.""" |
| 103 | cache = PrefixCache(max_entries=16, prefix_len=4) |
| 104 | cache.put((1, 2, 3, 4, 77), "X") |
| 105 | cache.put((5, 6, 7, 8, 88), "Y") |
| 106 | assert cache.get((1, 2, 3, 4, 99)) == "X" |
| 107 | |
| 108 | # Invalidate X |
| 109 | assert cache.invalidate((1, 2, 3, 4, 77)) is True |
| 110 | assert cache.get((1, 2, 3, 4, 99)) is None # X gone |
| 111 | assert cache.get((5, 6, 7, 8, 55)) == "Y" # Y still there |
| 112 | print(" [PASS] test_invalidate") |
| 113 | |
| 114 | |
| 115 | def test_evict_method(): |
| 116 | """Evict explicitly removes the LRU entry.""" |
| 117 | cache = PrefixCache(max_entries=3, prefix_len=2) |
| 118 | cache.put((1, 10), "A") |
| 119 | cache.put((2, 20), "B") |
| 120 | cache.put((3, 30), "C") |
| 121 | # Access B to make it MRU, A is LRU |
| 122 | cache.get((2, 20)) |
| 123 | assert cache.evict() == 1 # evicts A |
| 124 | assert cache.get((1, 10, 42)) is None |
| 125 | assert cache.size == 2 |
| 126 | print(" [PASS] test_evict_method") |
| 127 | |
| 128 | |
| 129 | def test_clear(): |
| 130 | """Clear removes everything.""" |
| 131 | cache = PrefixCache(max_entries=16, prefix_len=4) |
| 132 | cache.put((1, 2, 3, 4), "A") |
| 133 | cache.put((5, 6, 7, 8), "B") |
| 134 | assert cache.size == 2 |
| 135 | cache.clear() |
| 136 | assert cache.size == 0 |
| 137 | assert cache.get((1, 2, 3, 4, 99)) is None |
| 138 | print(" [PASS] test_clear") |
| 139 | |
| 140 | |
| 141 | def test_empty_cache(): |
| 142 | """Get on empty cache returns None.""" |
| 143 | cache = PrefixCache() |
| 144 | assert cache.get((1, 2, 3)) is None |
| 145 | assert cache.get(()) is None |
| 146 | assert cache.evict() == 0 |
| 147 | print(" [PASS] test_empty_cache") |
| 148 | |
| 149 | |
| 150 | def test_update_same_prefix(): |
| 151 | """Put with same prefix updates the stored snapshot.""" |
| 152 | cache = PrefixCache(max_entries=4, prefix_len=4) |
| 153 | cache.put((1, 2, 3, 4, 100), "old") |
| 154 | # Update with same prefix but new value |
| 155 | cache.put((1, 2, 3, 4, 200), "new") |
| 156 | assert cache.get((1, 2, 3, 4, 999)) == "new" |
| 157 | assert cache.size == 1 # no duplicate |
| 158 | print(" [PASS] test_update_same_prefix") |
| 159 | |
| 160 | |
| 161 | if __name__ == "__main__": |
| 162 | print("PrefixCache tests:") |
| 163 | for name, fn in sorted(globals().items()): |
| 164 | if name.startswith("test_"): |
| 165 | fn() |
| 166 | print("\nAll PrefixCache tests PASSED") |