|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +from transformer_lens.ActivationCache import ActivationCache |
| 5 | +from transformer_lens.model_bridge import TransformerBridge |
| 6 | + |
| 7 | + |
| 8 | +class TestActivationCacheCompatibility: |
| 9 | + """Test that ActivationCache works with TransformerBridge.""" |
| 10 | + |
| 11 | + @pytest.fixture |
| 12 | + def bridge_model(self): |
| 13 | + """Create a TransformerBridge model for testing.""" |
| 14 | + return TransformerBridge.boot_transformers("gpt2", device="cpu") |
| 15 | + |
| 16 | + @pytest.fixture |
| 17 | + def sample_cache(self, bridge_model): |
| 18 | + """Create a sample cache for testing.""" |
| 19 | + prompt = "The quick brown fox jumps over the lazy dog." |
| 20 | + output, cache = bridge_model.run_with_cache(prompt) |
| 21 | + return cache |
| 22 | + |
| 23 | + def test_cache_creation(self, bridge_model): |
| 24 | + """Test that caches can be created from TransformerBridge.""" |
| 25 | + prompt = "Test cache creation." |
| 26 | + |
| 27 | + # Test run_with_cache with cache object |
| 28 | + output, cache = bridge_model.run_with_cache(prompt, return_cache_object=True) |
| 29 | + |
| 30 | + assert isinstance(output, torch.Tensor) |
| 31 | + assert isinstance(cache, (dict, ActivationCache)) |
| 32 | + |
| 33 | + # If it's an ActivationCache, test its properties |
| 34 | + if isinstance(cache, ActivationCache): |
| 35 | + assert hasattr(cache, "cache_dict") |
| 36 | + assert hasattr(cache, "model") |
| 37 | + assert len(cache.cache_dict) > 0 |
| 38 | + |
| 39 | + def test_cache_dict_access(self, sample_cache): |
| 40 | + """Test that cache dictionary access works.""" |
| 41 | + # Get cache dict regardless of type |
| 42 | + if hasattr(sample_cache, "cache_dict"): |
| 43 | + cache_dict = sample_cache.cache_dict |
| 44 | + else: |
| 45 | + cache_dict = sample_cache |
| 46 | + |
| 47 | + assert isinstance(cache_dict, dict) |
| 48 | + assert len(cache_dict) > 0 |
| 49 | + |
| 50 | + # All values should be tensors or None |
| 51 | + for key, value in cache_dict.items(): |
| 52 | + if value is not None: |
| 53 | + assert isinstance(value, torch.Tensor), f"Cache value for {key} is not a tensor" |
| 54 | + |
| 55 | + def test_cache_key_patterns(self, sample_cache): |
| 56 | + """Test that cache keys follow expected patterns.""" |
| 57 | + # Get cache dict |
| 58 | + if hasattr(sample_cache, "cache_dict"): |
| 59 | + cache_dict = sample_cache.cache_dict |
| 60 | + else: |
| 61 | + cache_dict = sample_cache |
| 62 | + |
| 63 | + cache_keys = list(cache_dict.keys()) |
| 64 | + |
| 65 | + # Should have some keys |
| 66 | + assert len(cache_keys) > 0 |
| 67 | + |
| 68 | + # Log what patterns we find (for debugging) |
| 69 | + patterns_found = [] |
| 70 | + common_patterns = [ |
| 71 | + "embed", |
| 72 | + "pos_embed", |
| 73 | + "blocks", |
| 74 | + "ln_final", |
| 75 | + "unembed", |
| 76 | + "hook_", |
| 77 | + "attn", |
| 78 | + "mlp", |
| 79 | + "resid", |
| 80 | + ] |
| 81 | + |
| 82 | + for pattern in common_patterns: |
| 83 | + if any(pattern in key for key in cache_keys): |
| 84 | + patterns_found.append(pattern) |
| 85 | + |
| 86 | + print(f"Cache key patterns found: {patterns_found}") |
| 87 | + print(f"Total cache keys: {len(cache_keys)}") |
| 88 | + print(f"Sample keys: {cache_keys[:5]}") |
| 89 | + |
| 90 | + def test_cache_with_names_filter(self, bridge_model): |
| 91 | + """Test that names filtering works with caching.""" |
| 92 | + prompt = "Test names filter." |
| 93 | + |
| 94 | + # Get available hook names |
| 95 | + hook_dict = bridge_model.hook_dict |
| 96 | + if len(hook_dict) == 0: |
| 97 | + pytest.skip("No hooks available for filtering") |
| 98 | + |
| 99 | + # Use first few hook names |
| 100 | + filter_names = list(hook_dict.keys())[:3] |
| 101 | + |
| 102 | + try: |
| 103 | + output, cache = bridge_model.run_with_cache(prompt, names_filter=filter_names) |
| 104 | + |
| 105 | + # Get cache dict |
| 106 | + if hasattr(cache, "cache_dict"): |
| 107 | + cache_dict = cache.cache_dict |
| 108 | + else: |
| 109 | + cache_dict = cache |
| 110 | + |
| 111 | + # Should have some activations |
| 112 | + assert len(cache_dict) > 0 |
| 113 | + |
| 114 | + # Check that we got activations for the filtered names (or their aliases) |
| 115 | + cache_keys = set(cache_dict.keys()) |
| 116 | + filter_set = set(filter_names) |
| 117 | + |
| 118 | + # Should have some overlap (exact match not required due to aliasing) |
| 119 | + overlap = len(cache_keys & filter_set) |
| 120 | + # Allow for aliases by checking partial matches |
| 121 | + partial_matches = sum( |
| 122 | + 1 |
| 123 | + for cache_key in cache_keys |
| 124 | + for filter_name in filter_names |
| 125 | + if filter_name in cache_key or cache_key in filter_name |
| 126 | + ) |
| 127 | + |
| 128 | + assert overlap > 0 or partial_matches > 0, "No filtered activations found in cache" |
| 129 | + |
| 130 | + except Exception as e: |
| 131 | + pytest.skip(f"Names filtering not working: {e}") |
| 132 | + |
| 133 | + def test_cache_iteration(self, sample_cache): |
| 134 | + """Test that cache can be iterated over.""" |
| 135 | + # Get cache dict |
| 136 | + if hasattr(sample_cache, "cache_dict"): |
| 137 | + cache_dict = sample_cache.cache_dict |
| 138 | + else: |
| 139 | + cache_dict = sample_cache |
| 140 | + |
| 141 | + # Test iteration |
| 142 | + keys_from_iter = [] |
| 143 | + for key in cache_dict: |
| 144 | + keys_from_iter.append(key) |
| 145 | + |
| 146 | + keys_from_keys = list(cache_dict.keys()) |
| 147 | + |
| 148 | + assert set(keys_from_iter) == set(keys_from_keys) |
| 149 | + assert len(keys_from_iter) > 0 |
| 150 | + |
| 151 | + def test_cache_getitem(self, sample_cache): |
| 152 | + """Test that cache supports getitem access.""" |
| 153 | + # Get cache dict |
| 154 | + if hasattr(sample_cache, "cache_dict"): |
| 155 | + cache_dict = sample_cache.cache_dict |
| 156 | + else: |
| 157 | + cache_dict = sample_cache |
| 158 | + |
| 159 | + if len(cache_dict) == 0: |
| 160 | + pytest.skip("Empty cache") |
| 161 | + |
| 162 | + # Test accessing items |
| 163 | + for key in list(cache_dict.keys())[:3]: # Test first few |
| 164 | + value = cache_dict[key] |
| 165 | + if value is not None: |
| 166 | + assert isinstance(value, torch.Tensor) |
| 167 | + |
| 168 | + def test_cache_batch_dimension_handling(self, bridge_model): |
| 169 | + """Test that cache handles batch dimensions correctly.""" |
| 170 | + prompts = ["First prompt for batch testing.", "Second prompt for batch testing."] |
| 171 | + |
| 172 | + try: |
| 173 | + # Test with multiple prompts |
| 174 | + output, cache = bridge_model.run_with_cache(prompts) |
| 175 | + |
| 176 | + # Get cache dict |
| 177 | + if hasattr(cache, "cache_dict"): |
| 178 | + cache_dict = cache.cache_dict |
| 179 | + else: |
| 180 | + cache_dict = cache |
| 181 | + |
| 182 | + # Check that cached tensors have correct batch dimension |
| 183 | + for key, value in cache_dict.items(): |
| 184 | + if value is not None and isinstance(value, torch.Tensor): |
| 185 | + assert value.shape[0] == len( |
| 186 | + prompts |
| 187 | + ), f"Tensor {key} has wrong batch size: {value.shape[0]}" |
| 188 | + |
| 189 | + except Exception as e: |
| 190 | + pytest.skip(f"Batch processing not supported: {e}") |
| 191 | + |
| 192 | + def test_cache_device_consistency(self, bridge_model): |
| 193 | + """Test that cached tensors are on the correct device.""" |
| 194 | + prompt = "Test device consistency." |
| 195 | + |
| 196 | + # Test on CPU |
| 197 | + model_cpu = bridge_model.cpu() |
| 198 | + output, cache = model_cpu.run_with_cache(prompt) |
| 199 | + |
| 200 | + # Get cache dict |
| 201 | + if hasattr(cache, "cache_dict"): |
| 202 | + cache_dict = cache.cache_dict |
| 203 | + else: |
| 204 | + cache_dict = cache |
| 205 | + |
| 206 | + # All cached tensors should be on CPU |
| 207 | + for key, value in cache_dict.items(): |
| 208 | + if value is not None and isinstance(value, torch.Tensor): |
| 209 | + assert value.device.type == "cpu", f"Tensor {key} is not on CPU: {value.device}" |
| 210 | + |
| 211 | + def test_cache_memory_efficiency(self, bridge_model): |
| 212 | + """Test that cache doesn't cause memory leaks.""" |
| 213 | + prompt = "Test cache memory efficiency." |
| 214 | + |
| 215 | + # Record initial memory |
| 216 | + if torch.cuda.is_available(): |
| 217 | + torch.cuda.empty_cache() |
| 218 | + initial_memory = torch.cuda.memory_allocated() |
| 219 | + |
| 220 | + # Create and delete multiple caches |
| 221 | + for _ in range(3): |
| 222 | + output, cache = bridge_model.run_with_cache(prompt) |
| 223 | + del output, cache |
| 224 | + |
| 225 | + # Clean up |
| 226 | + import gc |
| 227 | + |
| 228 | + gc.collect() |
| 229 | + if torch.cuda.is_available(): |
| 230 | + torch.cuda.empty_cache() |
| 231 | + final_memory = torch.cuda.memory_allocated() |
| 232 | + |
| 233 | + # Memory shouldn't grow significantly |
| 234 | + memory_growth = final_memory - initial_memory |
| 235 | + assert ( |
| 236 | + memory_growth < 50 * 1024 * 1024 |
| 237 | + ), f"Cache caused memory growth of {memory_growth} bytes" |
| 238 | + |
| 239 | + def test_cache_with_different_inputs(self, bridge_model): |
| 240 | + """Test that cache works with different input types.""" |
| 241 | + # Test with string |
| 242 | + output1, cache1 = bridge_model.run_with_cache("String input test.") |
| 243 | + |
| 244 | + # Test with tokens |
| 245 | + tokens = bridge_model.to_tokens("Token input test.") |
| 246 | + output2, cache2 = bridge_model.run_with_cache(tokens) |
| 247 | + |
| 248 | + # Both should work |
| 249 | + assert isinstance(output1, torch.Tensor) |
| 250 | + assert isinstance(output2, torch.Tensor) |
| 251 | + |
| 252 | + # Get cache dicts |
| 253 | + if hasattr(cache1, "cache_dict"): |
| 254 | + cache_dict1 = cache1.cache_dict |
| 255 | + else: |
| 256 | + cache_dict1 = cache1 |
| 257 | + |
| 258 | + if hasattr(cache2, "cache_dict"): |
| 259 | + cache_dict2 = cache2.cache_dict |
| 260 | + else: |
| 261 | + cache_dict2 = cache2 |
| 262 | + |
| 263 | + # Both should have cached activations |
| 264 | + assert len(cache_dict1) > 0 |
| 265 | + assert len(cache_dict2) > 0 |
| 266 | + |
| 267 | + # Should have similar cache keys |
| 268 | + keys1 = set(cache_dict1.keys()) |
| 269 | + keys2 = set(cache_dict2.keys()) |
| 270 | + |
| 271 | + # At least some overlap in keys |
| 272 | + overlap = len(keys1 & keys2) |
| 273 | + assert overlap > 0, "No common cache keys between string and token inputs" |
| 274 | + |
| 275 | + |
| 276 | +if __name__ == "__main__": |
| 277 | + pytest.main([__file__, "-v", "-s"]) |
0 commit comments