main.cpp 11.3 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include "argument_parser.hpp"
#include "command.hpp"
Paul's avatar
Paul committed
3
#include "verify.hpp"
Paul's avatar
Paul committed
4
#include "perf.hpp"
5
#include "models.hpp"
Paul's avatar
Paul committed
6

Paul's avatar
Paul committed
7
8
9
10
#include <migraphx/tf.hpp>
#include <migraphx/onnx.hpp>
#include <migraphx/stringutils.hpp>

11
12
13
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/eliminate_pad.hpp>
14
15
#include <migraphx/generate.hpp>
#include <migraphx/pass_manager.hpp>
16
#include <migraphx/propagate_constant.hpp>
17
#include <migraphx/quantization.hpp>
18
#include <migraphx/register_op.hpp>
19
#include <migraphx/rewrite_batchnorm.hpp>
20
21
22
#include <migraphx/simplify_algebra.hpp>
#include <migraphx/simplify_reshapes.hpp>

23
24
#include <fstream>

Paul's avatar
Paul committed
25
26
27
28
29
30
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

struct loader
{
31
    std::string model;
Paul's avatar
Paul committed
32
    std::string file;
Paul's avatar
Paul committed
33
    std::string file_type;
34
35
36
37
38
    unsigned batch              = 1;
    bool is_nhwc                = true;
    unsigned trim               = 0;
    bool optimize               = false;
    bool skip_unknown_operators = false;
Paul's avatar
Paul committed
39
40
41

    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
42
        ap(file, {}, ap.metavar("<input file>"));
43
        ap(model, {"--model"}, ap.help("Load model"), ap.type("resnet50|inceptionv3|alexnet"));
Paul's avatar
Paul committed
44
45
        ap(file_type, {"--onnx"}, ap.help("Load as onnx"), ap.set_value("onnx"));
        ap(file_type, {"--tf"}, ap.help("Load as tensorflow"), ap.set_value("tf"));
46
        ap(batch, {"--batch"}, ap.help("Set batch size for model"));
Paul's avatar
Paul committed
47
        ap(is_nhwc, {"--nhwc"}, ap.help("Treat tensorflow format as nhwc"), ap.set_value(true));
48
49
50
51
        ap(skip_unknown_operators,
           {"--skip-unknown-operators"},
           ap.help("Skip unknown operators when parsing and continue to parse."),
           ap.set_value(true));
Paul's avatar
Paul committed
52
        ap(is_nhwc, {"--nchw"}, ap.help("Treat tensorflow format as nchw"), ap.set_value(false));
Paul's avatar
Paul committed
53
        ap(trim, {"--trim", "-t"}, ap.help("Trim instructions from the end"));
54
        ap(optimize, {"--optimize", "-O"}, ap.help("Optimize when reading"), ap.set_value(true));
Paul's avatar
Paul committed
55
56
    }

Paul's avatar
Paul committed
57
    program load()
Paul's avatar
Paul committed
58
59
    {
        program p;
60
        if(model.empty())
Paul's avatar
Paul committed
61
        {
62
63
64
65
66
67
68
69
70
            if(file_type.empty())
            {
                if(ends_with(file, ".onnx"))
                    file_type = "onnx";
                else if(ends_with(file, ".pb"))
                    file_type = "tf";
            }
            std::cout << "Reading: " << file << std::endl;
            if(file_type == "onnx")
71
72
73
74
75
76
77
            {
                onnx_options options;
                options.default_dim_value      = batch;
                options.skip_unknown_operators = skip_unknown_operators;
                options.print_program_on_error = true;
                p                              = parse_onnx(file, options);
            }
78
            else if(file_type == "tf")
79
            {
80
                p = parse_tf(file, tf_options{is_nhwc, batch});
81
            }
82
83
84
85
86
87
88
89
90
91
92
        }
        else
        {
            if(model == "resnet50")
                p = resnet50(batch);
            else if(model == "inceptionv3")
                p = inceptionv3(batch);
            else if(model == "alexnet")
                p = alexnet(batch);
            else
                MIGRAPHX_THROW("Unknown model: " + model);
Paul's avatar
Paul committed
93
        }
Paul's avatar
Paul committed
94
        if(trim > 0)
Paul's avatar
Paul committed
95
        {
Paul's avatar
Paul committed
96
            auto last = std::prev(p.end(), trim);
Paul's avatar
Paul committed
97
98
            p.remove_instructions(last, p.end());
        }
Paul's avatar
Paul committed
99
100
101
        if(optimize)
            migraphx::run_passes(p,
                                 {
102
                                     migraphx::rewrite_batchnorm{},
Paul's avatar
Paul committed
103
104
105
106
107
108
109
110
111
112
113
                                     migraphx::eliminate_identity{},
                                     migraphx::dead_code_elimination{},
                                     migraphx::simplify_algebra{},
                                     migraphx::dead_code_elimination{},
                                     migraphx::simplify_reshapes{},
                                     migraphx::dead_code_elimination{},
                                     migraphx::propagate_constant{},
                                     migraphx::dead_code_elimination{},
                                     migraphx::eliminate_pad{},
                                     migraphx::dead_code_elimination{},
                                 });
Paul's avatar
Paul committed
114
115
116
117
        return p;
    }
};

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
struct program_params
{
    std::vector<std::string> fill0{};
    std::vector<std::string> fill1{};
    void parse(argument_parser& ap)
    {
        ap(fill0, {"--fill0"}, ap.help("Fill parameter with 0s"), ap.append());
        ap(fill1, {"--fill1"}, ap.help("Fill parameter with 1s"), ap.append());
    }

