benchmark.cpp 7.43 KB
Newer Older
Ashok Emani's avatar
Ashok Emani committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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 17 18

#include <iomanip>

Ashok Emani's avatar
Ashok Emani committed
19
#include "benchmark.hpp"
20
#include "ngraph/graph_util.hpp"
Ashok Emani's avatar
Ashok Emani committed
21 22 23
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/tensor_view.hpp"
#include "ngraph/serializer.hpp"
24
#include "ngraph/util.hpp"
Ashok Emani's avatar
Ashok Emani committed
25 26
#include "random.hpp"

27 28 29
using namespace std;
using namespace ngraph;

30 31 32
multimap<size_t, string>
    aggregate_timing_details(const vector<runtime::PerformanceCounter>& perf_data,
                             shared_ptr<Function> func)
Ashok Emani's avatar
Ashok Emani committed
33
{
34 35 36 37
    unordered_map<string, shared_ptr<Node>> node_map;
    vector<shared_ptr<Function>> fs;
    traverse_functions(func, [&](shared_ptr<Function> f) { fs.push_back(f); });
    for (shared_ptr<Function> f : fs)
Ashok Emani's avatar
Ashok Emani committed
38
    {
39
        for (shared_ptr<Node> node : f->get_ops())
40
        {
41
            node_map.insert({node->get_name(), node});
42 43 44 45 46 47
        }
    }

    unordered_map<string, size_t> timing;
    for (const runtime::PerformanceCounter& p : perf_data)
    {
48
        shared_ptr<Node> node = node_map.at(p.name());
49 50 51
        string op = p.name().substr(0, p.name().find('_'));
        string shape_name = "{" + join(node->get_outputs()[0].get_shape()) + "}";
        timing[op + shape_name] += p.microseconds();
Ashok Emani's avatar
Ashok Emani committed
52 53
    }

54 55
    multimap<size_t, string> rc;
    for (const pair<string, size_t>& t : timing)
Ashok Emani's avatar
Ashok Emani committed
56 57 58 59 60 61
    {
        rc.insert({t.second, t.first});
    }
    return rc;
}

62
multimap<size_t, string> aggregate_timing(const vector<runtime::PerformanceCounter>& perf_data)
Ashok Emani's avatar
Ashok Emani committed
63
{
64 65 66 67 68 69 70 71 72
    unordered_map<string, size_t> timing;
    for (const runtime::PerformanceCounter& p : perf_data)
    {
        string op = p.name().substr(0, p.name().find('_'));
        timing[op] += p.microseconds();
    }

    multimap<size_t, string> rc;
    for (const pair<string, size_t>& t : timing)
Ashok Emani's avatar
Ashok Emani committed
73
    {
74
        rc.insert({t.second, t.first});
Ashok Emani's avatar
Ashok Emani committed
75
    }
76 77
    return rc;
}
Ashok Emani's avatar
Ashok Emani committed
78

