11import gc
2+ from contextlib import contextmanager
23from functools import partial
34
45import numpy as np
@@ -187,6 +188,18 @@ def verify_intervention(
187188 verify_intervention (expected_effects , layer , pos , feature_idx , new_activation )
188189
189190
191+ @contextmanager
192+ def patch_tokenizer_special_ids (model : TransformerLensReplacementModel , special_ids : list [int ]):
193+ assert model .tokenizer is not None
194+ tokenizer_class = type (model .tokenizer )
195+ original_all_special_ids = tokenizer_class .all_special_ids # type: ignore
196+ try :
197+ tokenizer_class .all_special_ids = property (lambda self : special_ids ) # type: ignore
198+ yield
199+ finally :
200+ tokenizer_class .all_special_ids = original_all_special_ids # type: ignore
201+
202+
190203def load_dummy_gemma_model (cfg : HookedTransformerConfig ) -> TransformerLensReplacementModel :
191204 transcoders = {
192205 layer_idx : SingleLayerTranscoder (
@@ -206,8 +219,6 @@ def load_dummy_gemma_model(cfg: HookedTransformerConfig) -> TransformerLensRepla
206219 model = ReplacementModel .from_config (cfg , transcoder_set )
207220 assert isinstance (model , TransformerLensReplacementModel )
208221
209- type(model .tokenizer ).all_special_ids = property (lambda self : [0 ]) # type: ignore
210-
211222 for _ , param in model .named_parameters ():
212223 nn .init .uniform_ (param , a = - 1 , b = 1 )
213224
@@ -291,10 +302,12 @@ def test_small_gemma_model():
291302 }
292303 cfg = HookedTransformerConfig .from_dict (gemma_small_cfg )
293304 model = load_dummy_gemma_model (cfg )
294- graph = attribute (s , model )
295305
296- verify_token_and_error_edges (model , graph )
297- verify_feature_edges (model , graph )
306+ with patch_tokenizer_special_ids (model , [0 ]):
307+ graph = attribute (s , model )
308+
309+ verify_token_and_error_edges (model , graph )
310+ verify_feature_edges (model , graph )
298311
299312
300313def test_large_gemma_model ():
@@ -386,10 +399,12 @@ def test_large_gemma_model():
386399 }
387400 cfg = HookedTransformerConfig .from_dict (gemma_large_cfg )
388401 model = load_dummy_gemma_model (cfg )
389- graph = attribute (s , model )
390402
391- verify_token_and_error_edges (model , graph )
392- verify_feature_edges (model , graph )
403+ with patch_tokenizer_special_ids (model , [0 ]):
404+ graph = attribute (s , model )
405+
406+ verify_token_and_error_edges (model , graph )
407+ verify_feature_edges (model , graph )
393408
394409
395410@pytest .mark .skipif (not torch .cuda .is_available (), reason = "CUDA not available" )
0 commit comments