-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
303 lines (266 loc) · 13.6 KB
/
main.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import torch
from tqdm import tqdm
import numpy as np
import os
import math
from torch_geometric.loader import DataLoader
from torch_geometric.nn import global_mean_pool, global_max_pool, global_add_pool
from torch_geometric.utils import to_edge_index
import yaml
from yaml import SafeLoader
from data.load import load_data
from data.sampling import collect_subgraphs, ego_graphs_sampler
from utils.peft import create_peft_config
from utils.args import Arguments
from models.encoder import GCN_Encoder, SAGE_Encoder, GIN_Encoder, MLP_Encoder, GAT_Encoder, PMLP_Encoder, GCNII_Encoder
def get_hidden_states(config):
path = f'./llm_cache/{config.dataset}/layers'
if not os.path.exists(os.path.join(path, 'layer_attr.pt')):
raise FileNotFoundError(f'No cache found! Please use `python cache.py --dataset {config.dataset}` to generate it.')
else:
layers_hid = torch.load(os.path.join(path, 'layer_attr.pt'))
xs = layers_hid
return xs
def get_dataloader(data, config):
train_idx = data.train_mask.nonzero().squeeze()
val_idx = data.val_mask.nonzero().squeeze()
test_idx = data.test_mask.nonzero().squeeze()
kwargs = {'batch_size': 256, 'num_workers': 6, 'persistent_workers': True}
if config.sampler =='rw':
train_graphs = collect_subgraphs(train_idx, data, walk_steps=config.walk_steps, restart_ratio=config.restart)
val_graphs = collect_subgraphs(val_idx, data, walk_steps=config.walk_steps, restart_ratio=config.restart)
test_graphs = collect_subgraphs(test_idx, data, walk_steps=config.walk_steps, restart_ratio=config.restart)
train_loader = DataLoader(train_graphs, shuffle=True, **kwargs)
val_loader = DataLoader(val_graphs, **kwargs)
test_loader = DataLoader(test_graphs, **kwargs)
else:
if config.dataset in ['ogbn-arxiv', 'arxiv_2023', 'photo'] and os.path.exists(f'../subgraphs/{config.dataset}/khop-1/train.pt') and os.path.exists(f'../subgraphs/{config.dataset}/khop-1/val.pt') and os.path.exists(f'../subgraphs/{config.dataset}/khop-1/test.pt'):
print('using cache of subgraphs')
train_graphs = torch.load(f'../subgraphs/{config.dataset}/khop-1/train.pt')
val_graphs = torch.load(f'../subgraphs/{config.dataset}/khop-1/val.pt')
test_graphs = torch.load(f'../subgraphs/{config.dataset}/khop-1/test.pt')
else:
train_graphs = ego_graphs_sampler(train_idx, data, hop=1, sparse=(config.dataset=='ogbn-arxiv'))
val_graphs = ego_graphs_sampler(val_idx, data, hop=1, sparse=(config.dataset=='ogbn-arxiv'))
test_graphs = ego_graphs_sampler(test_idx, data, hop=1, sparse=(config.dataset=='ogbn-arxiv'))
if config.dataset in ['ogbn-arxiv', 'arxiv_2023', 'photo']:
os.makedirs(f'../subgraphs/{config.dataset}/khop-1')
torch.save(train_graphs, f'../subgraphs/{config.dataset}/khop-1/train.pt')
torch.save(val_graphs, f'../subgraphs/{config.dataset}/khop-1/val.pt')
torch.save(test_graphs, f'../subgraphs/{config.dataset}/khop-1/test.pt')
train_loader = DataLoader(train_graphs, shuffle=True, **kwargs)
val_loader = DataLoader(val_graphs, **kwargs)
test_loader = DataLoader(test_graphs, **kwargs)
return train_loader, val_loader, test_loader
def efficient_train_eval(train_loader, val_loader, test_loader, xs, model_list, prog_list, alpha_list, exit_list, optimizer):
patience = config.patience
best_acc = 0
best_test_from_val = 0
best_state_list = []
cnt = 0
criterion = torch.nn.CrossEntropyLoss()
# criterion = LabelSmoothingCrossEntropy(smoothing=0.05)
for epoch in tqdm(range(config.epochs)):
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
last = None
total_loss = 0
for i, m in enumerate(model_list):
m.train()
prog_list[i].train()
exit_list[i].train()
if i == 0:
# out = m(prog_list[i](xs[i][data.original_idx]), data.edge_index)
out = m(prog_list[i]((xs[i][data.original_idx.cpu()]).to(device)), data.edge_index)
else:
a = torch.nn.functional.sigmoid(alpha_list[i]/T)
# x = prog_list[i](xs[i][data.original_idx])*a + last*(1-a)
x = prog_list[i]((xs[i][data.original_idx.cpu()]).to(device))*a + last*(1-a)
# x = prog_list[i](xs[i][data.original_idx]) + last
out = m(x, data.edge_index)
last = out
hid_out = torch.cat([last[data.root_n_index], global_mean_pool(last, data.batch)], dim=1)
hid_logits = exit_list[i](hid_out)
total_loss += criterion(hid_logits, data.y)
total_loss.backward(retain_graph=True)
optimizer.step()
val_acc = efficient_eval(val_loader, xs, model_list, prog_list, alpha_list, exit_list)
test_acc = efficient_eval(test_loader, xs, model_list, prog_list, alpha_list, exit_list)
if val_acc > best_acc:
best_acc = val_acc
cnt = 0
best_test_from_val = test_acc
else:
cnt += 1
if cnt >= patience:
print(f'early stop at epoch {epoch}')
return best_test_from_val
return best_test_from_val
def efficient_eval(test_loader, xs, model_list, prog_list, alpha_list, exit_list):
correct = 0
total_cnt = 0
for data in test_loader:
total_cnt += data.batch.max().item()+1
data = data.to(device)
results = []
last = 0
# num_classes = data.y.max().item() + 1
last_prediction = []
for i, m in enumerate(model_list):
m.eval()
prog_list[i].eval()
exit_list[i].eval()
if i == 0:
# out = m(prog_list[i](xs[i][data.original_idx]), data.edge_index)
out = m(prog_list[i]((xs[i][data.original_idx.cpu()]).to(device)), data.edge_index)
not_visited = torch.ones(data.batch.max()+1, device=out.device).bool()
results = torch.rand(data.batch.max()+1, num_classes, device=out.device) # initialize results
last_prediction = torch.ones(data.batch.max()+1, device=out.device) * -1
else:
a = torch.nn.functional.sigmoid(alpha_list[i]/T)
# x = prog_list[i](xs[i][data.original_idx])*a + last*(1-a)
x = prog_list[i]((xs[i][data.original_idx.cpu()]).to(device))*a + last*(1-a)
out = m(x, data.edge_index)
last = out
# dynamic early exit based on entropy
hid_out = torch.cat([last[data.root_n_index], global_mean_pool(last, data.batch)], dim=1)
hid_logits = exit_list[i](hid_out)
hid_prob = torch.nn.functional.softmax(hid_logits, dim=1)
current_prediction = hid_prob.argmax(dim=1)
early_mask = ((current_prediction == last_prediction) == not_visited)
results[early_mask] = hid_prob[early_mask]
not_visited[early_mask] = False
last_prediction = current_prediction
results[not_visited] = hid_prob[not_visited] # samples without early exiting
pred = results.argmax(dim=1)
correct += (pred == data.y).sum()
acc = int(correct) / total_cnt
# print(f'Accuracy: {acc:.4f}')
return acc
def train_eval(train_loader, val_loader, test_loader, xs, model_list, prog_list, alpha_list, exit_list, optimizer):
patience = config.patience
best_acc = 0
best_test_from_val = 0
best_state_list = []
cnt = 0
criterion = torch.nn.CrossEntropyLoss()
# criterion = LabelSmoothingCrossEntropy(smoothing=0.05)
for epoch in tqdm(range(config.epochs)):
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
last = None
for i, m in enumerate(model_list):
m.train()
prog_list[i].train()
if i == 0:
out = m(prog_list[i]((xs[i][data.original_idx.cpu()]).to(device)), data.edge_index)
else:
a = torch.nn.functional.sigmoid(alpha_list[i]/T)
x = prog_list[i]((xs[i][data.original_idx.cpu()]).to(device))*a + last*(1-a)
out = m(x, data.edge_index)
last = out
if hasattr(data, 'root_n_id'):
data.root_n_index = data.root_n_id
out = torch.cat([last[data.root_n_index], global_mean_pool(last, data.batch)], dim=1)
out = classifier(out)
loss = criterion(out, data.y)
loss.backward()
optimizer.step()
val_acc = eval(val_loader, xs, model_list, prog_list, alpha_list)
test_acc = eval(test_loader, xs, model_list, prog_list, alpha_list)
if val_acc > best_acc:
best_acc = val_acc
cnt = 0
best_test_from_val = test_acc
else:
cnt += 1
if cnt >= patience:
print(f'early stop at epoch {epoch}')
return best_test_from_val
# best_test_from_val = eval(test_loader, xs, model_list, prog_list, alpha_list, zero_list)
return best_test_from_val
def eval(test_loader, xs, model_list, prog_list, alpha_list):
correct = 0
total = 0
for data in test_loader:
data = data.to(device)
total += data.batch.max().item()+1
last = None
for i, m in enumerate(model_list):
m.eval()
prog_list[i].eval()
if i == 0:
out = m(prog_list[i]((xs[i][data.original_idx.cpu()]).to(device)), data.edge_index)
else:
a = torch.nn.functional.sigmoid(alpha_list[i]/T)
x = prog_list[i]((xs[i][data.original_idx.cpu()]).to(device))*a + last*(1-a)
# x = prog_list[i](xs[i][data.original_idx]) + last
out = m(x, data.edge_index)
last = out
if hasattr(data, 'root_n_id'):
data.root_n_index = data.root_n_id
out = torch.cat([out[data.root_n_index], global_mean_pool(out, data.batch)], dim=1)
out = classifier(out)
pred = out.argmax(dim=1)
correct += (pred == data.y).sum()
acc = int(correct) / total
print(f'Accuracy: {acc:.4f}')
return acc
if __name__ == '__main__':
config = Arguments().parse_args()
args = yaml.load(open(config.config), Loader=SafeLoader)
# combine args and config
for k, v in args.items():
config.__setattr__(k, v)
print(config)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
xs = get_hidden_states(config)
xs = [x for x in xs]
acc_list = []
for seed in range(5):
# load data
data, text, num_classes = load_data(config.dataset, use_text=True, seed=config.seeds[seed])
if config.dataset == 'ogbn-products':
edge_index, _ = to_edge_index(data.edge_index)
data.edge_index = edge_index
train_loader, val_loader, test_loader = get_dataloader(data, config)
r=config.r # used for dimensionality reduction
input_dim=config.input_dim # 4096
k = int(input_dim/r)
hidden = config.hidden_size
layer_select = config.layer_select
encoders = {
'GCN_Encoder': GCN_Encoder,
'GAT_Encoder': GAT_Encoder,
'SAGE_Encoder': SAGE_Encoder,
'MLP_Encoder': MLP_Encoder,
}
model_list = [encoders[config.encoder](k, config.layer_num, hidden, k, activation=config.activation, norm=config.norm, last_activation=(l !=len(layer_select)-1), dropout=config.dropout).to(device) for l in layer_select]
prog_list = [torch.nn.Sequential(torch.nn.Linear(input_dim, k), torch.nn.LayerNorm(k), torch.nn.ReLU(), torch.nn.Linear(k,k)).to(device) for l in layer_select]
alpha_list = [torch.nn.Parameter(torch.tensor(0.0), requires_grad=True) for l in layer_select]
exit_list = [torch.nn.Linear(k*2, num_classes).to(device) for l in layer_select]
classifier = torch.nn.Linear(k*2, num_classes).to(device)
T=config.T
lr = config.lr
weight_decay = config.weight_decay
params = []
xs_list = []
for i, l in enumerate(layer_select):
params.append({'params': model_list[i].parameters(), 'lr': lr, 'weight_decay': weight_decay})
params.append({'params': prog_list[i].parameters(), 'lr': lr, 'weight_decay': weight_decay})
params.append({'params': alpha_list[i], 'lr': lr, 'weight_decay': weight_decay})
params.append({'params': exit_list[i].parameters(), 'lr': lr, 'weight_decay': weight_decay})
xs_list.append(xs[l])
params.append({'params': classifier.parameters(), 'lr': lr, 'weight_decay': weight_decay})
optimizer = torch.optim.AdamW(params)
# ENGINE w/ caching
if config.early: # Early
acc = efficient_train_eval(train_loader, val_loader, test_loader, xs_list, model_list, prog_list, alpha_list, exit_list, optimizer)
else:
acc = train_eval(train_loader, val_loader, test_loader, xs_list, model_list, prog_list, alpha_list, exit_list, optimizer)
print(seed, acc)
acc_list.append(acc)
final_acc, final_acc_std = np.mean(acc_list), np.std(acc_list)
print(f"# final_acc: {final_acc*100:.2f}±{final_acc_std*100:.2f}")