Image-Text-to-Text
Transformers
Safetensors
Portuguese
tinyllava
text-generation
vision
custom_code
nicholasKluge commited on
Commit
e316477
·
verified ·
1 Parent(s): 87f7b3a

Upload configuration.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. configuration.py +116 -0
configuration.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ from transformers import CONFIG_MAPPING
3
+ from transformers import AutoConfig
4
+
5
+ IGNORE_INDEX = -100
6
+ IMAGE_TOKEN_INDEX = -200
7
+ DEFAULT_IMAGE_TOKEN = "<image>"
8
+
9
+
10
+ class TinyLlavaConfig(PretrainedConfig):
11
+
12
+ model_type = "tinyllava"
13
+ def __init__(
14
+ self,
15
+ llm_model_name_or_path = '',
16
+ tokenizer_name_or_path = None,
17
+ vision_model_name_or_path = '',
18
+ vision_model_name_or_path2 = '',
19
+ connector_type = None,
20
+ text_config=None,
21
+ hidden_size=2048,
22
+ vocab_size=32000,
23
+ ignore_index=-100,
24
+ image_token_index=32000,
25
+ pad_token = None,
26
+ pad_token_id = None,
27
+ tokenizer_padding_side = 'right',
28
+ tokenizer_model_max_length = 2048,
29
+ vision_config = None,
30
+ vision_hidden_size = None,
31
+ vision_feature_layer = -2,
32
+ vision_feature_select_strategy = 'patch',
33
+ image_aspect_ratio = 'square',
34
+ resampler_hidden_size = None,
35
+ num_queries = None,
36
+ num_resampler_layers = None,
37
+ use_cache = False,
38
+ cache_dir = None,
39
+ tokenizer_use_fast = False,
40
+ tune_type_llm = 'frozen',
41
+ tune_type_connector = 'frozen',
42
+ tune_type_vision_tower = 'frozen',
43
+ tune_vision_tower_from_layer = -1,
44
+
45
+ **kwargs
46
+
47
+ ):
48
+ self.llm_model_name_or_path = llm_model_name_or_path
49
+ self.tokenizer_name_or_path = tokenizer_name_or_path or self.llm_model_name_or_path
50
+ self.vision_model_name_or_path = vision_model_name_or_path
51
+ self.vision_model_name_or_path2 = vision_model_name_or_path2
52
+ self.connector_type = connector_type
53
+ self.tune_type_llm = tune_type_llm
54
+ self.tune_type_connector = tune_type_connector
55
+ self.tune_type_vision_tower = tune_type_vision_tower
56
+ self.tune_vision_tower_from_layer = tune_vision_tower_from_layer
57
+
58
+ self.ignore_index = IGNORE_INDEX
59
+ self.image_token_index = IMAGE_TOKEN_INDEX
60
+ self.pad_token = pad_token
61
+ self.pad_token_id = pad_token_id
62
+ self.tokenizer_padding_side = tokenizer_padding_side
63
+ self.tokenizer_model_max_length = tokenizer_model_max_length
64
+ self.vision_feature_layer = vision_feature_layer
65
+ self.vision_feature_select_strategy = vision_feature_select_strategy
66
+ self.image_aspect_ratio = image_aspect_ratio
67
+ self.resampler_hidden_size = resampler_hidden_size
68
+ self.num_queries = num_queries
69
+ self.num_resampler_layers = num_resampler_layers
70
+ self.use_cache = use_cache
71
+ self.cache_dir = cache_dir
72
+ self.tokenizer_use_fast = tokenizer_use_fast
73
+ self._load_text_config(text_config)
74
+ self._load_vision_config(vision_config)
75
+
76
+ super().__init__(**kwargs)
77
+
78
+
79
+ def _load_text_config(self, text_config=None):
80
+ if self.llm_model_name_or_path is None or self.llm_model_name_or_path == '':
81
+ self.text_config = CONFIG_MAPPING['llama']()
82
+
83
+ else:
84
+ self.text_config = AutoConfig.from_pretrained(self.llm_model_name_or_path, trust_remote_code=True)
85
+ if text_config is not None:
86
+ self.text_config = self.text_config.from_dict(text_config)
87
+
88
+ self.hidden_size = getattr(self.text_config, 'hidden_size', getattr(self.text_config, 'model_dim', None))
89
+ self.vocab_size = getattr(self.text_config, 'vocab_size', None)
90
+
91
+
92
+
93
+ def _load_vision_config(self, vision_config=None):
94
+ if self.vision_model_name_or_path is None or self.vision_model_name_or_path == '':
95
+ self.vision_config = CONFIG_MAPPING['clip_vision_model'](
96
+ intermediate_size=4096,
97
+ hidden_size=1024,
98
+ patch_size=14,
99
+ image_size=336,
100
+ num_hidden_layers=24,
101
+ num_attention_heads=16,
102
+ vocab_size=32000,
103
+ projection_dim=768,
104
+ )
105
+
106
+ else:
107
+ self.vision_config = AutoConfig.from_pretrained(self.vision_model_name_or_path.split(':')[-1])
108
+ self.vision_config = getattr(self.vision_config, 'vision_config', self.vision_config)
109
+ if vision_config is not None:
110
+ self.vision_config = self.vision_config.from_dict(vision_config)
111
+
112
+ self.vision_config.model_name_or_path = self.vision_model_name_or_path.split(':')[-1]
113
+ self.vision_config.model_name_or_path2 = self.vision_model_name_or_path2.split(':')[-1]
114
+ self.vision_hidden_size = getattr(self.vision_config, 'hidden_size', None)
115
+
116
+