main.cpp 14.2 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
#include <migraphx/tf.hpp>
#include <migraphx/onnx.hpp>
#include <migraphx/stringutils.hpp>
10
11
#include <migraphx/load_save.hpp>
#include <migraphx/json.hpp>
Paul's avatar
Paul committed
12

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

26
27
#include <fstream>

Paul's avatar
Paul committed
28
29
30
31
32
33
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

struct loader
{
34
    std::string model;
Paul's avatar
Paul committed
35
    std::string file;
Paul's avatar
Paul committed
36
    std::string file_type;
37
38
39
40
41
    unsigned batch              = 1;
    bool is_nhwc                = true;
    unsigned trim               = 0;
    bool optimize               = false;
    bool skip_unknown_operators = false;
42
43
44
    bool brief                  = false;
    std::string output_type;
    std::string output;
Paul's avatar
Paul committed
45
46
47

    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
48
        ap(file, {}, ap.metavar("<input file>"));
49
        ap(model, {"--model"}, ap.help("Load model"), ap.type("resnet50|inceptionv3|alexnet"));
Paul's avatar
Paul committed
50
51
        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"));
52
53
        ap(file_type, {"--migraphx"}, ap.help("Load as MIGraphX"), ap.set_value("migraphx"));
        ap(file_type, {"--migraphx-json"}, ap.help("Load as MIGraphX JSON"), ap.set_value("json"));
54
        ap(batch, {"--batch"}, ap.help("Set batch size for model"));
Paul's avatar
Paul committed
55
        ap(is_nhwc, {"--nhwc"}, ap.help("Treat tensorflow format as nhwc"), ap.set_value(true));
56
57
58
59
        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
60
        ap(is_nhwc, {"--nchw"}, ap.help("Treat tensorflow format as nchw"), ap.set_value(false));
Paul's avatar
Paul committed
61
        ap(trim, {"--trim", "-t"}, ap.help("Trim instructions from the end"));
62
        ap(optimize, {"--optimize", "-O"}, ap.help("Optimize when reading"), ap.set_value(true));
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        ap(output_type,
           {"--graphviz", "-g"},
           ap.help("Print out a graphviz representation."),
           ap.set_value("graphviz"));
        ap(brief, {"--brief"}, ap.help("Make the output brief."), ap.set_value(true));
        ap(output_type,
           {"--cpp"},
           ap.help("Print out the program as cpp program."),
           ap.set_value("cpp"));
        ap(output_type, {"--json"}, ap.help("Print out program as json."), ap.set_value("json"));
        ap(output_type,
           {"--text"},
           ap.help("Print out program in text format."),
           ap.set_value("text"));
        ap(output_type,
           {"--binary"},
           ap.help("Print out program in binary format."),
           ap.set_value("binary"));
        ap(output, {"--output", "-o"}, ap.help("Output to file."));
Paul's avatar
Paul committed
82
83
    }

Paul's avatar
Paul committed
84
    program load()
Paul's avatar
Paul committed
85
86
    {
        program p;
87
        if(model.empty())
Paul's avatar
Paul committed
88
        {
89
90
91
92
93
94
            if(file_type.empty())
            {
                if(ends_with(file, ".onnx"))
                    file_type = "onnx";
                else if(ends_with(file, ".pb"))
                    file_type = "tf";
95
96
97
98
                else if(ends_with(file, ".json"))
                    file_type = "json";
                else
                    file_type = "migraphx";
99
100
101
            }
            std::cout << "Reading: " << file << std::endl;
            if(file_type == "onnx")
102
103
104
105
106
107
108
            {
                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);
            }
109
            else if(file_type == "tf")
110
            {
111
                p = parse_tf(file, tf_options{is_nhwc, batch});
112
            }
113
114
115
116
117
118
119
120
121
122
            else if(file_type == "json")
            {
                file_options options;
                options.format = "json";
                p              = migraphx::load(file, options);
            }
            else if(file_type == "migraphx")
            {
                p = migraphx::load(file);
            }
123
124
125
126
127
128
129
130
131
132
133
        }
        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
134
        }
Paul's avatar
Paul committed
135
        if(trim > 0)
Paul's avatar
Paul committed
136
        {
Paul's avatar
Paul committed
137
            auto last = std::prev(p.end(), trim);
138
139
            auto* mm  = p.get_main_module();
            mm->remove_instructions(last, p.end());
Paul's avatar
Paul committed
140
        }
Paul's avatar
Paul committed
141
        if(optimize)
