-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsweep.py
230 lines (199 loc) · 8.01 KB
/
sweep.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
#
# SPDX-FileCopyrightText: Copyright © 2024 Idiap Research Institute <[email protected]>
#
# SPDX-FileContributor: Fabio Fehr <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-only
#
from dotenv import load_dotenv
load_dotenv()
import argparse
import os
import lightning.pytorch as pl
import torch
import yaml
from pytorch_lightning.loggers import WandbLogger
import wandb
from data_modules.SummarisationDataModule import SummarisationDataModule
from data_modules.TranslationDataModule import TranslationDataModule
from models_pl.bart_lightning import BartLightning
from models_pl.marian_lightning import MarianLightning
from models_pl.nvibart_lightning import NviBartLightning
from models_pl.nvibmarian_lightning import NvibMarianLightning
from utils import create_or_load_model
def main(args):
"""Main function for hyperparameter sweeps."""
OUTPUT_PATH = os.path.join(args["output_dir"], args["project_name"], args["experiment_name"])
args["output_path"] = OUTPUT_PATH
if not os.path.exists(OUTPUT_PATH):
os.makedirs(OUTPUT_PATH)
# If sweep id exists then load it
if os.path.exists(os.path.join(OUTPUT_PATH, "sweep_id.txt")):
print("Sweep id exists, loading sweep", os.path.join(OUTPUT_PATH, "sweep_id.txt"))
with open(os.path.join(OUTPUT_PATH, "sweep_id.txt")) as f:
sweep_id = f.read()
else:
print("Sweep id does not exist, creating sweep")
# Sweep config
parms = ("method", "metric", "parameters", "entity")
sweep_config = {k: args[k] for k in parms if k in args}
# Create sweep
sweep_id = wandb.sweep(
sweep=sweep_config, project=args["project_name"], entity=args["entity"]
)
# Save sweep id
with open(os.path.join(OUTPUT_PATH, "sweep_id.txt"), "w") as f:
f.write(sweep_id)
# Local scope but define the objective function
def objective():
"""Objective function for hyperparameter sweeps."""
# Use global arguments
global args
# Initialise wandb - This gives us our sweep parameters
wandb.init(project=args["project_name"], entity=args["entity"])
# update args with wandb config
args.update(wandb.config)
# Update experiment name with wandb config
for key, value in wandb.config.items():
args["experiment_name"] = args["experiment_name"] + "_" + key + str(round(value, 2))
args["output_path"] = os.path.join(
args["output_dir"], args["project_name"], args["experiment_name"]
)
# Set seed
pl.seed_everything(args["seed"])
# Explicit model path for evaluation - if None then use instantiated model
MODEL_PATH = args["model_path"]
if "MARIAN" in args["model"]:
translation_flag = True
else:
translation_flag = False
# Select model
model = {
"BART-LARGE-CNN": BartLightning,
"BART-LARGE-XSUM": BartLightning,
"NVIBBART-LARGE-CNN": NviBartLightning,
"NVIBBART-LARGE-XSUM": NviBartLightning,
"MARIAN": MarianLightning,
"NVIBMARIAN": NvibMarianLightning,
}[args["model"]]
# If NVIB model then load empirical distribution
if "NVIB" in args["model"]:
if args["emp_data"] is None:
args["emp_data"] = "gaussian" # No empirical distribution
# Load empirical distribution
if translation_flag:
empirical_distribution_path = os.path.join(
args["output_dir"],
args["project_name"],
"empirical_priors",
args["emp_data"]
+ "_"
+ args["model"].replace("NVIB", "")
+ "_train_perc"
+ str(args["emp_perc"])
+ "_"
+ args["src_lang"]
+ "_"
+ args["tgt_lang"]
+ "_"
+ "embedding_stats.pt",
)
else:
empirical_distribution_path = os.path.join(
args["output_dir"],
args["project_name"],
"empirical_priors",
args["emp_data"]
+ "_"
+ args["model"].replace("NVIB", "")
+ "_"
+ "train_perc"
+ str(args["emp_perc"])
+ "_"
+ "embedding_stats.pt",
)
if os.path.exists(empirical_distribution_path):
print("Loading empirical distribution from: ", empirical_distribution_path)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
empirical_distribution = torch.load(
empirical_distribution_path, map_location=device
)
# Encoder
args["prior_mus_encoder"] = empirical_distribution["encoder_means"]
args["prior_vars_encoder"] = empirical_distribution["encoder_var"]
args["prior_log_alphas_encoder"] = empirical_distribution[
"mean_of_encoder_scaled_l2norm2"
]
args["prior_log_alpha_stdevs_encoder"] = empirical_distribution[
"log_alpha_encoder_std"
]
# Decoder
args["prior_mus_decoder"] = empirical_distribution["decoder_means"]
args["prior_vars_decoder"] = empirical_distribution["decoder_var"]
args["prior_log_alphas_decoder"] = empirical_distribution[
"mean_of_decoder_scaled_l2norm2"
]
args["prior_log_alpha_stdevs_decoder"] = empirical_distribution[
"log_alpha_decoder_std"
]
# Cross
args["prior_mus_cross"] = empirical_distribution["cross_means"]
args["prior_vars_cross"] = empirical_distribution["cross_var"]
args["prior_log_alphas_cross"] = empirical_distribution[
"mean_of_cross_scaled_l2norm2"
]
args["prior_log_alpha_stdevs_cross"] = empirical_distribution[
"log_alpha_cross_std"
]
else:
print("No empirical distribution")
# Load best model
model, wandb_id = create_or_load_model(
args["output_path"], MODEL_PATH, model, argparse.Namespace(**args)
)
wandb_logger = WandbLogger(project=args["project_name"], id=wandb_id, log_model="None")
wandb_logger.log_hyperparams(args)
# Make data module
if translation_flag:
dm = TranslationDataModule(
model, fp16=True if args["quantisation"] == "16bit" else False, **args
)
else:
dm = SummarisationDataModule(
model, fp16=True if args["quantisation"] == "16bit" else False, **args
)
# Trainer
trainer = pl.Trainer(
# limit_val_batches=0.1,
# limit_test_batches=1,
# deterministic=True,
accelerator="auto",
logger=wandb_logger,
precision=16 if args["quantisation"] == "16bit" else 32,
)
# Evaluate model
model.eval()
trainer.validate(model, datamodule=dm)
trainer.test(model, datamodule=dm)
# Define the agent
wandb.agent(
sweep_id,
entity=args["entity"],
project=args["project_name"],
function=objective,
count=args["count"],
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# Config
parser.add_argument(
"--config_path",
default="sweep_configs/sweep_config.yml",
type=str,
help="Sweep name",
)
args = parser.parse_args()
# Load the sweep configuration
with open(args.config_path) as file:
args = yaml.safe_load(file)
main(args)