给大模型上下文窗口加虚拟内存,大幅减少无用内容占用
The Missing Memory Hierarchy: Demand Paging for LLM Context Windows
- 将上下文当作高速缓存,通过按需调页自动淘汰过期内容
- 实测节省93%上下文空间,故障率仅0.0254%
- 适合长期对话、高并发推理系统,解决上下文溢出问题
大模型的上下文窗口并非真正内存,而是类似L1缓存的小容量、高速、高成本资源,当前系统缺乏L2、虚拟内存和分页机制。所有工具定义、系统提示和陈旧结果都永久占用上下文。分析857个生产会话及445万有效输入令牌后发现,21.8%为结构性浪费。本文提出Pichay,一个透明代理型需求分页系统,在客户端与推理API间拦截消息流,主动淘汰过时内容,检测模型重请求时的缺页,并根据缺页历史锁定活跃工作集页面。离线回放140万次模拟淘汰,缺页率仅0.0254%;线上部署681轮对话中,上下文消耗从5038KB降至339KB,最高节省93%。在持续高压下系统仍可用,但出现预期的抖动现象。核心观点:上下文限制、注意力衰减、成本上升、会话状态丢失等问题本质是虚拟内存问题的伪装。解决方案早已存在:工作集理论(Denning, 1968)、需求分页、缺页驱动替换策略及多级可管理的存储层次。本文构建了大模型系统完整的内存层级架构(从L1到持久存储),并已上线部署前三个层级(L1淘汰、L2缺页驱动锁定、L3模型触发会话压缩),指出跨会话内存管理仍是未解前沿。
原文摘要 · Abstract (English)
The context window of a large language model is not memory. It is L1 cache: a small, fast, expensive resource that the field treats as the entire memory system. There is no L2, no virtual memory, no paging. Every tool definition, every system prompt, and every stale tool result occupies context for the lifetime of the session. The result is measurable: across 857 production sessions and 4.45 million effective input tokens, 21.8% is structural waste. We present Pichay, a demand paging system for LLM context windows. Implemented as a transparent proxy between client and inference API, Pichay interposes on the message stream to evict stale content, detect page faults when the model re-requests evicted material, and pin working-set pages identified by fault history. In offline replay across 1.4 million simulated evictions, the fault rate is 0.0254%. In live production deployment over 681turns, the system reduces context consumption by up to 93% (5,038KB to 339KB); under extreme sustained pressure, the system remains operational but exhibits the expected thrashing pathology, with repeated fault-in of evicted content. The key observation is that the problems the field faces, such as context limits, attention degradation, cost scaling, lost state across sessions, are virtual memory problems wearing different clothes. The solutions exist: working set theory (Denning, 1968), demand paging, fault-driven replacement policies, and memory hierarchies with multiple eviction-managed levels. We describe the architecture of a full memory hierarchy for LLM systems (L1 through persistent storage), report on the first three levels deployed in production use (L1 eviction, L2 fault-driven pinning, L3 model-initiated conversation compaction), and identify cross-session memory as the remaining frontier.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。