142
143
        {
            migraphx::run_passes(*p.get_main_module(),
Paul's avatar
Paul committed
144
                                 {
145
                                     migraphx::rewrite_batchnorm{},
Paul's avatar
Paul committed
146
147
148
149
150
151
152
153
154
155
156
                                     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{},
                                 });
157
        }
Paul's avatar
Paul committed
158
159
        return p;
    }
160
161
162
163
164
165

    static void write(std::ostream& os, const std::vector<char>& buffer)
    {
        os.write(buffer.data(), buffer.size());
    }

166
    void save(const program& p) const
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
193
194
195
    {
        auto* os = &std::cout;
        std::ofstream fs;
        if(not output.empty())
        {
            fs.open(output);
            os = &fs;
        }

        std::string type = output_type;
        if(type.empty())
        {
            if(output.empty())
                type = "text";
            else
                type = "binary";
        }

        if(type == "cpp")
            p.print_cpp(*os);
        else if(type == "graphviz")
            p.print_graph(*os, brief);
        else if(type == "text")
            *os << p << std::endl;
        else if(type == "json")
            *os << to_json_string(p.to_value()) << std::endl;
        else if(type == "binary")
            write(*os, save_buffer(p));
    }
Paul's avatar
Paul committed
196
197
};

198
199
200
201
202
203
204
205
206
207
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());
    }

208
    auto generate(const program& p, const target& t, bool offload)
209
    {
210
        parameter_map m;
211
212
213
214
        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);
215
        fill_param_map(m, p, t, offload);
216
217
218
219
        return m;
    }
};

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
struct compiler_target
{
#ifdef HAVE_GPU
    std::string target_name = "gpu";
#else
    std::string target_name = "cpu";
#endif

    void parse(argument_parser& ap)
    {
        ap(target_name, {"--gpu"}, ap.help("Compile on the gpu"), ap.set_value("gpu"));
        ap(target_name, {"--cpu"}, ap.help("Compile on the cpu"), ap.set_value("cpu"));
        ap(target_name,
           {"--ref"},
           ap.help("Compile on the reference implementation"),
           ap.set_value("ref"));
    }

    target get_target() const { return make_target(target_name); }
};

Paul's avatar
Paul committed
241
242
struct compiler
{
243
244
    static const int q_fp16 = 1;
    static const int q_int8 = 2;
Paul's avatar
Paul committed
245
    loader l;
246
    program_params parameters;
247
    compiler_target ct;
248
    bool offload_copy = false;
kahmed10's avatar
kahmed10 committed
249
    bool fast_math    = true;
250
    int quantize      = 0;
251

kahmed10's avatar
kahmed10 committed
252
    std::vector<std::string> fill0;
Paul's avatar
Paul committed
253
    std::vector<std::string> fill1;
Paul's avatar
Paul committed
254
255
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
256
        l.parse(ap);
257
        parameters.parse(ap);
258
        ct.parse(ap);
259
260
261
        ap(offload_copy,
           {"--enable-offload-copy"},
           ap.help("Enable implicit offload copying"),
262
           ap.set_value(true));
kahmed10's avatar
kahmed10 committed
263
264
265
266
        ap(fast_math,
           {"--disable-fast-math"},
           ap.help("Disable fast math optimization"),
           ap.set_value(false));
267
268
        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
269
270
    }

271
    auto params(const program& p) { return parameters.generate(p, ct.get_target(), offload_copy); }
272
273
274
275

    program compile()
    {
        auto p = l.load();
276
277
278
        // Dont compile if its already been compiled
        if(p.is_compiled())
            return p;
279
        auto t = ct.get_target();
280
281
282
283
284
285
        if(quantize == q_fp16)
        {
            quantize_fp16(p);
        }
        else if(quantize == q_int8)
        {
286
            quantize_int8(p, t, {params(p)});
287
        }
288
289
        compile_options options;
        options.offload_copy = offload_copy;
kahmed10's avatar
kahmed10 committed
290
        options.fast_math    = fast_math;
291
        p.compile(t, options);
292
        l.save(p);
293
294
        return p;
    }
Paul's avatar
Paul committed
295
296
};

Paul's avatar
Paul committed
297
298
299
struct read : command<read>
{
    loader l;
300
    void parse(argument_parser& ap) { l.parse(ap); }
Paul's avatar
Paul committed
301
302
303
304

    void run()
    {
        auto p = l.load();
305
        l.save(p);
Paul's avatar
Paul committed
306
307
308
    }
};

Paul's avatar
Paul committed
309
310
311
312
313
314
315
316
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
317
        for(auto&& param : p.get_parameter_shapes())
Paul's avatar
Paul committed
318
319
320
321
            std::cout << param.first << ": " << param.second << std::endl;
    }
};

