main.cpp 20.8 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
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
kahmed10's avatar
kahmed10 committed
24
#include "verify.hpp"
Paul's avatar
Paul committed
25
26
#include "argument_parser.hpp"
#include "command.hpp"
kahmed10's avatar
kahmed10 committed
27
#include "precision.hpp"
Paul's avatar
Paul committed
28
#include "perf.hpp"
29
#include "models.hpp"
30
#include "marker_roctx.hpp"
Paul's avatar
Paul committed
31

Paul's avatar
Paul committed
32
33
34
#include <migraphx/tf.hpp>
#include <migraphx/onnx.hpp>
#include <migraphx/stringutils.hpp>
35
36
#include <migraphx/load_save.hpp>
#include <migraphx/json.hpp>
37
#include <migraphx/version.h>
Paul's avatar
Paul committed
38

39
40
41
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/eliminate_pad.hpp>
42
43
#include <migraphx/generate.hpp>
#include <migraphx/pass_manager.hpp>
44
#include <migraphx/propagate_constant.hpp>
45
#include <migraphx/quantization.hpp>
46
#include <migraphx/register_op.hpp>
47
#include <migraphx/rewrite_batchnorm.hpp>
48
49
#include <migraphx/simplify_algebra.hpp>
#include <migraphx/simplify_reshapes.hpp>
50
#include <migraphx/register_target.hpp>
51

52
53
#include <fstream>

Paul's avatar
Paul committed
54
55
56
57
58
59
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

struct loader
{
60
    std::string model;
Paul's avatar
Paul committed
61
    std::string file;
Paul's avatar
Paul committed
62
    std::string file_type;
63
64
65
66
67
    unsigned batch              = 1;
    bool is_nhwc                = true;
    unsigned trim               = 0;
    bool optimize               = false;
    bool skip_unknown_operators = false;
68
69
70
    bool brief                  = false;
    std::string output_type;
    std::string output;
Shucai Xiao's avatar
Shucai Xiao committed
71
    std::vector<std::string> param_dims;
kahmed10's avatar
kahmed10 committed
72
    std::vector<std::string> output_names;
Paul's avatar
Paul committed
73
74
75

    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
76
        ap(file, {}, ap.metavar("<input file>"), ap.file_exist());
77
        ap(model, {"--model"}, ap.help("Load model"), ap.type("resnet50|inceptionv3|alexnet"));
Paul's avatar
Paul committed
78
79
        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"));
80
81
        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"));
82
        ap(batch, {"--batch"}, ap.help("Set batch size for model"));
Paul's avatar
Paul committed
83
        ap(is_nhwc, {"--nhwc"}, ap.help("Treat tensorflow format as nhwc"), ap.set_value(true));
84
85
86
87
        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
88
        ap(is_nhwc, {"--nchw"}, ap.help("Treat tensorflow format as nchw"), ap.set_value(false));
Paul's avatar
Paul committed
89
        ap(trim, {"--trim", "-t"}, ap.help("Trim instructions from the end"));
Shucai Xiao's avatar
Shucai Xiao committed
90
91
92
93
94
        ap(param_dims,
           {"--input-dim"},
           ap.help("Dim of a parameter (format: \"@name d1 d2 dn\")"),
           ap.append(),
           ap.nargs(2));
kahmed10's avatar
kahmed10 committed
95
96
97
98
99
100

        ap(output_names,
           {"--output-names"},
           ap.help("Names of node output (format: \"name_1 name_2 name_n\")"),
           ap.append(),
           ap.nargs(2));
101
        ap(optimize, {"--optimize", "-O"}, ap.help("Optimize when reading"), ap.set_value(true));
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        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
121
122
    }

Shucai Xiao's avatar
Shucai Xiao committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    static auto parse_param_dims(const std::vector<std::string>& param_dims_info)
    {
        std::unordered_map<std::string, std::vector<std::size_t>> map_input_dims;
        std::string name = "";
        for(auto&& x : param_dims_info)
        {
            if(x[0] == '@')
            {
                name = x.substr(1);
            }
            else
            {
                map_input_dims[name].push_back(value_parser<std::size_t>::apply(x));
            }
        }

        return map_input_dims;
    }

