gpu_imagenet_bench.py 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
17 18
"""Benchmark script for ImageNet models on GPU.
see README.md for the usage and results of this script.
19
"""
20
import argparse
21
import threading
22

23
import numpy as np
24

25
import tvm
26
from tvm import te
27
import tvm.contrib.graph_runtime as runtime
28
from tvm import relay
29

30 31
from util import get_network

32

33 34 35
def benchmark(network, target):
    net, params, input_shape, output_shape = get_network(network, batch_size=1)

36 37
    with relay.build_config(opt_level=3):
        graph, lib, params = relay.build(net, target=target, params=params)
38 39 40 41 42 43 44 45 46 47 48 49 50 51

    # create runtime
    ctx = tvm.context(str(target), 0)
    module = runtime.create(graph, lib, ctx)
    data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
    module.set_input('data', data_tvm)
    module.set_input(**params)

    # evaluate
    ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=args.repeat)
    prof_res = np.array(ftimer().results) * 1000  # multiply 1000 for converting to millisecond
    print("%-20s %-19s (%s)" % (network, "%.2f ms" % np.mean(prof_res), "%.2f ms" % np.std(prof_res)))


52
if __name__ == "__main__":
53
    parser = argparse.ArgumentParser()
54
    parser.add_argument("--network", type=str, choices=
55 56
                        ['resnet-18', 'resnet-34', 'resnet-50',
                         'vgg-16', 'vgg-19', 'densenet-121', 'inception_v3',
57
                         'mobilenet', 'squeezenet_v1.0', 'squeezenet_v1.1'],
58
                        help='The name of neural network')
59
    parser.add_argument("--model", type=str,
60
                        choices=['1080ti', 'titanx', 'tx2', 'gfx900'], default='1080ti',
61 62
                        help="The model of the test device. If your device is not listed in "
                             "the choices list, pick the most similar one as argument.")
63
    parser.add_argument("--repeat", type=int, default=600)
64 65 66
    parser.add_argument("--target", type=str,
                        choices=['cuda', 'opencl', 'rocm', 'nvptx', 'metal'], default='cuda',
                        help="The tvm compilation target")
67
    parser.add_argument("--thread", type=int, default=1, help="The number of threads to be run.")
68 69
    args = parser.parse_args()

70
    dtype = 'float32'
71

72 73
    if args.network is None:
        networks = ['resnet-50', 'mobilenet', 'vgg-19', 'inception_v3']
74
    else:
75
        networks = [args.network]
76

77
    target = tvm.target.create('%s -model=%s' % (args.target, args.model))
78

79 80 81 82
    print("--------------------------------------------------")
    print("%-20s %-20s" % ("Network Name", "Mean Inference Time (std dev)"))
    print("--------------------------------------------------")
    for network in networks:
83 84 85 86 87 88 89 90 91 92 93 94 95
        if args.thread == 1:
            benchmark(network, target)
        else:
            threads = list()
            for n in range(args.thread):
                thread = threading.Thread(target=benchmark, args=([network, target]), name="thread%d" % n)
                threads.append(thread)

            for thread in threads:
                thread.start()

            for thread in threads:
                thread.join()