Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
01996df
added macOS default directory files to gitignore
bryce13950 Mar 30, 2023
1916857
organized tutorials into their own directory
bryce13950 Mar 30, 2023
403c6d8
moved to demos directory
bryce13950 Apr 1, 2023
4adde14
normalized names
bryce13950 Apr 1, 2023
e35814a
added demo placeholder
bryce13950 Apr 1, 2023
4f383ff
Merge branch 'main' into refactor-tutorials-location
bryce13950 Apr 3, 2023
e841bbb
added documentation test
bryce13950 Apr 9, 2023
92dc6a0
updated lock file
bryce13950 Apr 9, 2023
7c21271
added documentation action runner
bryce13950 Apr 9, 2023
cc93f38
updated check for colab to include github
bryce13950 Apr 11, 2023
34a77f8
added testing library
bryce13950 Apr 11, 2023
0a937da
moved demo mode back
bryce13950 Apr 11, 2023
6243177
checked value more explicitly
bryce13950 Apr 11, 2023
092c095
updated activation to use new colab and github check
bryce13950 Apr 11, 2023
c8364eb
set do slow runs
bryce13950 Apr 11, 2023
52f238e
updated headers of ipynb files
bryce13950 Apr 13, 2023
3f5a800
added development mode flag
bryce13950 Apr 13, 2023
87f81ce
added development mode flag
bryce13950 Apr 13, 2023
29cd7f7
resolved some GPU issues
bryce13950 Apr 16, 2023
c6db89b
added do slow runs flag to image renders
bryce13950 Apr 17, 2023
6fa18b8
removed debug flag
bryce13950 Apr 17, 2023
038f89d
added slow runs flag to renders
bryce13950 Apr 17, 2023
cefc78f
added flag to demo for whether in github
bryce13950 Apr 17, 2023
eef97b6
added constant to exploratory demo
bryce13950 Apr 17, 2023
cb0fc6e
added another function call to slow runs check
bryce13950 Apr 18, 2023
befcaf2
removed slow runs check
bryce13950 Apr 18, 2023
6c1c5b0
added long tensor if not in github
bryce13950 Apr 18, 2023
60baca6
set device for config
bryce13950 Apr 19, 2023
3ba983b
installed gradio
bryce13950 Apr 19, 2023
ea61b1e
installed matplotlib
bryce13950 Apr 19, 2023
06aa3b1
added github check to cuda function
bryce13950 Apr 19, 2023
ca2159f
added cuda function check
bryce13950 Apr 19, 2023
de34d94
added check if we are running slow runs
bryce13950 Apr 19, 2023
98c3a9b
set train model to false if in github
bryce13950 Apr 20, 2023
4d65cee
added check for tensor value
bryce13950 Apr 20, 2023
f9b7c17
updated tensor dimensions
bryce13950 Apr 20, 2023
04815d3
fixed logic
bryce13950 Apr 20, 2023
80b0f86
skipped graph if not doing slow runs
bryce13950 Apr 22, 2023
c589a09
updated pth to be local
bryce13950 Apr 22, 2023
bfb565a
changed function call
bryce13950 Apr 22, 2023
8568593
added epochs variable
bryce13950 Apr 22, 2023
a15c3eb
changed tracr to use official one
bryce13950 Apr 22, 2023
36bf38f
added separate group for documentation checking
bryce13950 Apr 22, 2023
8833bbd
fixed name
bryce13950 Apr 22, 2023
6f8133b
removed documentation tests from normal run
bryce13950 Apr 22, 2023
1e2e8e6
updtaed config to run on python 3.9
bryce13950 Apr 24, 2023
45b91fe
Revert "skipped graph if not doing slow runs"
bryce13950 Apr 24, 2023
e712dce
removed doc check again
bryce13950 Apr 24, 2023
2c22e3f
added do slow runs again
bryce13950 Apr 25, 2023
f5bb785
added plotting library
bryce13950 Apr 25, 2023
14e79c2
changed function call to Tensor
bryce13950 Apr 25, 2023
02d2016
checked that key exists
bryce13950 Apr 25, 2023
e347171
added check for slow runs to another point
bryce13950 Apr 25, 2023
77b6c8c
added do slow runs to last scatter
bryce13950 Apr 26, 2023
ffcc1b0
changed function call to use numeric variable
bryce13950 Apr 26, 2023
3559ee0
changed name
bryce13950 Apr 26, 2023
83136ee
fixed spacing
bryce13950 Apr 26, 2023
56afe28
removed other two scatters
bryce13950 Apr 26, 2023
5a67a5d
updated cuda calls
bryce13950 Apr 26, 2023
1c6199e
skipped scatter again if not running slow runs
bryce13950 Apr 26, 2023
6eb10da
updated cuda call and removed key check
bryce13950 Apr 26, 2023
ec68829
Make nbmake test pass for no_position_experiment
rusheb Apr 26, 2023
e5c84f8
replace conditional on IN_GITHUB with .to(TORCH_DEVICE)
rusheb Apr 26, 2023
ef148d4
added cuda checks for remaining calls
bryce13950 Apr 27, 2023
863d4e3
added github check again
bryce13950 Apr 27, 2023
f4b8adf
added torch device constant to attribution patching
bryce13950 Apr 27, 2023
d1711f8
updated import
bryce13950 Apr 27, 2023
d66217d
skipped end of attribution patching
bryce13950 Apr 27, 2023
617e41e
added model back
bryce13950 Apr 27, 2023
f500041
fixed import and skipped metrics in github
bryce13950 Apr 28, 2023
3504a74
added slow runs check
bryce13950 Apr 28, 2023
f8543d8
added training
bryce13950 Apr 28, 2023
1695969
removed download for model
bryce13950 Apr 28, 2023
01b800f
tried loading model without processing
bryce13950 Apr 28, 2023
41c4e9f
added device param
bryce13950 Apr 28, 2023
0757b5d
skiupped plotting
bryce13950 May 1, 2023
c2f86d7
changed gpt szie
bryce13950 May 1, 2023
ac94ef4
updated fig setting
bryce13950 May 1, 2023
6377455
removed github flag
bryce13950 May 1, 2023
04dbce3
added last fig check to be skipped if not in github
bryce13950 May 1, 2023
04488a1
added not github check to fig line
bryce13950 May 1, 2023
e673172
added last fig to github checks
bryce13950 May 1, 2023
f781700
skipped print if in github
bryce13950 May 1, 2023
dd81d17
Merge branch 'main' into tests-regression
bryce13950 May 2, 2023
d7c0b72
patched new demos for new format
bryce13950 May 2, 2023
36451fe
added starter demo
bryce13950 May 2, 2023
8ab7eb7
removed old demo
bryce13950 May 2, 2023
19ee855
set device properly for github
bryce13950 May 2, 2023
570e5ae
added print
bryce13950 May 2, 2023
dcd4c7e
removed last device call
bryce13950 May 2, 2023
f8661cd
added safe variable setting again
bryce13950 May 3, 2023
93f1099
added not in github flag to attention multi flag
bryce13950 May 3, 2023
8e783e3
added sentencepiece install
bryce13950 May 3, 2023
267c834
set path to current directory
bryce13950 May 3, 2023
8a7e21d
loaded models in github
bryce13950 May 4, 2023
1b6210b
added do slow runs flag to image render
bryce13950 May 4, 2023
bcd22a6
Merge branch 'main' into tests-regression
bryce13950 Jul 1, 2023
d0410ed
added line from merge
bryce13950 Jul 1, 2023
fc08ec5
fixed spacing
bryce13950 Jul 1, 2023
82676d7
added install to doc check script
bryce13950 Jul 1, 2023
d51aa4c
added nbmake
bryce13950 Jul 1, 2023
ae4f4ee
Merge branch 'main' into tests-regression
bryce13950 Jul 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ permissions:

