-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
261 lines (237 loc) · 8.1 KB
/
Copy pathutils.py
File metadata and controls
261 lines (237 loc) · 8.1 KB
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 argparse
import functools
import logging
from contextlib import contextmanager
import torch
from torch.optim.lr_scheduler import LambdaLR
logger = logging.getLogger()
PRECISION_STR_TO_DTYPE = {
"fp16": torch.float16,
"bf16": torch.bfloat16,
"fp32": torch.float32,
"fp64": torch.float64,
}
def init_logger():
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
ch.setFormatter(formatter)
logger.addHandler(ch)
def get_num_params(model: torch.nn.Module, exclude_embedding: bool = False) -> int:
num_params = sum(p.numel() for p in model.parameters())
if exclude_embedding:
num_params -= sum(
sum(p.numel() for p in m.parameters())
for m in model.children()
if isinstance(m, torch.nn.Embedding)
)
return num_params
def get_num_flop_per_token(num_params: int, model_config) -> int:
l, h, q, t = (
model_config.n_layers,
model_config.n_heads,
model_config.dim // model_config.n_heads,
model_config.seq_len,
)
# Reasoning behind the factor of 12 for the self-attention part of the formula:
# 1. each self-attention has 2 matmul in the forward and 4 in the backward (6)
# 2. the flash attention does 1 more matmul recomputation in the backward
# but recomputation should not be counted in calculating MFU (+0)
# 3. each matmul performs 1 multiplication and 1 addition (*2)
# 4. we follow the convention and do not account for sparsity in causal attention
flop_per_token = 6 * num_params + 12 * l * h * q * t
return flop_per_token
def build_lr_scheduler(optimizer: torch.optim, warmup_steps: int):
def linear_warmup_constant(warmup_steps: int, current_step: int) -> float:
"""Computes linear warmup followed by linear decay.
Per LambdaLR requirement, this is accomplished by returning
a multiplicative factor to adjust the learning rate to
create the desired schedule.
"""
if current_step < warmup_steps:
# linear warmup
# 0-indexed step, hence + 1 adjustments
current_step += 1
curr_adjustment = float(current_step / (warmup_steps + 1))
else:
# constant
curr_adjustment = 1
return curr_adjustment
lr_lambda = functools.partial(linear_warmup_constant, warmup_steps)
return LambdaLR(optimizer, lr_lambda)
@torch.no_grad()
def clip_grad_norm_(parameters, grad_max_norm):
grads = [p.grad for p in parameters if p.grad is not None]
total_norm = torch.nn.utils.get_total_norm(grads, error_if_nonfinite=True)
torch.nn.utils.clip_grads_with_norm_(parameters, grad_max_norm, total_norm)
return total_norm
@contextmanager
def set_default_dtype(dtype: torch.dtype):
"""
Context manager to set torch's default dtype.
"""
old_dtype = torch.get_default_dtype()
torch.set_default_dtype(dtype)
try:
yield
finally:
torch.set_default_dtype(old_dtype)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="/capstor/store/cscs/ethz/large-sc/datasets/train_data.parquet",
help="Path to a parquet file containing a 'text' column with documents (`str`)",
)
parser.add_argument(
"--tokenizer-name-or-path",
type=str,
default="unsloth/Mistral-Nemo-Base-2407-bnb-4bit",
help="A path to a directory containing vocabulary files required by the tokenizer or the model id of a predefined tokenizer hosted inside a model repo on the Hugging Face Hub.",
)
parser.add_argument(
"--sequence-length",
type=int,
default=2048,
)
parser.add_argument(
"--batch-size",
type=int,
default=1,
)
parser.add_argument(
"--fused-optimizer",
action="store_true",
help="Set to fuse the optimizer for increased performance or not",
)
parser.add_argument(
"--learning-rate",
type=float,
default=1e-5,
)
parser.add_argument(
"--lr-warmup-steps",
type=int,
default=10,
)
parser.add_argument(
"--training-steps",
type=int,
default=1000,
help="Number of training steps to run. That is not the number of epochs!",
)
parser.add_argument(
"--logging-frequency",
type=int,
default=5,
help="Log every `--logging-frequency` steps",
)
parser.add_argument(
"--profile", action="store_true", help="Profile the run using the NSYS profiler"
)
parser.add_argument(
"--profile-step-start",
type=int,
default=10,
help="Starting step to profile using the NSYS profiler",
)
parser.add_argument(
"--profile-step-end",
type=int,
default=12,
help="Last step to profile using the NSYS profiler",
)
parser.add_argument(
"--grad-max-norm",
type=float,
default=1,
)
parser.add_argument(
"--model-dtype",
type=str,
default="bf16",
help="Model dtype for parameters, gradients and optimizer states. Default: bf16",
)
parser.add_argument(
"--compile",
action="store_true",
help="Set to compile the model with `torch.compile`",
)
parser.add_argument(
"--distributed",
action="store_true",
help="Set to run distributed training. In this case detects number of GPUs and Nodes and launches DDP",
)
parser.add_argument(
"--checkpoint-dir",
type=str,
default="checkpoints/", # default local folder from run dir
help="Directory to save checkpoints to. Default: checkpoints/",
)
parser.add_argument(
"--checkpoint-frequency",
type=int,
default=10,
help="Save checkpoint every `--checkpoint-frequency` steps (training step not checkpoints). If set to -1 no checkpoints are created.",
)
parser.add_argument(
"--resume-from-checkpoint",
type=str,
default=None,
help="Path to a checkpoint to resume training from. Default: None. Does not have to be subfolder of checkpoint dir. If set to 'latest', will resume from latest checkpoint in checkpoint dir.",
)
parser.add_argument(
"--experiment_name",
type=str,
default="default-exp",
help="Name of the experiment. Used to create a subfolder in the checkpoint dir.",
)
parser.add_argument(
"--verify-checkpoints",
action="store_true",
help="Verify checkpoints with checksums",
)
parser.add_argument(
"--max-kept-checkpoints",
type=int,
default=3,
help="Maximum number of checkpoints to keep.",
)
parser.add_argument(
"--use-torch-distributed-ckpt",
action="store_true",
help="Use torch.distributed.checkpoint for more efficient checkpoint saving/loading",
)
parser.add_argument(
"--default-iter-time",
type=float,
default=1.0,
help="Default value for max_iter_time in seconds. Only used if --timeaware-checkpointing is enabled.",
)
parser.add_argument(
"--default-ckpt-time",
type=float,
default=10.0,
help="Default value for max_ckpt_time in seconds. Only used if --timeaware-checkpointing is enabled.",
)
parser.add_argument(
"--timeaware-checkpointing",
action="store_true",
help="Enable time-aware checkpointing and early stopping based on SLURM walltime.",
)
parser.add_argument(
"--use_flash_attention",
action="store_true",
help="Replaces default attention with flash-attention in the transformer. Must install flash-attention first.",
)
parser.add_argument(
"--log-loss-to-csv",
action="store_true",
help="Log loss values to a CSV file in the experiment directory",
)
args = parser.parse_args()
return args