Skip to content

Model Interpretation

Caliper LIB connects live tensor publication and controls to transformer-lens, circuit-tracer, and sae-lens. The model, transcoders, and sparse autoencoders stay inside the Python process. Library pieces draw attribution graphs, feature examples, and next-token probabilities in the native app and the browser viewer. The interpretation tools expose the same operations through Caliper MCP.

Installation

Install the interp extra from the repository root to pin compatible library versions:

pip install -e ".[interp]"

The interp extra installs compatible tested versions:

  • torch>=2.15.0 and transformers>=5.24.0,<5.25.0
  • transformer-lens==3.9.0
  • sae-lens==6.56.3
  • nnsight==0.8.4
  • circuit-tracer at commit 82996f144b3371ae858ea5713590d004c218f0c2

On Apple Silicon, caliper.interp sets TRANSFORMERLENS_ALLOW_MPS=1 and selects mps automatically when available.

Model Probes

intervene.points (caliper._ablate.points(model)) lists module, attention-head, and semantic probe points on a PyTorch or HookedTransformer model. Decoder blocks expose resid_pre, attn_out, mlp_out, and attn_pattern (plus mlp_in when cfg.use_hook_mlp_in is true) alongside module and blocks.L.attn#H head points. Any watched module also exposes its latest forward activation to derive programs as live:<module>.act without a CPU copy.

caliper.interp.publish(name, tensor) exposes a named tensor to derive programs. For example, publish("trace.counts", counts) updates the catalog source live:trace.counts. Published tensors keep their device and storage, and watch removal clears publications owned by that model.

Publication Lifetime

Each publication belongs to the current caliper.watch lifetime, or to the next watch if none is active yet. Watch removal or model replacement invalidates prior publication leases and clears published tensors. A trace, example, or sweep request checks its lease under the program lock before it publishes a tensor group. If the watched model changes during execution, the request raises RuntimeError with code interpret.watch_changed. Producers observe either the complete previous tensor group or the complete new tensor group.

Feature Dictionaries

caliper.interp.load_backend(model_ref, dictionary_ref=None, *, sae_id=None, device=None, dtype=None) loads a staged Backend. Pass a Hugging Face model name or local snapshot path as model_ref. When dictionary_ref names a transcoder hub alias ("gemma" or "llama"), a TranscoderSet, or a local transcoder directory, load_backend builds a staged TransformerLensReplacementModel. When dictionary_ref names a sae-lens release (with sae_id) or a local SAE directory, load_backend builds a HookedTransformer and stages the SAE on CPU before moving it to the target device.

from caliper.interp import Tracer, load_backend

backend = load_backend("google/gemma-2-2b", "gemma")
tracer = Tracer(backend.model, backend.tokenizer, backend.dictionary, backend=backend)
graph = tracer.trace("He saw a carrot and had to grab it, his hunger was like a starving")

backend.dictionary is a DictionaryView with uniform metadata, encode(layer, x), and lazy decoder_vectors(layer, ids). Any HookedTransformer architecture in transformer-lens and any compatible sae-lens or circuit-tracer dictionary can attach through load_backend. For offline tests without model downloads, pass model_ref="tiny-lm" to construct a deterministic 2-layer ReplacementModel and TranscoderSet.

Example Index

FeatureIndex loads corpus examples and promoted tokens from a memory-mapped index on disk. When no disk index exists, LiveFeatureIndex(backend) resolves feature summaries and top 33-token windows in memory. When a session has network access and SAE.cfg.metadata defines neuronpedia_id, LiveFeatureIndex queries get_neuronpedia_feature. When a session runs offline or uses unindexed transcoders, LiveFeatureIndex scans prompt text with a bounded min-heap. That scan zeros BOS positions.

feature.show returns the heuristic label, activation count, maximum value, and promoted tokens for a feature. feature.examples returns up to 16 ranked 33-token windows and publishes the central 25 slots to token_strip@1.

Index Attachment

FeatureIndex reads corpus examples from an index folder on disk. FeatureIndex and FeatureIndex.resolve(path) check three locations in order and select the first directory with index.json:

  1. The index directory directly, such as <path>/index.
  2. A nested folder inside the dictionary at <dictionary>/index.
  3. A sibling folder next to the dictionary at <dictionary>/../index.

