Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions kv_cache_manager/optimizer/docs/optimizer_architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ KVCacheManager Optimizer is a standalone cache optimization analysis module. It
The Optimizer currently contains two execution paths: offline trace replay and online optimization service:

- The offline replay path uses `OptimizerManager`, `OptIndexerManager`, `OptEvictionManager`, and `RadixTreeIndex`, depending on replay configuration, trace loader/converter, and data storage types.
- The online service path uses `OnlineOptimizerManager`, LiteHit, `CacheIndexerFactory`, `LruCacheIndexer`/`TtlCacheIndexerWrapper`, and service/protobuf interfaces, depending on online instance group/instance configuration and registry. Full-attention multi-capacity LRU is directly held by `InstanceState` as LiteHit; linear attention is still simulated by the generic `CacheIndexer`.
- The online service path uses `OnlineOptimizerManager`, the unified LiteHit runtime, and service/protobuf interfaces, depending on online instance group/instance configuration and registry. Each `InstanceState` holds one `TtlLiteHit` decorator with a single `LiteHit` core inside; full attention uses the Full-only policy, and linear attention additionally enables the Linear state policy.

The two paths share the hit-rate modeling code within the optimizer module, but the runtime, configuration targets, and service boundaries remain independent, avoiding the online service introducing offline replay/data storage dependencies.

