-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathbenchmark.py
492 lines (437 loc) · 20.1 KB
/
benchmark.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# Copyright 2024 onwards Answer.AI, LightOn, and contributors
# License: Apache-2.0
import enum
import time
from pathlib import Path
from typing import Annotated, List, Optional
import numpy as np
import pandas as pd
import pynvml
import torch
import typer
import yaml
from rich import print
from rich.progress import BarColumn, Progress, TextColumn, TimeElapsedColumn, TimeRemainingColumn
from torch.utils.data import DataLoader, TensorDataset
from typer import Option
from src.bert_layers.configuration_bert import FlexBertConfig
from src.bert_layers.model import FlexBertForMaskedLM, FlexBertForSequenceClassification
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}, pretty_exceptions_show_locals=False)
class ModelType(str, enum.Enum):
mlm = "mlm"
seqcls = "seqcls"
# from maxb2: https://github.com/tiangolo/typer/issues/86#issuecomment-996374166
def conf_callback(ctx: typer.Context, param: typer.CallbackParam, config: Optional[str] = None):
if config is not None:
typer.echo(f"Loading config file: {config}\n")
try:
with open(config, "r") as f: # Load config file
conf = yaml.safe_load(f)
ctx.default_map = ctx.default_map or {} # Initialize the default map
ctx.default_map.update(conf) # Merge the config dict into default_map
except Exception as ex:
raise typer.BadParameter(str(ex))
return config
def get_model(
hidden_size: int,
num_hidden_layers: int,
intermediate_size: float,
parallel_attn: bool = True,
vocab_size: int = 32768,
model_type: ModelType = ModelType.mlm,
sliding_window: int = -1,
global_attn_every_n_layers: int = -1,
normalization: str = "layernorm",
compile_model: bool = True,
masked_prediction: bool = False,
):
config = FlexBertConfig(
num_attention_heads=hidden_size // 64,
hidden_size=hidden_size,
num_hidden_layers=num_hidden_layers,
intermediate_size=intermediate_size,
vocab_size=vocab_size,
attention_layer="rope_parallel" if parallel_attn else "rope",
attention_probs_dropout_prob=0.0,
attn_out_bias=False,
attn_out_dropout_prob=0.0,
attn_qkv_bias=False,
bert_layer="parallel_prenorm" if parallel_attn else "prenorm",
decoder_bias=False,
embed_dropout_prob=0.0,
embed_norm=True,
final_norm=False,
embedding_layer="sans_pos",
encoder_layer="base",
hidden_act="gelu",
loss_function="fa_cross_entropy",
loss_kwargs={"reduction": "mean"},
mlp_dropout_prob=0.0,
mlp_in_bias=False,
mlp_layer="parallel_glu" if parallel_attn else "glu",
mlp_out_bias=False,
norm_kwargs={"eps": 1e-5},
normalization=normalization,
padding="padded",
head_class_act="silu",
head_class_bias=False,
head_class_dropout=0.0,
head_class_norm=False,
head_pred_act="gelu",
head_pred_bias=False,
head_pred_dropout=0.0,
head_pred_norm=True,
pooling_type="cls",
rotary_emb_dim=None,
rotary_emb_base=10000.0,
rotary_emb_scale_base=None,
rotary_emb_interleaved=False,
use_fa2=True,
use_sdpa_attn_mask=False,
allow_embedding_resizing=False,
init_method="default",
init_std=0.02,
init_cutoff_factor=2.0,
init_small_embedding=False,
initial_attention_layer=None,
initial_bert_layer=None,
initial_mlp_layer=None,
num_initial_layers=1,
skip_first_prenorm=False,
sliding_window=sliding_window,
global_attn_every_n_layers=global_attn_every_n_layers,
unpad_embeddings=True,
pad_logits=False,
compile_model=compile_model,
masked_prediction=masked_prediction,
)
if model_type == ModelType.mlm:
config.tie_word_embeddings = True
return FlexBertForMaskedLM(config)
elif model_type == ModelType.seqcls:
config.num_labels = 5
return FlexBertForSequenceClassification(config)
else:
raise ValueError(f"Invalid model type: {model_type}")
def get_gpu_power(gpu_idx=0):
handle = pynvml.nvmlDeviceGetHandleByIndex(gpu_idx) # Assuming we're using the first GPU
power = pynvml.nvmlDeviceGetPowerUsage(handle) / 1000.0 # Convert mW to W
return power
def benchmark_training(model, dataloader, num_warmup_batches=10, gpu_idx=0):
model.train()
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)
device = next(model.parameters()).device
torch.cuda.reset_peak_memory_stats()
power_readings = []
max_allocated_memory = 0
max_reserved_memory = 0
with Progress(
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeRemainingColumn(),
TimeElapsedColumn(),
) as progress:
warmup_task = progress.add_task("[yellow]Warmup", total=num_warmup_batches)
for i, batch in enumerate(dataloader):
if i >= num_warmup_batches:
break
input_ids, attention_mask, labels = [t.to(device) for t in batch]
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
loss = outputs.loss
loss.backward()
optimizer.step()
optimizer.zero_grad()
progress.update(warmup_task, advance=1)
train_task = progress.add_task("[green]Training", total=len(dataloader))
total_time = 0
epoch_start_time = time.time()
for i, batch in enumerate(dataloader):
input_ids, attention_mask, labels = [t.to(device) for t in batch]
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
loss = outputs.loss
loss.backward()
optimizer.step()
optimizer.zero_grad()
progress.update(train_task, advance=1)
if i % 10 == 0:
power_readings.append(get_gpu_power(gpu_idx))
max_allocated_memory = max(max_allocated_memory, torch.cuda.max_memory_allocated())
max_reserved_memory = max(max_reserved_memory, torch.cuda.max_memory_reserved())
epoch_end_time = time.time()
total_time += epoch_end_time - epoch_start_time
avg_epoch_time = total_time
avg_power = np.mean(power_readings)
max_power = np.max(power_readings)
return avg_epoch_time, avg_power, max_power, max_allocated_memory, max_reserved_memory, loss.item()
def benchmark_inference(model, dataloader, num_warmup_batches=10, gpu_idx=0):
model.eval()
device = next(model.parameters()).device
torch.cuda.reset_peak_memory_stats()
power_readings = []
max_allocated_memory = 0
max_reserved_memory = 0
with Progress(
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeRemainingColumn(),
TimeElapsedColumn(),
) as progress:
warmup_task = progress.add_task("[yellow]Warmup", total=num_warmup_batches)
with torch.no_grad():
for i, batch in enumerate(dataloader):
if i >= num_warmup_batches:
break
input_ids, attention_mask, _ = [t.to(device) for t in batch]
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
_ = model(input_ids, attention_mask=attention_mask)
progress.update(warmup_task, advance=1)
inference_task = progress.add_task("[cyan]Inference", total=len(dataloader))
total_time = 0
with torch.no_grad():
run_start_time = time.time()
for i, batch in enumerate(dataloader):
input_ids, attention_mask, _ = [t.to(device) for t in batch]
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
_ = model(input_ids, attention_mask=attention_mask)
progress.update(inference_task, advance=1)
if i % 10 == 0:
power_readings.append(get_gpu_power(gpu_idx))
max_allocated_memory = max(max_allocated_memory, torch.cuda.max_memory_allocated())
max_reserved_memory = max(max_reserved_memory, torch.cuda.max_memory_reserved())
run_end_time = time.time()
total_time += run_end_time - run_start_time
avg_run_time = total_time
avg_power = np.mean(power_readings)
max_power = np.max(power_readings)
return avg_run_time, avg_power, max_power, max_allocated_memory, max_reserved_memory
def create_dummy_data(num_samples, seq_length, vocab_size, model_type):
input_ids = torch.randint(0, vocab_size, (num_samples, seq_length))
attention_mask = torch.ones((num_samples, seq_length))
if model_type == ModelType.mlm:
labels = torch.randint(0, vocab_size, (num_samples, seq_length))
mask = torch.rand(num_samples, seq_length) < 0.7
labels[mask] = -100
elif model_type == ModelType.seqcls:
labels = torch.randint(0, 5, (num_samples, 1))
else:
raise ValueError(f"Invalid model type: {model_type}")
return TensorDataset(input_ids, attention_mask, labels)
def tile_list_to_length(lst, length):
if len(lst) == 1:
return lst * length
return lst
# fmt: off
@app.command()
def main(
ctx: typer.Context, # Typer Context to grab config for --verbose and passing to WandB
hidden_sizes: Annotated[List[int], Option(help="List of hidden sizes", show_default=False)],
num_hidden_layers: Annotated[List[int], Option(help="List of number of hidden layers", show_default=False)],
intermediate_sizes: Annotated[List[int], Option(help="List of intermediate sizes", show_default=False)],
parallel_attn: Annotated[List[bool], Option(is_flag=False, help="List of parallel attention flags", show_default=False)],
sliding_window: Annotated[List[int], Option(help="Sliding window size. -1 to disable.")] = [-1],
global_attn_every_n_layers: Annotated[List[int], Option(help="Use global attention every `n` layers and sliding window for the rest. -1 to disable.")] = [-1],
normalization: Annotated[List[str], Option(help="Normalization type: layernorm or triton_layernorm")] = ["layernorm"],
compile_model: Annotated[List[bool], Option(help="Compile model")] = [True],
masked_prediction: Annotated[List[bool], Option(help="Only pass the masked tokens through the final MLM layers")] = [True],
model_type: Annotated[List[ModelType], Option(help="Model type: MLM or Multiple Choice")] = [ModelType.mlm],
vocab_size: Annotated[List[int], Option(help="Vocabulary size")] = [32768],
num_samples: Annotated[int, Option(help="Number of samples")] = 1000,
seq_length: Annotated[int, Option(help="Sequence length")] = 512,
batch_size: Annotated[Optional[int], Option(help="Batch size (if not provided, will be set based on model size)")] = None,
output_file: Annotated[str, Option(help="Output file name for results")] = "benchmark_results.md",
sleep_time: Annotated[int, Option(help="Time to sleep between each model run")] = 25,
print_model: Annotated[bool, Option(help="Print model")] = False,
num_workers: Annotated[int, Option(help="Number of workers")] = 8,
skip_inference: Annotated[bool, Option(help="Skip inference")] = False,
gpu_idx: Annotated[int, Option(help="GPU index for power measurements")] = 0,
config: Annotated[
Optional[Path],
Option(
callback=conf_callback,
is_eager=True,
help="Relative path to YAML config file for setting options. Passing CLI options will supersede config options.",
case_sensitive=False,
),
] = None,
):
# fmt: on
pynvml.nvmlInit()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Determine the maximum length of the lists
max_length = max(
len(hidden_sizes),
len(num_hidden_layers),
len(intermediate_sizes),
len(parallel_attn),
len(vocab_size),
len(model_type),
len(sliding_window),
len(global_attn_every_n_layers),
len(normalization),
len(compile_model),
len(masked_prediction),
)
# Tile lists to match the maximum length
hidden_sizes = tile_list_to_length(hidden_sizes, max_length)
num_hidden_layers = tile_list_to_length(num_hidden_layers, max_length)
intermediate_sizes = tile_list_to_length(intermediate_sizes, max_length)
parallel_attn = tile_list_to_length(parallel_attn, max_length)
vocab_size = tile_list_to_length(vocab_size, max_length)
model_type = tile_list_to_length(model_type, max_length)
sliding_window = tile_list_to_length(sliding_window, max_length)
global_attn_every_n_layers = tile_list_to_length(global_attn_every_n_layers, max_length)
normalization = tile_list_to_length(normalization, max_length)
compile_model = tile_list_to_length(compile_model, max_length)
masked_prediction = tile_list_to_length(masked_prediction, max_length)
# Create configs from the input lists
configs = [
{
"hidden_size": hs,
"num_hidden_layers": nhl,
"intermediate_size": ims,
"parallel_attn": pa,
"vocab_size": vs,
"model_type": mt,
"sliding_window": sw,
"global_attn_every_n_layers": swel,
"normalization": norm,
"compile_model": cm,
"masked_prediction": mp,
}
for hs, nhl, ims, pa, vs, mt, sw, swel, norm, cm, mp in zip(
hidden_sizes,
num_hidden_layers,
intermediate_sizes,
parallel_attn,
vocab_size,
model_type,
sliding_window,
global_attn_every_n_layers,
normalization,
compile_model,
masked_prediction,
)
]
results = []
for i, config_params in enumerate(configs):
if batch_size is None:
if config_params["hidden_size"] >= 1793:
config_batch_size = 8
elif config_params["hidden_size"] >= 1024:
config_batch_size = 8
else:
config_batch_size = 32
else:
config_batch_size = batch_size
# Create dummy dataset and dataloader
torch.manual_seed(42)
dataset = create_dummy_data(num_samples, seq_length, vocab_size[i], model_type[i])
dataloader = DataLoader(
dataset, batch_size=config_batch_size, shuffle=True, drop_last=True, num_workers=num_workers
)
print(f"\nBenchmarking model with config: {config_params}")
model = get_model(**config_params).to(device)
if print_model:
print(model)
num_params = model.get_number_parameters()
print("Training benchmark:")
train_run_time, avg_train_power, max_train_power, max_train_allocated_memory, max_train_reserved_memory, loss = (
benchmark_training(model, dataloader, num_warmup_batches=25, gpu_idx=gpu_idx)
)
model = None
torch.cuda.empty_cache()
if not skip_inference:
# allow gpu to cool off
time.sleep(sleep_time)
model = get_model(**config_params).to(device)
print("\nInference benchmark:")
infer_run_time, avg_infer_power, max_infer_power, max_infer_allocated_memory, max_infer_reserved_memory = (
benchmark_inference(model, dataloader, num_warmup_batches=50, gpu_idx=gpu_idx)
)
# Calculate tokens per second
tokens_per_sample = seq_length
train_tokens_per_second = (num_samples * tokens_per_sample) / train_run_time
infer_tokens_per_second = (num_samples * tokens_per_sample) / infer_run_time if not skip_inference else 0
# Calculate tokens per second per million parameters
train_tokens_per_second_per_million_params = train_tokens_per_second / (num_params / 1e6)
infer_tokens_per_second_per_million_params = infer_tokens_per_second / (num_params / 1e6) if not skip_inference else 0
# Store results
if skip_inference:
results.append(
{
"Final Loss": f"{loss:.4f}",
"Num Params (M)": f"{num_params / 1e6:.2f}",
"Vocab Size": int(config_params["vocab_size"]),
"Hidden Size": int(config_params["hidden_size"]),
"Num Layers": int(config_params["num_hidden_layers"]),
"Interm Size": int(config_params["intermediate_size"]),
"Parallel Attn": config_params["parallel_attn"],
"Normalization": config_params["normalization"],
"Compile Model": config_params["compile_model"],
"Masked Prediction": config_params["masked_prediction"],
"Train Time (s)": f"{train_run_time:.2f}",
"Train Tok/s": f"{train_tokens_per_second:.2f}",
"Avg Train W": f"{avg_train_power:.2f}",
"Max Train W": f"{max_train_power:.2f}",
"Max Train GiB": f"{max_train_reserved_memory / (1024**3):.2f}",
"Train Tok/s/M Params": f"{train_tokens_per_second_per_million_params:.2f}",
}
)
else:
results.append(
{
"Final Loss": f"{loss:.4f}",
"Num Params (M)": f"{num_params / 1e6:.2f}",
"Vocab Size": int(config_params["vocab_size"]),
"Hidden Size": int(config_params["hidden_size"]),
"Num Layers": int(config_params["num_hidden_layers"]),
"Interm Size": int(config_params["intermediate_size"]),
"Parallel Attn": config_params["parallel_attn"],
"Normalization": config_params["normalization"],
"Compile Model": config_params["compile_model"],
"Masked Prediction": config_params["masked_prediction"],
"Train Time (s)": f"{train_run_time:.2f}",
"Infer Time (s)": f"{infer_run_time:.2f}" ,
"Train Tok/s": f"{train_tokens_per_second:.2f}",
"Infer Tok/s": f"{infer_tokens_per_second:.2f}",
"Avg Train W": f"{avg_train_power:.2f}",
"Max Train W": f"{max_train_power:.2f}",
"Avg Infer W": f"{avg_infer_power:.2f}",
"Max Infer W": f"{max_infer_power:.2f}",
"Max Train GiB": f"{max_train_reserved_memory / (1024**3):.2f}",
"Max Infer GiB": f"{max_infer_reserved_memory / (1024**3):.2f}",
"Train Tok/s/M Params": f"{train_tokens_per_second_per_million_params:.2f}",
"Infer Tok/s/M Params": f"{infer_tokens_per_second_per_million_params:.2f}",
}
)
# Print individual results (optional, you can remove this if you only want the table)
print("\nResults:")
for key, value in results[-1].items():
print(f"{key}: {value:.4f}" if isinstance(value, float) else f"{key}: {value}")
model = None
dataset = None
dataloader = None
torch.cuda.empty_cache()
# allow gpu to cool off
if i < len(configs) - 1:
time.sleep(sleep_time)
# Create and print results table using pandas
print("\nResults Table:")
df = pd.DataFrame(results)
pd.set_option("display.max_columns", None)
pd.set_option("display.width", None)
pd.set_option("display.precision", 4)
print(df.to_string(index=False))
# Save results as markdown table
markdown_table = df.to_markdown(index=False, floatfmt=".4f")
with open(output_file, "w") as f:
f.write("# Benchmark Results\n\n")
f.write(markdown_table)
print(f"\nResults saved as '{output_file}'")
pynvml.nvmlShutdown()
if __name__ == "__main__":
app()