File size: 9,685 Bytes
90bc49a
6a4cfc1
4e3059a
 
c1d92a7
4e3059a
 
 
 
 
 
0b0237d
4e3059a
 
 
 
 
 
 
3f7c225
90bc49a
 
c2a185c
4238759
21e5758
c2a185c
90bc49a
 
21e5758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90bc49a
 
 
 
 
 
 
 
 
 
d1f492d
90bc49a
 
 
 
 
 
 
 
 
d6d657d
90bc49a
 
 
 
 
 
 
 
 
 
 
753c6fe
90bc49a
 
 
ce10917
90bc49a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53471ec
90bc49a
 
 
 
 
 
 
 
 
 
d1f492d
90bc49a
d1f492d
 
 
 
 
 
 
 
90bc49a
 
d1f492d
90bc49a
d1f492d
90bc49a
d1f492d
 
 
90bc49a
d1f492d
 
 
 
 
 
90bc49a
 
 
 
 
 
 
 
d1f492d
90bc49a
d1f492d
 
 
 
 
 
 
 
90bc49a
 
d1f492d
 
90bc49a
d1f492d
90bc49a
 
d1f492d
 
 
90bc49a
d1f492d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90bc49a
 
53471ec
1a8a44b
 
 
53471ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90bc49a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd85f7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
license: apache-2.0
datasets:
- HIT-TMG/KaLM-embedding-pretrain-data
- KaLM-Embedding/KaLM-embedding-finetuning-data
language:
- en
- zh
base_model:
- Qwen/Qwen2.5-0.5B
- HIT-TMG/KaLM-embedding-multilingual-mini-instruct-v2
pipeline_tag: feature-extraction
library_name: sentence-transformers
tags:
- Retrieval
- STS
- Classification
- Clustering
- Reranking
- vllm
---

<!-- <p align="center">
    <img src="imgs/logo.jpg" width="800"/>
<p>
 -->
<h1 align="center">KaLM-Embedding-V2.5</h1>

<p align="center">
  <a href="https://huggingface.co/KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5">
    <img src="https://img.shields.io/badge/%F0%9F%A4%97_HuggingFace-Model-ffbd45.svg" alt="HuggingFace">
  </a>
  <a href="https://huggingface.co/spaces/nazib61/qdarnt">
    <img src="https://img.shields.io/badge/%F0%9F%A4%97_HuggingFace-Demo-90EE90?style=flat" alt="Demo">
  </a>
  <a href="https://github.com/HITsz-TMG/KaLM-Embedding">
    <img src="https://img.shields.io/badge/GitHub-Code-blue.svg?logo=github&" alt="GitHub Code">
  </a>
  <a href="https://arxiv.org/abs/2506.20923">
    <img src="https://img.shields.io/badge/Paper-KaLM--Embedding-d4333f?logo=arxiv&logoColor=white&colorA=cccccc&colorB=d4333f&style=flat" alt="Paper">
  </a>
</p>

## Short Description

**KaLM-Embedding-V2.5** is a versatile and compact embedding model, which achieves SOTA performance among models of comparable size and competes with models 3–26x larger by leveraging superior training techniques and data.
![perf](./imgs/perf.jpg)


## Model Details
- Model Size: 0.5B
- Embedding Dimension: 896
- Max Input Tokens: 32k
- MRL dimensions: 896, 512, 256, 128, and 64
- Attn: Bidirectional attention
- Pooling: Mean pooling

![archi](./imgs/archi.jpg)

## Training Recipe
- Large-scale weakly supervised pretraining
- High-quality supervised finetuning
- Contrastive distillation with fine-grained soft labels

Additionally, focal-style sample reweighting and online hard-negative mixing are employed to emphasize difficult samples and enrich hard negatives.


## 📑 Open-source Plan

- [x] Model Checkpoint 
    - [x] [KaLM-embedding-multilingual-mini-v1](https://huggingface.co/HIT-TMG/KaLM-embedding-multilingual-mini-v1)
    - [x] [KaLM-embedding-multilingual-mini-instruct-v1](https://huggingface.co/HIT-TMG/KaLM-embedding-multilingual-mini-instruct-v1)
    - [x] [KaLM-embedding-multilingual-mini-instruct-v1.5](https://huggingface.co/HIT-TMG/KaLM-embedding-multilingual-mini-instruct-v1.5)
    - [x] [KaLM-embedding-multilingual-mini-instruct-v2](https://huggingface.co/HIT-TMG/KaLM-embedding-multilingual-mini-instruct-v2)
    - [x] [KaLM-embedding-multilingual-mini-instruct-v2.5](https://huggingface.co/KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5)
    - [x] [KaLM-Embedding-Gemma3-12B-2511](https://huggingface.co/tencent/KaLM-Embedding-Gemma3-12B-2511)
- [x] Training and Evaluation Code: [HITsz-TMG/KaLM-Embedding](https://github.com/HITsz-TMG/KaLM-Embedding)
- [x] Technical Report: [KaLM-Embedding-V2: Superior Training Techniques and Data Inspire A Versatile Embedding Model](https://arxiv.org/abs/2506.20923v4)
- [x] Pre-training Data: [Pre-training Data](https://huggingface.co/datasets/HIT-TMG/KaLM-embedding-pretrain-data)
- [x] Fine-tuning Data: [Fine-tuning Data](https://huggingface.co/datasets/KaLM-Embedding/KaLM-embedding-finetuning-data)

## Evaluation
### Overall results on MTEB (cmn, v1) and MTEB (eng, v1).
![overall](./imgs/overall.jpg)

### Detailed model performance on MTEB (cmn, v1).
![mteb_cmn](./imgs/mteb_cmn.jpg)

### Detailed model performance on MTEB (eng, v1).
![mteb_cmn](./imgs/mteb_eng.jpg)

### OOD evaluation: KaLM-Embedding-V2.5 exhibits strong OOD generalization, competing with the 15x larger model in real-world retrieval scenarios.
![ood](./imgs/ood.jpg)

### Matryoshka embedding evaluation: KaLM-Embedding-V2.5 maintains robust performance with matryoshka embeddings even at smaller dimensions.
![matry](./imgs/matry.jpg)

## Requirements
Since we have used the Qwen2 model, we advise you to install `transformers>=4.37.0`, or you might encounter the following error:
```
KeyError: 'qwen2'
```

## Usage
### sentence-transformers support
Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:

```
pip install -U sentence-transformers
```

Then you can use the model like this:

```python
from sentence_transformers import SentenceTransformer
import torch

model = SentenceTransformer(
    "KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
    trust_remote_code=True,
    model_kwargs={
        "torch_dtype": torch.bfloat16,
        "attn_implementation": "flash_attention_2",  # Optional
    },
)
model.max_seq_length = 512

sentences = ["This is an example sentence", "Each sentence is converted"]
embeddings = model.encode(
    sentences,
    normalize_embeddings=True,
    batch_size=256,
    show_progress_bar=True,
)
print(embeddings)
'''
[[-0.01043701 -0.02172852  0.0100708  ... -0.02807617  0.00157166
  -0.03637695]
 [-0.00424194  0.02966309  0.03686523 ... -0.02587891  0.01953125
  -0.00125122]]
'''
```

We add task instructions for asymmetric tasks: retrieval, reranking, classification, and clustering.
And, we add task instructions for both queries and passages in symmetric tasks, including STS and pair classification.
If you want to add task instructions to the query, you can use the model like this:

```python
from sentence_transformers import SentenceTransformer
import torch

model = SentenceTransformer(
    "KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
    trust_remote_code=True,
    model_kwargs={
        "torch_dtype": torch.bfloat16,
        "attn_implementation": "flash_attention_2",  # Optional
    },
)
model.max_seq_length = 512

sentences = ["This is an example sentence", "Each sentence is converted"]
prompt = "Instruct: Classifying the category of french news.\nQuery:"
embeddings = model.encode(
    sentences,
    prompt=prompt,
    normalize_embeddings=True,
    batch_size=256,
    show_progress_bar=True,
)
print(embeddings)
'''
[[-0.01867676  0.02319336  0.00280762 ... -0.02075195  0.00196838
  -0.0703125 ]
 [-0.0067749   0.03491211  0.01434326 ... -0.0043335   0.00509644
  -0.04174805]]
'''
```

Or you can use `encode_query` and `encode_document` to automatically add the default prompt for queries (`"Instruct: Given a query, retrieve documents that answer the query \n Query: "`) and documents (`""`), respectively.

```python
from sentence_transformers import SentenceTransformer
import torch

model = SentenceTransformer(
    "KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
    trust_remote_code=True,
    model_kwargs={
        "torch_dtype": torch.bfloat16,
        "attn_implementation": "flash_attention_2",  # Optional
    },
)
model.max_seq_length = 512

queries = [
    "What is the capital of China?",
    "Explain gravity",
]
documents = [
    "The capital of China is Beijing.",
    "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
]

query_embeddings = model.encode_query(queries)
document_embeddings = model.encode_document(documents)

similarities = model.similarity(query_embeddings, document_embeddings)
print(similarities)
'''
tensor([[0.9034, 0.2563],
        [0.3153, 0.7396]])
'''
```

### vllm support
```
pip install -U vllm==0.8.5
```
```python
import torch
import vllm
from vllm import LLM
def get_detailed_instruct(task_description: str, query: str) -> str:
    return f'Instruct: {task_description}\nQuery:{query}'

task = 'Given a query, retrieve documents that answer the query'
queries = [
    get_detailed_instruct(task, 'What is the capital of China?'),
    get_detailed_instruct(task, 'Explain gravity')
]
documents = [
    "The capital of China is Beijing.",
    "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun."
]
input_texts = queries + documents

model = LLM(model="{MODEL_NAME_OR_PATH}", task="embed", trust_remote_code=True, dtype="float16")

outputs = model.embed(input_texts)
embeddings = torch.tensor([o.outputs.embedding for o in outputs])
scores = (embeddings[:2] @ embeddings[2:].T)
print(scores.tolist())
```


## Citation
If you find this model useful, please consider giving a star and citation.
```
@misc{zhao2025kalmembeddingv2,
      title={KaLM-Embedding-V2: Superior Training Techniques and Data Inspire A Versatile Embedding Model}, 
      author={Xinping Zhao and Xinshuo Hu and Zifei Shan and Shouzheng Huang and Yao Zhou and Xin Zhang and Zetian Sun and Zhenyu Liu and Dongfang Li and Xinyuan Wei and Youcheng Pan and Yang Xiang and Meishan Zhang and Haofen Wang and Jun Yu and Baotian Hu and Min Zhang},
      year={2025},
      eprint={2506.20923},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2506.20923}, 
}

@misc{hu2025kalmembedding,
      title={KaLM-Embedding: Superior Training Data Brings A Stronger Embedding Model}, 
      author={Xinshuo Hu and Zifei Shan and Xinping Zhao and Zetian Sun and Zhenyu Liu and Dongfang Li and Shaolin Ye and Xinyuan Wei and Qian Chen and Baotian Hu and Haofen Wang and Jun Yu and Min Zhang},
      year={2025},
      eprint={2501.01028},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2501.01028}, 
}
```


## Contact
If you encounter any issue, feel free to contact us via the email: <[email protected]>, <[email protected]>