-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
296 lines (251 loc) · 11.6 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
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
import argparse
import os
from path_config import SAVEPATH, CACHEDIR, model_path, map_config
def tf_str(tf):
s = "true" if tf else "false"
return s
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def parse_path(eval_path):
attn_type = "concat_recur"
n_tok = 1
max_k = 32
for i in range(1, max_k + 1):
if (f"-ntok{i}" in eval_path):
n_tok = i
print(f"Update n_tok as {n_tok}")
if "concat_recur" in eval_path:
attn_type = "concat_recur"
elif "merge_recur" in eval_path:
attn_type = "merge_recur"
elif "concat" in eval_path:
attn_type = "concat"
elif "merge" in eval_path:
attn_type = "merge"
else:
print("No attention type matched!")
return attn_type, n_tok
def run(args):
base_cmd = f"python -B -m src.train"
if "debug" not in args.model:
if args.load_path != '':
assert args.model in args.load_path, "Check model and load_path"
if args.eval_path != '':
args.train = False
assert args.model in args.eval_path, "Check model and eval_path"
else:
args.no_wandb = True
## Config file
model = args.model
base_cmd = f"{base_cmd} +{args.dataset}={map_config(model)} model.model_name_or_path={model_path(model)}"
base_cmd = f"{base_cmd} training.do_train={tf_str(args.train)}"
## Data config
if args.dataset == 'metaicl':
base_cmd = f"{base_cmd} data.k={args.k}"
if args.dataset == 'lamp':
base_cmd = f"{base_cmd} data.dataset_name=lamp2"
base_cmd = f"{base_cmd} data.k={args.k}"
## Compression config
if args.comp_type in ["no", "neg_control"]:
base_cmd = f"{base_cmd} training.comp.add_comp_token=false"
args.cond_lora = args.sepembed = False
base_cmd = f"{base_cmd} training.comp.relative_embedding=base"
if args.attn_type == "gist":
args.comp_type = "fixed"
base_cmd = f"{base_cmd} training.comp.comp_type={args.comp_type}"
method_tag = args.comp_type
if args.eval_path != '':
args.attn_type, args.n_tok = parse_path(args.eval_path)
args.sink = True if "-sink" in args.eval_path else False
if args.comp_type not in ["no", "neg_control"]:
base_cmd = f"{base_cmd} training.comp.attn_type={args.attn_type}"
method_tag += f"-{args.attn_type}"
if args.n_tok > 1:
base_cmd = f"{base_cmd} training.comp.num_comp_tokens={args.n_tok}"
method_tag += f"-ntok{args.n_tok}"
if args.sink:
base_cmd = f"{base_cmd} training.comp.sink=true"
method_tag += f"-sink"
## Model config
model_tag = ''
if model.startswith("llama"):
if args.lora_r > 0:
base_cmd = f"{base_cmd} training.lora_r={args.lora_r}"
method_tag = f"r{args.lora_r}-{method_tag}"
if args.per_device_train_batch_size >= 1:
bs = args.per_device_train_batch_size
base_cmd = f"{base_cmd} training.per_device_train_batch_size={bs}"
base_cmd = f"{base_cmd} training.gradient_accumulation_steps={128//bs}"
args.tag += f"_batch{bs}"
if args.max_steps > 0:
st = args.max_steps
args.tag += f"_{st}"
base_cmd = f"{base_cmd} training.max_steps={st}"
base_cmd = f"{base_cmd} training.save_steps={st//4} training.eval_steps={st//2}"
## Training config
if args.lr > 0:
args.tag += f"_lr{args.lr}"
base_cmd = f"{base_cmd} training.learning_rate={args.lr}"
if args.seed != 42:
base_cmd = f"{base_cmd} training.seed={args.seed}"
args.tag += f"_seed{args.seed}"
## Conditional Lora
if not args.cond_lora:
base_cmd = f"{base_cmd} training.comp.cond_lora=false"
if not args.sepembed:
base_cmd = f"{base_cmd} training.comp.separate_embed=false"
## Logging path
if args.train_dataset is None:
wandb_group = wandb_group_out = args.dataset
else:
wandb_group = wandb_group_out = args.train_dataset
args.tag += f"_{args.dataset}"
# Set different folder for longer max context length
if model.startswith('llama') and args.max_length > 1024:
base_cmd = f"{base_cmd} data.max_length={args.max_length}"
wandb_group_out = f"{args.dataset}-len{args.max_length}"
generation_max_length = args.max_length + args.n_tok * args.k + 128
base_cmd = f"{base_cmd} training.generation_max_length={generation_max_length}"
## Evaluation path
if args.dataset in ["metaicl", "lamp"] and (args.k != 16 or args.eval_path != ''):
method_tag += f"-k{args.k}"
if args.tag != '':
method_tag += f"{args.tag}"
# Load a compression adapter/model for evaluation
if args.eval_path != '':
subfolder = 'test'
training_output_dir = f"{SAVEPATH}/{wandb_group_out}/{subfolder}/{args.eval_path}"
training_output_dir += f"-{method_tag}"
wandb_name = f"{subfolder}-{os.path.basename(training_output_dir)}"
# Load finetuned adapter/model
if args.eval_path.startswith("finetune"):
assert args.load_path != '', "Check args.load_path! (e.g., llama-7b-no)"
if args.load_path != '':
load_path = f"{SAVEPATH}/{wandb_group}/{args.load_path}"
base_cmd = f"{base_cmd} training.load_path={load_path}"
eval_path = f"{SAVEPATH}/{wandb_group}/{args.eval_path}"
base_cmd = f"{base_cmd} training.eval_path={eval_path}"
# Load finetuned adapter/model before training a compression adapter/model
elif args.load_path != '':
subfolder = 'finetune'
training_output_dir = f"{SAVEPATH}/{wandb_group_out}/{subfolder}/{args.load_path}"
training_output_dir += f"{model_tag}-{method_tag}"
wandb_name = f"{subfolder}-{os.path.basename(training_output_dir)}"
load_path = f"{SAVEPATH}/{wandb_group}/{args.load_path}"
base_cmd = f"{base_cmd} training.load_path={load_path}"
else:
wandb_name = f"{model}{model_tag}-{method_tag}"
training_output_dir = f"{SAVEPATH}/{wandb_group_out}/{wandb_name}"
if "debug" in model:
wandb_name = "debug"
training_output_dir = f"{SAVEPATH}/{wandb_group_out}/{wandb_name}"
base_cmd = f"{base_cmd} wandb.group={wandb_group}"
base_cmd = f"{base_cmd} wandb.name={wandb_name}"
base_cmd = f"{base_cmd} training.output_dir={training_output_dir}"
base_cmd = f"{base_cmd} training.eval_rouge={args.eval_rouge}"
base_cmd = f"{base_cmd} model.cache_dir={CACHEDIR}"
# No wandb for testing
if (args.no_wandb) or not args.train:
base_cmd = f"{base_cmd} wandb.log=false"
cmd = base_cmd.split()
print(cmd)
os.execvp(cmd[0], cmd)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# Model
parser.add_argument("--model",
"-m",
default="llama-7b",
choices=[
"llama-7b", "llama-13b", "llama-2-7b", "llama-2-7b-chat", "llama-2-13b",
"llama-2-13b-chat", "mistral-7b", "mistral-7b-inst", "flan-t5-base",
"flan-t5-large", "llama-debug", "mistral-debug"
])
parser.add_argument("--sepembed",
type=str2bool,
default=True,
help="Train only embeddings for COMP tokens")
parser.add_argument("--cond_lora", type=str2bool, default=True, help="Use conditional LoRA")
parser.add_argument("--lora_r",
"-r",
type=int,
default=-1,
help="LoRA rank size (default settings are provided in src/config)")
## Notes on compression types ##
## 1. 'no' refers to the full attention model without compression.
## 2. 'neg_control' refers to the full attention model attending only the input without context.
## 3. 'fixed' refers to the fixed compression method.
## 4. 'online' refers to the online compression method (ours).
parser.add_argument("--comp_type",
default="online",
choices=["no", "pos_control", "neg_control", "fixed", "online"],
help="Compression type")
## Notes on attention types ##
## 1. 'concat_recur/merge_recur' refers to CCM-concat/-merge
## 2. 'concat/merge' are the variants of CCM-concat/-merge.
## During compression, they do not attend to the previous memory state, attending only to the current context.
## When generating outputs, they attend to the concatenated/merged memory states.
## This strategy shows slightly better performance in MetaICL or LaMP.
## 3. 'gist' refers to the Gisting compression method.
parser.add_argument("--attn_type",
default="concat_recur",
choices=["concat_recur", "merge_recur", "concat", "merge", "gist"],
help="Attention type")
parser.add_argument("--n_tok",
type=int,
default=1,
help="Number of COMP tokens for each time step")
## Notes on datasets ##
## 'unified' refers to the mixture of MetaICL and SODA (Table 15 of our arXiv paper).
## 'pretrain' refers to the mixture of RedPajama-V2, LmSys-Chat, MetaICL, SODA (Table 4 of our arXiv paper).
parser.add_argument("--dataset",
"-d",
default='metaicl',
choices=['unified', 'metaicl', 'dialog', 'soda', 'lamp', 'pretrain'],
help="Training/evaluation dataset.")
parser.add_argument(
"--train_dataset",
type=str,
default=None,
help=
"Training dataset of the evaluation model. Use when the training dataset is differ from the evaluation dataset."
)
parser.add_argument("--k",
type=int,
default=16,
help="Max number of context time steps (metaicl, LaMP)")
parser.add_argument("--max_length",
type=int,
default=1024,
help="Max token length for each data sample")
# Training
parser.add_argument("--train", action="store_true", help="Do finetuning")
parser.add_argument("--sink",
action="store_true",
help="Keep attention sink (BOS token) during compression training")
parser.add_argument("--max_steps", type=int, default=-1, help="Max finetuning steps")
parser.add_argument("--lr", type=float, default=-1, help="Learning rate")
parser.add_argument("--per_device_train_batch_size", '-b', type=int, default=-1)
parser.add_argument("--seed", type=int, default=42, help="Random seed")
# Eval
parser.add_argument(
"--load_path",
type=str,
default='',
help=
"Path for the full-context finetuned adapter (not required for when using the already finetuned models, e.g., LLaMA-2-chat)"
)
parser.add_argument("--eval_path", type=str, default='', help="Compression adapter path")
parser.add_argument("--generation_max_length", type=int, default=-1)
parser.add_argument("--eval_rouge", action="store_true", help="Evaluate generation performance")
parser.add_argument("--no_wandb", action="store_true")
parser.add_argument("--tag", type=str, default='')
args = parser.parse_args()
run(args)