-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Support Phi-4 Multi-Modal (text + vision only) #6494
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
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5dec8db
Squash all changes.
lifuhuang 1d23b4a
Restore files.
lifuhuang 531ef2a
Add TODO tags for follow-ups.
lifuhuang 8ad79da
Refactor Phi4MMForCausalLM to use composition instead of inheritance.
lifuhuang e97b35e
Fix typo.
lifuhuang ad24434
Merge branch 'main' into phi4mm
lifuhuang e57cefb
Address PR comments.
lifuhuang 3d3c997
Merge branch 'main' into phi4mm
lifuhuang fed84c6
Fix lint
lifuhuang 85a57cd
Merge branch 'main' into phi4mm
lifuhuang 06f3567
Merge branch 'main' into phi4mm
lifuhuang e16eb71
Merge branch 'main' into phi4mm
zhyncs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,5 +228,8 @@ compile_commands.json | |
|
|
||
| 1 | ||
|
|
||
| # Autoenv | ||
| .env.leave | ||
|
|
||
| # Rust lib | ||
| Cargo.lock | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
python/sglang/srt/managers/multimodal_processors/phi4mm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import logging | ||
| from typing import List, Union | ||
|
|
||
| from sglang.srt.managers.multimodal_processors.base_processor import ( | ||
| BaseMultimodalProcessor, | ||
| MultimodalSpecialTokens, | ||
| ) | ||
| from sglang.srt.managers.schedule_batch import Modality, MultimodalDataItem | ||
| from sglang.srt.models.phi4mmvllm import Phi4MMForCausalLM | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _IMAGE_SPECIAL_TOKEN = "<|endoftext10|>" | ||
| _IMAGE_SPECIAL_TOKEN_ID = 200010 | ||
|
|
||
|
|
||
| class Phi4MMImageProcessor(BaseMultimodalProcessor): | ||
| models = [Phi4MMForCausalLM] | ||
|
|
||
| def __init__(self, hf_config, server_args, _processor): | ||
| super().__init__(hf_config, server_args, _processor) | ||
| self.multimodal_tokens = MultimodalSpecialTokens( | ||
| image_token=_IMAGE_SPECIAL_TOKEN, | ||
| ) | ||
|
|
||
| async def process_mm_data_async( | ||
| self, | ||
| image_data: List[Union[str, bytes]], | ||
| input_text, | ||
| request_obj, | ||
| max_req_input_len, | ||
| **kwargs, | ||
| ): | ||
| audio_data = request_obj.audio_data | ||
|
|
||
| if not image_data and not audio_data: | ||
| return None | ||
|
|
||
| if not isinstance(image_data, list): | ||
| image_data = [image_data] | ||
|
|
||
| if not isinstance(audio_data, list): | ||
| audio_data = [audio_data] | ||
|
|
||
| if audio_data: | ||
| logger.warning( | ||
| "Currently SGLang does not support audio data for Phi4MM. We are working on it. You can file an issue to help us prioritize." | ||
| ) | ||
| audio_data = [] | ||
|
|
||
| base_output = self.load_mm_data( | ||
| prompt=input_text, | ||
| max_req_input_len=max_req_input_len, | ||
| audio_data=audio_data, | ||
| image_data=image_data, | ||
| multimodal_tokens=self.multimodal_tokens, | ||
| ) | ||
| if base_output is None: | ||
| return None | ||
|
|
||
| res = self.process_mm_data( | ||
| input_text=base_output.input_text, | ||
| images=base_output.images, | ||
| audios=base_output.audios, | ||
| ) | ||
|
|
||
| input_ids = res["input_ids"].flatten() | ||
| image_offsets = self.get_mm_items_offset( | ||
| input_ids=input_ids, | ||
| mm_token_id=_IMAGE_SPECIAL_TOKEN_ID, | ||
| ) | ||
|
|
||
| items = [ | ||
| MultimodalDataItem( | ||
| pixel_values=res["input_image_embeds"], | ||
| image_sizes=res["image_sizes"], | ||
| image_emb_mask=res["image_attention_mask"], | ||
| image_offsets=image_offsets, | ||
| modality=Modality.IMAGE, | ||
| ) | ||
| ] | ||
|
|
||
| return { | ||
| "mm_items": items, | ||
| "input_ids": input_ids.tolist(), | ||
| "im_token_id": _IMAGE_SPECIAL_TOKEN_ID, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.