Contextual Counting: A Mechanistic Study of Transformers on a Quantitative Task
Not on the map. Why?
Abstract (arXiv)
Transformers have revolutionized machine learning across diverse domains, yet understanding their behavior remains crucial, particularly in high-stakes applications. This paper introduces the contextual counting task, a novel toy problem aimed at enhancing our understanding of Transformers in quantitative and scientific contexts. This task requires precise localization and computation within datasets, akin to object detection or region-based scientific analysis. We present theoretical and empirical analysis using both causal and non-causal Transformer architectures, investigating the influence of various positional encodings on performance and interpretability. In particular, we find that causal attention is much better suited for the task, and that no positional embeddings lead to the best accuracy, though rotary embeddings are competitive and easier to train. We also show that out of distribution performance is tightly linked to which tokens it uses as a bias term.
Key facts. Cited by 6 papers in the map (data through 7 Oct 2026). Builds on 28 of its 36 references that are in the map. First posted on arXiv 30 May 2024.
Builds on
28 of 36 references.
References · 28
- 2017Attention Is All You Needcited by 59,932
- 2020Language Models are Few-Shot Learnerscited by 32,199
- 2020An Image is Worth 16x16 Words: Transformers for Image Recognition at Scalecited by 29,915
- 2023LLaMA: Open and Efficient Foundation Language Modelscited by 13,958
- 2017A Unified Approach to Interpreting Model Predictionscited by 6,488
- 2016"Why Should I Trust You?": Explaining the Predictions of Any Classifiercited by 5,040
- 2021RoFormer: Enhanced Transformer with Rotary Position Embeddingcited by 4,581
- 2022Locating and Editing Factual Associations in GPTcited by 2,609
- 2022Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 smallcited by 1,130
- 2021Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolationcited by 1,085
- 2019What Does BERT Look At? An Analysis of BERT's Attentioncited by 994
- 2023Vision Transformers Need Registerscited by 844
16 more
- 2023Progress measures for grokking via mechanistic interpretabilitycited by 822
- 2023Dissecting Recall of Factual Associations in Auto-Regressive Language Modelscited by 512
- 2023Faith and Fate: Limits of Transformers on Compositionalitycited by 496
- 2023ClimaX: A foundation model for weather and climatecited by 325
- 2023The Impact of Positional Encoding on Length Generalization in Transformerscited by 319
- 2019A Multiscale Visualization of Attention in the Transformer Modelcited by 292
- 2019Theoretical Limitations of Self-Attention in Neural Sequence Modelscited by 286
- 2022Transformers Learn Shortcuts to Automatacited by 260
- 2023Birth of a Transformer: A Memory Viewpointcited by 154
- 2021Saturated Transformers are Constant-Depth Threshold Circuitscited by 137
- 2023Teaching Arithmetic to Small Transformerscited by 121
- 2022Unveiling Transformers with LEGO: a synthetic reasoning taskcited by 105
- 2023Multiple Physics Pretraining for Physical Surrogate Modelscited by 90
- 2023How Do Transformers Learn Topic Structure: Towards a Mechanistic Understandingcited by 85
- 2024Transformers, parallel computation, and logarithmic depthcited by 75
- 2023Length Generalization in Arithmetic Transformerscited by 64
Built on by
Citing papers per year: 2024: 3; 2025: 3.
6 papers.
- 2024When Can Transformers Count to n?cited by 22
- 2024More Expressive Attention with Negative Weightscited by 9
- 2024Counting in Small Transformers: The Delicate Interplay between Attention and Feed-Forward Layerscited by 5
- 2025Provable Benefits of In-Tool Learning for Large Language Modelscited by 4
- 2025Understanding Counting Mechanisms in Large Language and Vision-Language Modelscited by 4
- 2025Generating particle physics Lagrangians with transformerscited by 1
- 2025Understanding Counting Mechanisms in Large Language and Vision-Language Models21 Nov 2025
- 2025Provable Benefits of In-Tool Learning for Large Language Models28 Aug 2025
- 2025Generating particle physics Lagrangians with transformers16 Jan 2025
- 2024More Expressive Attention with Negative Weights11 Nov 2024
- 2024When Can Transformers Count to n?21 Jul 2024
- 2024Counting in Small Transformers: The Delicate Interplay between Attention and Feed-Forward Layers16 Jul 2024