-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
311 lines (253 loc) · 10.2 KB
/
main.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# ruff: noqa: E402
import os
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# os.environ["HF_HOME"] = "/path/to/fast/storage"
import argparse
import time
import shutil
import torch
import torch.nn.functional as F
import torch.distributed as dist
from torch.distributed.fsdp import (
FullyShardedDataParallel as FSDP,
FullStateDictConfig,
StateDictType,
)
from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (
apply_activation_checkpointing,
)
from pathlib import Path
from detectron2.config import LazyConfig, instantiate
from detectron2.solver import LRMultiplier
from detectron2.engine.hooks import LRScheduler
from detectron2.utils.env import seed_all_rng
from human_pref.logging import get_logger
from human_pref.utils import to_gpu
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("config")
parser.add_argument("--load-from", default=None, type=str)
parser.add_argument("--init-only", action="store_true")
parser.add_argument("--eval-only", action="store_true")
parser.add_argument("--no-log-file", action="store_true")
parser.add_argument("--seed", type=int, default=-1)
parser.add_argument("--output-root", default="../artifacts")
parser.add_argument(
"--opts",
help="""
Modify config options at the end of the command, use "path.key=value".
""".strip(),
default=[],
nargs=argparse.ZERO_OR_MORE,
)
parser.add_argument("--out", default=None, type=str)
return parser.parse_args()
class LogLossBuffer:
"""Circular buffer for storing log loss values"""
def __init__(self, size, device="cuda"):
self.buffer = torch.zeros(size, device=device)
self.size = size
self.idx = 0
self.num = 0
def append(self, value):
self.buffer[self.idx] = value
self.idx = (self.idx + 1) % self.size
self.num = min(self.num + 1, self.size)
def mean(self):
return self.buffer.sum().item() / self.num
@torch.no_grad()
def do_test(cfg, model):
logger = get_logger("lmsys")
logger.info("Evaluation start")
val_loader = instantiate(cfg.dataloader.val)
model.eval()
from tqdm import tqdm
rank = dist.get_rank()
world_size = dist.get_world_size()
if rank == 0:
prog_bar = tqdm(val_loader)
else:
prog_bar = val_loader
probs = []
for batch in prog_bar:
for micro_batch in batch:
micro_batch = to_gpu(micro_batch)
prob = model(micro_batch["input_ids"], micro_batch["cu_seqlens"]).softmax(
dim=-1
)
gather_probs = [torch.zeros_like(prob) for _ in range(world_size)]
dist.all_gather(gather_probs, prob)
prob = torch.stack(gather_probs, dim=1).flatten(0, 1)
probs.append(prob.data.cpu())
result = torch.cat(probs, dim=0).numpy()
# the last batch maybe padded to be divisible by world_size
result = result[: len(val_loader.dataset)]
logger.info("Evaluation prediction done")
if not hasattr(val_loader.dataset, "evaluate"):
eval_result = {"info": f"Not implemented for {type(val_loader.dataset)}"}
else:
eval_result = val_loader.dataset.evaluate(result)
logger.info("Evaluation end")
return result, eval_result
def save_checkpoint(model, optimizer, work_dir, checkpoint_path):
save_policy = FullStateDictConfig(offload_to_cpu=True, rank0_only=True)
with FSDP.state_dict_type(model, StateDictType.FULL_STATE_DICT, save_policy):
cpu_state = model.state_dict()
if dist.get_rank() == 0:
checkpoint = {
"model": cpu_state,
# "optimizer": optimizer.state_dict(),
}
torch.save(checkpoint, checkpoint_path)
def do_train(cfg, model):
cfg.optimizer.params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = instantiate(cfg.optimizer)
train_loader = instantiate(cfg.dataloader.train)
max_epochs = cfg.train.max_epochs
lr_scheduler = LRMultiplier(
optimizer,
multiplier=instantiate(cfg.lr_multiplier),
max_iter=max_epochs * len(train_loader),
)
best_param_group_id = LRScheduler.get_best_param_group_id(optimizer)
logger = get_logger("lmsys")
loss_history = LogLossBuffer(cfg.train.get("log_buffer_size", 100))
total_updates = 0
rank = dist.get_rank()
fsdp_loss = torch.zeros(2).to(rank)
clip_grad = cfg.train.get("clip_grad", True)
for curr_epoch in range(max_epochs):
model.train()
for curr_iter, batch in enumerate(train_loader):
total_batch_size = sum(micro_batch["batch_size"] for micro_batch in batch)
fsdp_loss.zero_()
for micro_batch in batch:
micro_batch = to_gpu(micro_batch)
logits = model(micro_batch["input_ids"], micro_batch["cu_seqlens"])
loss = F.cross_entropy(logits, micro_batch["label"])
fsdp_loss[0] += loss.detach() * micro_batch["batch_size"]
fsdp_loss[1] += micro_batch["batch_size"]
loss = loss * (micro_batch["batch_size"] / total_batch_size)
loss.backward()
dist.all_reduce(fsdp_loss, op=dist.ReduceOp.SUM)
if clip_grad:
grad_norm = model.clip_grad_norm_(1.0)
grad_norm = grad_norm.item()
else:
grad_norm = 0
optimizer.step()
optimizer.zero_grad(set_to_none=True)
loss_history.append(fsdp_loss[0] / fsdp_loss[1])
total_updates += 1
lr_scheduler.step()
if total_updates % cfg.train.log_interval == 0:
lr = optimizer.param_groups[best_param_group_id]["lr"]
loss_val = loss_history.mean()
max_mem_mb = torch.cuda.max_memory_allocated() / 1024.0 / 1024.0
logger.info(
f"Epoch [{curr_epoch+1}/{max_epochs}] Iter [{curr_iter+1}/{len(train_loader)}]"
f" lr: {lr:.4e}, loss: {loss_val:.4f}, grad_norm: {grad_norm:.4f}, max_mem: {max_mem_mb:.0f}M"
)
# save every N updates
if total_updates % cfg.train.checkpoint_interval == 0:
checkpoint_path = (
Path(cfg.train.work_dir) / f"update_{total_updates}.pth"
)
logger.info(f"Save checkpoint: {checkpoint_path}")
save_checkpoint(model, optimizer, cfg.train.work_dir, checkpoint_path)
logger.info("Save checkpoint done.")
dist.barrier()
# end of epoch checkpoint
checkpoint_path = Path(cfg.train.work_dir) / "update_last.pth"
logger.info(f"Save checkpoint: {checkpoint_path}")
save_checkpoint(model, optimizer, cfg.train.work_dir, checkpoint_path)
logger.info("Save checkpoint done.")
dist.barrier()
# evaluate
if (curr_epoch + 1) % cfg.train.get("eval_interval", 1) == 0:
result, eval_result = do_test(cfg, model)
if rank == 0:
logger.info(f"Epoch {curr_epoch+1} evaluation result: {eval_result}")
torch.save(
result,
Path(cfg.train.work_dir) / f"result_epoch_{curr_epoch+1}.pth",
)
def setup(args):
dist.init_process_group("nccl")
torch.cuda.set_device(dist.get_rank())
cfg = LazyConfig.load(args.config)
# default work_dir
cfg_path = Path(args.config)
work_dir_root = Path(args.output_root)
work_dir = str(work_dir_root / cfg_path.relative_to("configs/").with_suffix(""))
cfg.train.work_dir = work_dir
# override config
cfg = LazyConfig.apply_overrides(cfg, args.opts)
Path(cfg.train.work_dir).mkdir(parents=True, exist_ok=True)
# dump config
timestamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
if not args.eval_only and dist.get_rank() == 0:
# LazyConfig.save(cfg, str(Path(work_dir) / f"{timestamp}.yaml"))
shutil.copy(args.config, Path(work_dir) / f"{timestamp}.py")
# logger
if args.eval_only or args.no_log_file:
log_file = None
else:
log_file = Path(work_dir) / f"{timestamp}.log"
logger = get_logger("lmsys", log_file=log_file)
logger.info("Start")
# seed
if args.seed >= 0:
seed = args.seed
else:
seed = cfg.train.get("seed", 0)
seed_all_rng(seed)
logger.info(f"Set random seed: {seed}")
return cfg
def clean_up():
dist.destroy_process_group()
def main():
args = parse_args()
cfg = setup(args)
model = instantiate(cfg.model)
logger = get_logger("lmsys")
if args.init_only:
init_path = Path(cfg.train.work_dir) / "initialized.pth"
torch.save(model.state_dict(), init_path)
logger.info(f"Saved initialized model: {init_path}")
if cfg.train.get("cast_to_bf16", False):
logger.info("Casting model to BF16")
# for name, m in model.named_modules():
# m.to(torch.bfloat16)
for p in model.parameters():
p.data = p.data.to(torch.bfloat16)
load_from = cfg.train.get("load_from", None)
if args.load_from is not None:
load_from = args.load_from
if load_from is not None:
checkpoint = torch.load(load_from, map_location="cpu")
if "model" not in checkpoint:
checkpoint = {"model": checkpoint}
load_result = model.load_state_dict(checkpoint["model"], strict=False)
logger.info(f"Load checkpoint: {load_from}")
logger.info(f"Load checkpoint: {load_result}")
logger.info(f"Use sharding strategy: {cfg.fsdp.sharding_strategy}")
model = FSDP(
model,
auto_wrap_policy=cfg.fsdp.auto_wrap_policy,
sharding_strategy=cfg.fsdp.sharding_strategy,
device_id=torch.cuda.current_device(),
mixed_precision=cfg.fsdp.mixed_precision,
)
apply_activation_checkpointing(model, auto_wrap_policy=cfg.fsdp.auto_wrap_policy)
if args.eval_only:
result, eval_result = do_test(cfg, model)
logger.info(f"Evaluation result: {eval_result}")
if args.out is not None:
torch.save(result, args.out)
else:
do_train(cfg, model)
clean_up()
if __name__ == "__main__":
main()