sathvika181204
commited on
Commit
•
050bd6b
1
Parent(s):
3c1cd56
Upload configuration_internvl_chat.py with huggingface_hub
Browse files
configuration_internvl_chat.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# --------------------------------------------------------
|
2 |
+
# InternVL
|
3 |
+
# Copyright (c) 2024 OpenGVLab
|
4 |
+
# Licensed under The MIT License [see LICENSE for details]
|
5 |
+
# --------------------------------------------------------
|
6 |
+
|
7 |
+
import copy
|
8 |
+
|
9 |
+
from transformers import AutoConfig, LlamaConfig, Qwen2Config
|
10 |
+
from transformers.configuration_utils import PretrainedConfig
|
11 |
+
from transformers.utils import logging
|
12 |
+
|
13 |
+
from .configuration_intern_vit import InternVisionConfig
|
14 |
+
|
15 |
+
logger = logging.get_logger(__name__)
|
16 |
+
|
17 |
+
|
18 |
+
class InternVLChatConfig(PretrainedConfig):
|
19 |
+
model_type = 'internvl_chat'
|
20 |
+
is_composition = True
|
21 |
+
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
vision_config=None,
|
25 |
+
llm_config=None,
|
26 |
+
use_backbone_lora=0,
|
27 |
+
use_llm_lora=0,
|
28 |
+
select_layer=-1,
|
29 |
+
force_image_size=None,
|
30 |
+
downsample_ratio=0.5,
|
31 |
+
template=None,
|
32 |
+
dynamic_image_size=False,
|
33 |
+
use_thumbnail=False,
|
34 |
+
ps_version='v1',
|
35 |
+
min_dynamic_patch=1,
|
36 |
+
max_dynamic_patch=6,
|
37 |
+
**kwargs):
|
38 |
+
super().__init__(**kwargs)
|
39 |
+
|
40 |
+
if vision_config is None:
|
41 |
+
vision_config = {}
|
42 |
+
logger.info('vision_config is None. Initializing the InternVisionConfig with default values.')
|
43 |
+
|
44 |
+
if llm_config is None:
|
45 |
+
llm_config = {}
|
46 |
+
logger.info('llm_config is None. Initializing the LlamaConfig config with default values (`LlamaConfig`).')
|
47 |
+
|
48 |
+
self.vision_config = InternVisionConfig(**vision_config)
|
49 |
+
if llm_config['architectures'][0] == 'LlamaForCausalLM':
|
50 |
+
self.llm_config = LlamaConfig(**llm_config)
|
51 |
+
elif llm_config['architectures'][0] == 'Qwen2ForCausalLM':
|
52 |
+
self.llm_config = Qwen2Config(**llm_config)
|
53 |
+
else:
|
54 |
+
raise ValueError('Unsupported architecture: {}'.format(llm_config['architectures'][0]))
|
55 |
+
self.use_backbone_lora = use_backbone_lora
|
56 |
+
self.use_llm_lora = use_llm_lora
|
57 |
+
self.select_layer = select_layer
|
58 |
+
self.force_image_size = force_image_size
|
59 |
+
self.downsample_ratio = downsample_ratio
|
60 |
+
self.template = template
|
61 |
+
self.dynamic_image_size = dynamic_image_size
|
62 |
+
self.use_thumbnail = use_thumbnail
|
63 |
+
self.ps_version = ps_version # pixel shuffle version
|
64 |
+
self.min_dynamic_patch = min_dynamic_patch
|
65 |
+
self.max_dynamic_patch = max_dynamic_patch
|
66 |
+
|
67 |
+
logger.info(f'vision_select_layer: {self.select_layer}')
|
68 |
+
logger.info(f'ps_version: {self.ps_version}')
|
69 |
+
logger.info(f'min_dynamic_patch: {self.min_dynamic_patch}')
|
70 |
+
logger.info(f'max_dynamic_patch: {self.max_dynamic_patch}')
|
71 |
+
|
72 |
+
def to_dict(self):
|
73 |
+
"""
|
74 |
+
Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`].
|
75 |
+
|
76 |
+
Returns:
|
77 |
+
`Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
|
78 |
+
"""
|
79 |
+
output = copy.deepcopy(self.__dict__)
|
80 |
+
output['vision_config'] = self.vision_config.to_dict()
|
81 |
+
output['llm_config'] = self.llm_config.to_dict()
|
82 |
+
output['model_type'] = self.__class__.model_type
|
83 |
+
output['use_backbone_lora'] = self.use_backbone_lora
|
84 |
+
output['use_llm_lora'] = self.use_llm_lora
|
85 |
+
output['select_layer'] = self.select_layer
|
86 |
+
output['force_image_size'] = self.force_image_size
|
87 |
+
output['downsample_ratio'] = self.downsample_ratio
|
88 |
+
output['template'] = self.template
|
89 |
+
output['dynamic_image_size'] = self.dynamic_image_size
|
90 |
+
output['use_thumbnail'] = self.use_thumbnail
|
91 |
+
output['ps_version'] = self.ps_version
|
92 |
+
output['min_dynamic_patch'] = self.min_dynamic_patch
|
93 |
+
output['max_dynamic_patch'] = self.max_dynamic_patch
|
94 |
+
|
95 |
+
return output
|