79 80 81 82 83 84 85
void run_benchmark(const string& json_path,
                   const string& backend_name,
                   size_t iterations,
                   bool timing_detail)
{
    stopwatch timer;
    timer.start();
Ashok Emani's avatar
Ashok Emani committed
86 87 88
    const string json_string = file_util::read_file_to_string(json_path);
    stringstream ss(json_string);
    shared_ptr<Function> f = deserialize(ss);
89 90 91 92
    timer.stop();
    cout << "deserialize time: " << timer.get_milliseconds() << "ms" << endl;
    run_benchmark(f, backend_name, iterations, timing_detail);
}
Ashok Emani's avatar
Ashok Emani committed
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
void print_times(const multimap<size_t, string>& timing)
{
    // set the column widths
    int name_width = 0;
    int time_width = 0;
    for (const pair<size_t, string>& p : timing)
    {
        name_width = max(name_width, static_cast<int>(p.second.size()));
        stringstream ss;
        ss.imbue(locale(""));
        ss << p.first;
        time_width = max(time_width, static_cast<int>(ss.str().size()));
    }
    for (auto it = timing.rbegin(); it != timing.rend(); it++)
    {
        cout << setw(name_width + 2) << left << it->second << " " << setw(time_width + 2) << right
             << it->first << "us\n";
    }
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static default_random_engine s_random_engine;

template <typename T>
void init_int_tv(shared_ptr<runtime::TensorView> tv, T min, T max)
{
    uniform_int_distribution<T> dist(min, max);
    std::vector<T> vec = read_vector<T>(tv);
    for (T& element : vec)
    {
        element = dist(s_random_engine);
    }
    write_vector(tv, vec);
}

template <typename T>
void init_real_tv(shared_ptr<runtime::TensorView> tv, T min, T max)
{
    uniform_real_distribution<T> dist(min, max);
    std::vector<T> vec = read_vector<T>(tv);
    for (T& element : vec)
    {
        element = dist(s_random_engine);
    }
    write_vector(tv, vec);
}

static void random_init(shared_ptr<runtime::TensorView> tv)
{
    element::Type et = tv->get_tensor().get_element_type();
    if (et == element::boolean)
    {
        init_int_tv<char>(tv, 0, 1);
    }
    else if (et == element::f32)
    {
        init_real_tv<float>(tv, -1, 1);
    }
    else if (et == element::f64)
    {
        init_real_tv<double>(tv, -1, 1);
    }
    else if (et == element::i8)
    {
        init_int_tv<int8_t>(tv, -1, 1);
    }
    else if (et == element::i16)
    {
        init_int_tv<int16_t>(tv, -1, 1);
    }
    else if (et == element::i32)
    {
        init_int_tv<int32_t>(tv, -1, 1);
    }
    else if (et == element::i64)
    {
        init_int_tv<int64_t>(tv, -1, 1);
    }
    else if (et == element::u8)
    {
        init_int_tv<uint8_t>(tv, 0, 1);
    }
    else if (et == element::u16)
    {
        init_int_tv<uint16_t>(tv, 0, 1);
    }
    else if (et == element::u32)
    {
        init_int_tv<uint32_t>(tv, 0, 1);
    }
    else if (et == element::u64)
    {
        init_int_tv<uint64_t>(tv, 0, 1);
    }
    else
    {
        throw runtime_error("unsupported type");
    }
}

193 194 195 196 197 198 199
void run_benchmark(shared_ptr<Function> f,
                   const string& backend_name,
                   size_t iterations,
                   bool timing_detail)
{
    stopwatch timer;
    timer.start();
200 201 202
    auto backend = runtime::Backend::create(backend_name);
    backend->enable_performance_data(f, timing_detail);
    backend->compile(f);
203 204 205
    timer.stop();
    cout.imbue(locale(""));
    cout << "compile time: " << timer.get_milliseconds() << "ms" << endl;
Ashok Emani's avatar
Ashok Emani committed
206 207

    vector<shared_ptr<runtime::TensorView>> args;
208
    vector<bool> args_cacheable;
Ashok Emani's avatar
Ashok Emani committed
209 210
    for (shared_ptr<op::Parameter> param : f->get_parameters())
    {
211
        auto tensor = backend->create_tensor(param->get_element_type(), param->get_shape());
212
        random_init(tensor);
Ashok Emani's avatar
Ashok Emani committed
213
        args.push_back(tensor);
214
        args_cacheable.push_back(param->get_cacheable());
Ashok Emani's avatar
Ashok Emani committed
215 216 217 218
    }
    vector<shared_ptr<runtime::TensorView>> results;
    for (shared_ptr<Node> out : f->get_results())
    {
219
        auto result = backend->create_tensor(out->get_element_type(), out->get_shape());
Ashok Emani's avatar
Ashok Emani committed
220 221 222
        results.push_back(result);
    }

223 224 225 226 227 228 229
    for (size_t i = 0; i < args.size(); i++)
    {
        if (args_cacheable[i])
        {
            args[i]->set_stale(false);
        }
    }
Ashok Emani's avatar
Ashok Emani committed
230 231 232 233
    stopwatch t1;
    t1.start();
    for (size_t i = 0; i < static_cast<size_t>(iterations); i++)
    {
234
        backend->call(f, results, args);
Ashok Emani's avatar
Ashok Emani committed
235 236 237 238 239
    }
    t1.stop();
    float time = t1.get_milliseconds();
    cout << time / iterations << "ms per iteration" << endl;

240
    vector<runtime::PerformanceCounter> perf_data = backend->get_performance_data(f);
Ashok Emani's avatar
Ashok Emani committed
241 242 243 244 245 246
    sort(perf_data.begin(),
         perf_data.end(),
         [](const runtime::PerformanceCounter& p1, const runtime::PerformanceCounter& p2) {
             return p1.total_microseconds() > p2.total_microseconds();
         });
    multimap<size_t, string> timing = aggregate_timing(perf_data);
247 248 249 250 251 252 253
    multimap<size_t, string> timing_details = aggregate_timing_details(perf_data, f);

    cout << "\n---- Aggregate times per op type ----\n";
    print_times(timing);

    cout << "\n---- Aggregate times per op type/shape ----\n";
    print_times(timing_details);
Ashok Emani's avatar
Ashok Emani committed
254
}