From 739e8bfe607bc4940c837e16b026792c107188f6 Mon Sep 17 00:00:00 2001 From: degenfabian Date: Mon, 18 Aug 2025 17:17:01 +0200 Subject: [PATCH 1/5] updated loading in Activation Patching in TL Demo to use transformer bridge --- .github/workflows/checks.yml | 2 +- demos/Activation_Patching_in_TL_Demo.ipynb | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 4de51026c..5a96090a5 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -144,7 +144,7 @@ jobs: strategy: matrix: notebook: - # - "Activation_Patching_in_TL_Demo" + - "Activation_Patching_in_TL_Demo" # - "Attribution_Patching_Demo" - "ARENA_Content" - "Colab_Compatibility" diff --git a/demos/Activation_Patching_in_TL_Demo.ipynb b/demos/Activation_Patching_in_TL_Demo.ipynb index 3be728cb1..0650e0284 100644 --- a/demos/Activation_Patching_in_TL_Demo.ipynb +++ b/demos/Activation_Patching_in_TL_Demo.ipynb @@ -121,17 +121,13 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import transformer_lens\n", "import transformer_lens.utils as utils\n", - "from transformer_lens.hook_points import (\n", - " HookedRootModule,\n", - " HookPoint,\n", - ") # Hooking utilities\n", - "from transformer_lens import HookedTransformer, HookedTransformerConfig, FactoredMatrix, ActivationCache" + "from transformer_lens.model_bridge import TransformerBridge" ] }, { @@ -196,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -215,7 +211,8 @@ } ], "source": [ - "model = HookedTransformer.from_pretrained(\"gpt2-small\")" + "model = TransformerBridge.boot_transformers(\"gpt2\")\n", + "model.enable_compatibility_mode()" ] }, { @@ -943,7 +940,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -955,7 +952,8 @@ } ], "source": [ - "attn_only = HookedTransformer.from_pretrained(\"attn-only-2l\")\n", + "attn_only = TransformerBridge.boot_transformers(\"attn-only-2l\")\n", + "attn_only.enable_compatibility_mode()\n", "batch = 4\n", "seq_len = 20\n", "rand_tokens_A = torch.randint(100, 10000, (batch, seq_len)).to(attn_only.cfg.device)\n", From add5738a3f120c2697d36f132d611e1b6258e74f Mon Sep 17 00:00:00 2001 From: degenfabian Date: Tue, 19 Aug 2025 18:28:49 +0200 Subject: [PATCH 2/5] use undeprecated ipython code to avoid deprecation warnings --- demos/Activation_Patching_in_TL_Demo.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/demos/Activation_Patching_in_TL_Demo.ipynb b/demos/Activation_Patching_in_TL_Demo.ipynb index 0650e0284..52850ede8 100644 --- a/demos/Activation_Patching_in_TL_Demo.ipynb +++ b/demos/Activation_Patching_in_TL_Demo.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -67,9 +67,9 @@ " from IPython import get_ipython\n", "\n", " ipython = get_ipython()\n", - " # Code to automatically update the HookedTransformer code as its edited without restarting the kernel\n", - " ipython.magic(\"load_ext autoreload\")\n", - " ipython.magic(\"autoreload 2\")" + " # Code to automatically update the TransformerBridge code as its edited without restarting the kernel\n", + " ipython.run_line_magic(\"load_ext\", \"autoreload\")\n", + " ipython.run_line_magic(\"autoreload\", \"2\")" ] }, { From bacace717a35ef1df925a501ad48ece232214632 Mon Sep 17 00:00:00 2001 From: degenfabian Date: Tue, 19 Aug 2025 19:11:21 +0200 Subject: [PATCH 3/5] Adapt device selection to mirror HookedTransformer --- .../model_bridge/sources/transformers.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/transformer_lens/model_bridge/sources/transformers.py b/transformer_lens/model_bridge/sources/transformers.py index 1c738fa5a..2cc785b53 100644 --- a/transformer_lens/model_bridge/sources/transformers.py +++ b/transformer_lens/model_bridge/sources/transformers.py @@ -16,7 +16,7 @@ ) from transformer_lens.model_bridge.bridge import TransformerBridge -from transformer_lens.utils import get_tokenizer_with_bos +from transformer_lens.utils import get_device, get_tokenizer_with_bos def map_default_transformer_lens_config(hf_config): @@ -122,9 +122,12 @@ def boot( adapter = ArchitectureAdapterFactory.select_architecture_adapter(tl_config) + # No device specified by user, use the best available device for the current system + if device == None: + device = get_device() + # Add device information to the config - if device is not None: - adapter.cfg.device = device + adapter.cfg.device = device # Load the model from HuggingFace using the original config hf_model = AutoModelForCausalLM.from_pretrained( @@ -133,9 +136,8 @@ def boot( torch_dtype=dtype, ) - # Move model to device if specified - if device is not None: - hf_model = hf_model.to(device) + # Move model to device + hf_model = hf_model.to(device) # Load the tokenizer tokenizer = tokenizer From edca1ffbf57b6d88633cc4941704d582043730fb Mon Sep 17 00:00:00 2001 From: Fabian Degen <106864199+degenfabian@users.noreply.github.com> Date: Wed, 20 Aug 2025 15:46:26 +0200 Subject: [PATCH 4/5] revert metadata changes --- demos/Activation_Patching_in_TL_Demo.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/demos/Activation_Patching_in_TL_Demo.ipynb b/demos/Activation_Patching_in_TL_Demo.ipynb index 52850ede8..e9292ae19 100644 --- a/demos/Activation_Patching_in_TL_Demo.ipynb +++ b/demos/Activation_Patching_in_TL_Demo.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -121,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -192,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -940,7 +940,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [ { From 55f85eded5853242f9bd8230e66428020c584213 Mon Sep 17 00:00:00 2001 From: Bryce Meyer Date: Thu, 16 Oct 2025 16:21:29 +0200 Subject: [PATCH 5/5] updated installation source --- demos/Activation_Patching_in_TL_Demo.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/Activation_Patching_in_TL_Demo.ipynb b/demos/Activation_Patching_in_TL_Demo.ipynb index e9292ae19..1e57298c1 100644 --- a/demos/Activation_Patching_in_TL_Demo.ipynb +++ b/demos/Activation_Patching_in_TL_Demo.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -58,7 +58,7 @@ " import google.colab\n", " IN_COLAB = True\n", " print(\"Running as a Colab notebook\")\n", - " %pip install git+https://github.com/TransformerLensOrg/TransformerLens.git\n", + " %pip install transformer_lens\n", " # Install my janky personal plotting utils\n", " %pip install git+https://github.com/neelnanda-io/neel-plotly.git\n", "except:\n",