Expand Down Expand Up @@ -78,9 +78,10 @@ OnlineOptimizerManager (online runtime coordinator)
├── OptimizerRegistryManager (instance group/instance persistence)
└── InstanceState (instance_id-isolated runtime state)
├── LiteHit (full-attention multi-capacity LRU)
└── CacheIndexerFactory (linear attention)
└── LruCacheIndexer / TtlCacheIndexerWrapper
└── LiteHit (unified weighted LRU core)
├── Full-only (optional block-RLE)
└── Linear state policy (mixed Full + Linear charges)
both can layer a fixed TTL
```

### Directory Structure
Expand All @@ -97,10 +98,12 @@ kv_cache_manager/optimizer/
│ └── online_runtime/ # online runtime
│ └── online_optimizer_manager.h/cc # online instance registration, TraceQuery, and statistics
├── index/ # index layer
│ ├── radix_tree_index.h/cc # offline Radix tree index
│ └── online/ # linear attention online capacity/TTL indexer
├── liteHit/ # lightweight multi-capacity full-attention LRU hit-rate core
│ ├── lite_hit.h/cc # multi-capacity LRU hit-rate analyzer
│ └── radix_tree_index.h/cc # offline Radix tree index
├── liteHit/ # lightweight multi-capacity hit-rate core
│ ├── lite_hit.h/cc # capacity-independent weighted LRU core shared by Full/Mamba
│ ├── lite_hit_linear.h/cc # Linear state restore points and write policy (no separate LRU state)
│ ├── lite_hit_ttl.h/cc # fixed-TTL decorator wrapping LiteHit (epochs/watermark)
│ ├── weighted_lru_pool.h/cc # byte-weighted LRU pool (typed keys: Full / Linear)
│ └── dynamic_fenwick_tree.h/cc # order-statistics Fenwick for reuse-distance
├── eviction_policy/ # eviction policy layer
│ ├── base.h # policy base class
Expand Down Expand Up @@ -156,7 +159,9 @@ kv_cache_manager/optimizer/

The online service protocol is defined in `kv_cache_manager/protocol/protobuf/optimizer_service.proto`, and is converted by the service layer into optimizer online config/runtime objects.

A full-attention TraceQuery only puts complete blocks into `block_keys`, and passes the original input length including trailing tokens via `input_token_len`. The Online Manager uses the fixed byte charge of the full location spec group to floor `capacity_gb` to block capacity, then hands the same request to LiteHit; both per-request and cumulative hit rates are `prefix_hit_blocks * block_size_tokens / input_tokens`. When old clients lack the length, the compatibility assumption is that there are no trailing tokens. When a full-attention group config has `ttl_seconds != 0`, a fixed TTL is layered on top of LiteHit with wall-clock time (metrics consistent with `TtlCacheIndexerWrapper`); the statistics metrics and TTL behavior of linear attention keep the legacy path unchanged.
A full-attention TraceQuery only puts complete blocks into `block_keys`, and passes the original input length including trailing tokens via `input_token_len`. Like linear attention, the Online Manager keeps only byte capacities; the core's default byte-step can be losslessly compressed into Full-only block RLE, and the fixed byte charge of the full location spec group performs a single floor only at final projection. Both per-request and cumulative hit rates are `prefix_hit_blocks * block_size_tokens / input_tokens`. When old clients lack the length, the compatibility assumption is that there are no trailing tokens.

Linear attention (`linear_step > 0`) enables the Linear state policy on the same LiteHit: a Full block and a Linear state are two independent objects that share **one recency order and one total byte budget**; the capacity axis is total bytes rather than a block count, and the hit semantics is "resume from a Linear state". `LiteHit` itself has no notion of time; the outer `TtlLiteHit` decorator filters both Full and Linear states with a shared epoch/watermark, expiring an object from the weighted LRU's visible set once its age reaches the group TTL. Online uses wall-clock time, offline uses trace timestamps, and with TTL 0 the decorator is a transparent pass-through.

---

Expand Down
23 changes: 14 additions & 9 deletions kv_cache_manager/optimizer/docs/optimizer_architecture_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ KVCacheManager Optimizer 是一个独立的缓存优化分析模块,通过回
Optimizer 当前包含离线 trace 回放和在线优化服务两条运行路径:

- 离线回放路径使用 `OptimizerManager`、`OptIndexerManager`、`OptEvictionManager` 和 `RadixTreeIndex`,依赖 replay 配置、trace loader/converter 和 data storage 类型。
- 在线服务路径使用 `OnlineOptimizerManager`、LiteHit、`CacheIndexerFactory`、`LruCacheIndexer`/`TtlCacheIndexerWrapper` 和 service/protobuf 接口,依赖 online 实例组/实例配置与 registry。full-attention 多容量 LRU 由 `InstanceState` 直接持有 LiteHitlinear attention 仍由通用 `CacheIndexer` 模拟
- 在线服务路径使用 `OnlineOptimizerManager`、统一的 LiteHit 运行时和 service/protobuf 接口,依赖 online 实例组/实例配置与 registry。每个 `InstanceState` 持有一个 `TtlLiteHit` decorator,内部只有一个 `LiteHit` core;full-attention 使用 Full-only 策略,linear attention 额外启用 Linear state 策略

两条路径共享 optimizer 模块内的命中率建模代码,但运行时、配置 target 和服务边界保持独立,避免在线服务引入离线 replay/data storage 依赖。

Expand Down Expand Up @@ -78,9 +78,10 @@ OnlineOptimizerManager (在线运行时协调器)
├── OptimizerRegistryManager (实例组/实例持久化)
└── InstanceState (instance_id 隔离的运行状态)
├── LiteHit (full-attention 多容量 LRU)
└── CacheIndexerFactory (linear attention)
└── LruCacheIndexer / TtlCacheIndexerWrapper
└── LiteHit (统一 weighted LRU core)
├── Full-only(可选 block-RLE)
└── Linear state policy(Full + Linear 混合 charge)
两者均可叠固定 TTL
```

### 目录结构
Expand All @@ -97,10 +98,12 @@ kv_cache_manager/optimizer/
│ └── online_runtime/ # 在线运行时
│ └── online_optimizer_manager.h/cc # 在线实例注册、TraceQuery 和统计
├── index/ # 索引层
│ ├── radix_tree_index.h/cc # 离线 Radix 树索引
│ └── online/ # linear attention 在线容量/TTL 索引器
├── liteHit/ # 轻量多容量 full-attention LRU 命中率核心
│ ├── lite_hit.h/cc # 多容量 LRU 命中率分析器
│ └── radix_tree_index.h/cc # 离线 Radix 树索引
├── liteHit/ # 轻量多容量命中率核心
│ ├── lite_hit.h/cc # Full/Mamba 共享的容量无关 weighted LRU 核心
│ ├── lite_hit_linear.h/cc # Linear state 恢复点与写入策略(无独立 LRU 状态)
│ ├── lite_hit_ttl.h/cc # 包裹 LiteHit 的固定 TTL decorator(epoch/watermark)
│ ├── weighted_lru_pool.h/cc # 字节加权 LRU 池(typed key:Full / Linear)
│ └── dynamic_fenwick_tree.h/cc # reuse-distance 用的 order-statistics Fenwick
├── eviction_policy/ # 驱逐策略层
│ ├── base.h # 策略基类
Expand Down Expand Up @@ -156,7 +159,9 @@ kv_cache_manager/optimizer/

