{"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/improving-transformers-with-dynamically","title":"Improving Transformers with Dynamically Composable Multi-Head Attention","arxiv_id":"2405.08553","date":"2024-05-14","proceeding":null,"authors":["Da Xiao","Qingye Meng","Shengping Li","Xingyuan Yuan"],"abstract":"Multi-Head Attention (MHA) is a key component of Transformer. In MHA, attention heads work independently, causing problems such as low-rank bottleneck of attention score matrices and head redundancy. We propose Dynamically Composable Multi-Head Attention (DCMHA), a parameter and computation efficient attention architecture that tackles the shortcomings of MHA and increases the expressive power of the model by dynamically composing attention heads. At the core of DCMHA is a $\\it{Compose}$ function that transforms the attention score and weight matrices in an input-dependent way. DCMHA can be used as a drop-in replacement of MHA in any transformer architecture to obtain the corresponding DCFormer. DCFormer significantly outperforms Transformer on different architectures and model scales in language modeling, matching the performance of models with ~1.7x-2.0x compute. For example, DCPythia-6.9B outperforms open source Pythia-12B on both pretraining perplexity and downstream task evaluation. The code and models are available at https://github.com/Caiyun-AI/DCFormer.","url_abs":"https://arxiv.org/abs/2405.08553v2","url_pdf":"https://arxiv.org/pdf/2405.08553v2.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":"improving-transformers-with-dynamically","repo_url":"https://github.com/caiyun-ai/dcformer","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"jax","reach":{"status":"ok","spdx":"MIT"}},{"paper_slug":"improving-transformers-with-dynamically","repo_url":"https://github.com/caiyun-ai/gar","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null}],"tasks":[{"task_slug":"language-modeling","task_name":"Language Modeling"},{"task_slug":"language-modelling","task_name":"Language Modelling"}],"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=2405.08553","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2405.08553"}},"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/Caiyun-AI/DCFormer","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/caiyun-ai/gar","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/caiyun-ai/dcformer","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran_fixture":2,"ran":5,"ran_honours":1,"ran_draft_wrong":1,"unverified":3},"by_repo_kind":{"official":{"samples":12,"ran":9,"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":0,"samples":[{"code_sha256_prefix":"8a38d7c525fad8f1","entry":"accuracy","repo":"Caiyun-AI/DCFormer","repo_kind":"official","path":"pytorch/dcformer/run_clm_pt_dcformer.py","file_url":"https://github.com/Caiyun-AI/DCFormer/blob/HEAD/pytorch/dcformer/run_clm_pt_dcformer.py","link_basis":"first_harvest_node","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"8a38d7c525fad8f1"}},{"code_sha256_prefix":"6b376a0abe58a427","entry":"compute_metrics","repo":"Caiyun-AI/DCFormer","repo_kind":"official","path":"pytorch/dcformer/run_clm_pt_dcformer.py","file_url":"https://github.com/Caiyun-AI/DCFormer/blob/HEAD/pytorch/dcformer/run_clm_pt_dcformer.py","link_basis":"first_harvest_node","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"6b376a0abe58a427"}},{"code_sha256_prefix":"d5a3e6a05d912f0a","entry":"extract_dtype_callable","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcformer/modeling_dcformer.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcformer/modeling_dcformer.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"d5a3e6a05d912f0a"}},{"code_sha256_prefix":"aba86ffbed27ba65","entry":"find_multiple","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcformer/modeling_dcformer.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcformer/modeling_dcformer.py","link_basis":"harvester_set","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"well_formed","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"aba86ffbed27ba65"}},{"code_sha256_prefix":"f313d5feb241ec85","entry":"load_model","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcformer/maxtext2torch.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcformer/maxtext2torch.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":"f313d5feb241ec85"}},{"code_sha256_prefix":"dccbfeb9dc59e07a","entry":"make_window_mask","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcformer/modeling_dcformer.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcformer/modeling_dcformer.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":"dccbfeb9dc59e07a"}},{"code_sha256_prefix":"d478a1bd852b034a","entry":"rename_columns","repo":"Caiyun-AI/DCFormer","repo_kind":"official","path":"pytorch/dcformer/run_clm_pt_dcformer.py","file_url":"https://github.com/Caiyun-AI/DCFormer/blob/HEAD/pytorch/dcformer/run_clm_pt_dcformer.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"d478a1bd852b034a"}},{"code_sha256_prefix":"6cd8ea4949f52b54","entry":"rename_columns","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcformer/run_clm_sft_dcformer.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcformer/run_clm_sft_dcformer.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":"6cd8ea4949f52b54"}},{"code_sha256_prefix":"590e68066c00cb10","entry":"slice_dw","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcpythia/modeling_dcpythia.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcpythia/modeling_dcpythia.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":"590e68066c00cb10"}},{"code_sha256_prefix":"bb033a34d8b49b9d","entry":"build_instruction_dataset","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcformer/build_dataset.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcformer/build_dataset.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":"bb033a34d8b49b9d"}},{"code_sha256_prefix":"87c57735e68651cc","entry":"get_system_characteristics","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"jax/MaxText/accelerator_to_spec_map.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/jax/MaxText/accelerator_to_spec_map.py","link_basis":"harvester_set","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":"87c57735e68651cc"}},{"code_sha256_prefix":"19e80053090641bd","entry":"update_weight_from_maxtext","repo":"caiyun-ai/dcformer","repo_kind":"official","path":"pytorch/dcformer/maxtext2torch.py","file_url":"https://github.com/caiyun-ai/dcformer/blob/HEAD/pytorch/dcformer/maxtext2torch.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":"19e80053090641bd"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}