lexframe 1.2.0__tar.gz → 1.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. {lexframe-1.2.0/src/lexframe.egg-info → lexframe-1.3.0}/PKG-INFO +16 -14
  2. {lexframe-1.2.0 → lexframe-1.3.0}/README.md +15 -13
  3. lexframe-1.3.0/docs/MECHANISM.md +78 -0
  4. lexframe-1.3.0/docs/USAGE.md +76 -0
  5. {lexframe-1.2.0 → lexframe-1.3.0}/examples/baselines.py +6 -12
  6. {lexframe-1.2.0 → lexframe-1.3.0}/examples/quickstart.ipynb +13 -9
  7. {lexframe-1.2.0 → lexframe-1.3.0}/examples/visualize.py +25 -25
  8. lexframe-1.3.0/examples/walkthrough.py +70 -0
  9. lexframe-1.3.0/src/lexframe/__init__.py +7 -0
  10. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe/_basis.py +10 -31
  11. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe/_calibrate.py +104 -34
  12. lexframe-1.3.0/src/lexframe/_closure.py +99 -0
  13. lexframe-1.3.0/src/lexframe/_frame.py +748 -0
  14. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe/_transport.py +30 -56
  15. {lexframe-1.2.0 → lexframe-1.3.0/src/lexframe.egg-info}/PKG-INFO +16 -14
  16. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe.egg-info/SOURCES.txt +1 -0
  17. {lexframe-1.2.0 → lexframe-1.3.0}/tests/test_frame.py +148 -40
  18. lexframe-1.2.0/docs/MECHANISM.md +0 -52
  19. lexframe-1.2.0/docs/USAGE.md +0 -58
  20. lexframe-1.2.0/examples/walkthrough.py +0 -59
  21. lexframe-1.2.0/src/lexframe/__init__.py +0 -7
  22. lexframe-1.2.0/src/lexframe/_frame.py +0 -533
  23. {lexframe-1.2.0 → lexframe-1.3.0}/LICENSE +0 -0
  24. {lexframe-1.2.0 → lexframe-1.3.0}/MANIFEST.in +0 -0
  25. {lexframe-1.2.0 → lexframe-1.3.0}/pyproject.toml +0 -0
  26. {lexframe-1.2.0 → lexframe-1.3.0}/requirements.txt +0 -0
  27. {lexframe-1.2.0 → lexframe-1.3.0}/setup.cfg +0 -0
  28. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe/_model.py +0 -0
  29. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe/baselines.py +0 -0
  30. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe/py.typed +0 -0
  31. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe.egg-info/dependency_links.txt +0 -0
  32. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe.egg-info/requires.txt +0 -0
  33. {lexframe-1.2.0 → lexframe-1.3.0}/src/lexframe.egg-info/top_level.txt +0 -0
  34. {lexframe-1.2.0 → lexframe-1.3.0}/tests/conftest.py +0 -0
  35. {lexframe-1.2.0 → lexframe-1.3.0}/tests/test_baselines.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lexframe
3
- Version: 1.2.0
3
+ Version: 1.3.0
4
4
  Summary: A toolkit for interpreting LM residual streams in their native semantics
5
5
  Author-email: Jian Gu <jianguda@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -42,11 +42,11 @@ Dynamic: license-file
42
42
  <a href="https://jianguda.github.io/blogs/lexframe/"><img src="https://img.shields.io/badge/blog-lexframe-orange" alt="Blog"></a>
43
43
  </p>
44
44
 
45
- <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/overview.png" width="720" alt="LexFrame overview"></p>
45
+ <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/overview.gif" width="720" alt="LexFrame overview"></p>
46
46
 
47
47
  LexFrame reads a hidden state the way the model reads it, in the model's own vocabulary.
48
- It is **model-native**: the state is transported layer by layer through the model's own remaining layers, with nothing fitted to the output.
49
- It is **context-aware**: along the way the state attends to its prefix, for as many rounds as chosen.
48
+ It is **model-native**: the state runs through the model's own remaining layers, with nothing fitted to the output.
49
+ It is **context-aware**: it separates what the state carries by itself from what the context adds later, layer by layer and position by position.
50
50
 
51
51
  ## Quickstart
52
52
 
@@ -60,34 +60,36 @@ import lexframe as lf
60
60
  frame = lf.LexFrame(model, tokenizer).init(texts) # a loaded Hugging Face model and any unlabelled texts
61
61
  text = "The Eiffel Tower is located in the city of"
62
62
  v = frame.view(text, k=5)
63
- v.tokens # read: the top 5 tokens of every layer at every position
63
+ v.tokens # read: the top 5 tokens that every state carries
64
64
  v.first_layer(" Paris") # the first layer where " Paris" tops the reading
65
+ frame.ledger(text).sources # account: what each layer took from each position
65
66
  frame.attr(text) # attribute: each layer's contribution to the prediction
66
67
  c = frame.view(text, layers=16)
