Logical Distillation of Transformer Encoders

Matteo Forasassi, Thomas Gärtner, Thomas Lukasiewicz, Sagar MalhotraNeurIPS 2026

TL;DR

What if you could crack open a black-box Transformer and translate its dense matrix multiplications directly into crisp, human-readable logical rules? By exploiting a deep theoretical connection between Transformer layers and temporal logic, the authors introduce a layer-by-layer distillation algorithm that turns trained sequence models into stacks of logical decision trees. They use this to extract succinct, biologically meaningful rules directly from a protein language model with minimal loss in accuracy.

What Problem Does This Paper Address?

Transformers are brilliant at finding patterns in sequences—whether words in a sentence or amino acids in a protein—but they are notoriously opaque. When a biological language model flags a peptide as toxic, a scientist doesn't just want a high-dimensional activation vector; they want to know the rule driving that classification.

Existing mechanistic interpretability tools, like sparse autoencoders, are great at isolating individual "concepts" (sparse features) inside a network's activations. However, they struggle to show compositionality—how those features interact and depend on order across a sequence. Rather than reverse-engineering individual neurons, the authors ask: can we distill the entire Transformer encoder into an explicit, readable symbolic formula that captures sequence-wide patterns?

Methods

The magic here bridges formal logic and machine learning. Recent theoretical work proved that hard-attention Transformer encoders have the exact expressive power of Linear Temporal Logic (LTL)—a formal language for describing sequence order using operators like Next ($X$, meaning "true at the very next step") and Until ($U$, meaning "this stays true until that becomes true"). Crucially, theory shows a tight hierarchy: each layer in a Transformer adds at most one unit of temporal nesting depth.

Guided by this, the authors introduce a novel symbolic surrogate model called the Iterated Temporal Decision Tree (ITDT), paired with a layer-wise distillation algorithm:

  • Temporal Decision Trees (TDTs): Standard decision trees, except the split conditions at internal nodes are LTL formulas evaluated at specific positions in a sequence. Each leaf naturally corresponds to a logical formula representing the path taken to reach it.
  • Stacking and Temporal Expansion: An ITDT stacks $k$ trees to mirror a $k$-layer Transformer. After Tree $i$ is trained, its leaf formulas are harvested and "temporally expanded" (wrapped in $X$ and $U$ operators) to create deeper logical features for Tree $i+1$.
Figure 1: (Left) We have a temporal decision tree for ψ = q ∨Xr on w = ppqrr. Positive outputs ({w2, w3}) are in red, and negative outputs ({w0, w1, w4}) are in blue. Each leaf represents a formula: the left leaf χL := ¬q ∧¬Xr, the middle leaf χM := ¬q ∧Xr, and the right leaf χR := q. (Right) In an ITDT, we can add a second TDT layer T2 evaluating χR U χM.
  • Layer-wise Distillation: Instead of training the trees solely on the final label, Layer $i$ of the trained Transformer supervises Tree $i$ via standard multi-output regression—the tree learns splits that best approximate the Transformer's hidden token representations at that specific depth.
  • Practical Tweaks: Because generating all possible logical combinations explodes combinatorially, the practical algorithm only expands the formulas actually discovered at the tree leaves. For real-world biology tasks, the authors also add Count features (tracking how often a motif occurs), skip regressing on the final Transformer layer to avoid over-compressed representations, and slap a simple linear threshold (ridge or Lasso regression) on top for the final sequence prediction.

Datasets

The authors do not introduce new benchmarks, instead testing on two established domains:

  • Synthetic LTL Benchmarks: Nine standard formal-methods tasks covering existence, absence, and universality properties across synthetic string traces (lengths 10 to 100).
  • Real-World Protein Classification: Four binary peptide classification datasets—CPP924 (cell-penetrating), ToxinPred 3.0 (toxicity), ACP740 (anticancer), and AMP Scanner V2 (antimicrobial)—used to fine-tune and distill the 8M-parameter ESM-2-T6 protein language model.

