Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,104 +1,20 @@
|
|
| 1 |
---
|
| 2 |
language:
|
| 3 |
- en
|
| 4 |
-
pipeline_tag: text-classification
|
| 5 |
tags:
|
| 6 |
-
-
|
|
|
|
|
|
|
| 7 |
license: apache-2.0
|
| 8 |
-
library_name: sentence-transformers
|
| 9 |
---
|
| 10 |
|
| 11 |
# Qwen2-7B-embed-base
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
## Requirements
|
| 17 |
-
The code of Qwen2 has been in the latest Hugging face transformers and we advise you to install `transformers>=4.37.0`, or you might encounter the following error:
|
| 18 |
-
```
|
| 19 |
-
KeyError: 'qwen2'
|
| 20 |
-
```
|
| 21 |
-
|
| 22 |
-
## Usage
|
| 23 |
-
The 'lm_head' layer of this model has been removed, which means it can be used for embeddings. It will not perform greatly, as it needs to be further fine-tuned, as shown by [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct).
|
| 24 |
-
The basic Sentence-Transformers implementation is working correctly. This would imply other more sophisticated embeddings techniques such as adding a custom classification head, will work correctly as well.
|
| 25 |
-
|
| 26 |
-
## Inference (sentence-transformers)
|
| 27 |
-
```python
|
| 28 |
-
from sentence_transformers import SentenceTransformer
|
| 29 |
-
import torch
|
| 30 |
-
|
| 31 |
-
# 1. Load a pretrained Sentence Transformer model
|
| 32 |
-
model = SentenceTransformer("ssmits/Qwen2-7B-embed-base") # device = "cpu" when <= 24 GB VRAM
|
| 33 |
-
|
| 34 |
-
# The sentences to encode
|
| 35 |
-
sentences = [
|
| 36 |
-
"The weather is lovely today.",
|
| 37 |
-
"It's so sunny outside!",
|
| 38 |
-
"He drove to the stadium.",
|
| 39 |
-
]
|
| 40 |
-
|
| 41 |
-
# 2. Calculate embeddings by calling model.encode()
|
| 42 |
-
embeddings = model.encode(sentences)
|
| 43 |
-
print(embeddings.shape)
|
| 44 |
-
# (3, 3584)
|
| 45 |
-
|
| 46 |
-
# 3. Calculate the embedding similarities
|
| 47 |
-
# Assuming embeddings is a numpy array, convert it to a torch tensor
|
| 48 |
-
embeddings_tensor = torch.tensor(embeddings)
|
| 49 |
-
|
| 50 |
-
# Using torch to compute cosine similarity matrix
|
| 51 |
-
similarities = torch.nn.functional.cosine_similarity(embeddings_tensor.unsqueeze(0), embeddings_tensor.unsqueeze(1), dim=2)
|
| 52 |
-
|
| 53 |
-
print(similarities)
|
| 54 |
-
# tensor([[1.0000, 0.8735, 0.7051],
|
| 55 |
-
# [0.8735, 1.0000, 0.7199],
|
| 56 |
-
# [0.7051, 0.7199, 1.0000]])
|
| 57 |
-
```
|
| 58 |
-
|
| 59 |
-
Note: In my tests it utilizes more than 24GB (RTX 4090), so an A100 or A6000 would be required for inference.
|
| 60 |
-
|
| 61 |
|
| 62 |
-
##
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
import torch
|
| 68 |
-
|
| 69 |
-
#Mean Pooling - Take attention mask into account for correct averaging
|
| 70 |
-
def mean_pooling(model_output, attention_mask):
|
| 71 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
| 72 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 73 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 74 |
-
|
| 75 |
-
# Sentences we want sentence embeddings for
|
| 76 |
-
sentences = ['This is an example sentence', 'Each sentence is converted']
|
| 77 |
-
|
| 78 |
-
# Load model from HuggingFace Hub
|
| 79 |
-
tokenizer = AutoTokenizer.from_pretrained('ssmits/Qwen2-7B-embed-base')
|
| 80 |
-
model = AutoModel.from_pretrained('ssmits/Qwen2-7B-embed-base') # device = "cpu" when <= 24 GB VRAM
|
| 81 |
-
|
| 82 |
-
# Tokenize sentences
|
| 83 |
-
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
| 84 |
-
|
| 85 |
-
# Compute token embeddings
|
| 86 |
-
with torch.no_grad():
|
| 87 |
-
model_output = model(**encoded_input)
|
| 88 |
-
|
| 89 |
-
# Perform pooling. In this case, mean pooling.
|
| 90 |
-
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 91 |
-
|
| 92 |
-
print("Sentence embeddings:")
|
| 93 |
-
print(sentence_embeddings)
|
| 94 |
-
```
|
| 95 |
-
|
| 96 |
-
### How to enable Multi-GPU
|
| 97 |
-
```python
|
| 98 |
-
from transformers import AutoModel
|
| 99 |
-
from torch.nn import DataParallel
|
| 100 |
-
|
| 101 |
-
model = AutoModel.from_pretrained("ssmits/Qwen2-7B-embed-base")
|
| 102 |
-
for module_key, module in model._modules.items():
|
| 103 |
-
model._modules[module_key] = DataParallel(module)
|
| 104 |
-
```
|
|
|
|
| 1 |
---
|
| 2 |
language:
|
| 3 |
- en
|
|
|
|
| 4 |
tags:
|
| 5 |
+
- embeddings
|
| 6 |
+
- base-model
|
| 7 |
+
- qwen
|
| 8 |
license: apache-2.0
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
# Qwen2-7B-embed-base
|
| 12 |
|
| 13 |
+
This is a base model derived from Qwen2.5-7B-Instruct with the language modeling head removed.
|
| 14 |
+
It's intended to be used as a base for embedding tasks and further fine-tuning.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
## Model Details
|
| 17 |
+
- Base model: Qwen2.5-7B-Instruct
|
| 18 |
+
- The 'lm_head' layer has been removed
|
| 19 |
+
- Maintains the original model's norm layers
|
| 20 |
+
- Suitable for embedding tasks and custom head additions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|