jobs:
checks:
name: Checks
name: Code Checks
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -67,3 +67,27 @@ jobs:
# run: poetry run mypy transformer_lens
- name: Build check
run: poetry build
docs:
name: Documentation Checks
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- "3.9"
steps:
- uses: actions/checkout@v3
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Install dependencies
run: |
poetry lock --check
poetry install --with dev
- name: Documentation test
run: make documentation-test
19 changes: 11 additions & 8 deletions demos/Activation_Patching_in_TL_Demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,27 @@
}
],
"source": [
"# Janky code to do different setup when run in a Colab notebook vs VSCode\n",
"import os\n",
"\n",
"IN_COLAB = 'google.colab' in str(get_ipython())\n",
"IN_GITHUB = os.getenv(\"GITHUB_ACTIONS\") == \"true\"\n",
"DEBUG_MODE = False\n",
"try:\n",
" import google.colab\n",
" IN_COLAB = True\n",
"DO_SLOW_RUNS = not IN_GITHUB\n",
"\n",
"if IN_COLAB or IN_GITHUB:\n",
" print(\"Running as a Colab notebook\")\n",
" %pip install git+https://github.com/neelnanda-io/TransformerLens.git\n",
" # Install my janky personal plotting utils\n",
" %pip install git+https://github.com/neelnanda-io/neel-plotly.git\n",
"except:\n",
" IN_COLAB = False\n",
"else:\n",
" print(\"Running as a Jupyter notebook - intended for development only!\")\n",
" 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\")"
" ipython.magic(\"autoreload 2\")\n",
" "
]
},
{
Expand Down Expand Up @@ -319,7 +322,7 @@
"outputs": [],
"source": [
"# Whether to do the runs by head and by position, which are much slower\n",
"DO_SLOW_RUNS = True"
"# DO_SLOW_RUNS = False"
]
},
{
Expand Down
3,005 changes: 3,004 additions & 1 deletion demos/Attribution_Patching_Demo.ipynb

Large diffs are not rendered by default.

57 changes: 38 additions & 19 deletions demos/Exploratory_Analysis_Demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@
}
],
"source": [
"# Janky code to do different setup when run in a Colab notebook vs VSCode\n",
"import os\n",
"\n",
"IN_COLAB = 'google.colab' in str(get_ipython())\n",
"IN_GITHUB = os.getenv(\"GITHUB_ACTIONS\") == \"true\"\n",
"DEBUG_MODE = False\n",
"try:\n",
" import google.colab\n",
" IN_COLAB = True\n",
"DO_SLOW_RUNS = not IN_GITHUB\n",
"\n",
"if IN_COLAB or IN_GITHUB:\n",
" print(\"Running as a Colab notebook\")\n",
" %pip install git+https://github.com/neelnanda-io/TransformerLens.git\n",
" # Install another version of node that makes PySvelte work way faster\n",
" !curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -; sudo apt-get install -y nodejs\n",
" %pip install git+https://github.com/neelnanda-io/PySvelte.git\n",
"except:\n",
" IN_COLAB = False\n",
"else:\n",
" print(\"Running as a Jupyter notebook - intended for development only!\")\n",
" from IPython import get_ipython\n",
"\n",
Expand Down Expand Up @@ -188,6 +190,17 @@
"torch.set_grad_enabled(False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This variable needs to be used instead of allowing the default\n",
"# Any calls to .cuda() need to be .to(device) to allow for your notebook to be compatible with github CI\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\""
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -336,6 +349,7 @@
" center_writing_weights=True,\n",
" fold_ln=True,\n",
" refactor_factored_attn_matrices=True,\n",
" device=device\n",
")"
]
},
Expand Down Expand Up @@ -478,7 +492,8 @@
" )\n",
" # Insert the *incorrect* answer to the prompt, making the correct answer the indirect object.\n",
" prompts.append(prompt_format[i].format(answers[-1][1]))\n",
"answer_tokens = torch.tensor(answer_tokens).cuda()\n",
"answer_tokens = answer_tokens.cuda() if not IN_GITHUB else torch.LongTensor(answer_tokens)\n",
"\n",
"print(prompts)\n",
"print(answers)"
]
Expand Down Expand Up @@ -518,7 +533,7 @@
"source": [
"tokens = model.to_tokens(prompts, prepend_bos=True)\n",
"# Move the tokens to the GPU\n",
"tokens = tokens.cuda()\n",
"tokens = tokens.cuda() if not IN_GITHUB else torch.LongTensor(tokens)\n",
"# Run the model and cache all activations\n",
"original_logits, cache = model.run_with_cache(tokens)"
]
Expand Down Expand Up @@ -1004,6 +1019,10 @@
" local_cache: Optional[ActivationCache]=None, \n",
" local_tokens: Optional[torch.Tensor]=None, \n",
" title: str=\"\"):\n",
" \n",
" if IN_GITHUB:\n",
" return\n",
" \n",
" # Heads are given as a list of integers or a single integer in [0, n_layers * n_heads)\n",
" if isinstance(heads, int):\n",
" heads = [heads]\n",
Expand Down Expand Up @@ -1368,7 +1387,7 @@
" # 0 means zero change, negative means actively made worse, 1 means totally recovered clean performance, >1 means actively *improved* on clean performance\n",
" return (patched_logit_diff - corrupted_average_logit_diff)/(original_average_logit_diff - corrupted_average_logit_diff)\n",
"\n",
"patched_residual_stream_diff = torch.zeros(model.cfg.n_layers, tokens.shape[1], device=\"cuda\", dtype=torch.float32)\n",
"patched_residual_stream_diff = torch.zeros(model.cfg.n_layers, tokens.shape[1], device=device, dtype=torch.float32)\n",
"for layer in range(model.cfg.n_layers):\n",
" for position in range(tokens.shape[1]):\n",
" hook_fn = partial(patch_residual_component, pos=position, clean_cache=cache)\n",
Expand Down Expand Up @@ -1465,8 +1484,8 @@
"metadata": {},
"outputs": [],
"source": [
"patched_attn_diff = torch.zeros(model.cfg.n_layers, tokens.shape[1], device=\"cuda\", dtype=torch.float32)\n",
"patched_mlp_diff = torch.zeros(model.cfg.n_layers, tokens.shape[1], device=\"cuda\", dtype=torch.float32)\n",
"patched_attn_diff = torch.zeros(model.cfg.n_layers, tokens.shape[1], device=device, dtype=torch.float32)\n",
"patched_mlp_diff = torch.zeros(model.cfg.n_layers, tokens.shape[1], device=device, dtype=torch.float32)\n",
"for layer in range(model.cfg.n_layers):\n",
" for position in range(tokens.shape[1]):\n",
" hook_fn = partial(patch_residual_component, pos=position, clean_cache=cache)\n",
Expand Down Expand Up @@ -1639,7 +1658,7 @@
" return corrupted_head_vector\n",
"\n",
"\n",
"patched_head_z_diff = torch.zeros(model.cfg.n_layers, model.cfg.n_heads, device=\"cuda\", dtype=torch.float32)\n",
"patched_head_z_diff = torch.zeros(model.cfg.n_layers, model.cfg.n_heads, device=device, dtype=torch.float32)\n",
"for layer in range(model.cfg.n_layers):\n",
" for head_index in range(model.cfg.n_heads):\n",
" hook_fn = partial(patch_head_vector, head_index=head_index, clean_cache=cache)\n",
Expand Down Expand Up @@ -1737,7 +1756,7 @@
"metadata": {},
"outputs": [],
"source": [
"patched_head_v_diff = torch.zeros(model.cfg.n_layers, model.cfg.n_heads, device=\"cuda\", dtype=torch.float32)\n",
"patched_head_v_diff = torch.zeros(model.cfg.n_layers, model.cfg.n_heads, device=device, dtype=torch.float32)\n",
"for layer in range(model.cfg.n_layers):\n",
" for head_index in range(model.cfg.n_heads):\n",
" hook_fn = partial(patch_head_vector, head_index=head_index, clean_cache=cache)\n",
Expand Down Expand Up @@ -1899,7 +1918,7 @@
" corrupted_head_pattern[:, head_index, :, :] = clean_cache[hook.name][:, head_index, :, :]\n",
" return corrupted_head_pattern\n",
"\n",
"patched_head_attn_diff = torch.zeros(model.cfg.n_layers, model.cfg.n_heads, device=\"cuda\", dtype=torch.float32)\n",
"patched_head_attn_diff = torch.zeros(model.cfg.n_layers, model.cfg.n_heads, device=device, dtype=torch.float32)\n",
"for layer in range(model.cfg.n_layers):\n",
" for head_index in range(model.cfg.n_heads):\n",
" hook_fn = partial(patch_head_pattern, head_index=head_index, clean_cache=cache)\n",
Expand Down Expand Up @@ -2491,25 +2510,25 @@
"seq_len = 100\n",
"batch_size = 2\n",
"\n",
"prev_token_scores = torch.zeros((model.cfg.n_layers, model.cfg.n_heads), device=\"cuda\")\n",
"prev_token_scores = torch.zeros((model.cfg.n_layers, model.cfg.n_heads), device=device)\n",
"def prev_token_hook(pattern, hook):\n",
" layer = hook.layer()\n",
" diagonal = pattern.diagonal(offset=1, dim1=-1, dim2=-2)\n",
" # print(diagonal)\n",
" # print(pattern)\n",
" prev_token_scores[layer] = einops.reduce(diagonal, \"batch head_index diagonal -> head_index\", \"mean\")\n",
"duplicate_token_scores = torch.zeros((model.cfg.n_layers, model.cfg.n_heads), device=\"cuda\")\n",
"duplicate_token_scores = torch.zeros((model.cfg.n_layers, model.cfg.n_heads), device=device)\n",
"def duplicate_token_hook(pattern, hook):\n",
" layer = hook.layer()\n",
" diagonal = pattern.diagonal(offset=seq_len, dim1=-1, dim2=-2)\n",
" duplicate_token_scores[layer] = einops.reduce(diagonal, \"batch head_index diagonal -> head_index\", \"mean\")\n",
"induction_scores = torch.zeros((model.cfg.n_layers, model.cfg.n_heads), device=\"cuda\")\n",
"induction_scores = torch.zeros((model.cfg.n_layers, model.cfg.n_heads), device=device)\n",
"def induction_hook(pattern, hook):\n",
" layer = hook.layer()\n",
" diagonal = pattern.diagonal(offset=seq_len-1, dim1=-1, dim2=-2)\n",
" induction_scores[layer] = einops.reduce(diagonal, \"batch head_index diagonal -> head_index\", \"mean\")\n",
"original_tokens = torch.randint(100, 20000, size=(batch_size, seq_len))\n",
"repeated_tokens = einops.repeat(original_tokens, \"batch seq_len -> batch (2 seq_len)\").cuda()\n",
"repeated_tokens = einops.repeat(original_tokens, \"batch seq_len -> batch (2 seq_len)\").to(device)\n",
"\n",
"pattern_filter = lambda act_name: act_name.endswith(\"hook_attn\")\n",
"loss = model.run_with_hooks(repeated_tokens, return_type=\"loss\", fwd_hooks=[(pattern_filter, prev_token_hook), (pattern_filter, duplicate_token_hook), (pattern_filter, induction_hook)])\n",
Expand Down Expand Up @@ -2912,7 +2931,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.14"
"version": "3.8.10"
},
"vscode": {
"interpreter": {
Expand Down
Loading