AI/ML Mechanistic Interpretability Transcoders Circuits PyTorch

From Features to Circuits: Transcoders, Crosscoders & Attribution Graphs


In Disentangling Polysemantic Superposition, we used TopK and JumpReLU Sparse Autoencoders (SAEs) to decompose a single residual-stream vector into monosemantic features. That answers what concepts exist at layer $\ell$ – but not how layer $\ell$ computes layer $\ell+1$. Because standard SAEs reconstruct a layer's activations from themselves, the nonlinear MLP sits outside the sparse basis, forcing circuit analysts to differentiate through dense opaque neurons. Transcoders (Dunefsky, Chlenski & Nanda, NeurIPS 2024, arXiv:2406.11944) and Crosscoders solve this by placing the sparse bottleneck across the MLP or across multiple layers simultaneously.


1. Why Transcoders Unlock Input-Invariant Feature Circuits

Instead of minimizing $\|\mathbf{x} - \mathrm{SAE}(\mathbf{x})\|_2^2$, a Transcoder takes the pre-MLP residual stream $\mathbf{x}_{\text{in}}^{(\ell)}$ and predicts the post-MLP output $\mathbf{y}_{\text{out}}^{(\ell)} = \mathrm{MLP}^{(\ell)}(\mathbf{x}_{\text{in}}^{(\ell)})$ through a wide sparse latent layer $\mathbf{f}^{(\ell)} \in \mathbb{R}^{M}_{\ge 0}$ ($M \gg d$):

Transcoder Objective & Exact Input-Invariant Edge Attribution (Dunefsky et al., 2024)
$$\mathbf{f}^{(\ell)} = \mathrm{JumpReLU}_{\boldsymbol{\theta}}\!\left(W_{\text{enc}}^{(\ell)}\mathbf{x}_{\text{in}}^{(\ell)} + \mathbf{b}_{\text{enc}}^{(\ell)}\right), \qquad \hat{\mathbf{y}}_{\text{out}}^{(\ell)} = W_{\text{dec}}^{(\ell)}\mathbf{f}^{(\ell)} + \mathbf{b}_{\text{dec}}^{(\ell)}$$ $$\mathcal{A}_{i \to j}^{(\ell \to \ell+1)}(\mathbf{x}) = \underbrace{f_i^{(\ell)}(\mathbf{x})}_{\text{input-dependent}} \cdot \underbrace{\left(\mathbf{w}_{\text{dec}, i}^{(\ell)} \cdot \mathbf{w}_{\text{enc}, j}^{(\ell+1)}\right)}_{\text{input-invariant circuit weight}}$$

