-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
[Model] Add support for 'gte-Qwen2' embedding models #6282
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
Closed
Closed
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
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,58 @@ | ||
| from typing import Iterable, List, Optional, Tuple | ||
|
|
||
| import torch | ||
| from torch import nn | ||
| from transformers import Qwen2Config | ||
|
|
||
| from vllm.attention import AttentionMetadata | ||
| from vllm.config import CacheConfig, LoRAConfig | ||
| from vllm.model_executor.layers.pooler import Pooler, PoolingType | ||
| from vllm.model_executor.layers.quantization.base_config import ( | ||
| QuantizationConfig) | ||
| from vllm.model_executor.models.qwen2 import Qwen2ForCausalLM | ||
| from vllm.model_executor.pooling_metadata import PoolingMetadata | ||
| from vllm.sequence import IntermediateTensors, PoolerOutput | ||
|
|
||
|
|
||
| class Qwen2EmbeddingModel(nn.Module): | ||
| """A model that uses Qwen2 with additional embedding functionalities. | ||
| This class encapsulates the Qwen2ForCausalLM and provides an interface for | ||
| embedding operations and customized pooling functions. | ||
| Attributes: | ||
| model: An instance of Qwen2ForCausalLM used for forward operations. | ||
| _pooler: An instance of Pooler used for pooling operations. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| config: Qwen2Config, | ||
| cache_config: Optional[CacheConfig] = None, | ||
| quant_config: Optional[QuantizationConfig] = None, | ||
| lora_config: Optional[LoRAConfig] = None, | ||
| ) -> None: | ||
| super().__init__() | ||
| self.model = Qwen2ForCausalLM(config, cache_config, quant_config, | ||
| lora_config) | ||
|
|
||
| self._pooler = Pooler(pooling_type=PoolingType.LAST, normalize=True) | ||
|
|
||
| def forward( | ||
| self, | ||
| input_ids: torch.Tensor, | ||
| positions: torch.Tensor, | ||
| kv_caches: List[torch.Tensor], | ||
| attn_metadata: AttentionMetadata, | ||
| intermediate_tensors: Optional[IntermediateTensors] = None, | ||
| ) -> torch.Tensor: | ||
| return self.model(input_ids, positions, kv_caches, attn_metadata, | ||
| intermediate_tensors) | ||
|
|
||
| def pooler( | ||
| self, | ||
| hidden_states: torch.Tensor, | ||
| pooling_metadata: PoolingMetadata, | ||
| ) -> Optional[PoolerOutput]: | ||
| return self._pooler(hidden_states, pooling_metadata) | ||
|
|
||
| def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]): | ||
| self.model.load_weights(weights) |
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.
This hardcoded case based on the model id/path used is not acceptable. For instance, this wouldn't work in the case where a user has downloaded the model locally and passed in a path like
--model ~/my-model/Uh oh!
There was an error while loading. Please reload this page.
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.
The gte-Qwen2 embedding model's architecture is "Qwen2ForCausalLM", which is the same as Qwen2 LLMs. Is there any better solution to eliminate this ambiguity?
Perhaps we can add an option in argparser to specify whether it is an embedding model, rather than searching through the model architecture.
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.
How about working with the upstream to change or add an extra "Qwen2EmbeddingModel" in the "architectures" list?
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.
#9424 should be able to solve this.