forked from NVIDIA/Megatron-LM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
t5_microbenchmark_wrapper.py
261 lines (251 loc) · 7.09 KB
/
t5_microbenchmark_wrapper.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 sys
import os
import argparse
import subprocess
from pathlib import Path
MASTER_PORT = 8000
MLM_DIR = Path(__file__).parent
VOCAB_FILE = MLM_DIR / "vocabs" / "t5-base-vocab.txt"
DISTRIBUTED_ARGS = "--nproc_per_node {} --nnodes 1 --node_rank 0 --master_addr localhost --master_port {} --use-env"
CMD_TEMPLATE = """
CUDA_VISIBLE_DEVICES={} python3 -m torch.distributed.launch {} \
microbenchmark_t5.py \
--tensor-model-parallel-size {} \
--pipeline-model-parallel-size 1 \
--encoder-num-layers {} \
--decoder-num-layers {} \
--hidden-size {} \
--num-attention-heads {} \
--kv-channels {} \
--ffn-hidden-size {} \
--encoder-seq-length {} \
--decoder-seq-length {} \
--micro-batch-size {} \
--global-batch-size 4096 \
--max-position-embeddings {} \
--no-async-tensor-model-parallel-allreduce \
--train-iters {} \
--train-epochs 1 \
--lr-decay-iters 100 \
""" \
+ """ --vocab-file {} \
""".format(VOCAB_FILE) \
+ """ --data-impl mmap \
--split 949,50,1 \
--lr 0.0001 \
--min-lr 0.00001 \
--lr-decay-style linear \
--lr-warmup-fraction .01 \
--weight-decay 1e-2 \
--clip-grad 1.0 \
--log-interval 50 \
--save-interval 10000 \
--eval-interval 1000 \
--eval-iters 5 \
--fp16 \
--vocab-extra-ids 100 \
--num-workers 2 \
--dataloader-type ordered \
--microbenchmark-save-dir {} \
--tokens-per-global-batch 16384"""
def parse_args():
parser = argparse.ArgumentParser("Run single GPU benchmark for T5.")
parser.add_argument(
"-tp",
"--tensor-model-parallel-size",
type=int,
required=True,
help="Tensor model parallel size",
)
parser.add_argument(
"-e",
"--encoder-seq-length",
type=int,
required=True,
help="Encoder sequence length",
)
parser.add_argument(
"-d",
"--decoder-seq-length",
type=int,
required=True,
help="Decoder sequence length",
)
parser.add_argument(
"-b",
"--micro-batch-size",
type=int,
required=True,
help="Micro batch size",
)
parser.add_argument(
"-el",
"--encoder-num-layers",
type=int,
required=True,
help="Number of encoder layers",
)
parser.add_argument(
"-dl",
"--decoder-num-layers",
type=int,
required=True,
help="Number of decoder layers",
)
parser.add_argument(
"-rc",
"--recompute-type",
choices=["None", "Selective", "Full"],
default="None",
help="Enable recomputation",
)
parser.add_argument(
"-i",
"--benchmark-iters",
type=int,
default=50,
help="Number of iterations to benchmark",
)
parser.add_argument(
"-o",
"--output-dir",
type=str,
required=True,
help="Output directory for benchmark results",
)
parser.add_argument(
"--devices",
type=str,
default="0",
help="GPU to run benchmark on.",
)
# default model configuration corresponds to T5-11B
parser.add_argument(
"--hidden-size", type=int, default=1024, help="Model hidden size"
)
parser.add_argument(
"--num-attention-heads",
type=int,
default=128,
help="Number of attention heads",
)
parser.add_argument(
"--kv-channels", type=int, default=128, help="Number of KV Channels"
)
parser.add_argument(
"--ffn-hidden-size", type=int, default=65536, help="FFN hidden size"
)
parser.add_argument(
"--use-flash-attn",
action="store_true",
help="Use flash attention.",
)
args = parser.parse_args()
args.devices = [int(d) for d in args.devices.split(",")]
return args
def get_microbenchmark_name(tp_size, hidden_size, num_attention_heads,
kv_channels, ffn_hidden_size, encoder_seq_length,
decoder_seq_length, micro_batch_size, recompute_type):
name = "tp{}_hs{}_ah{}_kv{}_ffhs{}_encsl{}_decsl{}_mbs{}".format(
tp_size,
hidden_size,
num_attention_heads,
kv_channels,
ffn_hidden_size,
encoder_seq_length,
decoder_seq_length,
micro_batch_size,
)
# add recomputation settings if exist
if recompute_type != "None":
name += "_rc_{}".format(recompute_type.lower())
if recompute_type == "Full":
name += "_{}".format("uniform")
return name
def run_benchmark(
tp_size,
enc_seqlen,
dec_seqlen,
microbatch_size,
encoder_num_layers,
decoder_num_layers,
output_dir,
devices,
benchmark_iters=50,
hidden_size=1024,
n_attn_heads=128,
kv_channels=128,
ffn_hidden_size=65536,
recompute_type="None",
use_flash_attn=False,
log_file=None,
):
assert len(devices) >= 1, "Must have at least one device"
output_fn = "microbench_" + get_microbenchmark_name(
tp_size,
hidden_size,
n_attn_heads,
kv_channels,
ffn_hidden_size,
enc_seqlen,
dec_seqlen,
microbatch_size,
recompute_type,
) + ".txt"
output_path = os.path.join(output_dir, output_fn)
if os.path.exists(output_path):
# skip if already exists
return 0
distributed_args = DISTRIBUTED_ARGS.format(tp_size, MASTER_PORT + devices[0])
cmd = CMD_TEMPLATE.format(
",".join([str(d) for d in devices]),
distributed_args,
tp_size,
encoder_num_layers,
decoder_num_layers,
hidden_size,
n_attn_heads,
kv_channels,
ffn_hidden_size,
enc_seqlen,
dec_seqlen,
microbatch_size,
max(enc_seqlen, dec_seqlen),
benchmark_iters,
output_dir,
)
if recompute_type != "None":
if recompute_type == "Selective":
cmd += " --recompute-activations"
elif recompute_type == "Full":
cmd += " --recompute-granularity full --recompute-method uniform"
else:
raise ValueError(f"Unknown recompute type {recompute_type}")
if use_flash_attn:
cmd += " --use-flash-attn"
if log_file:
with open(log_file, "a") as f:
p = subprocess.run(cmd, shell=True, stderr=f, stdout=f)
else:
p = subprocess.run(cmd, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
return p.returncode
if __name__ == "__main__":
args = parse_args()
retval = run_benchmark(
args.tensor_model_parallel_size,
args.encoder_seq_length,
args.decoder_seq_length,
args.micro_batch_size,
args.encoder_num_layers,
args.decoder_num_layers,
args.output_dir,
args.devices,
args.benchmark_iters,
args.hidden_size,
args.num_attention_heads,
args.kv_channels,
args.ffn_hidden_size,
args.recompute_type,
args.use_flash_attn,
)
sys.exit(retval)