Upload folder using huggingface_hub
Browse files
README.md
CHANGED
@@ -10,87 +10,91 @@ This is the senior data synthesis model of SPEED.
|
|
10 |
|
11 |
## Usage
|
12 |
|
13 |
-
Below is an example to synthesize classification data using this senior generator.
|
|
|
|
|
14 |
|
15 |
### Transformers
|
16 |
|
17 |
```python
|
18 |
import torch
|
19 |
-
import
|
|
|
|
|
|
|
|
|
20 |
|
21 |
from torch import Tensor
|
22 |
-
from transformers import AutoTokenizer,
|
23 |
-
|
24 |
-
|
25 |
-
def last_token_pool(last_hidden_states: Tensor,
|
26 |
-
attention_mask: Tensor) -> Tensor:
|
27 |
-
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
28 |
-
if left_padding:
|
29 |
-
return last_hidden_states[:, -1]
|
30 |
-
else:
|
31 |
-
sequence_lengths = attention_mask.sum(dim=1) - 1
|
32 |
-
batch_size = last_hidden_states.shape[0]
|
33 |
-
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
34 |
|
|
|
|
|
35 |
|
36 |
-
def get_detailed_instruct(task_description: str, query: str) -> str:
|
37 |
-
return f'Instruct: {task_description}\nQuery: {query}'
|
38 |
|
|
|
|
|
|
|
39 |
|
40 |
# Each query must come with a one-sentence instruction that describes the task
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
get_detailed_instruct(task, 'summit define')
|
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 |
-
## MTEB Benchmark Evaluation
|
70 |
-
|
71 |
-
Check out [unilm/e5](https://github.com/microsoft/unilm/tree/master/e5) to reproduce evaluation results
|
72 |
-
on the [BEIR](https://arxiv.org/abs/2104.08663) and [MTEB benchmark](https://arxiv.org/abs/2210.07316).
|
73 |
-
|
74 |
-
## FAQ
|
75 |
-
|
76 |
-
**1. Do I need to add instructions to the query?**
|
77 |
-
|
78 |
-
Yes, this is how the model is trained, otherwise you will see a performance degradation.
|
79 |
-
The task definition should be a one-sentence instruction that describes the task.
|
80 |
-
This is a way to customize text embeddings for different scenarios through natural language instructions.
|
81 |
-
|
82 |
-
Please check out [unilm/e5/utils.py](https://github.com/microsoft/unilm/blob/9c0f1ff7ca53431fe47d2637dfe253643d94185b/e5/utils.py#L106) for instructions we used for evaluation.
|
83 |
-
|
84 |
-
On the other hand, there is no need to add instructions to the document side.
|
85 |
-
|
86 |
-
**2. Why are my reproduced results slightly different from reported in the model card?**
|
87 |
-
|
88 |
-
Different versions of `transformers` and `pytorch` could cause negligible but non-zero performance differences.
|
89 |
-
|
90 |
-
**3. Where are the LoRA-only weights?**
|
91 |
-
|
92 |
-
You can find the LoRA-only weights at [https://huggingface.co/Haon-Chen/speed-embedding-7b-instruct/tree/main/lora](https://huggingface.co/Haon-Chen/speed-embedding-7b-instruct/tree/main/lora).
|
93 |
-
|
94 |
## Citation
|
95 |
|
96 |
If you find our paper or models helpful, please consider cite as follows:
|
@@ -105,5 +109,3 @@ If you find our paper or models helpful, please consider cite as follows:
|
|
105 |
```
|
106 |
|
107 |
## Limitations
|
108 |
-
|
109 |
-
Using this model for inputs longer than 4096 tokens is not recommended.
|
|
|
10 |
|
11 |
## Usage
|
12 |
|
13 |
+
Below is an example to synthesize classification data using this senior generator.
|
14 |
+
|
15 |
+
The prompts and misc scripts can be found in our [github page](https://github.com/haon-chen/SPEED)
|
16 |
|
17 |
### Transformers
|
18 |
|
19 |
```python
|
20 |
import torch
|
21 |
+
import os
|
22 |
+
import random
|
23 |
+
import numpy as np
|
24 |
+
import json
|
25 |
+
import re
|
26 |
|
27 |
from torch import Tensor
|
28 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
from prompts_synthesis import get_create_classify_data_prompt
|
31 |
+
from utils import fix_common_json_errors_and_loads
|
32 |
|
|
|
|
|
33 |
|
34 |
+
LLAMA3_PROMPT = """
|
35 |
+
{prompt} [/INST]
|
36 |
+
""".strip("\n")
|
37 |
|
38 |
# Each query must come with a one-sentence instruction that describes the task
|
39 |
+
tasks = [
|
40 |
+
'Identify the intended age group for educational technology products.',
|
41 |
+
'Classify businesses based on their operational hours.'
|
|
|
42 |
]
|
43 |
+
language = 'English'
|
44 |
+
|
45 |
+
prompts = [LLAMA3_PROMPT.format(prompt=get_create_classify_data_prompt(task=task, language=language)[1]['content']) for task in tasks]
|
46 |
+
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained('Haon-Chen/speed-synthesis-7b-senior')
|
48 |
+
model = AutoModelForCausalLM.from_pretrained('Haon-Chen/speed-synthesis-7b-senior')
|
49 |
+
model.to("cuda:0")
|
50 |
+
model.eval()
|
51 |
+
tokenizer.pad_token = tokenizer.pad_token or tokenizer.eos_token
|
52 |
+
tokenizer.padding_side = "left"
|
53 |
+
tokenizer.truncation_side = "left"
|
54 |
+
|
55 |
+
with torch.inference_mode():
|
56 |
+
# Tokenize the input texts
|
57 |
+
encodes = tokenizer(prompts, padding="longest", add_special_tokens=True, return_tensors="pt")
|
58 |
+
input_ids = encodes.input_ids.to(model.device)
|
59 |
+
attention_mask = encodes.attention_mask.to(model.device)
|
60 |
+
|
61 |
+
# Set the generation parameters
|
62 |
+
GEN_CONFIG = {"do_sample":True, "temperature": 1.0, "top_p": 1.0, "max_new_tokens": 800}
|
63 |
+
output = model.generate(
|
64 |
+
input_ids=input_ids,
|
65 |
+
attention_mask=attention_mask,
|
66 |
+
pad_token_id = tokenizer.eos_token_id,
|
67 |
+
**GEN_CONFIG
|
68 |
+
)
|
69 |
+
output_texts = tokenizer.batch_decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
70 |
+
batch_results = []
|
71 |
+
for i in range(len(output_texts)):
|
72 |
+
batch_results.append(output_texts[i][len(prompts[i]):].strip(' '))
|
73 |
+
|
74 |
+
# Format outputs
|
75 |
+
bad_cnt=0
|
76 |
+
outputs = []
|
77 |
+
for i, result in enumerate(batch_results):
|
78 |
+
try:
|
79 |
+
output = fix_common_json_errors_and_loads(result)
|
80 |
+
user_query = output.get("input_text", "")
|
81 |
+
positive_document = output.get("label", "")
|
82 |
+
hard_negative_document = output.get("misleading_label", "")
|
83 |
+
except:
|
84 |
+
bad_cnt+=1
|
85 |
+
continue
|
86 |
+
out_data = {
|
87 |
+
"query": user_query,
|
88 |
+
"positives": [positive_document],
|
89 |
+
"negatives": [hard_negative_document],
|
90 |
+
"language": "English",
|
91 |
+
"task_definition": tasks[i],
|
92 |
+
}
|
93 |
+
outputs.append(out_data)
|
94 |
+
print(bad_cnt)
|
95 |
+
print(outputs)
|
96 |
```
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
## Citation
|
99 |
|
100 |
If you find our paper or models helpful, please consider cite as follows:
|
|
|
109 |
```
|
110 |
|
111 |
## Limitations
|
|
|
|