Key Insights and Ideas

  • Theory works in soft-attention practice: The layer-by-layer LTL correspondence was proven for idealized hard-attention Transformers. Yet, using it as an inductive bias works remarkably well on standard, messy soft-attention Transformers.
  • Intermediate Transformer states are superior teachers: When the authors replaced the Transformer's intermediate hidden states with random vectors or even the ground-truth labels broadcast across every layer, performance dropped. The Transformer's internal layer-wise representations genuinely guide the trees toward better intermediate concepts.
  • Order matters, not just counts: Stripping away the temporal operators ($X$, $U$) and leaving only amino-acid counts caused accuracy to drop across every single protein benchmark, proving that sequential order and motif arrangement are vital to what the Transformer learned.

Key Results

  • Near-perfect logic recovery: On all nine synthetic LTL benchmarks, ITDTs matched the teacher Transformer with near-perfect accuracy, recovering compact trees of depth 2 to 4 and temporal depth $\le 1$.
  • High fidelity on proteins: Distilling ESM-2-T6 into ITDTs preserved almost all of the neural network's predictive power: 0.92 vs. 0.92 accuracy on CPP924, 0.84 vs. 0.86 on ACP740, 0.85 vs. 0.88 on ToxinPred 3.0, and 0.87 vs. 0.92 on AMP Scanner V2.
Table 1: Accuracy (mean ± std) on four protein classification datasets. ESM-2-T6 is the fine- tuned teacher model. ITDT is distilled with layer-wise ESM-2 supervision (RQ2). ITDT-Random substitutes transformer representations with random Gaussian vectors (RQ3). ITDT-GT Guidance replaces them with the ground-truth label broadcast to all sequence positions (RQ3). ITDT-No Expansion removes temporal expansion, retaining only aggregation features (RQ4).
  • Biochemical alignment: Using Lasso regression to extract the top sparse rules, the authors ran a blind "LLM-as-expert" test (via GPT-5.5), giving it only a distilled logical rule and asking which biological class it described. The evaluator guessed right 90.0% of the time on AMP, 76.7% on ToxinPred 3.0, and 70.0% on ACP740 and CPP924—showing the distilled rules align with real biochemical priors rather than arbitrary statistical quirks.

Limitations and Future Work

The main bottleneck is vocabulary scaling. Even with their restricted temporal expansion, the pool of candidate logical formulas grows quadratically with the number of formulas being expanded. While totally manageable for the 20-letter amino acid alphabet in proteins, scaling this current implementation to large-vocabulary domains like natural language (with tens of thousands of tokens) remains an open challenge. However, once an ITDT is distilled, evaluating its fixed LTL formulas scales linearly with sequence length—beating the quadratic self-attention cost of the original Transformer.

Summary updated

Abstract

We present an algorithm that distills transformer encoders into logical formulas. Interpretable surrogates for transformers are important for safety and scientific knowledge discovery. Recent work extracts sparse latent features from transformer activations, but these features alone do not express how information is composed across a sequence. Building on theoretical connections between transformer expressivity and linear temporal logic (LTL), we introduce iterated temporal decision trees (ITDTs), a model that can express any LTL formula as a sequence of decision trees. Each decision tree builds on the previous ones, producing LTL formulas of increasing temporal depth. Our algorithm distills a transformer encoder into an ITDT. Empirically, our algorithm recovers succinct and accurate formulas on established LTL learning benchmarks and distills interpretable rules for multiple protein sequence datasets from ESM-2-T6, an established protein language model.
BibTeX
@inproceedings{forasassi2026logical,
  author = {Matteo Forasassi and Thomas Gärtner and Thomas Lukasiewicz and Sagar Malhotra},
  title = {{Logical Distillation of Transformer Encoders}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=vcu7CdwIWi},
}
Figure