LLMs Process Lists With General Filter Heads

Arnab Sen Sharma, Giordano Rogers, Natalie Shapira, David Bau · 2025

Read LLMs Process Lists With General Filter Heads by Arnab Sen Sharma, Giordano Rogers, Natalie Shapira, David Bau (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2510-26784.txt.

Introduction

Arnab Sen Sharma, Giordano Rogers, Natalie Shapira, and David Bau look inside language models for a built-in filtering operation. In functional programming, filter keeps the items of a list that pass a test. The team uses causal mediation analysis across many list-processing tasks to see how models do the same job.

Sen Sharma and his three co-authors find a small set of attention heads, which they call filter heads, that hold a compact encoding of the test in their query states. That encoding is portable: lifted out, it runs the same filtering on new collections, in other formats and languages, even on other tasks. In some cases, though, models use a second strategy, marking each item with a pass or fail flag as they go. The results show transformer models can build human-readable versions of abstract operations.

Catalog record

CREATORArnab Sen Sharma, Giordano Rogers, Natalie Shapira, David Bau
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE86 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2510-26784

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

LLMs Process Lists With General Filter Heads Arnab Sen Sharma, Giordano Rogers, Natalie Shapira, David Bau Abstract We investigate the mechanisms underlying a range of list-processing tasks in LLMs, and we find that LLMs have learned to encode a compact, causal representation of a general filtering operation that mirrors the generic “filter” function of functional programming. Using causal mediation analysis on a diverse set of list-processing tasks, we find that a small number of attention heads, which we dub filter heads , encode a compact representation of the filtering predicate in their query states at certain tokens. We demonstrate that this predicate representation is general and portable: it can be extracted and reapplied to execute the same filtering operation on different collections, presented in different formats, languages, or even in tasks. However, we also identify situations where transformer LMs can exploit a different strategy for filtering: eagerly evaluating if an item satisfies the predicate and storing this intermediate result as a flag directly in the item representations. Our results reveal that transformer LMs can develop human-interpretable implementations of abstract computational operations that generalize in ways that are surprisingly similar to strategies used in traditional functional programming patterns. 1 Introduction When asked to find the fruit in a list, language models reveal a surprisingly systematic mechanism: they don’t solve each filtering task anew, but instead encode predicates into portable representations. This neural representation of “is this a fruit?” can be extracted from one context and applied to a different list, presented in a different format, in a different language, and to some extent to a different task. These abstract, reusable operations suggest that transformers develop modular computational primitives rather than task-specific heuristics. Figure 1: A filter head [35,19] in Llama-70B encodes a compact representation of the predicate “is this fruit?” . (a) Within a prompt p_{\mathrm{src}} to find a fruit in a list, we examine the attention head’s behavior at the last token “:” (b) The head focuses its attention on the one fruit in the list. (c) We examine the same attention head’s behavior in a second prompt p_{\mathrm{dest}} searching a different list for a vehicle (d) and we also examine the behavior of the head when patching its query state to use the q_{\mathrm{src}} vector from the source context. (e) The head attends to the vehicle but then (f) redirects its attention to the fruit in the new list after the query vector is patched. (g) A sparse set of attention heads work together to conduct filtering over a wide range of predicates. These filter heads are concentrated in the middle layers (out of 80 layers in Llama-70B). To understand this phenomenon systematically, we turn to Marr’s three levels of analysis ( Marr 1982 ) . At the computational level, we identify what is being computed: the selection of elements satisfying a predicate. At the algorithmic level, we reveal how this is achieved: through a three phase computation corresponding to a map , filter , and reduce , occurring in that order. The map step is equivalent to populating the latents of the items in a list with the right associations or semantic information, a step that has been documented in prior literature ( Geva et al. 2023 ; Meng et al. 2022 ) . In this work we focus on the non-trivial computation step, filter , that follows after map. At the implementation level, we reveal how filtering is implemented in LMs: through specialized attention heads, which we dub filter heads , that encode predicates as geometric directions in query space. We find that these heads, concentrated in the middle layers of the LM, remain largely shared even as the specific predicate varies. This framework allows us to move beyond simply observing that LMs can filter, to understanding the explicit mechanisms through which list-processing operations emerge from the transformer architecture. Our analysis yields three key insights: Localized Mechanism. The list processing algorithm is implemented in a consistent set of localized components: a set of attention heads that we call filter heads. These heads encode a “compiled” representation of the predicate as query states at specific tokens — typically where the LM is required to produce its answer. These query states interact with the key states that carry semantic information of the list items, producing attention patterns that select the items satisfying the predicate. Generalization. These filter heads are not specific to a single predicate, but can encode a distribution of predicates. And this encoding is sufficiently abstract that it can be extracted from one context and transported to another context to trigger the same filtering operation on a different collection of items, presented in a different format, in a different language, even in a different reduce task that follows after the filtering step. Computational Redundancy. Additionally, our investigations reveal that LMs can perform filtering in two complementary ways: lazy evaluation via filter heads vs eager evaluation by storing is_match flags directly in the item latents. This dual implementation strategy mirrors the fundamental lazy/eager evaluation strategies in functional programming ( Henderson & Morris Jr 1976 ; Friedman et al. 1976 ) . This second route reveals a broader principle in neural computations: transformer LMs can maintain multiple pathways for the same operation ( McGrath et al. 2023 ; Wang et al. 2023 ) and can dynamically select between them based on what information is available. We validate these findings through experiments across six different filter-reduce tasks of varying complexity, each requiring the LM to filter based on different information before performing a reduce step to provide a specific answer. We test the portability of the “compiled" predicate across different presentation format, language, and tasks. We conduct ablation studies to confirm the necessity of filter heads when the LM performs filter operations. Finally, we demonstrate that the learned predicate representations can serve as zero-shot probes for concept detection, offering a training-free alternative to traditional linear probing methods. 2 Method 2.1 Backgrounds and Notations Language Model. An autoregressive transformer language model, M:\mathcal{X}\to\mathcal{Y} over a vocabulary \mathcal{V} , maps a sequence of tokens x=\{x_{1},x_{2},\ldots,x_{n}\;|\;x_{i}\in\mathcal{V}\} to y\in\mathbb{R}^{|\mathcal{V}|} , which is a probability distribution over the next token continuation of x . Internally, M has L layers, where the output of the \ell^{\mathrm{th}} layer is computed as, h^{\ell}=h^{\ell-1}+m^{\ell}+\sum_{j\leq J}a^{\ell j} . Here, m^{\ell} is the output of the MLP, and a^{\ell j} is the contribution of j^{\mathrm{th}} attention head. For an individual head, its contribution to h^{\ell} at token position t is computed as: \displaystyle a^{\ell j}_{t} \displaystyle=\sum_{i\leq t}h^{\ell-1}_{i}W^{\ell j}_{OV}\cdot\mathrm{Attn}(q_{t},K)_{i} (1) \displaystyle\mathrm{where}\quad q_{t}=h^{\ell-1}_{t}W_{Q}^{\ell j},\quad K= \displaystyle h^{\ell-1}_{\leq t}W_{K}^{\ell j},\quad\mathrm{and}\quad\mathrm{Attn}(q_{t},K)=\mathrm{softmax}\left(\frac{q_{t}K^{T}}{\sqrt{d_{\text{head}}}}\right) Here, \leq t denotes all tokens up to the current token t . Following Elhage et al. 2021 , we combine the value projection W_{V}^{\ell j} and out projection W_{O}^{\ell j} in a single W_{OV}^{\ell j} . From here onward we will denote the j^{\mathrm{th}} attention head at layer \ell as [\ell,\,j] . Filter Tasks. In functional programming, the filter operation is used to select items from a collection that satisfy specific criteria. filter takes two arguments: the collection, and a predicate function that returns a boolean value indicating whether an item meets the criteria. Formally: \displaystyle\texttt{filter}(\mathcal{C},\psi) \displaystyle=\{c\in\mathcal{C}\mid\psi(c)\text{ is True}\} (2) \displaystyle\text{where}\quad\mathcal{C} \displaystyle=\{c_{1},c_{2},\ldots,c_{n}\}\text{ is a

Read the complete text