-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathrun_benchmarks.py
199 lines (182 loc) · 6.24 KB
/
run_benchmarks.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# 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 argparse
import itertools
import json
import logging
from os.path import exists
from typing import Any, Dict
from benchmarks.benchmark_layer import run_layer_benchmark
from benchmarks.layers import LayerType
from benchmarks.utils import get_layer_set, get_path, save_results
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def run_and_save_benchmark(
layer: LayerType,
batch_size: int,
gsm_mode: str,
args,
layer_config: Dict[str, Any],
root: str = "./results/raw/",
suffix: str = "",
) -> None:
"""Runs and saves (if desired) the benchmark for the given layer and batch size.
Args:
layer: the layer to run benchmarks for
batch_size: the batch size to run benchmarks for
args: additional arguments
layer_config: the settings for this layer
root: directory to write results to
suffix: optional string to append to result file name
"""
logger.info(f"Benchmarking {layer} {gsm_mode} layer with batch size {batch_size}.")
results = []
for i in range(args.num_runs):
runtime, memory_stats = run_layer_benchmark(
num_repeats=args.num_repeats,
forward_only=args.forward_only,
layer_name=layer,
batch_size=batch_size,
random_seed=args.random_seed + i if args.random_seed is not None else None,
gsm_mode=gsm_mode,
**layer_config,
)
res = {"runtime": runtime, "memory_stats": memory_stats}
results.append(res)
logger.info(res)
# save the benchmark results if desired
if not args.no_save:
save_results(
layer=layer,
batch_size=batch_size,
num_runs=args.num_runs,
num_repeats=args.num_repeats,
gsm_mode=gsm_mode,
results=results,
config=layer_config,
random_seed=args.random_seed,
forward_only=args.forward_only,
root=root,
suffix=suffix,
)
def main(args) -> None:
if args.verbose:
logger.setLevel(logging.DEBUG)
# get config for each layer
logger.info(f"Using {args.config_file} as config file.")
with open(args.config_file) as config_file:
config = json.load(config_file)
for layer, batch_size, gsm_mode in itertools.product(
args.layers, args.batch_sizes, args.grad_sample_modes
):
# skip benchmark for this layer and batch size if applicable
if args.cont and exists(
get_path(
layer=layer,
batch_size=batch_size,
num_runs=args.num_runs,
num_repeats=args.num_repeats,
random_seed=args.random_seed,
forward_only=args.forward_only,
root=args.root,
suffix=args.suffix,
gsm_mode=gsm_mode,
)
):
logger.info(
f"Skipping {layer} ({gsm_mode}) at {batch_size} - already exists."
)
continue
try:
# run and save (if applicable) the benchmark for this layer and batch size
run_and_save_benchmark(
layer=layer,
batch_size=batch_size,
args=args,
layer_config=config[get_layer_set(layer)],
root=args.root,
suffix=args.suffix,
gsm_mode=gsm_mode,
)
except Exception as e:
logger.info(
f"Skipping {layer} ({gsm_mode}) at {batch_size} - Failed with {e}"
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
layers = [v for k, v in LayerType.__dict__.items() if not k.startswith("__")]
parser.add_argument("--layers", nargs="+", default=layers, type=str, choices=layers)
parser.add_argument(
"--batch_sizes",
nargs="+",
default=[16, 32, 64, 128, 256, 512],
type=int,
)
parser.add_argument(
"--num_runs",
default=100,
type=int,
help="number of benchmarking runs for each layer and batch size",
)
parser.add_argument(
"--num_repeats",
default=20,
type=int,
help="number of forward/backward passes per run",
)
parser.add_argument(
"--forward_only", action="store_true", help="only run forward passes"
)
parser.add_argument(
"--random_seed",
type=int,
help="random seed for the first run for each layer and batch size, "
"subsequent runs increase the random seed by 1",
)
parser.add_argument(
"-c",
"--config_file",
default="config.json",
type=str,
help="path to config file with settings for each layer",
)
parser.add_argument(
"--cont", action="store_true", help="only run missing experiments"
)
parser.add_argument(
"--root",
default="./results/raw/",
type=str,
help="path to directory where benchmark results should be saved",
)
parser.add_argument(
"--suffix",
default="",
type=str,
help="suffix to append to each result file's name",
)
parser.add_argument(
"--grad_sample_modes",
type=str,
nargs="+",
choices=["baseline", "hooks", "ew", "functorch"],
default=["baseline", "hooks"],
help="Mode to compute per sample gradinets: "
"Classic (hooks), Functorch(functorch), "
"ExpandedWeights(ew), Non-private(baseline)",
)
parser.add_argument("--no_save", action="store_true")
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
main(args)