-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic_similarity_generate_script.py
More file actions
148 lines (128 loc) · 5.3 KB
/
semantic_similarity_generate_script.py
File metadata and controls
148 lines (128 loc) · 5.3 KB
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
import json
import os
import requests
from torch.cuda import device_count
from tqdm import tqdm
from datasets import load_dataset
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
from automation.utils import kill_process_tree, parse_argument, flatten_nested_dict
from automation.datasets.tulu import make_tulu_prompt
from automation.datasets.openplatypus import make_openplatypus_prompt
from automation.datasets.alpaca import make_alpaca_prompt
from automation.datasets.defaults import make_default_prompt
try:
from clearml import OutputModel, Task, Model
clearml_available = True
except ImportError:
clearml_available = False
RESULTS_DIR = os.path.join(os.getcwd(), "results")
os.makedirs(RESULTS_DIR, exist_ok=True)
def semantic_similarity_generate_main(
model_id,
trust_remote_code,
dataset_args,
semantic_similarity_args,
max_model_len,
max_new_tokens,
clearml_model,
):
from collections import defaultdict
from huggingface_hub import snapshot_download
all_conversations = []
all_samples_dict = defaultdict(list)
print(">>> Loading dataset...")
for dataset_path, num_samples_per_dataset in dataset_args.items():
dataset_name = dataset_path.split("/")[1].lower()
print(f">>> Loading dataset {dataset_name}...")
dataset = load_dataset(dataset_path, split=f"train[:{int(num_samples_per_dataset)}]")
all_samples_dict[dataset_name].extend(dataset)
sorted_all_samples_dict = dict(sorted(all_samples_dict.items()))
for dataset_name,dataset_samples in sorted_all_samples_dict.items():
print(f">>> Loading values for {dataset_name}...")
for sample in dataset_samples:
if dataset_name == "alpaca":
prompt = make_alpaca_prompt(sample)
elif dataset_name == "open-platypus":
prompt = make_openplatypus_prompt(sample)
elif dataset_name == "tulu-3-sft-mixture":
prompt = make_tulu_prompt(sample)
else:
print("Using default prompt")
prompt = make_default_prompt(sample)
all_conversations.append(prompt)
print("Define sampling parameters")
sampling_params = SamplingParams(
temperature=semantic_similarity_args.get("temperature", 0.0),
max_tokens=max_new_tokens
)
llm_format = "auto"
if clearml_model:
HUGGINGFACE_DIR = Model(model_id).get_local_copy()
else:
HUGGINGFACE_DIR = "/home"
snapshot_download(repo_id=model_id, local_dir=HUGGINGFACE_DIR)
print(os.listdir(HUGGINGFACE_DIR))
if "mistral" in model_id.lower() and "quantized" not in model_id.lower():
llm_format = "mistral"
try:
print(f"Initializing vLLM: {model_id}...")
llm = LLM(
model= HUGGINGFACE_DIR,
dtype=semantic_similarity_args.get("dtype", "auto"),
trust_remote_code=trust_remote_code,
tensor_parallel_size=device_count(),
enforce_eager=semantic_similarity_args.get("enforce_eager", True),
enable_chunked_prefill=semantic_similarity_args.get("enable_chunked_prefill", True),
max_model_len=max_model_len,
load_format=llm_format,
config_format=llm_format,
tokenizer_mode=llm_format,
)
print("Completed the model initialization ")
print(">>> Running vLLM generation...")
outputs = llm.chat(messages=all_conversations, sampling_params=sampling_params)
except Exception as e:
print(f"Error initializing LLM: {e}")
return all_conversations, outputs
def main(configurations=None, args=None):
if clearml_available:
task = Task.current_task()
args = task.get_parameters_as_dict(cast=True)["Args"]
clearml_model = parse_argument(args["clearml_model"], bool)
else:
args = args["Args"]
clearml_model = False
# Parse arguments
force_download = parse_argument(args["force_download"], bool)
trust_remote_code = parse_argument(args["trust_remote_code"], bool)
model_id = parse_argument(args["model_id"], str)
max_model_len = parse_argument(args["max_model_len"], int)
max_new_tokens = parse_argument(args["max_new_tokens"], int)
dataset_args = flatten_nested_dict(parse_argument(args["dataset_args"], dict))
semantic_similarity_args= args.get("semantic_similarity_args", None)
tags = args.get("tags", None)
all_conversations, outputs = semantic_similarity_generate_main(
model_id,
trust_remote_code,
dataset_args,
semantic_similarity_args,
max_model_len,
max_new_tokens,
clearml_model,
)
OUTPUT_FILE = os.path.join(RESULTS_DIR,f"{model_id.replace('/', '_')}.jsonl")
print(">>> Writing outputs to file...")
with open(OUTPUT_FILE, "w") as fout:
for idx, (prompt, output) in enumerate(zip(all_conversations, outputs)):
response = output.outputs[0].text.strip()
fout.write(json.dumps({
"index": idx,
"prompt": prompt,
"response": response
}) + "\n")
print(f">>> Completed. Saved {len(outputs)} outputs to {OUTPUT_FILE}")
if clearml_available:
task.upload_artifact("jsonl_output", OUTPUT_FILE)
if __name__ == '__main__':
main()