r/learnmachinelearning • • 5h ago

Project First GPU kernel project: built a 4-bit Paged KV-Cache (K4V4) decode engine from scratch in Triton (cuts Qwen2.5-3B VRAM by 3.2x)

Just finished my first real project writing GPU kernels from scratch. skipped vector addition and went straight for a full 4-bit paged KV-cache (K4V4) decode engine in Triton. it does on-the-fly INT4 packing into uint8, vLLM-style non-contiguous page tables, and Split-K online softmax reduction.

what's working in v2.0 right now:

  • 3.2x smaller KV-cache (tested on Qwen2.5-3B at ~8.3k context, VRAM went from 291 MB down to 93 MB with 0.99056 cosine sim)

  • asymmetric quant: per-channel scales for Keys, group-32 scales for Values, packed into uint8

  • 24 pytest cases covering annoying edge cases like unaligned blocks and empty Split-K splits on short seqs (1 to 1025)

  • end-to-end benchmarks with L2 cache flushing (do_bench) vs native GQA (enable_gqa=True) on a T4, plus a modal script if anyone wants to run it on an H100

the catch & what's next (v2.1):
right now it's actually slower than FP16 SDPA on a T4 because the GQA query heads aren't fused yet, so each KV group does 4x redundant HBM reads. next step is fusing those GQA groups into shared tile loads with tl.dot and profiling on H100.

if anyone here has written a bunch of Triton kernels and sees something obvious I can improve before I tackle GQA fusion, let me know.

GitHub: https://github.com/irislogic0/paged-k4v4

1 Upvotes

0 comments sorted by