-
Notifications
You must be signed in to change notification settings - Fork 20
/
train_utils.py
261 lines (227 loc) · 8.73 KB
/
train_utils.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
import os
from dataclasses import asdict
from functools import partial
try:
import packaging.version
except ImportError:
from pkg_resources import packaging # type: ignore
import time
from datetime import timedelta
import torch.cuda.nccl as nccl
import torch.distributed as dist
from torch.distributed.fsdp import ShardingStrategy
from fms_fsdp.policies import *
def train(
cfg,
model,
local_rank,
rank,
train_loader,
optimizer,
scheduler,
profiler,
checkpointer,
start_step,
tokens_seen,
):
if cfg.tracker:
if cfg.tracker not in ["wandb", "aim"]:
raise ValueError(f"tracker {cfg.tracker} not supported.")
tracker_dir = cfg.tracker_dir
project_name = cfg.tracker_project_name
run_id = cfg.tracker_run_id
if cfg.tracker == "wandb":
try:
import wandb # type: ignore
except ImportError:
raise ImportError("tracker is set to wandb but wandb is not installed.")
if rank == 0:
print(f"--> wandb is enabled!")
try:
wandb.init(
project=project_name,
dir=tracker_dir,
resume="allow",
id=run_id,
)
except wandb.errors.UsageError:
raise ValueError(
"wandb failed to init, did you pass your wandb api key via WANDB_API_KEY?"
)
wandb.config = asdict(cfg)
if cfg.tracker == "aim":
try:
from aim import Run # type: ignore
except ImportError:
raise ImportError("tracker is set to aim but aim is not installed.")
if rank == 0:
print(f"--> aim is enabled!")
run = Run(
experiment=project_name,
repo=tracker_dir,
run_hash=run_id,
)
run["hparams"] = asdict(cfg)
model.train()
ddp_stats = torch.zeros(3).to(local_rank)
start = time.time()
loop_start = time.time()
for batch_idx, (input, label) in enumerate(train_loader, start=start_step + 1):
if batch_idx > cfg.num_steps:
break
input = input.to(local_rank)
label = label.to(local_rank)
optimizer.zero_grad()
output = model(input)
output = output.logits if hasattr(output, "logits") else output
ce_loss = torch.nn.CrossEntropyLoss()
loss = ce_loss(output.view(-1, output.size(-1)), label.view(-1).long())
loss.backward()
ddp_stats[1] += model.clip_grad_norm_(cfg.grad_clip_thresh).item()
optimizer.step()
scheduler.step()
ddp_stats[0] += loss.item()
ddp_stats[2] += 1
if profiler:
profiler.step()
if batch_idx % cfg.report_interval == 0:
dist.all_reduce(ddp_stats, op=dist.ReduceOp.SUM)
train_loss = ddp_stats[0] / ddp_stats[2]
g_norm = ddp_stats[1] / ddp_stats[2]
elapsed_time = time.time() - loop_start
world_size = int(os.environ["WORLD_SIZE"])
new_tokens_seen = (
(batch_idx - start_step) * world_size * cfg.batch_size * cfg.seq_length
)
if rank == 0:
total_tokens_seen = tokens_seen + new_tokens_seen
current_loss = train_loss.item()
current_lr = scheduler.get_last_lr()[0]
current_gnorm = g_norm.item()
current_step_time = (time.time() - start) / cfg.report_interval
overall_step_time = elapsed_time / (batch_idx - start_step)
current_throughput = int(
cfg.batch_size * cfg.seq_length / current_step_time
)
overall_throughput = int(
cfg.batch_size * cfg.seq_length / overall_step_time
)
reserved_mem = torch.cuda.max_memory_reserved(
device=torch.cuda.current_device()
)
allocated_mem = torch.cuda.max_memory_allocated(
device=torch.cuda.current_device()
)
print("step:", batch_idx)
print("loss:", current_loss)
print("LR:", current_lr)
print("tokens seen:", total_tokens_seen)
print("gradient norm:", current_gnorm)
print("reserved memory:", reserved_mem)
print("allocated memory:", allocated_mem)
print("current step time:", current_step_time)
print("overall step time:", overall_step_time)
print("current token per gpu per sec:", current_throughput)
print("overall token per gpu per sec:", overall_throughput)
print(
"overall token per day:",
int(new_tokens_seen / elapsed_time * 3600 * 24),
)
if cfg.tracker:
vals_to_track = {
"learning rate": current_lr,
"loss": current_loss,
"gradient norm": current_gnorm,
"token seen": total_tokens_seen,
"current throughput (token per gpu per sec)": current_throughput,
"overall throughput (token per gpu per sec)": overall_throughput,
"gpu reserved memory": reserved_mem,
"gpu allocated memory": allocated_mem,
}
if cfg.tracker == "wandb":
tracker_fn = wandb.log
elif cfg.tracker == "aim":
tracker_fn = run.track
tracker_fn(vals_to_track, step=batch_idx)
start = time.time()
ddp_stats.zero_()
torch.cuda.reset_peak_memory_stats(device=torch.cuda.current_device())
if batch_idx % cfg.checkpoint_interval == 0:
checkpointer.save(
batch_idx,
model,
optimizer,
train_loader,
tokens_seen=tokens_seen + new_tokens_seen,
)
return train_loss
def setup():
dist.init_process_group("nccl", timeout=timedelta(seconds=60 * 60))
def setup_environ_flags():
os.environ["TORCH_SHOW_CPP_STACKTRACES"] = str(1)
os.environ["NCCL_ASYNC_ERROR_HANDLING"] = str(1)
def get_policies(cfg, rank, block):
"""Get policies for mixed precision, wrapping, sharding, ac and param init function."""
# mixed precision
verify_bfloat_support = (
torch.version.cuda
and torch.cuda.is_bf16_supported()
and packaging.version.parse(torch.version.cuda).release >= (11, 0)
and dist.is_nccl_available()
and nccl.version() >= (2, 10)
)
if cfg.mixed_precision:
bf16_ready = verify_bfloat_support
if bf16_ready:
mixed_precision_policy = bfSixteen
if rank == 0:
print(f"bFloat16 enabled for mixed precision - using bfSixteen policy")
else:
mixed_precision_policy = fpSixteen
if rank == 0:
print(f"FP16 enabled")
else:
mixed_precision_policy = None
# wrapping policy
wrapping_policy = get_wrapper(block)
# sharding strategy
if cfg.sharding_strategy == "fsdp":
sharding_strategy = ShardingStrategy.FULL_SHARD
elif cfg.sharding_strategy == "hsdp":
sharding_strategy = ShardingStrategy.HYBRID_SHARD
elif cfg.sharding_strategy == "ddp":
sharding_strategy = ShardingStrategy.NO_SHARD
else:
sharding_strategy = ShardingStrategy.FULL_SHARD
if rank == 0:
print(f"Sharding strategy = {cfg.sharding_strategy}")
# ac handler
apply_selective_ac = partial(apply_fsdp_checkpointing, block=block)
# param init function
if cfg.low_cpu_fsdp:
param_init_fn = param_init_function
else:
param_init_fn = None
return (
mixed_precision_policy,
wrapping_policy,
sharding_strategy,
apply_selective_ac,
param_init_fn,
)
def get_profiler(cfg, rank):
if not cfg.use_profiler:
return
if cfg.profiler_rank0_only and rank != 0:
return
return torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
],
schedule=torch.profiler.schedule(wait=1, warmup=2, active=3, repeat=1),
on_trace_ready=torch.profiler.tensorboard_trace_handler("profile_traces"),
profile_memory=True,
with_stack=False,
record_shapes=True,
)