File size: 2,069 Bytes
83fedec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73ca1e4
 
 
5b7b707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
---
dataset_info:
  features:
  - name: task
    dtype: string
  - name: x
    dtype: string
  - name: Fx
    dtype: string
  - name: Gx
    dtype: string
  - name: GFx
    dtype: string
  - name: FGx
    dtype: string
  splits:
  - name: train
    num_bytes: 3065527
    num_examples: 36802
  download_size: 874325
  dataset_size: 3065527
configs:
- config_name: default
  data_files:
  - split: train
    path: data/train-*
license: mit
size_categories:
- 10K<n<100K
---

# How Do Language Models Compose Functions?

[[Paper]](https://arxiv.org/abs/2510.01685)
[[Repository]](https://github.com/apoorvkh/composing-functions)

**Apoorv Khandelwal & Ellie Pavlick**

**Abstract:** While large language models (LLMs) appear to be increasingly capable of solving compositional tasks, it is an open question whether they do so using compositional mechanisms. In this work, we investigate how feedforward LLMs solve two-hop factual recall tasks, which can be expressed compositionally as $g(f(x))$. We first confirm that modern LLMs continue to suffer from the "compositionality gap": i.e. their ability to compute both $z = f(x)$ and $y = g(z)$ does not entail their ability to compute the composition $y = g(f(x))$. Then, using logit lens on their residual stream activations, we identify two processing mechanisms, one which solves tasks *compositionally*, computing $f(x)$ along the way to computing $g(f(x))$, and one which solves them *directly*, without any detectable signature of the intermediate variable $f(x)$. Finally, we find that which mechanism is employed appears to be related to the embedding space geometry, with the idiomatic mechanism being dominant in cases where there exists a linear mapping from $x$ to $g(f(x))$ in the embedding spaces.

## Loading the dataset

```python
from datasets import load_dataset

dataset = load_dataset("apoorvkh/composing-functions")
```

```python
# print(dataset["train"][1])

{'task': 'antonym-spanish', 'x': 'experimental', 'Fx': 'traditional', 'Gx': 'experimental', 'GFx': 'tradicional', 'FGx': 'tradicional'}
```