nbench.cpp 7.2 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
    string model;
    string backend = "CPU";
40
    string directory;
41
    int iterations = 10;
42
    bool failed = false;
43 44
    bool statistics = false;
    bool timing_detail = false;
45
    bool visualize = false;
46 47
    int warmup_iterations = 1;

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
        else if (arg == "-s" || arg == "--statistics")
        {
            statistics = true;
        }
75
        else if (arg == "--timing_detail" || arg == "--timing-detail")
76 77 78
        {
            timing_detail = true;
        }
79 80 81 82
        else if (arg == "-v" || arg == "--visualize")
        {
            visualize = true;
        }
83 84 85 86
        else if (arg == "-d" || arg == "--directory")
        {
            directory = argv[++i];
        }
87 88 89 90 91 92 93 94 95 96 97 98
        else if (arg == "-w" || arg == "--warmup_iterations")
        {
            try
            {
                warmup_iterations = stoi(argv[++i]);
            }
            catch (...)
            {
                cout << "Invalid Argument\n";
                failed = true;
            }
        }
99 100 101 102 103
        else
        {
            cout << "Unknown option: " << arg << endl;
            failed = true;
        }
104
    }
105
    if (!model.empty() && !file_util::exists(model))
106 107 108 109
    {
        cout << "File " << model << " not found\n";
        failed = true;
    }
110 111 112 113 114 115 116 117 118 119
    else if (!directory.empty() && !file_util::exists(directory))
    {
        cout << "Directory " << model << " not found\n";
        failed = true;
    }
    else if (directory.empty() && model.empty())
    {
        cout << "Either file or directory must be specified\n";
        failed = true;
    }
120 121 122 123

    if (failed)
    {
        cout << R"###(
124 125 126 127
DESCRIPTION
    Benchmark ngraph json model with given backend.

SYNOPSIS
Ashok Emani's avatar
Ashok Emani committed
128
        nbench [-f <filename>] [-b <backend>] [-i <iterations>]
129 130

OPTIONS
131 132 133 134 135 136 137 138
        -f|--file                 Serialized model file
        -b|--backend              Backend to use (default: CPU)
        -d|--directory            Directory to scan for models. All models are benchmarked.
        -i|--iterations           Iterations (default: 10)
        -s|--statistics           Display op stastics
        -v|--visualize            Visualize a model (WARNING: requires GraphViz installed)
        --timing-detail           Gather detailed timing
        -w|--warmup_iterations    Number of warm-up iterations
139 140 141
)###";
        return 1;
    }
142

143 144
    if (visualize)
    {
145
        shared_ptr<Function> f = deserialize(model);
146 147 148 149 150 151 152 153
        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);
    }

154 155
    if (statistics)
    {
156 157
        shared_ptr<Function> f = deserialize(model);

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
        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;
        }
    }
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    else if (!directory.empty())
    {
        vector<string> models;
        file_util::iterate_files(directory,
                                 [&](const string& file, bool is_dir) {
                                     if (!is_dir)
                                     {
                                         models.push_back(file);
                                     }
                                 },
                                 true);
        for (const string& m : models)
        {
            try
            {
                shared_ptr<Function> f = deserialize(m);
                cout << "Benchmarking " << m << ", " << backend << " backend, " << iterations
                     << " iterations.\n";
208
                run_benchmark(f, backend, iterations, timing_detail, warmup_iterations);
209 210 211 212 213 214 215
            }
            catch (exception e)
            {
                cout << "Exception caught on '" << m << "'\n" << e.what() << endl;
            }
        }
    }
216 217
    else if (iterations > 0)
    {
218
        shared_ptr<Function> f = deserialize(model);
219 220
        cout << "Benchmarking " << model << ", " << backend << " backend, " << iterations
             << " iterations.\n";
221
        run_benchmark(f, backend, iterations, timing_detail, warmup_iterations);
222 223 224
    }

    return 0;
225
}