Skip to content

Protect a bounded instruction prefix in selective KV retention - #297

Draft
Gajesh2007 wants to merge 1 commit into
codex/selective-kv-retentionfrom
codex/kv-instruction-retention
Draft

Gajesh2007 wants to merge 1 commit into
codex/selective-kv-retentionfrom
codex/kv-instruction-retention

Conversation

@Gajesh2007

@Gajesh2007 Gajesh2007 commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

This adds a bounded, explicit instruction prefix to the existing selective full-attention KV experiment. A caller can protect at most 256 initial tokens; the separate provider candidate fixes this to 128. The default remains zero, preserving the original four-anchor policy. A read-only diagnostics accessor records the actual configured policy.

The physical compaction path, absolute-position clock, chronological retained order, recent band, speculative rollback and conservative native reservation are unchanged. This is a stacked draft on #295. It does not enable production serving or change quantization.

Before

flowchart LR
  A["Full-attention KV history"] --> B["Four initial anchors"]
  A --> C["Rank older chunks plus coverage"]
  C --> D["Early instruction entries may be removed"]
  B --> E["Owned compact KV; original positions"]
  D --> E
  classDef data fill:#dbeafe,stroke:#2563eb,color:#172554
  classDef policy fill:#fef3c7,stroke:#d97706,color:#451a03
  classDef result fill:#dcfce7,stroke:#16a34a,color:#14532d
  class A data
  class B,C,D policy
  class E result
Loading

After

flowchart LR
  A["Explicit bounded prefix policy"] --> B["Protect max of anchors and prefix"]
  B --> C["Rank remaining older chunks plus coverage"]
  C --> D["Same owned compact KV; original positions"]
  D --> E["Native reservation and rollback unchanged"]
  classDef data fill:#dbeafe,stroke:#2563eb,color:#172554
  classDef policy fill:#fef3c7,stroke:#d97706,color:#451a03
  classDef result fill:#dcfce7,stroke:#16a34a,color:#14532d
  class A data
  class B,C policy
  class D,E result
Loading

Validation: two CBv2InstructionPrefixRetentionTests pass on rebuilt native products, including a regression where the ordinary policy loses early entries, repeated pruning, chronology, recent512, absolute clock, speculative rejection and identical dense reservation. Provider integration tests pass separately. Strict Swift formatting and diff checks pass. The required dedicated refactor found and fixed a provider harness environment-injection mismatch in the companion PR; SDK arithmetic remained unchanged. Both published figures were visually verified on GitHub. Full hosted checks are tracked separately.

Quality is not qualified by these unit tests. The companion experiment preregisters one fixed candidate, six recovery observations per arm and twelve new arithmetic/code/tool cases. Full FP32 K-history casts and source/destination overlap still need bounded scratch and serving qualification. No admission credit is taken from logical or observed allocator savings.

The companion fixed campaign is now complete in provider PR #1417: both arms pass 10/12 new cases, but the original recovery regression remains native 6/6 versus candidate 0/6. This candidate remains draft and is not qualified for serving. No repeated tuning followed the result.

javiech1 pushed a commit to javiech1/mlx-swift-lm that referenced this pull request Oct 24, 2026

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