{"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/learning-transformer-programs-1","title":"Learning Transformer Programs","arxiv_id":"2306.01128","date":"2023-06-01","proceeding":"NeurIPS 2023 11","authors":["Dan Friedman","Alexander Wettig","Danqi Chen"],"abstract":"Recent research in mechanistic interpretability has attempted to reverse-engineer Transformer models by carefully inspecting network weights and activations. However, these approaches require considerable manual effort and still fall short of providing complete, faithful descriptions of the underlying algorithms. In this work, we introduce a procedure for training Transformers that are mechanistically interpretable by design. We build on RASP [Weiss et al., 2021], a programming language that can be compiled into Transformer weights. Instead of compiling human-written programs into Transformers, we design a modified Transformer that can be trained using gradient-based optimization and then automatically converted into a discrete, human-readable program. We refer to these models as Transformer Programs. To validate our approach, we learn Transformer Programs for a variety of problems, including an in-context learning task, a suite of algorithmic problems (e.g. sorting, recognizing Dyck languages), and NLP tasks including named entity recognition and text classification. The Transformer Programs can automatically find reasonable solutions, performing on par with standard Transformers of comparable size; and, more importantly, they are easy to interpret. To demonstrate these advantages, we convert Transformers into Python programs and use off-the-shelf code analysis tools to debug model errors and identify the \"circuits\" used to solve different sub-problems. We hope that Transformer Programs open a new path toward the goal of intrinsically interpretable machine learning.","url_abs":"https://arxiv.org/abs/2306.01128v2","url_pdf":"https://arxiv.org/pdf/2306.01128v2.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":"learning-transformer-programs-1","repo_url":"https://github.com/princeton-nlp/transformerprograms","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":null}],"tasks":[{"task_slug":"in-context-learning","task_name":"In-Context Learning"},{"task_slug":"interpretable-machine-learning","task_name":"Interpretable Machine Learning"},{"task_slug":"named-entity-recognition-1","task_name":"Named Entity Recognition"},{"task_slug":"text-classification","task_name":"Text Classification"},{"task_slug":"named-entity-recognition","task_name":"named-entity-recognition"},{"task_slug":"text-classification-1","task_name":"text-classification"}],"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":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2306.01128","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2306.01128"}},"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":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/princeton-nlp/transformerprograms","reach":null}],"summary":{"ran":13,"ran_draft_wrong":2,"ran_fixture":1,"unverified":5},"by_repo_kind":{"official":{"samples":21,"ran":16,"repositories":1}},"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":21,"samples":[{"code_sha256_prefix":"d12fd83022101c89","entry":"CatEmbed","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"d12fd83022101c89"}},{"code_sha256_prefix":"70bbbfdf15549456","entry":"CatMLP","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"70bbbfdf15549456"}},{"code_sha256_prefix":"758c7718eab6c7c7","entry":"ClippedRelPosBias","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"758c7718eab6c7c7"}},{"code_sha256_prefix":"15949249daac5067","entry":"ConstrainedRead","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"15949249daac5067"}},{"code_sha256_prefix":"5c3fbd7d9586f37e","entry":"FixedRelPosBias","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"5c3fbd7d9586f37e"}},{"code_sha256_prefix":"c46a4f9300b88dc6","entry":"NoRelPosBias","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"c46a4f9300b88dc6"}},{"code_sha256_prefix":"f02809a6319619b0","entry":"NumAttention","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"f02809a6319619b0"}},{"code_sha256_prefix":"5b3e2fc48121bc32","entry":"NumEmbed","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"5b3e2fc48121bc32"}},{"code_sha256_prefix":"96c1455fd1928061","entry":"OneHotPosEmbed","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"96c1455fd1928061"}},{"code_sha256_prefix":"3af625298c24b59b","entry":"ScoreAttention","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"3af625298c24b59b"}},{"code_sha256_prefix":"f6f581a88d741f08","entry":"Unembed","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"f6f581a88d741f08"}},{"code_sha256_prefix":"2792e151572ee1ec","entry":"WPred","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"2792e151572ee1ec"}},{"code_sha256_prefix":"db92ccece8dc55e0","entry":"WScore","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.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":"db92ccece8dc55e0"}},{"code_sha256_prefix":"e3599c195e20c0fc","entry":"softmax","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"e3599c195e20c0fc"}},{"code_sha256_prefix":"e35b60a92f2a52b5","entry":"softmax_no_temp","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"e35b60a92f2a52b5"}},{"code_sha256_prefix":"1316961252e8ad54","entry":"subsoftmax","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.py","link_basis":"first_harvest_node","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"1316961252e8ad54"}},{"code_sha256_prefix":"becdbd159c1bead4","entry":"CatAttention","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.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":"becdbd159c1bead4"}},{"code_sha256_prefix":"82fcb68539de38cb","entry":"CatMLPs","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.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":"82fcb68539de38cb"}},{"code_sha256_prefix":"ee5e8b570159bbdd","entry":"HookPoint","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.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":"ee5e8b570159bbdd"}},{"code_sha256_prefix":"4a864ddf34dc9452","entry":"TransformerProgramBlock","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.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":"4a864ddf34dc9452"}},{"code_sha256_prefix":"d39196027884c33b","entry":"TransformerProgramModel","repo":"princeton-nlp/transformerprograms","repo_kind":"official","path":"src/models/programs.py","file_url":"https://github.com/princeton-nlp/transformerprograms/blob/HEAD/src/models/programs.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":"d39196027884c33b"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}