-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
97 lines (75 loc) · 3.89 KB
/
run.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
import copy
import random
import importlib
import logging
import hydra
from omegaconf import OmegaConf
import numpy as np
import torch
import utils
from trainer import EditTrainer
import models
OmegaConf.register_new_resolver("uuid", lambda: utils.uuid())
logging.basicConfig(format='%(asctime)s - %(levelname)s [%(filename)s:%(lineno)d] %(message)s',
level=logging.INFO)
LOG = logging.getLogger(__name__)
def add_padding(tokenizer, model):
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model.resize_token_embeddings(len(tokenizer))
# model.transformer.wte.weight.data[-1] = model.transformer.wte.weight.data.mean(0)
@hydra.main(config_path='config', config_name='config')
def run(config):
LOG.info(f"\n\n{OmegaConf.to_yaml(config)}\n")
base_dir = hydra.utils.get_original_cwd()
LOG.info(f"Project base directory: {base_dir}")
random.seed(config.seed)
np.random.seed(config.seed)
torch.manual_seed(config.seed)
model = models.get_model(config)
tokenizer = models.get_tokenizer(config)
# if config.task == "gen" or config.task == "wiki":
# add_padding(tokenizer, model)
# from data_classes.wiki import GenDataset
# train_set = GenDataset("train", tokenizer, config, config.data.path, pct=10)
# val_set = GenDataset("validation", tokenizer, config, config.data.path, pct=10)
# elif config.task == "fc" or config.task == "fever":
# from data_classes.fever import BinaryAugmentedKILT
# train_set = BinaryAugmentedKILT(tokenizer, f"{base_dir}/data/fever/fever-train-kilt.jsonl", config)
# val_set = BinaryAugmentedKILT(tokenizer, f"{base_dir}/data/fever/fever-dev-kilt.jsonl", config)
# elif config.task == "qa" or config.task == "zsre":
# from data_classes.zsre import Seq2SeqAugmentedKILT
# train_set = Seq2SeqAugmentedKILT(tokenizer, f"{base_dir}/data/zsre/structured_zeroshot-train-new_annotated_final.jsonl",
# config)
# val_set = Seq2SeqAugmentedKILT(tokenizer, f"{base_dir}/data/zsre/structured_zeroshot-dev-new_annotated_final.jsonl",
# config)
# elif "debias" in config.task and config.dataset == "stereoset":
from data_classes.stereoset import StereoSetDataset
model_name = model.__class__.__name__.lower()
if "gpt" in model_name or "llama"in model_name:
add_padding(tokenizer, model)
if not config.eval_only:
train_set = StereoSetDataset(tokenizer, f"{base_dir}/data/stereoset/train.json", config, model_name)
val_set = StereoSetDataset(tokenizer, f"{base_dir}/data/stereoset/dev.json", config, model_name)
else:
train_set = StereoSetDataset(tokenizer, f"{base_dir}/data/stereoset/train.json", config, model_name)
val_set = StereoSetDataset(tokenizer, config.val_set, config, model_name)
# else:
# raise ValueError(f"Unrecognized task {config.task}")
alg_module = importlib.import_module(f"algs.{config.alg}")
LOG.info(f"Loading class {config.alg.upper()} from module {alg_module}")
AlgClass = getattr(alg_module, config.alg.upper())
alg = AlgClass(model, config, lambda: copy.deepcopy(model), tokenizer=tokenizer) # MEND
# if config.alg == "ft" and config.ft.locality.enabled:
# if config.ft.locality.oracle:
# alg.loc_sampler = train_set.edit_generator(config.ft.locality.batch_size + 1)
# else:
# state = np.random.get_state()
# np.random.seed(0)
# loc_batch = next(train_set.edit_generator(config.ft.locality.batch_size + 1))["loc"]
# np.random.set_state(state)
# alg.loc_ids = loc_batch["input_ids"]
# alg.loc_masks = loc_batch["attention_mask"]
trainer = EditTrainer(alg, tokenizer, config, train_set, val_set) # MEND, config, datasets -> Trainer
trainer.run()
if __name__ == "__main__":
run()