benchmark.cpp 4.48 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// 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.
//*****************************************************************************
16

Ashok Emani's avatar
Ashok Emani committed
17
#include "benchmark.hpp"
Robert Kimball's avatar
Robert Kimball committed
18
#include "benchmark_utils.hpp"
19
#include "ngraph/file_util.hpp"
Ashok Emani's avatar
Ashok Emani committed
20
#include "ngraph/runtime/backend.hpp"
21 22
#include "ngraph/runtime/host_tensor.hpp"
#include "ngraph/runtime/tensor.hpp"
Ashok Emani's avatar
Ashok Emani committed
23
#include "ngraph/serializer.hpp"
24
#include "ngraph/util.hpp"
Ashok Emani's avatar
Ashok Emani committed
25

26 27 28
using namespace std;
using namespace ngraph;

29 30 31 32
vector<runtime::PerformanceCounter> run_benchmark(shared_ptr<Function> f,
                                                  const string& backend_name,
                                                  size_t iterations,
                                                  bool timing_detail,
33 34
                                                  int warmup_iterations,
                                                  bool copy_data)
35 36 37
{
    stopwatch timer;
    timer.start();
38
    auto backend = runtime::Backend::create(backend_name);
39
    auto exec = backend->compile(f, timing_detail);
40 41 42
    timer.stop();
    cout.imbue(locale(""));
    cout << "compile time: " << timer.get_milliseconds() << "ms" << endl;
Ashok Emani's avatar
Ashok Emani committed
43

44 45
    vector<shared_ptr<runtime::HostTensor>> arg_data;
    vector<shared_ptr<runtime::Tensor>> args;
46
    vector<bool> args_cacheable;
Ashok Emani's avatar
Ashok Emani committed
47 48
    for (shared_ptr<op::Parameter> param : f->get_parameters())
    {
49
        auto tensor = backend->create_tensor(param->get_element_type(), param->get_shape());
50
        auto tensor_data =
51
            make_shared<runtime::HostTensor>(param->get_element_type(), param->get_shape());
52
        random_init(tensor_data);
53 54
        tensor->write(tensor_data->get_data_ptr(),
                      tensor_data->get_element_count() * tensor_data->get_element_type().size());
Ashok Emani's avatar
Ashok Emani committed
55
        args.push_back(tensor);
56
        arg_data.push_back(tensor_data);
57
        args_cacheable.push_back(param->get_cacheable());
Ashok Emani's avatar
Ashok Emani committed
58
    }
59 60
    set_denormals_flush_to_zero();

61 62
    vector<shared_ptr<runtime::HostTensor>> result_data;
    vector<shared_ptr<runtime::Tensor>> results;
Ashok Emani's avatar
Ashok Emani committed
63 64
    for (shared_ptr<Node> out : f->get_results())
    {
65
        auto result = backend->create_tensor(out->get_element_type(), out->get_shape());
66
        auto tensor_data =
67
            make_shared<runtime::HostTensor>(out->get_element_type(), out->get_shape());
Ashok Emani's avatar
Ashok Emani committed
68
        results.push_back(result);
69
        result_data.push_back(tensor_data);
Ashok Emani's avatar
Ashok Emani committed
70 71
    }

72 73 74 75 76 77 78
    for (size_t i = 0; i < args.size(); i++)
    {
        if (args_cacheable[i])
        {
            args[i]->set_stale(false);
        }
    }
79

80 81
    stopwatch t1;
    for (size_t i = 0; i < iterations + warmup_iterations; i++)
82
    {
83
        if (i == warmup_iterations)
84
        {
85
            t1.start();
86
        }
87 88 89 90
        if (copy_data)
        {
            for (size_t arg_index = 0; arg_index < args.size(); arg_index++)
            {
91
                const shared_ptr<runtime::Tensor>& arg = args[arg_index];
92 93
                if (arg->get_stale())
                {
94
                    const shared_ptr<runtime::HostTensor>& data = arg_data[arg_index];
95
                    arg->write(data->get_data_ptr(),
96
                               data->get_element_count() * data->get_element_type().size());
97 98 99
                }
            }
        }
100
        exec->call(results, args);
101 102 103 104
        if (copy_data)
        {
            for (size_t result_index = 0; result_index < results.size(); result_index++)
            {
105 106
                const shared_ptr<runtime::HostTensor>& data = result_data[result_index];
                const shared_ptr<runtime::Tensor>& result = results[result_index];
107 108
                result->read(data->get_data_ptr(),
                             data->get_element_count() * data->get_element_type().size());
109 110
            }
        }
Ashok Emani's avatar
Ashok Emani committed
111 112 113 114 115
    }
    t1.stop();
    float time = t1.get_milliseconds();
    cout << time / iterations << "ms per iteration" << endl;

116
    vector<runtime::PerformanceCounter> perf_data = exec->get_performance_data();
117 118
    return perf_data;
}