kahmed10's avatar
kahmed10 committed
142
143
144
145
146
147
148
149
150
151
152
    static auto parse_output_names(const std::vector<std::string>& output_names_info)
    {
        std::vector<std::string> output_node_names;
        std::transform(output_names_info.begin(),
                       output_names_info.end(),
                       std::back_inserter(output_node_names),
                       [&](auto x) { return value_parser<std::string>::apply(x); });

        return output_node_names;
    }

Paul's avatar
Paul committed
153
    program load()
Paul's avatar
Paul committed
154
155
    {
        program p;
156
        if(model.empty())
Paul's avatar
Paul committed
157
        {
kahmed10's avatar
kahmed10 committed
158
159
            auto map_input_dims    = parse_param_dims(param_dims);
            auto output_node_names = parse_output_names(output_names);
160
161
162
163
164
165
            if(file_type.empty())
            {
                if(ends_with(file, ".onnx"))
                    file_type = "onnx";
                else if(ends_with(file, ".pb"))
                    file_type = "tf";
166
167
168
169
                else if(ends_with(file, ".json"))
                    file_type = "json";
                else
                    file_type = "migraphx";
170
171
172
            }
            std::cout << "Reading: " << file << std::endl;
            if(file_type == "onnx")
173
174
175
176
177
            {
                onnx_options options;
                options.default_dim_value      = batch;
                options.skip_unknown_operators = skip_unknown_operators;
                options.print_program_on_error = true;
Shucai Xiao's avatar
Shucai Xiao committed
178
                options.map_input_dims         = map_input_dims;
179
180
                p                              = parse_onnx(file, options);
            }
181
            else if(file_type == "tf")
182
            {
kahmed10's avatar
kahmed10 committed
183
                p = parse_tf(file, tf_options{is_nhwc, batch, map_input_dims, output_node_names});
184
            }
185
186
187
188
189
190
191
192
193
194
            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);
            }
195
196
197
198
199
200
201
202
203
204
205
        }
        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
206
        }
Paul's avatar
Paul committed
207
        if(trim > 0)
Paul's avatar
Paul committed
208
        {
209
            auto* mm  = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
210
211
            auto last = std::prev(mm->end(), trim);
            mm->remove_instructions(last, mm->end());
Paul's avatar
Paul committed
212
        }
213
214
215
        // Remove unused variable when exporting to cpp
        if(output_type == "cpp")
            migraphx::run_passes(*p.get_main_module(), {migraphx::dead_code_elimination{}});
Paul's avatar
Paul committed
216
        if(optimize)
217
218
        {
            migraphx::run_passes(*p.get_main_module(),
Paul's avatar
Paul committed
219
                                 {
220
                                     migraphx::rewrite_batchnorm{},
Paul's avatar
Paul committed
221
222
223
224
225
226
227
228
229
230
231
                                     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{},
                                 });
232
        }
Paul's avatar
Paul committed
233
234
        return p;
    }
235
236
237
238
239
240

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

241
    void save(const program& p) const
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    {
        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
271
272
};

273
274
275
276
277
278
struct program_params
{
    std::vector<std::string> fill0{};
    std::vector<std::string> fill1{};
    void parse(argument_parser& ap)
    {
Shucai Xiao's avatar
Shucai Xiao committed
279
280
        ap(fill0, {"--fill0"}, ap.help("Fill parameter with 0s"), ap.append(), ap.nargs(2));
        ap(fill1, {"--fill1"}, ap.help("Fill parameter with 1s"), ap.append(), ap.nargs(2));
281
282
    }

283
    auto generate(const program& p, const target& t, bool offload)
284
    {
285
        parameter_map m;
286
287
288
289
        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);
290
        fill_param_map(m, p, t, offload);
291
292
293
294
        return m;
    }
};

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
316
317
318
struct compiler
{
    loader l;
319
    program_params parameters;
320
    compiler_target ct;
kahmed10's avatar
kahmed10 committed
321
322
323
    bool offload_copy  = false;
    bool fast_math     = true;
    precision quantize = precision::fp32;
324

kahmed10's avatar
kahmed10 committed
325
    std::vector<std::string> fill0;
Paul's avatar
Paul committed
326
    std::vector<std::string> fill1;
Paul's avatar
Paul committed
327
328
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
329
        l.parse(ap);
330
        parameters.parse(ap);
331
        ct.parse(ap);
332
333
334
        ap(offload_copy,
           {"--enable-offload-copy"},
           ap.help("Enable implicit offload copying"),
335
           ap.set_value(true));
kahmed10's avatar
kahmed10 committed
336
337
338
339
        ap(fast_math,
           {"--disable-fast-math"},
           ap.help("Disable fast math optimization"),
           ap.set_value(false));
kahmed10's avatar
kahmed10 committed
340
341
        ap(quantize, {"--fp16"}, ap.help("Quantize for fp16"), ap.set_value(precision::fp16));
        ap(quantize, {"--int8"}, ap.help("Quantize for int8"), ap.set_value(precision::int8));
Paul's avatar
Paul committed
342
343
    }

