Skip to content

feat(interchange): several positions per example, aligned or pooled - #2042

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
dipo101:multi-position-interchange
Nov 30, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
dipo101:multi-position-interchange

Conversation

@dipo101

@dipo101 dipo101 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Description

Lets the interchange tools intervene on several positions per example, as approved in #1939.

  • Aligned (default): positions (and fit_das's base_positions/source_positions) may be [batch, k]; base position j takes the source's position j. Each (example, position) pair is one row of the intervention, so every intervention, basis and fit_das work unchanged, with one basis shared by the k positions.
  • Pooled (pool="mean"): each row names a span; the intervention acts on the span's mean, and every position in the span moves by the change in the mean. Base and source spans may differ in length.
  • Padding: shorter rows are padded with PAD_POSITION, a sentinel that is never a valid index. Negative indices in [batch, k] positions raise an error, so -1 (the last position in [batch] mode) is never read as padding. [batch] positions behave exactly as before.

On GPT-2 small at block 4, patching subject number at the subject alone flips 0.25 of held-out pairs, at the preposition alone 0.44, and at both aligned 1.00 (pooled 0.97). The integration test pins this. Docs gain a "Several positions" section, including the note that pooling is a different intervention, not a cheaper version of alignment.

Fixes #1939

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

The interchange tools intervened on one position per example, but a
variable is often spread over several, and then a one-position interchange
underestimates it. positions (and fit_das's base_positions and
source_positions) may now also be [batch, k], in two modes:

- Aligned (default): base position j takes the source's position j. Each
  (example, position) pair is one row of the intervention, so
  SubspaceInterchange, NormMatchedPlacebo, every basis and fit_das work
  unchanged, with one basis shared by the k positions.
  collect_activations returns [batch, k, d_model], NaN in padded slots, and
  a run checks that base and source pad the same slots.
- Pooled (pool="mean"): each row names a span. collect_activations returns
  the span means, the intervention maps the base and source means to a new
  mean, and every base position in the span moves by the change in the
  mean. Base and source spans may differ in length.

Shorter rows are padded with PAD_POSITION, exported from
transformer_lens.tools.analysis: a sentinel that is never a valid index
and fits int32 and int64 position tensors. Negative indices are rejected
in [batch, k] positions with an error naming PAD_POSITION, so -1, the last
position in [batch] positions, is never read as padding. [batch]
positions behave exactly as before, -1 included. Rows out of range, with
a repeated position or with no position are rejected too.

On GPT-2 small at block 4, subject number in "The key(s) near the cabinet"
is carried jointly by the subject and the preposition: patching the whole
residual at either alone flips 0.25 and 0.44 of the pairs, at both aligned
1.00 and pooled 0.97. The integration test pins this; the docs gain a
"Several positions" section with the same example, and note that pooling
is a different intervention, not a cheaper version of alignment.

Implements TransformerLensOrg#1939.

@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 putting this together @dipo101, just a couple small comments related to testing

Comment thread tests/unit/tools/test_interchange.py Resolved
…idths

Review follow-ups:

- The aligned-slot test ran on _SumToy, which sums over positions, so with a
  full interchange it gave the same output whichever source slot went to
  which base position: taking the source's slots in reverse order passed
  every test. It now runs on _Echo, which returns every position's
  activation, with base and source positions listed in different orders,
  and checks each base slot against the matching source slot. The reversal
  now fails it.
- fit_das typed base_positions and source_positions with one width name,
  so under the type checker pooled spans of different widths were rejected,
  the case the docs describe. source_positions now has its own width
  (SourcePositions). Aligned positions must still match: fit_das checks
  that first and names pool='mean' in the error.
@dipo101
dipo101 requested a review from jlarson4 November 28, 2026 21:04
@jlarson4

Copy link
Copy Markdown
Collaborator

Thank you for making those updates @dipo101, this looks great, merged!

@jlarson4
jlarson4 merged commit 9fc15c2 into TransformerLensOrg:dev Nov 30, 2026
31 checks passed
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.

2 participants