67
- frame.view(c.steer({" Paris": -4, " London": 4})).tokens # steer: move layer 16 from Paris toward London
68
+ moved = c.steer({" Paris": -4, " London": 4}) # steer: move layer 16 from Paris toward London
69
+ frame.view(moved, frame="native").tokens # the model's output after the move
68
70
  ```
69
71
 
70
72
  [docs/USAGE.md](https://github.com/jianguda/lexframe/blob/main/docs/USAGE.md) lists every command, 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 reading and where the state sits among the lexical bases, layer by layer. To try LexFrame without installing anything, [open the quickstart in Colab](https://colab.research.google.com/github/jianguda/lexframe/blob/main/examples/quickstart.ipynb).
71
73
 
72
74
  ## Mechanism
73
75
 
74
- Lenses such as the logit lens and the Jacobian lens read every hidden state of a layer with one fixed map, whatever the context, so they share a ceiling and, everywhere except near the output, often miss what the model already knows. LexFrame instead carries each hidden state and its context through the model's remaining layers and reads the result over the vocabulary, as the model reads it. These readings can be attributed and steered at every layer. [docs/MECHANISM.md](https://github.com/jianguda/lexframe/blob/main/docs/MECHANISM.md) explains the details.
76
+ Lenses such as the logit lens and the Jacobian lens read every state of a layer with one fixed map, whatever its context. LexFrame instead runs each state through the model's remaining layers: the closure reading shows what the state carries by itself, the ledger what the context adds after each layer and from which positions, and the native frame gives each token a readout and a basis for attribution and steering. In the closure, every attention write after the state's layer is replaced by its calibrated prediction plus Gaussian noise, so the reading depends on the state alone and becomes the model's own output at the last layer. The ledger adds up exactly to the gap between a reading and the output. [docs/MECHANISM.md](https://github.com/jianguda/lexframe/blob/main/docs/MECHANISM.md) explains the details.
75
77
 
76
78
  <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/lenses-qwen.png" width="900" alt="The top token each method reads in Qwen3-0.6B"></p>
77
79
  <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/lenses-minicpm.png" width="900" alt="The top token each method reads in MiniCPM5-1B"></p>
78
- <p align="center"><sub>The top token that each method reads at every second layer of Qwen3-0.6B (top) and MiniCPM5-1B (bottom), shaded by its probability, with the Jacobian lens fitted on 100 sequences. At the last position, LexFrame reads " Paris" from layer 2 in Qwen3-0.6B and from layer 0 in MiniCPM5-1B, while both lenses read it only from layer 18 or later.</sub></p>
80
+ <p align="center"><sub>The top token that each method reads at every second layer of Qwen3-0.6B (top) and MiniCPM5-1B (bottom), shaded by its probability, with the Jacobian lens fitted on 100 sequences. At the last position, LexFrame reads " Paris" from layer 14 in MiniCPM5-1B, while both lenses read it only from layer 18 or later; in Qwen3-0.6B all three read it from layer 22.</sub></p>
79
81
 
80
82
  ## Compatibility
81
83
 
82
- LexFrame supports the same causal language models as 🤗 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`.
84
+ 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`.
83
85
 
84
86
  ## Citation
85
87
 
86
88
  ```bibtex
