Repository navigation
deepseek r1 distilled tokenizer fix for qwen2 mapping - #45741
Merged
Merged
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: auto |
hmellor
approved these changes
May 5, 2026
4 tasks
4 tasks
This was referenced Sep 29, 2026
Closed
davidquarel
added a commit
to ARENA-education/ARENA_materials
that referenced
this pull request
Oct 2, 2026
…Colab installs (#563) * Bump TransformerLens to 3.9 (minimal: keep HookedTransformer) TL<3 pinned numpy<2, which made requirements.txt unresolvable with jax (#358) and broke Colab installs (#388, #419). TL 3.9 still ships HookedTransformer (deprecated) and allows numpy 2, but requires transformers>=5.9, so this also adapts the code that transformers 5 changed: - tokenizer.encode prepends BOS: ioi_dataset add_special_tokens=False, [1.1] sampler uses a plain GPT2TokenizerFast, [2.4] tests prepend_bos=True - batch_decode on 1D input: unsqueeze ([1.3.2], [4.1]) - apply_chat_template(tokenize=True) returns a dict: return_dict=False ([1.3.4], [4.4]) - decoder layers may return a tensor instead of a tuple ([4.4]) - [1.4.2] to_str_tokens(tokens) (TL 3.9 crashes on strings for Gemma); circuit-tracer from ARENA's fork (upstream needs huggingface_hub<1) sae-vis and CircuitsVis come from ARENA fork tags (PyPI sae-vis is uninstallable; Callum's CircuitsVis pins numpy<2). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Follow-ups from the audit: sae-lens>=6.51.3, 1.1 sampler tokenizer, 1.3.4 Colab line, comments - sae-lens>=6.51.3 (requirements and Colab lines): 6.51.0-6.51.2 accept TL 3.9 but HookedSAETransformer.from_pretrained("attn-only-2l-demo") fails on them ('TransformerBlock' object has no attribute 'mlp'), and pip won't upgrade an already-installed 6.51.2 that satisfies >=6.51. - [1.1] The training loop's and sampling_fn's samplers use a plain GPT-2 tokenizer, as the sampling section already does; transformers 5 makes reference_gpt2.tokenizer prepend <|endoftext|> in encode. - [1.3.4] The Colab cell installed transformer_lens (unused in 1.3.4) to detect Colab, which would upgrade Colab's transformers to 5.x. Detect and install peft / einops / jaxtyping / openai / python-dotenv instead. - Comments carried over from the TL4 port said "TL4" / "required by TransformerLens 4"; they now refer to TransformerLens 3.9. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Cap transformers<6; note that the attention_heads resizing bug is fixed From #554: - requirements.txt: transformers>=5.9,<6 (sae-lens 6.51.3 already requires <6; this makes the cap explicit). - [1.1] Un-comment the "attention_heads plots are behaving weirdly" dropdown and drop the "may exhibit a bug in VSCode" sentence; [1.1] and [1.4.1] note that the bug was fixed upstream (CircuitsVis PR #63). The ARENA CircuitsVis fork tag we install (arena-tl4.1, frontend 1.41.0) includes that fix (cc21677 pointed the package at the fixed 1.40.1 build). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Sampling: keep the BOS token that GPT-2 prompts should always have had TransformerLens configures GPT-2's tokenizer with add_bos_token=True, and model.to_tokens() has always prepended <|endoftext|>. transformers < 5 silently ignored add_bos_token in tokenizer.encode(), so the 1.1 sampling section fed GPT-2 prompts without the BOS token the rest of the course uses. transformers 5 (required by TransformerLens 3.9) honours it, so encode(prompt) now matches to_tokens(prompt). Treat that as the fix rather than working around it (reverts #450's swap to a plain GPT2TokenizerFast): - Use reference_gpt2.tokenizer again for the sampler. - Decode with skip_special_tokens=True so outputs don't start with <|endoftext|>; add a hint explaining the BOS token to the sampler exercise. - Re-measure the hard-coded expectations with BOS, on TransformerLens 3.9 / transformers 5.17: greedy "Jingle bells" ("...all the way down to...", was "up"), the basic/top-k top-5 table (church .0466, hospital .0223, store .0181, house .0181, doctor .0124) and the top_p=0.1 set (now 4 tokens, cumulative .105; the old 2-token comment no longer held). - Unsqueeze the 1D batch_decode in the "most likely next tokens" cell: transformers 5 decodes a flat 1D input as one string, which the cell then zipped against tokens character by character. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Beam search: re-measure the generate/filter expectations with BOS Same cause as the previous commit: the beam-search unit tests hard-coded logprob sums and completions measured without the BOS token. Re-measured on TransformerLens 3.9 / transformers 5.17 (generate: a kid -2.177, a child -3.803, a teenager -4.178; filter(3): a kid, growing up -2.675, a child), and regenerated the three pre-rendered "Best completions" tables with rich (byte-identical format; checked by re-rendering the old table). The prose no longer says the output should match the diagram, which was drawn from the BOS-less numbers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * Move the attention_heads note out of the port (now #562) The CircuitsVis "attention_heads resizing bug is fixed" note doesn't depend on TransformerLens 3.9 (main's CircuitsVis already has the fix), so it has its own PR, #562. Keep this branch to what the upgrade needs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [4.3] BOS at tokenizer load + sentence boundaries from the model input's offsets (transformers >= 5) Replaces #474 + #518 for the transformers >= 5 world (stacked on the TransformerLens 3.9 port). - Load the DeepSeek-R1-Distill tokenizers with add_bos_token=True and assert they add a BOS. transformers loads these checkpoints as Qwen2Tokenizer / TokenizersBackend (huggingface/transformers#45741, v5.8.0), which never add a BOS, although the repos set add_bos_token=true and the model expects one. - get_sentence_token_boundaries: take the offset mapping of the model input tokenizer(text) itself, so the BOS (offsets (0, 0)) is accounted for without a manual shift. A sentence covers every token overlapping its characters (bisect over token starts/ends), which also handles characters like "≈" that byte-level BPE splits across several tokens (char_to_token would drop the second token) and "." merged with the following "\n\n". - get_sentence_char_boundaries (from #518): map sentences found in the whitespace-normalised text back to positions in the original text, and use it for text_subset. Regenerated attention-suppression output from #518 (identical model inputs). Checked on 20 dataset problems (transformers 5.17.0): exact decode 2952/2952, 0 overlaps, 0 empty spans, boundaries identical to #518 for 20/20; #518's synthetic edge cases decode identically. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * wip: split transformers-5-compatible changes out of #563 (no-ops on transformers 4) * wip: drop round-tripped master .ipynb (CI regenerates them) * wip: 2.4 test comment states the measured cause * wip: 1.3.4 Colab cell detects jaxtyping (Colab preinstalls peft, so 'import peft' would skip the install) * wip: 4.3 suppression dropdown re-measured on transformers 4.57.6 (matches 5.18.0 to 3 s.f.) * Align with #566: wording, 4.3 regex escapes, 1.3.4 Colab check, re-measured 4.3 output The hunks #563 shares with #566 now have identical text, so the two PRs merge in either order. - Comments on lines that also work on transformers 4 say "in transformers>=5", not "required by TransformerLens 3.9". - [4.3] normalize_spaces: restore the \u00A0...\uFEFF escapes (the port had replaced them with the literal invisible characters). - [4.3] Suppression cell's expected output, re-measured (the pasted 634x was never run on this branch): sentence 66 vs 54, KL ratio 3.0x; the same on transformers 4.57.6 and 5.18.0. - [1.3.4] Colab cell detects jaxtyping, not peft (Colab preinstalls peft, so the install would be skipped). - [2.4] test_transformer_with_value_head comment states the measured cause (TransformerLens 3.9 sets default_prepend_bos=False for Pythia). Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [1.3.2] top-logits cell: unsqueeze(-1) instead of a list comprehension * wip: batch_decode notes are master-only (FILTERS: ~); students see one explanation, in 1.3.2's tokenizer section * wip: return_dict and to_str_tokens notes master-only (FILTERS: ~); drop the inline note from part34 utils.py * wip: [4.3] Sentence token boundaries from the model input's offset mapping; BOS for the R1-Distill tokenizers (split from #566) * wip: [4.4] Hooks accept a tuple or a tensor; apply_chat_template(return_dict=False) (split from #566) * wip: move the [4.3] and [4.4] changes to their own PRs * wip: get_sentence_char_boundaries gets an Args/Returns docstring like its neighbours * wip: 4.4 notes master-only (FILTERS: ~), wording covers Qwen2 (tensor already on 4.57) and Gemma 2 (tensor from 5) * Move the [4.4] hook / return_dict changes out to #570, which now builds on this PR #570 is rewritten for transformers>=5 only (plain-tensor layer outputs), so it should merge right after this PR. This PR keeps only the TransformerLens 3.9 pin line in master_4_4.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Drop the [4.3] sentence-boundary changes (#568) from this PR; #568 stays separate 4.3 keeps main's code plus the TransformerLens 3.9 pin. On transformers 5 the R1-Distill tokenizers then add no BOS token (main's boundary code happens to line up without one); #568 is the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HqBn5Uf3EY6Ddvxii1Drg7 --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
davidquarel
added a commit
to ARENA-education/ARENA_materials
that referenced
this pull request
Oct 5, 2026
…es tensor; apply_chat_template(return_dict=False) (merge right after #563) (#570) * Bump TransformerLens to 3.9 (minimal: keep HookedTransformer) TL<3 pinned numpy<2, which made requirements.txt unresolvable with jax (#358) and broke Colab installs (#388, #419). TL 3.9 still ships HookedTransformer (deprecated) and allows numpy 2, but requires transformers>=5.9, so this also adapts the code that transformers 5 changed: - tokenizer.encode prepends BOS: ioi_dataset add_special_tokens=False, [1.1] sampler uses a plain GPT2TokenizerFast, [2.4] tests prepend_bos=True - batch_decode on 1D input: unsqueeze ([1.3.2], [4.1]) - apply_chat_template(tokenize=True) returns a dict: return_dict=False ([1.3.4], [4.4]) - decoder layers may return a tensor instead of a tuple ([4.4]) - [1.4.2] to_str_tokens(tokens) (TL 3.9 crashes on strings for Gemma); circuit-tracer from ARENA's fork (upstream needs huggingface_hub<1) sae-vis and CircuitsVis come from ARENA fork tags (PyPI sae-vis is uninstallable; Callum's CircuitsVis pins numpy<2). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Follow-ups from the audit: sae-lens>=6.51.3, 1.1 sampler tokenizer, 1.3.4 Colab line, comments - sae-lens>=6.51.3 (requirements and Colab lines): 6.51.0-6.51.2 accept TL 3.9 but HookedSAETransformer.from_pretrained("attn-only-2l-demo") fails on them ('TransformerBlock' object has no attribute 'mlp'), and pip won't upgrade an already-installed 6.51.2 that satisfies >=6.51. - [1.1] The training loop's and sampling_fn's samplers use a plain GPT-2 tokenizer, as the sampling section already does; transformers 5 makes reference_gpt2.tokenizer prepend <|endoftext|> in encode. - [1.3.4] The Colab cell installed transformer_lens (unused in 1.3.4) to detect Colab, which would upgrade Colab's transformers to 5.x. Detect and install peft / einops / jaxtyping / openai / python-dotenv instead. - Comments carried over from the TL4 port said "TL4" / "required by TransformerLens 4"; they now refer to TransformerLens 3.9. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Cap transformers<6; note that the attention_heads resizing bug is fixed From #554: - requirements.txt: transformers>=5.9,<6 (sae-lens 6.51.3 already requires <6; this makes the cap explicit). - [1.1] Un-comment the "attention_heads plots are behaving weirdly" dropdown and drop the "may exhibit a bug in VSCode" sentence; [1.1] and [1.4.1] note that the bug was fixed upstream (CircuitsVis PR #63). The ARENA CircuitsVis fork tag we install (arena-tl4.1, frontend 1.41.0) includes that fix (cc21677 pointed the package at the fixed 1.40.1 build). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Sampling: keep the BOS token that GPT-2 prompts should always have had TransformerLens configures GPT-2's tokenizer with add_bos_token=True, and model.to_tokens() has always prepended <|endoftext|>. transformers < 5 silently ignored add_bos_token in tokenizer.encode(), so the 1.1 sampling section fed GPT-2 prompts without the BOS token the rest of the course uses. transformers 5 (required by TransformerLens 3.9) honours it, so encode(prompt) now matches to_tokens(prompt). Treat that as the fix rather than working around it (reverts #450's swap to a plain GPT2TokenizerFast): - Use reference_gpt2.tokenizer again for the sampler. - Decode with skip_special_tokens=True so outputs don't start with <|endoftext|>; add a hint explaining the BOS token to the sampler exercise. - Re-measure the hard-coded expectations with BOS, on TransformerLens 3.9 / transformers 5.17: greedy "Jingle bells" ("...all the way down to...", was "up"), the basic/top-k top-5 table (church .0466, hospital .0223, store .0181, house .0181, doctor .0124) and the top_p=0.1 set (now 4 tokens, cumulative .105; the old 2-token comment no longer held). - Unsqueeze the 1D batch_decode in the "most likely next tokens" cell: transformers 5 decodes a flat 1D input as one string, which the cell then zipped against tokens character by character. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Beam search: re-measure the generate/filter expectations with BOS Same cause as the previous commit: the beam-search unit tests hard-coded logprob sums and completions measured without the BOS token. Re-measured on TransformerLens 3.9 / transformers 5.17 (generate: a kid -2.177, a child -3.803, a teenager -4.178; filter(3): a kid, growing up -2.675, a child), and regenerated the three pre-rendered "Best completions" tables with rich (byte-identical format; checked by re-rendering the old table). The prose no longer says the output should match the diagram, which was drawn from the BOS-less numbers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * Move the attention_heads note out of the port (now #562) The CircuitsVis "attention_heads resizing bug is fixed" note doesn't depend on TransformerLens 3.9 (main's CircuitsVis already has the fix), so it has its own PR, #562. Keep this branch to what the upgrade needs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [4.3] BOS at tokenizer load + sentence boundaries from the model input's offsets (transformers >= 5) Replaces #474 + #518 for the transformers >= 5 world (stacked on the TransformerLens 3.9 port). - Load the DeepSeek-R1-Distill tokenizers with add_bos_token=True and assert they add a BOS. transformers loads these checkpoints as Qwen2Tokenizer / TokenizersBackend (huggingface/transformers#45741, v5.8.0), which never add a BOS, although the repos set add_bos_token=true and the model expects one. - get_sentence_token_boundaries: take the offset mapping of the model input tokenizer(text) itself, so the BOS (offsets (0, 0)) is accounted for without a manual shift. A sentence covers every token overlapping its characters (bisect over token starts/ends), which also handles characters like "≈" that byte-level BPE splits across several tokens (char_to_token would drop the second token) and "." merged with the following "\n\n". - get_sentence_char_boundaries (from #518): map sentences found in the whitespace-normalised text back to positions in the original text, and use it for text_subset. Regenerated attention-suppression output from #518 (identical model inputs). Checked on 20 dataset problems (transformers 5.17.0): exact decode 2952/2952, 0 overlaps, 0 empty spans, boundaries identical to #518 for 20/20; #518's synthetic edge cases decode identically. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * wip: split transformers-5-compatible changes out of #563 (no-ops on transformers 4) * wip: drop round-tripped master .ipynb (CI regenerates them) * wip: 2.4 test comment states the measured cause * wip: 1.3.4 Colab cell detects jaxtyping (Colab preinstalls peft, so 'import peft' would skip the install) * wip: 4.3 suppression dropdown re-measured on transformers 4.57.6 (matches 5.18.0 to 3 s.f.) * Align with #566: wording, 4.3 regex escapes, 1.3.4 Colab check, re-measured 4.3 output The hunks #563 shares with #566 now have identical text, so the two PRs merge in either order. - Comments on lines that also work on transformers 4 say "in transformers>=5", not "required by TransformerLens 3.9". - [4.3] normalize_spaces: restore the \u00A0...\uFEFF escapes (the port had replaced them with the literal invisible characters). - [4.3] Suppression cell's expected output, re-measured (the pasted 634x was never run on this branch): sentence 66 vs 54, KL ratio 3.0x; the same on transformers 4.57.6 and 5.18.0. - [1.3.4] Colab cell detects jaxtyping, not peft (Colab preinstalls peft, so the install would be skipped). - [2.4] test_transformer_with_value_head comment states the measured cause (TransformerLens 3.9 sets default_prepend_bos=False for Pythia). Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [1.3.2] top-logits cell: unsqueeze(-1) instead of a list comprehension * wip: batch_decode notes are master-only (FILTERS: ~); students see one explanation, in 1.3.2's tokenizer section * wip: return_dict and to_str_tokens notes master-only (FILTERS: ~); drop the inline note from part34 utils.py * wip: [4.3] Sentence token boundaries from the model input's offset mapping; BOS for the R1-Distill tokenizers (split from #566) * wip: [4.4] Hooks accept a tuple or a tensor; apply_chat_template(return_dict=False) (split from #566) * wip: move the [4.3] and [4.4] changes to their own PRs * wip: get_sentence_char_boundaries gets an Args/Returns docstring like its neighbours * wip: 4.4 notes master-only (FILTERS: ~), wording covers Qwen2 (tensor already on 4.57) and Gemma 2 (tensor from 5) * Move the [4.4] hook / return_dict changes out to #570, which now builds on this PR #570 is rewritten for transformers>=5 only (plain-tensor layer outputs), so it should merge right after this PR. This PR keeps only the TransformerLens 3.9 pin line in master_4_4.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [4.4] rewrite for transformers>=5: decoder-layer hooks take and return the hidden-states tensor; ActivationSteerer/ActivationCapper simplified to match; return_dict=False * Drop the [4.3] sentence-boundary changes (#568) from this PR; #568 stays separate 4.3 keeps main's code plus the TransformerLens 3.9 pin. On transformers 5 the R1-Distill tokenizers then add no BOS token (main's boundary code happens to line up without one); #568 is the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HqBn5Uf3EY6Ddvxii1Drg7 --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
davidquarel
added a commit
to ARENA-education/ARENA_materials
that referenced
this pull request
Oct 5, 2026
…`.output[0]` on GPT-2 (after #563) (#573) * Bump TransformerLens to 3.9 (minimal: keep HookedTransformer) TL<3 pinned numpy<2, which made requirements.txt unresolvable with jax (#358) and broke Colab installs (#388, #419). TL 3.9 still ships HookedTransformer (deprecated) and allows numpy 2, but requires transformers>=5.9, so this also adapts the code that transformers 5 changed: - tokenizer.encode prepends BOS: ioi_dataset add_special_tokens=False, [1.1] sampler uses a plain GPT2TokenizerFast, [2.4] tests prepend_bos=True - batch_decode on 1D input: unsqueeze ([1.3.2], [4.1]) - apply_chat_template(tokenize=True) returns a dict: return_dict=False ([1.3.4], [4.4]) - decoder layers may return a tensor instead of a tuple ([4.4]) - [1.4.2] to_str_tokens(tokens) (TL 3.9 crashes on strings for Gemma); circuit-tracer from ARENA's fork (upstream needs huggingface_hub<1) sae-vis and CircuitsVis come from ARENA fork tags (PyPI sae-vis is uninstallable; Callum's CircuitsVis pins numpy<2). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Follow-ups from the audit: sae-lens>=6.51.3, 1.1 sampler tokenizer, 1.3.4 Colab line, comments - sae-lens>=6.51.3 (requirements and Colab lines): 6.51.0-6.51.2 accept TL 3.9 but HookedSAETransformer.from_pretrained("attn-only-2l-demo") fails on them ('TransformerBlock' object has no attribute 'mlp'), and pip won't upgrade an already-installed 6.51.2 that satisfies >=6.51. - [1.1] The training loop's and sampling_fn's samplers use a plain GPT-2 tokenizer, as the sampling section already does; transformers 5 makes reference_gpt2.tokenizer prepend <|endoftext|> in encode. - [1.3.4] The Colab cell installed transformer_lens (unused in 1.3.4) to detect Colab, which would upgrade Colab's transformers to 5.x. Detect and install peft / einops / jaxtyping / openai / python-dotenv instead. - Comments carried over from the TL4 port said "TL4" / "required by TransformerLens 4"; they now refer to TransformerLens 3.9. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Cap transformers<6; note that the attention_heads resizing bug is fixed From #554: - requirements.txt: transformers>=5.9,<6 (sae-lens 6.51.3 already requires <6; this makes the cap explicit). - [1.1] Un-comment the "attention_heads plots are behaving weirdly" dropdown and drop the "may exhibit a bug in VSCode" sentence; [1.1] and [1.4.1] note that the bug was fixed upstream (CircuitsVis PR #63). The ARENA CircuitsVis fork tag we install (arena-tl4.1, frontend 1.41.0) includes that fix (cc21677 pointed the package at the fixed 1.40.1 build). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Sampling: keep the BOS token that GPT-2 prompts should always have had TransformerLens configures GPT-2's tokenizer with add_bos_token=True, and model.to_tokens() has always prepended <|endoftext|>. transformers < 5 silently ignored add_bos_token in tokenizer.encode(), so the 1.1 sampling section fed GPT-2 prompts without the BOS token the rest of the course uses. transformers 5 (required by TransformerLens 3.9) honours it, so encode(prompt) now matches to_tokens(prompt). Treat that as the fix rather than working around it (reverts #450's swap to a plain GPT2TokenizerFast): - Use reference_gpt2.tokenizer again for the sampler. - Decode with skip_special_tokens=True so outputs don't start with <|endoftext|>; add a hint explaining the BOS token to the sampler exercise. - Re-measure the hard-coded expectations with BOS, on TransformerLens 3.9 / transformers 5.17: greedy "Jingle bells" ("...all the way down to...", was "up"), the basic/top-k top-5 table (church .0466, hospital .0223, store .0181, house .0181, doctor .0124) and the top_p=0.1 set (now 4 tokens, cumulative .105; the old 2-token comment no longer held). - Unsqueeze the 1D batch_decode in the "most likely next tokens" cell: transformers 5 decodes a flat 1D input as one string, which the cell then zipped against tokens character by character. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Beam search: re-measure the generate/filter expectations with BOS Same cause as the previous commit: the beam-search unit tests hard-coded logprob sums and completions measured without the BOS token. Re-measured on TransformerLens 3.9 / transformers 5.17 (generate: a kid -2.177, a child -3.803, a teenager -4.178; filter(3): a kid, growing up -2.675, a child), and regenerated the three pre-rendered "Best completions" tables with rich (byte-identical format; checked by re-rendering the old table). The prose no longer says the output should match the diagram, which was drawn from the BOS-less numbers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * Move the attention_heads note out of the port (now #562) The CircuitsVis "attention_heads resizing bug is fixed" note doesn't depend on TransformerLens 3.9 (main's CircuitsVis already has the fix), so it has its own PR, #562. Keep this branch to what the upgrade needs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [4.3] BOS at tokenizer load + sentence boundaries from the model input's offsets (transformers >= 5) Replaces #474 + #518 for the transformers >= 5 world (stacked on the TransformerLens 3.9 port). - Load the DeepSeek-R1-Distill tokenizers with add_bos_token=True and assert they add a BOS. transformers loads these checkpoints as Qwen2Tokenizer / TokenizersBackend (huggingface/transformers#45741, v5.8.0), which never add a BOS, although the repos set add_bos_token=true and the model expects one. - get_sentence_token_boundaries: take the offset mapping of the model input tokenizer(text) itself, so the BOS (offsets (0, 0)) is accounted for without a manual shift. A sentence covers every token overlapping its characters (bisect over token starts/ends), which also handles characters like "≈" that byte-level BPE splits across several tokens (char_to_token would drop the second token) and "." merged with the following "\n\n". - get_sentence_char_boundaries (from #518): map sentences found in the whitespace-normalised text back to positions in the original text, and use it for text_subset. Regenerated attention-suppression output from #518 (identical model inputs). Checked on 20 dataset problems (transformers 5.17.0): exact decode 2952/2952, 0 overlaps, 0 empty spans, boundaries identical to #518 for 20/20; #518's synthetic edge cases decode identically. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * wip: split transformers-5-compatible changes out of #563 (no-ops on transformers 4) * wip: drop round-tripped master .ipynb (CI regenerates them) * wip: 2.4 test comment states the measured cause * wip: 1.3.4 Colab cell detects jaxtyping (Colab preinstalls peft, so 'import peft' would skip the install) * wip: 4.3 suppression dropdown re-measured on transformers 4.57.6 (matches 5.18.0 to 3 s.f.) * Align with #566: wording, 4.3 regex escapes, 1.3.4 Colab check, re-measured 4.3 output The hunks #563 shares with #566 now have identical text, so the two PRs merge in either order. - Comments on lines that also work on transformers 4 say "in transformers>=5", not "required by TransformerLens 3.9". - [4.3] normalize_spaces: restore the \u00A0...\uFEFF escapes (the port had replaced them with the literal invisible characters). - [4.3] Suppression cell's expected output, re-measured (the pasted 634x was never run on this branch): sentence 66 vs 54, KL ratio 3.0x; the same on transformers 4.57.6 and 5.18.0. - [1.3.4] Colab cell detects jaxtyping, not peft (Colab preinstalls peft, so the install would be skipped). - [2.4] test_transformer_with_value_head comment states the measured cause (TransformerLens 3.9 sets default_prepend_bos=False for Pythia). Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [1.3.2] top-logits cell: unsqueeze(-1) instead of a list comprehension * wip: batch_decode notes are master-only (FILTERS: ~); students see one explanation, in 1.3.2's tokenizer section * wip: return_dict and to_str_tokens notes master-only (FILTERS: ~); drop the inline note from part34 utils.py * wip: [4.3] Sentence token boundaries from the model input's offset mapping; BOS for the R1-Distill tokenizers (split from #566) * wip: [4.4] Hooks accept a tuple or a tensor; apply_chat_template(return_dict=False) (split from #566) * wip: move the [4.3] and [4.4] changes to their own PRs * wip: get_sentence_char_boundaries gets an Args/Returns docstring like its neighbours * wip: 4.4 notes master-only (FILTERS: ~), wording covers Qwen2 (tensor already on 4.57) and Gemma 2 (tensor from 5) * Move the [4.4] hook / return_dict changes out to #570, which now builds on this PR #570 is rewritten for transformers>=5 only (plain-tensor layer outputs), so it should merge right after this PR. This PR keeps only the TransformerLens 3.9 pin line in master_4_4.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Drop the [4.3] sentence-boundary changes (#568) from this PR; #568 stays separate 4.3 keeps main's code plus the TransformerLens 3.9 pin. On transformers 5 the R1-Distill tokenizers then add no BOS token (main's boundary code happens to line up without one); #568 is the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HqBn5Uf3EY6Ddvxii1Drg7 * [1.3.2] Test that steering changes every steered completion (catches `.output[0]` on GPT-2) On transformers 5, GPT-2 blocks return the hidden-states tensor rather than a tuple, so the `.output[0]` idiom students used for GPT-J silently selects batch element 0 and the steering does nothing, with no error. `calculate_and_apply_steering_vector` had no test. `test_calculate_and_apply_steering_vector` switches the student's `SAMPLING_KWARGS` to greedy decoding for one short run (restored afterwards) with `n_comparisons=2`, and asserts every steered completion differs from its unsteered twin. Two rows are needed: with one, the buggy `+=` still perturbs the (wrong) slice and changes the output. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [1.3.2] Keep the GPT-2 `.output` (no `[0]`) code comment in the master only Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
BarryTheShen
pushed a commit
to BarryTheShen/ARENA_materials
that referenced
this pull request
Nov 25, 2026
…Colab installs (#563) * Bump TransformerLens to 3.9 (minimal: keep HookedTransformer) TL<3 pinned numpy<2, which made requirements.txt unresolvable with jax (#358) and broke Colab installs (#388, #419). TL 3.9 still ships HookedTransformer (deprecated) and allows numpy 2, but requires transformers>=5.9, so this also adapts the code that transformers 5 changed: - tokenizer.encode prepends BOS: ioi_dataset add_special_tokens=False, [1.1] sampler uses a plain GPT2TokenizerFast, [2.4] tests prepend_bos=True - batch_decode on 1D input: unsqueeze ([1.3.2], [4.1]) - apply_chat_template(tokenize=True) returns a dict: return_dict=False ([1.3.4], [4.4]) - decoder layers may return a tensor instead of a tuple ([4.4]) - [1.4.2] to_str_tokens(tokens) (TL 3.9 crashes on strings for Gemma); circuit-tracer from ARENA's fork (upstream needs huggingface_hub<1) sae-vis and CircuitsVis come from ARENA fork tags (PyPI sae-vis is uninstallable; Callum's CircuitsVis pins numpy<2). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Follow-ups from the audit: sae-lens>=6.51.3, 1.1 sampler tokenizer, 1.3.4 Colab line, comments - sae-lens>=6.51.3 (requirements and Colab lines): 6.51.0-6.51.2 accept TL 3.9 but HookedSAETransformer.from_pretrained("attn-only-2l-demo") fails on them ('TransformerBlock' object has no attribute 'mlp'), and pip won't upgrade an already-installed 6.51.2 that satisfies >=6.51. - [1.1] The training loop's and sampling_fn's samplers use a plain GPT-2 tokenizer, as the sampling section already does; transformers 5 makes reference_gpt2.tokenizer prepend <|endoftext|> in encode. - [1.3.4] The Colab cell installed transformer_lens (unused in 1.3.4) to detect Colab, which would upgrade Colab's transformers to 5.x. Detect and install peft / einops / jaxtyping / openai / python-dotenv instead. - Comments carried over from the TL4 port said "TL4" / "required by TransformerLens 4"; they now refer to TransformerLens 3.9. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Cap transformers<6; note that the attention_heads resizing bug is fixed From #554: - requirements.txt: transformers>=5.9,<6 (sae-lens 6.51.3 already requires <6; this makes the cap explicit). - [1.1] Un-comment the "attention_heads plots are behaving weirdly" dropdown and drop the "may exhibit a bug in VSCode" sentence; [1.1] and [1.4.1] note that the bug was fixed upstream (CircuitsVis PR #63). The ARENA CircuitsVis fork tag we install (arena-tl4.1, frontend 1.41.0) includes that fix (cc21677 pointed the package at the fixed 1.40.1 build). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Sampling: keep the BOS token that GPT-2 prompts should always have had TransformerLens configures GPT-2's tokenizer with add_bos_token=True, and model.to_tokens() has always prepended <|endoftext|>. transformers < 5 silently ignored add_bos_token in tokenizer.encode(), so the 1.1 sampling section fed GPT-2 prompts without the BOS token the rest of the course uses. transformers 5 (required by TransformerLens 3.9) honours it, so encode(prompt) now matches to_tokens(prompt). Treat that as the fix rather than working around it (reverts #450's swap to a plain GPT2TokenizerFast): - Use reference_gpt2.tokenizer again for the sampler. - Decode with skip_special_tokens=True so outputs don't start with <|endoftext|>; add a hint explaining the BOS token to the sampler exercise. - Re-measure the hard-coded expectations with BOS, on TransformerLens 3.9 / transformers 5.17: greedy "Jingle bells" ("...all the way down to...", was "up"), the basic/top-k top-5 table (church .0466, hospital .0223, store .0181, house .0181, doctor .0124) and the top_p=0.1 set (now 4 tokens, cumulative .105; the old 2-token comment no longer held). - Unsqueeze the 1D batch_decode in the "most likely next tokens" cell: transformers 5 decodes a flat 1D input as one string, which the cell then zipped against tokens character by character. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Beam search: re-measure the generate/filter expectations with BOS Same cause as the previous commit: the beam-search unit tests hard-coded logprob sums and completions measured without the BOS token. Re-measured on TransformerLens 3.9 / transformers 5.17 (generate: a kid -2.177, a child -3.803, a teenager -4.178; filter(3): a kid, growing up -2.675, a child), and regenerated the three pre-rendered "Best completions" tables with rich (byte-identical format; checked by re-rendering the old table). The prose no longer says the output should match the diagram, which was drawn from the BOS-less numbers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * Move the attention_heads note out of the port (now #562) The CircuitsVis "attention_heads resizing bug is fixed" note doesn't depend on TransformerLens 3.9 (main's CircuitsVis already has the fix), so it has its own PR, #562. Keep this branch to what the upgrade needs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [4.3] BOS at tokenizer load + sentence boundaries from the model input's offsets (transformers >= 5) Replaces #474 + #518 for the transformers >= 5 world (stacked on the TransformerLens 3.9 port). - Load the DeepSeek-R1-Distill tokenizers with add_bos_token=True and assert they add a BOS. transformers loads these checkpoints as Qwen2Tokenizer / TokenizersBackend (huggingface/transformers#45741, v5.8.0), which never add a BOS, although the repos set add_bos_token=true and the model expects one. - get_sentence_token_boundaries: take the offset mapping of the model input tokenizer(text) itself, so the BOS (offsets (0, 0)) is accounted for without a manual shift. A sentence covers every token overlapping its characters (bisect over token starts/ends), which also handles characters like "≈" that byte-level BPE splits across several tokens (char_to_token would drop the second token) and "." merged with the following "\n\n". - get_sentence_char_boundaries (from #518): map sentences found in the whitespace-normalised text back to positions in the original text, and use it for text_subset. Regenerated attention-suppression output from #518 (identical model inputs). Checked on 20 dataset problems (transformers 5.17.0): exact decode 2952/2952, 0 overlaps, 0 empty spans, boundaries identical to #518 for 20/20; #518's synthetic edge cases decode identically. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * wip: split transformers-5-compatible changes out of #563 (no-ops on transformers 4) * wip: drop round-tripped master .ipynb (CI regenerates them) * wip: 2.4 test comment states the measured cause * wip: 1.3.4 Colab cell detects jaxtyping (Colab preinstalls peft, so 'import peft' would skip the install) * wip: 4.3 suppression dropdown re-measured on transformers 4.57.6 (matches 5.18.0 to 3 s.f.) * Align with #566: wording, 4.3 regex escapes, 1.3.4 Colab check, re-measured 4.3 output The hunks #563 shares with #566 now have identical text, so the two PRs merge in either order. - Comments on lines that also work on transformers 4 say "in transformers>=5", not "required by TransformerLens 3.9". - [4.3] normalize_spaces: restore the \u00A0...\uFEFF escapes (the port had replaced them with the literal invisible characters). - [4.3] Suppression cell's expected output, re-measured (the pasted 634x was never run on this branch): sentence 66 vs 54, KL ratio 3.0x; the same on transformers 4.57.6 and 5.18.0. - [1.3.4] Colab cell detects jaxtyping, not peft (Colab preinstalls peft, so the install would be skipped). - [2.4] test_transformer_with_value_head comment states the measured cause (TransformerLens 3.9 sets default_prepend_bos=False for Pythia). Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [1.3.2] top-logits cell: unsqueeze(-1) instead of a list comprehension * wip: batch_decode notes are master-only (FILTERS: ~); students see one explanation, in 1.3.2's tokenizer section * wip: return_dict and to_str_tokens notes master-only (FILTERS: ~); drop the inline note from part34 utils.py * wip: [4.3] Sentence token boundaries from the model input's offset mapping; BOS for the R1-Distill tokenizers (split from #566) * wip: [4.4] Hooks accept a tuple or a tensor; apply_chat_template(return_dict=False) (split from #566) * wip: move the [4.3] and [4.4] changes to their own PRs * wip: get_sentence_char_boundaries gets an Args/Returns docstring like its neighbours * wip: 4.4 notes master-only (FILTERS: ~), wording covers Qwen2 (tensor already on 4.57) and Gemma 2 (tensor from 5) * Move the [4.4] hook / return_dict changes out to #570, which now builds on this PR #570 is rewritten for transformers>=5 only (plain-tensor layer outputs), so it should merge right after this PR. This PR keeps only the TransformerLens 3.9 pin line in master_4_4.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Drop the [4.3] sentence-boundary changes (#568) from this PR; #568 stays separate 4.3 keeps main's code plus the TransformerLens 3.9 pin. On transformers 5 the R1-Distill tokenizers then add no BOS token (main's boundary code happens to line up without one); #568 is the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HqBn5Uf3EY6Ddvxii1Drg7 --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
BarryTheShen
pushed a commit
to BarryTheShen/ARENA_materials
that referenced
this pull request
Nov 25, 2026
…es tensor; apply_chat_template(return_dict=False) (merge right after #563) (#570) * Bump TransformerLens to 3.9 (minimal: keep HookedTransformer) TL<3 pinned numpy<2, which made requirements.txt unresolvable with jax (#358) and broke Colab installs (#388, #419). TL 3.9 still ships HookedTransformer (deprecated) and allows numpy 2, but requires transformers>=5.9, so this also adapts the code that transformers 5 changed: - tokenizer.encode prepends BOS: ioi_dataset add_special_tokens=False, [1.1] sampler uses a plain GPT2TokenizerFast, [2.4] tests prepend_bos=True - batch_decode on 1D input: unsqueeze ([1.3.2], [4.1]) - apply_chat_template(tokenize=True) returns a dict: return_dict=False ([1.3.4], [4.4]) - decoder layers may return a tensor instead of a tuple ([4.4]) - [1.4.2] to_str_tokens(tokens) (TL 3.9 crashes on strings for Gemma); circuit-tracer from ARENA's fork (upstream needs huggingface_hub<1) sae-vis and CircuitsVis come from ARENA fork tags (PyPI sae-vis is uninstallable; Callum's CircuitsVis pins numpy<2). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Follow-ups from the audit: sae-lens>=6.51.3, 1.1 sampler tokenizer, 1.3.4 Colab line, comments - sae-lens>=6.51.3 (requirements and Colab lines): 6.51.0-6.51.2 accept TL 3.9 but HookedSAETransformer.from_pretrained("attn-only-2l-demo") fails on them ('TransformerBlock' object has no attribute 'mlp'), and pip won't upgrade an already-installed 6.51.2 that satisfies >=6.51. - [1.1] The training loop's and sampling_fn's samplers use a plain GPT-2 tokenizer, as the sampling section already does; transformers 5 makes reference_gpt2.tokenizer prepend <|endoftext|> in encode. - [1.3.4] The Colab cell installed transformer_lens (unused in 1.3.4) to detect Colab, which would upgrade Colab's transformers to 5.x. Detect and install peft / einops / jaxtyping / openai / python-dotenv instead. - Comments carried over from the TL4 port said "TL4" / "required by TransformerLens 4"; they now refer to TransformerLens 3.9. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Cap transformers<6; note that the attention_heads resizing bug is fixed From #554: - requirements.txt: transformers>=5.9,<6 (sae-lens 6.51.3 already requires <6; this makes the cap explicit). - [1.1] Un-comment the "attention_heads plots are behaving weirdly" dropdown and drop the "may exhibit a bug in VSCode" sentence; [1.1] and [1.4.1] note that the bug was fixed upstream (CircuitsVis PR #63). The ARENA CircuitsVis fork tag we install (arena-tl4.1, frontend 1.41.0) includes that fix (cc21677 pointed the package at the fixed 1.40.1 build). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Sampling: keep the BOS token that GPT-2 prompts should always have had TransformerLens configures GPT-2's tokenizer with add_bos_token=True, and model.to_tokens() has always prepended <|endoftext|>. transformers < 5 silently ignored add_bos_token in tokenizer.encode(), so the 1.1 sampling section fed GPT-2 prompts without the BOS token the rest of the course uses. transformers 5 (required by TransformerLens 3.9) honours it, so encode(prompt) now matches to_tokens(prompt). Treat that as the fix rather than working around it (reverts #450's swap to a plain GPT2TokenizerFast): - Use reference_gpt2.tokenizer again for the sampler. - Decode with skip_special_tokens=True so outputs don't start with <|endoftext|>; add a hint explaining the BOS token to the sampler exercise. - Re-measure the hard-coded expectations with BOS, on TransformerLens 3.9 / transformers 5.17: greedy "Jingle bells" ("...all the way down to...", was "up"), the basic/top-k top-5 table (church .0466, hospital .0223, store .0181, house .0181, doctor .0124) and the top_p=0.1 set (now 4 tokens, cumulative .105; the old 2-token comment no longer held). - Unsqueeze the 1D batch_decode in the "most likely next tokens" cell: transformers 5 decodes a flat 1D input as one string, which the cell then zipped against tokens character by character. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Beam search: re-measure the generate/filter expectations with BOS Same cause as the previous commit: the beam-search unit tests hard-coded logprob sums and completions measured without the BOS token. Re-measured on TransformerLens 3.9 / transformers 5.17 (generate: a kid -2.177, a child -3.803, a teenager -4.178; filter(3): a kid, growing up -2.675, a child), and regenerated the three pre-rendered "Best completions" tables with rich (byte-identical format; checked by re-rendering the old table). The prose no longer says the output should match the diagram, which was drawn from the BOS-less numbers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * Move the attention_heads note out of the port (now #562) The CircuitsVis "attention_heads resizing bug is fixed" note doesn't depend on TransformerLens 3.9 (main's CircuitsVis already has the fix), so it has its own PR, #562. Keep this branch to what the upgrade needs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [4.3] BOS at tokenizer load + sentence boundaries from the model input's offsets (transformers >= 5) Replaces #474 + #518 for the transformers >= 5 world (stacked on the TransformerLens 3.9 port). - Load the DeepSeek-R1-Distill tokenizers with add_bos_token=True and assert they add a BOS. transformers loads these checkpoints as Qwen2Tokenizer / TokenizersBackend (huggingface/transformers#45741, v5.8.0), which never add a BOS, although the repos set add_bos_token=true and the model expects one. - get_sentence_token_boundaries: take the offset mapping of the model input tokenizer(text) itself, so the BOS (offsets (0, 0)) is accounted for without a manual shift. A sentence covers every token overlapping its characters (bisect over token starts/ends), which also handles characters like "≈" that byte-level BPE splits across several tokens (char_to_token would drop the second token) and "." merged with the following "\n\n". - get_sentence_char_boundaries (from #518): map sentences found in the whitespace-normalised text back to positions in the original text, and use it for text_subset. Regenerated attention-suppression output from #518 (identical model inputs). Checked on 20 dataset problems (transformers 5.17.0): exact decode 2952/2952, 0 overlaps, 0 empty spans, boundaries identical to #518 for 20/20; #518's synthetic edge cases decode identically. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * wip: split transformers-5-compatible changes out of #563 (no-ops on transformers 4) * wip: drop round-tripped master .ipynb (CI regenerates them) * wip: 2.4 test comment states the measured cause * wip: 1.3.4 Colab cell detects jaxtyping (Colab preinstalls peft, so 'import peft' would skip the install) * wip: 4.3 suppression dropdown re-measured on transformers 4.57.6 (matches 5.18.0 to 3 s.f.) * Align with #566: wording, 4.3 regex escapes, 1.3.4 Colab check, re-measured 4.3 output The hunks #563 shares with #566 now have identical text, so the two PRs merge in either order. - Comments on lines that also work on transformers 4 say "in transformers>=5", not "required by TransformerLens 3.9". - [4.3] normalize_spaces: restore the \u00A0...\uFEFF escapes (the port had replaced them with the literal invisible characters). - [4.3] Suppression cell's expected output, re-measured (the pasted 634x was never run on this branch): sentence 66 vs 54, KL ratio 3.0x; the same on transformers 4.57.6 and 5.18.0. - [1.3.4] Colab cell detects jaxtyping, not peft (Colab preinstalls peft, so the install would be skipped). - [2.4] test_transformer_with_value_head comment states the measured cause (TransformerLens 3.9 sets default_prepend_bos=False for Pythia). Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [1.3.2] top-logits cell: unsqueeze(-1) instead of a list comprehension * wip: batch_decode notes are master-only (FILTERS: ~); students see one explanation, in 1.3.2's tokenizer section * wip: return_dict and to_str_tokens notes master-only (FILTERS: ~); drop the inline note from part34 utils.py * wip: [4.3] Sentence token boundaries from the model input's offset mapping; BOS for the R1-Distill tokenizers (split from #566) * wip: [4.4] Hooks accept a tuple or a tensor; apply_chat_template(return_dict=False) (split from #566) * wip: move the [4.3] and [4.4] changes to their own PRs * wip: get_sentence_char_boundaries gets an Args/Returns docstring like its neighbours * wip: 4.4 notes master-only (FILTERS: ~), wording covers Qwen2 (tensor already on 4.57) and Gemma 2 (tensor from 5) * Move the [4.4] hook / return_dict changes out to #570, which now builds on this PR #570 is rewritten for transformers>=5 only (plain-tensor layer outputs), so it should merge right after this PR. This PR keeps only the TransformerLens 3.9 pin line in master_4_4.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [4.4] rewrite for transformers>=5: decoder-layer hooks take and return the hidden-states tensor; ActivationSteerer/ActivationCapper simplified to match; return_dict=False * Drop the [4.3] sentence-boundary changes (#568) from this PR; #568 stays separate 4.3 keeps main's code plus the TransformerLens 3.9 pin. On transformers 5 the R1-Distill tokenizers then add no BOS token (main's boundary code happens to line up without one); #568 is the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HqBn5Uf3EY6Ddvxii1Drg7 --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
BarryTheShen
pushed a commit
to BarryTheShen/ARENA_materials
that referenced
this pull request
Nov 25, 2026
…`.output[0]` on GPT-2 (after #563) (#573) * Bump TransformerLens to 3.9 (minimal: keep HookedTransformer) TL<3 pinned numpy<2, which made requirements.txt unresolvable with jax (#358) and broke Colab installs (#388, #419). TL 3.9 still ships HookedTransformer (deprecated) and allows numpy 2, but requires transformers>=5.9, so this also adapts the code that transformers 5 changed: - tokenizer.encode prepends BOS: ioi_dataset add_special_tokens=False, [1.1] sampler uses a plain GPT2TokenizerFast, [2.4] tests prepend_bos=True - batch_decode on 1D input: unsqueeze ([1.3.2], [4.1]) - apply_chat_template(tokenize=True) returns a dict: return_dict=False ([1.3.4], [4.4]) - decoder layers may return a tensor instead of a tuple ([4.4]) - [1.4.2] to_str_tokens(tokens) (TL 3.9 crashes on strings for Gemma); circuit-tracer from ARENA's fork (upstream needs huggingface_hub<1) sae-vis and CircuitsVis come from ARENA fork tags (PyPI sae-vis is uninstallable; Callum's CircuitsVis pins numpy<2). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Follow-ups from the audit: sae-lens>=6.51.3, 1.1 sampler tokenizer, 1.3.4 Colab line, comments - sae-lens>=6.51.3 (requirements and Colab lines): 6.51.0-6.51.2 accept TL 3.9 but HookedSAETransformer.from_pretrained("attn-only-2l-demo") fails on them ('TransformerBlock' object has no attribute 'mlp'), and pip won't upgrade an already-installed 6.51.2 that satisfies >=6.51. - [1.1] The training loop's and sampling_fn's samplers use a plain GPT-2 tokenizer, as the sampling section already does; transformers 5 makes reference_gpt2.tokenizer prepend <|endoftext|> in encode. - [1.3.4] The Colab cell installed transformer_lens (unused in 1.3.4) to detect Colab, which would upgrade Colab's transformers to 5.x. Detect and install peft / einops / jaxtyping / openai / python-dotenv instead. - Comments carried over from the TL4 port said "TL4" / "required by TransformerLens 4"; they now refer to TransformerLens 3.9. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012k9oKarCJM76vcjVpKdhGG * Cap transformers<6; note that the attention_heads resizing bug is fixed From #554: - requirements.txt: transformers>=5.9,<6 (sae-lens 6.51.3 already requires <6; this makes the cap explicit). - [1.1] Un-comment the "attention_heads plots are behaving weirdly" dropdown and drop the "may exhibit a bug in VSCode" sentence; [1.1] and [1.4.1] note that the bug was fixed upstream (CircuitsVis PR #63). The ARENA CircuitsVis fork tag we install (arena-tl4.1, frontend 1.41.0) includes that fix (cc21677 pointed the package at the fixed 1.40.1 build). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Sampling: keep the BOS token that GPT-2 prompts should always have had TransformerLens configures GPT-2's tokenizer with add_bos_token=True, and model.to_tokens() has always prepended <|endoftext|>. transformers < 5 silently ignored add_bos_token in tokenizer.encode(), so the 1.1 sampling section fed GPT-2 prompts without the BOS token the rest of the course uses. transformers 5 (required by TransformerLens 3.9) honours it, so encode(prompt) now matches to_tokens(prompt). Treat that as the fix rather than working around it (reverts #450's swap to a plain GPT2TokenizerFast): - Use reference_gpt2.tokenizer again for the sampler. - Decode with skip_special_tokens=True so outputs don't start with <|endoftext|>; add a hint explaining the BOS token to the sampler exercise. - Re-measure the hard-coded expectations with BOS, on TransformerLens 3.9 / transformers 5.17: greedy "Jingle bells" ("...all the way down to...", was "up"), the basic/top-k top-5 table (church .0466, hospital .0223, store .0181, house .0181, doctor .0124) and the top_p=0.1 set (now 4 tokens, cumulative .105; the old 2-token comment no longer held). - Unsqueeze the 1D batch_decode in the "most likely next tokens" cell: transformers 5 decodes a flat 1D input as one string, which the cell then zipped against tokens character by character. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [1.1] Beam search: re-measure the generate/filter expectations with BOS Same cause as the previous commit: the beam-search unit tests hard-coded logprob sums and completions measured without the BOS token. Re-measured on TransformerLens 3.9 / transformers 5.17 (generate: a kid -2.177, a child -3.803, a teenager -4.178; filter(3): a kid, growing up -2.675, a child), and regenerated the three pre-rendered "Best completions" tables with rich (byte-identical format; checked by re-rendering the old table). The prose no longer says the output should match the diagram, which was drawn from the BOS-less numbers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * Move the attention_heads note out of the port (now #562) The CircuitsVis "attention_heads resizing bug is fixed" note doesn't depend on TransformerLens 3.9 (main's CircuitsVis already has the fix), so it has its own PR, #562. Keep this branch to what the upgrade needs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * [4.3] BOS at tokenizer load + sentence boundaries from the model input's offsets (transformers >= 5) Replaces #474 + #518 for the transformers >= 5 world (stacked on the TransformerLens 3.9 port). - Load the DeepSeek-R1-Distill tokenizers with add_bos_token=True and assert they add a BOS. transformers loads these checkpoints as Qwen2Tokenizer / TokenizersBackend (huggingface/transformers#45741, v5.8.0), which never add a BOS, although the repos set add_bos_token=true and the model expects one. - get_sentence_token_boundaries: take the offset mapping of the model input tokenizer(text) itself, so the BOS (offsets (0, 0)) is accounted for without a manual shift. A sentence covers every token overlapping its characters (bisect over token starts/ends), which also handles characters like "≈" that byte-level BPE splits across several tokens (char_to_token would drop the second token) and "." merged with the following "\n\n". - get_sentence_char_boundaries (from #518): map sentences found in the whitespace-normalised text back to positions in the original text, and use it for text_subset. Regenerated attention-suppression output from #518 (identical model inputs). Checked on 20 dataset problems (transformers 5.17.0): exact decode 2952/2952, 0 overlaps, 0 empty spans, boundaries identical to #518 for 20/20; #518's synthetic edge cases decode identically. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKRVpag9HQFjzWJRT2QZaU * wip: split transformers-5-compatible changes out of #563 (no-ops on transformers 4) * wip: drop round-tripped master .ipynb (CI regenerates them) * wip: 2.4 test comment states the measured cause * wip: 1.3.4 Colab cell detects jaxtyping (Colab preinstalls peft, so 'import peft' would skip the install) * wip: 4.3 suppression dropdown re-measured on transformers 4.57.6 (matches 5.18.0 to 3 s.f.) * Align with #566: wording, 4.3 regex escapes, 1.3.4 Colab check, re-measured 4.3 output The hunks #563 shares with #566 now have identical text, so the two PRs merge in either order. - Comments on lines that also work on transformers 4 say "in transformers>=5", not "required by TransformerLens 3.9". - [4.3] normalize_spaces: restore the \u00A0...\uFEFF escapes (the port had replaced them with the literal invisible characters). - [4.3] Suppression cell's expected output, re-measured (the pasted 634x was never run on this branch): sentence 66 vs 54, KL ratio 3.0x; the same on transformers 4.57.6 and 5.18.0. - [1.3.4] Colab cell detects jaxtyping, not peft (Colab preinstalls peft, so the install would be skipped). - [2.4] test_transformer_with_value_head comment states the measured cause (TransformerLens 3.9 sets default_prepend_bos=False for Pythia). Co-Authored-By: Claude Opus 5.5 <[email protected]> * wip: [1.3.2] top-logits cell: unsqueeze(-1) instead of a list comprehension * wip: batch_decode notes are master-only (FILTERS: ~); students see one explanation, in 1.3.2's tokenizer section * wip: return_dict and to_str_tokens notes master-only (FILTERS: ~); drop the inline note from part34 utils.py * wip: [4.3] Sentence token boundaries from the model input's offset mapping; BOS for the R1-Distill tokenizers (split from #566) * wip: [4.4] Hooks accept a tuple or a tensor; apply_chat_template(return_dict=False) (split from #566) * wip: move the [4.3] and [4.4] changes to their own PRs * wip: get_sentence_char_boundaries gets an Args/Returns docstring like its neighbours * wip: 4.4 notes master-only (FILTERS: ~), wording covers Qwen2 (tensor already on 4.57) and Gemma 2 (tensor from 5) * Move the [4.4] hook / return_dict changes out to #570, which now builds on this PR #570 is rewritten for transformers>=5 only (plain-tensor layer outputs), so it should merge right after this PR. This PR keeps only the TransformerLens 3.9 pin line in master_4_4.py. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Drop the [4.3] sentence-boundary changes (#568) from this PR; #568 stays separate 4.3 keeps main's code plus the TransformerLens 3.9 pin. On transformers 5 the R1-Distill tokenizers then add no BOS token (main's boundary code happens to line up without one); #568 is the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HqBn5Uf3EY6Ddvxii1Drg7 * [1.3.2] Test that steering changes every steered completion (catches `.output[0]` on GPT-2) On transformers 5, GPT-2 blocks return the hidden-states tensor rather than a tuple, so the `.output[0]` idiom students used for GPT-J silently selects batch element 0 and the steering does nothing, with no error. `calculate_and_apply_steering_vector` had no test. `test_calculate_and_apply_steering_vector` switches the student's `SAMPLING_KWARGS` to greedy decoding for one short run (restored afterwards) with `n_comparisons=2`, and asserts every steered completion differs from its unsteered twin. Two rows are needed: with one, the buggy `+=` still perturbs the (wrong) slice and changes the output. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [1.3.2] Keep the GPT-2 `.output` (no `[0]`) code comment in the master only Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
deepseek r1 distilled tokenizer fix for qwen2 mapping