Skip to content

Step 36: Paged KV-cache — PagedAttention with block pool, block tables, CLI flags #96

Closed

Description

@kkokosa

Summary

Implement PagedAttention: block-based KV-cache allocation replacing static pre-allocation. Adds --no-ui flag for API-only serving and --paged/--no-paged CLI flags.

Motivation

SimpleKvCache pre-allocates maxSeqLen × layers per sequence — wastes ~60% memory for variable-length workloads. PagedAttention allocates fixed-size blocks on demand, enabling efficient multi-sequence serving with <4% waste.

Acceptance Criteria

  • --no-ui flag on serve command disables web UI, serves API only
  • KvBlockPool: fixed-size block allocator with free list, ref counting, copy-on-write
  • KvBlockTable: per-sequence logical→physical block mapping
  • PagedKvCache : IKvCache: paged cache with staging-buffer gather for attention kernel compatibility
  • PagedKvCacheFactory: shared pool across sequences
  • CLI defaults: paged OFF for run/chat (--paged to enable), ON for serve (--no-paged to disable)
  • Fallback with warning when combined with quantized KV or CUDA (not supported in v1)
  • Unit tests: KvBlockPool, KvBlockTable, PagedKvCache (correctness oracle vs SimpleKvCache)
  • Greedy generation produces identical output with --paged and without
  • Docs: KV_CACHE.md CLI section, ROADMAP.md checkmark, README.md update
  • Future considerations added to ROADMAP: paged attention kernels, quantized paged KV, CUDA paged KV

Key Design Decisions

  • Staging buffer (gather) for attention integration — zero kernel changes, gather cost negligible vs O(n²) attention
  • Block size 16 — fits in L1 for typical head dims
  • Per-layer contiguous pool storage — 64-byte aligned, fixed offsets
  • Paged OFF for CLI, ON for serve — single-user CLI doesn't benefit; server batching does

References

  • docs/KV_CACHE.md — PagedAttention design spec
  • Step 7 (Simple KV-cache), Step 33 (KV-cache quantization), Step 34 (ASP.NET server)
  • Foundation for Step 37 (prefix sharing), Step 43 (speculative decoding)

Closes #96

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions