-
Notifications
You must be signed in to change notification settings - Fork 91
feat: add mm embedding model and its factory. #471
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
Open
dongxianzhe
wants to merge
3
commits into
jd-opensource:main
Choose a base branch
from
dongxianzhe:feat/mm_embed_model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* Copyright 2025 The xLLM Authors. All Rights Reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| https://github.com/jd-opensource/xllm/blob/main/LICENSE | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <c10/core/Device.h> | ||
| #include <torch/torch.h> | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "causal_vlm.h" | ||
| #include "core/framework/kv_cache/kv_cache.h" | ||
| #include "core/framework/quant_args.h" | ||
| #include "core/framework/state_dict/state_dict.h" | ||
| #include "model_args.h" | ||
| #include "model_input_params.h" | ||
|
|
||
| namespace xllm { | ||
|
|
||
| class MMEmbeddingVLM : public CausalVLM { | ||
| public: | ||
| ~MMEmbeddingVLM() override = default; | ||
|
|
||
| virtual std::vector<torch::Tensor> encode( | ||
| const ModelInputParams& input_params) = 0; | ||
|
|
||
| virtual torch::Tensor logits(const torch::Tensor& hidden_states, | ||
| const torch::Tensor& selected_idxes) { | ||
| return torch::Tensor(); | ||
| } | ||
|
|
||
| virtual torch::Tensor forward(const torch::Tensor& tokens, | ||
| const torch::Tensor& positions, | ||
| std::vector<KVCache>& kv_caches, | ||
| const ModelInputParams& input_params) { | ||
| return torch::Tensor{}; | ||
| } | ||
| virtual void prepare_expert_weight(int32_t layer_id, | ||
| const std::vector<int32_t>& expert_ids) { | ||
| return; | ||
| } | ||
| virtual void update_expert_weight(int32_t layer_id) { return; } | ||
| virtual void set_lm_head(layer::LmHead& head) { return; } | ||
| virtual layer::LmHead get_lm_head() { return nullptr; } | ||
| virtual layer::WordEmbedding get_word_embedding() { return nullptr; } | ||
| virtual void set_word_embedding(layer::WordEmbedding& embedding) { return; } | ||
| }; | ||
|
|
||
| template <typename Model> | ||
| class MMEmbeddingVLMImpl : public MMEmbeddingVLM { | ||
| public: | ||
| MMEmbeddingVLMImpl(Model model, const torch::TensorOptions& options) | ||
| : model_(std::move(model)), options_(options) {} | ||
|
|
||
| virtual std::vector<torch::Tensor> encode( | ||
| const ModelInputParams& input_params) override { | ||
| return model_->encode(input_params); | ||
| }; | ||
|
|
||
| void load_model(std::unique_ptr<ModelLoader> loader) override { | ||
| model_->load_model(std::move(loader)); | ||
| } | ||
|
|
||
| torch::Device device() const override { return options_.device(); } | ||
|
|
||
| const torch::TensorOptions& options() const override { return options_; } | ||
|
|
||
| private: | ||
| Model model_; | ||
|
|
||
| torch::TensorOptions options_; | ||
| }; | ||
|
|
||
| } // namespace xllm | ||
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 |
|---|---|---|
|
|
@@ -115,6 +115,20 @@ void ModelRegistry::register_vlm_embedding_factory( | |
| } | ||
| } | ||
|
|
||
| void ModelRegistry::register_vlm_mm_embedding_factory( | ||
| const std::string& name, | ||
| MMEmbeddingVLMFactory factory) { | ||
| ModelRegistry* instance = get_instance(); | ||
|
|
||
| if (instance->model_registry_[name].mm_embedding_vlm_factory != nullptr) { | ||
| SAFE_LOG_WARNING("mm embedding vlm factory for " << name | ||
| << " already registered."); | ||
| } else { | ||
| instance->model_registry_[name].mm_embedding_vlm_factory = factory; | ||
| instance->model_backend_[name] = "vlm"; | ||
| } | ||
| } | ||
|
|
||
| void ModelRegistry::register_dit_model_factory(const std::string& name, | ||
| DiTModelFactory factory) { | ||
| ModelRegistry* instance = get_instance(); | ||
|
|
@@ -216,6 +230,13 @@ EmbeddingVLMFactory ModelRegistry::get_embeddingvlm_factory( | |
| return instance->model_registry_[name].embedding_vlm_factory; | ||
| } | ||
|
|
||
| MMEmbeddingVLMFactory ModelRegistry::get_mm_embedding_vlm_factory( | ||
| const std::string& name) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe need yo rename :( and |
||
| ModelRegistry* instance = get_instance(); | ||
|
|
||
| return instance->model_registry_[name].mm_embedding_vlm_factory; | ||
| } | ||
|
|
||
| DiTModelFactory ModelRegistry::get_dit_model_factory(const std::string& name) { | ||
| ModelRegistry* instance = get_instance(); | ||
| return instance->model_registry_[name].dit_model_factory; | ||
|
|
@@ -317,6 +338,21 @@ std::unique_ptr<EmbeddingVLM> create_vlm_embedding_model( | |
| return nullptr; | ||
| } | ||
|
|
||
| std::unique_ptr<MMEmbeddingVLM> create_vlm_mm_embedding_model( | ||
| const ModelContext& context) { | ||
| // get the factory function for the model type from model registry | ||
| auto factory = ModelRegistry::get_mm_embedding_vlm_factory( | ||
| context.get_model_args().model_type()); | ||
| if (factory) { | ||
| return factory(context); | ||
| } | ||
|
|
||
| LOG(ERROR) << "Unsupported model type: " | ||
| << context.get_model_args().model_type(); | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
| std::unique_ptr<DiTModel> create_dit_model(const DiTModelContext& context) { | ||
| // get the factory function for the model type from model registry | ||
| auto factory = ModelRegistry::get_dit_model_factory(context.model_type()); | ||
|
|
||
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.
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.
no need to define these interface in
MMEmbeddingVLM, move these toMMEmbeddingVLMImpl