Skip to content

Add clean-coordinate clamping for JacobianLens swaps - #1747

Open
koriyoshi2041 wants to merge 6 commits into
TransformerLensOrg:devfrom
koriyoshi2041:fix/jacobian-lens-swap-clamp-dev
Open

Add clean-coordinate clamping for JacobianLens swaps#1747
koriyoshi2041 wants to merge 6 commits into
TransformerLensOrg:devfrom
koriyoshi2041:fix/jacobian-lens-swap-clamp-dev

Conversation

@koriyoshi2041

Copy link
Copy Markdown
Contributor

Description

Adds JacobianLens.swap_clamp_hooks, which fixes each hooked layer's source/target lens coordinates to the exchanged values from a clean-run cache. This implements the multi-layer clamp form without changing the existing live-coordinate semantics of swap_hooks.

Also documents that repeated live swaps are involutive and can cancel across a layer band.

Fixes #1746

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Screenshots

Not applicable; numerical evidence is attached to #1746.

Validation

  • make format
  • uv run pytest tests/unit/tools/test_jacobian_lens.py -q (93 passed)
  • uv run mypy transformer_lens/tools/analysis/jacobian_lens.py
  • git diff --check

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing relevant unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for documenting and putting together a fix for this @koriyoshi2041. The parity mechanism reproduces as described, and the clamp holds at every band width. Just a couple small comments below on some edge cases that were missed by the CI

Comment thread transformer_lens/tools/analysis/jacobian_lens.py Outdated
Comment thread transformer_lens/tools/analysis/jacobian_lens.py
Comment thread transformer_lens/tools/analysis/jacobian_lens.py Outdated
Comment thread tests/unit/tools/test_jacobian_lens.py
Comment thread demos/Jacobian_Lens_Demo.ipynb Outdated
Comment thread transformer_lens/tools/analysis/jacobian_lens.py Outdated
koriyoshi2041 and others added 2 commits September 9, 2026 15:42
- accept either an ActivationCache or the plain dict from
  run_with_cache(return_cache_object=False) in swap_clamp_hooks, and
  say so in the docstring
- add a unit test that passes the raw cache dict through the clamp
- move the demo's rank cell onto swap_clamp_hooks via a shared
  clamped_logits helper, reword the sweep intro so the live swap is no
  longer called a clamp, drop the stale swap name from the cleanup cell
- re-execute the notebook end to end with the pinned model and lens
  revisions so the stored outputs match the code, and update the
  section 3 and 4 prose to this run's numbers
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.

JacobianLens.swap_hooks re-reads live coordinates, causing odd/even cancellation across layer bands

2 participants