87
- @article{gu2026lexframe,
88
- title = {Lexical Reference Frames: Interpreting LM Residual Streams in Native Semantics},
89
- author = {Gu, Jian and Zhang, Hongyu and Chen, Chunyang and Aleti, Aldeida},
90
- journal = {arXiv preprint arXiv:2610.22169},
91
- year = {2026}
89
+ @article{gu2026lexical,
90
+ title={Lexical Reference Frames: Interpreting LM Residual Streams in Native Semantics},
91
+ author={Gu, Jian and Zhang, Hongyu and Chen, Chunyang and Aleti, Aldeida},
92
+ journal={arXiv preprint arXiv:2610.22169},
93
+ year={2026}
92
94
  }
93
95
  ```
@@ -9,11 +9,11 @@
9
9
  <a href="https://jianguda.github.io/blogs/lexframe/"><img src="https://img.shields.io/badge/blog-lexframe-orange" alt="Blog"></a>
10
10
  </p>
11
11
 
12
- <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/overview.png" width="720" alt="LexFrame overview"></p>
12
+ <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/overview.gif" width="720" alt="LexFrame overview"></p>
13
13
 
14
14
  LexFrame reads a hidden state the way the model reads it, in the model's own vocabulary.
15
- It is **model-native**: the state is transported layer by layer through the model's own remaining layers, with nothing fitted to the output.
16
- It is **context-aware**: along the way the state attends to its prefix, for as many rounds as chosen.
15
+ It is **model-native**: the state runs through the model's own remaining layers, with nothing fitted to the output.
16
+ It is **context-aware**: it separates what the state carries by itself from what the context adds later, layer by layer and position by position.
17
17
 
18
18
  ## Quickstart
19
19
 
@@ -27,34 +27,36 @@ import lexframe as lf
27
27
  frame = lf.LexFrame(model, tokenizer).init(texts) # a loaded Hugging Face model and any unlabelled texts
28
28
  text = "The Eiffel Tower is located in the city of"
29
29
  v = frame.view(text, k=5)
30
- v.tokens # read: the top 5 tokens of every layer at every position
30
+ v.tokens # read: the top 5 tokens that every state carries
31
31
  v.first_layer(" Paris") # the first layer where " Paris" tops the reading
32
+ frame.ledger(text).sources # account: what each layer took from each position
32
33
  frame.attr(text) # attribute: each layer's contribution to the prediction
33
34
  c = frame.view(text, layers=16)
34
- frame.view(c.steer({" Paris": -4, " London": 4})).tokens # steer: move layer 16 from Paris toward London
35
+ moved = c.steer({" Paris": -4, " London": 4}) # steer: move layer 16 from Paris toward London
36
+ frame.view(moved, frame="native").tokens # the model's output after the move
35
37
  ```
36
38
 
37
39
  [docs/USAGE.md](https://github.com/jianguda/lexframe/blob/main/docs/USAGE.md) lists every command, 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 reading and where the state sits among the lexical bases, layer by layer. To try LexFrame without installing anything, [open the quickstart in Colab](https://colab.research.google.com/github/jianguda/lexframe/blob/main/examples/quickstart.ipynb).
38
40
 
39
41
  ## Mechanism
40
42
 
41
- Lenses such as the logit lens and the Jacobian lens read every hidden state of a layer with one fixed map, whatever the context, so they share a ceiling and, everywhere except near the output, often miss what the model already knows. LexFrame instead carries each hidden state and its context through the model's remaining layers and reads the result over the vocabulary, as the model reads it. These readings can be attributed and steered at every layer. [docs/MECHANISM.md](https://github.com/jianguda/lexframe/blob/main/docs/MECHANISM.md) explains the details.
43
+ Lenses such as the logit lens and the Jacobian lens read every state of a layer with one fixed map, whatever its context. LexFrame instead runs each state through the model's remaining layers: the closure reading shows what the state carries by itself, the ledger what the context adds after each layer and from which positions, and the native frame gives each token a readout and a basis for attribution and steering. In the closure, every attention write after the state's layer is replaced by its calibrated prediction plus Gaussian noise, so the reading depends on the state alone and becomes the model's own output at the last layer. The ledger adds up exactly to the gap between a reading and the output. [docs/MECHANISM.md](https://github.com/jianguda/lexframe/blob/main/docs/MECHANISM.md) explains the details.
42
44
 
43
45
  <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/lenses-qwen.png" width="900" alt="The top token each method reads in Qwen3-0.6B"></p>
44
46
  <p align="center"><img src="https://raw.githubusercontent.com/jianguda/lexframe/main/assets/lenses-minicpm.png" width="900" alt="The top token each method reads in MiniCPM5-1B"></p>
45
- <p align="center"><sub>The top token that each method reads at every second layer of Qwen3-0.6B (top) and MiniCPM5-1B (bottom), shaded by its probability, with the Jacobian lens fitted on 100 sequences. At the last position, LexFrame reads " Paris" from layer 2 in Qwen3-0.6B and from layer 0 in MiniCPM5-1B, while both lenses read it only from layer 18 or later.</sub></p>
47
+ <p align="center"><sub>The top token that each method reads at every second layer of Qwen3-0.6B (top) and MiniCPM5-1B (bottom), shaded by its probability, with the Jacobian lens fitted on 100 sequences. At the last position, LexFrame reads " Paris" from layer 14 in MiniCPM5-1B, while both lenses read it only from layer 18 or later; in Qwen3-0.6B all three read it from layer 22.</sub></p>
46
48
 
47
49
  ## Compatibility
48
50
 
49
- LexFrame supports the same causal language models as 🤗 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`.
51
+ 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`.
50
52
 
51
53
  ## Citation
52
54
 
53
55
  ```bibtex
54
- @article{gu2026lexframe,
55
- title = {Lexical Reference Frames: Interpreting LM Residual Streams in Native Semantics},
56
- author = {Gu, Jian and Zhang, Hongyu and Chen, Chunyang and Aleti, Aldeida},
57
- journal = {arXiv preprint arXiv:2610.22169},
58
- year = {2026}
56
+ @article{gu2026lexical,
57
+ title={Lexical Reference Frames: Interpreting LM Residual Streams in Native Semantics},
58
+ author={Gu, Jian and Zhang, Hongyu and Chen, Chunyang and Aleti, Aldeida},
59
+ journal={arXiv preprint arXiv:2610.22169},
60
+ year={2026}
59
61
  }
60
62
  ```
@@ -0,0 +1,78 @@
1
+ # Mechanism
2
+
3
+ How LexFrame reads a hidden state. The notation follows the paper.
4
+
5
+ LexFrame answers three questions about the state $x_k(t)$ at layer $k$ and position $t$, each with its own tool:
6
+
7
+ | Question | Tool | Method |
8
+ |---|---|---|
9
+ | What does the state carry? | the closure reading $R_k$ | `view()` |
10
+ | What does the context add after layer $k$? | the ledger | `ledger()` |
11
+ | How does the model use the state here? | the native frame: readout, basis, coefficients | `readout()`, `basis()`, `coef()`, `steer()`, `attr()` |
12
+
13
+ Layers run from 1 to $L$. Layer 0 is the embedding, a fixed input frame, and is shown for diagnosis only.
14
+
15
+ ## 1. Native semantics
16
+
17
+ A language model's vocabulary is its native semantics. At the output, the logit of a token $v$ is
18
+
19
+ $$z_v = w_v^\top N(x_L),$$
20
+
21
+ with the last hidden state $x_L$, the final normalisation $N$ and the unembedding row $w_v$. Its centred logit $c_v$ subtracts the mean over the vocabulary. Every state at the output is read in this one way. A state at an intermediate layer has no fixed reading, because what it means depends on the layers that remain.
22
+
23
+ ## 2. The closure reading
24
+
25
+ The closure reads $x_k(t)$ by running it through the model's own layers $k+1, \dots, L$ on its own column. Norms, feed-forward blocks and residual connections are computed as the model computes them. Other positions enter a layer only through its attention write, so the closure replaces each write by its prediction from the column's own attention input $u$, plus an independent Gaussian innovation:
26
+
27
+ $$a_\ell = \bar a_\ell + G_\ell\,(u - \bar u_\ell) + o_\ell[\beta(t)] + \varepsilon_\ell, \qquad \varepsilon_\ell \sim \mathcal N(0, \Sigma_\ell).$$
28
+
29
+ The reading is the mean next-token distribution over the innovations,
30
+
31
+ $$R_k = \mathbb E_\varepsilon\big[\mathrm{softmax}(W N(h_L))\big].$$
32
+
33
+ After layer $k$ the column never reads another position. There is no reference context and no order to choose, and attention never runs on a context with parts missing. The reading is a function of $x_k(t)$ alone, given the model and the calibration, which the swap test checks exactly: a state carried into another text reads the same. At the output, $R_L$ is the model's own distribution. Elsewhere it is not, so the reading is not a tautology. Position 0 attends only to itself, so its writes are computed exactly and its reading equals the output at every layer.
34
+
35
+ **Calibration.** One forward pass over unlabelled text gives, per layer, the typical attention output: $G_\ell$ is the ridge regression of the write on its input, $\bar u_\ell$ and $\bar a_\ell$ are the means. The ridge is the MacKay evidence fixed point, a closed form without a data split, started from two-fold cross-validation. A second pass gives the residuals of this prediction, excluding position 0, which is exact:
36
+
37
+ - the offsets $o_\ell[\beta]$ are the mean residual in each position bucket $\beta(t)$: positions 0 to 7 one each, then buckets of doubling width;
38
+ - $\Sigma_\ell$ is the posterior-predictive covariance, the pooled residual covariance times $n/(n-\gamma_\ell)$, where $\gamma_\ell$ is the effective number of parameters of the regression.
39
+
40
+ The calibration is part of the reading's definition, a prior over contexts. `init()` takes 1024 blocks of 128 tokens, one from each of 1024 evenly spaced texts (WikiText-103 in the paper), cut by the same pipeline that cuts the texts it reads.
41
+
42
+ **Integration.** The expectation is computed by randomised quasi-Monte Carlo. The points are scrambled Sobol points on the eigen-coordinates of every $\Sigma_\ell$, with the largest variances first. By default there are 64. The same points serve every layer, position and text, so a reading is deterministic, and the readings of different layers are paired.
43
+
44
+ ## 3. The ledger
45
+
46
+ The closure readings of one position telescope to the output:
47
+
48
+ $$\log p_L(v) - \log R_k(v) = \sum_{j \ge k} \big(\log R_{j+1}(v) - \log R_j(v)\big).$$
49
+
50
+ The sum is exact, because every term uses the same integration points. Step $j$ replaces layer $j$'s closure write, its prediction plus the innovation, by the true write $a_j$. The step splits into two terms:
51
+
52
+ - the **innovation** of the true write over its prediction;
53
+ - a **noise** term: the closure's own spread at layer $j$, which the true write removes.
54
+
55
+ The true write is a sum over source positions, $a_j(t) = \sum_s c_s$, where $c_s$ is what the heads read at $s$, weighted by their attention. Since the head-averaged attention weights $w_s$ sum to one, the innovation splits exactly by source position,
56
+
57
+ $$a_j - \hat a_j = \sum_s (c_s - w_s \hat a_j).$$
58
+
59
+ Each source term is mapped to the target's log reading by integrated gradients along the straight path from $\hat a_j$ to $a_j$. `ledger()` reports every term in log-probability units and the readings themselves as probabilities. Integrated gradients leave a small discretisation residual, which is reported as well.
60
+
61
+ ## 4. The native frame
62
+
63
+ The native frame carries the state through the model's remaining layers with the true context. Each position attends to the true states of its prefix and to its own key, through the layer's own attention module and the model's own position embeddings. For an unmoved state this reproduces the model exactly. For a moved state it gives the model's output after the move. Let $J_k$ be the Jacobian of this transport followed by $N$. Each token then has two directions at the state:
64
+
65
+ - The **lexical readout** $b_k(v) = J_k^\top (w_v - \bar w)$ is the gradient of the token's centred logit. Its inner product with a displacement of the state is the logit change that the displacement causes, to first order.
66
+ - The **lexical basis** $s_k(v) = J_k^{+} s_v$ is the move of the state that raises the token alone, where $s_v = (W^\top W)^{-1} w_v$ is the output basis vector of $v$. It solves the damped least-squares problem
67
+ $$\min_\delta \lVert J_k \delta - s_v \rVert^2 + \lambda \lVert \delta \rVert^2, \qquad \lambda = \rho\, \lVert J_k^\top s_v \rVert^2 / \lVert s_v \rVert^2,$$
68
+ by LSMR, with forward-mode products $J_k \delta$ and reverse-mode products $J_k^\top y$ and without forming $J_k$. Each solution is scaled so that one unit adds exactly one logit.
69
+
70
+ The readout and basis of a token have an inner product of one. Attribution, steering and every other edit use the native frame. Edits along closure directions do not transfer better across paraphrases.
71
+
72
+ **The closure readout and the frame gap.** The gradient of the centred log reading, $\nabla_x \log R_k(v)$, is the closure readout. It is defined on the readout side only. The frame gap
73
+
74
+ $$1 - \cos\big(b_k(v),\ \nabla_x \log R_k(v)\big)$$
75
+
76
+ measures how much the context will still reinterpret the state. Fixed lenses fall short of both directions. The logit lens, the Jacobian lens and the best fixed lens (at most $\sqrt\kappa$, see `bound()`) agree with the native readout less than the closure readout does.
77
+
78
+ **Attribution.** `attr()` removes what a layer, or its attention or feed-forward sublayer, wrote at a position. It carries both states through the native frame and returns the change of the target's centred logit.
@@ -0,0 +1,76 @@
1
+ # Usage
2
+
3
+ A frame is built on a loaded Hugging Face model and calibrated once on unlabelled text. `view()` returns a `Carrier`: the hidden states of a text at one or more layers, with their reading and the directions that move and measure them. `ledger()` returns a `Ledger`: what the context adds to the reading of one position after each layer.
4
+
5
+ ```python
6
+ import lexframe as lf
7
+
8
+ frame = lf.LexFrame(model, tokenizer).init(texts)
9
+ c = frame.view("The Eiffel Tower is located in the city of", k=5, layers=16)
10
+ c.tokens # the closure reading: what each state carries by itself
11
+ b, r = c.basis([" Paris", " London"]), c.readout([" Paris", " London"])
12
+ moved = c.steer({" Paris": 2, " London": -2}) # the same as c + 2 * b[:, 0] - 2 * b[:, 1]
13
+ frame.view(moved, frame="native").tokens # the model's output after the move
14
+ frame.ledger("The Eiffel Tower is located in the city of").sources # what each layer took from each position
15
+ ```
16
+
17
+ ## Frame
18
+
19
+ | Command | Returns |
20
+ |---|---|
21
+ | `LexFrame(model, tokenizer, samples=64)` | a frame whose closure reading averages over `samples` quasi-Monte Carlo points |
22
+ | `frame.init(texts, size=1024, spread=True, cache=True, cache_dir=None, tol=0.05, progress=True)` | the frame, calibrated on `size` sequences of 128 tokens (cached on disk) and checked: `frame.identities` |
23
+ | `frame.config(steps=30, ridge=1e-3, warm=False, samples=64)` | the frame, with these LSMR settings for the basis (`frame.solver`) and this number of integration points |
24
+ | `frame.view(text, k=1, layers=None, positions=None, frame="closure")` | a `Carrier` at every layer (or the given ones) and every position (or the given ones), or the reading of a (moved) `Carrier` |
25
+ | `frame.ledger(text, target=None, position=-1, steps=8)` | a `Ledger` of one position for the target token (default: the model's prediction) |
26
+ | `frame.attr(text, sublayer=None, target=None)` | each layer's (or its `"attn"` or `"mlp"` sublayer's) contribution to the target's logit, `[layer, position]` |
27
+ | `frame.bound(texts, token, layers=None)` | the best agreement that any fixed lens can reach with the native readout, per layer |
28
+ | `frame.token_id(token)` | the id of a token given as an id or as a string of exactly one token |
29
+
30
+ ## Carrier
31
+
32
+ | Attribute or method | Returns |
33
+ |---|---|
34
+ | `vector` | the hidden states, `[..., position, hidden]` |
35
+ | `tokens`, `ids`, `probs` | the top `k` tokens of the reading, `[..., position, k]` |
36
+ | `kl` | the divergence of the reading from the model's output, in nats, `[..., position]` |
37
+ | `reading` | `"closure"` or `"native"`, the frame that `view` read the states in |
38
+ | `first_layer(token)` | the first read layer at which the token tops the reading, `[..., position]`, or -1 |
39
+ | `basis(tokens)` | vectors that move the state to add one logit to each token, in the native frame, `[..., position, token, hidden]` |
40
+ | `readout(tokens, frame="native")` | covectors that measure each token's logit, so `readout @ basis.mT` has ones on the diagonal; with `frame="closure"`, the gradient of each token's centred log reading |
41
+ | `coef(tokens, frame="native")` | the centred output logits of the tokens when the state is carried with its true context; with `frame="closure"`, their centred log reading, `[..., position, token]` |
42
+ | `gap(tokens)` | one minus the cosine of the native and the closure readout, `[..., position, token]` |
43
+ | `steer({token: weight})` | a moved `Carrier`, shifted along each token's basis by its weight in logits |
44
+ | `positions` | the positions that the `Carrier` holds, or `None` for every position |
45
+ | `carrier + v`, `carrier - v` | a moved `Carrier`, read again with `frame.view(moved)` |
46
+
47
+ ## Ledger
48
+
49
+ | Attribute | Returns |
50
+ |---|---|
51
+ | `token`, `target`, `position` | the target token as text and as an id, and the position |
52
+ | `logp`, `prob` | the closure reading of the target at every layer, `[layer + 1]`; the last is the model's output |
53
+ | `step` | `logp[j + 1] - logp[j]`, so that `step[k:].sum()` is `logp[-1] - logp[k]` exactly, `[layer]` |
54
+ | `innovation`, `noise` | the two parts of each step: the true attention write against its closure prediction, and the closure's own spread |
55
+ | `sources` | the innovation split by source position, `[layer, position]`, or `None` where the attention module does not allow the split |
56
+ | `residual`, `check` | the integrated-gradients residual of `sources`, and the check that the true write rebuilds the next reading |
57
+
58
+ Tokens are given as ids or as strings that encode to exactly one token, such as `" Paris"`; a string of several tokens is refused. Layers run from 0, the embedding, to `frame.num_layers`, the output; the reading starts at layer 1, and layer 0 is shown for diagnosis. `view` and `attr` take a list of texts: texts of one token length are read together, which adds a leading axis, and texts of different lengths one at a time, which returns a list. `basis`, `readout`, `coef`, `gap` and `ledger` take one text.
59
+
60
+ ## Notes
61
+
62
+ **Calibration.** `init` uses `size` sequences of 128 tokens, one from each of `size` evenly spaced texts, so that the calibration covers the whole corpus. With fewer texts than sequences, or with `spread=False`, the sequences are cut contiguously from the start. The calibration is part of the reading's definition: pass the whole corpus, e.g. all of WikiText-103's training split, rather than its first lines. It takes two passes over the sequences and is cached on disk, in `cache_dir`, `$LEXFRAME_CACHE` or `~/.cache/lexframe`, under a fingerprint of the model's configuration and weights and of the calibration tokens. `frame.calibration.stats` lists each layer's ridge, effective number of parameters and held-out fit. `frame.identities` reports how far the native frame and the closure reading of the first position are from the model, where both must be exact, and `init` warns if either exceeds `tol` nats.
63
+
64
+ **Reading.** The closure reading of a state depends on the state and its position alone. It is deterministic: the integration points are fixed. More `samples` integrate more accurately at a proportional cost. `frame="native"` reads states with their true context instead. For the model's own states this is the output at every layer, and for moved states the output after the move.
65
+
66
+ **Solver.** `frame.config` sets the LSMR solver behind `basis`, which solves all tokens and positions at once and the layers from the top down. `steps` is the number of iterations per layer, more of which give a more accurate basis, `ridge` the relative damping, and `warm` starts each layer from the solution of the layer above instead of from zero.
67
+
68
+ **Moving a carrier.** `carrier + v` moves every position on its own. `frame.view(moved)` gives the closure reading of the moved states, and `frame.view(moved, frame="native")` the model's output after the move, with the unmoved prefix. `coef` and `readout` of a moved carrier are taken at its new states, while its `basis` is refused, because the basis is taken at the model's own states.
69
+
70
+ **Ledger.** `ledger` integrates each source term along the path of the write with `steps` points. More points shrink `residual`. The split by source position needs the attention module's weights and an output projection `o_proj`. Where the weights do not sum to one, e.g. with attention sinks, `sources` is `None` and the steps, innovations and noise terms are still exact.
71
+
72
+ **Positions.** `positions` reads only the given positions of a text, and of each text in a list. As the model is causal, the text is cut after the last of them, which saves the computation of the later positions. Such a `Carrier` holds its reading only, so take directions or steer on a view of every position.
73
+
74
+ **Bound.** `bound` measures how much the native readouts of a token agree across at least two texts. Its square root is the best mean cosine that any lens with fixed directions per layer can reach.
75
+
76
+ **Cost.** The closure reading of one state runs `samples` columns through the remaining layers. These columns attend to nothing, so `view` over every layer of a text of $T$ tokens costs about `samples` $\cdot T (L + 1)/2$ single-position layer passes, batched together. `layers=` and `positions=` read less at a lower cost. `readout` adds one backward pass per token, `basis` one forward-mode and one reverse-mode pass per solver step and layer, and `ledger` about `steps + 2` closure readings per layer.
@@ -17,13 +17,13 @@ from lexframe.baselines import JacobianLens, LogitLens
17
17
  name = "HuggingFaceTB/SmolLM3-3B"
18
18
  model = AutoModelForCausalLM.from_pretrained(name, device_map="cuda" if torch.cuda.is_available() else "cpu")
19
19
  tokenizer = AutoTokenizer.from_pretrained(name)
20
- texts = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train")["text"][:1000]
20
+ texts = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train")["text"]
21
21
 
22
22
  frame = lf.LexFrame(model, tokenizer).init(texts)
23
23
  text = "The Eiffel Tower is located in the city of"
24
24
  methods = {
25
25
  "logit lens": LogitLens(model, tokenizer),
26
- "Jacobian lens": JacobianLens(model, tokenizer).fit(texts, size=32), # more sequences fit the lens better
26
+ "Jacobian lens": JacobianLens(model, tokenizer).fit(texts[:1000], size=32), # more sequences fit the lens better
27
27
  "LexFrame": frame,
28
28
  }
29
29
  L = frame.num_layers
@@ -61,17 +61,11 @@ fig.savefig("baselines_tokens.png", dpi=150)
61
61
  print("saved baselines_tokens.png")
62
62
 
63
63
  # 2. The divergence in nats of each reading from the model's output, per layer, as a mean over the positions of a few
64
- # sequences: LexFrame of order 0, 1 and 2 (the calibration comes from the cache) next to the two lenses. A frame of
65
- # order n is exact where at most n layers remain, so its divergence there is zero and drops out of the log scale.
66
- readers = {
67
- "logit lens": methods["logit lens"],
68
- "Jacobian lens": methods["Jacobian lens"],
69
- "LexFrame[0]": lf.LexFrame(model, tokenizer, order=0).init(texts),
70
- "LexFrame[1]": frame,
71
- "LexFrame[2]": lf.LexFrame(model, tokenizer, order=2).init(texts),
72
- }
64
+ # sequences. The closure reading is not fitted to the output: it is what each state carries by itself, so its divergence
65
+ # measures what the context has yet to add, and it reaches zero only at the output.
66
+ readers = {"logit lens": methods["logit lens"], "Jacobian lens": methods["Jacobian lens"], "LexFrame": frame}
73
67
  length, count = 64, 8 # tokens per sequence and sequences
74
- seqs = [torch.tensor(i[:length]) for i in (tokenizer.encode(t, add_special_tokens=False) for t in texts)]
68
+ seqs = [torch.tensor(i[:length]) for i in (tokenizer.encode(t, add_special_tokens=False) for t in texts[:1000])]
75
69
  seqs = [s for s in seqs if len(s) == length][:count]
76
70
 
77
71
  fig, ax = plt.subplots(figsize=(7, 4.5), layout="constrained")
@@ -6,7 +6,7 @@
6
6
  "source": [
7
7
  "# LexFrame quickstart\n",
8
8
  "\n",
9
- "LexFrame reads a hidden state the way the model reads it: through the model's own remaining layers, with the context the state attends to, in the model's own vocabulary. This notebook reads SmolLM3-3B, compares LexFrame with the logit lens and the Jacobian lens, and steers a prediction. Choose a GPU runtime (Runtime > Change runtime type > T4 GPU).\n",
9
+ "LexFrame reads a hidden state the way the model reads it, in the model's own vocabulary: what the state carries by itself (the closure reading), what the context adds after each layer (the ledger), and how the model uses the state (the native frame). This notebook reads SmolLM3-3B, compares LexFrame with the logit lens and the Jacobian lens, and steers a prediction. Choose a GPU runtime (Runtime > Change runtime type > T4 GPU).\n",
10
10
  "\n",
11
11
  "[Paper](https://arxiv.org/abs/2610.22169) | [Blog post](https://jianguda.github.io/blogs/lexframe/) | [Code](https://github.com/jianguda/lexframe)"
12
12
  ],
@@ -28,7 +28,7 @@
28
28
  "source": [
29
29
  "## Load a model and calibrate a frame\n",
30
30
  "\n",
31
- "Any causal language model in Hugging Face Transformers works. The frame is calibrated once on unlabelled text, which takes a few minutes and is cached on disk."
31
+ "Any causal language model in Hugging Face Transformers works. The frame is calibrated once on unlabelled text, 1024 blocks spread over WikiText-103, which takes a few minutes and is cached on disk."
32
32
  ],
33
33
  "id": "cell02"
34
34
  },
@@ -47,7 +47,7 @@
47
47
  "name = \"HuggingFaceTB/SmolLM3-3B\"\n",
48
48
  "model = AutoModelForCausalLM.from_pretrained(name, dtype=\"auto\", device_map=\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
49
49
  "tokenizer = AutoTokenizer.from_pretrained(name)\n",
50
- "texts = load_dataset(\"Salesforce/wikitext\", \"wikitext-103-raw-v1\", split=\"train\")[\"text\"][:1000]\n",
50
+ "texts = load_dataset(\"Salesforce/wikitext\", \"wikitext-103-raw-v1\", split=\"train\")[\"text\"]\n",
51
51
  "\n",
52
52
  "frame = lf.LexFrame(model, tokenizer).init(texts)\n",
53
53
  "frame"
@@ -58,9 +58,9 @@
58
58
  "cell_type": "markdown",
59
59
  "metadata": {},
60
60
  "source": [
61
- "## Read, attribute and steer\n",
61
+ "## Read, account, attribute and steer\n",
62
62
  "\n",
63
- "`view` reads every layer at every position. The reading at the last position shows where the answer appears, `attr` shows which layers wrote it, and `steer` moves a state along the lexical basis of two tokens."
63
+ "`view` reads every layer at every position: what each state carries by itself. The reading at the last position shows where the answer appears, `ledger` what the context added after each layer and from which positions, `attr` which layers wrote it, and `steer` moves a state along the lexical basis of two tokens."
64
64
  ],
65
65
  "id": "cell04"
66
66
  },
