dmedhi commited on
Commit
04e1d9d
·
verified ·
1 Parent(s): 91f5b32

Upload FloSmolV

Browse files
config.json ADDED
@@ -0,0 +1,383 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "FloSmolV"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_flosmolv.FloSmolVConfig",
7
+ "AutoModelForCausalLM": "modeling_flosmolv.FloSmolV"
8
+ },
9
+ "llm_config": {
10
+ "_name_or_path": "HuggingFaceTB/SmolLM-360M-Instruct",
11
+ "add_cross_attention": false,
12
+ "architectures": [
13
+ "LlamaForCausalLM"
14
+ ],
15
+ "attention_bias": false,
16
+ "attention_dropout": 0.0,
17
+ "bad_words_ids": null,
18
+ "begin_suppress_tokens": null,
19
+ "bos_token_id": 1,
20
+ "chunk_size_feed_forward": 0,
21
+ "cross_attention_hidden_size": null,
22
+ "decoder_start_token_id": null,
23
+ "diversity_penalty": 0.0,
24
+ "do_sample": false,
25
+ "early_stopping": false,
26
+ "encoder_no_repeat_ngram_size": 0,
27
+ "eos_token_id": 2,
28
+ "exponential_decay_length_penalty": null,
29
+ "finetuning_task": null,
30
+ "forced_bos_token_id": null,
31
+ "forced_eos_token_id": null,
32
+ "hidden_act": "silu",
33
+ "hidden_size": 960,
34
+ "id2label": {
35
+ "0": "LABEL_0",
36
+ "1": "LABEL_1"
37
+ },
38
+ "initializer_range": 0.02,
39
+ "intermediate_size": 2560,
40
+ "is_decoder": false,
41
+ "is_encoder_decoder": false,
42
+ "label2id": {
43
+ "LABEL_0": 0,
44
+ "LABEL_1": 1
45
+ },
46
+ "length_penalty": 1.0,
47
+ "max_length": 20,
48
+ "max_position_embeddings": 2048,
49
+ "min_length": 0,
50
+ "mlp_bias": false,
51
+ "model_type": "llama",
52
+ "no_repeat_ngram_size": 0,
53
+ "num_attention_heads": 15,
54
+ "num_beam_groups": 1,
55
+ "num_beams": 1,
56
+ "num_hidden_layers": 32,
57
+ "num_key_value_heads": 5,
58
+ "num_return_sequences": 1,
59
+ "output_attentions": false,
60
+ "output_hidden_states": false,
61
+ "output_scores": false,
62
+ "pad_token_id": 2,
63
+ "prefix": null,
64
+ "pretraining_tp": 1,
65
+ "problem_type": null,
66
+ "pruned_heads": {},
67
+ "remove_invalid_values": false,
68
+ "repetition_penalty": 1.0,
69
+ "return_dict": true,
70
+ "return_dict_in_generate": false,
71
+ "rms_norm_eps": 1e-05,
72
+ "rope_scaling": null,
73
+ "rope_theta": 10000.0,
74
+ "sep_token_id": null,
75
+ "suppress_tokens": null,
76
+ "task_specific_params": null,
77
+ "temperature": 1.0,
78
+ "tf_legacy_loss": false,
79
+ "tie_encoder_decoder": false,
80
+ "tie_word_embeddings": true,
81
+ "tokenizer_class": null,
82
+ "top_k": 50,
83
+ "top_p": 1.0,
84
+ "torch_dtype": "bfloat16",
85
+ "torchscript": false,
86
+ "typical_p": 1.0,
87
+ "use_bfloat16": false,
88
+ "use_cache": true,
89
+ "vocab_size": 49152
90
+ },
91
+ "model_type": "flosmolV",
92
+ "torch_dtype": "float32",
93
+ "transformers_version": "4.44.0",
94
+ "vision_config": {
95
+ "_name_or_path": "microsoft/Florence-2-base",
96
+ "add_cross_attention": false,
97
+ "architectures": [
98
+ "Florence2ForConditionalGeneration"
99
+ ],
100
+ "auto_map": {
101
+ "AutoConfig": "microsoft/Florence-2-base--configuration_florence2.Florence2Config",
102
+ "AutoModelForCausalLM": "microsoft/Florence-2-base--modeling_florence2.Florence2ForConditionalGeneration"
103
+ },
104
+ "bad_words_ids": null,
105
+ "begin_suppress_tokens": null,
106
+ "bos_token_id": 0,
107
+ "chunk_size_feed_forward": 0,
108
+ "cross_attention_hidden_size": null,
109
+ "decoder_start_token_id": null,
110
+ "diversity_penalty": 0.0,
111
+ "do_sample": false,
112
+ "early_stopping": false,
113
+ "encoder_no_repeat_ngram_size": 0,
114
+ "eos_token_id": 2,
115
+ "exponential_decay_length_penalty": null,
116
+ "finetuning_task": null,
117
+ "forced_bos_token_id": null,
118
+ "forced_eos_token_id": null,
119
+ "id2label": {
120
+ "0": "LABEL_0",
121
+ "1": "LABEL_1"
122
+ },
123
+ "ignore_index": -100,
124
+ "is_decoder": false,
125
+ "is_encoder_decoder": true,
126
+ "label2id": {
127
+ "LABEL_0": 0,
128
+ "LABEL_1": 1
129
+ },
130
+ "length_penalty": 1.0,
131
+ "max_length": 20,
132
+ "min_length": 0,
133
+ "model_type": "florence2",
134
+ "no_repeat_ngram_size": 0,
135
+ "num_beam_groups": 1,
136
+ "num_beams": 1,
137
+ "num_return_sequences": 1,
138
+ "output_attentions": false,
139
+ "output_hidden_states": false,
140
+ "output_scores": false,
141
+ "pad_token_id": 1,
142
+ "prefix": null,
143
+ "problem_type": null,
144
+ "projection_dim": 768,
145
+ "pruned_heads": {},
146
+ "remove_invalid_values": false,
147
+ "repetition_penalty": 1.0,
148
+ "return_dict": true,
149
+ "return_dict_in_generate": false,
150
+ "sep_token_id": null,
151
+ "suppress_tokens": null,
152
+ "task_specific_params": null,
153
+ "temperature": 1.0,
154
+ "text_config": {
155
+ "_name_or_path": "",
156
+ "activation_dropout": 0.1,
157
+ "activation_function": "gelu",
158
+ "add_bias_logits": false,
159
+ "add_cross_attention": false,
160
+ "add_final_layer_norm": false,
161
+ "architectures": null,
162
+ "attention_dropout": 0.1,
163
+ "bad_words_ids": null,
164
+ "begin_suppress_tokens": null,
165
+ "bos_token_id": 0,
166
+ "chunk_size_feed_forward": 0,
167
+ "classif_dropout": 0.1,
168
+ "classifier_dropout": 0.0,
169
+ "cross_attention_hidden_size": null,
170
+ "d_model": 768,
171
+ "decoder_attention_heads": 12,
172
+ "decoder_ffn_dim": 3072,
173
+ "decoder_layerdrop": 0.0,
174
+ "decoder_layers": 6,
175
+ "decoder_start_token_id": 2,
176
+ "diversity_penalty": 0.0,
177
+ "do_sample": false,
178
+ "dropout": 0.1,
179
+ "early_stopping": true,
180
+ "encoder_attention_heads": 12,
181
+ "encoder_ffn_dim": 3072,
182
+ "encoder_layerdrop": 0.0,
183
+ "encoder_layers": 6,
184
+ "encoder_no_repeat_ngram_size": 0,
185
+ "eos_token_id": 2,
186
+ "exponential_decay_length_penalty": null,
187
+ "finetuning_task": null,
188
+ "forced_bos_token_id": 0,
189
+ "forced_eos_token_id": 2,
190
+ "gradient_checkpointing": false,
191
+ "id2label": {
192
+ "0": "LABEL_0",
193
+ "1": "LABEL_1",
194
+ "2": "LABEL_2"
195
+ },
196
+ "init_std": 0.02,
197
+ "is_decoder": false,
198
+ "is_encoder_decoder": true,
199
+ "label2id": {
200
+ "LABEL_0": 0,
201
+ "LABEL_1": 1,
202
+ "LABEL_2": 2
203
+ },
204
+ "length_penalty": 1.0,
205
+ "max_length": 20,
206
+ "max_position_embeddings": 1024,
207
+ "min_length": 0,
208
+ "model_type": "florence2_language",
209
+ "no_repeat_ngram_size": 3,
210
+ "normalize_before": false,
211
+ "num_beam_groups": 1,
212
+ "num_beams": 3,
213
+ "num_hidden_layers": 6,
214
+ "num_return_sequences": 1,
215
+ "output_attentions": false,
216
+ "output_hidden_states": false,
217
+ "output_scores": false,
218
+ "pad_token_id": 1,
219
+ "prefix": null,
220
+ "problem_type": null,
221
+ "pruned_heads": {},
222
+ "remove_invalid_values": false,
223
+ "repetition_penalty": 1.0,
224
+ "return_dict": true,
225
+ "return_dict_in_generate": false,
226
+ "scale_embedding": false,
227
+ "sep_token_id": null,
228
+ "suppress_tokens": null,
229
+ "task_specific_params": null,
230
+ "temperature": 1.0,
231
+ "tf_legacy_loss": false,
232
+ "tie_encoder_decoder": false,
233
+ "tie_word_embeddings": true,
234
+ "tokenizer_class": null,
235
+ "top_k": 50,
236
+ "top_p": 1.0,
237
+ "torch_dtype": null,
238
+ "torchscript": false,
239
+ "typical_p": 1.0,
240
+ "use_bfloat16": false,
241
+ "use_cache": true,
242
+ "vocab_size": 51289
243
+ },
244
+ "tf_legacy_loss": false,
245
+ "tie_encoder_decoder": false,
246
+ "tie_word_embeddings": true,
247
+ "tokenizer_class": null,
248
+ "top_k": 50,
249
+ "top_p": 1.0,
250
+ "torch_dtype": "float16",
251
+ "torchscript": false,
252
+ "typical_p": 1.0,
253
+ "use_bfloat16": false,
254
+ "vision_config": {
255
+ "_name_or_path": "",
256
+ "add_cross_attention": false,
257
+ "architectures": null,
258
+ "bad_words_ids": null,
259
+ "begin_suppress_tokens": null,
260
+ "bos_token_id": null,
261
+ "chunk_size_feed_forward": 0,
262
+ "cross_attention_hidden_size": null,
263
+ "decoder_start_token_id": null,
264
+ "depths": [
265
+ 1,
266
+ 1,
267
+ 9,
268
+ 1
269
+ ],
270
+ "dim_embed": [
271
+ 128,
272
+ 256,
273
+ 512,
274
+ 1024
275
+ ],
276
+ "diversity_penalty": 0.0,
277
+ "do_sample": false,
278
+ "drop_path_rate": 0.1,
279
+ "early_stopping": false,
280
+ "enable_checkpoint": false,
281
+ "encoder_no_repeat_ngram_size": 0,
282
+ "eos_token_id": null,
283
+ "exponential_decay_length_penalty": null,
284
+ "finetuning_task": null,
285
+ "forced_bos_token_id": null,
286
+ "forced_eos_token_id": null,
287
+ "id2label": {
288
+ "0": "LABEL_0",
289
+ "1": "LABEL_1"
290
+ },
291
+ "image_feature_source": [
292
+ "spatial_avg_pool",
293
+ "temporal_avg_pool"
294
+ ],
295
+ "image_pos_embed": {
296
+ "max_pos_embeddings": 50,
297
+ "type": "learned_abs_2d"
298
+ },
299
+ "is_decoder": false,
300
+ "is_encoder_decoder": false,
301
+ "label2id": {
302
+ "LABEL_0": 0,
303
+ "LABEL_1": 1
304
+ },
305
+ "length_penalty": 1.0,
306
+ "max_length": 20,
307
+ "min_length": 0,
308
+ "model_type": "",
309
+ "no_repeat_ngram_size": 0,
310
+ "num_beam_groups": 1,
311
+ "num_beams": 1,
312
+ "num_groups": [
313
+ 4,
314
+ 8,
315
+ 16,
316
+ 32
317
+ ],
318
+ "num_heads": [
319
+ 4,
320
+ 8,
321
+ 16,
322
+ 32
323
+ ],
324
+ "num_return_sequences": 1,
325
+ "output_attentions": false,
326
+ "output_hidden_states": false,
327
+ "output_scores": false,
328
+ "pad_token_id": null,
329
+ "patch_padding": [
330
+ 3,
331
+ 1,
332
+ 1,
333
+ 1
334
+ ],
335
+ "patch_prenorm": [
336
+ false,
337
+ true,
338
+ true,
339
+ true
340
+ ],
341
+ "patch_size": [
342
+ 7,
343
+ 3,
344
+ 3,
345
+ 3
346
+ ],
347
+ "patch_stride": [
348
+ 4,
349
+ 2,
350
+ 2,
351
+ 2
352
+ ],
353
+ "prefix": null,
354
+ "problem_type": null,
355
+ "projection_dim": 768,
356
+ "pruned_heads": {},
357
+ "remove_invalid_values": false,
358
+ "repetition_penalty": 1.0,
359
+ "return_dict": true,
360
+ "return_dict_in_generate": false,
361
+ "sep_token_id": null,
362
+ "suppress_tokens": null,
363
+ "task_specific_params": null,
364
+ "temperature": 1.0,
365
+ "tf_legacy_loss": false,
366
+ "tie_encoder_decoder": false,
367
+ "tie_word_embeddings": true,
368
+ "tokenizer_class": null,
369
+ "top_k": 50,
370
+ "top_p": 1.0,
371
+ "torch_dtype": null,
372
+ "torchscript": false,
373
+ "typical_p": 1.0,
374
+ "use_bfloat16": false,
375
+ "visual_temporal_embedding": {
376
+ "max_temporal_embeddings": 100,
377
+ "type": "COSINE"
378
+ },
379
+ "window_size": 12
380
+ },
381
+ "vocab_size": 51289
382
+ }
383
+ }
configuration_flosmolv.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoConfig, PretrainedConfig
2
+
3
+ class FloSmolVConfig(PretrainedConfig):
4
+ model_type = "flosmolV"
5
+ is_composition = True
6
+
7
+ def __init__(
8
+ self,
9
+ vision_config=None,
10
+ llm_config=None,
11
+ **kwargs,
12
+ ):
13
+
14
+ if vision_config is None:
15
+ vision_config = AutoConfig.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
16
+ if llm_config is None:
17
+ llm_config = AutoConfig.from_pretrained("HuggingFaceTB/SmolLM-360M-Instruct", trust_remote_code=True)
18
+ self.vision_config = vision_config
19
+ self.llm_config = llm_config
20
+ super().__init__(**kwargs)
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:0f5dd2bcf0af7477ad0cff0de5270263564167ded8c7e94ccc71eb07489ce3c1
3
- size 2531249132
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4655e21a7276883f9db1dec7880a0c192c37b6b5e2db6cec9c25e72c8c0e7052
3
+ size 2531248820
modeling_flosmolv.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel
2
+ from typing import List
3
+ import torch
4
+ from .configuration_flosmolv import FloSmolVConfig
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor
6
+
7
+ class FloSmolV(PreTrainedModel):
8
+ config_class = FloSmolVConfig
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ print(f"Device: {device}")
11
+ def __init__(self, config):
12
+ super().__init__(config)
13
+ self.florence2_model = AutoModelForCausalLM.from_pretrained(
14
+ self.config.vision_config._name_or_path,
15
+ trust_remote_code=True,
16
+ ).eval().to(self.device)
17
+ self.florence2_processor = AutoProcessor.from_pretrained(self.config.vision_config._name_or_path, trust_remote_code=True,)
18
+ self.smollm_model = AutoModelForCausalLM.from_pretrained(
19
+ self.config.llm_config._name_or_path,
20
+ trust_remote_code=True,
21
+ ).to(self.device)
22
+ self.smollm_tokenizer = AutoTokenizer.from_pretrained(self.config.llm_config._name_or_path, trust_remote_code=True,)
23
+
24
+ def forward(self, image, query: str):
25
+ ## Vision
26
+ prompt = "<MORE_DETAILED_CAPTION>"
27
+ vision_inpupt = self.florence2_processor(text=prompt, images=image, return_tensors="pt")
28
+ generated_ids = self.florence2_model.generate(
29
+ input_ids=vision_inpupt["input_ids"].to(torch.int64).to(self.device),
30
+ pixel_values=vision_inpupt["pixel_values"].to(self.device),
31
+ max_new_tokens=1024,
32
+ early_stopping=False,
33
+ do_sample=False,
34
+ num_beams=3,
35
+ )
36
+ generated_text = self.florence2_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
37
+ parsed_answer = self.florence2_processor.post_process_generation(
38
+ generated_text,
39
+ task=prompt,
40
+ image_size=(image.width, image.height)
41
+ )
42
+ ## LM
43
+ messages = [{"role": "user", "content": f"You are an expert AI assistant. Based on the CONTENT, you should answer the QUESTION in short.\n\nCONTENT:{str(parsed_answer[prompt])}\n\nQUESTION:{str(query)}\n"}]
44
+ input_text=self.smollm_tokenizer.apply_chat_template(messages, tokenize=False)
45
+ llm_inputs = self.smollm_tokenizer.encode(input_text, return_tensors="pt").to(self.device)
46
+ outputs = self.smollm_model.generate(
47
+ llm_inputs,
48
+ max_new_tokens=50,
49
+ temperature=0.2,
50
+ # top_p=0.9,
51
+ do_sample=True,
52
+ )
53
+ response = self.smollm_tokenizer.decode(outputs[0])
54
+ cleaned_text = response.split("assistant\n", 1)[-1].strip()
55
+ return cleaned_text[:-10] if cleaned_text[-10:] == "<|im_end|>" else cleaned_text