Optimize prefill vs decode kernels

From wenyi-li/awesome-agent-kernel-skills · View interactive version

Optimize prefill vs decode kernels is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Guide the agent through choosing and tuning kernels for the prefill phase versus the decode phase of LLM inference. The two phases have fundamentally different arithmetic intensity, occupy different s

How to use this skill

Skill file

Skill: Optimize Prefill vs Decode Kernels

Purpose

Guide the agent through choosing and tuning kernels for the prefill phase versus the decode phase of LLM inference. The two phases have fundamentally different arithmetic intensity, occupy different sides of the roofline, and respond to different optimizations. Continuous batching and speculative decoding shift the balance and must be reasoned about explicitly.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Identify the phase and resolve the effective M. Prefill: M = seq_len (per request) or sum of seq_lens (mixed batch). Decode: M = 1 per request, M = B for plain decode batch of B requests, M = k * B for speculative decode with tree size k. Write down M, K, N for the dominant matmul before going further.
  1. Compute arithmetic intensity for each dominant matmul. For an M×K @ K×N matmul: FLOPs ≈ 2*M*K*N, bytes (fp16) ≈ (M*K + K*N + M*N) * 2, intensity = FLOPs / Bytes.
  1. Locate against the hardware roofline. Compute the machine balance: peak_FLOPs / peak_bandwidth. For H100 SXM (fp16 tensor core): ~989 TFLOP/s / 3.35 TB/s ≈ 295 FLOPs/byte. For MI300X: ~1300 TFLOP/s / 5.3 TB/s ≈ 245. Any matmul with intensity below the machine balance is memory-bound. Decode (~1 FLOP/byte) sits roughly two orders of magnitude below the ridge — it cannot be made compute-bound by tuning alone.
  1. Pick the kernel family by phase.
  1. Decide split-K (or split-KV) factor for decode. Target grid size ≥ numSMs * 2–4. Split factor S = ceil(numSMs * target_blocks_per_SM / (M_blocks * N_blocks)); for decode, S is often 4–32. Each split adds a partial output and a final reduction — clamp S so each split processes ≥ ~512–1024 K elements, otherwise the reduction dominates. For attention, split along KV-cache sequence with LSE-correct combine.
  1. Choose tile shapes and dtypes.
  1. Account for KV cache traffic explicitly. Per decode step, every layer reads the entire per-request KV history. KV bytes per step = 2 * num_layers * num_kv_heads * head_dim * past_len * dtype_bytes. For a 70B model with GQA at past_len=8k this is hundreds of MB per request per step and frequently exceeds weight-load bandwidth. KV intensity in attention is ~1 FLOP/byte; reason about it as a first-class bottleneck, not an afterthought.
  1. Reason about continuous batching heterogeneity. A single batch may mix prefilling (large M) and decoding (M=1) requests. Two strategies:

The kernel is selected per batch, not per model. State which strategy the engine uses before tuning.

  1. Account for speculative decoding. With k draft tokens verified per step, M jumps from 1 to k:
  1. Evaluate tradeoffs and decide. Write down: chosen kernel family per phase, split factors, expected occupancy, expected memory-bandwidth utilization, and what is being measured (TTFT, ITL, throughput). State the conditions under which the choice would flip — these are the conditions that warrant retuning later.

Kernel design rules

Correctness requirements

Performance requirements

Output format

The agent should produce:

  1. Phase identification: prefill, decode, or mixed; effective M, K, N for each dominant matmul; KV cache bytes per step per request.
  2. Roofline placement: arithmetic intensity per kernel, machine balance of the target HW, classification (memory-bound / compute-bound / at the knee).
  3. Kernel selection table: per phase × per operation (QKV proj, attention, MLP up/gate, MLP down, output proj), the chosen kernel family, tile shape, split factor, expected bottleneck.
  4. Continuous batching strategy: chunked prefill vs separate batches, with rationale tied to SLO targets.
  5. Speculative decoding adjustment (if applicable): how the selection changes when M jumps from 1 to k, and the breakeven acceptance rate.
  6. Tradeoff summary: 2–3 explicit tradeoffs.
  7. What to measure to validate: TTFT, ITL p50/p99, achieved bandwidth on decode, achieved TFLOPs on prefill, and the test workloads.

Common failure modes

Review checklist

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.