Skip to content

Fix error opening jupyter notebook#1436

Closed
danra wants to merge 44 commits into
TransformerLensOrg:mainfrom
danra:fix_jupyter
Closed

Fix error opening jupyter notebook#1436
danra wants to merge 44 commits into
TransformerLensOrg:mainfrom
danra:fix_jupyter

Conversation

@danra

@danra danra commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Description

uv run jupyter notebook demos/xxx.ipynb currently fails with:

   ModuleNotFoundError: No module named 'jupyter_server.contents'

This was fixed in jupyer 6.5.6 (see jupyter/notebook#7048)

The PR updates the locked jupyter notebook version from v6.5.4->v6.5.7, resolving the issue.
A couple of minor drive-by fixes are also applied.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

danra and others added 30 commits June 8, 2026 09:14
…ensOrg#1316)

* Add Direct Logit Attribution tool for TransformerBridge

* Resolve review feedback and add Direct Logit Attribution tests

Resolved review feedback from @jlarson4, added tests covering
reconstruction invariants on a distilgpt2 bridge in compatibility mode,
arguments, asserting sum(scores) == logit_diff - (b_U[correct] -
b_U[wrong]) against the model's real logits, plus labels/shape and
batch-averaging checks.

Added additional hardening:
- Fix a latent direction-shape bug: replace the fragile
  answer_tokens.numel()==1 branch with a robust reshape so single-prompt,
  single-token inputs are handled correctly
- Detect hybrid blocks via bridge.layer_types() instead of substring
  matching named_modules(), the codebase's own semantic mechanism
- Import get_act_name from transformer_lens.utilities to avoid the
  transformer_lens.utils DeprecationWarning; drop the invalid
  return_type kwarg to run_with_cache
- Register the analysis subpackage in tools/__init__.py

Closes TransformerLensOrg#1263.
…merLensOrg#1369)

* Add Direct Logit Attribution tool (TransformerLensOrg#1263)

Add transformer_lens/tools/analysis/direct_logit_attribution.py, a single-call
DLA analysis that decomposes a logit (or logit difference) into per-component,
per-layer (logit-lens), or per-head contributions. Wraps the existing
ActivationCache primitives (decompose_resid / accumulated_resid /
stack_head_results / logit_attrs) and works with both HookedTransformer and
TransformerBridge, since they share the cache API.

Returns a DirectLogitAttribution dataclass (attribution tensor + aligned
labels, plus a top(k) helper). Adds integration tests asserting the exact DLA
correctness invariant on both systems: the complete decomposition reconstructs
the model's real logit up to the unembedding bias b_U.

Closes TransformerLensOrg#1263

* Resolving conflicts between 1316 and 1369

* format fixes

---------

Co-authored-by: Azra Bano <azrabano23@gmail.com>
Co-authored-by: Jonah Larson <jonahalarson@comcast.net>
* Add Phi adapter tests

* Add comment about setup component test

* Delete redundant config literal tests
* Fixed SVD interpreter test

* Format SVD interpreter fixture test
The Restricted Loss section called loss_fn(all_logits, labels), but
all_logits had been rearranged earlier into a (p, p, d_vocab) grid for
the logit periodicity analysis. loss_fn's 3-D branch assumes
(batch, pos, d_vocab) and takes logits[:, -1], producing a (p, p)
tensor that crashes the gather against the p*p labels (TransformerLensOrg#543).

Use original_logits instead, which is recomputed just above and is the
same full-dataset loss the cell intends to print. Also clear the stored
RuntimeError output from the cell.
Breaking: removes the public eps_attr constructor argument and the config.eps_attr attribute. The field was never read (its consumer was deleted when NormalizationBridge moved to direct HF delegation), so no model behavior changes, but it is an API removal.
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
* Add Olmo2 architecture adapter tests

* Drop test_attn_output_shape per the unit-test guide (shared bridge contract)
* Fixed SVD interpreter test

* Format SVD interpreter fixture test

* Add qwen adapter unit test

* Retrigger CI (unrelated HF 429 error failing the build)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
…ransformerLensOrg#1390)

The adapter already conditionally omitted ln2 from the block submodules
  when use_parallel_residual=True, but still wrapped them in a plain
  BlockBridge, which rejects the attn+mlp+no-ln2 shape. Switched to
  conditional block_cls (ParallelBlockBridge for the parallel branch,
  BlockBridge for sequential), mirroring the dual-mode pattern in
  falcon.py.
26 tests covering: component mapping (slots, bridge types, HF paths,
submodule structure), anti-drift config flags (final_rms, uses_rms_norm,
gated_mlp), weight conversion key set and rearrange patterns, GQA
propagation to K/V only, and setup_component_testing rotary-emb wiring.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: Fix broken line graphs

- Fixes incorrect induction loss graph at end of notebook (values were incorrect, loss appeared to be going up with training instead of down!)
- Fixes x/y axes showing "index"/"value" instead of configured names

* fix: broken/outdated links

* doc: Reference ARENA chapter directly instead of an older equivalent that forwards there

* chore: Align text in code with text in markdown

* doc: Update texts on supported models and architectures

- Updated counts
- Updated link to list of all models supported on v3
- "HookedTransformed.from_pretrained" instances -> "TransformerBridge.boot_transformers", the up-to-date recommended method which makes the wider variety accessible
- "consistent(-ish) architecture" -> "consistent(-ish) interface": with v3, the consistent interface proxies the non-consistent underlying architectures
- "Transformer architecture" title -> "HookedTransformer architecture", mention deprecation
- Mention overview of models last updated in 2023
- Drop stale reference to phantom "table for hyper-parameters"

* doc: Add note on boot_transformers incompatibility with checkpoints

* doc: Document purpose of enabling compatibility mode

* doc: Drop minor comment on default_prepend_bos, only relevant for legacy HookedTransformer

* doc: Remove stale gotcha, hooks are now properly removed even in case of an error in a hook

* fix: Mismatching values in code vs. descriptions

* chore: Replace deprecated circuitvis attention visualization with newer one

* chore: Replace deprecated model names aliases

* chore: Remove deprecated prepend_bos argument

* chore: Update hook name to v3

* chore: Full OV graph title no longer the same as the preceding very similar OV graph

* doc: Clearer spaces in tokens

* doc: Bracket tokens styled as code to prevent Colab from collapsing spaces

Previously Colab showed both tokens as identical, squashing the space

* chore: Try the model out right after calls to do so

* doc: approximate number can be approximate (consistent with another preceding one)

* chore: Remove installing old node version which was the newest at the time

Now actually gives a deprecation warning and waits for 10 seconds

* chore: Remove leftover cell

* chore: fix bad closing tag

* doc: fix typos

* doc: More precise wording, setting a value, not adding it

* doc: move floating function names into complete sentence
25 tests across 4 classes covering component mapping, config flags,
weight conversions, and GQA head-count propagation.

- TestMistralComponentMapping (12 tests): top-level keys, bridge types,
  HF module paths, block submodules, attn flags, QKVO paths, MLP paths.
  Includes explicit guard that attn uses AttentionBridge, not
  PositionEmbeddingsAttentionBridge.
- TestMistralAdapterConfig (4 tests): final_rms=False, uses_rms_norm,
  gated_mlp, attn_only — anti-drift flags.
- TestMistralWeightConversions (5 tests): exactly 4 QKVO weight keys,
  split-heads and merge-heads rearrange patterns, no bias/norm entries.
- TestMistralGQASupport (4 tests): K/V use n_key_value_heads, Q/O
  unchanged, fallback to n_heads when n_key_value_heads is unset.
…ansformerLensOrg#1399)

Adds focused test suites for three architecture adapters per the
proposal in issue TransformerLensOrg#1302.

tests/unit/model_bridge/supported_architectures/test_phi3_adapter.py
- Component mapping structure (bridge types and HF module paths)
- Weight conversion key set and source keys for fused qkv/gate_up
- _SizedSplitConversion numerical correctness (Q/K/V GQA splits)
- Config flags (RMS norm, rotary, gated MLP, supports_fold_ln=False)
- preprocess_weights LN folding into QKV and gate/up projections

tests/unit/model_bridge/supported_architectures/test_granite_adapter.py
- Component mapping for dense GraniteArchitectureAdapter
- Weight conversion key set (standard QKVO rearrangements)
- Config flags (final_rms=True, default_prepend_bos=False, GQA heads)
- GraniteMoeArchitectureAdapter: MoE bridge replaces dense MLP,
  all other components and config flags match dense Granite

Closes part of TransformerLensOrg#1302.
* chore: Plot helper allows customizing graph before showing it

* feat: Direct path patching in exploratory analysis demo, resolves TransformerLensOrg#111

* doc: fix head index in prose
* chore: Rename Llama demo notebook to match TransformerLensOrg#1233 changes

* chore: fix title in Llama demo notebook to match TransformerLensOrg#1233 changes
…TransformerLensOrg#1396)

* feat: add get_act_patch_direct_path for head-to-head circuit analysis

Closes TransformerLensOrg#111.

Implements direct path patching — a finer-grained variant of activation
patching that isolates the direct information flow between two specific
attention heads, rather than replacing the full residual stream.

Why
---
Standard activation patching tells you that *some* component at layer L
matters, but it cannot distinguish whether head B at layer L+2 matters
because it received information directly from head A, or because A's
output propagated through many intermediate components first. Direct path
patching isolates the A → B causal edge precisely.

Implementation
--------------
For a fixed source head A = (src_layer, src_head) and every downstream
destination head B = (dst_layer, dst_head):

  delta_resid  = clean_A_result - corrupted_A_result    # [batch, pos, d_model]
  delta_B_q    = (delta_resid / ln1_scale) @ W_Q[hb]  # [batch, pos, d_head]
  patched_B_q  = corrupted_B_q + delta_B_q

The per-head residual contribution is computed from hook_z @ W_O (always
available in the default cache) rather than hook_result, which requires
the non-default cfg.use_hook_result=True flag.

New files
---------
- transformer_lens/direct_path_patching.py
    get_act_patch_direct_path()          [n_layers, n_heads] sweep
    get_act_patch_direct_path_all_sources()  [n_layers, n_heads, n_layers, n_heads] full sweep

- tests/unit/test_direct_path_patching.py
    12 tests covering output shape, causal structure, manual correctness
    verification, and edge cases. All pass on a tiny randomly-initialised
    3-layer model (no downloads, runs in ~3s on CPU).

- demos/direct_path_patching_ioi.py
    Validated on GPT-2 small / IOI task. S-inhibition heads (7.3, 7.9,
    8.6, 8.10) show strongest direct paths into name-mover heads (9.9,
    9.6, 10.0), confirming the Wang et al. 2022 IOI circuit.

    (8,6) → (9,9):  +0.083 normalised logit diff
    (8,10) → (9,9): +0.066
    (7,9) → (9,9):  +0.036

API matches existing get_act_patch_* functions in patching.py for
drop-in use alongside the existing circuit analysis toolkit.

* style: apply black + isort formatting (line-length=100)

* fix: remove unused imports, add type: ignore for mypy, clean up demo import

* fix: add type: ignore[index] on W_O[h] indexing for mypy

* fix: address reviewer feedback — TransformerBridge support, fold_ln guard, independent test, notebook demo

* fix: check fold_ln for TransformerBridge via .weight attribute

TransformerBridge wraps the original HuggingFace LayerNorm module, which
stores the learned scale as .weight rather than the .w used by
HookedTransformer.  Fall back to .weight so the guard actually fires when
a TransformerBridge model is passed without folded LN, rather than silently
skipping the check.

* refactor: move to tools/analysis/, logit-diff metric, TestCheckFoldLn, fix _check_fold_ln tensor bug

- Move direct_path_patching.py to transformer_lens/tools/analysis/ alongside
  the Direct Logit Attribution tool; add tools/analysis/__init__.py exporting
  both public functions; update transformer_lens/__init__.py accordingly.

- Fix _check_fold_ln: replace 'getattr(...) or getattr(...)' with explicit
  None checks to avoid RuntimeError on multi-element tensors.

- test_correctness_against_actual_ln_forward: switch patching metric to
  logit diff (correct_tok - incorrect_tok), which cancels the centering
  offset introduced by process_weights_() and tightens tolerance 0.15 -> 1e-3.

- Add TestCheckFoldLn (5 tests): folded model no-warning, unfolded model
  warns, pre-fold .w attribute present, no crash on missing attribute,
  no RuntimeError on multi-element tensor regression check.

All 17 tests pass.

* fix: loosen _check_fold_ln type hint to Any for beartype compatibility

_check_fold_ln is a private defensive helper with a try/except that
handles arbitrary model types. The Union[HookedTransformer, TransformerBridge]
annotation was causing beartype to reject valid test fixtures (and any
non-standard model) at the call boundary before the function's own
exception handling could run. Any is the correct annotation for a
function intentionally designed to tolerate unknown model shapes.

* style: black formatting on test_direct_path_patching.py
RecreationalMath and others added 14 commits June 22, 2026 09:54
* Add unit tests for MistralArchitectureAdapter

25 tests across 4 classes covering component mapping, config flags,
weight conversions, and GQA head-count propagation.

- TestMistralComponentMapping (12 tests): top-level keys, bridge types,
  HF module paths, block submodules, attn flags, QKVO paths, MLP paths.
  Includes explicit guard that attn uses AttentionBridge, not
  PositionEmbeddingsAttentionBridge.
- TestMistralAdapterConfig (4 tests): final_rms=False, uses_rms_norm,
  gated_mlp, attn_only — anti-drift flags.
- TestMistralWeightConversions (5 tests): exactly 4 QKVO weight keys,
  split-heads and merge-heads rearrange patterns, no bias/norm entries.
- TestMistralGQASupport (4 tests): K/V use n_key_value_heads, Q/O
  unchanged, fallback to n_heads when n_key_value_heads is unset.

* auto-checkpoint: before editing test_bert_adapter.py

* auto-checkpoint: before editing test_falcon_adapter.py

* test: add adapter unit tests for Falcon and BERT

* style: fix isort import formatting in BERT adapter tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
… handle (TransformerLensOrg#1426)

HookPoint.add_hook returns None, but the benchmark helpers stored its
return value as a "handle" (silencing mypy with
behind an `if handle is not None` guard). Since the value was always None,
the guard was always false and the capture hooks were never removed.

Track the HookPoint that was registered on and clean up via
hook_point.remove_hooks() (dir="bwd" for backward hooks), matching the
existing idiom in TransformerBridge.run_with_cache.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
…ransformerLensOrg#1432)

* test(hubert): add unit adapter tests for HubertArchitectureAdapter

Covers component mapping (bare HubertModel + HubertForCTC prefix
rebinding), bridge types and HF module paths, post-LN vs pre-LN
supports_fold_ln tied to do_stable_layer_norm, weight conversion key
set and rearrange patterns, and prepare_loading/prepare_model branches.

Part of TransformerLensOrg#1302.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* style: fix black/isort formatting in test_hubert_adapter.py

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
* tidy: Remove reference to deleted additional comments (TransformerLensOrg#1410)

Simple resolution for TransformerLensOrg#1409

* Add GLM-4 MoE TransformerBridge adapter

* Ignore BERT import cell output in nbval

---------

Co-authored-by: Dan Raviv <dan.raviv@gmail.com>
Co-authored-by: jlarson4 <jonahalarson@comcast.net>
…sformerLensOrg#1408)

* feat: add DeepSeek-V2 architecture adapter (MLA, V2/V2-Lite, complex RoPE)

Closes TransformerLensOrg#1400.

DeepSeek-V2, V2-Lite, and Coder-V2 all use DeepseekV2ForCausalLM.
This adds a bridge adapter covering three V2-specific differences
from V3:

1. Complex-exponential RoPE: V2's rotary embedding returns freqs_cis
   (a complex tensor via torch.polar) rather than a (cos, sin) tuple.
   - RotaryEmbeddingBridge.forward() now passes complex tensors through
     without raising, leaving them for the attention bridge to consume.
   - MLAAttentionBridge.forward() detects complex position_embeddings
     and dispatches to a new _apply_rotary_complex() helper that mirrors
     DeepSeek-V2's apply_rotary_emb (view_as_complex, multiply, flatten).

2. Optional Q LoRA path: V2-Lite sets q_lora_rank=None, skipping
   q_a_proj/q_a_layernorm/q_b_proj and using q_proj directly instead.
   All three Q-path submodules are marked optional=True in the adapter;
   q_a_layernorm uses GeneralizedComponent (which already supports
   optional) rather than RMSNormalizationBridge. MLAAttentionBridge
   already branches on q_lora_rank at runtime.

3. Gate not hookable: DeepseekV2Moe.forward() routes via
   nn.functional.linear(..., self.gate.weight) rather than
   self.gate(hidden_states), so the gate module's forward() is never
   called and bridge hooks cannot fire. The gate is omitted from
   MoEBridge submodules; shared_experts uses __call__ and hooks fine.

Files changed:
- supported_architectures/deepseek_v2.py (new)
- supported_architectures/__init__.py: register adapter
- factories/architecture_adapter_factory.py: map DeepseekV2ForCausalLM
- generalized_components/mla_attention.py: complex RoPE support
- generalized_components/rotary_embedding.py: complex tensor pass-through
- tests/integration/model_bridge/test_deepseek_v2_adapter.py (new, 17 tests)

* style: fix formatting and mypy errors

* fix: register DeepseekV2ForCausalLM in model registry
TL;DR: add TransformerBridge support for LiquidAI LFM2 MoE and Microsoft PhiMoE, including registry wiring, config passthrough, unit coverage, native PhiMoE loading, and correct chat EOS stopping.

Adds native architecture adapters for Lfm2MoeForCausalLM and PhiMoEForCausalLM. LFM2 is exposed conservatively as a residual-probing adapter over the HF decoder layers, avoiding fake attention/MLP internals for the hybrid conv/full-attention architecture. PhiMoE targets the native Transformers implementation with trust_remote_code=False rather than the archived remote implementation, which is incompatible with current generation/cache semantics.

Registers both architectures across the TransformerBridge adapter factory, supported-architecture exports, model-registry architecture lists, report metadata, and supported_models.json. Adds explicit registry entries for LiquidAI/LFM2.5-8B-A1B and microsoft/Phi-mini-MoE-instruct.

Extends HF config passthrough for hybrid/MoE and tokenizer-related attributes used by these adapters, including LFM2 layer/norm/MoE metadata, PhiMoE routing/bias metadata, and eos_token_id. TransformerBridge generation now prefers cfg.eos_token_id when present, allowing architecture adapters to provide multiple default stop tokens.

Handles PhiMoE chat generation correctly by stopping on both the tokenizer EOS (<|endoftext|>) and the chat-template turn terminator (<|end|>). This prevents generation from continuing into another assistant turn after the model has ended its answer.

Adds unit tests covering both adapters' config flags, component mappings, weight conversions, and PhiMoE EOS defaults.
By updating jupyter notebook in lockfile (v6.5.4->v6.5.7)

Previously, `uv run jupyter notebook demos/xxx.ipynb` failed with:

   ModuleNotFoundError: No module named 'jupyter_server.contents'

This was fixed in jupyer 6.5.6 (see jupyter/notebook#7048)
@danra danra closed this Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.