Skip to content

[Idea]: More Flexible KV Cache Allocation and Advanced Quantization #105

Backlog

Description

@baiomys

What problem would this solve?

Motivation

RustingStrata already supports KV cache quantization through --kv, with the available options reducing memory consumption and leaving more VRAM available for MoE experts.

However, the current approach appears to rely on a fixed cache format and allocation strategy.

This becomes a significant limitation on GPUs with 10–12 GB of VRAM. For example, fitting a 100K-token context while retaining enough VRAM for the expert cache can be difficult even with Q8 KV quantization.

I'd like to propose three improvements, ranging from relatively straightforward to more advanced.

Proposed improvement

1. Independent K and V cache quantization

Add separate configuration options for Key and Value cache formats, following the llama.cpp interface:

-ctk q8_0 -ctv q4_0

Ideally, RustingStrata could expose equivalent options, for example:

--kv-k q8
--kv-v q4

This would allow users to retain higher precision for the Key cache while using more aggressive quantization for the Value cache.

The important point is that K and V should be independently configurable, rather than being forced to use the same quantization format.

This is a relatively straightforward improvement and would provide an immediate additional memory/quality trade-off.

2. Implement SJ-KVaRN KV cache support

A much more interesting option would be integrating SJ-KVaRN (Staged, Journaled KVarN), as implemented in llamAmpere.

Unlike conventional block-wise quantization, SJ-KVaRN uses a staged and journaled architecture:

  • A 128-token FP16 sink preserves the initial positions.
    
  • New K/V entries are written into a tq6_0 staging ring.
    
  • Recent tokens remain in a high-quality adaptive tail.
    
  • Older tokens are periodically committed in batches into a compressed KVarN body.
    
  • The body uses Hadamard rotation, variance normalization and asymmetric quantization.
    
  • Trellis-coded quantization is available for more aggressive compression.
    

The default adaptive tail grows from 4096 to 8192 tokens before committing older entries in batches.

This avoids continuously quantizing individual incoming tokens and preserves higher precision for recent context.

SJ-KVaRN offers several useful configurations:
Configuration | K | V | Use case
4/4 | 4-bit | 4-bit | Quality-oriented
3/3t | 3-bit | 3-bit | Smaller VRAM budgets
3/2t | 3-bit | 2-bit | Maximum context per GB

In llamAmpere, these configurations are selected using:

-ctk sjkvarn4 -ctv sjkvarn4

Or, for more aggressive compression:

-ctk sjkvarn3 -ctv sjkvarn2 --sjkvarn-body-type auto

This would be particularly interesting for RustingStrata because reducing KV cache consumption directly increases the amount of VRAM available for MoE expert residency.

Reference implementation:
Reference integration:

3. Dynamic KV cache allocation and adaptive precision

The most ambitious improvement would be making KV cache allocation adaptive to actual context usage and available VRAM.

Currently, cache allocation appears to be largely determined at model initialization. This means that VRAM is reserved according to the configured context size, potentially limiting the memory available for expert residency even when the actual context is still relatively short.

Instead, consider implementing an elastic KV cache that can grow as the context expands.

Alternatives and tradeoffs

No response

Activity

  1. sayampy commented on Nov 25, 2026

    @sayampy
    Contributor

    For the 1st Suggestion, I already did that and opened a PR(within the turboquant PR), If it gets merged then there will be separated --k and --v instead of --kv.
    Edit: Also after a quick go-through of SJ-KVaRN, I think I can try a faithful implementation since I am working on lowering KV cache with turboquant.

  2. Vasyl-Trefilov commented on Nov 26, 2026

    @Vasyl-Trefilov
    Member

    Hi, ty for the long write up. Separate K and V types are in main now with #84 from sayampy (ty!), so --k and --v work, for example --k q8 --v q4, and on CUDA also the turbo types. For the dynamic part, the parallel serve path on branch vllm2 already starts each slot at 512 tokens of KV and grows it when needed, I will look at doing the same for one user. SJ-KVaRN is interesting but big, sayampy wants to try it, so I leave it open for that.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions