-
Notifications
You must be signed in to change notification settings - Fork 22
[model] Add MiniCPM-V 4.6 model support #137
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 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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| # Copyright (c) ModelScope Contributors. All rights reserved. | ||
| from . import gemma4, glm, internvl, kimi_vl, llama4, llava, qwen, qwen3_5, qwen3_5_gdn, qwen3_asr, qwen3_omni, qwen3_vl | ||
| from . import gemma4, glm, internvl, kimi_vl, llama4, llava, minicpmv4_6, qwen, qwen3_5, qwen3_5_gdn, qwen3_asr, qwen3_omni, qwen3_vl |
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,91 @@ | ||||||||||
| # Copyright (c) ModelScope Contributors. All rights reserved. | ||||||||||
| import torch | ||||||||||
| from transformers import PretrainedConfig | ||||||||||
|
|
||||||||||
| from ..constant import ModelType | ||||||||||
| from ..gpts.qwen3_next_gdn import Qwen3NextGDNBridgeMixin, Qwen3NextLoader | ||||||||||
| from ..register import ModelMeta, register_model | ||||||||||
| from .utils import HuggingFaceVit | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class MiniCPMV46Vit(HuggingFaceVit): | ||||||||||
| module_mapping = { | ||||||||||
| 'model.vision_tower': 'vision_tower', | ||||||||||
| 'model.merger': 'merger', | ||||||||||
| } | ||||||||||
| _vision_tower = ['vision_tower'] | ||||||||||
| _aligner = ['merger'] | ||||||||||
|
|
||||||||||
| def prepare_model(self, hf_config: PretrainedConfig): | ||||||||||
| from transformers.models.minicpmv4_6.modeling_minicpmv4_6 import ( | ||||||||||
| MiniCPMV4_6VisionModel, | ||||||||||
| MiniCPMV4_6Merger, | ||||||||||
| MiniCPMV4_6Model | ||||||||||
| ) | ||||||||||
| self.vision_tower = MiniCPMV4_6VisionModel._from_config(hf_config.vision_config) | ||||||||||
| self.merger = MiniCPMV4_6Merger(hf_config).to(dtype=self.vision_tower.dtype) | ||||||||||
| self.model_cls = MiniCPMV4_6Model | ||||||||||
|
|
||||||||||
| def get_inputs_embeds(self, inputs_embeds, **kwargs): | ||||||||||
| input_ids = kwargs.get('input_ids') | ||||||||||
| pixel_values = kwargs.get('pixel_values') | ||||||||||
| pixel_values_videos = kwargs.get('pixel_values_videos') | ||||||||||
| target_sizes = kwargs.get('target_sizes') | ||||||||||
| target_sizes_videos = kwargs.get('target_sizes_videos') | ||||||||||
| hf_config = self.hf_config | ||||||||||
|
|
||||||||||
| if pixel_values is None and pixel_values_videos is None: | ||||||||||
| patch_size = hf_config.vision_config.patch_size | ||||||||||
| dummy_pv = torch.zeros( | ||||||||||
| 1, 3, 4 * patch_size, 4 * patch_size, | ||||||||||
| device=inputs_embeds.device, dtype=self.vision_tower.dtype) | ||||||||||
| dummy_ts = torch.tensor( | ||||||||||
| [[4, 4]], device=inputs_embeds.device, dtype=torch.int32) | ||||||||||
| with self.patch_hf_config(): | ||||||||||
| vision_output = self.model_cls.get_image_features(self, dummy_pv, dummy_ts) | ||||||||||
| image_embeds = torch.cat(vision_output.pooler_output, dim=0) | ||||||||||
| inputs_embeds = inputs_embeds + image_embeds.mean() * 0. | ||||||||||
| else: | ||||||||||
| if pixel_values is not None: | ||||||||||
| num_beams = pixel_values.shape[0] | ||||||||||
| with self.patch_hf_config(): | ||||||||||
| vision_output = self.model_cls.get_image_features( | ||||||||||
| self, pixel_values[:1].to(dtype=self.vision_tower.dtype), target_sizes) | ||||||||||
| image_features = ( | ||||||||||
| torch.cat(vision_output.pooler_output, dim=0) | ||||||||||
| .to(device=inputs_embeds.device, dtype=inputs_embeds.dtype) | ||||||||||
| .repeat(num_beams, 1)) | ||||||||||
| mask = self.model_cls.get_placeholder_mask( | ||||||||||
| self, input_ids, inputs_embeds, image_features, hf_config.image_token_id) | ||||||||||
| inputs_embeds = inputs_embeds.masked_scatter(mask, image_features) | ||||||||||
|
|
||||||||||
| if pixel_values_videos is not None: | ||||||||||
| num_beams = pixel_values_videos.shape[0] | ||||||||||
| with self.patch_hf_config(): | ||||||||||
| vision_output = self.model_cls.get_video_features( | ||||||||||
| self, pixel_values_videos[:1].to(dtype=self.vision_tower.dtype), target_sizes_videos) | ||||||||||
|
Comment on lines
+65
to
+66
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. 与上述图片处理逻辑类似,当
Suggested change
|
||||||||||
| video_features = ( | ||||||||||
| torch.cat(vision_output.pooler_output, dim=0) | ||||||||||
| .to(device=inputs_embeds.device, dtype=inputs_embeds.dtype) | ||||||||||
| .repeat(num_beams, 1)) | ||||||||||
| mask = self.model_cls.get_placeholder_mask( | ||||||||||
| self, input_ids, inputs_embeds, video_features, hf_config.video_token_id) | ||||||||||
| inputs_embeds = inputs_embeds.masked_scatter(mask, video_features) | ||||||||||
|
|
||||||||||
| return inputs_embeds | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class MiniCPMV46Bridge(Qwen3NextGDNBridgeMixin): | ||||||||||
| hf_layers_prefix = 'model.language_model.layers' | ||||||||||
| hf_embed_key = 'model.language_model.embed_tokens.weight' | ||||||||||
| hf_final_layernorm_key = 'model.language_model.norm.weight' | ||||||||||
|
|
||||||||||
|
|
||||||||||
| register_model( | ||||||||||
| ModelMeta( | ||||||||||
| ModelType.minicpmv4_6, | ||||||||||
| ['minicpmv4_6'], | ||||||||||
| bridge_cls=MiniCPMV46Bridge, | ||||||||||
| visual_cls=MiniCPMV46Vit, | ||||||||||
| loader=Qwen3NextLoader, | ||||||||||
| )) | ||||||||||
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.
当
num_beams > 1(例如在使用 Beam Search 或 Batch Size > 1 进行推理/生成)时,pixel_values被切片为pixel_values[:1](即 Batch Size 为 1),但target_sizes却没有进行相应的切片。这会导致get_image_features内部因为输入维度不匹配而报错或产生错误结果。建议对target_sizes也进行[:1]切片,以保持与pixel_values[:1]的维度一致。