Repository navigation
Expand file tree
/
Copy pathtraining_manager.py
More file actions
298 lines (258 loc) · 11.2 KB
/
Copy pathtraining_manager.py
File metadata and controls
298 lines (258 loc) · 11.2 KB
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
import os
import yaml
# codacy ignore python-use-of-pickle: Pickle usage is safe in this context (local data only).
import pickle
import shutil
import pandas as pd
from pathlib import Path
from warnings import warn
import lightning.pytorch as pl
from lightning.pytorch.profilers import PyTorchProfiler
from lightning.pytorch.tuner import Tuner as LightningTuner
from GANDLF.utils import get_dataframe, split_data
from GANDLF.models.lightning_module import GandlfLightningModule
from GANDLF.data.lightning_datamodule import GandlfTrainingDatamodule
from typing import Optional
def TrainingManager(
dataframe: pd.DataFrame,
outputDir: str,
parameters: dict,
resume: bool,
reset: bool,
profile: Optional[bool] = False,
) -> None:
"""
This is the training manager that ties all the training functionality together
Args:
dataframe (pandas.DataFrame): The full data from CSV.
outputDir (str): The main output directory.
parameters (dict): The parameters dictionary.
resume (bool): Whether the previous run will be resumed or not.
reset (bool): Whether the previous run will be reset or not.
profile(bool): Whether we want the profile activity or not. Defaults to False.
"""
if "output_dir" not in parameters:
parameters["output_dir"] = outputDir
if reset:
shutil.rmtree(outputDir)
Path(outputDir).mkdir(parents=True, exist_ok=True)
# save the current model configuration as a sanity check
currentModelConfigPickle = os.path.join(outputDir, "parameters.pkl")
if (not os.path.exists(currentModelConfigPickle)) or reset or resume:
with open(currentModelConfigPickle, "wb") as handle:
pickle.dump(parameters, handle, protocol=pickle.HIGHEST_PROTOCOL)
else:
if os.path.exists(currentModelConfigPickle):
print(
"Using previously saved parameter file",
currentModelConfigPickle,
flush=True,
)
parameters = pickle.load(open(currentModelConfigPickle, "rb"))
dataframe_split = split_data(dataframe, parameters)
last_indeces, _, _, _ = dataframe_split[-1]
# check the last indeces to see if single fold training is requested
singleFoldTesting = True if last_indeces[0] == 0 else False
singleFoldValidation = True if last_indeces[1] == 0 else False
for (
testing_and_valid_indeces,
trainingData,
validationData,
testingData,
) in dataframe_split:
# the output of the current fold is only needed if multi-fold training is happening
currentTestingOutputFolder = outputDir
if not singleFoldTesting:
currentTestingOutputFolder = os.path.join(
outputDir, "testing_" + str(testing_and_valid_indeces[0])
)
Path(currentTestingOutputFolder).mkdir(parents=True, exist_ok=True)
currentValidationOutputFolder = currentTestingOutputFolder
if not singleFoldValidation:
currentValidationOutputFolder = os.path.join(
currentTestingOutputFolder, str(testing_and_valid_indeces[1])
)
Path(currentValidationOutputFolder).mkdir(parents=True, exist_ok=True)
# initialize the dataframes and save them to disk
data_dict = {
"training": trainingData,
"validation": validationData,
"testing": testingData,
}
data_dict_files = {}
for data_type, data in data_dict.items():
data_dict_files[data_type] = None
if data is not None:
currentDataPickle = os.path.join(
currentValidationOutputFolder, "data_" + data_type + ".pkl"
)
data_dict_files[data_type] = currentDataPickle
if (not os.path.exists(currentDataPickle)) or reset or resume:
data.to_pickle(currentDataPickle)
data.to_csv(currentDataPickle.replace(".pkl", ".csv"), index=False)
else:
# read the data from the pickle if present
data_dict[data_type] = get_dataframe(currentDataPickle)
# Dataloader initialization - should be extracted somewhere else (preferably abstracted away)
datamodule = GandlfTrainingDatamodule(data_dict_files, parameters)
parameters = datamodule.updated_parameters_dict
# This entire section should be handled in config parser
accelerator = parameters.get("accelerator", "auto")
allowed_accelerators = ["cpu", "gpu", "auto"]
# codacy ignore Generic/ReDoS: This is not a SQL query, it's an error message.
assert (
accelerator in allowed_accelerators
), f"Invalid accelerator selected: {accelerator}. Please select from {allowed_accelerators}"
strategy = parameters.get("strategy", "auto")
allowed_strategies = ["auto", "ddp"]
# codacy ignore Generic/ReDoS: This is not a SQL query, it's an error message.
assert (
strategy in allowed_strategies
), f"Invalid strategy selected: {strategy}. Please select from {allowed_strategies}"
precision = parameters.get("precision", "32")
allowed_precisions = [
"64",
"64-true",
"32",
"32-true",
"16",
"16-mixed",
"bf16",
"bf16-mixed",
]
# codacy ignore Generic/ReDoS: This is not a SQL query, it's an error message.
assert (
precision in allowed_precisions
), f"Invalid precision selected: {precision}. Please select from {allowed_precisions}"
warn(
f"Configured to use {accelerator} with {strategy} for training, but current development configuration will force single-device only training."
)
trainer = pl.Trainer(
accelerator=accelerator,
strategy=strategy,
fast_dev_run=False,
devices=parameters.get("devices", "auto"),
num_nodes=parameters.get("num_nodes", 1),
precision=precision,
gradient_clip_algorithm=parameters["clip_mode"],
gradient_clip_val=parameters["clip_grad"],
max_epochs=parameters["num_epochs"],
sync_batchnorm=False,
enable_checkpointing=False,
logger=False,
num_sanity_val_steps=0,
profiler=PyTorchProfiler(sort_by="cpu_time_total", row_limit=10)
if profile
else None,
)
lightning_module = GandlfLightningModule(
parameters, output_dir=currentValidationOutputFolder
)
if parameters.get("auto_batch_size_find", False):
LightningTuner(trainer).scale_batch_size(
lightning_module, datamodule=datamodule
)
if parameters.get("auto_lr_find", False):
LightningTuner(trainer).lr_find(lightning_module, datamodule=datamodule)
trainer.fit(lightning_module, datamodule=datamodule)
testing_data = data_dict_files.get("testing", None)
if testing_data:
trainer.test(lightning_module, datamodule=datamodule)
def TrainingManager_split(
dataframe_train: pd.DataFrame,
dataframe_validation: pd.DataFrame,
dataframe_testing: pd.DataFrame,
outputDir: str,
parameters: dict,
resume: bool,
reset: bool,
profile: Optional[bool] = False,
):
"""
This is the training manager that ties all the training functionality together
Args:
dataframe_train (pd.DataFrame): The training data from CSV.
dataframe_validation (pd.DataFrame): The validation data from CSV.
dataframe_testing (pd.DataFrame): The testing data from CSV.
outputDir (str): The main output directory.
parameters (dict): The parameters dictionary.
resume (bool): Whether the previous run will be resumed or not.
reset (bool): Whether the previous run will be reset or not.
profile(bool): Whether the we want the profile activity or not. Defaults to False.
"""
currentModelConfigPickle = os.path.join(outputDir, "parameters.pkl")
currentModelConfigYaml = os.path.join(outputDir, "config.yaml")
if (not os.path.exists(currentModelConfigPickle)) or reset or resume:
with open(currentModelConfigPickle, "wb") as handle:
pickle.dump(parameters, handle, protocol=pickle.HIGHEST_PROTOCOL)
else:
if os.path.exists(currentModelConfigPickle):
print(
"Using previously saved parameter file",
currentModelConfigPickle,
flush=True,
)
parameters = pickle.load(open(currentModelConfigPickle, "rb"))
if (not os.path.exists(currentModelConfigYaml)) or reset or resume:
with open(currentModelConfigYaml, "w") as handle:
yaml.dump(parameters, handle, default_flow_style=False)
data_dict_files = {
"training": dataframe_train,
"validation": dataframe_validation,
"testing": dataframe_testing,
}
datamodule = GandlfTrainingDatamodule(data_dict_files, parameters)
parameters = datamodule.updated_parameters_dict
# This entire section should be handled in config parser
accelerator = parameters.get("accelerator", "auto")
allowed_accelerators = ["cpu", "gpu", "auto"]
assert (
accelerator in allowed_accelerators
), f"Invalid accelerator selected: {accelerator}. Please select from {allowed_accelerators}"
strategy = parameters.get("strategy", "auto")
allowed_strategies = ["auto", "ddp"]
assert (
strategy in allowed_strategies
), f"Invalid strategy selected: {strategy}. Please select from {allowed_strategies}"
precision = parameters.get("precision", "32")
allowed_precisions = [
"64",
"64-true",
"32",
"32-true",
"16",
"16-mixed",
"bf16",
"bf16-mixed",
]
assert (
precision in allowed_precisions
), f"Invalid precision selected: {precision}. Please select from {allowed_precisions}"
trainer = pl.Trainer(
accelerator=accelerator,
strategy=strategy,
fast_dev_run=False,
devices=parameters.get("devices", "auto"),
num_nodes=parameters.get("num_nodes", 1),
precision=precision,
gradient_clip_algorithm=parameters["clip_mode"],
gradient_clip_val=parameters["clip_grad"],
max_epochs=parameters["num_epochs"],
sync_batchnorm=False,
enable_checkpointing=False,
logger=False,
num_sanity_val_steps=0,
profiler=PyTorchProfiler(sort_by="cpu_time_total", row_limit=10)
if profile
else None,
)
lightning_module = GandlfLightningModule(parameters, output_dir=outputDir)
if parameters.get("auto_batch_size_find", False):
LightningTuner(trainer).scale_batch_size(
lightning_module, datamodule=datamodule
)
if parameters.get("auto_lr_find", False):
LightningTuner(trainer).lr_find(lightning_module, datamodule=datamodule)
trainer.fit(lightning_module, datamodule=datamodule)
if dataframe_testing is not None:
trainer.test(lightning_module, datamodule=datamodule)