"lmdeploy/vscode:/vscode.git/clone" did not exist on "9bfe03c6d9d383344ff54749f4c10fe6b0372fb0"
main.cpp 6.24 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"
Paul's avatar
Paul committed
5

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

10
11
12
13
14
15
16
17
#include <migraphx/pass_manager.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/eliminate_pad.hpp>
#include <migraphx/propagate_constant.hpp>
#include <migraphx/simplify_algebra.hpp>
#include <migraphx/simplify_reshapes.hpp>

Paul's avatar
Paul committed
18
19
20
21
22
23
24
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

struct loader
{
    std::string file;
Paul's avatar
Paul committed
25
    std::string file_type;
Paul's avatar
Paul committed
26
    bool is_nhwc  = true;
Paul's avatar
Paul committed
27
    unsigned trim = 0;
28
    bool optimize = false;
Paul's avatar
Paul committed
29
30
31

    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
32
        ap(file, {}, ap.metavar("<input file>"));
Paul's avatar
Paul committed
33
34
        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"));
Paul's avatar
Paul committed
35
        ap(is_nhwc, {"--nhwc"}, ap.help("Treat tensorflow format as nhwc"), ap.set_value(true));
Paul's avatar
Paul committed
36
        ap(is_nhwc, {"--nchw"}, ap.help("Treat tensorflow format as nchw"), ap.set_value(false));
Paul's avatar
Paul committed
37
        ap(trim, {"--trim", "-t"}, ap.help("Trim instructions from the end"));
38
        ap(optimize, {"--optimize"}, ap.help("Optimize when reading"), ap.set_value(true));
Paul's avatar
Paul committed
39
40
    }

Paul's avatar
Paul committed
41
    program load()
Paul's avatar
Paul committed
42
43
    {
        program p;
Paul's avatar
Paul committed
44
        if(file_type.empty())
Paul's avatar
Paul committed
45
        {
Paul's avatar
Paul committed
46
            if(ends_with(file, ".onnx"))
Paul's avatar
Paul committed
47
                file_type = "onnx";
Paul's avatar
Paul committed
48
49
            else if(ends_with(file, ".pb"))
                file_type = "tf";
Paul's avatar
Paul committed
50
        }
Paul's avatar
Paul committed
51
        std::cout << "Reading: " << file << std::endl;
Paul's avatar
Paul committed
52
        if(file_type == "onnx")
Paul's avatar
Paul committed
53
            p = parse_onnx(file);
Paul's avatar
Paul committed
54
        else if(file_type == "tf")
Paul's avatar
Paul committed
55
            p = parse_tf(file, is_nhwc);
Paul's avatar
Paul committed
56
        if(trim > 0)
Paul's avatar
Paul committed
57
        {
Paul's avatar
Paul committed
58
            auto last = std::prev(p.end(), trim);
Paul's avatar
Paul committed
59
60
            p.remove_instructions(last, p.end());
        }
61
62
63
64
65
66
67
68
69
70
71
72
73
        if (optimize)
            migraphx::run_passes(p, {
                            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
74
75
76
77
        return p;
    }
};

Paul's avatar
Paul committed
78
79
80
81
struct compiler
{
    loader l;
    bool gpu = true;
Paul's avatar
Paul committed
82
83
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
84
85
86
87
88
89
90
91
92
93
94
95
        l.parse(ap);
        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));
    }

    program compile()
    {
        auto p = l.load();
        compile_program(p, gpu);
        return p;
    }

Paul's avatar
Paul committed
96
    auto params(const program& p) { return create_param_map(p, gpu); }
Paul's avatar
Paul committed
97
98
};

Paul's avatar
Paul committed
99
100
101
struct read : command<read>
{
    loader l;
Paul's avatar
Paul committed
102
    void parse(argument_parser& ap) { l.parse(ap); }
Paul's avatar
Paul committed
103
104
105
106
107
108
109
110

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

Paul's avatar
Paul committed
111
112
113
struct verify : command<verify>
{
    loader l;
Paul's avatar
Paul committed
114
    double tolerance     = 80;
Paul's avatar
Paul committed
115
    bool per_instruction = false;
Paul's avatar
Paul committed
116
117
118
    bool reduce          = false;
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
119
        l.parse(ap);
Paul's avatar
Paul committed
120
121
        ap(tolerance, {"--tolerance"}, ap.help("Tolerance for errors"));
        ap(per_instruction,
Paul's avatar
Paul committed
122
123
124
125
           {"-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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    }

    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
148
149
150
struct compile : command<compile>
{
    compiler c;
Paul's avatar
Paul committed
151
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
152
153
154
155
156
157
158
159
160
161
162
163

    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
164
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
165
166
167
168
169
170
171

    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
172
        p.eval(m);
Paul's avatar
Paul committed
173
174
175
176
        std::cout << p << std::endl;
    }
};

Paul's avatar
Paul committed
177
178
179
180
struct perf : command<perf>
{
    compiler c;
    unsigned n = 100;
Paul's avatar
Paul committed
181
182
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        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);
    }
};

Paul's avatar
Paul committed
198
199
200
201
202
struct main_command
{
    static std::string get_command_help()
    {
        std::string result = "Commands:\n";
Paul's avatar
Paul committed
203
204
205
206
        return std::accumulate(get_commands().begin(),
                               get_commands().end(),
                               result,
                               [](auto r, auto&& p) { return r + "    " + p.first + "\n"; });
Paul's avatar
Paul committed
207
    }
Paul's avatar
Paul committed
208
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
209
    {
Paul's avatar
Paul committed
210
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
Paul's avatar
Paul committed
211
212
213
214
215
    }

    void run() {}
};

Paul's avatar
Paul committed
216
217
218
219
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
220
using namespace migraphx::driver; // NOLINT
Paul's avatar
Paul committed
221
222
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
223
    std::vector<std::string> args(argv + 1, argv + argc);
Paul's avatar
Paul committed
224
    if(args.empty())
Paul's avatar
Paul committed
225
        return 0;
Paul's avatar
Paul committed
226
    auto&& m = get_commands();
Paul's avatar
Paul committed
227
    auto cmd = args.front();
Paul's avatar
Paul committed
228
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
229
    {
Paul's avatar
Paul committed
230
        m.at(cmd)({args.begin() + 1, args.end()});
Paul's avatar
Paul committed
231
    }
Paul's avatar
Paul committed
232
    else
Paul's avatar
Paul committed
233
    {
Paul's avatar
Paul committed
234
        run_command<main_command>(args);
Paul's avatar
Paul committed
235
236
237
    }
    return 0;
}