jamesagilesoda commited on
Commit
81bb8b7
1 Parent(s): d235ade

Upload model

Browse files
Files changed (3) hide show
  1. config.json +6 -2
  2. config.py +16 -0
  3. model.py +38 -0
config.json CHANGED
@@ -1,9 +1,13 @@
1
  {
2
  "_name_or_path": "/home/jovyan/workspace/1_user/anhdungitvn@agilesoda.ai/repo/models/jamesagilesoda/Twindoc-Mistral-7B-Alpha-Backbone-Embedding",
3
  "architectures": [
4
- "MistralModel"
5
  ],
6
  "attention_dropout": 0.0,
 
 
 
 
7
  "bos_token_id": 1,
8
  "eos_token_id": 2,
9
  "hidden_act": "silu",
@@ -11,7 +15,7 @@
11
  "initializer_range": 0.02,
12
  "intermediate_size": 14336,
13
  "max_position_embeddings": 32768,
14
- "model_type": "mistral",
15
  "num_attention_heads": 32,
16
  "num_hidden_layers": 32,
17
  "num_key_value_heads": 8,
 
1
  {
2
  "_name_or_path": "/home/jovyan/workspace/1_user/anhdungitvn@agilesoda.ai/repo/models/jamesagilesoda/Twindoc-Mistral-7B-Alpha-Backbone-Embedding",
3
  "architectures": [
4
+ "MistralForEmbeddingModel"
5
  ],
6
  "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "config.MistralForEmbeddingConfig",
9
+ "AutoModel": "model.MistralForEmbeddingModel"
10
+ },
11
  "bos_token_id": 1,
12
  "eos_token_id": 2,
13
  "hidden_act": "silu",
 
15
  "initializer_range": 0.02,
16
  "intermediate_size": 14336,
17
  "max_position_embeddings": 32768,
18
+ "model_type": "mistral_for_embedding",
19
  "num_attention_heads": 32,
20
  "num_hidden_layers": 32,
21
  "num_key_value_heads": 8,
config.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020-present the AI Algorithm Research Team.
3
+ # http://agilesoda.ai
4
+ # contact@agilesoda.ai
5
+ # Model
6
+
7
+
8
+ from transformers import PretrainedConfig
9
+ from transformers import AutoConfig
10
+
11
+
12
+ class MistralForEmbeddingConfig(PretrainedConfig):
13
+ model_type = "mistral_for_embedding"
14
+
15
+
16
+ AutoConfig.register("mistral_for_embedding", MistralForEmbeddingConfig)
model.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020-present the AI Algorithm Research Team.
3
+ # http://agilesoda.ai
4
+ # contact@agilesoda.ai
5
+ # Model
6
+
7
+
8
+ import torch
9
+ import torch.nn.functional as F
10
+ from transformers import MistralModel
11
+ from transformers import AutoModel
12
+ from .config import MistralForEmbeddingConfig
13
+
14
+
15
+ class MistralForEmbeddingModel(MistralModel):
16
+ config_class = MistralForEmbeddingConfig
17
+ def forward(self, *args, **kwargs):
18
+ outputs = super().forward(*args, **kwargs)
19
+ last_hidden_states = outputs.last_hidden_state
20
+ attention_mask = kwargs.get("attention_mask")
21
+ left_padding = torch.equal(attention_mask[:, -1], torch.ones(attention_mask.shape[0], dtype=torch.int64))
22
+
23
+ if left_padding: # -1 is the last token
24
+ output_embeddings = last_hidden_states[:, -1]
25
+ else: # find the last token
26
+ sequence_lengths = attention_mask.sum(dim=1) - 1
27
+ batch_size = last_hidden_states.shape[0]
28
+ output_embeddings = last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
29
+
30
+ # It should be performed outside of this model because of batching.
31
+ # output_embeddings = F.normalize(embeddings, p=2, dim=1)
32
+ # scores = (embeddings[:2] @ embeddings[2:].T) * 100
33
+ # scores = scores.tolist()
34
+
35
+ return output_embeddings
36
+
37
+
38
+ AutoModel.register(MistralForEmbeddingConfig, MistralForEmbeddingModel)