We are excited to introduce the gte-modernbert series of models, which are built upon the latest modernBERT pre-trained encoder-only foundation models. The gte-modernbert series models include both text embedding models and rerank models.
The gte-modernbert models demonstrates competitive performance in several text embedding and text retrieval evaluation tasks when compared to similar-scale models from the current open-source community. This includes assessments such as MTEB, LoCO, and COIR evaluation.
[!TIP]
For transformers and sentence-transformers, if your GPU supports it, the efficient Flash Attention 2 will be used automatically if you have flash_attn installed. It is not mandatory.
pip install flash_attn
Use with transformers
python
1# Requires transformers>=4.48.023import torch.nn.functional as F
4from transformers import AutoModel, AutoTokenizer
56input_texts =[7"what is the capital of China?",8"how to implement quick sort in python?",9"Beijing",10"sorting algorithms"11]1213model_path ="Alibaba-NLP/gte-modernbert-base"14tokenizer = AutoTokenizer.from_pretrained(model_path)15model = AutoModel.from_pretrained(model_path)1617# Tokenize the input texts18batch_dict = tokenizer(input_texts, max_length=8192, padding=True, truncation=True, return_tensors='pt')1920outputs = model(**batch_dict)21embeddings = outputs.last_hidden_state[:,0]2223# (Optionally) normalize embeddings24embeddings = F.normalize(embeddings, p=2, dim=1)25scores =(embeddings[:1] @ embeddings[1:].T)*10026print(scores.tolist())27# [[42.89073944091797, 71.30911254882812, 33.664554595947266]]
Use with sentence-transformers:
python
1# Requires transformers>=4.48.02from sentence_transformers import SentenceTransformer
3from sentence_transformers.util import cos_sim
45input_texts =[6"what is the capital of China?",7"how to implement quick sort in python?",8"Beijing",9"sorting algorithms"10]1112model = SentenceTransformer("Alibaba-NLP/gte-modernbert-base")13embeddings = model.encode(input_texts)14print(embeddings.shape)15# (4, 768)1617similarities = cos_sim(embeddings[0], embeddings[1:])18print(similarities)19# tensor([[0.4289, 0.7131, 0.3366]])
Use with transformers.js:
js
1// npm i @huggingface/transformers2import{ pipeline, matmul }from"@huggingface/transformers";34// Create a feature extraction pipeline5const extractor =awaitpipeline(6"feature-extraction",7"Alibaba-NLP/gte-modernbert-base",8{dtype:"fp32"},// Supported options: "fp32", "fp16", "q8", "q4", "q4f16"9);1011// Embed queries and documents12const embeddings =awaitextractor(13[14"what is the capital of China?",15"how to implement quick sort in python?",16"Beijing",17"sorting algorithms",18],19{pooling:"cls",normalize:true},20);2122// Compute similarity scores23const similarities =(awaitmatmul(embeddings.slice([0,1]), embeddings.slice([1,null]).transpose(1,0))).mul(100);24console.log(similarities.tolist());// [[42.89077377319336, 71.30916595458984, 33.66455841064453]]
The results of other models are retrieved from MTEB leaderboard. Given that all models in the gte-modernbert series have a size of less than 1B parameters, we focused exclusively on the results of models under 1B from the MTEB leaderboard.
We have open positions for Research Interns and Full-Time Researchers to join our team at Tongyi Lab.
We are seeking passionate individuals with expertise in representation learning, LLM-driven information retrieval, Retrieval-Augmented Generation (RAG), and agent-based systems.
Our team is located in the vibrant cities of Beijing and Hangzhou.
If you are driven by curiosity and eager to make a meaningful impact through your work, we would love to hear from you. Please submit your resume along with a brief introduction to dingkun.ldk@alibaba-inc.com.
Citation
If you find our paper or models helpful, feel free to give us a cite.
@inproceedings{zhang2024mgte,
title={mGTE: Generalized Long-Context Text Representation and Reranking Models for Multilingual Text Retrieval},
author={Zhang, Xin and Zhang, Yanzhao and Long, Dingkun and Xie, Wen and Dai, Ziqi and Tang, Jialong and Lin, Huan and Yang, Baosong and Xie, Pengjun and Huang, Fei and others},
booktitle={Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing: Industry Track},
pages={1393--1412},
year={2024}
}
@article{li2023towards,
title={Towards general text embeddings with multi-stage contrastive learning},
author={Li, Zehan and Zhang, Xin and Zhang, Yanzhao and Long, Dingkun and Xie, Pengjun and Zhang, Meishan},
journal={arXiv preprint arXiv:2308.03281},
year={2023}
}