-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathbenchmark.py
120 lines (94 loc) · 3.83 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
# Copyright (c) 2025 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import time
import torch
import torch.distributed as dist
from torch import nn
TIME_SCALES = {"ms": 1000}
def warmup(layer, input_, runs, forward_only=False):
for _ in range(runs):
new_i = layer(input_)
if not forward_only:
new_i[0].sum().backward()
def run_wall(layer, input_size_, device, runs, is_print=True, dtype=torch.float) -> dict[str, float]:
input_ = torch.randn(input_size_, device=torch.device(device), dtype=dtype)
# Force CUDA initialization & warm up
warmup(layer, input_, 100)
torch.cuda.synchronize()
start = time.time()
for _ in range(runs):
layer.zero_grad()
new_i = layer(input_)
new_i[0].sum().backward()
torch.cuda.synchronize()
elapsed = time.time() - start
ctime, scale = list(TIME_SCALES.items())[0]
fbtime = elapsed / runs * scale
if is_print:
print(f"Forward&Backward: {fbtime:.3f} {ctime}")
return {"forward + backward": fbtime}
def run_profile(layer, input_size_, device, runs, forward_only=False, dtype=torch.float) -> dict[str, float]:
input_ = torch.randn(input_size_, device=torch.device(device), dtype=dtype)
# Force CUDA initialization & warm up
warmup(layer, input_, 100, forward_only)
forward_min = math.inf
forward_time = 0
backward_min = math.inf
backward_time = 0
for _ in range(runs):
layer.zero_grad()
torch.cuda.synchronize()
start = time.time()
new_i = layer(input_)
torch.cuda.synchronize()
elapsed = time.time() - start
forward_min = min(forward_min, elapsed)
forward_time += elapsed
if not forward_only:
torch.cuda.synchronize()
start = time.time()
new_i[0].sum().backward()
torch.cuda.synchronize()
elapsed = time.time() - start
backward_min = min(backward_min, elapsed)
backward_time += elapsed
ctime, scale = list(TIME_SCALES.items())[0]
forward_min *= scale
backward_min *= scale
forward_average = forward_time / runs * scale
backward_average = backward_time / runs * scale
print(
f"Forward: min {forward_min:.3f}{ctime} / avg {forward_average:.3f}{ctime} |"
f" Backward: min {backward_min:.3f}{ctime} / avg {backward_average:.3f}{ctime}"
)
return {
"forward_min": forward_min,
"forward_avg": forward_average,
"backward_min": backward_min,
"backward_avg": backward_average,
}
def run_worker(gpu, world_size, layer, input_size_, runs, dtype=torch.float, output: list[dict[str, int]] = None):
dist.init_process_group(backend="nccl", init_method="tcp://127.0.0.1:8899", world_size=world_size, rank=gpu)
device = torch.device(f"cuda:{gpu}")
torch.cuda.set_device(gpu)
batch = (int)(input_size_[0] / world_size)
if gpu == 0:
run_size = input_size_.copy()
run_size[0] = input_size_[0] - batch * (world_size - 1)
else:
run_size = input_size_.copy()
run_size[0] = batch
run_model = layer.to(device)
run_model = nn.parallel.DistributedDataParallel(run_model, device_ids=[gpu])
retval = run_wall(run_model, run_size, device, runs, (gpu == 0), dtype)
if output is not None:
output.append(retval)