Look at that factorization: once the MLP is replaced by a Transcoder, the direct contribution from feature $i$ in layer $\ell$ to feature $j$ in layer $\ell+1$ splits cleanly into a scalar activation $f_i^{(\ell)}(\mathbf{x})$ times a static, prompt-independent inner product $\mathbf{w}_{\text{dec}, i}^{(\ell)} \cdot \mathbf{w}_{\text{enc}, j}^{(\ell+1)}$. For attention-mediated paths, freezing the attention pattern $A^{(h)}$ yields a linear OV-circuit matrix $W_{OV}^{(h)}$ so the exact attribution weight is $\mathbf{w}_{\text{dec}, i}^{(\ell)} W_{OV}^{(h)} \mathbf{w}_{\text{enc}, j}^{(\ell')}$.

Standard SAE x_out → sparse f → x̂_out MLP stays dense & opaque Best for single-layer probes MLP Transcoder x_in → sparse f → ŷ_MLP Linear pull-back: W_dec · W_enc Builds exact Attribution Graphs Multi-Layer Crosscoder Σ_ℓ W_enc^(ℓ) x^(ℓ) → shared f Tracks cross-layer & base-vs-chat Eliminates duplicate features
Transcoders linearize MLP computation for circuit tracing; Crosscoders share a single sparse dictionary across layers or across two fine-tuned checkpoints.

2. Crosscoders & Model Diffing (Base vs. RL-Tuned Checkpoints)

A Crosscoder generalizes the sparse encoder to read from a set of activation streams $\{\mathbf{x}^{(k)}\}_{k=1}^K$ – either layers $1\dots L$ of one model, or the same layer in a base model and an RL-aligned model – and decode back into all $K$ streams with separate decoders $W_{\text{dec}}^{(k)}$. By inspecting the decoder norm ratio $\|\mathbf{w}_{\text{dec}, i}^{(\text{chat})}\| / (\|\mathbf{w}_{\text{dec}, i}^{(\text{base})}\| + \|\mathbf{w}_{\text{dec}, i}^{(\text{chat})}\|)$, you immediately isolate chat-exclusive features (refusal triggers, sycophancy detectors, chain-of-thought verification tokens) that were created during post-training.

💻 Python: JumpReLU Transcoder & Input-Invariant Circuit Pull-Back WeightPyTorch
import math
import torch
import torch.nn as nn

class JumpReLUTranscoder(nn.Module):
    """Sparse MLP replacement: maps MLP input x_in -> sparse features -> MLP output y_out."""
    def __init__(self, d_model: int, d_features: int, init_threshold: float = 0.1):
        super().__init__()
        self.W_enc = nn.Parameter(torch.randn(d_model, d_features) * (d_model ** -0.5))
        self.b_enc = nn.Parameter(torch.zeros(d_features))
        self.W_dec = nn.Parameter(torch.randn(d_features, d_model) * (d_features ** -0.5))
        self.b_dec = nn.Parameter(torch.zeros(d_model))
        self.log_theta = nn.Parameter(torch.full((d_features,), math.log(init_threshold)))

    def encode(self, x_in: torch.Tensor) -> torch.Tensor:
        pre = x_in @ self.W_enc + self.b_enc
        theta = self.log_theta.exp()
        # Inference forward; training uses a bandwidth STE kernel for log_theta gradients
        return pre * (pre > theta).to(pre.dtype)

    def forward(self, x_in: torch.Tensor):
        f = self.encode(x_in)
        y_hat = f @ self.W_dec + self.b_dec
        return y_hat, f

@torch.no_grad()
def input_invariant_circuit_weight(tc_src: JumpReLUTranscoder, tc_dst: JumpReLUTranscoder,
                                   i_src: int, j_dst: int) -> float:
    """Direct residual-stream pull-back weight between transcoder feature i (layer L) and j (layer L+1)."""
    return float(torch.dot(tc_src.W_dec[i_src], tc_dst.W_enc[:, j_dst]))
Attribution Graph Pitfalls: (1) Error-node leakage: if your Transcoder explains only $85\%$ of MLP variance, the residual error node $\mathbf{e}^{(\ell)} = \mathbf{y}_{\text{out}}^{(\ell)} - \hat{\mathbf{y}}_{\text{out}}^{(\ell)}$ can silently carry the real circuit! Always measure replacement-model faithfulness (KL divergence between the original logits and the model with all MLPs swapped for Transcoders). (2) LayerNorm linearization: treat RMSNorm scale factors as frozen constants during backward attribution so edge scores sum cleanly to the target logit.
ToolInput → OutputSolves Superposition?Enables Direct Circuit Graph?
TopK / JumpReLU SAE$\mathbf{x}^{(\ell)} \to \hat{\mathbf{x}}^{(\ell)}$YesNo (MLP nonlinearity blocks weight pull-back)
Transcoder$\mathbf{x}_{\text{in}}^{(\ell)} \to \hat{\mathbf{y}}_{\text{MLP}}^{(\ell)}$YesYes (exact $W_{\text{dec}}^{(\ell)} \cdot W_{\text{enc}}^{(\ell+1)}$ edges)
Crosscoder$\{\mathbf{x}^{(k)}\} \to \{\hat{\mathbf{x}}^{(k)}\}$Yes (cross-layer/model)Tracks feature birth, persistence & model diffs