Path resolution checks only local paths for sibling folders. The sibling check runs only when <dictionary> exists on disk. As a result, hub aliases such as "gemma" do not match an unrelated index folder in the current directory. An explicit index path always takes priority over nested and sibling folders.

Pass either the dictionary folder or the index folder to FeatureIndex:

from caliper.interp import FeatureIndex, load_backend

backend = load_backend("google/gemma-2-2b", "/path/to/dictionary")
index_dir = FeatureIndex.resolve("/path/to/dictionary")
if index_dir is not None:
    index = FeatureIndex(
        index_dir,
        model=backend.model,
        tokenizer=backend.tokenizer,
        dictionary=backend.dictionary,
    )

Run tools/interp/build_index.py to build a new index inside <dictionary>/index from a JSONL corpus:

python -m tools.interp.build_index --model google/gemma-2-2b --dictionary "$SCOPE_DIR" --text-jsonl "$CORPUS_JSONL"

Index Layout

Each index folder holds index.json and four binary files. FeatureIndex maps the binary files in read-only mode and checks the layer and latent counts against dictionary.metadata. In index.json, the latent count uses either "latents" or "features". When "latents" is absent, FeatureIndex reads "features" to support 2-million-token Gemma indexes. If both keys appear with different values, FeatureIndex raises ValueError.

File Format Contents
index.json JSON layers, latents or features, index_hash, and corpus metadata
table.npy NumPy (layers, latents, 16) Ranked activation (<f8) and token offset (<i8) pairs
counts.npy NumPy (layers, latents) Positive activation count (<u8) per feature
tokens.bin Raw <u4 stream Concatenated token IDs
documents.bin Fixed 145-byte records UTF-8 id (S128), start (<i8), length (<i8), and truncated flag

Attribution Graphs

Tracer(model, tokenizer, dictionary, backend=backend).trace(prompt) runs circuit_tracer.attribute, transfers the graph to CPU, runs prune_graph, and publishes graph tensors. Prompts accept up to 32 model tokens with BOS and refuse longer inputs without truncation. Default settings select up to 10 logit targets (desired_logit_prob=0.95), up to 5,000 feature targets, node_threshold=0.7, and edge_threshold=0.9. Pass show_more=True to relax pruning to node_threshold=0.78 and edge_threshold=0.93.

After a standalone tracer.trace(prompt) call, invoke caliper.step() once so derive programs process the published tensors. In a hosted inference run, trace.run executes and publishes automatically at the next run boundary. Tracer caches up to 16 completed graphs keyed by prompt, active interventions, thresholds, and label index identity.

Graph Tensors

Graph.publish() writes fixed-capacity tensors on the model device. Graphs within 512 nodes and 8,192 edges use (512, 8192) capacities, while larger graphs up to 8,192 nodes and 262,144 edges use (8192, 262144) capacities.

Source Type Shape Fields
live:trace.counts int32 (4,) Nodes, edges, tokens, layers
live:trace.node_i int32 (N, 4) Kind, layer, position, feature or token ID
live:trace.node_f float32 (N, 4) Activation, influence, top-logit effect, reserved
live:trace.edge_i int32 (E, 2) Source and target node rows
live:trace.edge_f float32 (E, 2) Weight and normalized weight
live:trace.label_text uint32 (32 + N, 16) Packed prompt and node labels

Node kinds are token 0, feature 1, error 2, and logit 3. Token layers use -1, logit layers use the layer count, and feature and error layers use zero-based indices. trace.read pages through full JSON node and edge records up to 256 items per page.

Graph Pieces

attribution_graph@1 places prompt tokens horizontally and model layers vertically. Bind graph_counts to live:trace.counts and bind node_i, node_f, edge_i, edge_f, and label_text to their live:trace.* sources. Its selected_node parameter reads shared state selected_node, highlights connected edges, and displays the selected label.

token_strip@1 binds to the five live:examples.* tensors documented in Example Panel Bindings. logit_bars@1 binds either to live:trace.probs for static baseline logits or to live:output.probs, live:output.tokens, and live:output.label_text for live baseline-versus-steered comparisons. heat_grid@1 binds to live:patch.grid to render layer-by-position activation recovery.

Feature Steering

Backend.intervene_logits and live InterventionExecutor hooks support clamp, scale, add, and ablate interventions. On a ReplacementModel, feature clamp and scale edits call ReplacementModel.feature_intervention directly. On a standalone HookedTransformer with an SAE, edits run in delta form (x + (sae.decode(modified) - sae.decode(base))), which preserves exact logits at identity.