344
    auto params(const program& p) { return parameters.generate(p, ct.get_target(), offload_copy); }
345
346
347
348

    program compile()
    {
        auto p = l.load();
349
350
351
        // Dont compile if its already been compiled
        if(p.is_compiled())
            return p;
352
        auto t = ct.get_target();
kahmed10's avatar
kahmed10 committed
353
        if(quantize == precision::fp16)
354
355
356
        {
            quantize_fp16(p);
        }
kahmed10's avatar
kahmed10 committed
357
        else if(quantize == precision::int8)
358
        {
359
            quantize_int8(p, t, {params(p)});
360
        }
361
362
        compile_options options;
        options.offload_copy = offload_copy;
kahmed10's avatar
kahmed10 committed
363
        options.fast_math    = fast_math;
364
        p.compile(t, options);
365
        l.save(p);
366
367
        return p;
    }
Paul's avatar
Paul committed
368
369
};

Paul's avatar
Paul committed
370
371
372
struct read : command<read>
{
    loader l;
373
    void parse(argument_parser& ap) { l.parse(ap); }
Paul's avatar
Paul committed
374
375
376
377

    void run()
    {
        auto p = l.load();
378
        l.save(p);
Paul's avatar
Paul committed
379
380
381
    }
};

Paul's avatar
Paul committed
382
383
384
385
386
387
388
389
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
390
        for(auto&& param : p.get_parameter_shapes())
Paul's avatar
Paul committed
391
392
393
394
            std::cout << param.first << ": " << param.second << std::endl;
    }
};

Paul's avatar
Paul committed
395
396
397
struct verify : command<verify>
{
    loader l;
398
    program_params parameters;
399
    compiler_target ct;
Paul's avatar
Paul committed
400
    double tolerance     = 80;
Paul's avatar
Paul committed
401
    bool per_instruction = false;
Paul's avatar
Paul committed
402
    bool reduce          = false;
403
    bool offload_copy    = false;
kahmed10's avatar
kahmed10 committed
404
    bool fast_math       = true;
kahmed10's avatar
kahmed10 committed
405
    precision quantize   = precision::fp32;
Paul's avatar
Paul committed
406
407
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
408
        l.parse(ap);
409
        parameters.parse(ap);
410
        ct.parse(ap);
411
412
413
414
        ap(offload_copy,
           {"--enable-offload-copy"},
           ap.help("Enable implicit offload copying"),
           ap.set_value(true));
kahmed10's avatar
kahmed10 committed
415
416
417
418
        ap(fast_math,
           {"--disable-fast-math"},
           ap.help("Disable fast math optimization"),
           ap.set_value(false));
Paul's avatar
Paul committed
419
420
        ap(tolerance, {"--tolerance"}, ap.help("Tolerance for errors"));
        ap(per_instruction,
Paul's avatar
Paul committed
421
422
423
424
           {"-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));
kahmed10's avatar
kahmed10 committed
425
        ap(quantize, {"--fp16"}, ap.help("Quantize for fp16"), ap.set_value(precision::fp16));
Paul's avatar
Paul committed
426
427
428
429
430
    }

    void run()
    {
        auto p = l.load();
431
        l.save(p);
Paul's avatar
Paul committed
432
433
        std::cout << p << std::endl;

434
435
        compile_options options;
        options.offload_copy = offload_copy;
kahmed10's avatar
kahmed10 committed
436
        options.fast_math    = fast_math;
437
438
        auto t               = ct.get_target();
        auto m               = parameters.generate(p, t, true);
439

Paul's avatar
Paul committed
440
441
        if(per_instruction)
        {
kahmed10's avatar
kahmed10 committed
442
            verify_instructions(p, t, options, quantize, tolerance);
Paul's avatar
Paul committed
443
444
445
        }
        else if(reduce)
        {
kahmed10's avatar
kahmed10 committed
446
            verify_reduced_program(p, t, options, quantize, m, tolerance);
Paul's avatar
Paul committed
447
448
449
        }
        else
        {
kahmed10's avatar
kahmed10 committed
450
            verify_program(l.file, p, t, options, quantize, m, tolerance);
Paul's avatar
Paul committed
451
452
453
454
        }
    }
};

455
456
457
458
459
460
461
462
463
464
struct version : command<version>
{
    void parse(const argument_parser&) {}
    void run() const
    {
        std::cout << "MIGraphX Version: " << MIGRAPHX_VERSION_MAJOR << "." << MIGRAPHX_VERSION_MINOR
                  << std::endl;
    }
};

Paul's avatar
Paul committed
465
466
467
struct compile : command<compile>
{
    compiler c;
Paul's avatar
Paul committed
468
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
469
470
471
472

    void run()
    {
        std::cout << "Compiling ... " << std::endl;
473
        c.compile();
Paul's avatar
Paul committed
474
475
476
477
478
479
    }
};

struct run_cmd : command<run_cmd>
{
    compiler c;
Paul's avatar
Paul committed
480
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
481
482
483
484
485
486
487

    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
488
        p.eval(m);
Paul's avatar
Paul committed
489
490
491
492
        std::cout << p << std::endl;
    }
};

Paul's avatar
Paul committed
493
494
495
496
struct perf : command<perf>
{
    compiler c;
    unsigned n = 100;
Paul's avatar
Paul committed
497
498
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
499
500
501
502
503
504
505
506
507
508
509
        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;
510
        p.perf_report(std::cout, n, m, c.l.batch);
Paul's avatar
Paul committed
511
    }
512
513
};

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
struct roctx : command<roctx>
{
    compiler c;
    void parse(argument_parser& ap) { c.parse(ap); }

    void run()
    {
        std::cout << "Compiling ... " << std::endl;
        auto p = c.compile();
        std::cout << "Allocating params ... " << std::endl;
        auto m = c.params(p);
        std::cout << "rocTX:\tLoading rocTX library..." << std::endl;
        auto rtx = create_marker_roctx();
        p.mark(m, std::move(rtx));
    }
};

531
532
533
struct op : command<op>
{
    bool show_ops = false;
534
    std::string op_name{};
535
536
    void parse(argument_parser& ap)
    {
537
        ap(op_name, {}, ap.metavar("<MIGraphX operator name>"));
538
539
540
541
542
543
544
545
546
547
548
549
        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;
        }
550
551
552
553
554
555
        else
        {
            auto op = load_op(op_name);
            std::cout << op_name << ": " << std::endl;
            std::cout << to_pretty_json_string(op.to_value()) << std::endl;
        }
556
    }
Paul's avatar
Paul committed
557
558
};

559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
struct onnx : command<onnx>
{
    bool show_ops = false;
    void parse(argument_parser& ap)
    {
        ap(show_ops,
           {"--list", "-l"},
           ap.help("List all onnx operators supported by MIGraphX"),
           ap.set_value(true));
    }
    void run() const
    {
        if(show_ops)
        {
            for(const auto& name : get_onnx_operators())
                std::cout << name << std::endl;
        }
    }
};

