write-triton-kv-cache-append-kernel

Guidance for writing a Triton GPU kernel that adds newly calculated key and value data to a pre-allocated cache during large-language-model text generation. The cache stores attention data so earlier tokens do not need to be recomputed.

In plain words
What is it for?
Implementing contiguous or paged cache updates, handling prompt processing and token-by-token generation, slot mappings, grouped attention heads, and quantized caches.
Why use it?
It helps prevent incorrect or overlapping cache writes across requests, layers, layouts, and numeric formats.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/tensormux/kernel-skills/write-triton-kv-cache-append-kernel
Any agent
npx skills add tensormux/kernel-skills --skill write-triton-kv-cache-append-kernel
Clone the repo
git clone --depth 1 https://github.com/tensormux/kernel-skills

Made for: Claude Code, Codex.

Per session 0 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 4,869 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.04869
Opus 5 $0.00000 $0.02434
Sonnet 5 $0.00000 $0.00974
Haiku 4.5 $0.00000 $0.00487

Measured yesterday against content hash 3d562a4ad60e, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

write-triton-kv-cache-append-kernel scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/inference/write-triton-kv-cache-append-kernel/SKILL.md · 214 lines

How it starts

The opening of the file, as written. The whole thing — 214 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Skill: Write a Triton KV Cache Append Kernel

Purpose

Guide the agent through implementing a Triton kernel that writes newly computed K and V tensors into a pre-allocated KV cache during LLM inference. This covers two cache layouts (contiguous and paged / vLLM-style PagedAttention), unified prefill and decode handling via a slot_mapping tensor, GQA/MQA where the cache stores fewer heads than Q, optional fp8/int8 quantized KV cache with scaling, coalesced versus scattered write patterns, and the boundary checks required to avoid corrupting other requests' cache regions. The kernel runs once per layer per forward step; correctness is the dominant concern, throughput is secondary because the data volume per call is small.


Use this when

  • You are building a custom serving stack and need a KV cache append step that is not provided by vLLM or FlashInfer.
  • You need a paged KV cache layout (block_table indirection) with a non-standard block_size, an exotic block layout, or a sharding scheme like RingAttention that the existing libraries do not support.
  • You need fp8 or int8 quantized KV cache with per-block or per-tensor scales, and the in-tree kernels do not match your scaling convention.
  • You want a unified kernel that handles prefill (many new tokens per request) and decode (one new token per request) through the same slot_mapping interface.
  • You are fusing the append into a custom QKV epilogue and need a reference standalone kernel to verify against.

Do not use this when

  • You are inside vLLM and the existing reshape_and_cache_flash (or reshape_and_cache) kernel already covers your layout. It is heavily tuned and maintained.
  • You are using FlashInfer — its append_paged_kv_cache is well optimized and integrates with the rest of the FlashInfer attention API.
  • You only need a contiguous cache and a simple PyTorch indexing assignment is fast enough. For small batch sizes the launch overhead of a custom kernel can exceed cache[:, :, pos, :] = new_k.
  • You are tempted to fuse the append into the QKV projection in your first iteration. The projection's tile shape constraints and the cache's scatter pattern rarely align cleanly. Write the standalone kernel first, profile, then consider fusion if the launch overhead actually matters.
  • You need a backward pass. KV cache append is inference-only; there is no autograd-relevant version of this kernel.

Read the full file on GitHub · 214 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 214 lines · 0 tokens per session scan A 3d562a4ad60e

Subscribe to this mod's changes

write-triton-kv-cache-append-kernel is a skill published in the GitHub repository tensormux/kernel-skills (70 stars, last pushed 2mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 4,869 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.

Related

Other skills, from other repositories

design-impl-audit

The Design-vs-Implementation Audit Copilot. A universal skill for any project, any language, any design document. Before work starts, it reminds engineers what must be delivered; after code lands, it checks what was actually delivered, what drifted, and what silently went beyond the design. Feed it a design doc plus a…

MagicKidd/Rokid-agentic-workflow · 0 tokens

skill-creator

Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.

MagicKidd/Rokid-agentic-workflow · 45 tokens

brainstorming

Brainstorming design process. Must be used before any creative work - creating features, building components, adding functionality, or modifying behavior. Transforms ideas into complete designs and specifications through collaborative dialogue. Trigger words: brainstorm, design discussion, requirement analysis…

MagicKidd/Rokid-agentic-workflow · 0 tokens

diagnose

Disciplined diagnosis loop for hard bugs and performance regressions. Reproduce → minimise → hypothesise → instrument → fix → regression-test. Use when user says "diagnose this" / "debug this", reports a bug, says something is broken/throwing/failing, or describes a performance regression.

MagicKidd/Rokid-agentic-workflow · 66 tokens

mergeable-feature-dev

For new-module development or cross-module refactors, enforce separation of scaffolding from deliverables so that "locally standalone runnable" and "low-cost mergeable into the trunk" hold simultaneously. Trigger words: mergeable, low-cost handoff, standalone run, scaffolding, deliverable layer, refactor merge.

MagicKidd/Rokid-agentic-workflow · 0 tokens

prompt-engineering

Prompt engineering techniques and patterns. Use when writing agent commands, hooks, skills, subagent prompts, or any LLM interaction: optimizing prompts, improving output reliability, and designing production-grade prompt templates. Trigger words: prompt engineering, prompt, prompt optimization, LLM interaction.

MagicKidd/Rokid-agentic-workflow · 0 tokens