@@ -85,7 +85,10 @@
85
85
  "source": [
86
86
  "print(\"Paris tops the reading from layer\", v.first_layer(\" Paris\")[-1])\n",
87
87
  "contribution = frame.attr(text)[:, -1]\n",
88
- "print(\"the layers that wrote the prediction most:\", contribution.argsort()[::-1][:3])"
88
+ "print(\"the layers that wrote the prediction most:\", contribution.argsort()[::-1][:3])\n",
89
+ "led = frame.ledger(text, target=\" Paris\")\n",
90
+ "print(\"the reading of Paris per layer:\", led.prob.round(3))\n",
91
+ "print(\"the layers whose attention added most:\", led.innovation.argsort()[::-1][:3])"
89
92
  ],
90
93
  "id": "cell06"
91
94
  },
@@ -96,8 +99,9 @@
96
99
  "outputs": [],
97
100
  "source": [
98
101
  "c = frame.view(text, layers=16)\n",
99
- "print(\"before:\", frame.view(c).tokens[-1])\n",
100
- "print(\"after: \", frame.view(c.steer({\" Paris\": -4, \" London\": 4})).tokens[-1])"
102
+ "moved = c.steer({\" Paris\": -4, \" London\": 4})\n",
103
+ "print(\"before:\", frame.view(c, frame=\"native\").tokens[-1])\n",
104
+ "print(\"after: \", frame.view(moved, frame=\"native\").tokens[-1])"
101
105
  ],
