-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_pretrain_logits.py
167 lines (141 loc) · 7.11 KB
/
eval_pretrain_logits.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
import sys
sys.path.append("./")
sys.path.append("./utils")
import argparse
import torch
import os
import json
from tqdm import tqdm
import shortuuid
from graphllm.constants import GRAPH_TOKEN_INDEX, DEFAULT_GRAPH_TOKEN, DEFAULT_GRAPH_PAD_ID, DEFAULT_GRAPH_START_TOKEN, DEFAULT_GRAPH_END_TOKEN
from graphllm.converstation import conv_templates, SeparatorStyle
from graphllm.builder import load_pretrained_model
from graphllm.utils import disable_torch_init, tokenizer_graph_token, get_model_name_from_path
from torch_geometric.nn import MessagePassing
import math
import os.path as osp
class MP(MessagePassing):
def __init__(self):
super().__init__(aggr='add') # "Add" aggregation (Step 5).
def message(self, x_j, norm):
return norm.view(-1, 1) * x_j
def split_list(lst, n):
"""Split a list into n (roughly) equal-sized chunks"""
chunk_size = math.ceil(len(lst) / n) # integer division
return [lst[i:i+chunk_size] for i in range(0, len(lst), chunk_size)]
def load_pretrain_embedding_hop_lp(data_dir, pretrained_embedding_type, hop):
pretrained_embs = [torch.load(os.path.join(data_dir, f"{pretrained_embedding_type}_0hop_x.pt"))]+ [torch.load(os.path.join(data_dir, f"{pretrained_embedding_type}_{i}hop_x_notestlink.pt")) for i in range(1, hop+1)]
return pretrained_embs
def eval_model(args):
# Model
disable_torch_init()
model_path = os.path.expanduser(args.model_path)
model_name = get_model_name_from_path(model_path)
print(f"Loaded from {model_path}. Model Base: {args.model_base}")
tokenizer, model, context_len = load_pretrained_model(model_path, args.model_base, model_name,
cache_dir=args.cache_dir)
model = model.to(torch.float16).cuda()
data_path = osp.join(args.data_saved_path, args.dataset, 'processed', "processed_data_link_notest.pt")
data_dir = osp.join(args.data_saved_path, args.dataset, 'processed')
prompt_file = os.path.join(data_dir, f"edge_sampled_2_10_only_test.jsonl")
data = torch.load(data_path)
print(f"Load from {prompt_file}\n")
lines = open(prompt_file, "r").readlines()
if args.start >= 0:
if args.end < 0:
args.end = len(lines)
lines = lines[args.start:args.end]
elif args.end > 0:
lines = lines[:args.end]
answers_file = os.path.expanduser(args.answers_file)
os.makedirs(os.path.dirname(answers_file), exist_ok=True)
if "tmp" not in args.answers_file and os.path.exists(answers_file):
line_number = len(open(answers_file, 'r').readlines())
print(f"{args.answers_file} already exists! it has {line_number} lines!!")
if line_number >= len(lines):
return
lines = lines[line_number:]
ans_file = open(answers_file, "a")
else:
ans_file = open(answers_file, "w")
questions = [json.loads(q) for q in lines]
pretrained_emb= load_pretrain_embedding_hop_lp(data_dir, args.pretrained_embedding_type,args.use_hop)
for line in tqdm(questions):
idx = line["id"]
if args.task == "lp":
qs=f"Given two node-centered subgraphs: {DEFAULT_GRAPH_TOKEN} and {DEFAULT_GRAPH_TOKEN}, we need to predict whether these two nodes connect with each other. Please tell me whether two center nodes in the subgraphs should connect to each other."
else:
print(f"NOT SUPPORT {args.task}!!!")
raise ValueError
cur_prompt = qs
conv = conv_templates[args.conv_mode].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer_graph_token(prompt, tokenizer, GRAPH_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
if not isinstance(line['graph'][0], list):
line['graph'] = [line['graph']]
for g in range(len(line['graph'])):
center_id = line['graph'][g][0]
line['graph'][g] = [center_id]*(args.use_hop+1)
graph = torch.LongTensor(line['graph'])
center_id = graph[:, 0]
graph_emb = torch.stack([emb[center_id] for emb in pretrained_emb], dim=1)
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
try:
with torch.inference_mode():
model_inputs = model.prepare_inputs_for_generation(
input_ids,
graph_emb=graph_emb.half().cuda(),
graph=graph.cuda(),
do_sample=True,
temperature=args.temperature,
top_p=args.top_p,
num_beams=args.num_beams,
# no_repeat_ngram_size=3,
max_new_tokens=1024,
use_cache=True)
outputs = model.forward(**model_inputs)
logits = outputs.logits[0, -1]
# for vicuna
yes_id = tokenizer.encode("yes")[-1]
no_id = tokenizer.encode("no")[-1]
yes_logit = logits[yes_id].item()
no_logit = logits[no_id].item()
except Exception as e:
print(f"!!!!!!Error!!!!! {e}")
yes_logit = no_logit = 0
ans_id = shortuuid.uuid()
ans_file.write(json.dumps({"question_id": idx,
"prompt": cur_prompt,
"graph": line['graph'],
"logit": [yes_logit, no_logit],
"gt":'yes' if line['gt'] == 1 else 'no',
"answer_id": ans_id}) + "\n")
ans_file.flush()
ans_file.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", type=str, default="facebook/opt-350m")
parser.add_argument("--model_base", type=str, default=None)
# parser.add_argument("--data_dir", type=str, default=None)
parser.add_argument("--data_saved_path", type=str, default="cache_data_minilm")
parser.add_argument("--pretrained_embedding_type", type=str, default="sbert")
parser.add_argument("--use_hop", type=int, default=2)
parser.add_argument("--sample_neighbor_size", type=int, default=5)
parser.add_argument("--answers_file", type=str, default="answer.jsonl")
parser.add_argument("--conv_mode", type=str, default="v1")
parser.add_argument("--temperature", type=float, default=0.2)
parser.add_argument("--top_p", type=float, default=None)
parser.add_argument("--num_beams", type=int, default=1)
parser.add_argument("--prompt", type=str, default=None)
parser.add_argument("--start", type=int, default=-1)
parser.add_argument("--end", type=int, default=-1)
parser.add_argument("--test_path", type=str, default=None)
parser.add_argument("--mm_use_graph_start_end",default=False, action="store_true")
parser.add_argument("--task", type=str, default="lp")
parser.add_argument("--dataset", type=str, default="arxiv")
parser.add_argument("--cache_dir", type=str, default="../../checkpoint")
parser.add_argument("--template", type=str, default="ND")
args = parser.parse_args()
eval_model(args)