benchmark_vm.py 4.73 KB
Newer Older
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
# 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.
"""Benchmarking Relay VM using models from MXNet."""
import numpy as np

import tvm
from tvm.contrib import graph_runtime
from tvm import relay
from tvm.relay import testing


26
def benchmark_execution(mod,
27 28 29 30 31
                        params,
                        measure=False,
                        data_shape=(1, 3, 224, 224),
                        out_shape=(1, 1000),
                        dtype='float32'):
32
    def get_tvm_output(mod, data, params, target, ctx, dtype='float32'):
33
        with relay.build_config(opt_level=1):
34
            graph, lib, params = relay.build(mod, target, params=params)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

        m = graph_runtime.create(graph, lib, ctx)
        # set inputs
        m.set_input("data", data)
        m.set_input(**params)
        m.run()
        out = m.get_output(0, tvm.nd.empty(out_shape, dtype))

        if measure:
            print("Evaluate graph runtime inference time cost...")
            ftimer = m.module.time_evaluator("run", ctx, number=1, repeat=20)
            # Measure in millisecond.
            prof_res = np.array(ftimer().results) * 1000
            print("Mean inference time (std dev): %.2f ms (%.2f ms)" %
                  (np.mean(prof_res), np.std(prof_res)))

        return out.asnumpy()

53 54 55
    def get_tvm_vm_output(mod, data, params, target, ctx, dtype='float32'):
        ex = relay.create_executor('vm', mod=mod, ctx=ctx)
        result = ex.evaluate()(data, **params)
56 57 58 59 60 61 62
        return result.asnumpy().astype(dtype)

    # random input
    data = np.random.uniform(size=data_shape).astype(dtype)
    target = "llvm"
    ctx = tvm.cpu(0)

63
    tvm_out = get_tvm_output(mod, tvm.nd.array(data.astype(dtype)), params,
64
                             target, ctx, dtype)
65
    vm_out = get_tvm_vm_output(mod, tvm.nd.array(data.astype(dtype)), params,
66 67 68 69 70
                               target, ctx, dtype)
    tvm.testing.assert_allclose(vm_out, tvm_out, rtol=1e-5, atol=1e-5)


def test_mlp():
71 72 73
    image_shape = (1, 1, 28, 28)
    mod, params = testing.mlp.get_workload(1)
    benchmark_execution(mod, params, data_shape=image_shape, out_shape=(1, 10))
74 75 76 77


def test_vgg():
    for n in [11, 16]:
78 79
        mod, params = testing.vgg.get_workload(1, num_layers=n)
        benchmark_execution(mod, params)
80 81 82 83


def test_resnet():
    for n in [18, 50]:
84 85
        mod, params = testing.resnet.get_workload(batch_size=1, num_layers=n)
        benchmark_execution(mod, params, True)
86 87 88 89


def test_squeezenet():
    for version in ['1.0', '1.1']:
90 91
        mod, params = testing.squeezenet.get_workload(version=version)
        benchmark_execution(mod, params)
92 93 94


def test_inception_v3():
95
    image_shape = (3, 299, 299)
96
    mod, params = testing.inception_v3.get_workload(image_shape=image_shape)
97
    benchmark_execution(mod, params, data_shape=(1, 3, 299, 299))
98 99 100


def test_dqn():
101 102
    image_shape = (1, 4, 84, 84)
    mod, params = testing.dqn.get_workload(
103
        batch_size=1, image_shape=image_shape)
104
    benchmark_execution(mod, params, data_shape=image_shape, out_shape=(1, 18))
105 106 107 108


def test_dcgan():
    image_shape = (1, 100)
109
    mod, params = testing.dcgan.get_workload(batch_size=1)
110
    benchmark_execution(mod, params, data_shape=image_shape, out_shape=(1, 3, 64, 64))
111 112 113


def test_mobilenet():
114 115
    mod, params = testing.mobilenet.get_workload(batch_size=1)
    benchmark_execution(mod, params)
116

117 118 119 120 121 122 123
# TODO: enable when the low building performance (several minutes) fixed.
def test_mobilenet_nhwc():
    image_shape = (1, 224, 224, 3)
    mod, params = testing.mobilenet.get_workload(batch_size=1,
                                                 image_shape=image_shape[1:],
                                                 layout='NHWC')
    benchmark_execution(mod, params, measure=False, data_shape=image_shape)
124 125

def test_densenet():
126 127
    mod, params = testing.densenet.get_workload(batch_size=1)
    benchmark_execution(mod, params)
128 129 130 131 132 133 134 135


if __name__ == '__main__':
    test_resnet()
    test_vgg()
    test_squeezenet()
    test_mobilenet()
    test_densenet()
136 137 138 139
    test_inception_v3()
    test_mlp()
    test_dqn()
    test_dcgan()