-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed_dataloader_utils.py
343 lines (302 loc) · 13.3 KB
/
fixed_dataloader_utils.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import importlib
import os
import pickle
from recbole.data.dataloader import *
from recbole.sampler import KGSampler, Sampler, RepeatableSampler
from recbole.utils import ModelType, ensure_dir, set_color
from recbole.utils.argument_list import dataset_arguments
def create_dataset(config):
"""Create dataset according to :attr:`config['model']` and :attr:`config['MODEL_TYPE']`.
If :attr:`config['dataset_save_path']` file exists and
its :attr:`config` of dataset is equal to current :attr:`config` of dataset.
It will return the saved dataset in :attr:`config['dataset_save_path']`.
Args:
config (Config): An instance object of Config, used to record parameter information.
Returns:
Dataset: Constructed dataset.
"""
dataset_module = importlib.import_module("recbole.data.dataset")
if hasattr(dataset_module, config["model"] + "Dataset"):
dataset_class = getattr(dataset_module, config["model"] + "Dataset")
else:
model_type = config["MODEL_TYPE"]
type2class = {
ModelType.GENERAL: "Dataset",
ModelType.SEQUENTIAL: "SequentialDataset",
ModelType.CONTEXT: "Dataset",
ModelType.KNOWLEDGE: "KnowledgeBasedDataset",
ModelType.TRADITIONAL: "Dataset",
ModelType.DECISIONTREE: "Dataset",
}
dataset_class = getattr(dataset_module, type2class[model_type])
default_file = os.path.join(
config["checkpoint_dir"], f'{config["dataset"]}-dataset.pth'
)
file = config["dataset_save_path"] or default_file
if os.path.exists(file):
with open(file, "rb") as f:
dataset = pickle.load(f)
dataset_args_unchanged = True
for arg in dataset_arguments + ["seed", "repeatable"]:
if config[arg] != dataset.config[arg]:
dataset_args_unchanged = False
# print(f'arg: {arg}')
# print(f'saved config: {config[arg]}')
# print(f'new config: {dataset.config[arg]}')
break
if dataset_args_unchanged:
logger = getLogger()
logger.info(set_color("Load filtered dataset from", "pink") + f": [{file}]")
return dataset
dataset = dataset_class(config)
if config["save_dataset"]:
dataset.save()
return dataset
def save_split_dataloaders(config, dataloaders:list):
"""Save split dataloaders.
Args:
config (Config): An instance object of Config, used to record parameter information.
dataloaders (tuple of KGDataLoader): The split dataloaders.
"""
ensure_dir(config["checkpoint_dir"])
save_path = config["checkpoint_dir"]
saved_dataloaders_file = f'{config["dataset"]}-for-{config["model"]}-dataloader.pth'
file_path = os.path.join(save_path, saved_dataloaders_file)
logger = getLogger()
logger.info(set_color("Saving split dataloaders into", "pink") + f": [{file_path}]")
Serialization_dataloaders = []
for dataloader in dataloaders:
if isinstance(dataloader, KnowledgeBasedDataLoader):
generator_state = dataloader.general_dataloader.generator.get_state()
dataloader.general_dataloader.generator = None
dataloader.general_dataloader.sampler.generator = None
KGgenerator_state = dataloader.kg_dataloader.generator.get_state()
dataloader.kg_dataloader.generator = None
dataloader.kg_dataloader.sampler.generator = None
Serialization_dataloaders += [(dataloader, generator_state, KGgenerator_state)]
elif isinstance(dataloader, AbstractDataLoader):
generator_state = dataloader.generator.get_state()
dataloader.generator = None
dataloader.sampler.generator = None
Serialization_dataloaders += [(dataloader, generator_state)]
with open(file_path, "wb") as f:
pickle.dump(Serialization_dataloaders, f)
def load_split_dataloaders(config):
"""Load split dataloaders if saved dataloaders exist and
their :attr:`config` of dataset are the same as current :attr:`config` of dataset.
Args:
config (Config): An instance object of Config, used to record parameter information.
Returns:
dataloaders (tuple of AbstractDataLoader or None): The split dataloaders.
"""
default_file = os.path.join(
config["checkpoint_dir"],
f'{config["dataset"]}-for-{config["model"]}-dataloader.pth',
)
dataloaders_save_path = config["dataloaders_save_path"] or default_file
if not os.path.exists(dataloaders_save_path):
return None
with open(dataloaders_save_path, "rb") as f:
dataloaders = []
for dataloader_list in pickle.load(f):
if len(dataloader_list) == 2:
data_loader, generator_state = dataloader_list
generator = torch.Generator()
generator.set_state(generator_state)
data_loader.generator = generator
data_loader.sampler.generator = generator
dataloaders.append(data_loader)
elif len(dataloader_list) == 3:
data_loader, generator_state, KGgenerator_state = dataloader_list
generator = torch.Generator()
generator.set_state(generator_state)
data_loader.general_dataloader.generator = generator
data_loader.general_dataloader.sampler.generator = generator
kg_generator = torch.Generator()
kg_generator.set_state(KGgenerator_state)
data_loader.kg_dataloader.generator = kg_generator
data_loader.kg_dataloader.sampler.generator = kg_generator
dataloaders.append(data_loader)
train_data, valid_data, test_data = dataloaders
for arg in dataset_arguments + ["seed", "repeatable", "eval_args"]:
if isinstance(train_data, AbstractDataLoader) and config[arg] != train_data.config[arg]:
return None
elif isinstance(train_data, KnowledgeBasedDataLoader) and config[arg] != train_data.general_dataloader.config[arg]:
return None
train_data.update_config(config)
valid_data.update_config(config)
test_data.update_config(config)
logger = getLogger()
logger.info(
set_color("Load split dataloaders from", "pink")
+ f": [{dataloaders_save_path}]"
)
return train_data, valid_data, test_data
def data_preparation(config, dataset):
"""Split the dataset by :attr:`config['eval_args']` and create training, validation and test dataloader.
Note:
If we can load split dataloaders by :meth:`load_split_dataloaders`, we will not create new split dataloaders.
Args:
config (Config): An instance object of Config, used to record parameter information.
dataset (Dataset): An instance object of Dataset, which contains all interaction records.
Returns:
tuple:
- train_data (AbstractDataLoader): The dataloader for training.
- valid_data (AbstractDataLoader): The dataloader for validation.
- test_data (AbstractDataLoader): The dataloader for testing.
"""
dataloaders = load_split_dataloaders(config)
if dataloaders is not None:
train_data, valid_data, test_data = dataloaders
else:
model_type = config["MODEL_TYPE"]
built_datasets = dataset.build()
train_dataset, valid_dataset, test_dataset = built_datasets
train_sampler, valid_sampler, test_sampler = create_samplers(
config, dataset, built_datasets
)
if model_type != ModelType.KNOWLEDGE:
train_data = get_dataloader(config, "train")(
config, train_dataset, train_sampler, shuffle=config["shuffle"]
)
else:
kg_sampler = KGSampler(
dataset,
config["train_neg_sample_args"]["distribution"],
config["train_neg_sample_args"]["alpha"],
)
train_data = get_dataloader(config, "train")(
config, train_dataset, train_sampler, kg_sampler, shuffle=True
)
valid_data = get_dataloader(config, "evaluation")(
config, valid_dataset, valid_sampler, shuffle=False
)
test_data = get_dataloader(config, "evaluation")(
config, test_dataset, test_sampler, shuffle=False
)
if config["save_dataloaders"]:
save_split_dataloaders(
config, dataloaders=(train_data, valid_data, test_data)
)
logger = getLogger()
logger.info(
set_color("[Training]: ", "pink")
+ set_color("train_batch_size", "cyan")
+ " = "
+ set_color(f'[{config["train_batch_size"]}]', "yellow")
+ set_color(" train_neg_sample_args", "cyan")
+ ": "
+ set_color(f'[{config["train_neg_sample_args"]}]', "yellow")
)
logger.info(
set_color("[Evaluation]: ", "pink")
+ set_color("eval_batch_size", "cyan")
+ " = "
+ set_color(f'[{config["eval_batch_size"]}]', "yellow")
+ set_color(" eval_args", "cyan")
+ ": "
+ set_color(f'[{config["eval_args"]}]', "yellow")
)
return train_data, valid_data, test_data
def get_dataloader(config, phase):
"""Return a dataloader class according to :attr:`config` and :attr:`phase`.
Args:
config (Config): An instance object of Config, used to record parameter information.
phase (str): The stage of dataloader. It can only take two values: 'train' or 'evaluation'.
Returns:
type: The dataloader class that meets the requirements in :attr:`config` and :attr:`phase`.
"""
register_table = {
"MultiDAE": _get_AE_dataloader,
"MultiVAE": _get_AE_dataloader,
"MacridVAE": _get_AE_dataloader,
"CDAE": _get_AE_dataloader,
"ENMF": _get_AE_dataloader,
"RaCT": _get_AE_dataloader,
"RecVAE": _get_AE_dataloader,
}
if config["model"] in register_table:
return register_table[config["model"]](config, phase)
model_type = config["MODEL_TYPE"]
if phase == "train":
if model_type != ModelType.KNOWLEDGE:
return TrainDataLoader
else:
return KnowledgeBasedDataLoader
else:
eval_mode = config["eval_args"]["mode"]
if eval_mode == "full":
return FullSortEvalDataLoader
else:
return NegSampleEvalDataLoader
def _get_AE_dataloader(config, phase):
"""Customized function for VAE models to get correct dataloader class.
Args:
config (Config): An instance object of Config, used to record parameter information.
phase (str): The stage of dataloader. It can only take two values: 'train' or 'evaluation'.
Returns:
type: The dataloader class that meets the requirements in :attr:`config` and :attr:`phase`.
"""
if phase == "train":
return UserDataLoader
else:
eval_mode = config["eval_args"]["mode"]
if eval_mode == "full":
return FullSortEvalDataLoader
else:
return NegSampleEvalDataLoader
def create_samplers(config, dataset, built_datasets):
"""Create sampler for training, validation and testing.
Args:
config (Config): An instance object of Config, used to record parameter information.
dataset (Dataset): An instance object of Dataset, which contains all interaction records.
built_datasets (list of Dataset): A list of split Dataset, which contains dataset for
training, validation and testing.
Returns:
tuple:
- train_sampler (AbstractSampler): The sampler for training.
- valid_sampler (AbstractSampler): The sampler for validation.
- test_sampler (AbstractSampler): The sampler for testing.
"""
phases = ["train", "valid", "test"]
train_neg_sample_args = config["train_neg_sample_args"]
eval_neg_sample_args = config["eval_neg_sample_args"]
sampler = None
train_sampler, valid_sampler, test_sampler = None, None, None
if train_neg_sample_args["distribution"] != "none":
if not config["repeatable"]:
sampler = Sampler(
phases,
built_datasets,
train_neg_sample_args["distribution"],
train_neg_sample_args["alpha"],
)
else:
sampler = RepeatableSampler(
phases,
dataset,
train_neg_sample_args["distribution"],
train_neg_sample_args["alpha"],
)
train_sampler = sampler.set_phase("train")
if eval_neg_sample_args["distribution"] != "none":
if sampler is None:
if not config["repeatable"]:
sampler = Sampler(
phases,
built_datasets,
eval_neg_sample_args["distribution"],
train_neg_sample_args["alpha"],
)
else:
sampler = RepeatableSampler(
phases,
dataset,
eval_neg_sample_args["distribution"],
train_neg_sample_args["alpha"],
)
else:
sampler.set_distribution(eval_neg_sample_args["distribution"])
valid_sampler = sampler.set_phase("valid")
test_sampler = sampler.set_phase("test")
return train_sampler, valid_sampler, test_sampler