forked from facebookincubator/AITemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_pt.py
100 lines (91 loc) · 3.12 KB
/
benchmark_pt.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
# 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 os
import click
import torch
from aitemplate.testing.benchmark_pt import benchmark_torch_function
from timm.models.vision_transformer import VisionTransformer
from torch import nn
def create_vit(model_name):
if model_name == "vit_base_patch16_224":
img_size = 224
embed_dim = 768
class_token = False
global_pool = "avg"
depth = 12
patch_size = 16
num_heads = 12
elif model_name == "vit_large_patch16_384":
img_size = 384
embed_dim = 1024
class_token = False
global_pool = "avg"
depth = 24
patch_size = 16
num_heads = 16
else:
raise NotImplementedError
model = (
VisionTransformer(
img_size=img_size,
act_layer=nn.GELU,
norm_layer=nn.LayerNorm,
class_token=class_token,
global_pool=global_pool,
depth=depth,
patch_size=patch_size,
num_heads=num_heads,
embed_dim=embed_dim,
)
.cuda()
.half()
)
return model
def benchmark(model_name, batch_size, img_size):
if model_name == "vit_base_patch16_224":
img_size = 224
elif model_name == "vit_large_patch16_384":
img_size = 384
model = create_vit(model_name)
with torch.inference_mode():
input_shape = (batch_size, 3, img_size, img_size)
input_data = torch.randn(input_shape).cuda().half()
# warm up
benchmark_torch_function(100, model, input_data)
# benchmark
t = benchmark_torch_function(100, model, input_data)
print("batch_size: {}, time: {}".format(batch_size, t))
dev_flag = os.environ.get("HIP_VISIBLE_DEVICES", "-1")
dev_flag = dev_flag.replace(",", "_")
with open(f"{model_name}_pt_benchmark_dev_{dev_flag}.txt", "a") as f:
f.write("batch_size: {}, latency: {}\n".format(batch_size, t))
@click.command()
@click.option("--model-name", type=str, default="vit_base_patch16_224")
@click.option("--batch-size", default=0, type=int)
def main(model_name, batch_size):
img_size = 224
if model_name == "vit_base_patch16_224":
img_size = 224
elif model_name == "vit_large_patch16_384":
img_size = 384
else:
raise NotImplementedError
if batch_size == 0:
for batch_size in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
benchmark(model_name, batch_size, img_size)
else:
benchmark(model_name, batch_size, img_size)
if __name__ == "__main__":
main()