Views
No views yet
pip install hf-hub-ctranslate2>=2.12.0 ctranslate2>=3.17.11# from transformers import AutoTokenizer
2model_name = "michaelfeil/ct2fast-gte-base"
3model_name_orig="thenlper/gte-base"
4
5from hf_hub_ctranslate2 import EncoderCT2fromHfHub
6model = EncoderCT2fromHfHub(
7 # load in int8 on CUDA
8 model_name_or_path=model_name,
9 device="cuda",
10 compute_type="int8_float16"
11)
12outputs = model.generate(
13 text=["I like soccer", "I like tennis", "The eiffel tower is in Paris"],
14 max_length=64,
15) # perform downstream tasks on outputs
16outputs["pooler_output"]
17outputs["last_hidden_state"]
18outputs["attention_mask"]
19
20# alternative, use SentenceTransformer Mix-In
21# for end-to-end Sentence embeddings generation
22# (not pulling from this CT2fast-HF repo)
23
24from hf_hub_ctranslate2 import CT2SentenceTransformer
25model = CT2SentenceTransformer(
26 model_name_orig, compute_type="int8_float16", device="cuda"
27)
28embeddings = model.encode(
29 ["I like soccer", "I like tennis", "The eiffel tower is in Paris"],
30 batch_size=32,
31 convert_to_numpy=True,
32 normalize_embeddings=True,
33)
34print(embeddings.shape, embeddings)
35scores = (embeddings @ embeddings.T) * 100
36
37# Hint: you can also host this code via REST API and
38# via github.com/michaelfeil/infinity
39
40compute_type=int8_float16 for device="cuda"compute_type=int8 for device="cpu"LLama-2 -> removed <pad> token.| Model Name | Model Size (GB) | Dimension | Sequence Length | Average (56) | Clustering (11) | Pair Classification (3) | Reranking (4) | Retrieval (15) | STS (10) | Summarization (1) | Classification (12) |
|---|---|---|---|---|---|---|---|---|---|---|---|
| gte-large | 0.67 | 1024 | 512 | 63.13 | 46.84 | 85.00 | 59.13 | 52.22 | 83.35 | 31.66 | 73.33 |
| gte-base | 0.22 | 768 | 512 | 62.39 | 46.2 | 84.57 | 58.61 | 51.14 | 82.3 | 31.17 | 73.01 |
| e5-large-v2 | 1.34 | 1024 | 512 | 62.25 | 44.49 | 86.03 | 56.61 | 50.56 | 82.05 | 30.19 | 75.24 |
| e5-base-v2 | 0.44 | 768 | 512 | 61.5 | 43.80 | 85.73 | 55.91 | 50.29 | 81.05 | 30.28 | 73.84 |
| gte-small | 0.07 | 384 | 512 | 61.36 | 44.89 | 83.54 | 57.7 | 49.46 | 82.07 | 30.42 | 72.31 |
| text-embedding-ada-002 | - | 1536 | 8192 | 60.99 | 45.9 | 84.89 | 56.32 | 49.25 | 80.97 | 30.8 | 70.93 |
| e5-small-v2 | 0.13 | 384 | 512 | 59.93 | 39.92 | 84.67 | 54.32 | 49.04 | 80.39 | 31.16 | 72.94 |
| sentence-t5-xxl | 9.73 | 768 | 512 | 59.51 | 43.72 | 85.06 | 56.42 | 42.24 | 82.63 | 30.08 | 73.42 |
| all-mpnet-base-v2 | 0.44 | 768 | 514 | 57.78 | 43.69 | 83.04 | 59.36 | 43.81 | 80.28 | 27.49 | 65.07 |
| sgpt-bloom-7b1-msmarco | 28.27 | 4096 | 2048 | 57.59 | 38.93 | 81.9 | 55.65 | 48.22 | 77.74 | 33.6 | 66.19 |
| all-MiniLM-L12-v2 | 0.13 | 384 | 512 | 56.53 | 41.81 | 82.41 | 58.44 | 42.69 | 79.8 | 27.9 | 63.21 |
| all-MiniLM-L6-v2 | 0.09 | 384 | 512 | 56.26 | 42.35 | 82.37 | 58.04 | 41.95 | 78.9 | 30.81 | 63.05 |
| contriever-base-msmarco | 0.44 | 768 | 512 | 56.00 | 41.1 | 82.54 | 53.14 | 41.88 | 76.51 | 30.36 | 66.68 |
| sentence-t5-base | 0.22 | 768 | 512 | 55.27 | 40.21 | 85.18 | 53.09 | 33.63 | 81.14 | 31.39 | 69.81 |
1import torch.nn.functional as F
2from torch import Tensor
3from transformers import AutoTokenizer, AutoModel
4
5def average_pool(last_hidden_states: Tensor,
6 attention_mask: Tensor) -> Tensor:
7 last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
8 return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
9
10input_texts = [
11 "what is the capital of China?",
12 "how to implement quick sort in python?",
13 "Beijing",
14 "sorting algorithms"
15]
16
17tokenizer = AutoTokenizer.from_pretrained("thenlper/gte-base")
18model = AutoModel.from_pretrained("thenlper/gte-base")
19
20# Tokenize the input texts
21batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
22
23outputs = model(**batch_dict)
24embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
25
26# (Optionally) normalize embeddings
27embeddings = F.normalize(embeddings, p=2, dim=1)
28scores = (embeddings[:1] @ embeddings[1:].T) * 100
29print(scores.tolist())1from sentence_transformers import SentenceTransformer
2from sentence_transformers.util import cos_sim
3
4sentences = ['That is a happy person', 'That is a very happy person']
5
6model = SentenceTransformer('thenlper/gte-base')
7embeddings = model.encode(sentences)
8print(cos_sim(embeddings[0], embeddings[1]))@misc{li2023general,
title={Towards General Text Embeddings with Multi-stage Contrastive Learning},
author={Zehan Li and Xin Zhang and Yanzhao Zhang and Dingkun Long and Pengjun Xie and Meishan Zhang},
year={2023},
eprint={2308.03281},
archivePrefix={arXiv},
primaryClass={cs.CL}
}