nbench.cpp 5.22 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

25 26 27 28 29 30 31
#include "benchmark.hpp"
#include "ngraph/file_util.hpp"
#include "ngraph/pass/manager.hpp"
#include "ngraph/pass/visualize_tree.hpp"
#include "ngraph/runtime/backend.hpp"
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
32

33
using namespace std;
34
using namespace ngraph;
35 36 37

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

    if (failed)
    {
        cout << R"###(
95 96 97 98
DESCRIPTION
    Benchmark ngraph json model with given backend.

SYNOPSIS
Ashok Emani's avatar
Ashok Emani committed
99
        nbench [-f <filename>] [-b <backend>] [-i <iterations>]
100 101

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

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

    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);
    }

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
    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;
168
}