[RL] DSV4: add env to quantize SWA KV cache from bf16-rounded values (#31086)

Signed-off-by: zhihaow6 <zhihaow6@illinois.edu>
This commit is contained in:
Zhihao Wang
2026-07-24 15:52:00 -07:00
committed by GitHub
parent 4ececf2b1d
commit 0a212c6119
3 changed files with 17 additions and 0 deletions
@@ -710,6 +710,11 @@ SGLang supports various environment variables that can be used to configure its
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>Default <code>reasoning_effort</code> for the DeepSeek V4 chat encoder when a request does not set it (accepts <code>max</code>, <code>high</code>; empty means unset).</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}><code>""</code></td>
</tr>
<tr>
<td style={{padding: "9px 12px", fontWeight: 500, backgroundColor: "rgba(255,255,255,0.02)"}}><code>SGLANG_DSV4_USE_BF16_KV_QUANT_SOURCE</code></td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.05)"}}>For DeepSeek V4, quantize the SWA FP8 KV cache from BF16-rounded values instead of FP32 registers. This matches trainer-side QAT and the DSA prefill-CP path, at the cost of an extra BF16 KV materialization and separate cache-store kernels.</td>
<td style={{padding: "9px 12px", backgroundColor: "rgba(255,255,255,0.02)"}}><code>false</code></td>
</tr>
</tbody>
</table>
+3
View File
@@ -1018,6 +1018,9 @@ class Envs:
# Default reasoning_effort for dsv4 chat encoder when request doesn't set it.
# Accepts "", "max", "high" (empty string means unset); other values filtered to None.
SGLANG_DSV4_REASONING_EFFORT = EnvStr("")
# Quantize the SWA fp8 KV cache from bf16-rounded values (matches
# trainer-side QAT and the DSA-CP path) instead of fp32 registers.
SGLANG_DSV4_USE_BF16_KV_QUANT_SOURCE = EnvBool(False)
# CUDA kernels
SGLANG_OPT_DEEPGEMM_HC_PRENORM = EnvBool(True)
+9
View File
@@ -722,6 +722,15 @@ class MQALayer(MqaAttentionBase):
Replaces the bf16-kv-intermediate path. Used everywhere except the DSA
prefill-CP case (which needs bf16 kv for the cross-rank all-gather).
"""
if envs.SGLANG_DSV4_USE_BF16_KV_QUANT_SOURCE.get():
# Quantize the nope payload from bf16-rounded values (the fused
# kernel quantizes from fp32 registers; the bf16 rounding moves
# values across fp8 bins relative to bf16-sourced consumers).
kv = self._compute_kv_bf16(x, positions, qkv_a=qkv_a)
attn_backend.store_cache(
layer_id=self.layer_id, swa_k=kv, forward_batch=forward_batch
)
return
if qkv_a is not None:
kv = qkv_a[..., self.q_lora_rank :]
else: