wenmin-wu/ds-skills/kv-cache-prefix-scoring

Claude Code and agent skill by wenmin-wu/ds-skills

Trust tier 2
  • community
LLM review pending
Source check pending

Checking that the SKILL.md file is still in the repository.

Shares KV cache from a common prefix (context+question) across multiple answer suffixes for efficient multi-choice scoring.

Install

npx vskill@latest install wenmin-wu/ds-skills/kv-cache-prefix-scoring

SKILL.md

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

python
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

  • Memory: Cache is ~2x model hidden size per layer; fits for models up to 13B on T4
  • Scoring metric: Mean log-prob normalizes for option length differences
  • Batch suffixes: If GPU memory allows, batch all suffixes in one pass
  • Applicability: Any LLM multiple-choice or ranking task

References

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewNot runA model review is scheduled for the next rescan.

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
35
Repository forks
2
7-day trend score
18

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 28 Apr 2026, 6 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/wenmin-wu/ds-skills/kv-cache-prefix-scoring/badge)](https://verified-skill.com/skills/wenmin-wu/ds-skills/kv-cache-prefix-scoring)

Something wrong with this skill or its page? Tell us.