Skip to content

Conversation

@HetanshWaghela
Copy link

Description

Implements Google's Learning Interpretability Tool (LIT) integration for TransformerLens, allowing interactive model analysis and visualization.

Features:

  • HookedTransformerLIT wrapper implementing the full LIT Model API
  • Interactive attention pattern visualization across all layers/heads
  • Token salience/gradient-based importance scores
  • Embedding projector for layer-wise representations
  • Built-in datasets: IOI (Indirect Object Identification), Induction, SimpleText, PromptCompletion
  • serve() function to launch LIT server
  • Demo notebook with complete examples
  • 37 unit tests (all passing)

Usage:

from transformer_lens import HookedTransformer
from transformer_lens.lit import HookedTransformerLIT, SimpleTextDataset, serve

model = HookedTransformer.from_pretrained("gpt2-small")
lit_model = HookedTransformerLIT(model)
dataset = SimpleTextDataset.from_strings(["Hello world!"])

serve({"gpt2": lit_model}, {"examples": dataset}, port=5433)
# Open http://localhost:5433

LIT is an optional dependency - existing installations are unaffected.

Fixes #121

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

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 unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

bryce13950 and others added 14 commits June 12, 2025 11:19
- Fix HookedTransformer type imports to use class from module path
- Remove unused imports (TYPE_CHECKING, Optional, Sequence, etc.)
- Fix return type annotations for functions returning Optional values
- Add proper type: ignore comments for dynamic base classes
- Add noqa comments for intentionally unused TYPE_CHECKING imports
- Add explicit type annotation for output dict
- Format code with ruff formatter
lit-nlp requires Python >=3.9, so add python constraint to ensure
compatibility with the project's Python >=3.8 support
…e errors

Beartype tries to resolve forward reference strings at runtime,
which fails for TYPE_CHECKING-only imports. Replace string type
hints like 'HookedTransformer' and 'ActivationCache' with Any
to allow runtime type checking to pass while maintaining
documentation in docstrings.

Also clean up unused imports from test_lit.py:
- Remove unused TYPE_CHECKING, Any, Dict, List imports
- Remove unused sys and lit_nlp imports
- Remove unused inputs variable in test
- Apply black formatting to test_lit.py and __init__.py
- Add NBVAL_IGNORE_OUTPUT comment to BERT.ipynb import cell to ignore warnings during import
…n LIT module

- Add NBVAL_IGNORE_OUTPUT to Othello_GPT.ipynb import cell (Cell 5) to ignore stderr
- Add doctest: +SKIP to all doctest examples in LIT module since lit-nlp is optional
- This prevents docstring tests from failing when LIT is not installed
@HetanshWaghela
Copy link
Author

Note on the large diff (+26k / -20k changes):
The significant number of line changes is almost entirely due to adding # NBVAL_IGNORE_OUTPUT to several large Jupyter Notebook files (such as Main_Demo.ipynb and Exploratory_Analysis_Demo.ipynb).

This was necessary to resolve GitHub Action failures where nbval was failing due to environmental warnings (stderr) in the CI runner. Because .ipynb files are large JSON documents, adding even a single comment line causes significant changes in the underlying file structure and metadata.

The actual code logic for the LIT integration is localized and can be reviewed primarily in the ' transformer_lens/lit/ 'directory.

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.

3 participants