Paul's avatar
Paul committed
579
580
struct main_command
{
Paul's avatar
Format  
Paul committed
581
582
    static std::string get_command_help(const std::string& title = colorize(color::fg_yellow,
                                                                            "COMMANDS:"))
Paul's avatar
Paul committed
583
    {
Paul's avatar
Paul committed
584
        std::string result = title + "\n";
Paul's avatar
Paul committed
585
        std::vector<std::string> commands(get_commands().size());
Paul's avatar
Format  
Paul committed
586
587
588
589
        std::transform(get_commands().begin(),
                       get_commands().end(),
                       commands.begin(),
                       [](const auto& p) { return colorize(color::fg_green, p.first); });
Paul's avatar
Paul committed
590
        std::sort(commands.begin(), commands.end());
Paul's avatar
Format  
Paul committed
591
592
593
        return std::accumulate(commands.begin(), commands.end(), result, [](auto r, auto&& s) {
            return r + "    " + s + "\n";
        });
Paul's avatar
Paul committed
594
    }
Paul's avatar
Paul committed
595
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
596
    {
597
598
        std::string version_str = "MIGraphX Version: " + std::to_string(MIGRAPHX_VERSION_MAJOR) +
                                  "." + std::to_string(MIGRAPHX_VERSION_MINOR);
599
        ap(wrong_commands, {}, ap.metavar("<command>"), ap.append());
Paul's avatar
Paul committed
600
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
601
602
603
604
        ap(nullptr,
           {"-v", "--version"},
           ap.help("Show MIGraphX version"),
           ap.show_help(version_str));
Paul's avatar
Paul committed
605
606
607

        // Trim command off of exe name
        ap.set_exe_name(ap.get_exe_name().substr(0, ap.get_exe_name().size() - 5));
608
        ap.set_exe_name_to(exe_name);
Paul's avatar
Paul committed
609
610
    }

611
612
    std::vector<std::string> wrong_commands{};
    std::string exe_name = "<exe>";
Paul's avatar
Paul committed
613
614

    void run()
Paul's avatar
Format  
Paul committed
615
    {
Paul's avatar
Paul committed
616
        std::cout << color::fg_red << color::bold << "error: " << color::reset;
617
618
619
        auto it = std::find_if(wrong_commands.begin(), wrong_commands.end(), [](const auto& c) {
            return get_commands().count(c) > 0;
        });
Paul's avatar
Format  
Paul committed
620
        if(it == wrong_commands.end())
621
        {
Paul's avatar
Format  
Paul committed
622
623
            std::cout << "'" << color::fg_yellow << wrong_commands.front() << color::reset
                      << "' is not a valid command." << std::endl;
624
625
626
627
            std::cout << get_command_help("Available commands:") << std::endl;
        }
        else
        {
Paul's avatar
Format  
Paul committed
628
629
            std::cout << "command '" << color::fg_yellow << *it << color::reset
                      << "' must be first argument" << std::endl;
630
631
632
633
634
635
            std::cout << std::endl;

            std::cout << color::fg_yellow << "USAGE:" << color::reset << std::endl;
            std::cout << "    " << exe_name << " " << *it << " <options>" << std::endl;
        }
        std::cout << std::endl;
Paul's avatar
Paul committed
636
    }
Paul's avatar
Paul committed
637
638
};

Paul's avatar
Paul committed
639
640
641
642
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
643
using namespace migraphx::driver; // NOLINT
Paul's avatar
Paul committed
644
645
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
646
    std::vector<std::string> args(argv + 1, argv + argc);
Shucai Xiao's avatar
Shucai Xiao committed
647
648

    // no argument, print the help infomration by default
Paul's avatar
Paul committed
649
    if(args.empty())
Shucai Xiao's avatar
Shucai Xiao committed
650
651
652
653
    {
        args.push_back("-h");
    }

Paul's avatar
Paul committed
654
    auto&& m = get_commands();
Paul's avatar
Paul committed
655
    auto cmd = args.front();
Paul's avatar
Paul committed
656
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
657
    {
Paul's avatar
Paul committed
658
        m.at(cmd)(argv[0], {args.begin() + 1, args.end()});
Paul's avatar
Paul committed
659
    }
Paul's avatar
Paul committed
660
    else
Paul's avatar
Paul committed
661
    {
Paul's avatar
Paul committed
662
        run_command<main_command>(argv[0], args);
Paul's avatar
Paul committed
663
    }
Shucai Xiao's avatar
Shucai Xiao committed
664

Paul's avatar
Paul committed
665
666
    return 0;
}