-
Notifications
You must be signed in to change notification settings - Fork 77
Fix compile error for Gemma3 multimodal inputs #671
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
iboiko-habana
merged 3 commits into
vllm-project:main
from
jiminha:jha/gemma3_textembedding
Dec 3, 2025
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from collections.abc import Callable | ||
| import torch | ||
| from torch import Tensor | ||
| from vllm.model_executor.models.interfaces import SupportsMultiModal | ||
|
|
||
|
|
||
| def _embed_text_input_ids( | ||
| self, | ||
| input_ids: Tensor, | ||
| embed_input_ids: Callable[[Tensor], Tensor], | ||
| *, | ||
| is_multimodal: Tensor | None, | ||
| handle_oov_mm_token: bool, | ||
| ) -> Tensor: | ||
| if handle_oov_mm_token and is_multimodal is not None: | ||
| is_text = ~is_multimodal | ||
|
|
||
| # Original implementation uses dynamic indexing. | ||
| # Replacing it to use fixed shape for HPU and then fill in text position. | ||
| ''' | ||
| text_embeds = embed_input_ids(input_ids[is_text]) | ||
|
|
||
| return torch.empty( | ||
| (input_ids.shape[0], text_embeds.shape[1]), | ||
| dtype=text_embeds.dtype, | ||
| device=text_embeds.device, | ||
| ).masked_scatter_(is_text.unsqueeze_(-1), text_embeds) | ||
| ''' | ||
| all_text_embeds = embed_input_ids(input_ids) | ||
| result = torch.zeros_like(all_text_embeds) | ||
|
|
||
| return torch.where( | ||
| is_text.unsqueeze(-1), # [batch, seq_len, 1] | ||
| all_text_embeds, # [batch, seq_len, embed_dim] | ||
| result # [batch, seq_len, embed_dim] | ||
| ) | ||
|
|
||
| return embed_input_ids(input_ids) | ||
|
|
||
|
|
||
| SupportsMultiModal._embed_text_input_ids = _embed_text_input_ids |
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,35 @@ | ||
| import torch | ||
| import torch.nn.functional as F | ||
| from vllm.model_executor.layers.conv import Conv2dLayer | ||
|
|
||
|
|
||
| @Conv2dLayer.register_oot | ||
| class HPUConv2dLayer(Conv2dLayer): | ||
|
|
||
| def _forward_mulmat(self, x: torch.Tensor) -> torch.Tensor: | ||
| assert x.dim() == 4 | ||
| B, C, H, W = x.shape | ||
| K1, K2 = self.kernel_size | ||
jiminha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| H, W = H // K1, W // K2 | ||
|
|
||
| # TODO: HPU doesn't support unfold, implement with view,reshape. | ||
| #x = x.unfold(2, K1, K1).unfold(3, K2, K2) | ||
| #x = x.permute(0, 2, 3, 1, 4, 5).reshape(-1, self.input_size) | ||
| x = x.view(B, C, H, K1, W, K2) | ||
| x = x.permute(0, 2, 4, 1, 3, 5).reshape(-1, self.input_size) # [B*H*W, C*K1*K2] | ||
|
|
||
| x = F.linear( | ||
| x, | ||
| self.weight.view(self.out_channels, self.input_size), | ||
| self.bias, | ||
| ) | ||
| x = x.view(B, H, W, self.out_channels).permute(0, 3, 1, 2) | ||
| return x | ||
|
|
||
| def forward_oot(self, x: torch.Tensor) -> torch.Tensor: | ||
| """Expected input shape: (batch_size, in_channels, height, width)""" | ||
| assert x.dim() == 4 | ||
| if self.enable_linear: | ||
| return self._forward_mulmat(x) | ||
| else: | ||
| return self._forward_conv(x) | ||
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.
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.
slightly clearer version