LeetKernels
ProblemsStart solving
Problems

96. INT8 KV-Cache Attention medium

Implement decode-phase multi-head attention where the key and value caches are stored as int8 with per-token scale factors. This memory layout halves KV-cache bandwidth versus float32 and is used in production LLM serving systems such as TensorRT-LLM and vLLM. Given a query tensor Q for a single new token, int8 key cache K_int8, int8 value cache V_int8, and per-token scales k_scale and v_scale, dequantize the caches and compute scaled dot-product attention to produce output. All non-integer tensors use float32.

Implementation Requirements

  • Implement the function solve(Q, K_int8, V_int8, k_scale, v_scale, output, num_heads, seq_len, head_dim).
  • Do not change the function signature or use external libraries beyond the standard GPU frameworks.
  • Write the result into the provided output buffer.
  • Dequantize using per-token scales: K_float[h, s, d] = K_int8[h, s, d] × k_scale[h, s] (and analogously for V).
  • Use scaled dot-product attention with scale factor 1 / sqrt(head_dim) and a softmax over the sequence dimension.

Example

With num_heads = 1, seq_len = 3, head_dim = 4:

Input:
Q (1×4): [1​1​1​1​] K_int8 (1×3×4): ​1000​0100​0010​000​​ k_scale (1×3): [0.1​0.1​0.1​]  ⇒  K_float (1×3×4): ​100​010​001​000​​ V_int8 (1×3×4): ​105090​2060100​3070110​4080120​​ v_scale (1×3): [0.1​0.1​0.1​]  ⇒  V_float (1×3×4): ​159​2610​3711​4812​​

Scores = Q⋅K_floatT/4​ = [0.5​0.5​0.5​], so softmax weights = [1/3​1/3​1/3​].

Output (1×4): [5.00​6.00​7.00​8.00​]

Constraints

  • 1 ≤ num_heads ≤ 64
  • 1 ≤ seq_len ≤ 32,768
  • 8 ≤ head_dim ≤ 256; head_dim is a multiple of 8
  • K_int8 and V_int8 values are in [−128,127]
  • All scale values are positive float32
  • Performance is measured with num_heads = 32, seq_len = 8,192, head_dim = 128

Starter code (CUDA)

#include <cuda_runtime.h>

// Q, K_int8, V_int8, k_scale, v_scale, output are device pointers
extern "C" void solve(const float* Q, const int8_t* K_int8, const int8_t* V_int8,
                      const float* k_scale, const float* v_scale, float* output, int num_heads,
                      int seq_len, int head_dim) {}

Solve it on your GPU

Previous: SSM Selective ScanNext: Group Normalization

From LeetGPU, CC BY-NC-ND 4.0.