在线服务协议定义在 `kv_cache_manager/protocol/protobuf/optimizer_service.proto`,由 service 层转换为 optimizer online config/runtime 对象。

full-attention TraceQuery 只把完整 block 放入 `block_keys`,并用 `input_token_len` 传入包含尾部 token 的原始输入长度。Online Manager 用 full location spec group 的固定字节 charge 将 `capacity_gb` 向下取整为 block 容量,再把同一请求交给 LiteHit;请求级与累计命中率均为 `prefix_hit_blocks * block_size_tokens / input_tokens`。旧客户端缺少长度时兼容假设没有尾部 token。full-attention 组配置 `ttl_seconds != 0` 时,固定 TTL 以墙钟时间叠加在 LiteHit 之上(口径与 `TtlCacheIndexerWrapper` 一致);linear attention 的统计口径和 TTL 行为保持 legacy 路径不变。
full-attention TraceQuery 只把完整 block 放入 `block_keys`,并用 `input_token_len` 传入包含尾部 token 的原始输入长度。Online Manager 与 linear attention 一样只保存 byte 容量;核心默认 byte-step 可无损压成 Full-only block-RLE,最终投影时再用 full location spec group 的固定字节 charge 做一次 floor。请求级与累计命中率均为 `prefix_hit_blocks * block_size_tokens / input_tokens`。旧客户端缺少长度时兼容假设没有尾部 token。

linear attention(`linear_step > 0`)在同一个 LiteHit 上启用 Linear state 策略:Full block 与 Linear state 是两个独立对象,但共享**同一个 recency 和总字节预算**;容量轴是总字节而非 block 数,命中语义是“从某个 Linear state 恢复”。`LiteHit` 本身没有时间概念;外层 `TtlLiteHit` decorator 用共享 epoch/watermark 同时过滤 Full 与 Linear state,age 达到组 TTL 时从 weighted LRU 可见集合中失效。在线使用墙钟,离线使用 trace 时间戳,TTL 为 0 时 decorator 透明透传。

---

Expand Down
45 changes: 0 additions & 45 deletions kv_cache_manager/optimizer/index/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,3 @@ cc_library(
"//kv_cache_manager/optimizer/analysis",
],
)

cc_library(
name = "cache_indexer",
srcs = [
"online/lru_cache_indexer.cc",
],
hdrs = [
"online/cache_indexer.h",
"online/lru_cache_indexer.h",
],
deps = [
"//kv_cache_manager/common:logger",
"//kv_cache_manager/common/cache",
],
)

cc_library(
name = "ttl_cache_indexer_wrapper",
srcs = [
"online/ttl_cache_indexer_wrapper.cc",
],
hdrs = [
"online/ttl_cache_indexer_wrapper.h",
],
deps = [
":cache_indexer",
"//kv_cache_manager/common:timestamp_util",
],
)

cc_library(
name = "cache_indexer_factory",
srcs = [
"online/cache_indexer_factory.cc",
],
hdrs = [
"online/cache_indexer_factory.h",
],
deps = [
":cache_indexer",
":ttl_cache_indexer_wrapper",
"//kv_cache_manager/common:env_util",
"//kv_cache_manager/common:logger",
],
)
79 changes: 0 additions & 79 deletions kv_cache_manager/optimizer/index/online/cache_indexer.h

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions kv_cache_manager/optimizer/index/online/cache_indexer_factory.h

This file was deleted.

Loading
Loading