MechIR: A Mechanistic Interpretability Framework for Information Retrieval
TL;DR. MechIR is to neural IR what TransformerLens is to GPT-style models. It wraps bi-encoders (e.g. TAS-B) and cross-encoders (e.g. monoELECTRA) with hooks, runs activation patching over blocks and attention heads, and builds paired datasets from ir_datasets with a @perturbation decorator. Install with pip install mechir.
Key idea
Section titled “Key idea”Activation patching in IR needs a pair: a baseline query–document input and a perturbed one (e.g. a query term appended to the document, with padding to keep lengths aligned). Three runs follow. Cache the run with the higher expected score, run the other one, then patch components from the cache into the lower-scoring run. The patching effect is the change in relevance score, which is the dot product for bi-encoders and the logit for cross-encoders.
from mechir import Dotfrom mechir.perturb import perturbation
model = Dot("bert-base-uncased")
@perturbationdef func(document): return "MechIR! " + documentResults. The demo uses a grade-stratified sample (n = 1000) of TREC DL19 and DL20 and compares adding a random query term (TFC1) with adding the most discriminative term by IDF (TDC). In TAS-B, effects are diffuse, with no single head dominating. In monoELECTRA, several heads respond consistently to term matching, mostly in the final layers, with some middle-layer heads under TDC.
Connections
Section titled “Connections”- Axiomatic causal interventions is the study this toolkit packages (Chen et al.).
- Activation patching explains the method and its metrics.
- TransformerLens is the library MechIR extends.
- Cross-encoders as semantic BM25 is a full circuit analysis of the kind MechIR enables.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Mechanism
In MechIR's activation patching for a bi-encoder, what are the baseline and perturbed inputs, which run is patched, and what scalar is measured?
What a strong answer covers
- The perturbed input applies a function to the document, e.g. appending a query term; the baseline pads the original to keep token lengths aligned.
- Activations are cached from the run with higher expected relevance.
- The patched run is the lower-scoring input with chosen components (block outputs or heads, per position) replaced from the cache.
- The metric is the relevance score: the query–document dot product for bi-encoders, the logit for cross-encoders.
- Critique
The demo finds diffuse head effects in TAS-B but a few consistent term-matching heads in monoELECTRA. Why is this expected, and what confounds does the paper flag?
What a strong answer covers
- Bi-encoders have no token-level query–document interaction, so term matching can only appear as many small changes to the pooled document vector.
- Cross-encoders attend across query and document tokens, so dedicated heads can detect matches, mostly in final layers.
- The authors note that penalties for added tokens in early layers may come from term matching or from positional bias, which is not resolved.
- Results are on a 1,000-pair grade-stratified sample of DL19/DL20, so they are illustrative rather than conclusive.
- Design
Use MechIR to test whether a cross-encoder implements the TDC axiom (prefer documents matching more discriminative query terms). Specify the perturbation, the components and the control.
What a strong answer covers
- Perturbation: append the highest-IDF query term vs a random query term (TFC1) to the same document, via a @perturbation function.
- Patch attention heads across positions from the TDC-perturbed run into the baseline and record the logit change.
- Control: the same patch with the TFC1 perturbation; heads whose effect is larger for TDC are candidate IDF-sensitive heads.
- Confirm by ablating those heads and checking the score gap between the two perturbations shrinks.