{"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/exploiting-transformer-activation-sparsity","title":"Exploiting Activation Sparsity with Dense to Dynamic-k Mixture-of-Experts Conversion","arxiv_id":"2310.04361","date":"2023-10-06","proceeding":null,"authors":["Filip Szatkowski","Bartosz Wójcik","Mikołaj Piórczyński","Simone Scardapane"],"abstract":"Transformer models can face practical limitations due to their high computational requirements. At the same time, such models exhibit significant activation sparsity, which can be leveraged to reduce the inference cost by converting parts of the network into equivalent Mixture-of-Experts (MoE) layers. Despite the crucial role played by activation sparsity, its impact on this process remains unexplored. We demonstrate that the efficiency of the conversion can be significantly enhanced by a proper regularization of the activation sparsity of the base model. Moreover, motivated by the high variance of the number of activated neurons for different inputs, we introduce a more effective dynamic-$k$ expert selection rule that adjusts the number of executed experts on a per-token basis. To achieve further savings, we extend this approach to multi-head attention projections. Finally, we develop an efficient implementation that translates these computational savings into actual wall-clock speedup. The proposed method, Dense to Dynamic-$k$ Mixture-of-Experts (D2DMoE), outperforms existing approaches on common NLP and vision tasks, reducing inference cost by up to 60% without significantly impacting performance.","url_abs":"https://arxiv.org/abs/2310.04361v4","url_pdf":"https://arxiv.org/pdf/2310.04361v4.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":"exploiting-transformer-activation-sparsity","repo_url":"https://github.com/bartwojcik/d2dmoe","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}},{"paper_slug":"exploiting-transformer-activation-sparsity","repo_url":"https://github.com/bartwojcik/sadmoe","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}}],"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":"base","method_name":"BASE"},{"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=2310.04361","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2310.04361"}},"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/bartwojcik/sadmoe","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/bartwojcik/d2dmoe","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran":13,"ran_honours":1,"ran_fixture":1,"unverified":2},"by_repo_kind":{"official":{"samples":17,"ran":15,"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":0,"samples":[{"code_sha256_prefix":"24b15be4477fe752","entry":"ResNet18","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"architectures/resnets.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/architectures/resnets.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":"24b15be4477fe752"}},{"code_sha256_prefix":"724da9ddbe842c71","entry":"ResNet34","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"architectures/resnets.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/architectures/resnets.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":"724da9ddbe842c71"}},{"code_sha256_prefix":"fdc0140887df3eb4","entry":"ResNet50","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"architectures/resnets.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/architectures/resnets.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":"fdc0140887df3eb4"}},{"code_sha256_prefix":"4937750817132da0","entry":"calculate_budget_probs","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"methods/l2w.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/methods/l2w.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":"4937750817132da0"}},{"code_sha256_prefix":"749fa6172f0861c6","entry":"distill_last","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"methods/early_exit.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/methods/early_exit.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":"749fa6172f0861c6"}},{"code_sha256_prefix":"354a6510f7664ed4","entry":"distill_later","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"methods/early_exit.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/methods/early_exit.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":"354a6510f7664ed4"}},{"code_sha256_prefix":"81302f1b9ee58713","entry":"distill_next","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"methods/early_exit.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/methods/early_exit.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":"81302f1b9ee58713"}},{"code_sha256_prefix":"60ea649e8d15f74e","entry":"function_to_module","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"common.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/common.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":"60ea649e8d15f74e"}},{"code_sha256_prefix":"99d9eef611aa5bd9","entry":"get_distribution_target","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"methods/avit.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/methods/avit.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":"99d9eef611aa5bd9"}},{"code_sha256_prefix":"bbfd90d32d03bc6a","entry":"init_","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"architectures/moe/moe_layers.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/architectures/moe/moe_layers.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":"bbfd90d32d03bc6a"}},{"code_sha256_prefix":"c6e83b9cea10865d","entry":"mixup_target","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"utils.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/utils.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":"c6e83b9cea10865d"}},{"code_sha256_prefix":"6caffa789f1bdabe","entry":"one_hot","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"utils.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/utils.py","link_basis":"harvester_set","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":"6caffa789f1bdabe"}},{"code_sha256_prefix":"3913c620f6926f42","entry":"rand_bbox","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"utils.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/utils.py","link_basis":"harvester_set","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":"3913c620f6926f42"}},{"code_sha256_prefix":"6c7d046d7dd60b22","entry":"round_filters","repo":"bartwojcik/sadmoe","repo_kind":"official","path":"architectures/efficientnet.py","file_url":"https://github.com/bartwojcik/sadmoe/blob/HEAD/architectures/efficientnet.py","link_basis":"plan_row","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"6c7d046d7dd60b22"}},{"code_sha256_prefix":"97d77e37b5677cd8","entry":"torch_conv_out_spatial_shape","repo":"bartwojcik/sadmoe","repo_kind":"official","path":"architectures/efficientnet.py","file_url":"https://github.com/bartwojcik/sadmoe/blob/HEAD/architectures/efficientnet.py","link_basis":"plan_row","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"97d77e37b5677cd8"}},{"code_sha256_prefix":"64451ac21be6935a","entry":"ffn_filter_condition","repo":"bartwojcik/d2dmoe","repo_kind":"official","path":"architectures/gpt.py","file_url":"https://github.com/bartwojcik/d2dmoe/blob/HEAD/architectures/gpt.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":"64451ac21be6935a"}},{"code_sha256_prefix":"d9d9a4a8f00a818e","entry":"get_activation","repo":"bartwojcik/sadmoe","repo_kind":"official","path":"architectures/efficientnet.py","file_url":"https://github.com/bartwojcik/sadmoe/blob/HEAD/architectures/efficientnet.py","link_basis":"plan_row","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":"d9d9a4a8f00a818e"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}