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
outputbuffer. - 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:
(1×4):
(1×3×4):
(1×3):
⇒
(1×3×4):
(1×3×4):
(1×3):
⇒
(1×3×4):
Scores = = , so softmax weights = .
Output (1×4):
Constraints
- 1 ≤
num_heads≤ 64 - 1 ≤
seq_len≤ 32,768 - 8 ≤
head_dim≤ 256;head_dimis a multiple of 8 K_int8andV_int8values are in- 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) {}
From LeetGPU, CC BY-NC-ND 4.0.