izhx commited on
Commit
6c5448f
·
verified ·
1 Parent(s): 63ebe3e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +216 -3
README.md CHANGED
@@ -1,3 +1,216 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ pipeline_tag: sentence-similarity
4
+ ---
5
+
6
+ # Lychee Embed
7
+
8
+
9
+ `Lychee-embed` is the latest generalist text embedding model developed based on the `Qwen2.5` basic model. It is suitable for text retrieval (semantic correlation), text similarity and other downstream tasks, and supports multiple languages of `Qwen2.5`. `Lychee-embed` is jointly developed by the NLP Team of Harbin Institute of Technology, Shenzhen and is built based on an innovative multi-stage training framework (warm-up, task-learning, model merging, annealing).
10
+
11
+ ![The multi-stage training framework](assets/framework-crop.png)
12
+
13
+
14
+ **Lychee-embed**:
15
+
16
+ - Model Type: Text Embedding
17
+ - Language Support: 100+ Languages
18
+ - Param Size: 1.5B
19
+ - Context Length: 8k
20
+ - Embedding Dim: 1536, Supports diverse settings with 32 steps from 32 to 1536
21
+ - Model Precision: BF16
22
+
23
+ For more details, please refer to our [paper](assets/colm25-paper.pdf).
24
+
25
+
26
+ ### Model List
27
+
28
+ | Model Type | Models | Size | Layers | Sequence Length | Embedding Dimension | MRL Support | Instruction Aware |
29
+ |------------------|----------------------|------|--------|-----------------|---------------------|-------------|----------------|
30
+ | Text Embedding | [lychee-embed](https://huggingface.co/vec-ai/lychee-embed) | 1.5B | 28 | 8K | 1636 | Yes | Yes |
31
+ | Text Reranking | [lychee-rerank](https://huggingface.co/vec-ai/lychee-rerank) | 1.5B | 28 | 8K | - | - | Yes |
32
+
33
+
34
+ > **Note**:
35
+ > - `MRL Support` indicates whether the embedding model supports custom dimensions for the final embedding.
36
+ > - `Instruction Aware` notes whether the embedding or reranking model supports customizing the input instruction according to different tasks.
37
+ > - Like most embedding models, for most downstream tasks, using instructions (instruct) typically yields an improvement of 1% to 5% compared to not using them. Therefore, we recommend that developers create tailored instructions specific to their tasks and scenarios. In multilingual contexts, we also advise users to write their instructions in English, as most instructions utilized during the model training process were originally written in English.
38
+
39
+
40
+ ## Model Usage
41
+
42
+ 📌 **Tips**: We recommend that developers customize the `instruct` according to their specific scenarios, tasks, and languages. Our tests have shown that in most retrieval scenarios, not using an `instruct` on the `query` side can lead to a drop in retrieval performance by approximately 1% to 5%.
43
+
44
+
45
+ ### Sentence Transformers Usage
46
+
47
+ ```python
48
+ # Requires transformers>=4.51.0
49
+ # Requires sentence-transformers>=2.7.0
50
+
51
+ from sentence_transformers import SentenceTransformer
52
+
53
+ # Load the model
54
+ model = SentenceTransformer("vec-ai/lychee-embed")
55
+
56
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving,
57
+ # together with setting `padding_side` to "left":
58
+ # model = SentenceTransformer(
59
+ # "vec-ai/lychee-embed",
60
+ # model_kwargs={"attn_implementation": "flash_attention_2", "device_map": "auto"},
61
+ # tokenizer_kwargs={"padding_side": "left"},
62
+ # )
63
+
64
+ # The queries and documents to embed
65
+ queries = [
66
+ "What is the capital of China?",
67
+ "Explain gravity",
68
+ ]
69
+ documents = [
70
+ "The capital of China is Beijing.",
71
+ "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.",
72
+ ]
73
+
74
+ # Encode the queries and documents. Note that queries benefit from using a prompt
75
+ # Here we use the prompt called "query" stored under `model.prompts`, but you can
76
+ # also pass your own prompt via the `prompt` argument
77
+ query_embeddings = model.encode(queries, prompt_name="query")
78
+ document_embeddings = model.encode(documents)
79
+
80
+ # Compute the (cosine) similarity between the query and document embeddings
81
+ similarity = model.similarity(query_embeddings, document_embeddings)
82
+ print(similarity)
83
+ # tensor([[0.8952, 0.4001],
84
+ # [0.4668, 0.8334]])
85
+ ```
86
+
87
+ ### Transformers Usage
88
+
89
+ ```python
90
+ # Requires transformers>=4.51.0
91
+
92
+ import torch
93
+ from transformers import AutoTokenizer, AutoModel
94
+
95
+
96
+ def last_token_pool(last_hidden_states: torch.Tensor,
97
+ attention_mask: torch.Tensor) -> torch.Tensor:
98
+ left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
99
+ if left_padding:
100
+ return last_hidden_states[:, -1]
101
+ else:
102
+ sequence_lengths = attention_mask.sum(dim=1) - 1
103
+ batch_size = last_hidden_states.shape[0]
104
+ return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
105
+
106
+
107
+ def get_detailed_instruct(task_description: str, query: str) -> str:
108
+ return f'Instruct: {task_description}\nQuery:{query}'
109
+
110
+ # Each query must come with a one-sentence instruction that describes the task
111
+ task = 'Given a web search query, retrieve relevant passages that answer the query'
112
+
113
+ queries = [
114
+ get_detailed_instruct(task, 'What is the capital of China?'),
115
+ get_detailed_instruct(task, 'Explain gravity')
116
+ ]
117
+ # No need to add instruction for retrieval documents
118
+ documents = [
119
+ "The capital of China is Beijing.",
120
+ "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."
121
+ ]
122
+ input_texts = queries + documents
123
+
124
+ tokenizer = AutoTokenizer.from_pretrained('vec-ai/lychee-embed', padding_side='left')
125
+ model = AutoModel.from_pretrained('vec-ai/lychee-embed')
126
+
127
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving.
128
+ # model = AutoModel.from_pretrained('vec-ai/lychee-embed', attn_implementation="flash_attention_2", torch_dtype=torch.float16).cuda()
129
+
130
+ max_length = 8192
131
+
132
+ # Tokenize the input texts
133
+ batch_dict = tokenizer(
134
+ input_texts,
135
+ padding=True,
136
+ truncation=True,
137
+ max_length=max_length,
138
+ return_tensors="pt",
139
+ )
140
+ batch_dict.to(model.device)
141
+ outputs = model(**batch_dict)
142
+ embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
143
+
144
+ # normalize embeddings
145
+ embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)
146
+ scores = (embeddings[:2] @ embeddings[2:].T)
147
+ print(scores.tolist())
148
+ # [[0.8952088952064514, 0.40010833740234375], [0.4668009877204895, 0.8333653807640076]]
149
+ ```
150
+
151
+ ### vLLM Usage
152
+
153
+ ```python
154
+ # Requires vllm>=0.8.5
155
+ import torch
156
+ from vllm import LLM
157
+
158
+ def get_detailed_instruct(task_description: str, query: str) -> str:
159
+ return f'Instruct: {task_description}\nQuery:{query}'
160
+
161
+ # Each query must come with a one-sentence instruction that describes the task
162
+ task = 'Given a web search query, retrieve relevant passages that answer the query'
163
+
164
+ queries = [
165
+ get_detailed_instruct(task, 'What is the capital of China?'),
166
+ get_detailed_instruct(task, 'Explain gravity')
167
+ ]
168
+ # No need to add instruction for retrieval documents
169
+ documents = [
170
+ "The capital of China is Beijing.",
171
+ "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."
172
+ ]
173
+ input_texts = queries + documents
174
+
175
+ model = LLM(model="vec-ai/lychee-embed", task="embed")
176
+
177
+ outputs = model.embed(input_texts)
178
+ embeddings = torch.tensor([o.outputs.embedding for o in outputs])
179
+ scores = (embeddings[:2] @ embeddings[2:].T)
180
+ print(scores.tolist())
181
+ # [[0.9007290601730347, 0.4043760895729065], [0.469818651676178, 0.8317853212356567]]
182
+ ```
183
+
184
+
185
+ ## Evaluation
186
+
187
+ | Model | Param | MTEB | CMTEB | MMTEB | MLDR | MTEB-Code | ToolBench | FollowIR | BRIGHT |
188
+ |---|---|---|---|---|---|---|---|---|---|
189
+ | BGE-multilingual | 9.24B | 69.88 | 68.44 | 61.25 | 49.10 | 62.04 | 63.65 | -2.13 | 17.68 |
190
+ | NV-Embed-v2 | 7.85B | 72.31 | - | 56.25 | - | 63.74 | 50.54 | 1.04 | 19.28 |
191
+ | GritLM-7B | 7.24B | 66.8 | - | 60.93 | - | 73.6$^\sigma$ | 35.42 | 3.45 | 20.63 |
192
+ | E5-mistral | 7.11B | 66.6 | 59.92 | 60.28 | - | 69.2$^\sigma$ | 31.79 | -0.62 | 17.54 |
193
+ | GTE-Qwen2-7B | 7.62B | 69.88 | 71.62 | 62.51 | 56.53 | 62.17 | 59.48 | 4.94 | 22.89 |
194
+ | GTE-Qwen2-1.5B | 1.54B | 67.19 | 67.12 | 59.47 | 52.11 | 61.98 | 62.57 | 0.74 | 18.47 |
195
+ | BGE-M3 (Dense) | 0.56B | 59.84 | 61.79 | 59.54 | 52.50 | 58.22 | 58.45 | -3.11 | 11.94 |
196
+ | Jina-v3 | 0.57B | 65.52 | 63.07 | 58.37 | 40.71 | 58.85 | 59.64 | -1.34 | 11.34 |
197
+ |Qwen3-Embedding-8B | 7.57B | | 73.84 | 70.58 | | 80.68 |
198
+ |Qwen3-Embedding-4B | 4.02B | | 72.27 | 69.45 | | 80.06 |
199
+ |Qwen3-Embedding-0.6B | 0.60B | | 66.33 | 64.33 | | 75.41 |
200
+ | **Lychee-embed** | 1.54B | 68.39 |69.77 | 58.43 | 53.85 | 72.54 | 86.35 | 5.74 | 19.47 |
201
+
202
+ For more details, please refer to our [paper](assets/colm25-paper.pdf).
203
+
204
+ ## Citation
205
+
206
+ If you find our work helpful, feel free to give us a cite.
207
+
208
+ ```
209
+ @inproceedings{zhang2025phased,
210
+ title={Phased Training for LLM-powered Text Retrieval Models Beyond Data Scaling},
211
+ author={Xin Zhang and Yanzhao Zhang and Wen Xie and Dingkun Long and Mingxin Li and Pengjun Xie and Meishan Zhang and Wenjie Li and Min Zhang},
212
+ booktitle={Second Conference on Language Modeling},
213
+ year={2025},
214
+ url={https://openreview.net/forum?id=NC6G1KCxlt}
215
+ }
216
+ ```