Paul's avatar
Paul committed
322
323
324
struct verify : command<verify>
{
    loader l;
325
    program_params parameters;
326
    compiler_target ct;
Paul's avatar
Paul committed
327
    double tolerance     = 80;
Paul's avatar
Paul committed
328
    bool per_instruction = false;
Paul's avatar
Paul committed
329
    bool reduce          = false;
330
    bool offload_copy    = false;
kahmed10's avatar
kahmed10 committed
331
    bool fast_math       = true;
Paul's avatar
Paul committed
332
333
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
334
        l.parse(ap);
335
        parameters.parse(ap);
336
        ct.parse(ap);
337
338
339
340
        ap(offload_copy,
           {"--enable-offload-copy"},
           ap.help("Enable implicit offload copying"),
           ap.set_value(true));
kahmed10's avatar
kahmed10 committed
341
342
343
344
        ap(fast_math,
           {"--disable-fast-math"},
           ap.help("Disable fast math optimization"),
           ap.set_value(false));
Paul's avatar
Paul committed
345
346
        ap(tolerance, {"--tolerance"}, ap.help("Tolerance for errors"));
        ap(per_instruction,
Paul's avatar
Paul committed
347
348
349
350
           {"-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
351
352
353
354
355
    }

    void run()
    {
        auto p = l.load();
356
        l.save(p);
Paul's avatar
Paul committed
357
358
        std::cout << p << std::endl;

359
360
        compile_options options;
        options.offload_copy = offload_copy;
kahmed10's avatar
kahmed10 committed
361
        options.fast_math    = fast_math;
362
363
        auto t               = ct.get_target();
        auto m               = parameters.generate(p, t, true);
364

Paul's avatar
Paul committed
365
366
        if(per_instruction)
        {
367
            verify_instructions(p, t, options, tolerance);
Paul's avatar
Paul committed
368
369
370
        }
        else if(reduce)
        {
371
            verify_reduced_program(p, t, options, m, tolerance);
Paul's avatar
Paul committed
372
373
374
        }
        else
        {
375
            verify_program(l.file, p, t, options, m, tolerance);
Paul's avatar
Paul committed
376
377
378
379
        }
    }
};

Paul's avatar
Paul committed
380
381
382
struct compile : command<compile>
{
    compiler c;
Paul's avatar
Paul committed
383
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
384
385
386
387

    void run()
    {
        std::cout << "Compiling ... " << std::endl;
388
        c.compile();
Paul's avatar
Paul committed
389
390
391
392
393
394
    }
};

struct run_cmd : command<run_cmd>
{
    compiler c;
Paul's avatar
Paul committed
395
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
396
397
398
399
400
401
402

    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
403
        p.eval(m);
Paul's avatar
Paul committed
404
405
406
407
        std::cout << p << std::endl;
    }
};

Paul's avatar
Paul committed
408
409
410
411
struct perf : command<perf>
{
    compiler c;
    unsigned n = 100;
Paul's avatar
Paul committed
412
413
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
414
415
416
417
418
419
420
421
422
423
424
425
426
        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);
    }
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
};

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
447
448
};

Paul's avatar
Paul committed
449
450
451
452
453
struct main_command
{
    static std::string get_command_help()
    {
        std::string result = "Commands:\n";
Paul's avatar
Paul committed
454
455
456
457
        return std::accumulate(get_commands().begin(),
                               get_commands().end(),
                               result,
                               [](auto r, auto&& p) { return r + "    " + p.first + "\n"; });
Paul's avatar
Paul committed
458
    }
Paul's avatar
Paul committed
459
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
460
    {
Paul's avatar
Paul committed
461
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
Paul's avatar
Paul committed
462
463
464
465
466
    }

    void run() {}
};

Paul's avatar
Paul committed
467
468
469
470
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
471
using namespace migraphx::driver; // NOLINT
Paul's avatar
Paul committed
472
473
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
474
    std::vector<std::string> args(argv + 1, argv + argc);
Paul's avatar
Paul committed
475
    if(args.empty())
Paul's avatar
Paul committed
476
        return 0;
Paul's avatar
Paul committed
477
    auto&& m = get_commands();
Paul's avatar
Paul committed
478
    auto cmd = args.front();
Paul's avatar
Paul committed
479
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
480
    {
Paul's avatar
Paul committed
481
        m.at(cmd)({args.begin() + 1, args.end()});
Paul's avatar
Paul committed
482
    }
Paul's avatar
Paul committed
483
    else
Paul's avatar
Paul committed
484
    {
Paul's avatar
Paul committed
485
        run_command<main_command>(args);
Paul's avatar
Paul committed
486
487
488
    }
    return 0;
}