To prevent the perplexity degradation caused by MSE-only attention scores in 3-bit compressed KV caches, implement the QJL (two-term unbiased estimator) correction. This correction compensates for the bias in MSE-reconstructed keys by adding a second term based on a random Gaussian projection.
The QJL Estimator Formula
$$\langle q, k \rangle \approx \underbrace{\langle q, k_{mse} \rangle}{term1} + \underbrace{\frac{|residual| \cdot \sqrt{\pi/2}}{m} \cdot \langle S@q, \text{sign}(S@residual) \rangle}{term2 (QJL\ correction)}$$
Implementation Requirements
1. Storage Updates
Add the following to the compressed cache for each key:
qjl_signs: Packed bits representing sign(S @ residual.T) as int8 {+1, -1} with shape [batch, n_kv_heads, kv_len, head_dim].residual_norms: fp16 values representing ||residual|| with shape [batch, n_kv_heads, kv_len].S: A random Gaussian projection matrix of shape [head_dim, head_dim] (shared or per-layer).
2. Key Compression Workflow
After MSE quantization, compute the QJL components:
- Dequantize indices to get
k_mse. - Calculate
residual = k_original - k_mse. - Compute
residual_norm = ||residual||. - Compute
qjl_signs = sign(S @ residual.T).
3. Query Pre-processing
Project the query through the projection matrix S to create a sketch:
query_sketch = Q @ S.T (Shape: [batch, n_heads, q_len, m])
4. Fused Kernel Logic
The Triton kernel must compute the score using both terms:
term1: The existing MSE score using norm * sum_d(Q_rot[d] * centroids[idx[s,d]]).term2: The QJL correction using res_norm[s] * sqrt(π/2)/m * sum_d(q_sketch[d] * signs[s,d]).score[s] = (term1 + term2) * scale
# Key compression logic snippet
k_mse = dequantize(indices, key_norms)
residual = k_original - k_mse
residual_norm = ||residual||
qjl_signs = sign(S @ residual.T) # 1-bit per dim
# Query pre-processing
query_sketch = Q @ S.T # [batch, n_heads, q_len, m]
# Fused kernel score calculation
term1 = norm * sum_d(Q_rot[d] * centroids[idx[s,d]])
term2 = res_norm[s] * sqrt(π/2)/m * sum_d(q_sketch[d] * signs[s,d])
score[s] = (term1 + term2) * scale