[Relax][Frontend][KVCache] Restructure kv_cache kernels#19405
Open
MasterJH5574 wants to merge 1 commit intoapache:mainfrom
Open
[Relax][Frontend][KVCache] Restructure kv_cache kernels#19405MasterJH5574 wants to merge 1 commit intoapache:mainfrom
MasterJH5574 wants to merge 1 commit intoapache:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive set of TIR kernels for LLM attention, covering decode, prefill, and paged KV-cache operations across CPU and GPU targets. It refactors shared logic into a common utility module to improve maintainability. The review feedback identifies a critical out-of-bounds indexing bug in the CPU prefill kernel, recommends dynamic calculation of data type sizes to replace hardcoded values, and points out incorrect TVMScript type hints for local variables.
Pure refactor — does not change any generated TIR / kernel behavior.
Dedupe the tiled prefill kernels in kv_cache.py by extracting the shared
online-softmax pieces as @T.macro helpers (init_states, compute_s_gemm,
softmax_update_{causal,valid_length}, compute_o_gemm, advance_tile_batch,
paged_store_output_lse), plus Python helpers for the common buffer
allocations (softmax state, MHA/MLA Q/K/V/O, tile-walk scalars).
Split the kernel factories out of kv_cache.py into private sibling
modules: _kernel_common.py (shared helpers + macros + schedule),
_page_kernels.py (append/debug/copy/compact), _prefill_kernels.py
(paged/ragged/MLA/dense/masked-sequence), _decode_kernels.py (decode +
state merge). kv_cache.py now holds only the PagedKVCache classes and
re-exports every moved symbol so existing imports keep working.
tree_attn.py also switches to the shared helpers.
kv_cache.py drops from 2815 to 668 lines; the package is ~2.4k lines
smaller overall. No test files modified; GPU tests pass unchanged
(72 passed, 4 pre-existing skips).
98ea654 to
4dd41a1
Compare
tlopex
approved these changes
Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pure refactor — does not change any generated TIR / kernel behavior.
Dedupe the tiled prefill kernels in kv_cache.py by extracting the shared online-softmax pieces as T.macro helpers (init_states, compute_s_gemm, softmax_update_{causal,valid_length}, compute_o_gemm, advance_tile_batch, paged_store_output_lse), plus Python helpers for the common buffer allocations (softmax state, MHA/MLA Q/K/V/O, tile-walk scalars).
Split the kernel factories out of kv_cache.py into private sibling modules: _kernel_common.py (shared helpers + macros + schedule), _page_kernels.py (append/debug/copy/compact), _prefill_kernels.py (paged/ragged/MLA/dense/masked-sequence), _decode_kernels.py (decode + state merge). kv_cache.py now holds only the PagedKVCache classes and re-exports every moved symbol so existing imports keep working. tree_attn.py also switches to the shared helpers.
kv_cache.py drops from 2815 to 668 lines; the package is ~2.4k lines smaller overall. No test files modified; GPU tests pass unchanged (72 passed, 4 pre-existing skips).