{"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/chunkattention-efficient-self-attention-with","title":"ChunkAttention: Efficient Self-Attention with Prefix-Aware KV Cache and Two-Phase Partition","arxiv_id":"2402.15220","date":"2024-02-23","proceeding":null,"authors":["Lu Ye","Ze Tao","Yong Huang","Yang Li"],"abstract":"Self-attention is an essential component of large language models (LLM) but a significant source of inference latency for long sequences. In multi-tenant LLM serving scenarios, the compute and memory operation cost of self-attention can be optimized by using the probability that multiple LLM requests have shared system prompts in prefixes. In this paper, we introduce ChunkAttention, a prefix-aware self-attention module that can detect matching prompt prefixes across multiple requests and share their key/value tensors in memory at runtime to improve the memory utilization of KV cache. This is achieved by breaking monolithic key/value tensors into smaller chunks and structuring them into the auxiliary prefix tree. Consequently, on top of the prefix-tree based KV cache, we design an efficient self-attention kernel, where a two-phase partition algorithm is implemented to improve the data locality during self-attention computation in the presence of shared system prompts. Experiments show that ChunkAttention can speed up the self-attention kernel by 3.2-4.8$\\times$ compared to the state-of-the-art implementation, with the length of the system prompt ranging from 1024 to 4096.","url_abs":"https://arxiv.org/abs/2402.15220v4","url_pdf":"https://arxiv.org/pdf/2402.15220v4.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":"chunkattention-efficient-self-attention-with","repo_url":"https://github.com/microsoft/chunk-attention","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":null}],"tasks":[],"methods":[{"method_slug":"speed","method_name":"SPEED"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":"https://syntology.ai/paper/2402.15220","atlas_url":"https://app.syntology.ai/?focus=2402.15220","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2402.15220"}},"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/microsoft/chunk-attention","reach":null}],"summary":{"ran":3,"unverified":3},"by_repo_kind":{"official":{"samples":6,"ran":3,"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":"72f120426b530e6b","entry":"LlamaConfig","repo":"microsoft/chunk-attention","repo_kind":"official","path":"src/chunk_attn/models/llama_hf/modeling_llama.py","file_url":"https://github.com/microsoft/chunk-attention/blob/HEAD/src/chunk_attn/models/llama_hf/modeling_llama.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":"72f120426b530e6b"}},{"code_sha256_prefix":"de4dc6b3c9f22350","entry":"RotaryEmbedding","repo":"microsoft/chunk-attention","repo_kind":"official","path":"src/chunk_attn/models/llama_hf/modeling_llama.py","file_url":"https://github.com/microsoft/chunk-attention/blob/HEAD/src/chunk_attn/models/llama_hf/modeling_llama.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":"de4dc6b3c9f22350"}},{"code_sha256_prefix":"d79ade1199d36527","entry":"Sequence","repo":"microsoft/chunk-attention","repo_kind":"official","path":"src/chunk_attn/models/llama_hf/modeling_llama.py","file_url":"https://github.com/microsoft/chunk-attention/blob/HEAD/src/chunk_attn/models/llama_hf/modeling_llama.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":"d79ade1199d36527"}},{"code_sha256_prefix":"77f8e37c1cca6cea","entry":"LlamaChunkAttention","repo":"microsoft/chunk-attention","repo_kind":"official","path":"src/chunk_attn/models/llama_hf/modeling_llama.py","file_url":"https://github.com/microsoft/chunk-attention/blob/HEAD/src/chunk_attn/models/llama_hf/modeling_llama.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":"77f8e37c1cca6cea"}},{"code_sha256_prefix":"5d23e6459e17dbae","entry":"range_pop","repo":"microsoft/chunk-attention","repo_kind":"official","path":"src/chunk_attn/models/llama_hf/modeling_llama.py","file_url":"https://github.com/microsoft/chunk-attention/blob/HEAD/src/chunk_attn/models/llama_hf/modeling_llama.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":"5d23e6459e17dbae"}},{"code_sha256_prefix":"20b3f6b6c1ae1a3c","entry":"range_push","repo":"microsoft/chunk-attention","repo_kind":"official","path":"src/chunk_attn/models/llama_hf/modeling_llama.py","file_url":"https://github.com/microsoft/chunk-attention/blob/HEAD/src/chunk_attn/models/llama_hf/modeling_llama.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":"20b3f6b6c1ae1a3c"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}