Skip to content

KvCacheStore should declare its Format (dtype + encoding), not just an encoding #1077

Description

@michalharakal

Follow-up from the M1 acceptance work (#1032): writing the plan-vs-actual assertion surfaced that the KV cache's dtype is not declarable, only its encoding.

What is there today

KvCacheConfig carries keyEncoding / valueEncoding: TensorEncoding, defaulting to Dense(4); DefaultKvCacheStore hard-codes FloatArray rings. So a store can say "4 bytes per element" but not "FP32", and nothing connects the two.

The consequence, found by the harness: the memory planner assumed bf16 KV while the dense store holds FP32 — a 2× understatement of the ring. #1032 works around it by adding KvCacheMode.FP32 and having callers pick the right mode; the planner still guesses rather than asks.

Proposal: a Format per side, not type parameters

public val keyFormat: Format     // Format(FP32, Dense(4)) · Format(BF16, Dense(2)) · Format(FP32, TurboQuantPolar(4, 128))
public val valueFormat: Format

Why not KvCacheStore<T : DType, V>

Generic type parameters would push the storage representation onto every caller: attention code, sdpa and the whole SKaiNET-transformers stack would need to name the cache's value type just to hold a reference, and a compressed store could not honestly satisfy V = Float. The boundary API (appendToken(key: FloatArray), readKeys(): FloatArray) is a decoded value contract — SKEEP-003 rule 4 — with encode/decode inside the store. That is exactly the erasure the storage rework removes elsewhere; re-introducing it in the KV API would be a step back.

Scope

  • keyFormat/valueFormat on KvCacheStore (default-derived from the encodings, so no implementation breaks);
  • optional keyFormat/valueFormat on KvCacheConfig;
  • DefaultKvCacheStore honours a narrow-float format (bf16/fp16 rings) as well as FP32;
  • MemoryPlan takes the KV byte width from the store's format when one is supplied, instead of KvCacheMode;
  • TurboQuantKvCacheStore reports its TurboQuant format.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions