-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathmodel.py
More file actions
177 lines (147 loc) · 7.02 KB
/
model.py
File metadata and controls
177 lines (147 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import json
from lightllm.models.internlm2.model import Internlm2TpPartModel
from lightllm.models.llama.model import LlamaTpPartModel
from lightllm.models.phi3.model import Phi3TpPartModel
from lightllm.models.qwen2.model import Qwen2TpPartModel
from lightllm.models.qwen_vl.layer_infer.pre_layer_infer import LlamaMultimodalPreLayerInfer
from lightllm.server.multimodal_params import MultimodalParams, ImageItem
from lightllm.common.build_utils import repair_config
from lightllm.models.internvl.layer_weights.pre_and_post_layer_weight import (
InternVLLlamaPreAndPostLayerWeight,
InternVLPhi3PreAndPostLayerWeight,
)
from lightllm.models.internvl.layer_weights.pre_and_post_layer_weight import InternVLInternlm2PreAndPostLayerWeight
from lightllm.models.llava.llava_visual import LlavaVisionModel
# from lightllm.models.internvl.img_process import get_image_patch
from lightllm.models.vit import get_image_patch_func
from typing import Dict
import lightllm.models.internvl.internvl_visual
import torch
import numpy
IMG_START_TOKEN = "<img>"
IMG_END_TOKEN = "</img>"
IMG_TOKEN = "<image>"
# Warp of the origal tokenizer
class InternvlTokenizer:
def __init__(self, tokenizer, model_cfg, **kwargs):
self.llm_model_type = model_cfg.get("llm_config").get("model_type")
self.tokenizer = tokenizer
self.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)
self.get_image_patch_func = get_image_patch_func(kwargs["weight_dir"])
def get_image_token_length(self, img: ImageItem):
return self.get_image_patch_func(img.image_w, img.image_h, use_thumbnail=True) * self.image_length
# only change the impl of the encode func:
def encode(self, prompt, multimodal_params: MultimodalParams = None, **kwargs):
# TEXT<image>TEXT<image>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"])
# <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:])
return input_ids
def __getattr__(self, name):
if name != "encode":
return getattr(self.tokenizer, name)
return self.encode
class InternVLPhi3TpPartModel(Phi3TpPartModel):
# weight class
pre_and_post_weight_class = InternVLPhi3PreAndPostLayerWeight
# 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)["llm_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
class InternVLInternlm2TpPartModel(Internlm2TpPartModel):
# weight class
pre_and_post_weight_class = InternVLInternlm2PreAndPostLayerWeight
# 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)["llm_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
class InternVLLlamaTpPartModel(LlamaTpPartModel):
# weight class
pre_and_post_weight_class = InternVLLlamaPreAndPostLayerWeight
# 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)["llm_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
class InternVLQwen2TpPartModel(Qwen2TpPartModel):
# weight class
pre_and_post_weight_class = InternVLLlamaPreAndPostLayerWeight
# 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)["llm_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