nbench.cpp 5.33 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
/*******************************************************************************
* 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.
*******************************************************************************/

// tool to benchmark any ngraph json model with given backend.
// compile and run with:
// g++ ./nbench.cpp -std=c++11 -I$HOME/ngraph_dist/include -L$HOME/ngraph_dist/lib -lngraph -o nbench
// env LD_LIBRARY_PATH=$HOME/ngraph_dist/lib env NGRAPH_INTERPRETER_EMIT_TIMING=1 ./nbench
// sample models are under ../../test/models

#include <fstream>
24
#include <ngraph/file_util.hpp>
25 26 27
#include <ngraph/file_util.hpp>
#include <ngraph/pass/manager.hpp>
#include <ngraph/pass/visualize_tree.hpp>
Ashok Emani's avatar
Ashok Emani committed
28
#include <ngraph/runtime/backend.hpp>
29 30
#include <ngraph/runtime/call_frame.hpp>
#include <ngraph/runtime/manager.hpp>
31 32
#include <ngraph/util.hpp>

33 34
#include "util/benchmark.hpp"
#include "util/test_tools.hpp"
35

36
using namespace std;
37
using namespace ngraph;
38 39 40

int main(int argc, char** argv)
{
41 42 43
    string model;
    string backend = "CPU";
    int iterations = 10;
44
    bool failed = false;
45 46
    bool statistics = false;
    bool timing_detail = false;
47
    bool visualize = false;
48 49
    for (size_t i = 1; i < argc; i++)
    {
50 51
        string arg = argv[i];
        if (arg == "-f" || arg == "--file")
52 53 54
        {
            model = argv[++i];
        }
55
        else if (arg == "-b" || arg == "--backend")
56 57 58
        {
            backend = argv[++i];
        }
59
        else if (arg == "-i" || arg == "--iterations")
60 61 62
        {
            try
            {
63
                iterations = stoi(argv[++i]);
64 65 66 67 68 69 70
            }
            catch (...)
            {
                cout << "Invalid Argument\n";
                failed = true;
            }
        }
71 72 73 74 75 76 77 78
        else if (arg == "-s" || arg == "--statistics")
        {
            statistics = true;
        }
        else if (arg == "--timing_detail")
        {
            timing_detail = true;
        }
79 80 81 82
        else if (arg == "-v" || arg == "--visualize")
        {
            visualize = true;
        }
83 84 85 86 87
        else
        {
            cout << "Unknown option: " << arg << endl;
            failed = true;
        }
88 89 90 91 92 93 94 95 96 97
    }
    if (!static_cast<bool>(ifstream(model)))
    {
        cout << "File " << model << " not found\n";
        failed = true;
    }

    if (failed)
    {
        cout << R"###(
98 99 100 101
DESCRIPTION
    Benchmark ngraph json model with given backend.

SYNOPSIS
Ashok Emani's avatar
Ashok Emani committed
102
        nbench [-f <filename>] [-b <backend>] [-i <iterations>]
103 104

OPTIONS
105 106 107 108
        -f|--file          Serialized model file
        -b|--backend       Backend to use (default: CPU)
        -i|--iterations    Iterations (default: 10)
        -s|--statistics    Display op stastics
109
        -v|--visualize     Visualize a model (WARNING: requires GraphViz installed)
110
        --timing_detail    Gather detailed timing
111 112 113
)###";
        return 1;
    }
114 115 116 117

    const string json_string = file_util::read_file_to_string(model);
    stringstream ss(json_string);
    shared_ptr<Function> f = deserialize(ss);
118 119 120 121 122 123 124 125 126 127 128

    if (visualize)
    {
        auto model_file_name = ngraph::file_util::get_file_name(model) + std::string(".") +
                               pass::VisualizeTree::get_file_ext();

        pass::Manager pass_manager;
        pass_manager.register_pass<pass::VisualizeTree>(model_file_name);
        pass_manager.run_passes(f);
    }

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
    if (statistics)
    {
        cout << "statistics:" << endl;
        cout << "total nodes: " << f->get_ops().size() << endl;
        size_t total_constant_bytes = 0;
        unordered_map<string, size_t> op_list;
        for (shared_ptr<Node> node : f->get_ordered_ops())
        {
            string name = node->get_name();
            string op_name = name.substr(0, name.find('_'));
            string shape_name = "{" + join(node->get_outputs()[0].get_shape()) + "}";
            op_list[op_name + shape_name]++;

            if (op_name == "Constant")
            {
                const Shape& shape = node->get_outputs()[0].get_shape();
                size_t const_size = node->get_outputs()[0].get_element_type().size();
                if (shape.size() == 0)
                {
                    total_constant_bytes += const_size;
                }
                else
                {
                    total_constant_bytes +=
                        (const_size * shape_size(node->get_outputs()[0].get_shape()));
                }
            }
        }
        cout << "Total Constant size: " << total_constant_bytes << " bytes\n";
        for (const pair<string, size_t>& op_info : op_list)
        {
            cout << op_info.first << ": " << op_info.second << " ops" << endl;
        }
    }
    else if (iterations > 0)
    {
        cout << "Benchmarking " << model << ", " << backend << " backend, " << iterations
             << " iterations.\n";
        run_benchmark(f, backend, iterations, timing_detail);
    }

    return 0;
171
}