forked from catalystforyou/RetroRanker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphormer_task.py
287 lines (237 loc) · 8.87 KB
/
graphormer_task.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
import contextlib
import logging
from dataclasses import dataclass, field
from functools import partial
import numpy as np
import torch
import torch.nn as nn
from fairseq import metrics
from fairseq.criterions import FairseqCriterion, register_criterion
from fairseq.data import (FairseqDataset, NestedDictionaryDataset,
NumSamplesDataset, data_utils)
from fairseq.dataclass.configs import FairseqDataclass
from fairseq.tasks import FairseqDataclass, FairseqTask, register_task
from omegaconf import II, OmegaConf, open_dict
# from graphormer_data import (RetroGraphormerDataset,build_split_files)
from graphormer_dbdata import RetroGraphormerDBDataset, build_split_files
from graphormer_fairseq import UnreusedEpochBatchIterator
from graphormer_rank import GraphormerRanker
from graphormer_utils import collator_gh
logger = logging.getLogger(__name__)
@dataclass
class GraphRankConfig(FairseqDataclass):
root: str = field(
default="../",
metadata={"help": "root dir of the dataset"},
)
dataset_name: str = field(
default="rsmiles",
metadata={"help": "name of the dataset"},
)
batch_size: int = field(
default=32,
metadata={"help": "batch size"},
)
num_classes: int = field(
default=1,
metadata={"help": "number of classes or regression targets"},
)
max_nodes: int = field(
default=128,
metadata={"help": "max nodes per graph"},
)
num_atoms: int = field(
default=128 * 512, # actually is 106
metadata={"help": "number of atom types in the graph"},
)
num_edges: int = field(
default=16 * 512, # actually is 13
metadata={"help": "number of edge types in the graph"},
)
num_in_degree: int = field(
default=512,
metadata={"help": "number of in degree types in the graph"},
)
num_out_degree: int = field(
default=512,
metadata={"help": "number of out degree types in the graph"},
)
num_spatial: int = field(
default=512,
metadata={"help": "number of spatial types in the graph"},
)
num_edge_dis: int = field(
default=128,
metadata={"help": "number of edge dis types in the graph"},
)
multi_hop_max_dist: int = field(
default=5,
metadata={"help": "max distance of multi-hop edges"},
)
spatial_pos_max: int = field(
default=1024,
metadata={"help": "max distance of multi-hop edges"},
)
edge_type: str = field(
default="multi_hop",
metadata={"help": "edge type in the graph"},
)
seed: int = II("common.seed")
pretrained_model_name: str = field(
default="none",
metadata={"help": "name of used pretrained model"},
)
load_pretrained_model_output_layer: bool = field(
default=False,
metadata={"help": "whether to load the output layer of pretrained model"},
)
user_data_dir: str = field(
default="",
metadata={"help": "path to the module of user-defined dataset"},
)
@register_task("graph_rank", dataclass=GraphRankConfig)
class GraphRankTask(FairseqTask):
def __init__(self, cfg):
super().__init__(cfg)
def load_dataset(self, split, epoch=1, combine=False, **kwargs):
# """Load a given dataset split (e.g., train, valid, test)."""
assert split in ["train", "valid", "test"]
# if split in "test":
# raise
paths = build_split_files(self.cfg, self.cfg.dataset_name, split)
if split == 'train':
chunk_size = 30
paths = [paths[i:i + chunk_size] for i in range(0, len(paths), chunk_size)]
paths = paths[(epoch - 1) % len(paths)]
if split == 'test':
paths = paths[epoch:epoch+1]
logger.info(f'loading {len(paths)} files for {split}')
batched_data = RetroGraphormerDBDataset(paths, is_test= split == 'test')
batched_data.collater = partial(collator_gh, max_node=self.cfg.max_nodes,
multi_hop_max_dist=self.cfg.multi_hop_max_dist, spatial_pos_max=self.cfg.spatial_pos_max)
data_sizes = np.array([self.max_nodes()] * len(batched_data))
dataset = NestedDictionaryDataset(
{
"nsamples": NumSamplesDataset(),
"net_input": {"batched_data": batched_data}
},
sizes=data_sizes,
)
self.datasets[split] = dataset
def build_model(self, cfg):
with open_dict(cfg) if OmegaConf.is_config(cfg) else contextlib.ExitStack():
cfg.max_nodes = self.cfg.max_nodes
model = GraphormerRanker.build_model(cfg, self)
return model
def max_nodes(self):
return self.cfg.max_nodes
@property
def source_dictionary(self):
return None
@property
def target_dictionary(self):
return None
@property
def label_dictionary(self):
return None
def has_sharded_data(self, split):
if split == 'train':
return True
return False
def get_batch_iterator(
self,
dataset,
max_tokens=None,
max_sentences=None,
max_positions=None,
ignore_invalid_inputs=False,
required_batch_size_multiple=1,
seed=1,
num_shards=1,
shard_id=0,
num_workers=0,
epoch=1,
data_buffer_size=0,
disable_iterator_cache=False,
skip_remainder_batch=False,
grouped_shuffling=False,
update_epoch_batch_itr=False,
):
assert isinstance(dataset, FairseqDataset)
# initialize the dataset with the correct starting epoch
dataset.set_epoch(epoch)
# get indices ordered by example size
with data_utils.numpy_seed(seed):
indices = dataset.ordered_indices()
# filter examples that are too large
if max_positions is not None:
indices = self.filter_indices_by_size(
indices, dataset, max_positions, ignore_invalid_inputs
)
# create mini-batches with given size constraints
batch_sampler = dataset.batch_by_size(
indices,
max_tokens=max_tokens,
max_sentences=max_sentences,
required_batch_size_multiple=required_batch_size_multiple,
)
# return an unreused, sharded iterator
epoch_iter = UnreusedEpochBatchIterator(
dataset=dataset,
collate_fn=dataset.collater,
batch_sampler=batch_sampler,
seed=seed,
num_shards=num_shards,
shard_id=shard_id,
num_workers=num_workers,
epoch=epoch,
buffer_size=data_buffer_size,
skip_remainder_batch=skip_remainder_batch,
grouped_shuffling=grouped_shuffling,
reuse_dataloader=False,
)
return epoch_iter
@register_criterion("Gl2_loss", dataclass=FairseqDataclass)
class GraphRankL2Loss(FairseqCriterion):
def forward(self, model, sample, reduce=True):
"""Compute the loss for the given sample.
Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
"""
sample_size = sample["nsamples"]
with torch.no_grad():
natoms = sample["net_input"]["batched_data"][0]["x"].shape[1]
logits = model(**sample["net_input"])
targets = torch.ones(
logits.shape[0], dtype=torch.long, device=logits.device)
loss = nn.CrossEntropyLoss(reduction="sum")(logits, targets)
acc_output = logits.detach().cpu().numpy()
acc = len(acc_output[acc_output[:, 0] < acc_output[:, 1]])
logging_output = {
"loss": loss.data,
"acc": acc,
"sample_size": logits.size(0),
"nsentences": sample_size,
"ntokens": natoms,
}
return loss, sample_size, logging_output
@staticmethod
def reduce_metrics(logging_outputs) -> None:
"""Aggregate logging outputs from data parallel training."""
loss_sum = sum(log.get("loss", 0) for log in logging_outputs)
acc_sum = sum(log.get("acc", 0) for log in logging_outputs)
sample_size = sum(log.get("sample_size", 0) for log in logging_outputs)
metrics.log_scalar("acc", acc_sum / sample_size,
sample_size, round=6)
metrics.log_scalar("loss", loss_sum / sample_size,
sample_size, round=6)
@staticmethod
def logging_outputs_can_be_summed() -> bool:
"""
Whether the logging outputs returned by `forward` can be summed
across workers prior to calling `reduce_metrics`. Setting this
to True will improves distributed training speed.
"""
return True