-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
195 lines (165 loc) · 6.1 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
from datetime import datetime
from pytz import timezone
import time
from functools import partial
import wandb
import os
import fire
import tqdm
import torch
from torch.distributed.fsdp.wrap import transformer_auto_wrap_policy
import lightning as L
from lightning.fabric.strategies import FSDPStrategy
from transformers import AutoConfig, AutoTokenizer
from model_utils.modeling_llama import LlamaForCausalLM, LlamaDecoderLayer
from main_utils import (
load_jsonl_examples,
get_cosine_lr_decay_fn,
get_grad_norm,
save_checkpoint,
get_last_ckpt_idx)
TIMEZONE = timezone('EST')
DATE = str(datetime.now(tz=TIMEZONE)).split()[0]
MODEL_SIZE = '7b'
PROJECT_NAME = f'amber_{MODEL_SIZE}'
RUN_NAME = f'pretraining_{MODEL_SIZE}_{DATE}'
HF_MODEL_NAME_OR_PATH = f'huggyllama/llama-{MODEL_SIZE}'
WORKDIR = f'workdir_{MODEL_SIZE}'
LEARNING_RATE = 3e-4
LR_SCHEDULE_TYPE = 'cosine'
END_LEARNING_RATE = 3e-5
WARMUP_GRAD_STEPS = 2000
GRAD_NORM_CLIP = 1.
WEIGHT_DECAY = 0.1
BETA1 = 0.9
BETA2 = 0.95
ACCELERATOR = 'cuda'
PRECISION = 'bf16-mixed'
RANDOM_SEED = 11111
TRAIN_DATA_DIR = './data'
TRAIN_EXAMPLES_PER_CHUNK = 1706976
N_CHUNKS = 360
def collate_fn(examples, device):
token_ids = torch.tensor(
[example['token_ids'] for example in examples], device=device)
return {'input_ids': token_ids[:, :-1], 'labels': token_ids[:, 1:]}
def train_chunk(fabric,
tokenizer,
model,
optimizer,
lr_schedule_fn,
examples,
per_device_batch_size,
accumulate_grad_batches,
chunk_idx,
run_wandb):
step = chunk_idx * (len(examples) // per_device_batch_size)
example_batch_idxes = tqdm.trange(
0, len(examples), per_device_batch_size,
desc=f'Training chunk {chunk_idx} (global_micro_batch_size='
f'{per_device_batch_size * fabric.world_size}, '
f'accumulate_grad_batches={accumulate_grad_batches})')
for i in example_batch_idxes:
t0 = time.time()
lr = lr_schedule_fn(step)
step += 1
for param_group in optimizer.param_groups:
param_group["lr"] = lr
is_accumulating = (step % accumulate_grad_batches != 0)
batch = collate_fn(
examples=examples[i:i+per_device_batch_size], device=fabric.device)
input_ids, labels = batch['input_ids'], batch['labels']
with fabric.no_backward_sync(model, enabled=is_accumulating):
logits = model(input_ids).logits
loss = torch.nn.functional.cross_entropy(
logits.reshape((-1, logits.size(-1))), labels.reshape(-1))
fabric.backward(loss / accumulate_grad_batches)
if not is_accumulating:
grad_norm = get_grad_norm(model=model)
fabric.clip_gradients(model, optimizer, max_norm=GRAD_NORM_CLIP)
optimizer.step()
optimizer.zero_grad()
log = {
'loss': loss.item(),
'learning_rate': lr,
'step': step,
'speed(#tok/s/gpu)': int(input_ids.numel() / (time.time() - t0))
}
if not is_accumulating:
log['grad_norm'] = grad_norm
example_batch_idxes.set_postfix(log)
if run_wandb and fabric.global_rank == 0:
wandb.log(log)
save_checkpoint(
fabric=fabric,
tokenizer=tokenizer,
model=model,
optimizer=optimizer,
save_dir=f'{WORKDIR}/ckpt_{chunk_idx}')
def main(n_nodes=1,
n_devices_per_node=4,
per_device_batch_size=10,
accumulate_grad_batches=1,
run_wandb=False):
fabric = L.Fabric(
accelerator=ACCELERATOR,
num_nodes=n_nodes,
devices=n_devices_per_node,
precision=PRECISION,
strategy=FSDPStrategy(
auto_wrap_policy=partial(
transformer_auto_wrap_policy,
transformer_layer_cls={LlamaDecoderLayer}),
activation_checkpointing_policy={LlamaDecoderLayer},
cpu_offload=True,
limit_all_gathers=True))
fabric.launch()
if fabric.global_rank == 0:
os.makedirs(WORKDIR, exist_ok=True)
if run_wandb:
wandb.init(project=PROJECT_NAME, name=RUN_NAME)
last_ckpt_idx = get_last_ckpt_idx(workdir=WORKDIR)
fabric.seed_everything(RANDOM_SEED + last_ckpt_idx + 1)
tokenizer = AutoTokenizer.from_pretrained(HF_MODEL_NAME_OR_PATH)
model = LlamaForCausalLM(
config=AutoConfig.from_pretrained(HF_MODEL_NAME_OR_PATH))
optimizer = torch.optim.AdamW(
model.parameters(),
lr=LEARNING_RATE,
weight_decay=WEIGHT_DECAY,
betas=(BETA1, BETA2),
foreach=False)
model, optimizer = fabric.setup(model, optimizer)
if last_ckpt_idx != -1:
fabric.load(
path=f'{WORKDIR}/ckpt_{last_ckpt_idx}/fabric_ckpt',
state={'model': model, 'optimizer': optimizer})
torch.cuda.empty_cache()
global_micro_batch_size = per_device_batch_size * fabric.world_size
total_steps = TRAIN_EXAMPLES_PER_CHUNK // global_micro_batch_size * N_CHUNKS
lr_schedule_fn = get_cosine_lr_decay_fn(
total_steps=total_steps,
warmup_steps=WARMUP_GRAD_STEPS * accumulate_grad_batches,
learning_rate=LEARNING_RATE,
end_learning_rate=END_LEARNING_RATE)
for chunk_idx in range(last_ckpt_idx + 1, N_CHUNKS):
examples = load_jsonl_examples(
filename=f'{TRAIN_DATA_DIR}/train_{chunk_idx}.jsonl',
n_examples=TRAIN_EXAMPLES_PER_CHUNK,
shuffle=True,
global_micro_batch_size=global_micro_batch_size,
global_rank=fabric.global_rank,
world_size=fabric.world_size)
train_chunk(
fabric=fabric,
tokenizer=tokenizer,
model=model,
optimizer=optimizer,
lr_schedule_fn=lr_schedule_fn,
examples=examples,
per_device_batch_size=per_device_batch_size,
accumulate_grad_batches=accumulate_grad_batches,
chunk_idx=chunk_idx,
run_wandb=run_wandb)
if __name__ == '__main__':
fire.Fire(main)