{"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/moh-multi-head-attention-as-mixture-of-head","title":"MoH: Multi-Head Attention as Mixture-of-Head Attention","arxiv_id":"2410.11842","date":"2024-10-15","proceeding":null,"authors":["Peng Jin","Bo Zhu","Li Yuan","Shuicheng Yan"],"abstract":"In this work, we upgrade the multi-head attention mechanism, the core of the Transformer model, to improve efficiency while maintaining or surpassing the previous accuracy level. We show that multi-head attention can be expressed in the summation form. Drawing on the insight that not all attention heads hold equal significance, we propose Mixture-of-Head attention (MoH), a new architecture that treats attention heads as experts in the Mixture-of-Experts (MoE) mechanism. MoH has two significant advantages: First, MoH enables each token to select the appropriate attention heads, enhancing inference efficiency without compromising accuracy or increasing the number of parameters. Second, MoH replaces the standard summation in multi-head attention with a weighted summation, introducing flexibility to the attention mechanism and unlocking extra performance potential. Extensive experiments on ViT, DiT, and LLMs demonstrate that MoH outperforms multi-head attention by using only 50%-90% of the attention heads. Moreover, we demonstrate that pre-trained multi-head attention models, such as LLaMA3-8B, can be further continue-tuned into our MoH models. Notably, MoH-LLaMA3-8B achieves an average accuracy of 64.0% across 14 benchmarks, outperforming LLaMA3-8B by 2.4% by utilizing only 75% of the attention heads. We believe the proposed MoH is a promising alternative to multi-head attention and provides a strong foundation for developing advanced and efficient attention-based models.","url_abs":"https://arxiv.org/abs/2410.11842v1","url_pdf":"https://arxiv.org/pdf/2410.11842v1.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":"moh-multi-head-attention-as-mixture-of-head","repo_url":"https://github.com/skyworkai/moh","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"moh-multi-head-attention-as-mixture-of-head","repo_url":"https://github.com/pku-yuangroup/chat-univi","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"Apache-2.0"}},{"paper_slug":"moh-multi-head-attention-as-mixture-of-head","repo_url":"https://github.com/skyworkai/moe-plus-plus","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null}],"tasks":[{"task_slug":"mixture-of-experts","task_name":"Mixture-of-Experts"}],"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":{"syntology_url":"https://syntology.ai/paper/2410.11842","atlas_url":"https://app.syntology.ai/?focus=2410.11842","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2410.11842"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-25T09:33:49+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/pku-yuangroup/chat-univi","reach":{"status":"ok","spdx":"Apache-2.0"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/skyworkai/moh","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/skyworkai/moe-plus-plus","reach":null}],"summary":{"ran_fixture":3,"ran":6,"ran_draft_wrong":5,"unverified":4},"by_repo_kind":{"official":{"samples":4,"ran":3,"repositories":1},"listed":{"samples":11,"ran":8,"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":3,"samples":[{"code_sha256_prefix":"3f37a54ade181a61","entry":"normal_kl","repo":"SkyworkAI/MoH","repo_kind":"official","path":"MoH-DiT/diffusion/diffusion_utils.py","file_url":"https://github.com/SkyworkAI/MoH/blob/HEAD/MoH-DiT/diffusion/diffusion_utils.py","link_basis":"first_harvest_node","language":"python","status":"ran_fixture","verification_level":2,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"3f37a54ade181a61"}},{"code_sha256_prefix":"30d7eec482ebf6b1","entry":"repeat_kv","repo":null,"repo_kind":null,"path":null,"file_url":null,"link_basis":"identical_code_first_harvested_elsewhere","language":"python","status":"ran_fixture","verification_level":2,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":null,"inline_ok":false,"mcp_get_code":{"code_sha256":"30d7eec482ebf6b1"}},{"code_sha256_prefix":"c39d2f7f60570686","entry":"DynamicNTKScalingRotaryEmbedding","repo":"skyworkai/moe-plus-plus","repo_kind":"listed","path":"MoE++/modeling_moe_plus_plus.py","file_url":"https://github.com/skyworkai/moe-plus-plus/blob/HEAD/MoE%2B%2B/modeling_moe_plus_plus.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":"c39d2f7f60570686"}},{"code_sha256_prefix":"eebf71aed18a0f87","entry":"LinearScalingRotaryEmbedding","repo":"skyworkai/moe-plus-plus","repo_kind":"listed","path":"MoE++/modeling_moe_plus_plus.py","file_url":"https://github.com/skyworkai/moe-plus-plus/blob/HEAD/MoE%2B%2B/modeling_moe_plus_plus.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":"eebf71aed18a0f87"}},{"code_sha256_prefix":"420932dc6224ccb9","entry":"MoeConfig","repo":"skyworkai/moe-plus-plus","repo_kind":"listed","path":"MoE++/modeling_moe_plus_plus.py","file_url":"https://github.com/skyworkai/moe-plus-plus/blob/HEAD/MoE%2B%2B/modeling_moe_plus_plus.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":"420932dc6224ccb9"}},{"code_sha256_prefix":"9a1f7fea1bda87f8","entry":"NTKScalingRotaryEmbedding","repo":"skyworkai/moe-plus-plus","repo_kind":"listed","path":"MoE++/modeling_moe_plus_plus.py","file_url":"https://github.com/skyworkai/moe-plus-plus/blob/HEAD/MoE%2B%2B/modeling_moe_plus_plus.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":"9a1f7fea1bda87f8"}},{"code_sha256_prefix":"4390a52a532857aa","entry":"RotaryEmbedding","repo":"skyworkai/moe-plus-plus","repo_kind":"listed","path":"MoE++/modeling_moe_plus_plus.py","file_url":"https://github.com/skyworkai/moe-plus-plus/blob/HEAD/MoE%2B%2B/modeling_moe_plus_plus.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":"4390a52a532857aa"}},{"code_sha256_prefix":"d61c483a3c2b3156","entry":"apply_rotary_pos_emb","repo":null,"repo_kind":null,"path":null,"file_url":null,"link_basis":"identical_code_first_harvested_elsewhere","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":null,"inline_ok":false,"mcp_get_code":{"code_sha256":"d61c483a3c2b3156"}},{"code_sha256_prefix":"d6a68e210556f857","entry":"approx_standard_normal_cdf","repo":"SkyworkAI/MoH","repo_kind":"official","path":"MoH-DiT/diffusion/diffusion_utils.py","file_url":"https://github.com/SkyworkAI/MoH/blob/HEAD/MoH-DiT/diffusion/diffusion_utils.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":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"d6a68e210556f857"}},{"code_sha256_prefix":"57848f6ed380e2e5","entry":"continuous_gaussian_log_likelihood","repo":"SkyworkAI/MoH","repo_kind":"official","path":"MoH-DiT/diffusion/diffusion_utils.py","file_url":"https://github.com/SkyworkAI/MoH/blob/HEAD/MoH-DiT/diffusion/diffusion_utils.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"57848f6ed380e2e5"}},{"code_sha256_prefix":"42a46570620cd9fa","entry":"get_chunk","repo":"pku-yuangroup/chat-univi","repo_kind":"listed","path":"ChatUniVi/eval/model_coco_vqa.py","file_url":"https://github.com/pku-yuangroup/chat-univi/blob/HEAD/ChatUniVi/eval/model_coco_vqa.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"well_formed","behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"42a46570620cd9fa"}},{"code_sha256_prefix":"c578a6846674ff45","entry":"read_json","repo":"pku-yuangroup/chat-univi","repo_kind":"listed","path":"ChatUniVi/eval/model_video_qa.py","file_url":"https://github.com/pku-yuangroup/chat-univi/blob/HEAD/ChatUniVi/eval/model_video_qa.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":"c578a6846674ff45"}},{"code_sha256_prefix":"b99eea6376d1e212","entry":"rotate_half","repo":null,"repo_kind":null,"path":null,"file_url":null,"link_basis":"identical_code_first_harvested_elsewhere","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":null,"inline_ok":false,"mcp_get_code":{"code_sha256":"b99eea6376d1e212"}},{"code_sha256_prefix":"076c252c52cbb161","entry":"split_list","repo":"pku-yuangroup/chat-univi","repo_kind":"listed","path":"ChatUniVi/eval/model_coco_vqa.py","file_url":"https://github.com/pku-yuangroup/chat-univi/blob/HEAD/ChatUniVi/eval/model_coco_vqa.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"076c252c52cbb161"}},{"code_sha256_prefix":"513d27b0995b5f3e","entry":"Attention","repo":"skyworkai/moe-plus-plus","repo_kind":"listed","path":"MoE++/modeling_moe_plus_plus.py","file_url":"https://github.com/skyworkai/moe-plus-plus/blob/HEAD/MoE%2B%2B/modeling_moe_plus_plus.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":"513d27b0995b5f3e"}},{"code_sha256_prefix":"7b9f5a8e366cc24e","entry":"Attention","repo":"skyworkai/moh","repo_kind":"official","path":"MoH-ViT/attention.py","file_url":"https://github.com/skyworkai/moh/blob/HEAD/MoH-ViT/attention.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":"7b9f5a8e366cc24e"}},{"code_sha256_prefix":"e2e5c3a95a6aebb4","entry":"save_image_to_local","repo":"pku-yuangroup/chat-univi","repo_kind":"listed","path":"main_demo_13B.py","file_url":"https://github.com/pku-yuangroup/chat-univi/blob/HEAD/main_demo_13B.py","link_basis":"plan_row","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":"e2e5c3a95a6aebb4"}},{"code_sha256_prefix":"cab3546afd60006d","entry":"save_video_to_local","repo":"pku-yuangroup/chat-univi","repo_kind":"listed","path":"main_demo_13B.py","file_url":"https://github.com/pku-yuangroup/chat-univi/blob/HEAD/main_demo_13B.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":"cab3546afd60006d"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}