KBench / agent /README.md
ZMC2019's picture
agent/: Ada/Lovelace and Blackwell coverage, verified by compilation
7202032 verified
|
Raw
History Blame Contribute Delete
6.65 kB

The KBench kernel agent

A Claude Code skill and four subagents for writing and optimizing GPU kernels. Built for the KBench tasks in this repo, but nothing here is KBench-specific — it works on any kernel with a correctness check and a timer.

The premise it is built around: the score is an absolute metric and it is uncapped. There is no "good enough", so the agent is told to keep attacking the largest remaining component rather than to stop at a percentage of roofline.

Install

Copy into a project so Claude Code can find it:

mkdir -p .claude/skills/kernel-optimization .claude/agents
cp -r agent/*.md agent/ref agent/tools .claude/skills/kernel-optimization/
cp agent/agents/kernel-*.md .claude/agents/
rm .claude/skills/kernel-optimization/README.md          # this file; not part of the skill
chmod +x .claude/skills/kernel-optimization/tools/*.sh   # exec bits do not survive the download

Paths inside SKILL.md assume that layout. The skill triggers on requests like "make this kernel faster", "implement a fused attention kernel", or "solve this KBench task".

What is in it

file purpose
SKILL.md the 7-step loop: harness → roofline → algorithm → toolchain → implement → profile → specialize → iterate
profiling.md nsys before ncu, the metric→conclusion tree, and the permission wall
toolchain.md CUDA vs Triton vs CUTLASS vs CuTe — where Triton wins and where it loses
algorithms.md the restructuring catalog: online softmax, chunked recurrences, recompute-in-backward
hardware.md the sm_80/90/100 capability ladder
pitfalls.md measurement lies, correctness traps, and how to read a profile without chasing a healthy-looking counter
references.md which open-source kernel demonstrates which technique

Language references (ref/)

file covers
ref/ptx.md inline PTX: constraints, cache hints, cp.async, TMA, mbarrier, mma/wgmma, ldmatrix/stmatrix, setmaxnreg, fast-math opcodes
ref/cuda-cpp.md CUDA C++: opt-in shared memory, cuda::pipeline, cooperative groups, clusters/DSMEM, host-side TMA descriptors, build and debug flags
ref/triton.md the Triton 3.6 API surface, launch knobs, idioms, and where it caps out
ref/cute-cutlass.md CuTe layout algebra, swizzles, atoms, and where CUTLASS lives in the image
ref/official-docs.md a working guide to NVIDIA's own documentation: what each guide contains, chapter by chapter, which one answers which question, version matching, and the licence position

These are verified, not recalled. Every instruction and API was compiled with the container's own nvcc on sm_90a / CUDA 12.8: 39/41 PTX instructions assemble (the two that do not are recorded — tcgen05 is Blackwell-only), 24/24 CUDA C++ constructs compile, and the Triton tables come from introspecting the installed 3.6.0.

Two examples of what that buys you. wgmma.m64nNk16.f32 needs exactly N/2 accumulator registers per thread — measured across five shapes — so an m64n256k16 tile spends 128 registers on the accumulator alone, which is what forces warp specialisation. And Triton 3.6 accepts cache_modifier=".cg" together with eviction_policy="evict_first" but ptxas rejects the pair; the full combination matrix is in ref/triton.md.

Tools (tools/)

  • roofline.py — measures the device rather than quoting a datasheet, then gives the floor and whether you are compute- or memory-bound.
  • bench.py — CUDA events, fresh inputs per rep, min-of-N, warmup excluded, and a warning when the measurement is host-bound.
  • profile.shnsys mode answers "is the time even in a kernel?"; ncu mode emits a diagnosis, not a metric dump.
  • dump_ir.sh — Triton PTX/TTGIR/cubin, Inductor's generated Triton, and SASS. Reading the SASS is how you confirm the MMA you intended actually issued and that nothing spilled.
  • occupancy.py — finds the occupancy limiter and checks a persistent grid for the co-residency deadlock.
  • check_toolchain.py — re-derives every table in ref/ by compiling on the machine you are actually on; --matrix compares sm_89 (Ada), sm_90a (Hopper) and sm_100a/sm_120a (Blackwell) side by side, which works even for architectures you do not own. The docs were verified on sm_90a / CUDA 12.8; on a different GPU or toolkit some rows change, and this tool wins over the docs.
  • fetch_nvidia_docs.sh / search_docs.sh — downloads NVIDIA's official CUDA/PTX guides as greppable text (~11 MB) and searches them offline. The documents are not bundled here: NVIDIA's documentation is copyrighted and the CUDA EULA distributes only the runtime libraries listed in its Attachment A, which does not include documentation — so you fetch your own copy under NVIDIA's terms. Task containers have network at image-build time and none at run time, so a Dockerfile RUN step puts the guides inside an offline container. It auto-matches your CUDA version, which matters: the live docs always serve the newest CUDA, and 12.8 wants PTX ISA 8.7, not the 9.3 now published. PDFs are deliberately not fetched — a 700-page reference you cannot grep is not useful offline.
  • ledger.md — one row per variant. The column that matters records whether an abandoned variant was slower or merely broken; dropping a good optimization over a fixable bug is the most common way to leave 2x on the table.

Subagents (agents/)

kernel-algorithmist restructures the maths, kernel-implementer writes it, kernel-profiler diagnoses the bottleneck, kernel-verifier builds the harness and measures the tolerance. The split exists mostly to keep the profiler's output out of the implementer's context.

Two things that will bite you

ncu needs GPU performance counters. Without them it exits with ERR_NVGPUCTRPERM. Run the container with --cap-add SYS_ADMIN, or set NVreg_RestrictProfilingToAdminUsers=0 on the host. ncu --version succeeding proves nothing — it never touches a counter. nsys needs no extra capability, and it is the right first tool anyway.

Counters are hints, not verdicts. Measured here on an H200: cuBLAS's own bf16 GEMM profiles at SM 90.5%, DRAM 16.5%, occupancy 14.6%, 168 registers/thread and 136k shared-memory bank conflicts — and it is essentially the fastest thing on the device. Three of those numbers look like defects. Establish which resource is saturated first, then look for a specific mechanism explaining the gap.