102
106
  "id": "cell07"
103
107
  },
@@ -119,7 +123,7 @@
119
123
  "source": [
120
124
  "from lexframe.baselines import JacobianLens, LogitLens\n",
121
125
  "\n",
122
- "lenses = {\"logit lens\": LogitLens(model, tokenizer), \"Jacobian lens\": JacobianLens(model, tokenizer).fit(texts, size=4)}"
126
+ "lenses = {\"logit lens\": LogitLens(model, tokenizer), \"Jacobian lens\": JacobianLens(model, tokenizer).fit(texts[:1000], size=4)}"
123
127
  ],
124
128
  "id": "cell09"
125
129
  },
@@ -1,9 +1,9 @@
1
1
  """Plot what every layer of SmolLM3 reads about the Eiffel Tower: the top token at each position, the coefficients of
2
2
  the answer and the runner-up, and each layer's contribution to the answer (visualize.png); then follow the lexical bases
3
3
  around the state at the last position through the layers (visualize_bases.png); and, as in the paper, compare the
4
- directions of LexFrame and of two lenses with the model's own readouts, against the ceiling that no fixed lens can pass
5
- (visualize_directions.png); last, split each layer's contribution into attention and MLP and steer the state, with the
6
- frame's prediction next to the model's response (visualize_steering.png). Needs matplotlib."""
4
+ directions of the closure readout and of two lenses with the model's own readouts, against the ceiling that no fixed lens
5
+ can pass (visualize_directions.png); last, split each layer's contribution into attention and MLP and steer the state, with
6
+ the frame's prediction next to the model's response (visualize_steering.png). Needs matplotlib."""
7
7
 
8
8
  import matplotlib.pyplot as plt
9
9
  from matplotlib import font_manager
@@ -19,7 +19,7 @@ from lexframe.baselines import JacobianLens, LogitLens
19
19
  name = "HuggingFaceTB/SmolLM3-3B"
20
20
  model = AutoModelForCausalLM.from_pretrained(name, device_map="cuda" if torch.cuda.is_available() else "cpu")
21
21
  tokenizer = AutoTokenizer.from_pretrained(name)
22
- texts = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train")["text"][:1000]
22
+ texts = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train")["text"]
23
23
 
24
24
  frame = lf.LexFrame(model, tokenizer).init(texts)
25
25
 
@@ -44,7 +44,7 @@ top_k = 3 # how many of the bases nearest to the state to follow at every layer
44
44
  extra = [rival] # further labels whose bases to follow
45
45
 
46
46
  v = frame.view(text, k=top_k)
47
- coef = v.coef([answer, rival])[:, -1].cpu().numpy()
47
+ coef = v.coef([answer, rival], "closure")[:, -1].cpu().numpy() # what each state carries: its centred log reading
48
48
  contribution = frame.attr(text, target=answer)[:, -1]
49
49
  first = v.first_layer(answer)[-1]
50
50
  layers = range(frame.num_layers + 1)
@@ -68,7 +68,7 @@ ax2.plot(coef[:, 0], layers, label=answer.strip())
68
68
  ax2.plot(coef[:, 1], layers, label=rival.strip())
69
69
  if first >= 0:
70
70
  ax2.axhline(first, color="grey", linestyle=":", label=f"{answer.strip()} on top")
71
- ax2.set_xlabel("coefficient (centred logit)")
71
+ ax2.set_xlabel("closure coefficient (centred log reading)")
72
72
  ax2.set_title("answer and runner-up")
73
73
  ax2.legend()
74
74
 
@@ -80,10 +80,10 @@ ax3.set_title(f"who wrote {answer.strip()}")
80
80
  fig.savefig("visualize.png", dpi=150)
81
81
  print("saved visualize.png")
82
82
 
83
- # 4. Where the state at the last position sits among the lexical bases, layer by layer: its cosine with each basis
84
- # and its coefficient on it, its coordinate in the frame. Followed are the bases of the input token at this position,
85
- # where the state starts, of the model's output, where it ends, of the extra labels, and at every layer the top_k
86
- # nearest ones, those with the largest coefficients.
83
+ # 4. Where the state at the last position sits among the lexical bases, layer by layer: its cosine with each basis of the
84
+ # native frame and its closure coefficient on it, what the state carries of that token. Followed are the bases of the
85
+ # input token at this position, where the state starts, of the model's output, where it ends, of the extra labels, and
86
+ # at every layer the top_k nearest ones, those with the largest coefficients.
87
87
  near = v.ids[:, -1] # [layer, top_k]
88
88
  start = tokenizer.encode(text, add_special_tokens=False)[-1]
89
89
  target = int(near[-1, 0])
@@ -94,7 +94,7 @@ tracked = list(dict.fromkeys([*roles, *(int(t) for t in near.reshape(-1))]))
94
94
  names = [label(t) for t in tracked]
95
95
  b = v.basis(tracked)[:, -1].float() # [layer, token, hidden]
96
96
  cos = torch.nn.functional.cosine_similarity(b, v.vector[:, -1, None].float(), dim=-1).cpu().numpy()
97
- coef = v.coef(tracked)[:, -1].cpu().numpy()
97
+ coef = v.coef(tracked, "closure")[:, -1].cpu().numpy()
98
98
 
99
99
  cell = lambda i, l: f"{names[i]} {coef[l, i]:+.1f} {cos[l, i]:+.2f}"
100
100
  print(f"layer: the {top_k} nearest bases | " + ", ".join(f"{r} {names[tracked.index(t)]}" for t, r in roles.items()))
@@ -122,8 +122,8 @@ for i, token in enumerate(tracked):
122
122
  ax1.plot([], [], color="grey", linewidth=0.8, label=f"top {top_k} nearest at some layer")
123
123
  ax1.set_ylabel("cosine of the state with the basis")
124
124
  ax1.set_title("the state's angle to each basis")
125
- ax2.set_ylabel("coefficient (centred logit)")
126
- ax2.set_title("the state's coordinate on each basis")
125
+ ax2.set_ylabel("closure coefficient (centred log reading)")
126
+ ax2.set_title("what the state carries of each token")
127
127
  fig.legend(*ax1.get_legend_handles_labels(), loc="outside lower center", ncol=4, fontsize=8, frameon=False)
128
128
  for ax in (ax1, ax2):
129
129
  ax.set_xlabel("layer")
@@ -134,26 +134,26 @@ print("saved visualize_bases.png")
134
134
  # 5. As in the paper: the cosine of each method's readout of a label with the model's own readout (the native frame),
135
135
  # per layer, as a mean over the states of a few sequences and over labels, with a 95% interval over the sequences.
136
136
  # The ceiling sqrt(kappa) is the best mean cosine that any lens with fixed directions per layer can reach, as
137
- # frame.bound() computes it, from the same readouts. The first position, which attends only to itself, is left out.
138
- native = lf.LexFrame(model, tokenizer, order="inf").init(texts) # the calibration comes from the cache
137
+ # frame.bound() computes it, from the same readouts. The closure readout is the gradient of the state's own reading; the
138
+ # frame gap is one minus its cosine. The first position, which attends only to itself, is left out.
139
+ logit, jacobian = LogitLens(model, tokenizer), JacobianLens(model, tokenizer).fit(texts[:1000], size=32)
139
140
  methods = {
140
- "logit lens": LogitLens(model, tokenizer),
141
- "Jacobian lens": JacobianLens(model, tokenizer).fit(texts, size=32), # more sequences fit the lens better
142
- "LexFrame[1]": frame,
143
- "LexFrame[2]": lf.LexFrame(model, tokenizer, order=2).init(texts),
141
+ "logit lens": lambda s: logit.view(s).readout(labels),
142
+ "Jacobian lens": lambda s: jacobian.view(s).readout(labels), # more sequences fit the lens better
143
+ "closure readout": lambda s: frame.view(s).readout(labels, "closure"),
144
144
  }
145
145
  labels = [" the", " of", " and", " in", " city", " people", " world", " time"]
146
146
  length, count = 32, 8 # tokens per sequence and sequences
147
- seqs = [torch.tensor(i[:length]) for i in (tokenizer.encode(t, add_special_tokens=False) for t in texts)]
147
+ seqs = [torch.tensor(i[:length]) for i in (tokenizer.encode(t, add_special_tokens=False) for t in texts[:1000])]
148
148
  seqs = [s for s in seqs if len(s) == length][:count]
149
149
 
150
150
  cosines = {title: [] for title in methods}
151
151
  units = []
152
152
  for s in seqs:
153
- truth = native.view(s).readout(labels)[:, 1:] # [layer, position, label, hidden]
153
+ truth = frame.view(s).readout(labels)[:, 1:] # [layer, position, label, hidden]
154
154
  units.append((truth / truth.norm(dim=-1, keepdim=True).clamp_min(1e-30)).sum(1).double())
155
155
  for title, method in methods.items():
156
- r = method.view(s).readout(labels)[:, 1:]
156
+ r = method(s)[:, 1:]
157
157
  c = torch.nn.functional.cosine_similarity(r, truth.to(r.device), dim=-1)
158
158
  cosines[title].append(c.mean((1, 2)).cpu().numpy())
159
159
  U = torch.stack(units) # [sequence, layer, label, hidden]: each sequence's sum of unit readouts
@@ -179,19 +179,19 @@ print("saved visualize_directions.png")
179
179
 
180
180
  # 6. Who wrote the answer, split into the attention and MLP sublayers of each layer, and what a steer does: move the
181
181
  # state at mid-depth or later from the answer toward the runner-up by a growing amount, then compare the change in
182
- # their coefficients that the frame predicts with the change in the model's own output, read in the native frame.
182
+ # their coefficients that the readout predicts with the change in the model's own output, read in the native frame.
183
183
  attn = frame.attr(text, "attn", target=answer)[:, -1]
184
184
  mlp = frame.attr(text, "mlp", target=answer)[:, -1]
185
185
  at = max(int(first), frame.num_layers // 2)
186
186
  s = frame.view(text, layers=at)
187
187
  o = s.readout([answer, rival])
188
- base = native.view(s).coef([answer, rival])[-1]
188
+ base = s.coef([answer, rival])[-1]
189
189
  sizes = np.linspace(0, 8, 17)
190
190
  predicted, actual, top = [], [], []
191
191
  for size in sizes:
192
192
  moved = s.steer({answer: -size, rival: size})
193
193
  predicted.append((o @ (moved - s).vector[..., None])[-1, :, 0].float().cpu().numpy())
194
- out = native.view(moved)
194
+ out = frame.view(moved, frame="native")
195
195
  actual.append((out.coef([answer, rival])[-1] - base).float().cpu().numpy())
196
196
  top.append(out.tokens[-1][0])
197
197
  predicted, actual = np.stack(predicted), np.stack(actual)