Output(backend, interventions) compares clean and steered next-token distributions and publishes live:output.probs (2, 10), live:output.tokens (10,), and live:output.label_text (10, 16). Output.generate(prompt) generates 24 greedy baseline tokens and 24 greedy steered tokens while keeping permanent replacement hooks intact.

Activation Patching

caliper.interp.patch_sweep(backend, interventions, clean_prompt=..., corrupted_prompt=..., metric=...) runs a hook_resid_pre activation patching sweep across all (layer, position) cells. Clean and corrupted prompts must tokenize to equal lengths within the 32-token cap. metric specifies distinct positive_token and negative_token vocabulary IDs. The sweep runs at most 32 cells per forward pass, publishes normalized recovery as live:patch.grid (layers, positions), and returns full summary values.

Refusals and Limits

MemoryGuard enforces a 40 GB process limit (40,000,000,000 bytes) and an 8 GB host available memory reserve (8,000,000,000 bytes). Missing dictionaries, unknown probe points, out-of-range features, and unequal patch prompt lengths return structured refusals. See Interpretation Tools for MCP request schemas and paging bounds.

Reference Calls

The caliper.interp package exports the following functions and classes.

publish

publish(name, tensor)

Register a named tensor for read-only use by a derive half.

load_backend

load_backend(
    model_ref: str | Path,
    dictionary_ref=None,
    *,
    sae_id: str | None = None,
    device: str | device | None = None,
    dtype: dtype | None = None,
    guard=None,
    progress=None,
) -> Backend

Load a staged ReplacementModel or HookedTransformer (+ optional SAE) backend.

Tracer

Cached circuit attribution and atomic tensor publication over Backend.

Graph

Display graph with completeness from all sources before display pruning.

Node id values identify semantic nodes across pruning. Edge endpoints are display row indices. read pages records without a device readback. tensors contains exactly the five names of the public trace contract.

from_circuit_tracer

from_circuit_tracer(
    ct_graph,
    pruned=None,
    *,
    replacement_score: float | None = None,
    completeness_score: float | None = None,
    node_influence: Tensor | None = None,
    tokenizer=None,
    device="cpu",
    trace_id: str = "trace-1",
    max_nodes: int = LARGE_NODE_CAP,
    max_edges: int = LARGE_EDGE_CAP,
    node_threshold: float | None = None,
    edge_threshold: float | None = None,
    show_more: bool = False,
    feature_labels: dict[tuple[int, int], str] | None = None,
    settings: dict | None = None,
    memory=None,
) -> "Graph"

Convert a CPU-resident circuit_tracer.Graph (pruning if needed) into a Caliper Graph.

publish

publish()

Publish structural tensors and all graph and bar inputs on their existing device.

read

read(*, node_offset=0, node_limit=128, edge_offset=0, edge_limit=256)

Return bounded JSON records, totals, and completeness summary.

FeatureIndex

Memory-mapped on-disk feature example table and promoted-token summaries.

resolve

resolve(path: str | PathLike | None) -> Path | None

Resolve an index folder from an index path or dictionary folder.

LiveFeatureIndex

Zero-disk-setup index backed by Backend.top_examples and W_dec @ W_U.

patch_sweep

patch_sweep(
    backend,
    interventions,
    *,
    clean_prompt: str,
    corrupted_prompt: str,
    metric: dict,
    guard=None,
) -> dict

Patch each hook_resid_pre cell (up to 32 cells per batch) and publish patch.grid.

make_tiny_backend

make_tiny_backend(
    *,
    device: str | device = "cpu",
    dtype: dtype = torch.float32,
    n_layers: int = 2,
    d_model: int = 16,
    n_heads: int = 2,
    d_mlp: int = 32,
    n_features: int = 32,
    d_vocab: int = 128,
    seed: int = 42,
    guard=None,
) -> Backend

Create a deterministic offline 2-layer ReplacementModel + TranscoderSet fixture.

make_tiny_sae_backend

make_tiny_sae_backend(
    *,
    device: str | device = "cpu",
    dtype: dtype = torch.float32,
    layer: int = 0,
    d_model: int = 16,
    n_features: int = 32,
    d_vocab: int = 128,
    seed: int = 42,
    guard=None,
) -> Backend

Create a deterministic offline 2-layer HookedTransformer + SAELens StandardSAE fixture.