feat: Add FP4 (E2M1) KV Cache Support with Quantization Utilities for MLA (#10078)
Signed-off-by: Ho-Ren (Jack) Chuang <horenchuang@bytedance.com> Co-authored-by: Yichen Wang <yichen.wang@bytedance.com>
This commit is contained in:
committed by
GitHub
parent
95191ebdca
commit
76196b3cbf
@@ -138,6 +138,8 @@ from sglang.srt.utils import (
|
||||
get_bool_env_var,
|
||||
get_cpu_ids_by_node,
|
||||
init_custom_process_group,
|
||||
is_cuda,
|
||||
is_float4_e2m1fn_x2,
|
||||
is_hip,
|
||||
is_npu,
|
||||
log_info_on_rank0,
|
||||
@@ -195,6 +197,7 @@ def add_chunked_prefix_cache_attention_backend(backend_name):
|
||||
)
|
||||
|
||||
|
||||
_is_cuda = is_cuda()
|
||||
_is_hip = is_hip()
|
||||
_is_npu = is_npu()
|
||||
_is_cpu_amx_available = cpu_has_amx_support()
|
||||
@@ -1273,6 +1276,21 @@ class ModelRunner:
|
||||
* num_layers
|
||||
* torch._utils._element_size(self.kv_cache_dtype)
|
||||
)
|
||||
if is_float4_e2m1fn_x2(self.kv_cache_dtype):
|
||||
# kv_scale_buffer
|
||||
scale_block_size = 16
|
||||
cell_size = (cell_size // 2) + (
|
||||
(
|
||||
(
|
||||
self.model_config.kv_lora_rank
|
||||
+ self.model_config.qk_rope_head_dim
|
||||
)
|
||||
// scale_block_size
|
||||
)
|
||||
* num_layers
|
||||
* torch._utils._element_size(self.kv_cache_dtype)
|
||||
)
|
||||
|
||||
# Add indexer KV cache overhead for NSA models (DeepSeek V3.2)
|
||||
if is_deepseek_nsa(self.model_config.hf_config):
|
||||
index_head_dim = get_nsa_index_head_dim(self.model_config.hf_config)
|
||||
@@ -1509,6 +1527,15 @@ class ModelRunner:
|
||||
self.kv_cache_dtype = torch.float8_e4m3fn
|
||||
elif self.server_args.kv_cache_dtype in ("bf16", "bfloat16"):
|
||||
self.kv_cache_dtype = torch.bfloat16
|
||||
elif self.server_args.kv_cache_dtype == "fp4_e2m1":
|
||||
if hasattr(torch, "float4_e2m1fn_x2"):
|
||||
self.kv_cache_dtype = torch.float4_e2m1fn_x2
|
||||
logger.warning(f"FP4 (E2M1) KV Cache might lead to a accuracy drop!")
|
||||
else:
|
||||
logger.warning(
|
||||
f"--kv-cache-dtype falls back to 'auto' because this torch version does not support torch.float4_e2m1fn_x2"
|
||||
)
|
||||
self.kv_cache_dtype = self.dtype
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Unsupported kv_cache_dtype: {self.server_args.kv_cache_dtype}."
|
||||
|
||||
Reference in New Issue
Block a user