gpu_imagenet_bench.py 3.91 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 27
from tvm.contrib.util import tempdir
import tvm.contrib.graph_runtime as runtime
28 29 30
import nnvm.compiler
import nnvm.testing

31 32
from util import get_network

33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
def benchmark(network, target):
    net, params, input_shape, output_shape = get_network(network, batch_size=1)

    with nnvm.compiler.build_config(opt_level=3):
        graph, lib, params = nnvm.compiler.build(
            net, target=target, shape={'data': input_shape}, params=params, dtype=dtype)

    # 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)))


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

72
    dtype = 'float32'
73

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

79
    target = tvm.target.create('%s -model=%s' % (args.target, args.model))
80

81 82 83 84
    print("--------------------------------------------------")
    print("%-20s %-20s" % ("Network Name", "Mean Inference Time (std dev)"))
    print("--------------------------------------------------")
    for network in networks:
85 86 87 88 89 90 91 92 93 94 95 96 97
        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()