Integrate with Sentence Transformers via MultiVectorEncoder

#1
by tomaarsen HF Staff - opened
1_Dense/config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "in_features": 1024,
3
+ "out_features": 128,
4
+ "bias": true,
5
+ "activation_function": "torch.nn.modules.linear.Identity",
6
+ "module_input_name": "token_embeddings",
7
+ "module_output_name": "token_embeddings"
8
+ }
1_Dense/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e8a23b489bfbea8795e6d76e1c2e912155cdd6e3b556a6da61bbe17200c18e3
3
+ size 262560
2_Normalize/config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "module_input_name": "token_embeddings",
3
+ "module_output_name": "token_embeddings"
4
+ }
3_MultiVectorMask/config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "skiplist_words": [],
3
+ "skiplist_tasks": [],
4
+ "keep_only_token_ids": null
5
+ }
README.md CHANGED
@@ -24,6 +24,7 @@ tags:
24
  - hierarchical-merge
25
  - mteb
26
  - vidore
 
27
  base_model: LiquidAI/LFM2-VL-450M
28
  pipeline_tag: image-text-to-text
29
  datasets:
@@ -241,6 +242,42 @@ Specialist Model
241
 
242
  ## Installation & Usage
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  > ⚠️ **Important**: Install our package first before loading the model:
245
 
246
  ```bash
 
24
  - hierarchical-merge
25
  - mteb
26
  - vidore
27
+ - sentence-transformers
28
  base_model: LiquidAI/LFM2-VL-450M
29
  pipeline_tag: image-text-to-text
30
  datasets:
 
242
 
243
  ## Installation & Usage
244
 
245
+ ### Sentence Transformers
246
+
247
+ This model can be used with [Sentence Transformers](https://www.sbert.net/) as a multi-vector (ColBERT-style late interaction) retriever via the `MultiVectorEncoder`:
248
+
249
+ ```bash
250
+ pip install "sentence-transformers[image]>=6.0.0"
251
+ ```
252
+
253
+ ```python
254
+ from sentence_transformers import MultiVectorEncoder
255
+
256
+ model = MultiVectorEncoder("VAGOsolutions/SauerkrautLM-ColLFM2-450M-v0.1")
257
+
258
+ queries = [
259
+ "What is the variable represented on the y-axis of the graph?",
260
+ "Total outlay is maximum in which year?",
261
+ ]
262
+ images = [
263
+ "https://huggingface.co/datasets/sentence-transformers/example-documents/resolve/main/doc1.jpg",
264
+ "https://huggingface.co/datasets/sentence-transformers/example-documents/resolve/main/doc2.jpg",
265
+ ]
266
+
267
+ query_embeddings = model.encode_query(queries)
268
+ image_embeddings = model.encode_document(images)
269
+ print(query_embeddings[0].shape, image_embeddings[0].shape)
270
+ # torch.Size([14, 128]) torch.Size([1792, 128])
271
+
272
+ # Diagonal should have higher scores
273
+ scores = model.similarity(query_embeddings, image_embeddings)
274
+ print(scores)
275
+ # tensor([[13.5820, 13.4766],
276
+ # [ 9.2461, 9.5703]], device='cuda:0')
277
+ ```
278
+
279
+ ### SauerkrautLM ColPali
280
+
281
  > ⚠️ **Important**: Install our package first before loading the model:
282
 
283
  ```bash
