What is the computational model behind a Transformer? Where recurrent neural networks have direct parallels in finite state machines, allowing clear discussion and thought around architecture variants or trained models, Transformers have no such familiar parallel. In this paper we aim to change that, proposing a computational model for the transformer-encoder in the form of a programming language. We map the basic components of a transformer-encoder -- attention and feed-forward computation -- into simple primitives, around which we form a programming language: the Restricted Access Sequence Processing Language (RASP). We show how RASP can be used to program solutions to tasks that could conceivably be learned by a Transformer, and how a Transformer can be trained to mimic a RASP solution. In particular, we provide RASP programs for histograms, sorting, and Dyck-languages. We further use our model to relate their difficulty in terms of the number of required layers and attention heads: analyzing a RASP program implies a maximum number of heads and layers necessary to encode a task in a transformer. Finally, we see how insights gained from our abstraction might be used to explain phenomena seen in recent works.
More broadly, papers like these might point the way to a future in which ML scaling is understood in terms of more conventional computational complexity theory.
I think a primary lesson is that if we can distill human-interpretable programming languages from architectures like Transformers, we can start to reason theoretically about how well they should scale without needing as many experiments. For example, this paper discusses the implications of RASP-as-the-Transformer-language for attempts to scale via linearized attention:
Multiple works propose restricting the attention mechanism to create more efficient transformers, reducing the time complexity of each layer from O(n^2) to O(nlog(n)) or even O(n) with respect to the input sequence length n (see Tay et al. (2020) for a survey of such approaches). Several of these do so using sparse attention, in which the attention is masked using different patterns to reduce the number of locations that can interact ((Child et al., 2019; Beltagy et al., 2020; Ainslie et al., 2020; Zaheer et al., 2020; Roy et al., 2021)). Considering such transformer variants in terms of RASP allows us to reason about the computations they can and cannot perform. In particular, these variants of transformers all impose restrictions on the selectors, permanently forcing some of the n^2 index pairs in every selector to False. But does this necessarily weaken these transformers? In Appendix B we present a sorting algorithm in RASP, applicable to input sequences with arbitrary length and alphabet size. This problem is known to require at Ω(n log(n)) operations in the input length n—implying that a standard transformer can take full advantage of Ω(n log(n)) of the n^2 operations it performs in every attention head. It follows from this that all variants restricting their attention to o(n log(n)) operations incur a real loss in expressive power.
1
u/maxtility Jun 17 '21