huseinzol05
commited on
Commit
•
76c96d0
1
Parent(s):
b2bd187
Upload model
Browse files- config.json +38 -0
- mistral_contrastive.py +60 -0
- model.safetensors +3 -0
config.json
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "embedding-model-mistral-349m-contrastive/checkpoint-66600",
|
3 |
+
"architectures": [
|
4 |
+
"MistralModelEmbedding"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoModel": "mistral_contrastive.MistralModelEmbedding"
|
8 |
+
},
|
9 |
+
"bos_token_id": 1,
|
10 |
+
"eos_token_id": 2,
|
11 |
+
"hidden_act": "silu",
|
12 |
+
"hidden_size": 1024,
|
13 |
+
"id2label": {
|
14 |
+
"0": "LABEL_0"
|
15 |
+
},
|
16 |
+
"initializer_range": 0.02,
|
17 |
+
"intermediate_size": 4096,
|
18 |
+
"label2id": {
|
19 |
+
"LABEL_0": 0
|
20 |
+
},
|
21 |
+
"max_position_embeddings": 32768,
|
22 |
+
"model_type": "mistral",
|
23 |
+
"normalized": true,
|
24 |
+
"num_attention_heads": 16,
|
25 |
+
"num_hidden_layers": 18,
|
26 |
+
"num_key_value_heads": 8,
|
27 |
+
"pad_token_id": 0,
|
28 |
+
"rms_norm_eps": 1e-05,
|
29 |
+
"rope_theta": 10000.0,
|
30 |
+
"sentence_pooling_method": "mean",
|
31 |
+
"sliding_window": 4096,
|
32 |
+
"temperature": 0.02,
|
33 |
+
"tie_word_embeddings": false,
|
34 |
+
"torch_dtype": "float32",
|
35 |
+
"transformers_version": "4.35.0",
|
36 |
+
"use_cache": true,
|
37 |
+
"vocab_size": 32000
|
38 |
+
}
|
mistral_contrastive.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import MistralModel, MistralConfig
|
3 |
+
from typing import Dict
|
4 |
+
from transformers.file_utils import ModelOutput
|
5 |
+
from typing import List, Optional, Tuple, Union
|
6 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
7 |
+
from torch import nn, Tensor
|
8 |
+
from dataclasses import dataclass
|
9 |
+
from torch import nn
|
10 |
+
from typing import Dict
|
11 |
+
import torch
|
12 |
+
from transformers.file_utils import ModelOutput
|
13 |
+
import torch.nn.functional as F
|
14 |
+
|
15 |
+
COSINE_DISTANCE = lambda x, y: 1-F.cosine_similarity(x, y)
|
16 |
+
|
17 |
+
@dataclass
|
18 |
+
class EncoderOutput(ModelOutput):
|
19 |
+
loss: Optional[Tensor] = None
|
20 |
+
|
21 |
+
class MistralModelEmbedding(MistralModel):
|
22 |
+
def __init__(self, config: MistralConfig, **kwargs):
|
23 |
+
super().__init__(config, **kwargs)
|
24 |
+
|
25 |
+
self.dense_layer = nn.Linear(self.config.hidden_size,768)
|
26 |
+
|
27 |
+
def sentence_embedding(self, hidden_state, mask):
|
28 |
+
if self.config.sentence_pooling_method == 'mean':
|
29 |
+
s = torch.sum(hidden_state * mask.unsqueeze(-1).float(), dim=1)
|
30 |
+
d = mask.sum(axis=1, keepdim=True).float()
|
31 |
+
return s / d
|
32 |
+
elif self.config.sentence_pooling_method == 'cls':
|
33 |
+
return hidden_state[:,0]
|
34 |
+
|
35 |
+
def encode(self, features):
|
36 |
+
if features is None:
|
37 |
+
return None
|
38 |
+
psg_out = super().forward(**features,return_dict=True)
|
39 |
+
output = self.dense_layer(psg_out.last_hidden_state)
|
40 |
+
p_reps = self.sentence_embedding(output, features['attention_mask'])
|
41 |
+
if self.config.normalized:
|
42 |
+
p_reps = torch.nn.functional.normalize(p_reps, dim=-1)
|
43 |
+
return p_reps.contiguous()
|
44 |
+
|
45 |
+
|
46 |
+
def forward(self, query: Dict[str, Tensor] = None,
|
47 |
+
passage: Dict[str, Tensor] = None, labels = None, margin = 0.5):
|
48 |
+
q_reps = self.encode(query)
|
49 |
+
p_reps = self.encode(passage)
|
50 |
+
|
51 |
+
loss = None
|
52 |
+
if labels is not None:
|
53 |
+
distances = COSINE_DISTANCE(q_reps, p_reps)
|
54 |
+
losses = 0.5 * (labels.float() * distances.pow(2) + (1 - labels).float() * F.relu(margin - distances).pow(2))
|
55 |
+
loss = losses.mean()
|
56 |
+
|
57 |
+
return EncoderOutput(
|
58 |
+
loss=loss,
|
59 |
+
)
|
60 |
+
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8b1c1b7042d6286942fd5b3de2e415e397af4e530a635e04774991d69032042d
|
3 |
+
size 1266852176
|