Skip to content

fix: sync server_args.kv_cache_dtype when detecting FP8 KV cache#18394

Merged
Fridge003 merged 1 commit intosgl-project:mainfrom
zack041:fix-kv-cache-dtype
Feb 8, 2026
Merged

fix: sync server_args.kv_cache_dtype when detecting FP8 KV cache#18394
Fridge003 merged 1 commit intosgl-project:mainfrom
zack041:fix-kv-cache-dtype

Conversation

@zack041
Copy link
Contributor

@zack041 zack041 commented Feb 7, 2026

Motivation

fixes #18290 #12298

Source of error:
In sglang/srt/model_executor/model_runner.py

    def configure_kv_cache_dtype(self):
        if self.server_args.kv_cache_dtype == "auto":
            quant_config = getattr(self.model, "quant_config", None)
            kv_cache_quant_algo = getattr(quant_config, "kv_cache_quant_algo", None)
            if (
                isinstance(kv_cache_quant_algo, str)
                and kv_cache_quant_algo.upper() == "FP8"
            ):
                if _is_hip:
                    self.kv_cache_dtype = fp8_dtype
                    self.server_args.kv_cache_dtype = "FP8"
                else:
                    self.kv_cache_dtype = torch.float8_e4m3fn
                    self.server_args.kv_cache_dtype = "FP8"
            else:
                self.kv_cache_dtype = self.dtype

While configure_kv_cache_dtype correctly handles the update of self.kv_cache_dtype for modelopt case when server_args.kv_cache_dtype is "auto", it did not update itself. server_args.kv_cache_dtype is used in flashattention_backend.py to determine the data type for kv cache:

if self.kv_cache_dtype_str != "auto" and layer.head_dim <= 256:
            if layer.k_scale is not None:
                descale_shape = (forward_batch.batch_size, layer.tp_k_head_num)
                k_descale = layer.k_scale.expand(descale_shape)
                v_descale = layer.v_scale.expand(descale_shape)
            q = q.to(self.kv_cache_dtype)
            q_rope = q_rope.to(self.kv_cache_dtype) if q_rope is not None else None
            k_rope = k_rope.to(self.kv_cache_dtype) if k_rope is not None else None

self.kv_cache_dtype_str is assigned with server_args.kv_cache_dtype.

Modifications

To solve, the server_args.kv_cache_dtype is also updated to FP8 along with kv_cache_dtype.

Accuracy Tests

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Copy link
Collaborator

@b8zhong b8zhong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think this is the right solution, compared to the other ones. Could you maybe try to use a util (e.g mapping torch dtype to str for KV cache here)

@b8zhong
Copy link
Collaborator

b8zhong commented Feb 7, 2026

/tag-and-rerun-ci

@github-actions github-actions bot added the run-ci label Feb 7, 2026
@zack041 zack041 force-pushed the fix-kv-cache-dtype branch from aba3ec7 to a47f0ee Compare February 7, 2026 23:29
@zack041
Copy link
Contributor Author

zack041 commented Feb 7, 2026

Thanks! I think this is the right solution, compared to the other ones. Could you maybe try to use a util (e.g mapping torch dtype to str for KV cache here)

Added a mapping dict!

@Fridge003 Fridge003 merged commit d71ccd8 into sgl-project:main Feb 8, 2026
206 of 218 checks passed
@b8zhong
Copy link
Collaborator

b8zhong commented Feb 8, 2026

Hi if you could add a test for this Modelopt model on SM90, that would be really great. @zack041 Thanks a lot!

@zack041
Copy link
Contributor Author

zack041 commented Feb 9, 2026

Hi if you could add a test for this Modelopt model on SM90, that would be really great. @zack041 Thanks a lot!

Sure! I'll add the test in a follow-up PR.

1StepForever pushed a commit to 1StepForever/sglang that referenced this pull request Feb 26, 2026
* www/pr/ks: (265 commits)
  [BugFix][PD]Fix metadata_buffer_index leak when aborted in PD (sgl-project#17483)
  Refactoring Mooncake TE as a shared distributed component (sgl-project#17810)
  [ModelOPT] Support Qwen 3 Next Coder NVFP4 (sgl-project#18224)
  Update author information in pyproject.toml (sgl-project#18453)
  [Kimi-K2.5] Fix missing `quant_config` in `KimiK25` (sgl-project#18440)
  Add tensor parallelism support to LFM2 ShortConv layers (sgl-project#17777)
  [diffusion] chore: revise process title (sgl-project#18446)
  Fix TRT-LLM MLA backend applying k_scale to BF16 KV cache in BMM1 (sgl-project#18396)
  [diffusion] refactor: group component loaders under the component_loaders/ directory (sgl-project#18438)
  [ModelOpt] Fix broken Qwen3-235B-A22B-Instruct-2507-NVFP4 launch (sgl-project#18189)
  [diffusion] feat: support efficient sequence shard (sgl-project#18161)
  [CI] fix: notebook ci may not working (sgl-project#18417)
  fix: sync server_args.kv_cache_dtype when detecting FP8 KV cache (sgl-project#18394)
  [Fix] Fix backend selection after flashinfer version update (sgl-project#18364)
  [diffusion] platform: support WAN/FLUX/Qwen-Image/Qwen-Image-edit on Ascend (sgl-project#13662)
  fix: fix NVFP4 Kimi-K2.5 weight mapping and exclude list (sgl-project#18370)
  [diffusion] feat: support saving videos directly on the server to avoid the overhead of tensor transfer (sgl-project#18253)
  [diffusion] fix: respect dist_timeout option (sgl-project#18386)
  [Doc] Fix outdated `--fp4-gemm-backend` documentation (sgl-project#18350)
  [diffusion] fix: remove unnecessary norm_type argument from GLM-Image dits (sgl-project#18382)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] RuntimeError in flash_attn_with_kvcache: query and key must have the same dtype with FP8 models

3 participants