    auto generate(const program& p, bool use_gpu)
    {
        program::parameter_map m;
        for(auto&& s : fill0)
            m[s] = fill_argument(p.get_parameter_shape(s), 0);
        for(auto&& s : fill1)
            m[s] = fill_argument(p.get_parameter_shape(s), 1);
        fill_param_map(m, p, use_gpu);
        return m;
    }
};

Paul's avatar
Paul committed
140
141
struct compiler
{
142
143
    static const int q_fp16 = 1;
    static const int q_int8 = 2;
Paul's avatar
Paul committed
144
    loader l;
145
    program_params parameters;
146
147
148
    bool gpu          = true;
    bool offload_copy = false;
    int quantize      = 0;
149

kahmed10's avatar
kahmed10 committed
150
    std::vector<std::string> fill0;
Paul's avatar
Paul committed
151
    std::vector<std::string> fill1;
Paul's avatar
Paul committed
152
153
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
154
        l.parse(ap);
155
        parameters.parse(ap);
Paul's avatar
Paul committed
156
157
        ap(gpu, {"--gpu"}, ap.help("Compile on the gpu"), ap.set_value(true));
        ap(gpu, {"--cpu"}, ap.help("Compile on the cpu"), ap.set_value(false));
158
159
160
        ap(offload_copy,
           {"--enable-offload-copy"},
           ap.help("Enable implicit offload copying"),
161
           ap.set_value(true));
162
163
        ap(quantize, {"--fp16"}, ap.help("Quantize for fp16"), ap.set_value(q_fp16));
        ap(quantize, {"--int8"}, ap.help("Quantize for int8"), ap.set_value(q_int8));
Paul's avatar
Paul committed
164
165
    }

166
    auto params(const program& p, bool use_gpu = true)
Paul's avatar
Paul committed
167
    {
168
        return parameters.generate(p, use_gpu && gpu && !offload_copy);
Paul's avatar
Paul committed
169
    }
170
171
172
173
174
175
176
177
178
179
180
181
182

    program compile()
    {
        auto p = l.load();
        auto t = get_target(gpu);
        if(quantize == q_fp16)
        {
            quantize_fp16(p);
        }
        else if(quantize == q_int8)
        {
            quantize_int8(p, t, {params(p, false)});
        }
183
184
185
        compile_options options;
        options.offload_copy = offload_copy;
        p.compile(t, options);
186
187
        return p;
    }
Paul's avatar
Paul committed
188
189
};

Paul's avatar
Paul committed
190
191
192
struct read : command<read>
{
    loader l;
193
    bool cpp      = false;
194
195
196
197
198
199
200
201
202
203
204
    bool graphviz = false;
    bool brief    = false;
    std::string output;
    void parse(argument_parser& ap)
    {
        l.parse(ap);
        ap(graphviz,
           {"--graphviz", "-g"},
           ap.help("Print out a graphviz representation."),
           ap.set_value(true));
        ap(brief, {"--brief"}, ap.help("Make the output brief."), ap.set_value(true));
205
        ap(cpp, {"--cpp"}, ap.help("Print out the program as cpp program."), ap.set_value(true));
206
207
        ap(output, {"--output", "-o"}, ap.help("Output to file."));
    }
Paul's avatar
Paul committed
208
209
210
211

    void run()
    {
        auto p = l.load();
212
213
214
215
216
217
218
219
220

        auto* os = &std::cout;
        std::ofstream fs;
        if(not output.empty())
        {
            fs.open(output);
            os = &fs;
        }

221
222
223
        if(cpp)
            p.print_cpp(*os);
        else if(graphviz)
224
225
226
            p.print_graph(*os, brief);
        else
            *os << p << std::endl;
Paul's avatar
Paul committed
227
228
229
    }
};

Paul's avatar
Paul committed
230
231
232
233
234
235
236
237
struct params : command<params>
{
    loader l;
    void parse(argument_parser& ap) { l.parse(ap); }

    void run()
    {
        auto p = l.load();
Paul's avatar
Paul committed
238
        for(auto&& param : p.get_parameter_shapes())
Paul's avatar
Paul committed
239
240
241
242
            std::cout << param.first << ": " << param.second << std::endl;
    }
};

