main.cpp 3.42 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

Paul's avatar
Paul committed
5
6
7
8
9
10
11
12
13
14
15
16
#include <migraphx/tf.hpp>
#include <migraphx/onnx.hpp>
#include <migraphx/stringutils.hpp>

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

struct loader
{
    std::string file;
    std::string type;
Paul's avatar
Paul committed
17
    bool is_nhwc  = false;
Paul's avatar
Paul committed
18
    unsigned trim = 0;
Paul's avatar
Paul committed
19
20
21
22
23
24
25

    void parse(argument_parser& ap)
    {
        ap.add(file, {}, ap.metavar("<input file>"));
        ap.add(type, {"--onnx"}, ap.help("Load as onnx"), ap.set_value("onnx"));
        ap.add(type, {"--tf"}, ap.help("Load as tensorflow"), ap.set_value("tf"));
        ap.add(is_nhwc, {"--nhwc"}, ap.help("Treat tensorflow format as nhwc"), ap.set_value(true));
Paul's avatar
Paul committed
26
27
        ap.add(
            is_nhwc, {"--nchw"}, ap.help("Treat tensorflow format as nchw"), ap.set_value(false));
Paul's avatar
Paul committed
28
        ap.add(trim, {"--trim", "-t"}, ap.help("Trim instructions from the end"));
Paul's avatar
Paul committed
29
30
    }

Paul's avatar
Paul committed
31
    program load()
Paul's avatar
Paul committed
32
33
    {
        program p;
Paul's avatar
Paul committed
34
        if(type.empty())
Paul's avatar
Paul committed
35
        {
Paul's avatar
Paul committed
36
            if(ends_with(file, ".onnx"))
Paul's avatar
Paul committed
37
38
39
40
                type = "onnx";
            else
                type = "tf";
        }
Paul's avatar
Paul committed
41
        std::cout << "Reading: " << file << std::endl;
Paul's avatar
Paul committed
42
        if(type == "onnx")
Paul's avatar
Paul committed
43
            p = parse_onnx(file);
Paul's avatar
Paul committed
44
        else if(type == "tf")
Paul's avatar
Paul committed
45
            p = parse_tf(file, is_nhwc);
Paul's avatar
Paul committed
46
        if(trim > 0)
Paul's avatar
Paul committed
47
        {
Paul's avatar
Paul committed
48
            auto last = std::prev(p.end(), trim);
Paul's avatar
Paul committed
49
50
            p.remove_instructions(last, p.end());
        }
Paul's avatar
Paul committed
51
52
53
54
55
56
57
        return p;
    }
};

struct read : command<read>
{
    loader l;
Paul's avatar
Paul committed
58
    void parse(argument_parser& ap) { l.parse(ap); }
Paul's avatar
Paul committed
59
60
61
62
63
64
65
66

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

Paul's avatar
Paul committed
67
68
69
struct verify : command<verify>
{
    loader l;
Paul's avatar
Paul committed
70
    double tolerance     = 80;
Paul's avatar
Paul committed
71
    bool per_instruction = false;
Paul's avatar
Paul committed
72
73
74
    bool reduce          = false;
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
75
76
        l.parse(ap);
        ap.add(tolerance, {"--tolerance"}, ap.help("Tolerance for errors"));
Paul's avatar
Paul committed
77
78
79
80
81
82
        ap.add(per_instruction,
               {"-i", "--per-instruction"},
               ap.help("Verify each instruction"),
               ap.set_value(true));
        ap.add(
            reduce, {"-r", "--reduce"}, ap.help("Reduce program and verify"), ap.set_value(true));
Paul's avatar
Paul committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    }

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

        if(per_instruction)
        {
            verify_instructions(p, tolerance);
        }
        else if(reduce)
        {
            verify_reduced_program(p, tolerance);
        }
        else
        {
            verify_program(l.file, p, tolerance);
        }
    }
};

Paul's avatar
Paul committed
105
106
107
108
109
struct main_command
{
    static std::string get_command_help()
    {
        std::string result = "Commands:\n";
Paul's avatar
Paul committed
110
        for(const auto& p : get_commands())
Paul's avatar
Paul committed
111
112
113
            result += "    " + p.first + "\n";
        return result;
    }
Paul's avatar
Paul committed
114
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
115
116
117
118
119
120
121
    {
        ap.add(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
    }

    void run() {}
};

Paul's avatar
Paul committed
122
123
124
125
126
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

using namespace migraphx::driver;
Paul's avatar
Paul committed
127
128
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
129
    std::vector<std::string> args(argv + 1, argv + argc);
Paul's avatar
Paul committed
130
    if(args.empty())
Paul's avatar
Paul committed
131
        return 0;
Paul's avatar
Paul committed
132
    auto&& m = get_commands();
Paul's avatar
Paul committed
133
    auto cmd = args.front();
Paul's avatar
Paul committed
134
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
135
    {
Paul's avatar
Paul committed
136
        m.at(cmd)({args.begin() + 1, args.end()});
Paul's avatar
Paul committed
137
    }
Paul's avatar
Paul committed
138
    else
Paul's avatar
Paul committed
139
    {
Paul's avatar
Paul committed
140
        run_command<main_command>(args);
Paul's avatar
Paul committed
141
142
143
    }
    return 0;
}