forked from LechengKong/OneForAll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_cdm.py
244 lines (208 loc) · 6.57 KB
/
run_cdm.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
import argparse
import os
from types import SimpleNamespace
import torch
from pytorch_lightning.loggers import WandbLogger
from torchmetrics import AUROC, Accuracy
import utils
from gp.lightning.data_template import DataModule
from gp.lightning.metric import (
flat_binary_func,
EvalKit,
)
from gp.lightning.module_template import ExpConfig
from gp.lightning.training import lightning_fit
from gp.utils.utils import (
load_yaml,
combine_dict,
merge_mod,
setup_exp,
set_random_seed,
)
from lightning_model import GraphPredLightning
from models.model import BinGraphModel, BinGraphAttModel
from models.model import PyGRGCNEdge
from task_constructor import UnifiedTaskConstructor
from utils import (
SentenceEncoder,
MultiApr,
MultiAuc,
)
# os.environ["CUDA_LAUNCH_BLOCKING"]="1"
def main(params):
"""
0. Check GPU setting.
"""
device, gpu_ids = utils.get_available_devices()
gpu_size = len(gpu_ids)
"""
1. Initiate task constructor.
"""
encoder = SentenceEncoder(params.llm_name, batch_size=params.llm_b_size)
task_config_lookup = load_yaml(
os.path.join(os.path.dirname(__file__), "configs", "task_config.yaml")
)
data_config_lookup = load_yaml(os.path.join(os.path.dirname(__file__), "configs", "data_config.yaml"))
if isinstance(params.task_names, str):
task_names = [a.strip() for a in params.task_names.split(",")]
else:
task_names = params.task_names
tasks = UnifiedTaskConstructor(
task_names,
params.load_texts,
encoder,
task_config_lookup,
data_config_lookup,
batch_size=params.batch_size,
sample_size=params.train_sample_size,
)
val_task_index_lst, val_pool_mode = tasks.construct_exp()
# remove llm model
if encoder is not None:
encoder.flush_model()
"""
2. Load model
"""
out_dim = params.emb_dim + (params.rwpe if params.rwpe is not None else 0)
gnn = PyGRGCNEdge(
params.num_layers,
5,
out_dim,
out_dim,
drop_ratio=params.dropout,
JK=params.JK,
)
bin_model = BinGraphAttModel if params.JK == "none" else BinGraphModel
model = bin_model(model=gnn, llm_name=params.llm_name, outdim=out_dim, task_dim=1,
add_rwpe=params.rwpe, dropout=params.dropout)
"""
3. Construct datasets and lightning datamodule.
"""
if hasattr(params, "d_multiple"):
if isinstance(params.d_multiple, str):
data_multiple = [float(a) for a in params.d_multiple.split(",")]
else:
data_multiple = params.d_multiple
else:
data_multiple = [1]
if hasattr(params, "d_min_ratio"):
if isinstance(params.d_min_ratio, str):
min_ratio = [float(a) for a in params.d_min_ratio.split(",")]
else:
min_ratio = params.d_min_ratio
else:
min_ratio = [1]
train_data = tasks.make_train_data(data_multiple, min_ratio, data_val_index=val_task_index_lst)
text_dataset = tasks.make_full_dm_list(
data_multiple, min_ratio, train_data
)
params.datamodule = DataModule(
text_dataset, gpu_size=gpu_size, num_workers=params.num_workers
)
"""
4. Initiate evaluation kit.
"""
eval_data = text_dataset["val"] + text_dataset["test"]
val_state = [dt.state_name for dt in text_dataset["val"]]
test_state = [dt.state_name for dt in text_dataset["test"]]
eval_state = val_state + test_state
eval_metric = [dt.metric for dt in eval_data]
eval_funcs = [dt.meta_data["eval_func"] for dt in eval_data]
loss = torch.nn.BCEWithLogitsLoss()
evlter = []
for dt in eval_data:
if dt.metric == "acc":
evlter.append(Accuracy(task="multiclass", num_classes=dt.classes))
elif dt.metric == "auc":
evlter.append(AUROC(task="binary"))
elif dt.metric == "apr":
evlter.append(MultiApr(num_labels=dt.classes))
elif dt.metric == "aucmulti":
evlter.append(MultiAuc(num_labels=dt.classes))
metrics = EvalKit(
eval_metric,
evlter,
loss,
eval_funcs,
flat_binary_func,
eval_mode="max",
exp_prefix="",
eval_state=eval_state,
val_monitor_state=val_state[0],
test_monitor_state=test_state[0],
)
"""
5. Initiate optimizer, scheduler and lightning model module.
"""
optimizer = torch.optim.Adam(
model.parameters(), lr=params.lr, weight_decay=params.l2
)
lr_scheduler = {
"scheduler": torch.optim.lr_scheduler.StepLR(optimizer, 15, 0.5),
"interval": "epoch",
"frequency": 1,
}
exp_config = ExpConfig(
"",
optimizer,
dataset_callback=train_data.update,
lr_scheduler=lr_scheduler,
)
exp_config.val_state_name = val_state
exp_config.test_state_name = test_state
pred_model = GraphPredLightning(exp_config, model, metrics)
"""
6. Start training and logging.
"""
wandb_logger = WandbLogger(
project=params.log_project,
name=params.exp_name,
save_dir=params.exp_dir,
offline=params.offline_log,
)
strategy = "deepspeed_stage_2" if gpu_size > 1 else "auto"
val_res, test_res = lightning_fit(
wandb_logger,
pred_model,
params.datamodule,
metrics,
params.num_epochs,
strategy=strategy,
save_model=False,
load_best=params.load_best,
reload_freq=1,
test_rep=params.test_rep,
val_interval=params.val_interval
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="rl")
parser.add_argument("--override", type=str)
parser.add_argument(
"opts",
default=[],
nargs=argparse.REMAINDER,
help="Modify config options using the command-line",
)
params = parser.parse_args()
configs = []
configs.append(
load_yaml(
os.path.join(
os.path.dirname(__file__), "configs", "default_config.yaml"
)
)
)
if params.override is not None:
override_config = load_yaml(params.override)
configs.append(override_config)
# Add for few-shot parameters
mod_params = combine_dict(*configs)
mod_params = merge_mod(mod_params, params.opts)
setup_exp(mod_params)
params = SimpleNamespace(**mod_params)
set_random_seed(params.seed)
torch.set_float32_matmul_precision("high")
params.log_project = "full_cdm"
params.exp_name += f"_{params.llm_name}_ofa1"
print(params)
main(params)