huangpustar/metainfer/kv-cache
Claude Code and agent skill by huangpustar/metainfer
- community
Checking that the SKILL.md file is still in the repository.
Designs prefix-aware KV cache and radix-tree caching for LLM inference engines. Use when implementing or refactoring block tables, paged KV, radix prefix cache, cache hit/miss, eviction, or when the user mentions nano-vllm BlockManager, nano-sglang/mini-sglang RadixCache, or Agent-generated inference frameworks.
Install
npx vskill@latest install huangpustar/metainfer/kv-cacheSKILL.md
KV Cache(逻辑层)
与 Memory Pool 的分工
- KV Cache 组件:管「哪些 token 的 KV 已存在、能否复用、如何索引」——块表、前缀匹配、Radix 树、引用计数、驱逐策略、与调度器的交互。
- Memory Pool 组件:管「GPU 上 KV 张量缓冲区从哪来、槽位/页如何分配」——见姊妹技能
memory-pool。
二者通过 物理槽位索引(block id、token slot、indices 张量)衔接:KV Cache 命中返回已有下标;未命中向 Memory Pool 申请新槽位并写回树/块表。
源码对照(ref_projects)
| 项目 | 主文件 | 职责摘要 | |
|---|---|---|---|
| nano-vllm | nanovllm/engine/block_manager.py | 块级分配 + xxhash 块内容哈希前缀复用;hash_to_block_id;ref_count | |
| nano-sglang | python/sglang/srt/managers/router/radix_cache.py | Radix 树:match_prefix / insert / evict;inc_ref_counter / dec_ref_counter;evictable_size_ | |
| mini-sglang | python/minisgl/kvcache/radix_cache.py | RadixPrefixCache:match_prefix、insert_prefix(按 page_size 对齐)、lock_handle、LRU 叶节点驱逐 | |
| mini-sglang(无前缀) | python/minisgl/kvcache/naive_cache.py | NaivePrefixCache:永不命中、无驱逐;占位实现 |
nano-vllm:BlockManager 要点
- `Block`:
block_id、ref_count、hash、token_ids;reset()将ref_count置 1。 - `allocate(seq)`:按序列块迭代;满块用
compute_hash(token_ids, prefix_hash)查hash_to_block_id;命中则ref_count += 1且可更新num_cached_tokens;未命中从free_block_ids取块并update哈希表。 - `deallocate(seq)`:逆序遍历
block_table,ref_count--,为 0 时归还空闲块。 - `may_append(seq)`:序列长度跨块边界时挂新块;块填满时用前缀块 hash 计算当前块 hash 并登记。
与 ModelRunner 协同:prepare_prefill / prepare_decode 用 block_table 生成 slot_mapping 与 block_tables(见 model_runner.py),供 attention 写入/读取分页 KV。
nano-sglang:RadixCache 要点
- `TreeNode`:
children(边键为 token 序列)、value(该边对应 KV 槽位索引列表,常为 tensor 片段)、ref_counter、last_access_time。 - `match_prefix(key)`:沿树匹配;部分匹配时
_split_node分裂边。 - `insert(key, value)`:插入剩余后缀为新叶或延伸路径。
- `evict(num_tokens, evict_callback)`:叶节点最小堆(按
last_access_time);ref_counter > 0跳过;evict_callback负责把node.value对应槽位还给 TokenToKVPool。 - `inc_ref_counter` / `dec_ref_counter`:沿父链更新;
ref_counter0↔1 时维护evictable_size_。
mini-sglang:RadixPrefixCache 要点
- 边上存 `torch.Tensor` key/value(value 为物理 KV 池下标);
get_match_len可用fast_compare_key。 - `insert_prefix`:
insert_len = align_down(len, page_size),与 MHAKVCache 页对齐。 - `lock_handle`:请求持有前缀时沿路径
ref_count++/--,维护evictable_size/protected_size。 - `evict`:叶节点 LRU(
timestamp),ref_count==0才可驱逐;删除叶后若父成叶且可驱逐则重新入堆。
Agent 生成推理框架时的检查清单
- 是否分页:块表(nano-vllm)还是 token 槽位 + radix(sglang 系)?
- 前缀语义:仅完整块哈希(nano-vllm)还是任意前缀(Radix)?
- 引用生命周期:请求开始/结束/抢占时何时
inc/dec?驱逐前是否保证ref_count==0? - 与 runner 的接口:输出是否为
block_tables+slot_mapping或req_to_token行? - MLA / GQA:逻辑层不变,但 每 token KV 字节数 影响池容量(由 memory-pool 技能估算)。
延伸阅读
Cursor 技能副本:meta-infer/.cursor/skills/kv-cache/SKILL.md(便于 Agent 发现)
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingmetainfer/tasks/gen_infer_framework/notebooks/01_framework_design/kv_cache/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/huangpustar/metainfer/kv-cache)