Metadata-Version: 2.4
Name: lexframe
Version: 1.6.0
Summary: A toolkit for interpreting LM residual streams in their native semantics
Author-email: Jian Gu <jianguda@gmail.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/jianguda/lexframe
Project-URL: Repository, https://github.com/jianguda/lexframe
Project-URL: Issues, https://github.com/jianguda/lexframe/issues
Keywords: interpretability,language models,logit lens,residual stream,steering
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=2.4.1
Requires-Dist: torch>=2.10.0
Requires-Dist: tqdm>=4.66
Requires-Dist: transformers>=5.0.0
Provides-Extra: test
Requires-Dist: pytest>=9.0.3; extra == "test"
Provides-Extra: demo
Requires-Dist: datasets>=4.5.0; extra == "demo"
Requires-Dist: matplotlib>=3.10; extra == "demo"
Dynamic: license-file

<h1 align="center">LexFrame: Lexical Reference Frames</h1>

<p align="center">A toolkit for interpreting LM residual streams in their native semantics.</p>

<p align="center">
  <a href="https://pypi.org/project/lexframe/"><img src="https://img.shields.io/pypi/v/lexframe?cacheSeconds=3600" alt="PyPI"></a>
  <a href="https://github.com/jianguda/lexframe/actions/workflows/workflow.yml"><img src="https://img.shields.io/github/actions/workflow/status/jianguda/lexframe/workflow.yml?branch=main&label=CI" alt="CI"></a>
  <a href="https://arxiv.org/abs/2610.22169"><img src="https://img.shields.io/badge/arXiv-2610.22169-b31b1b" alt="arXiv"></a>
  <a href="https://jianguda.github.io/blogs/lexframe/"><img src="https://img.shields.io/badge/blog-lexframe-orange" alt="Blog"></a>
</p>

<p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/overview.gif" width="720" alt="LexFrame overview"></p>

LexFrame reads every hidden state of a language model, at every layer and position, in the model's own vocabulary: its logits, its gradients, and the semantic bases that link them, for every label.
It is **model-native**: the state runs through the model's own remaining layers, calibrated once on unlabelled text, with nothing fitted to the output.
It is **context-aware**: it separates what the state carries by itself from what the context adds later, and steers or ablates the model's run with the full context, carrying every move forward to the output.

## Quickstart

```bash
pip install lexframe
```

```python
import lexframe as lf
frame = lf.Frame(model, tokenizer).calibrate(texts)  # calibrate once
text = "The Eiffel Tower is located in the city of"
trace = frame.trace(text)                            # last token
trace.logits().softmax(-1).topk(5)                   # top labels per layer
state = trace[16]                                    # layer 16
b = state.basis([" Paris", " London"])               # label bases
state.compose(state.logits())                        # reconstruct the state
state.project(state.vector)                          # recover its logits
moved = state.steer({" Paris": -4, " London": 4})    # steer toward London
moved[-1].logits().softmax(-1).topk(5)               # output after steering
trace[13].ablate()[-1].logits().softmax(-1).topk(5)  # remove block 12's write
```

[docs/USAGE.md](https://github.com/jianguda/lexframe/blob/main/docs/USAGE.md) describes every command, from logits, gradients and semantic bases to composition, projection, steering and ablation, and [examples/walkthrough.py](https://github.com/jianguda/lexframe/blob/main/examples/walkthrough.py) puts them to work: where SmolLM3 decides that the Eiffel Tower is in Paris, who wrote that decision, and how to change it. [examples/visualize.py](https://github.com/jianguda/lexframe/blob/main/examples/visualize.py) plots every layer's logits, basis angles, attribution, steering effects and lens comparisons. Explore LexFrame directly with the [notebook](https://colab.research.google.com/github/jianguda/lexframe/blob/main/examples/quickstart.ipynb).

## Mechanism

LexFrame reads a hidden state by running the model's remaining layers with later attention writes replaced by calibrated conditional means and residual variation; the centered log of the average output distribution gives its logits. True gradients differentiate the actual output with the prefix context fixed. Under one fixed output-derived inner product shared across layers, logits-weighted label bases reconstruct the native state, projection recovers its logits, and the angular part preserves the true differential between hidden-state and logit tangent spaces. For finite effects, `steer` and `ablate` rerun the model with full context; steering targets output-logit changes at fixed state norm while minimizing KL change among other labels. [docs/MECHANISM.md](https://github.com/jianguda/lexframe/blob/main/docs/MECHANISM.md) gives the definitions and derivations.

<p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/lenses-olmo.png" width="900" alt="The top token each method reads in OLMo3-7B"></p>
<p align="center"><sub>The top token each method reads at every second layer of OLMo3-7B, for "The Space Needle is located in the city of" after the document start, shaded by its probability (Jacobian lens fitted on 100 sequences). The model answers " Seattle" (0.42). At the last position, LexFrame reads " Seattle" from layer 20, the Jacobian lens from layer 22 and the logit lens from layer 24.</sub></p>

## Compatibility

LexFrame supports any causal LM in 🤗 Transformers, including popular model families such as `Llama`, `Qwen`, `Gemma`, `Mistral` and `DeepSeek`, and compact or fully open ones such as `Phi`, `OLMo` and `MiniCPM`.

## Citation

```bibtex
@article{gu2026lexical,
  title={Lexical Reference Frames: Interpreting LM Residual Streams in Native Semantics},
  author={Gu, Jian and Zhang, Hongyu and Chen, Chunyang and Aleti, Aldeida},
  journal={arXiv preprint arXiv:2610.22169},
  year={2026}
}
```
