main.cpp 8.23 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
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/eliminate_pad.hpp>
13
14
#include <migraphx/generate.hpp>
#include <migraphx/pass_manager.hpp>
15
#include <migraphx/propagate_constant.hpp>
16
17
#include <migraphx/quantization.hpp>
#include <migraphx/rewrite_batchnorm.hpp>
18
19
20
#include <migraphx/simplify_algebra.hpp>
#include <migraphx/simplify_reshapes.hpp>

21
22
#include <fstream>

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

struct loader
{
    std::string file;
Paul's avatar
Paul committed
30
    std::string file_type;
Paul's avatar
Paul committed
31
    bool is_nhwc  = true;
Paul's avatar
Paul committed
32
    unsigned trim = 0;
33
    bool optimize = false;
Paul's avatar
Paul committed
34
35
36

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

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

Paul's avatar
Paul committed
85
86
struct compiler
{
87
88
    static const int q_fp16 = 1;
    static const int q_int8 = 2;
Paul's avatar
Paul committed
89
    loader l;
90
91
92
    bool gpu     = true;
    int quantize = 0;

Paul's avatar
Paul committed
93
    std::vector<std::string> fill1;
Paul's avatar
Paul committed
94
95
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
96
97
98
        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));
99
100
        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
101
        ap(fill1, {"--fill1"}, ap.help("Fill parameter with 1s"), ap.append());
Paul's avatar
Paul committed
102
103
    }

104
    auto params(const program& p, bool use_gpu = true)
Paul's avatar
Paul committed
105
106
    {
        program::parameter_map m;
Paul's avatar
Paul committed
107
        for(auto&& s : fill1)
Paul's avatar
Paul committed
108
            m[s] = fill_argument(p.get_parameter_shape(s), 1);
109
        fill_param_map(m, p, use_gpu && gpu);
Paul's avatar
Paul committed
110
111
        return m;
    }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

    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)});
        }
        p.compile(t);
        return p;
    }
Paul's avatar
Paul committed
128
129
};

Paul's avatar
Paul committed
130
131
132
struct read : command<read>
{
    loader l;
133
134
135
136
137
138
139
140
141
142
143
144
145
    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));
        ap(output, {"--output", "-o"}, ap.help("Output to file."));
    }
Paul's avatar
Paul committed
146
147
148
149

    void run()
    {
        auto p = l.load();
150
151
152
153
154
155
156
157
158
159
160
161
162

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

        if(graphviz)
            p.print_graph(*os, brief);
        else
            *os << p << std::endl;
Paul's avatar
Paul committed
163
164
165
    }
};

Paul's avatar
Paul committed
166
167
168
169
170
171
172
173
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
174
        for(auto&& param : p.get_parameter_shapes())
Paul's avatar
Paul committed
175
176
177
178
            std::cout << param.first << ": " << param.second << std::endl;
    }
};

Paul's avatar
Paul committed
179
180
181
struct verify : command<verify>
{
    loader l;
Paul's avatar
Paul committed
182
    double tolerance     = 80;
Paul's avatar
Paul committed
183
    bool per_instruction = false;
Paul's avatar
Paul committed
184
185
186
    bool reduce          = false;
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
187
        l.parse(ap);
Paul's avatar
Paul committed
188
189
        ap(tolerance, {"--tolerance"}, ap.help("Tolerance for errors"));
        ap(per_instruction,
Paul's avatar
Paul committed
190
191
192
193
           {"-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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    }

    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
216
217
218
struct compile : command<compile>
{
    compiler c;
Paul's avatar
Paul committed
219
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
220
221
222
223
224
225
226
227
228
229
230
231

    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
232
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
233
234
235
236
237
238
239

    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
240
        p.eval(m);
Paul's avatar
Paul committed
241
242
243
244
        std::cout << p << std::endl;
    }
};

Paul's avatar
Paul committed
245
246
247
248
struct perf : command<perf>
{
    compiler c;
    unsigned n = 100;
Paul's avatar
Paul committed
249
250
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
        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
266
267
268
269
270
struct main_command
{
    static std::string get_command_help()
    {
        std::string result = "Commands:\n";
Paul's avatar
Paul committed
271
272
273
274
        return std::accumulate(get_commands().begin(),
                               get_commands().end(),
                               result,
                               [](auto r, auto&& p) { return r + "    " + p.first + "\n"; });
Paul's avatar
Paul committed
275
    }
Paul's avatar
Paul committed
276
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
277
    {
Paul's avatar
Paul committed
278
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
Paul's avatar
Paul committed
279
280
281
282
283
    }

    void run() {}
};

Paul's avatar
Paul committed
284
285
286
287
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
288
using namespace migraphx::driver; // NOLINT
Paul's avatar
Paul committed
289
290
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
291
    std::vector<std::string> args(argv + 1, argv + argc);
Paul's avatar
Paul committed
292
    if(args.empty())
Paul's avatar
Paul committed
293
        return 0;
Paul's avatar
Paul committed
294
    auto&& m = get_commands();
Paul's avatar
Paul committed
295
    auto cmd = args.front();
Paul's avatar
Paul committed
296
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
297
    {
Paul's avatar
Paul committed
298
        m.at(cmd)({args.begin() + 1, args.end()});
Paul's avatar
Paul committed
299
    }
Paul's avatar
Paul committed
300
    else
Paul's avatar
Paul committed
301
    {
Paul's avatar
Paul committed
302
        run_command<main_command>(args);
Paul's avatar
Paul committed
303
304
305
    }
    return 0;
}