morpheushoc commited on
Commit
4cd9b05
·
verified ·
1 Parent(s): 52699fd

Upload InternVideo2_cls

Browse files
config.json CHANGED
@@ -1,12 +1,11 @@
1
  {
2
- "_attn_implementation_autoset": true,
3
  "_name_or_path": "OpenGVLab/InternVideo2-Chat-8B",
4
  "architectures": [
5
- "InternVideo2_VideoChat2"
6
  ],
7
  "auto_map": {
8
  "AutoConfig": "model_config.VideoChat2Config",
9
- "AutoModel": "OpenGVLab/InternVideo2-Chat-8B--modeling_videochat2.InternVideo2_VideoChat2"
10
  },
11
  "model_cls": "InternVideo2_VideoChat2",
12
  "model_config": null,
 
1
  {
 
2
  "_name_or_path": "OpenGVLab/InternVideo2-Chat-8B",
3
  "architectures": [
4
+ "InternVideo2_cls"
5
  ],
6
  "auto_map": {
7
  "AutoConfig": "model_config.VideoChat2Config",
8
+ "AutoModel": "modeling_videochat2_cls.InternVideo2_cls"
9
  },
10
  "model_cls": "InternVideo2_VideoChat2",
11
  "model_config": null,
model-00001-of-00007.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bd09aaa0f9bf939da238f5a59884e2724a476d8fbf73e79c09a3c89e5391fac7
3
+ size 4897891808
model-00002-of-00007.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3178ad9789022615e0d8b3038d7c2a48fd7c2dc690dcbe5cef21811ec9af67c5
3
+ size 4962470256
model-00003-of-00007.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e9f3cd59785982299fb0a8d05a3924c19250d8f036a41e83367cd5b9aeee1802
3
+ size 4928226584
model-00004-of-00007.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:96507511a2b07c3729e23c5f79ab81ca57bec18760594dc456400cb0947929ed
3
+ size 4794042872
model-00005-of-00007.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a731dcdca4fc2474027a29dd05470f052d3f68c603317b7b484024d9c50982cd
3
+ size 4860593096
model-00006-of-00007.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc88627c93989ed60f3d38246dbd19431543664b2c5ec6792db35e7b5fc6fb42
3
+ size 4794042872
model-00007-of-00007.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cdf0c1532cd2fb0d005b2b4829ada1ce05ee074d2e0905f8563a3b4146d70c3d
3
+ size 4109221232
model.safetensors.index.json ADDED
The diff for this file is too large to render. See raw diff
 
modeling_videochat2_cls.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from modeling_videochat2 import *
3
+ from modeling_base import freeze_module
4
+ from transformers import AutoConfig
5
+ token = os.environ['HF_TOKEN']
6
+
7
+
8
+ class InternVideo2_cls(InternVideo2_VideoChat2):
9
+ def __init__(self, config):
10
+ super(InternVideo2_VideoChat2, self).__init__(config=config)
11
+
12
+ def build_llm(self):
13
+ self.lm_name = self.model_config.llm.name
14
+ if self.model_config.llm.name == 'mistral_7b':
15
+ from transformers import AutoModelForSequenceClassification
16
+ config = AutoConfig.from_pretrained(
17
+ self.model_config.llm.pretrained_llm_path,
18
+ torch_dtype=torch.bfloat16,
19
+ token=token,
20
+ # attn_implementation="flash_attention_2",
21
+ )
22
+ self.lm = AutoModelForSequenceClassification.from_config(config)
23
+ elif self.model_config.llm.name == 'internlm_20b':
24
+ from transformers import AutoModelForSequenceClassification
25
+ self.lm = AutoModelForSequenceClassification.from_pretrained(
26
+ self.model_config.llm.pretrained_llm_path,
27
+ torch_dtype=torch.bfloat16,
28
+ trust_remote_code=True,
29
+ )
30
+ self.lm.gradient_checkpointing = True
31
+ self.lm._set_gradient_checkpointing()
32
+ elif self.model_config.llm.name == 'internlm2_5_7b':
33
+ from transformers import AutoModelForSequenceClassification
34
+ self.lm = AutoModelForSequenceClassification.from_pretrained(
35
+ self.model_config.llm.pretrained_llm_path,
36
+ torch_dtype=torch.bfloat16,
37
+ trust_remote_code=True,
38
+ local_files_only=True,
39
+ )
40
+ else:
41
+ raise NotImplementedError(self.model_config.llm.name)
42
+
43
+ self.freeze_llm = self.model_config.get("freeze_llm", True)
44
+ logger.info(f'freeze_llm: {self.freeze_llm}')
45
+ if self.freeze_llm:
46
+ logger.info("freeze llm")
47
+ freeze_module(self.lm)
48
+
49
+ if self.model_config.llm.use_lora:
50
+ self.use_lora = True
51
+ from peft import get_peft_model, LoraConfig, TaskType
52
+ logger.info("Use lora")
53
+ if self.model_config.llm.name == 'internlm_20b':
54
+ peft_config = LoraConfig(
55
+ task_type=TaskType.CAUSAL_LM, inference_mode=False,
56
+ r=self.model_config.llm.lora_r, lora_alpha=self.model_config.llm.lora_alpha, lora_dropout=self.model_config.llm.lora_dropout,
57
+ target_modules=['wqkv', 'wo', 'w1', 'w2', 'w3', 'output']
58
+ )
59
+ else:
60
+ peft_config = LoraConfig(
61
+ task_type=TaskType.CAUSAL_LM, inference_mode=False,
62
+ r=self.model_config.llm.lora_r, lora_alpha=self.model_config.llm.lora_alpha, lora_dropout=self.model_config.llm.lora_dropout,
63
+ target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
64
+ "gate_proj", "up_proj", "down_proj", "lm_head"]
65
+ )
66
+
67
+ self.lm = get_peft_model(self.lm, peft_config)
68
+ self.lm.enable_input_require_grads()
69
+ self.lm.print_trainable_parameters()
70
+ else:
71
+ self.use_lora = False
72
+
73
+
74
+ def build_conversation(self,instruction, user_prompt,media_type='video',msg=''):
75
+
76
+ conversation = ""
77
+ if instruction:
78
+ conversation += instruction
79
+ conversation += ("[INST]" + " ")
80
+
81
+ if media_type == 'image':
82
+ conversation +=( "<Image>" + IMG_TOKEN + "</Image>")#*ilen
83
+ else:
84
+ conversation += ("<Video>" + VID_TOKEN + "</Video>")#*ilen
85
+
86
+ conversation += (msg.rstrip() + "[/INST]")
87
+ conversation += (" [INST] " + user_prompt + " [/INST]")
88
+ conversation += ("")
89
+ return conversation
90
+
91
+
92
+ if __name__ == "__main__":
93
+
94
+ tokenizer = AutoTokenizer.from_pretrained('OpenGVLab/InternVideo2-Chat-8B',trust_remote_code=True,use_fast=False)
95
+ config = AutoConfig.from_pretrained('OpenGVLab/InternVideo2-Chat-8B', torch_dtype=torch.bfloat16,trust_remote_code=True)
96
+ model = InternVideo2_Classification(config).cuda()
97
+
98
+ B, T, C, H, W = 1, 8, 3, 224, 224
99
+ video_tensor = torch.randn(B,T,C,H,W).cuda()
100
+ user_prompt = "this is a user prompt"
101
+ instruction = "this is an instruction"
102
+
103
+ conversation = model.build_conversation(instruction=instruction, user_prompt=user_prompt, media_type='video')
104
+ tokenized = model.build_input_ids(tokenizer,conversation,max_length=248,add_special_tokens=True,truncation=False,padding=False,return_tensors='pt')
105
+
106
+ input_ids = tokenized['input_ids'].unsqueeze(0).to(model.device)
107
+ attn_mask = tokenized['attention_mask'].unsqueeze(0).to(model.device)
108
+ indexes = tokenized['index'].unsqueeze(0)
109
+ text_embeds = model.pad_text_embeds(input_ids = input_ids,video = video_tensor,video_idx = indexes)
110
+ outputs = model.lm(inputs_embeds=text_embeds, attention_mask=attn_mask,output_hidden_states=True,return_dict=True)