Kv cache prefix scoring

From wenmin-wu/ds-skills · View interactive version

Kv cache prefix scoring is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Shares KV cache from a common prefix (context+question) across multiple answer suffixes for efficient multi-choice scoring.

How to use this skill

Skill file

KV Cache Prefix Scoring

Overview

For multiple-choice QA, the context and question are shared across all answer options. Compute the KV cache once for this shared prefix, then score each answer suffix by expanding the cache. This avoids redundant computation — scoring 5 options costs ~1.2x of a single forward pass instead of 5x.

Quick Start

import torch

def score_options(model, prefix_ids, suffix_ids_list):
    """Score multiple suffixes against a shared prefix."""
    with torch.no_grad():
        # Forward pass on shared prefix — cache the KV states
        prefix_out = model(prefix_ids, use_cache=True)
        kv_cache = prefix_out.past_key_values

        scores = []
        for suffix_ids in suffix_ids_list:
            # Expand cache to match suffix batch
            expanded_kv = [(k.expand_as(k), v.expand_as(v)) for k, v in kv_cache]
            out = model(suffix_ids, past_key_values=expanded_kv)
            # Score = mean log-prob of suffix tokens
            logprobs = torch.log_softmax(out.logits[:, :-1], dim=-1)
            token_scores = logprobs.gather(2, suffix_ids[:, 1:].unsqueeze(-1))
            scores.append(token_scores.mean().item())
    return scores

Workflow

  1. Tokenize shared prefix (context + question)
  2. Run single forward pass to get KV cache
  3. For each answer option, run forward pass with cached KV states
  4. Score each option by mean log-probability of suffix tokens
  5. Rank options by score

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.