Skip to content

[Model] New model support for microsoft/Phi-4-mini-flash-reasoning #20702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

congcongchen123
Copy link
Contributor

@congcongchen123 congcongchen123 commented Jul 9, 2025

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

New Model for https://huggingface.co/microsoft/Phi-4-mini-flash-reasoning

co-author: @aatkinson and @renll

Test Plan

Test Result

(Optional) Documentation Update

Copy link

github-actions bot commented Jul 9, 2025

👋 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 @congcongchen123, 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 introduces full support for the microsoft/Phi-4-mini-flash-reasoning model, which utilizes a novel hybrid architecture integrating both FlashAttention and Mamba (State Space Model) layers. The changes involve implementing a new 'Differential FlashAttention' backend, integrating Mamba's selective scan and causal convolution operations, and enhancing the KV cache management system to support cross-layer sharing, optimizing memory usage for this complex model.

Highlights

  • New Model Support: Adds comprehensive support for the microsoft/Phi-4-mini-flash-reasoning model, which features a hybrid architecture combining FlashAttention and Mamba (SSM) layers.
  • Differential FlashAttention Backend: Introduces a specialized attention backend (DifferentialFlashAttentionBackend) designed for the Phi-4 model, implementing a unique 'differential' attention mechanism with split QKV and KV cache handling.
  • Mamba (SSM) Integration: Implements the Phi4Mamba module to support the State Space Model components of the Phi-4 architecture, including causal convolution and selective scan operations, with specific handling for KV cache sharing (yoco_kv, yoco_cross).
  • Cross-Layer KV Cache Sharing: Enhances the KV cache management system to support cross-layer KV cache sharing, allowing certain attention layers to reuse the KV cache from other layers, which can lead to significant memory savings for the Phi-4 model's hybrid architecture.
  • Dynamic SSM z Parameter: Modifies the Mamba selective scan CUDA kernel and its Python wrapper to dynamically handle the z parameter, making it optional rather than a mandatory input, which improves flexibility.
  • Conditional Hidden State Pruning: Adds a new parameter to the LogitsProcessor to conditionally prune hidden states, enabling optimization for specific model architectures like Phi-4 where intermediate states might be truncated.
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.

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

The pull request introduces a new model architecture, Phi4Flash, with a hybrid Mamba/Attention mechanism and cross-layer KV cache sharing. The changes involve modifications to CUDA kernels, attention backends, model layers, and configuration files. The review focuses on ensuring the correctness, maintainability, and performance of the new implementation, particularly concerning the handling of conditional logic, error conditions, and integration with existing vLLM features.

ssm_output = None
for i in range(self.start_layer, self.end_layer):
layer = self.layers[i]
if i == self.config.num_hidden_layers // 2 + 2:
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 condition if i == self.config.num_hidden_layers // 2 + 2: is used to conditionally execute code during a profile run. Ensure that this condition accurately identifies the intended layer for profiling and that the profiling logic does not inadvertently affect the behavior of other layers or the overall model execution.

Comment on lines 163 to 165
# if not envs.VLLM_USE_V1:
# raise NotImplementedError(
# "Cross-layer KV sharing is not supported in V0.")
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 NotImplementedError for cross-layer KV sharing in V0 has been commented out. Confirm that cross-layer KV sharing is indeed correctly handled or disabled elsewhere, and that this change does not inadvertently enable unsupported functionality.

@congcongchen123 congcongchen123 force-pushed the congcongchen/phi4-mini-shadow branch from 479eb8a to 0e20e17 Compare July 9, 2025 23:38
@heheda12345 heheda12345 added the new-model Requests to new models label Jul 10, 2025
@congcongchen123 congcongchen123 requested a review from hmellor as a code owner July 10, 2025 08:31
@mergify mergify bot added the documentation Improvements or additions to documentation label Jul 10, 2025
Copy link
Collaborator

@heheda12345 heheda12345 left a comment

Choose a reason for hiding this comment

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

I like the current kv sharing implementation. It is quite clean. But not sure whether cross_layer_shared_block_tables is necessary.

Please add the required test as mentioned here https://docs.vllm.ai/en/latest/contributing/model/tests.html#required-tests

@luccafong
Copy link
Collaborator

luccafong commented Jul 11, 2025

Is this runnable in V1?
@sarckk can we try with V1 on this model with your PR #19719 and see if any gains?

@heheda12345
Copy link
Collaborator

can we try with V1 on this model with your PR #19719 and see if any gains?

You can use gemma-3n to test #19719 . This PR is not runable with v1 now and I think it needs some effort to enable v1.

@mergify mergify bot added the rocm Related to AMD ROCm label Jul 11, 2025
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
@congcongchen123 congcongchen123 force-pushed the congcongchen/phi4-mini-shadow branch from 4e92996 to 1ff7eeb Compare July 11, 2025 20:43
@tlrmchlsmth
Copy link
Collaborator

LGTM, congrats on the model launch -- @heheda12345 is it good to land from your end?

@tlrmchlsmth tlrmchlsmth added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 11, 2025
Copy link
Collaborator

@heheda12345 heheda12345 left a comment

Choose a reason for hiding this comment

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

Only some small comments.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Where is the check for YOCO and chunk prefill now?

Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Copy link
Collaborator

@heheda12345 heheda12345 left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for your effort. Looking forward to v1 integration.

@heheda12345 heheda12345 enabled auto-merge (squash) July 12, 2025 07:13
@congcongchen123
Copy link
Contributor Author

Took a look at the CI failures, they are unrelated to this PR, is it possible to merge this PR without rerunning the CIs?

@vllm-bot vllm-bot merged commit 2c11a73 into vllm-project:main Jul 12, 2025
97 of 99 checks passed
Chen-zexi pushed a commit to Chen-zexi/vllm that referenced this pull request Jul 13, 2025
@DarkLight1337 DarkLight1337 added this to the v0.10.0 milestone Jul 14, 2025
patrickvonplaten pushed a commit to patrickvonplaten/vllm that referenced this pull request Jul 15, 2025
…llm-project#20702)

Signed-off-by: Congcong Chen <congcongchen@microsoft.com>
Signed-off-by: Patrick von Platen <patrick.v.platen@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation new-model Requests to new models ready ONLY add when PR is ready to merge/full CI is needed rocm Related to AMD ROCm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants