-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_knowledge.py
278 lines (213 loc) · 10.6 KB
/
gen_knowledge.py
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
274
275
276
277
278
import os
import pandas as pd
import argparse
import json
from transformers import set_seed, AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from vllm import LLM, SamplingParams
from tqdm import trange
import torch
def BX_user_data(tokenizer, args):
prompt = (
"Given a user who is {age} and in {location}, this user's book reading history over time is listed as below: \n{user_hist}.\n"
"Analyze the user's preferences (consider factors like genre, author, characters, plot, topic/theme, writing style, award/critical acclaim, etc.)."
"Provide clear explanations based on details from the user's reading history and other pertinent factors."
)
df = pd.read_parquet(os.path.join(args.data_dir, "valid.parquet.gz"))
meta = json.load(open(os.path.join(args.data_dir, "match-meta.json")))
feature_dict = meta["feature_dict"]
book_feats_table = meta["book_feats_table"]
title_dict = feature_dict["Book title"]
reverse_title_dict = {int(v): k for k, v in title_dict.items()}
age_dict = feature_dict["Age"]
reverse_age_dict = {int(v): k for k, v in age_dict.items()}
location_dict = feature_dict["Location"]
reverse_location_dict = {int(v): k for k, v in location_dict.items()}
inputs = []
for _, row in df.iterrows():
age = reverse_age_dict[row["Age"]]
location = reverse_location_dict[row["Location"]]
user_hist = ", ".join([f"{idx}. " + reverse_title_dict[book_feats_table[i][1]] for idx, i in enumerate(row["user history ID"][-30:])])
conversation = [
{"role": "user", "content": prompt.format(age=age, location=location, user_hist=user_hist)},
]
cur_input = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
inputs.append(cur_input)
split = args.split.split(":")
start = int(split[0])
end = int(split[1]) if split[1] != "" else len(inputs)
return inputs, start, end
def BX_item_data(tokenizer, args):
prompt = (
"Introduce book {title} and describe its attributes precisely "
"(including but not limited to genre, author, characters, plot, topic/theme, writing style, award/critical acclaim, etc.)."
)
meta = json.load(open(os.path.join(args.data_dir, "match-meta.json")))
title_dict = meta["feature_dict"]["Book title"]
reverse_title_dict = {int(v): k for k, v in title_dict.items()}
inputs = []
for i in range(len(reverse_title_dict)):
conversation = [
{"role": "user", "content": prompt.format(title=reverse_title_dict[i])},
]
cur_input = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
inputs.append(cur_input)
split = args.split.split(":")
start = int(split[0])
end = int(split[1]) if split[1] != "" else len(inputs)
return inputs, start, end
def ml25m_user_data(tokenizer, args):
prompt = (
"A user's movie viewing history over time is listed below: \n{user_hist}.\n"
"Analyze the user's preferences on movies (consider factors like genre, director/actors, time "
"period/country, character, plot/theme, mood/tone, critical acclaim/award, production quality, "
"and soundtrack). Provide clear explanations based on relevant details from the user\'s movie "
"viewing history and other pertinent factors."
)
df = pd.read_parquet(os.path.join(args.data_dir, "valid.parquet.gz"))
meta = json.load(open(os.path.join(args.data_dir, "match-meta.json")))
feature_dict = meta["feature_dict"]
title_dict = feature_dict["Movie title"]
reverse_title_dict = {int(v): k for k, v in title_dict.items()}
movie_feats_table = meta["movie_feats_table"]
inputs = []
for _, row in df.iterrows():
user_hist = "; ".join([f"{idx}. " + reverse_title_dict[movie_feats_table[i][1]] for idx, i in enumerate(row["user history ID"][-30:])])
conversation = [
{"role": "user", "content": prompt.format(user_hist=user_hist)},
]
cur_input = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
inputs.append(cur_input)
split = args.split.split(":")
start = int(split[0])
end = int(split[1]) if split[1] != "" else len(inputs)
return inputs, start, end
def ml25m_item_data(tokenizer, args):
prompt = (
"Introduce movie {title} and describe its attributes (including but not limited to genre, "
"director/cast, country, character, plot/theme, mood/tone, critical "
"acclaim/award, production quality, and soundtrack)."
)
meta = json.load(open(os.path.join(args.data_dir, "match-meta.json"), encoding="utf-8"))
title_dict = meta["feature_dict"]["Movie title"]
reverse_title_dict = {int(v): k for k, v in title_dict.items()}
movie_feats_table = meta["movie_feats_table"]
inputs = []
for i in range(len(movie_feats_table)):
title_id = movie_feats_table[i][1]
conversation = [
{"role": "user", "content": prompt.format(title=reverse_title_dict[title_id])},
]
cur_input = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
inputs.append(cur_input)
split = args.split.split(":")
start = int(split[0])
end = int(split[1]) if split[1] != "" else len(inputs)
return inputs, start, end
def az_user_data(tokenizer, args):
prompt = (
"A user's movie viewing history over time is listed below: \n{user_hist}.\n"
"Analyze the user's preferences on movies (consider factors like genre, director/actors, time "
"period/country, character, plot/theme, mood/tone, critical acclaim/award, production quality, "
"and soundtrack). Provide clear explanations based on relevant details from the user\'s movie "
"viewing history and other pertinent factors."
)
df = pd.read_parquet(os.path.join(args.data_dir, "valid.parquet.gz"))
title_dict = json.load(open("/NAS2020/Workspaces/DMGroup/rongshan/GNN_LLM/data/az-books/proc_data/datamaps.json"))["itemid2title"]
title_dict = {int(k): v for k, v in title_dict.items()}
inputs = []
for _, row in df.iterrows():
user_hist = "; ".join([f"{idx}. " + title_dict[i] for idx, i in enumerate(row["user history ID"][-30:])])
conversation = [
{"role": "user", "content": prompt.format(user_hist=user_hist)},
]
cur_input = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
inputs.append(cur_input)
split = args.split.split(":")
start = int(split[0])
end = int(split[1]) if split[1] != "" else len(inputs)
return inputs, start, end
def az_item_data(tokenizer, args):
prompt = (
"Introduce movie {title} and describe its attributes (including but not limited to genre, "
"director/cast, country, character, plot/theme, mood/tone, critical "
"acclaim/award, production quality, and soundtrack)."
)
title_dict = json.load(open("/NAS2020/Workspaces/DMGroup/rongshan/GNN_LLM/data/az-books/proc_data/datamaps.json"))["itemid2title"]
title_dict = {int(k): v for k, v in title_dict.items()}
inputs = []
for i in range(1, len(title_dict)+1):
conversation = [
{"role": "user", "content": prompt.format(title=title_dict[i])},
]
cur_input = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
inputs.append(cur_input)
split = args.split.split(":")
start = int(split[0])
end = int(split[1]) if split[1] != "" else len(inputs)
return inputs, start, end
def main(args):
tokenizer = AutoTokenizer.from_pretrained(
args.model_path,
padding_side="left",
torch_dtype=torch.bfloat16,
device_map="auto"
)
llm = LLM(args.model_path, tensor_parallel_size=args.num_gpus, gpu_memory_utilization=args.gpu_ratio)
if args.dataset == "BookCrossing":
if args.part == "user":
texts, start, end = BX_user_data(tokenizer, args)
else:
texts, start, end = BX_item_data(tokenizer, args)
elif args.dataset == "ml-25m":
if args.part == "user":
texts, start, end = ml25m_user_data(tokenizer, args)
else:
texts, start, end = ml25m_item_data(tokenizer, args)
elif args.dataset == "az-books":
if args.part == "user":
texts, start, end = az_user_data(tokenizer, args)
else:
texts, start, end = az_item_data(tokenizer, args)
texts = texts[start:end]
print(texts[0])
exit()
print(f"Data prepared, range {start}:{end}.")
# terminators = [
# tokenizer.eos_token_id,
# tokenizer.convert_tokens_to_ids("<|eot_id|>")
# ]
# sampling_params = SamplingParams(temperature=0.9, top_p=0.6, max_tokens=args.max_tokens, stop_token_ids=terminators)
sampling_params = SamplingParams(temperature=0.9, top_p=0.6, max_tokens=args.max_tokens)
generated_texts = []
saved_groups = 0
group_lens = 5120 * 2
for i in trange(0, len(texts), args.batch_size):
outputs = llm.generate(texts[i:i+args.batch_size], sampling_params, use_tqdm=False)
for output in outputs:
generated_texts.append(output.outputs[0].text)
if len(generated_texts) == group_lens:
cur_start = start + saved_groups * group_lens
cur_end = cur_start + group_lens
output_path = f"{args.embed_dir}/{args.dataset}/txt_{args.part}_{cur_start}_{cur_end}.json"
json.dump(generated_texts, open(output_path, "w"), indent=2)
generated_texts = []
saved_groups += 1
if len(generated_texts) != 0:
output_path = f"{args.embed_dir}/{args.dataset}/txt_{args.part}_{cur_end}_{end}.json"
json.dump(generated_texts, open(output_path, "w"), indent=2)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str)
parser.add_argument("--embed_dir", type=str, default="../outputs_llm")
parser.add_argument("--model_path", type=str)
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--part", type=str, default="item")
parser.add_argument("--max_tokens", type=int, default=2048)
parser.add_argument("--num_gpus", type=int, default=1)
parser.add_argument("--split", type=str, default="0:")
parser.add_argument("--gpu_ratio", type=float, default=0.9)
args = parser.parse_args()
args.dataset = args.data_dir.split("/")[-2]
os.makedirs(f"{args.embed_dir}/{args.dataset}", exist_ok=True)
set_seed(42)
main(args)