Skip to content

Conversation

@chenyang78
Copy link
Contributor

@chenyang78 chenyang78 commented Jul 10, 2025

This PR fixed fp8 kv-cache issues for the FlashInfer attn backend.

Along with #17005, got reasonable eval results on B200:

$ VLLM_USE_V1=1 VLLM_ATTENTION_BACKEND=FLASHINFER lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,kv_cache_dtype=fp8 --trust_remote_code --tasks gsm8k --num_fewshot 5 --batch_size auto
...
vllm (pretrained=meta-llama/Llama-3.1-8B-Instruct,kv_cache_dtype=fp8,trust_remote_code=True), gen_kwargs: (None), limit: None, num_fewshot: 5, batch_size: auto
|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.7779|±  |0.0114|
|     |       |strict-match    |     5|exact_match|↑  |0.7582|±  |0.0118|

compared with bf16 kv-cache

$ VLLM_USE_V1=1 VLLM_ATTENTION_BACKEND=FLASHINFER lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct --trust_remote_code --tasks gsm8k --num_fewshot 5 --batch_size auto
...
|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.7756|±  |0.0115|
|     |       |strict-match    |     5|exact_match|↑  |0.7498|±  |0.0119|

Tags:

Essential Elements of an Effective PR Description Checklist

  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

Purpose

Test Plan

Test Result

(Optional) Documentation Update

This PR fixed fp8 kv-cache issues for the FlashInfer attn
backend.

Along with vllm-project#17005,
got reasonable eval results on B200:

```
$ VLLM_USE_V1=1 VLLM_ATTENTION_BACKEND=FLASHINFER lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,kv_cache_dtype=fp8 --trust_remote_code --tasks gsm8k --num_fewshot 5 --batch_size auto
...
vllm (pretrained=meta-llama/Llama-3.1-8B-Instruct,kv_cache_dtype=fp8,trust_remote_code=True), gen_kwargs: (None), limit: None, num_fewshot: 5, batch_size: auto
|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.7779|±  |0.0114|
|     |       |strict-match    |     5|exact_match|↑  |0.7582|±  |0.0118|
```

compared with bf16 kv-cache

```
$ VLLM_USE_V1=1 VLLM_ATTENTION_BACKEND=FLASHINFER lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct --trust_remote_code --tasks gsm8k --num_fewshot 5 --batch_size auto
...
|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.7756|±  |0.0115|
|     |       |strict-match    |     5|exact_match|↑  |0.7498|±  |0.0119|
```

Tags:
Signed-off-by: Yang Chen <[email protected]>
@github-actions
Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @chenyang78, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves critical issues related to the use of FP8 KV-cache with the FlashInfer attention backend. The changes ensure that FP8 data types are correctly identified, handled, and cast throughout the KV-cache management and attention computation processes, leading to stable and accurate model evaluation results when using FP8 precision.

Highlights

  • FP8 Type Mapping Utility: Introduced a new static method get_fp8_dtype_for_flashinfer within FlashInferBackend to accurately map string representations of FP8 data types (e.g., "fp8", "fp8_e4m3", "fp8_e5m2") to their corresponding torch.dtype objects, ensuring correct type resolution for FlashInfer.
  • Correct KV-Cache Type Handling in Build Process: Modified the build method within FlashInferBackend to ensure the correct torch.dtype for FP8 KV-cache is passed to FlashInferMetadata. This change addresses an issue where the gpu_model_runner might incorrectly convert fp8 to uint8, preserving the intended FP8 precision.
  • Explicit KV-Cache Type Casting in Forward Pass: Added logic to the forward method of FlashInferBackend to explicitly view the KV-cache tensor to the correct torch.float8_e4m3fn or torch.float8_e5m2 dtype when FP8 KV-cache is enabled. This ensures that FlashInfer receives the KV-cache data in the precise FP8 format it expects for computation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@mergify mergify bot added the v1 label Jul 10, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This PR fixes fp8 kv-cache issues for the FlashInfer attention backend. I suggested storing the fp8 dtype in self to avoid re-deriving it, improving efficiency and consistency.

Comment on lines +494 to +496
if cache_dtype.startswith("fp8"):
kv_cache_dtype = FlashInferBackend.get_fp8_dtype_for_flashinfer(
cache_dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The torch.dtype for fp8 is derived using FlashInferBackend.get_fp8_dtype_for_flashinfer.[^1] Consider storing this dtype in self during initialization to avoid re-deriving it on each call to build. This can improve efficiency.

self.kv_cache_dtype_fp8 = FlashInferBackend.get_fp8_dtype_for_flashinfer(
                cache_dtype)

head_dim=self.kv_cache_spec.head_size,
page_size=page_size,
data_type=self.kv_cache_spec.dtype,
data_type=kv_cache_dtype,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The torch.dtype for fp8 is already computed and stored in self.kv_cache_dtype_fp8. Instead of using kv_cache_dtype here, use self.kv_cache_dtype_fp8 to ensure consistency and avoid potential errors if kv_cache_dtype is modified elsewhere.

            data_type=self.kv_cache_dtype_fp8,

Comment on lines +644 to +646
torch_dtype = FlashInferBackend.get_fp8_dtype_for_flashinfer(
self.kv_cache_dtype)
kv_cache = kv_cache.view(torch_dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The torch.dtype for fp8 is derived using FlashInferBackend.get_fp8_dtype_for_flashinfer.[^1] Consider storing this dtype in self during initialization to avoid re-deriving it here. This can improve efficiency.

            kv_cache = kv_cache.view(self.kv_cache_dtype_fp8)

@mgoin
Copy link
Member

mgoin commented Jul 10, 2025

Thanks for finding this fix! Pavani seemed to uncover the same in her TRTLLM decode integration PR #19825, and since that is high priority I think we'll try to merge it in there today

@chenyang78
Copy link
Contributor Author

Thanks for finding this fix! Pavani seemed to uncover the same in her TRTLLM decode integration PR #19825, and since that is high priority I think we'll try to merge it in there today

Sounds good. Thanks for the heads-up.

@chenyang78
Copy link
Contributor Author

Closing this one per discussion.

@chenyang78 chenyang78 closed this Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants