-
Notifications
You must be signed in to change notification settings - Fork 282
support interns1 #1060
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
xhx1022
wants to merge
1
commit into
ModelTC:main
Choose a base branch
from
xhx1022:intern-s1
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.
Open
support interns1 #1060
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
Large diffs are not rendered by default.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
lightllm/models/interns1/layer_weights/pre_and_post_layer_weight.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,28 @@ | ||
| import torch | ||
| import numpy as np | ||
| from lightllm.models.llama.layer_weights.pre_and_post_layer_weight import LlamaPreAndPostLayerWeight | ||
|
|
||
|
|
||
|
|
||
| # add key: model.language_model.xxx -> model.xxx | ||
| # only change keys at PreAndPostLayerWeight load, TransformLayerWeight is correct now | ||
| def rename_weight_keys(weights): | ||
| prefix = "model.language_model." | ||
| keys = list(weights.keys()) | ||
| for k in keys: | ||
| if prefix in k: | ||
| weights["model." + k[len(prefix) :]] = weights[k] | ||
|
|
||
|
|
||
| class InternS1PreAndPostLayerWeight(LlamaPreAndPostLayerWeight): | ||
| def __init__(self, data_type, network_config, mode): | ||
| super().__init__(data_type, network_config, mode) | ||
| return | ||
|
|
||
| def load_hf_weights(self, weights): | ||
| rename_weight_keys(weights) | ||
| super().load_hf_weights(weights) | ||
| return | ||
|
|
||
|
|
||
|
|
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,127 @@ | ||
| import os | ||
| import json | ||
| from lightllm.models.registry import ModelRegistry, llm_model_type_is | ||
| from lightllm.common.basemodel.multimodal_tokenizer import BaseMultiModalTokenizer | ||
| from lightllm.common.build_utils import repair_config | ||
| from lightllm.server.core.objs import SamplingParams | ||
| from lightllm.server.multimodal_params import AudioItem, MultimodalParams, ImageItem | ||
| from lightllm.models.qwen3_moe.model import Qwen3MOEModel | ||
| from lightllm.models.qwen_vl.layer_infer.pre_layer_infer import LlamaMultimodalPreLayerInfer | ||
| from lightllm.models.interns1.layer_weights.pre_and_post_layer_weight import ( | ||
| InternS1PreAndPostLayerWeight, | ||
| ) | ||
|
|
||
|
|
||
| IMG_START_TOKEN = "<img>" | ||
| IMG_END_TOKEN = "</img>" | ||
| IMG_TOKEN = "<IMG_CONTEXT>" | ||
|
|
||
|
|
||
|
|
||
| # Warp of the origal tokenizer | ||
| class InternS1Tokenizer(BaseMultiModalTokenizer): | ||
| def __init__(self, tokenizer, model_cfg, **kwargs): | ||
| super().__init__(tokenizer) | ||
| self.llm_model_type = model_cfg.get("text_config").get("model_type") | ||
| self.image_length = int(os.environ.get("INTERNVL_IMAGE_LENGTH", 256)) | ||
|
|
||
| self.image_start_tag = IMG_START_TOKEN | ||
| self.image_start_id = tokenizer.convert_tokens_to_ids(self.image_start_tag) | ||
|
|
||
| self.image_end_tag = IMG_END_TOKEN | ||
| self.image_end_id = tokenizer.convert_tokens_to_ids(self.image_end_tag) | ||
|
|
||
|
|
||
| def init_imageitem_extral_params( | ||
| self, img: ImageItem, multi_params: MultimodalParams, sampling_params: SamplingParams | ||
| ): | ||
| img.extra_params["image_patch_max_num"] = 12 # 好丑的写法,后面改动 | ||
| return | ||
|
|
||
| def init_audioitem_extral_params( | ||
| self, audio: AudioItem, multi_params: MultimodalParams, sampling_params: SamplingParams | ||
| ): | ||
| return | ||
|
|
||
| def get_image_token_length(self, img: ImageItem): | ||
| return self.image_length | ||
|
|
||
| def get_audio_token_length(self, audio: AudioItem): | ||
| return | ||
|
|
||
| # only change the impl of the encode func: | ||
| def encode(self, prompt, multimodal_params: MultimodalParams = None, **kwargs): | ||
| # TEXT<IMG_CONTEXT>TEXT<IMG_CONTEXT>TEXT --> TEXT<img></img>TEXT<img></img>TEXT | ||
| image_tokens = IMG_START_TOKEN + IMG_END_TOKEN | ||
| if multimodal_params is None: | ||
| add_special_tokens = kwargs.get("add_special_tokens", True) | ||
| return self.tokenizer.encode(prompt, add_special_tokens=add_special_tokens) | ||
| image_count = len(multimodal_params.images) | ||
| prompt = prompt.replace(IMG_TOKEN, image_tokens, image_count) | ||
| origin_ids = self.tokenizer.encode(prompt, add_special_tokens=kwargs["add_special_tokens"]) | ||
|
|
||
| # print("[debug] prompt: ", prompt) | ||
| # print("[debug] origin_ids: ", origin_ids) | ||
| # import copy | ||
| # origin_ids_ = copy.deepcopy(origin_ids) | ||
|
|
||
|
Comment on lines
+63
to
+67
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. |
||
| # <img></img> --> <img>id,id+1...id+num</img> | ||
| input_ids = [] | ||
| image_id = 0 | ||
| start_idx = 0 | ||
| while True: | ||
| try: | ||
| start_idx = origin_ids.index(self.image_start_id, start_idx) | ||
| if start_idx + 1 >= len(origin_ids): | ||
| break | ||
| if origin_ids[start_idx + 1] == self.image_end_id: | ||
| input_ids.extend(origin_ids[: start_idx + 1]) | ||
| token_id = multimodal_params.images[image_id].token_id | ||
| token_num = multimodal_params.images[image_id].token_num | ||
| input_ids.extend(range(token_id, token_id + token_num)) | ||
| input_ids.append(self.image_end_id) | ||
| origin_ids = origin_ids[start_idx + 2 :] | ||
| start_idx = 0 | ||
| image_id += 1 | ||
| else: | ||
| raise ValueError("image token error") | ||
| except ValueError: | ||
| break | ||
| input_ids.extend(origin_ids[start_idx:]) | ||
|
|
||
| # print("[debug] input_ids: ", input_ids) | ||
| # data = { | ||
| # "origin_ids": origin_ids_, | ||
| # "input_ids": input_ids | ||
| # } | ||
| # with open("input_ids_lightllm.json", "w") as f: | ||
| # json.dump(data, f) | ||
|
Comment on lines
+92
to
+98
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. |
||
|
|
||
| return input_ids | ||
|
|
||
|
|
||
|
|
||
| @ModelRegistry(["interns1"], is_multimodal=True, condition=llm_model_type_is("qwen3_moe")) | ||
| class InternS1Qwen3MOETpPartModel(Qwen3MOEModel): | ||
| # weight class | ||
| pre_and_post_weight_class = InternS1PreAndPostLayerWeight | ||
|
|
||
| # infer class | ||
| pre_layer_infer_class = LlamaMultimodalPreLayerInfer | ||
|
|
||
| def __init__(self, kvargs): | ||
| super().__init__(kvargs) | ||
| return | ||
|
|
||
| def _init_config(self): | ||
| with open(os.path.join(self.weight_dir_, "config.json"), "r") as json_file: | ||
| self.config = json.load(json_file)["text_config"] | ||
| # rename keys | ||
| repair_config(self.config, same_names=["num_attention_heads", "n_head"]) | ||
| repair_config(self.config, same_names=["hidden_size", "n_embd", "n_embed"]) | ||
| repair_config(self.config, same_names=["num_hidden_layers", "n_layer"]) | ||
| if self.finetune_config: | ||
| self.config["vocab_size"] = self.finetune_config.vocab_size | ||
| return | ||
|
|
||
|
|
||
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
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.
The value
12forimage_patch_max_numis hardcoded. The comment# 好丑的写法,后面改动(ugly way of writing, change later) indicates this is a temporary solution. This value should be made configurable, for example by reading it from the model configuration in the__init__method and storing it as an instance attribute.