additional_chat_templates/sentence_transformers.jinja ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- for message in messages -%}
2
+ {%- set ns = namespace(text='') -%}
3
+ {%- if message['content'] is string -%}
4
+ {%- set ns.text = message['content'] -%}
5
+ {%- else -%}
6
+ {%- for item in message['content'] -%}
7
+ {%- if 'text' in item -%}
8
+ {%- set ns.text = ns.text + item.text -%}
9
+ {%- endif -%}
10
+ {%- endfor -%}
11
+ {%- endif -%}
12
+ {%- if task is defined and task == 'query' -%}
13
+ {{- bos_token + ns.text -}}
14
+ {%- else -%}
15
+ {{- '<|im_start|>user\n<image>Describe the image.<|im_end|>' -}}
16
+ {%- endif -%}
17
+ {%- endfor -%}
config_sentence_transformers.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "MultiVectorEncoder",
3
+ "similarity_fn_name": "maxsim",
4
+ "prompts": {},
5
+ "default_prompt_name": null,
6
+ "__version__": {
7
+ "sentence_transformers": "6.0.0"
8
+ }
9
+ }
modules.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.base.modules.transformer.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Dense",
12
+ "type": "sentence_transformers.base.modules.dense.Dense"
13
+ },
14
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "2_Normalize",
18
+ "type": "sentence_transformers.base.modules.normalize.Normalize"
19
+ },
20
+ {
21
+ "idx": 3,
22
+ "name": "3",
23
+ "path": "3_MultiVectorMask",
24
+ "type": "sentence_transformers.multi_vector_encoder.modules.multi_vector_mask.MultiVectorMask"
25
+ }
26
+ ]
sentence_bert_config.json ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "transformer_task": "feature-extraction",
3
+ "modality_config": {
4
+ "text": {
5
+ "method": "forward",
6
+ "method_output_name": "last_hidden_state"
7
+ },
8
+ "image": {
9
+ "method": "forward",
10
+ "method_output_name": "last_hidden_state"
11
+ },
12
+ "message": {
13
+ "method": "forward",
14
+ "method_output_name": "last_hidden_state",
15
+ "format": "structured"
16
+ }
17
+ },
18
+ "module_output_name": "token_embeddings",
19
+ "processing_kwargs": {
20
+ "chat_template": {
21
+ "chat_template": "sentence_transformers"
22
+ }
23
+ },
24
+ "config_kwargs": {
25
+ "do_image_splitting": true,
26
+ "downsample_factor": 2,
27
+ "encoder_patch_size": 16,
28
+ "image_token_id": 396,
29
+ "image_token_index": 396,
30
+ "max_image_tokens": 256,
31
+ "max_num_patches": 1024,
32
+ "max_pixels_tolerance": 2.0,
33
+ "max_tiles": 10,
34
+ "min_image_tokens": 64,
35
+ "min_tiles": 2,
36
+ "model_type": "lfm2_vl",
37
+ "projector_bias": true,
38
+ "projector_hidden_act": "gelu",
39
+ "projector_hidden_size": 2560,
40
+ "text_config": {
41
+ "architectures": [
42
+ "Lfm2ForCausalLM"
43
+ ],
44
+ "block_auto_adjust_ff_dim": true,
45
+ "block_dim": 1024,
46
+ "block_ff_dim": 6656,
47
+ "block_ffn_dim_multiplier": 1.0,
48
+ "block_mlp_init_scale": 1.0,
49
+ "block_multiple_of": 256,
50
+ "block_norm_eps": 1e-05,
51
+ "block_out_init_scale": 1.0,
52
+ "block_use_swiglu": true,
53
+ "block_use_xavier_init": true,
54
+ "conv_L_cache": 3,
55
+ "conv_bias": false,
56
+ "conv_dim": 1024,
57
+ "conv_dim_out": 1024,
58
+ "conv_use_xavier_init": true,
59
+ "eos_token_id": 7,
60
+ "hidden_size": 1024,
61
+ "initializer_range": 0.02,
62
+ "intermediate_size": 6656,
63
+ "layer_types": [
64
+ "conv",
65
+ "conv",
66
+ "full_attention",
67
+ "conv",
68
+ "conv",
69
+ "full_attention",
70
+ "conv",
71
+ "conv",
72
+ "full_attention",
73
+ "conv",
74
+ "full_attention",
75
+ "conv",
76
+ "full_attention",
77
+ "conv",
78
+ "full_attention",
79
+ "conv"
80
+ ],
81
+ "max_position_embeddings": 128000,
82
+ "model_type": "lfm2",
83
+ "norm_eps": 1e-05,
84
+ "num_attention_heads": 16,
85
+ "num_heads": 16,
86
+ "num_hidden_layers": 16,
87
+ "num_key_value_heads": 8,
88
+ "rope_theta": 1000000.0,
89
+ "use_cache": true,
90
+ "use_pos_enc": true,
91
+ "vocab_size": 65536
92
+ },
93
+ "tile_size": 512,
94
+ "use_image_special_tokens": true,
95
+ "use_thumbnail": false,
96
+ "vision_config": {
97
+ "attention_dropout": 0.0,
98
+ "hidden_act": "gelu_pytorch_tanh",
99
+ "hidden_size": 768,
100
+ "intermediate_size": 3072,
101
+ "layer_norm_eps": 1e-06,
102
+ "model_type": "siglip2_vision_model",
103
+ "num_attention_heads": 12,
104
+ "num_channels": 3,
105
+ "num_hidden_layers": 12,
106
+ "num_patches": 256,
107
+ "patch_size": 16,
108
+ "vision_use_head": false
109
+ }
110
+ },
111
+ "model_kwargs": {
112
+ "key_mapping": {
113
+ "^model\\.": ""
114
+ }
115
+ }
116
+ }
tokenizer_config.json CHANGED
@@ -4084,5 +4084,6 @@
4084
  "spaces_between_special_tokens": false,
4085
  "tokenizer_class": "PreTrainedTokenizerFast",
4086
  "use_default_system_prompt": false,
4087
- "use_fast": true
 
4088
  }
 
4084
  "spaces_between_special_tokens": false,
4085
  "tokenizer_class": "PreTrainedTokenizerFast",
4086
  "use_default_system_prompt": false,
4087
+ "use_fast": true,
4088
+ "padding_side": "left"
4089
  }