forked from facebookincubator/AITemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_ait.py
132 lines (112 loc) · 4.12 KB
/
benchmark_ait.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
# 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.
#
"""benchmark for resnet50"""
import os
import click
import torch
from aitemplate.compiler import compile_model, Model
from aitemplate.frontend import Tensor
from aitemplate.testing import detect_target
from modeling.resnet import build_resnet_backbone
from weight_utils import export_to_torch_tensor
def mark_output(y):
"""Different to PyTorch, we need to explicit mark output tensor for optimization,
Parameters
----------
y : List[Tensor]
List of output tensors
"""
if type(y) is not tuple:
y = (y,)
for i in range(len(y)):
y[i]._attrs["is_output"] = True
y[i]._attrs["name"] = "output_%d" % (i)
y_shape = [d._attrs["values"][0] for d in y[i]._attrs["shape"]]
print("output_{} shape: {}".format(i, y_shape))
def compile_module(model_name, batch_size, **kwargs):
if model_name != "resnet50":
raise NotImplementedError
model_name = f"{model_name}_{batch_size}"
target = detect_target(**kwargs)
# Create input tensor, need to specify the shape, dtype and is_input flag
x = Tensor(
shape=[batch_size, 224, 224, 3], dtype="float16", name="input0", is_input=True
)
model = build_resnet_backbone(50, activation="ReLU")
# Mark all parameters with name same to PyTorch name convention
model.name_parameter_tensor()
# Forward the input tensor to the model, get output tensor
y = model(x)
# Mark output tensor
mark_output(y)
# Compile the model
module = compile_model(y, target, "./tmp", model_name)
return module
def benchmark(model_name, batch_size, mod=None, graph_mode=True):
# Load params
cuda_params = export_to_torch_tensor(model_name)
# Load compiled model
if mod is None:
model_name = f"{model_name}_{batch_size}"
mod = Model(os.path.join("./tmp", model_name, "test.so"))
# Set params
for k, v in cuda_params.items():
mod.set_constant_with_tensor(k, v)
# prepare input/output tensor
x_input = torch.randn([batch_size, 224, 224, 3]).cuda().half()
x_input = x_input.contiguous()
y_output = torch.zeros([batch_size, 1, 1, 1000]).cuda().half()
y_output = y_output.contiguous()
# warm up
t, _, __ = mod.benchmark_with_tensors(
[x_input],
[y_output],
count=100,
repeat=4,
graph_mode=graph_mode,
)
# benchmark
t, _, __ = mod.benchmark_with_tensors(
[x_input],
[y_output],
count=100,
repeat=4,
graph_mode=graph_mode,
)
print(f"batch_size: {batch_size}, latency: {t}")
dev_flag = os.environ.get("HIP_VISIBLE_DEVICES", "-1")
dev_flag = dev_flag.replace(",", "_")
with open(f"resnet50_ait_benchmark_dev_{dev_flag}.txt", "a") as f:
f.write(f"batch_size: {batch_size}, latency: {t}\n")
@click.command()
@click.option(
"--use-fp16-acc",
type=bool,
default=True,
help="Whether to use FP16 for accumulation (similar to TensorRT)",
)
@click.option("--use-graph", type=bool, default=True, help="Whether to use CUDA graph")
@click.option("--batch-size", type=int, default=0, help="Batch size")
def main(use_fp16_acc=True, use_graph=True, batch_size=0):
if detect_target().name() == "rocm":
use_graph = False
if batch_size < 1:
for bs in (1, 2, 4, 8, 16, 32, 64, 128, 256):
compile_module("resnet50", bs, use_fp16_acc=use_fp16_acc)
benchmark("resnet50", bs, graph_mode=use_graph)
else:
benchmark("resnet50", batch_size, graph_mode=use_graph)
if __name__ == "__main__":
main()