{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/thinking-like-transformers-1","title":"Thinking Like Transformers","arxiv_id":"2106.06981","date":"2021-06-13","proceeding":null,"authors":["Gail Weiss","Yoav Goldberg","Eran Yahav"],"abstract":"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.","url_abs":"https://arxiv.org/abs/2106.06981v2","url_pdf":"https://arxiv.org/pdf/2106.06981v2.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"thinking-like-transformers-1","repo_url":"https://github.com/tech-srl/RASP","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"none","reach":null},{"paper_slug":"thinking-like-transformers-1","repo_url":"https://github.com/deepmind/tracr","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"jax","reach":null},{"paper_slug":"thinking-like-transformers-1","repo_url":"https://github.com/google-deepmind/tracr","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"jax","reach":{"status":"ok","spdx":"Apache-2.0"}},{"paper_slug":"thinking-like-transformers-1","repo_url":"https://github.com/princeton-nlp/transformerprograms","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"thinking-like-transformers-1","repo_url":"https://github.com/tvergara/tracr-injection","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"jax","reach":{"status":"ok"}}],"tasks":[{"task_slug":"question-answering","task_name":"Question Answering"}],"methods":[{"method_slug":"absolute-position-encodings","method_name":"Absolute Position Encodings"},{"method_slug":"adam","method_name":"Adam"},{"method_slug":"attention","method_name":"Attention"},{"method_slug":"bpe","method_name":"BPE"},{"method_slug":"dense-connections","method_name":"Dense Connections"},{"method_slug":"dropout","method_name":"Dropout"},{"method_slug":"label-smoothing","method_name":"Label Smoothing"},{"method_slug":"layer-normalization","method_name":"Layer Normalization"},{"method_slug":"linear-layer","method_name":"Linear Layer"},{"method_slug":"multi-head-attention","method_name":"Multi-Head Attention"},{"method_slug":"position-wise-feed-forward-layer","method_name":"Position-Wise Feed-Forward Layer"},{"method_slug":"residual-connection","method_name":"Residual Connection"},{"method_slug":"softmax","method_name":"Softmax"},{"method_slug":"transformer","method_name":"Transformer"}],"datasets_introduced":[],"methods_introduced":[],"results":[{"leaderboard":"/sota/question-answering-on-newsqa","task":"Question Answering","dataset":"NewsQA","model":"OpenAI/o4-mini-2025-05-01-high","rank_in_archive_order":3,"of":18,"metrics":{"EM":"88.24","F1":"91.31"},"uses_additional_data":true}],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2106.06981","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2106.06981"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"deterministic:regex_extraction","url":"https://github.com/tech-srl/RASP","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/google-deepmind/tracr","reach":{"status":"ok","spdx":"Apache-2.0"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/tvergara/tracr-injection","reach":{"status":"ok"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/deepmind/tracr","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/princeton-nlp/transformerprograms","reach":null}],"summary":{"ran_violates":1,"ran":8,"ran_draft_wrong":2,"unverified":7},"by_repo_kind":{"official":{"samples":10,"ran":5,"repositories":1},"listed":{"samples":8,"ran":6,"repositories":2}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":5,"samples":[{"code_sha256_prefix":"527cffd82932410e","entry":"is_comment","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/REPL.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/REPL.py","link_basis":"first_harvest_node","language":"python","status":"ran_violates","verification_level":2,"contract_check":"VIOLATES","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"527cffd82932410e"}},{"code_sha256_prefix":"189b4e46925ee573","entry":"AlreadyPrintedTheException","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"189b4e46925ee573"}},{"code_sha256_prefix":"d10a4856884491c5","entry":"NextId","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"d10a4856884491c5"}},{"code_sha256_prefix":"4088156aa095d937","entry":"_Annotations","repo":"deepmind/tracr","repo_kind":"listed","path":"tracr/rasp/rasp.py","file_url":"https://github.com/deepmind/tracr/blob/HEAD/tracr/rasp/rasp.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"4088156aa095d937"}},{"code_sha256_prefix":"fec1c122bc1ffea7","entry":"aggregate","repo":"princeton-nlp/transformerprograms","repo_kind":"listed","path":"programs/rasp/double_hist/double_hist.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/programs/rasp/double_hist/double_hist.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"fec1c122bc1ffea7"}},{"code_sha256_prefix":"40e0b3f88e966813","entry":"aggregate_sum","repo":"princeton-nlp/transformerprograms","repo_kind":"listed","path":"programs/rasp/double_hist/double_hist.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/programs/rasp/double_hist/double_hist.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"40e0b3f88e966813"}},{"code_sha256_prefix":"dacd53883797b544","entry":"annotate","repo":"deepmind/tracr","repo_kind":"listed","path":"tracr/rasp/rasp.py","file_url":"https://github.com/deepmind/tracr/blob/HEAD/tracr/rasp/rasp.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"dacd53883797b544"}},{"code_sha256_prefix":"643410128e16a94a","entry":"creation_order_id","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"643410128e16a94a"}},{"code_sha256_prefix":"4bc47faa32e9cf7e","entry":"formatstr","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/REPL.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/REPL.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"4bc47faa32e9cf7e"}},{"code_sha256_prefix":"51fc4ee5a5955f4c","entry":"select","repo":"princeton-nlp/transformerprograms","repo_kind":"listed","path":"programs/rasp/double_hist/double_hist.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/programs/rasp/double_hist/double_hist.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"51fc4ee5a5955f4c"}},{"code_sha256_prefix":"09c0dae7b0da54e5","entry":"select_closest","repo":"princeton-nlp/transformerprograms","repo_kind":"listed","path":"programs/rasp/double_hist/double_hist.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/programs/rasp/double_hist/double_hist.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"09c0dae7b0da54e5"}},{"code_sha256_prefix":"c4c570c5bafb4f46","entry":"RASPError","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c4c570c5bafb4f46"}},{"code_sha256_prefix":"0093ebab55f0cb4b","entry":"RASPExpr","repo":"deepmind/tracr","repo_kind":"listed","path":"tracr/rasp/rasp.py","file_url":"https://github.com/deepmind/tracr/blob/HEAD/tracr/rasp/rasp.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"0093ebab55f0cb4b"}},{"code_sha256_prefix":"4520cbdca778c43b","entry":"RASPTypeError","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"4520cbdca778c43b"}},{"code_sha256_prefix":"04bc286a17d1056c","entry":"UnfinishedSelect","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"04bc286a17d1056c"}},{"code_sha256_prefix":"e9e07cd03cf1da14","entry":"UnfinishedSequence","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e9e07cd03cf1da14"}},{"code_sha256_prefix":"95bb5c3597ddb64d","entry":"is_real_unfinished","repo":"tech-srl/RASP","repo_kind":"official","path":"RASP_support/FunctionalSupport.py","file_url":"https://github.com/tech-srl/RASP/blob/HEAD/RASP_support/FunctionalSupport.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"95bb5c3597ddb64d"}},{"code_sha256_prefix":"b99713c212d2c8f6","entry":"run","repo":"princeton-nlp/transformerprograms","repo_kind":"listed","path":"programs/rasp/double_hist/double_hist.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/programs/rasp/double_hist/double_hist.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"b99713c212d2c8f6"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}