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