Paul's avatar
Paul committed
243
244
245
struct verify : command<verify>
{
    loader l;
246
    program_params parameters;
Paul's avatar
Paul committed
247
    double tolerance     = 80;
Paul's avatar
Paul committed
248
    bool per_instruction = false;
Paul's avatar
Paul committed
249
    bool reduce          = false;
250
    bool offload_copy    = false;
Paul's avatar
Paul committed
251
252
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
253
        l.parse(ap);
254
        parameters.parse(ap);
255
256
257
258
        ap(offload_copy,
           {"--enable-offload-copy"},
           ap.help("Enable implicit offload copying"),
           ap.set_value(true));
Paul's avatar
Paul committed
259
260
        ap(tolerance, {"--tolerance"}, ap.help("Tolerance for errors"));
        ap(per_instruction,
Paul's avatar
Paul committed
261
262
263
264
           {"-i", "--per-instruction"},
           ap.help("Verify each instruction"),
           ap.set_value(true));
        ap(reduce, {"-r", "--reduce"}, ap.help("Reduce program and verify"), ap.set_value(true));
Paul's avatar
Paul committed
265
266
267
268
269
270
271
    }

    void run()
    {
        auto p = l.load();
        std::cout << p << std::endl;

272
273
274
        compile_options options;
        options.offload_copy = offload_copy;

275
276
        auto m = parameters.generate(p, false);

Paul's avatar
Paul committed
277
278
        if(per_instruction)
        {
279
            verify_instructions(p, options, tolerance);
Paul's avatar
Paul committed
280
281
282
        }
        else if(reduce)
        {
283
            verify_reduced_program(p, options, m, tolerance);
Paul's avatar
Paul committed
284
285
286
        }
        else
        {
287
            verify_program(l.file, p, options, m, tolerance);
Paul's avatar
Paul committed
288
289
290
291
        }
    }
};

Paul's avatar
Paul committed
292
293
294
struct compile : command<compile>
{
    compiler c;
Paul's avatar
Paul committed
295
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
296
297
298
299
300
301
302
303
304
305
306
307

    void run()
    {
        std::cout << "Compiling ... " << std::endl;
        auto p = c.compile();
        std::cout << p << std::endl;
    }
};

struct run_cmd : command<run_cmd>
{
    compiler c;
Paul's avatar
Paul committed
308
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
309
310
311
312
313
314
315

    void run()
    {
        std::cout << "Compiling ... " << std::endl;
        auto p = c.compile();
        std::cout << "Allocating params ... " << std::endl;
        auto m = c.params(p);
Paul's avatar
Paul committed
316
        p.eval(m);
Paul's avatar
Paul committed
317
318
319
320
        std::cout << p << std::endl;
    }
};

Paul's avatar
Paul committed
321
322
323
324
struct perf : command<perf>
{
    compiler c;
    unsigned n = 100;
Paul's avatar
Paul committed
325
326
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
327
328
329
330
331
332
333
334
335
336
337
338
339
        c.parse(ap);
        ap(n, {"--iterations", "-n"}, ap.help("Number of iterations to run for perf report"));
    }

    void run()
    {
        std::cout << "Compiling ... " << std::endl;
        auto p = c.compile();
        std::cout << "Allocating params ... " << std::endl;
        auto m = c.params(p);
        std::cout << "Running performance report ... " << std::endl;
        p.perf_report(std::cout, n, m);
    }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
};

struct op : command<op>
{
    bool show_ops = false;
    void parse(argument_parser& ap)
    {
        ap(show_ops,
           {"--list", "-l"},
           ap.help("List all the operators of MIGraphX"),
           ap.set_value(true));
    }
    void run() const
    {
        if(show_ops)
        {
            for(const auto& name : get_operators())
                std::cout << name << std::endl;
        }
    }
Paul's avatar
Paul committed
360
361
};

Paul's avatar
Paul committed
362
363
364
365
366
struct main_command
{
    static std::string get_command_help()
    {
        std::string result = "Commands:\n";
Paul's avatar
Paul committed
367
368
369
370
        return std::accumulate(get_commands().begin(),
                               get_commands().end(),
                               result,
                               [](auto r, auto&& p) { return r + "    " + p.first + "\n"; });
Paul's avatar
Paul committed
371
    }
Paul's avatar
Paul committed
372
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
373
    {
Paul's avatar
Paul committed
374
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
Paul's avatar
Paul committed
375
376
377
378
379
    }

    void run() {}
};

Paul's avatar
Paul committed
380
381
382
383
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
384
using namespace migraphx::driver; // NOLINT
Paul's avatar
Paul committed
385
386
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
387
    std::vector<std::string> args(argv + 1, argv + argc);
Paul's avatar
Paul committed
388
    if(args.empty())
Paul's avatar
Paul committed
389
        return 0;
Paul's avatar
Paul committed
390
    auto&& m = get_commands();
Paul's avatar
Paul committed
391
    auto cmd = args.front();
Paul's avatar
Paul committed
392
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
393
    {
Paul's avatar
Paul committed
394
        m.at(cmd)({args.begin() + 1, args.end()});
Paul's avatar
Paul committed
395
    }
Paul's avatar
Paul committed
396
    else
Paul's avatar
Paul committed
397
    {
Paul's avatar
Paul committed
398
        run_command<main_command>(args);
Paul's avatar
Paul committed
399
400
401
    }
    return 0;
}