michaeldinzinger commited on
Commit
23b0e33
·
1 Parent(s): 228724f

Initial commit

Browse files
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .env
2
+ poetry.lock
3
+ **/__pycache__/
4
+ model/
config.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ConcatModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "modeling_arctic_s_bge_small.ConcatModelConfig",
7
+ "AutoModel": "modeling_arctic_s_bge_small.ConcatModel"
8
+ },
9
+ "model_type": "arctic-l-bge-small",
10
+ "torch_dtype": "float32",
11
+ "transformers_version": "4.42.3"
12
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:920fbc079a8ad847586b821b8288c77361f2217a22b55375e3dc5679fb28ccae
3
+ size 1474083464
modeling_arctic_l_bge_small.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from transformers import BertModel, PreTrainedModel, BertConfig, PretrainedConfig, AutoModel
5
+ from typing import *
6
+
7
+
8
+ class ConcatModelConfig(PretrainedConfig):
9
+ model_type = "arctic-l-bge-small"
10
+
11
+ def __init__(self, **kwargs):
12
+ super().__init__(**kwargs)
13
+
14
+
15
+ # See https://huggingface.co/Marqo/marqo-chimera-arctic-bge-m
16
+ class ConcatModel(PreTrainedModel):
17
+ config_class = ConcatModelConfig
18
+
19
+ def __init__(self, config: ConcatModelConfig):
20
+ super().__init__(config)
21
+ bert_config_1 = BertConfig(
22
+ vocab_size=30522,
23
+ hidden_size=1024,
24
+ num_hidden_layers=24,
25
+ num_attention_heads=16,
26
+ intermediate_size=4096,
27
+ hidden_act="gelu",
28
+ hidden_dropout_prob=0.1,
29
+ attention_probs_dropout_prob=0.1,
30
+ max_position_embeddings=512,
31
+ type_vocab_size=2,
32
+ initializer_range=0.02,
33
+ layer_norm_eps=1e-12,
34
+ )
35
+
36
+ bert_config_2 = BertConfig(
37
+ vocab_size=30522,
38
+ hidden_size=384,
39
+ num_hidden_layers=12,
40
+ num_attention_heads=12,
41
+ intermediate_size=1536,
42
+ hidden_act="gelu",
43
+ hidden_dropout_prob=0.1,
44
+ attention_probs_dropout_prob=0.1,
45
+ max_position_embeddings=512,
46
+ type_vocab_size=2,
47
+ initializer_range=0.02,
48
+ layer_norm_eps=1e-12,
49
+ )
50
+
51
+ self.model = nn.ModuleDict(
52
+ {
53
+ "model_0": BertModel(bert_config_1),
54
+ "model_1": BertModel(bert_config_2),
55
+ }
56
+ )
57
+
58
+ def forward(
59
+ self,
60
+ input_ids: torch.Tensor,
61
+ attention_mask: torch.Tensor,
62
+ token_type_ids: torch.Tensor = None,
63
+ **kwargs
64
+ ) -> torch.Tensor:
65
+ embeddings = []
66
+ for _, model in self.model.items():
67
+ model_output = model(
68
+ input_ids=input_ids,
69
+ attention_mask=attention_mask,
70
+ token_type_ids=token_type_ids,
71
+ )
72
+ pooled_output = model_output[0][:, 0]
73
+ pooled_output = F.normalize(pooled_output, p=2, dim=-1)
74
+ embeddings.append(pooled_output)
75
+
76
+ return torch.cat(embeddings, dim=-1)
77
+
78
+ def load_weights_from_automodels(
79
+ self, in_models: List[str], has_pooling_layer: List[bool]
80
+ ):
81
+ model_list = []
82
+ for i, model_name in enumerate(in_models):
83
+ model = AutoModel.from_pretrained(
84
+ model_name,
85
+ add_pooling_layer=has_pooling_layer[i],
86
+ trust_remote_code=True,
87
+ )
88
+ model.eval()
89
+ model_list.append(model)
90
+
91
+ self.model = nn.ModuleDict(
92
+ {f"model_{i}": model for i, model in enumerate(model_list)}
93
+ )
pyproject.toml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [build-system]
2
+ requires = ["poetry-core"]
3
+ build-backend = "poetry.core.masonry.api"
4
+
5
+ [tool.poetry]
6
+ name = "arctic-l-bge-small"
7
+ version = "1.0.0"
8
+ description = "Upload ConcatModels."
9
+ authors = [
10
+ "Michael Dinzinger"
11
+ ]
12
+ homepage = "https://www.fim.uni-passau.de"
13
+ repository = "https://www.fim.uni-passau.de"
14
+ readme = "README.md"
15
+ license = "MIT"
16
+ package-mode = false
17
+
18
+ [tool.poetry.dependencies]
19
+ python = ">=3.10,<3.12"
20
+ transformers = "4.42.3"
21
+ torch = "2.5.0"
save_safetensors.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BertTokenizer
2
+ from modeling_arctic_l_bge_small import ConcatModel, ConcatModelConfig
3
+
4
+ config = ConcatModelConfig()
5
+ model = ConcatModel(config)
6
+ model.load_weights_from_automodels(
7
+ in_models=['Snowflake/snowflake-arctic-embed-l', 'BAAI/bge-small-en-v1.5'],
8
+ has_pooling_layer=[True, True]
9
+ )
10
+
11
+ tokenizer = BertTokenizer(vocab_file='vocab.txt')
12
+
13
+ output_path = 'model'
14
+ model.save_pretrained(output_path)
15
+ tokenizer.save_pretrained(output_path)
16
+
17
+ print(f'Model saved as {output_path}')
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": true,
45
+ "cls_token": "[CLS]",
46
+ "do_basic_tokenize": true,
47
+ "do_lower_case": true,
48
+ "mask_token": "[MASK]",
49
+ "model_max_length": 1000000000000000019884624838656,
50
+ "never_split": null,
51
+ "pad_token": "[PAD]",
52
+ "sep_token": "[SEP]",
53
+ "strip_accents": null,
54
+ "tokenize_chinese_chars": true,
55
+ "tokenizer_class": "BertTokenizer",
56
+ "unk_token": "[UNK]"
57
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff