-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Add CheXAgent model integration with tests and documentation #20886
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
base: main
Are you sure you want to change the base?
Conversation
👋 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 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 🚀 |
There was a problem hiding this 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 @WeiqiangLv, 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 significantly expands vLLM's capabilities by integrating the CheXagent multimodal model, a specialized tool for medical image analysis. The core of this work involved implementing CheXagent's unique QFormer architecture and ensuring its seamless operation within vLLM's existing multimodal framework. The addition includes robust testing and thorough documentation, making CheXagent readily available for users to perform tasks like chest X-ray analysis and medical report generation.
Highlights
- New Model Integration: Added comprehensive support for the CheXagent multimodal model, specifically designed for medical image analysis, addressing its previous incompatibility due to its QFormer architecture.
- QFormer Architecture Implementation: Implemented the full QFormer architecture within vLLM, including custom modules for multi-head attention, layers, and the overall QFormer model, enabling robust vision-language integration.
- Multimodal System Integration: Seamlessly integrated CheXagent with vLLM's multimodal system, including a dedicated processor for handling image inputs and inserting image tokens into the prompt.
- Extensive Testing and Documentation: Introduced a comprehensive Pytest suite and a simple validation script, alongside detailed user-facing and internal implementation documentation, ensuring the model's functionality and ease of use.
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
-
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. ↩
This pull request has merge conflicts that must be resolved before it can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for the CheXAgent model, a specialized medical image analysis model, into vLLM. The changes are comprehensive, including the model implementation, registration, extensive tests, and user documentation.
The implementation correctly follows the existing patterns for multimodal models in vLLM, particularly drawing from the BLIP-2 model which also uses a Q-Former architecture. The addition of both a simple validation script and a full pytest suite ensures good test coverage. The documentation is detailed, covering usage, architecture, and limitations.
I've identified a few minor issues, primarily related to documentation accuracy and a type hint in the model implementation. Once these are addressed, the PR should be in excellent shape for merging. Great work on this contribution!
### 2. Model Registration | ||
|
||
**Modified Files**: | ||
- `vllm/vllm/model_executor/models/registry.py` - Added to `_MULTIMODAL_MODELS` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a small typo in this file path; the vllm
directory is duplicated. Correcting this will improve clarity for anyone reading the documentation.
- `vllm/vllm/model_executor/models/registry.py` - Added to `_MULTIMODAL_MODELS` | |
- `vllm/model_executor/models/registry.py` - Added to `_MULTIMODAL_MODELS` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok thank you I will update it
@article{chexagent2024, | ||
title={CheXagent: Towards a Foundation Model for Chest X-Ray Interpretation}, | ||
author={...}, | ||
journal={...}, | ||
year={2024} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1) | ||
|
||
# Process through QFormer | ||
query_output = self.qformer( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,190 @@ | |||
# CheXagent Implementation for vLLM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the contents of this file to the PR description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK thank you! I will
@@ -0,0 +1,279 @@ | |||
# PR: Add CheXagent Model Support to vLLM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the contents of this file to the PR description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it
@@ -0,0 +1,127 @@ | |||
# CheXagent Model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should update our existing example scripts in https://github.com/vllm-project/vllm/tree/main/examples/offline_inference to keep everything in one place. There is no need to add model-specific documentation under docs/
directory apart from updating the Supported Models page/l https://docs.vllm.ai/en/latest/models/supported_models.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I will thank you !
@@ -315,6 +315,8 @@ def check_available_online( | |||
"Blip2ForConditionalGeneration": _HfExamplesInfo("Salesforce/blip2-opt-2.7b", # noqa: E501 | |||
extras={"6b": "Salesforce/blip2-opt-6.7b"}, # noqa: E501 | |||
v0_only=True), | |||
"CheXagentForConditionalGeneration": _HfExamplesInfo("StanfordAIMI/CheXagent-8b", # noqa: E501 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep this in alphabetical order
CheXagentImageInputs = Union[CheXagentImagePixelInputs, CheXagentImageEmbeddingInputs] | ||
|
||
|
||
class CheXagentQFormerMultiHeadAttention(nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pretty much looks identical to Blip2QFormerMultiHeadAttention
so we can just import that instead
return context_layer | ||
|
||
|
||
class CheXagentQFormerSelfOutput(nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical to Blip2QFormerSelfOutput
return hidden_states | ||
|
||
|
||
class CheXagentQFormerAttention(nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical to Blip2QFormerAttention
return hidden_states | ||
|
||
|
||
class CheXagentQFormerOutput(nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical to Blip2QFormerOutput
return attention_output | ||
|
||
|
||
class CheXagentQFormerIntermediate(nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical to Blip2QFormerIntermediate
return hidden_states | ||
|
||
|
||
class CheXagentQFormerLayer(nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as Blip2QFormerLayer
when config.cross_attention_frequency == 0`
return sequence_output | ||
|
||
|
||
class CheXagentProcessingInfo(BaseProcessingInfo): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can also import these classes from BLIP-2 file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this file is unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I will delete it !
from vllm.config import VllmConfig, ModelConfig | ||
|
||
model_config = ModelConfig( | ||
"StanfordAIMI/CheXagent-8b", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add corresponding model tests/models/multimodal/generation/test_common.py
directly to avoid such a complicated test flow.
self.language_projection = nn.Linear( | ||
config.qformer_config.hidden_size, | ||
config.text_config.hidden_size, | ||
bias=True, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use ReplicatedLinear
here.
if inputs_embeds is not None: | ||
return self.language_model( | ||
positions=positions, | ||
intermediate_tensors=intermediate_tensors, | ||
inputs_embeds=inputs_embeds, | ||
**kwargs, | ||
) | ||
|
||
return self.language_model( | ||
input_ids=input_ids, | ||
positions=positions, | ||
intermediate_tensors=intermediate_tensors, | ||
**kwargs, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this forwarding implementation can work with PP. You can refer to Bilp-2's forwarding implementation:
vllm/vllm/model_executor/models/blip2.py
Lines 699 to 715 in 66f6fbd
if intermediate_tensors is not None: | |
inputs_embeds = None | |
# NOTE: In v1, inputs_embeds is always generated at model runner, this | |
# condition is for v0 compatibility. | |
elif inputs_embeds is None: | |
vision_embeddings = self.get_multimodal_embeddings(**kwargs) | |
inputs_embeds = self.get_input_embeddings(input_ids, | |
vision_embeddings) | |
input_ids = None | |
hidden_states = self.language_model.model(input_ids, | |
positions, | |
intermediate_tensors, | |
inputs_embeds=inputs_embeds) | |
return hidden_states |
def get_dummy_mm_data( | ||
self, | ||
seq_len: int, | ||
mm_counts: Mapping[str, int], | ||
) -> MultiModalDataDict: | ||
return { | ||
"image": [torch.randn(3, 224, 224) for _ in range(mm_counts["image"])] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dummy images should be a list of PIL.Image
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I will double check
def get_dummy_text(self, mm_counts: Mapping[str, int]) -> str: | ||
return "<image> Describe this medical image." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def get_dummy_text(self, mm_counts: Mapping[str, int]) -> str: | |
return "<image> Describe this medical image." | |
def get_dummy_text(self, mm_counts: Mapping[str, int]) -> str: | |
num_images = mm_counts.get("image", 0) | |
return "<image>" * num_images |
We should consider num_images
for multi-image support.
tokenizer = self.info.get_tokenizer() | ||
vocab = tokenizer.get_vocab() | ||
|
||
image_token_id = vocab["<image>"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
image_token_id = vocab["<image>"] | |
image_token_id = _IMAGE_TOKEN_ID |
Can we use _IMAGE_TOKEN_ID
here?
|
||
self.layernorm = nn.LayerNorm(config.hidden_size, | ||
eps=config.layer_norm_eps) | ||
self.dropout = nn.Dropout(config.hidden_dropout_prob) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.dropout = nn.Dropout(config.hidden_dropout_prob) |
Redundant for model inference.
Essential Elements of an Effective PR Description Checklist
supported_models.md
andexamples
for a new model.Purpose
Test Plan
Test Result
(Optional) Documentation Update