forked from stanfordnlp/pyvene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_alignment.py
180 lines (163 loc) · 6.76 KB
/
run_alignment.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
#!/usr/bin/env python
# coding: utf-8
import os, random, argparse, sys, torch
from models.configuration_alignable_model import AlignableLlamaConfig
from trainer import Aligner, CACHE_DIR
import counterfactual_datasets.price_tagging_game as price_tagging_game
from transformers import (
set_seed,
AutoTokenizer,
AutoConfig,
get_linear_schedule_with_warmup
)
from torch.utils.data import DataLoader, SequentialSampler
from models.modelings_alignable import AutoAlignableModel
from transformers.utils import logging
logging.set_verbosity_info()
logger = logging.get_logger("transformers")
if __name__ == '__main__':
is_notebook = False
try:
cmd = argparse.ArgumentParser('The testing components of')
cmd.add_argument('--train_batch_size', default=128, type=int, help='training batch size')
cmd.add_argument('--eval_batch_size', default=128, type=int, help='training batch size')
cmd.add_argument('--lr', default=0.01, type=float, help='learning rate')
cmd.add_argument(
'--encoder_config_path',
type=str, help='path to the encoder config'
)
cmd.add_argument(
'--decoder_config_path',
type=str, help='path to the decoder config'
)
cmd.add_argument('--max_seq_len', default=512, type=int)
cmd.add_argument('--seed', default=42, type=int)
cmd.add_argument('--gradient_accumulation_steps', default=1, type=int)
cmd.add_argument('--output_dir', required=True, type=str, help='save dir')
cmd.add_argument('--local_rank', default=-1, type=int, help='multi gpu training')
cmd.add_argument('--epochs', default=10, type=int, help='training epochs')
cmd.add_argument('--model_path', type=str, required=False, default="../alpaca_7b/")
cmd.add_argument('--warm_up', type=float, default=0.1)
cmd.add_argument('--is_wandb', default=False, action='store_true')
cmd.add_argument('--wandb_username', type=str, default="")
cmd.add_argument('--bf16', default=False, action='store_true')
cmd.add_argument('--log_step', default=10, type=int)
cmd.add_argument('--valid_steps', default=500, type=int)
cmd.add_argument('--early_stopping', default=5, type=int)
cmd.add_argument('--device', default="cuda", type=str, help='')
cmd.add_argument('--do_align', default=False, action='store_true')
cmd.add_argument('--do_eval', default=False, action='store_true')
cmd.add_argument('--do_test', default=False, action='store_true')
cmd.add_argument('--n_training_examples', default=10000, type=int)
cmd.add_argument('--n_eval_examples', default=1000, type=int)
cmd.add_argument('--task_name', default="pricing_tag_lb", type=str, help='')
args = cmd.parse_args(sys.argv[1:])
except:
assert False
set_seed(args.seed)
###################
# data loaders
###################
tokenizer = AutoTokenizer.from_pretrained(
pretrained_model_name_or_path=args.model_path,
cache_dir=CACHE_DIR
)
if args.task_name in ["pricing_tag_lb", "pricing_tag_lub", "pricing_tag_mid_diff", "pricing_tag_bracket", "pricing_tag_fixed"]:
prepare_dataloader_fn = price_tagging_game.prepare_dataloader
prealign_dataloader, train_dataloader, eval_dataloader, test_dataloader = prepare_dataloader_fn(
args, tokenizer
)
###################
# model object loading
###################
#das_config = AlignableLlamaConfig.from_pretrained(
# os.path.join(args.model_path, "das_config")
#)
alignment_config = {
'layer': das_config.das_layer,
"token_range" : [
das_config.das_token_range[0],
das_config.das_token_range[1],
]
}
logger.info(f"alignment_config = {alignment_config}")
model_type = AutoConfig.from_pretrained(args.model_path).architectures[0]
run_name = f"{model_type}.task.{args.task_name}."\
f"seed.{args.seed}.intl.{alignment_config['layer']}.intr.{alignment_config['token_range'][0]}."\
f"{alignment_config['token_range'][1]}"
is_master = True
if not os.path.exists(args.output_dir) and is_master:
os.mkdir(args.output_dir)
os.environ["WANDB_PROJECT"] = f"Boundless-DAS"
output_dir = os.path.join(args.output_dir, run_name)
if not os.path.exists(output_dir) and is_master:
os.mkdir(output_dir)
# now we check whether we can skip ...
# if there is last, we need to skip!
file_path = os.path.join(args.output_dir, run_name, "pytorch-rotate-last.bin")
if os.path.isfile(file_path):
logger.info("Skipping! Found previously finished training run for this experiment.")
quit()
#das_config.save_pretrained(os.path.join(args.output_dir, run_name, "das_config"))
logger.info(f"Loading Pretrained LLM with bf16 = {args.bf16}...")
model = AutoAlignableModel.from_pretrained(
args.model_path,
alignment_config=alignment_config,
torch_dtype=torch.bfloat16 if args.bf16 else None,
cache_dir=CACHE_DIR
)
# set off the gradients among all other layers.
for name, param in model.named_parameters():
if "rotate_layer" not in name and "intervention_boundaries" not in name:
param.requires_grad = False
else:
logger.info(f"Requiring gradients on layer: {name}")
t_total = int(len(train_dataloader) * args.epochs)
warm_up_steps = args.warm_up * t_total
optimizer = torch.optim.Adam(
[{'params': model.model.rotate_layer.parameters()},
{'params': model.model.intervention_boundaries, 'lr': 1e-2}],
lr=args.lr
)
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=warm_up_steps,
num_training_steps=t_total
)
device = "cuda"
model.to(device)
# You can define your custom compute_metrics function.
def compute_metrics(eval_preds, eval_labels):
total_count = 0
correct_count = 0
for eval_pred, eval_label in zip(eval_preds, eval_labels):
actual_test_labels = eval_label[:, -1]
pred_test_labels = torch.argmax(eval_pred[:, -1], dim=-1)
correct_labels = (actual_test_labels==pred_test_labels)
total_count += len(correct_labels)
correct_count += correct_labels.sum().tolist()
accuracy = round(correct_count/total_count, 2)
return {"accuracy" : accuracy}
###################
# trainer loading
###################
aligner = Aligner(
model,
logger=logger,
args=args,
is_master=is_master,
n_gpu=torch.cuda.device_count(),
model_name=run_name,
device=device,
compute_metrics=compute_metrics
)
# Prealign Eval is a must
aligner.prealign_eval(prealign_dataloader, output_dir)
# Train
if args.do_align:
aligner.train(
train_dataloader, eval_dataloader, test_dataloader,
optimizer, scheduler,
log_step=args.log_step, valid_steps=args.valid_steps,
output_dir=output_dir, epochs=args.epochs,
gradient_accumulation_steps=args.gradient_accumulation_steps,
)