Skip to content

Add experimental selective full-attention KV working sets - #295

Draft
Gajesh2007 wants to merge 2 commits into
mainfrom
codex/selective-kv-retention
Draft

Gajesh2007 wants to merge 2 commits into
mainfrom
codex/selective-kv-retention

Conversation

@Gajesh2007

@Gajesh2007 Gajesh2007 commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Full-attention rows currently retain every native K/V entry through decode. Add an explicit experimental working-set policy that preserves dense prefill, initial anchors, at least 512 recent tokens, and half of older history in chronological 16-token chunks. Attention mass from at most four queries ranks chunks; one quarter of the retained chunk budget supplies evenly spaced coverage.

The row's absolute RoPE clock remains independent of retained count, native dtype/value geometry stays intact, and configured sliding windows stay native. Unknown prefix identity fences exact cache export/adoption. MTP is disabled because assistant snapshots lack sparse absolute positions. The companion provider PR #1412 restricts activation to explicit Gemma/GPT-OSS singleton benchmarks.

This remains draft: real generation found a reproducible GPT capped-completion regression. Six frozen-binary native replays finish correctly in 164 tokens; six selective replays exhaust the matched 256-token cap before a final answer. Serving remains rejected. Gemma matches all 14 native output arrays in the measured matrix, including its existing native errors; that is limited parity evidence, not general quality qualification.

Before

flowchart LR
  R["Request"] --> P["Dense prefill"]
  P --> K["CBv2FullSequenceKV: entire history"]
  K --> A["AttentionV1: decode over every entry"]
  A --> O["Next token"]
  classDef input fill:#dbeafe,color:#172554,stroke:#2563eb
  classDef compute fill:#dcfce7,color:#14532d,stroke:#16a34a
  class R,O input
  class P,K,A compute
Loading

After

flowchart LR
  R["Explicit policy"] --> P["Dense prefill"]
  P --> S["SelectiveSequenceKV: rank older chunks"]
  S --> K["Own compact buffers; preserve positions"]
  K --> A["AttentionV1: selected history + new tokens"]
  A --> O["Next token"]
  S -.-> F["Fence prefix export and MTP"]
  W["Native sliding windows"] --> A
  classDef input fill:#dbeafe,color:#172554,stroke:#2563eb
  classDef exact fill:#dcfce7,color:#14532d,stroke:#16a34a
  classDef candidate fill:#fef3c7,color:#78350f,stroke:#d97706
  classDef fence fill:#fee2e2,color:#7f1d1d,stroke:#dc2626
  class R,O input
  class P,K,W exact
  class S,A candidate
  class F fence
Loading

The dedicated refactor removes a redundant gather-to-new-row copy. One gather includes bounded spare slots; the owned row adopts those buffers and overwrites each spare before exposing it. Tests cover native and unequal value widths, spare capacity, rollback, GQA/sinks, chronological selection, Gemma borrowing, and real allocator release.

Validation:

  • Standalone SDK build/tests, lint, fork-map and CodeQL checks pass on 46477f0.
  • Focused nested selective tests and provider coverage pass; the isolated allocator fixture releases exactly 15 MiB after evaluated compaction.
  • Paired native-model pilots retain about 52–53% of full-attention bytes. In 4K/32K generated-output runs, GPT passes 12/12 answer/tool oracles natively and 11/12 selectively; Gemma passes 8/12 in both modes. All main arms pass cancellation/recovery and shutdown checks.
  • Separate local full-test attempts expose existing SwiftPM paged/Qwen4 resource lookup failures; those do not become a green gate merely because focused tests pass.

Full native-model results, artifact hashes, raw reports, refactor evidence and limitations.

The score matrix uses at most four query rows, but non-FP32 key histories are still cast in full to FP32. Gemma 26B's 32K global layer has two KV heads and D512: 128 MiB of key-cast payload per layer, in addition to score/normalization scratch and compact destination overlap. Dense prefill still determines peak prompt memory, and no additional admission capacity is claimed. Fused native-key scoring and protected instruction spans require separate, newly qualified cohorts.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant