Sentence Similarity
sentence-transformers
Safetensors
Transformers
gte
feature-extraction
mteb
multilingual
text-embeddings-inference
Eval Results (legacy)
Instructions to use Alibaba-NLP/gte-multilingual-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use Alibaba-NLP/gte-multilingual-base with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("Alibaba-NLP/gte-multilingual-base") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Transformers
How to use Alibaba-NLP/gte-multilingual-base with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("Alibaba-NLP/gte-multilingual-base") model = AutoModel.from_pretrained("Alibaba-NLP/gte-multilingual-base", device_map="auto") - Inference
- Notebooks
- Google Colab
- Kaggle
Commit ·
6dde5e2
1
Parent(s): 9bbca17
feat(transformers): Add native Transformers config (#31)
Browse files- Set model_type and add rope_parameters (3a27607e6a68d1ea949a635b70c7ca4796b8f574)
- Add clean native GteModel/GteForTokenClassification config and update README & script (807b7d1e16f23a472ce7c29377e13c35f427ae2c)
Co-authored-by: Harshal Janjani <harshaljanjani@users.noreply.huggingface.co>
- README.md +10 -7
- config.json +10 -23
- scripts/gte_embedding.py +6 -3
README.md
CHANGED
|
@@ -4633,6 +4633,11 @@ model-index:
|
|
| 4633 |
|
| 4634 |
## gte-multilingual-base
|
| 4635 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4636 |
The **gte-multilingual-base** model is the latest in the [GTE](https://huggingface.co/collections/Alibaba-NLP/gte-models-6680f0b13f885cb431e6d469) (General Text Embedding) family of models, featuring several key attributes:
|
| 4637 |
|
| 4638 |
- **High Performance**: Achieves state-of-the-art (SOTA) results in multilingual retrieval tasks and multi-task representation model evaluations when compared to models of similar size.
|
|
@@ -4653,16 +4658,14 @@ The **gte-multilingual-base** model is the latest in the [GTE](https://huggingfa
|
|
| 4653 |
|
| 4654 |
## Usage
|
| 4655 |
|
| 4656 |
-
-
|
| 4657 |
-
refer to [enable-unpadding-and-xformers](https://huggingface.co/Alibaba-NLP/new-impl#recommendation-enable-unpadding-and-acceleration-with-xformers).**
|
| 4658 |
-
- **How to use it offline: [new-impl/discussions/2](https://huggingface.co/Alibaba-NLP/new-impl/discussions/2#662b08d04d8c3d0a09c88fa3)**
|
| 4659 |
- **How to use with [TEI](https://github.com/huggingface/text-embeddings-inference): [refs/pr/7](https://huggingface.co/Alibaba-NLP/gte-multilingual-base/discussions/7#66bfb82ea03b764ca92a2221)**
|
| 4660 |
|
| 4661 |
|
| 4662 |
|
| 4663 |
### Get Dense Embeddings with Transformers
|
| 4664 |
```python
|
| 4665 |
-
# Requires transformers>=
|
| 4666 |
|
| 4667 |
import torch.nn.functional as F
|
| 4668 |
from transformers import AutoModel, AutoTokenizer
|
|
@@ -4676,7 +4679,7 @@ input_texts = [
|
|
| 4676 |
|
| 4677 |
model_name_or_path = 'Alibaba-NLP/gte-multilingual-base'
|
| 4678 |
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
|
| 4679 |
-
model = AutoModel.from_pretrained(model_name_or_path
|
| 4680 |
|
| 4681 |
# Tokenize the input texts
|
| 4682 |
batch_dict = tokenizer(input_texts, max_length=8192, padding=True, truncation=True, return_tensors='pt')
|
|
@@ -4695,7 +4698,7 @@ print(scores.tolist())
|
|
| 4695 |
|
| 4696 |
### Use with sentence-transformers
|
| 4697 |
```python
|
| 4698 |
-
# Requires sentence-transformers>=3.0.0
|
| 4699 |
|
| 4700 |
from sentence_transformers import SentenceTransformer
|
| 4701 |
|
|
@@ -4707,7 +4710,7 @@ input_texts = [
|
|
| 4707 |
]
|
| 4708 |
|
| 4709 |
model_name_or_path="Alibaba-NLP/gte-multilingual-base"
|
| 4710 |
-
model = SentenceTransformer(model_name_or_path
|
| 4711 |
embeddings = model.encode(input_texts, normalize_embeddings=True) # embeddings.shape (4, 768)
|
| 4712 |
|
| 4713 |
# sim scores
|
|
|
|
| 4633 |
|
| 4634 |
## gte-multilingual-base
|
| 4635 |
|
| 4636 |
+
> [!NOTE]
|
| 4637 |
+
> This model now works natively with Hugging Face `transformers` (`transformers>=5.18.0`) without `trust_remote_code=True`. The original model weights and tokenizer are unchanged.
|
| 4638 |
+
>
|
| 4639 |
+
> Native GTE support was added to `transformers` in [huggingface/transformers#48416](https://github.com/huggingface/transformers/pull/48416) with the initial Hub PR by [@harshaljanjani](https://huggingface.co/harshaljanjani). For older versions (`transformers<5.18.0`), pin `revision="9bbca17"` with `trust_remote_code=True`.
|
| 4640 |
+
|
| 4641 |
The **gte-multilingual-base** model is the latest in the [GTE](https://huggingface.co/collections/Alibaba-NLP/gte-models-6680f0b13f885cb431e6d469) (General Text Embedding) family of models, featuring several key attributes:
|
| 4642 |
|
| 4643 |
- **High Performance**: Achieves state-of-the-art (SOTA) results in multilingual retrieval tasks and multi-task representation model evaluations when compared to models of similar size.
|
|
|
|
| 4658 |
|
| 4659 |
## Usage
|
| 4660 |
|
| 4661 |
+
- With native `transformers>=5.18.0`, PyTorch SDPA and Flash Attention 2 work out of the box without custom code. (For legacy `transformers<5.18.0` via `revision="9bbca17"`, see [enable-unpadding-and-xformers](https://huggingface.co/Alibaba-NLP/new-impl#recommendation-enable-unpadding-and-acceleration-with-xformers) and [offline usage](https://huggingface.co/Alibaba-NLP/new-impl/discussions/2#662b08d04d8c3d0a09c88fa3).)
|
|
|
|
|
|
|
| 4662 |
- **How to use with [TEI](https://github.com/huggingface/text-embeddings-inference): [refs/pr/7](https://huggingface.co/Alibaba-NLP/gte-multilingual-base/discussions/7#66bfb82ea03b764ca92a2221)**
|
| 4663 |
|
| 4664 |
|
| 4665 |
|
| 4666 |
### Get Dense Embeddings with Transformers
|
| 4667 |
```python
|
| 4668 |
+
# Requires transformers>=5.18.0
|
| 4669 |
|
| 4670 |
import torch.nn.functional as F
|
| 4671 |
from transformers import AutoModel, AutoTokenizer
|
|
|
|
| 4679 |
|
| 4680 |
model_name_or_path = 'Alibaba-NLP/gte-multilingual-base'
|
| 4681 |
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
|
| 4682 |
+
model = AutoModel.from_pretrained(model_name_or_path)
|
| 4683 |
|
| 4684 |
# Tokenize the input texts
|
| 4685 |
batch_dict = tokenizer(input_texts, max_length=8192, padding=True, truncation=True, return_tensors='pt')
|
|
|
|
| 4698 |
|
| 4699 |
### Use with sentence-transformers
|
| 4700 |
```python
|
| 4701 |
+
# Requires sentence-transformers>=3.0.0 and transformers>=5.18.0
|
| 4702 |
|
| 4703 |
from sentence_transformers import SentenceTransformer
|
| 4704 |
|
|
|
|
| 4710 |
]
|
| 4711 |
|
| 4712 |
model_name_or_path="Alibaba-NLP/gte-multilingual-base"
|
| 4713 |
+
model = SentenceTransformer(model_name_or_path)
|
| 4714 |
embeddings = model.encode(input_texts, normalize_embeddings=True) # embeddings.shape (4, 768)
|
| 4715 |
|
| 4716 |
# sim scores
|
config.json
CHANGED
|
@@ -1,43 +1,30 @@
|
|
| 1 |
{
|
| 2 |
"architectures": [
|
| 3 |
-
"
|
| 4 |
-
"
|
| 5 |
],
|
| 6 |
"attention_probs_dropout_prob": 0.0,
|
| 7 |
-
"auto_map": {
|
| 8 |
-
"AutoConfig": "Alibaba-NLP/new-impl--configuration.NewConfig",
|
| 9 |
-
"AutoModelForMaskedLM": "Alibaba-NLP/new-impl--modeling.NewForMaskedLM",
|
| 10 |
-
"AutoModel": "Alibaba-NLP/new-impl--modeling.NewModel",
|
| 11 |
-
"AutoModelForMultipleChoice": "Alibaba-NLP/new-impl--modeling.NewForMultipleChoice",
|
| 12 |
-
"AutoModelForQuestionAnswering": "Alibaba-NLP/new-impl--modeling.NewForQuestionAnswering",
|
| 13 |
-
"AutoModelForSequenceClassification": "Alibaba-NLP/new-impl--modeling.NewForSequenceClassification",
|
| 14 |
-
"AutoModelForTokenClassification": "Alibaba-NLP/new-impl--modeling.NewForTokenClassification"
|
| 15 |
-
},
|
| 16 |
"classifier_dropout": 0.0,
|
|
|
|
| 17 |
"hidden_act": "gelu",
|
| 18 |
"hidden_dropout_prob": 0.1,
|
| 19 |
"hidden_size": 768,
|
| 20 |
"initializer_range": 0.02,
|
| 21 |
"intermediate_size": 3072,
|
| 22 |
"layer_norm_eps": 1e-12,
|
| 23 |
-
"layer_norm_type": "layer_norm",
|
| 24 |
"max_position_embeddings": 8192,
|
| 25 |
-
"model_type": "
|
| 26 |
"num_attention_heads": 12,
|
| 27 |
"num_hidden_layers": 12,
|
| 28 |
"num_labels": 1,
|
| 29 |
-
"pack_qkv": true,
|
| 30 |
"pad_token_id": 1,
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
},
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"transformers_version": "4.39.1",
|
| 39 |
"type_vocab_size": 1,
|
| 40 |
-
"unpad_inputs": false,
|
| 41 |
-
"use_memory_efficient_attention": false,
|
| 42 |
"vocab_size": 250048
|
| 43 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"architectures": [
|
| 3 |
+
"GteModel",
|
| 4 |
+
"GteForTokenClassification"
|
| 5 |
],
|
| 6 |
"attention_probs_dropout_prob": 0.0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"classifier_dropout": 0.0,
|
| 8 |
+
"dtype": "float16",
|
| 9 |
"hidden_act": "gelu",
|
| 10 |
"hidden_dropout_prob": 0.1,
|
| 11 |
"hidden_size": 768,
|
| 12 |
"initializer_range": 0.02,
|
| 13 |
"intermediate_size": 3072,
|
| 14 |
"layer_norm_eps": 1e-12,
|
|
|
|
| 15 |
"max_position_embeddings": 8192,
|
| 16 |
+
"model_type": "gte",
|
| 17 |
"num_attention_heads": 12,
|
| 18 |
"num_hidden_layers": 12,
|
| 19 |
"num_labels": 1,
|
|
|
|
| 20 |
"pad_token_id": 1,
|
| 21 |
+
"rope_parameters": {
|
| 22 |
+
"factor": 1.0671404006768237,
|
| 23 |
+
"rope_theta": 160000.0,
|
| 24 |
+
"rope_type": "linear"
|
| 25 |
},
|
| 26 |
+
"tie_word_embeddings": true,
|
| 27 |
+
"transformers_version": "5.23.0",
|
|
|
|
| 28 |
"type_vocab_size": 1,
|
|
|
|
|
|
|
| 29 |
"vocab_size": 250048
|
| 30 |
}
|
scripts/gte_embedding.py
CHANGED
|
@@ -35,7 +35,7 @@ class GTEEmbeddidng(torch.nn.Module):
|
|
| 35 |
self.use_fp16 = use_fp16
|
| 36 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 37 |
self.model = AutoModelForTokenClassification.from_pretrained(
|
| 38 |
-
model_name,
|
| 39 |
)
|
| 40 |
self.vocab_size = self.model.config.vocab_size
|
| 41 |
self.model.to(self.device)
|
|
@@ -92,11 +92,14 @@ class GTEEmbeddidng(torch.nn.Module):
|
|
| 92 |
|
| 93 |
text_input = self.tokenizer(texts, padding=True, truncation=True, return_tensors='pt', max_length=max_length)
|
| 94 |
text_input = {k: v.to(self.model.device) for k,v in text_input.items()}
|
| 95 |
-
model_out = self.model(**text_input, return_dict=True)
|
| 96 |
|
| 97 |
output = {}
|
| 98 |
if return_dense:
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
| 100 |
if self.normalized:
|
| 101 |
dense_vecs = torch.nn.functional.normalize(dense_vecs, dim=-1)
|
| 102 |
output['dense_embeddings'] = dense_vecs
|
|
|
|
| 35 |
self.use_fp16 = use_fp16
|
| 36 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 37 |
self.model = AutoModelForTokenClassification.from_pretrained(
|
| 38 |
+
model_name, dtype=torch.float16 if self.use_fp16 else torch.float32
|
| 39 |
)
|
| 40 |
self.vocab_size = self.model.config.vocab_size
|
| 41 |
self.model.to(self.device)
|
|
|
|
| 92 |
|
| 93 |
text_input = self.tokenizer(texts, padding=True, truncation=True, return_tensors='pt', max_length=max_length)
|
| 94 |
text_input = {k: v.to(self.model.device) for k,v in text_input.items()}
|
| 95 |
+
model_out = self.model(**text_input, return_dict=True, output_hidden_states=return_dense)
|
| 96 |
|
| 97 |
output = {}
|
| 98 |
if return_dense:
|
| 99 |
+
last_hidden_state = getattr(model_out, "last_hidden_state", None)
|
| 100 |
+
if last_hidden_state is None:
|
| 101 |
+
last_hidden_state = model_out.hidden_states[-1]
|
| 102 |
+
dense_vecs = last_hidden_state[:, 0, :dimension]
|
| 103 |
if self.normalized:
|
| 104 |
dense_vecs = torch.nn.functional.normalize(dense_vecs, dim=-1)
|
| 105 |
output['dense_embeddings'] = dense_vecs
|