main.cpp 8.55 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;
    bool offload_copy = false;
    int quantize      = 0;
93

Paul's avatar
Paul committed
94
    std::vector<std::string> fill1;
Paul's avatar
Paul committed
95
96
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
97
98
99
        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));
100
101
102
103
        ap(offload_copy,
           {"--enable-offload-copy"},
           ap.help("Enable implicit offload copying"),
           ap.set_value(false));
104
105
        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
106
        ap(fill1, {"--fill1"}, ap.help("Fill parameter with 1s"), ap.append());
Paul's avatar
Paul committed
107
108
    }

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

    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)});
        }
131
132
133
        compile_options options;
        options.offload_copy = offload_copy;
        p.compile(t, options);
134
135
        return p;
    }
Paul's avatar
Paul committed
136
137
};

Paul's avatar
Paul committed
138
139
140
struct read : command<read>
{
    loader l;
141
142
143
144
145
146
147
148
149
150
151
152
153
    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
154
155
156
157

    void run()
    {
        auto p = l.load();
158
159
160
161
162
163
164
165
166
167
168
169
170

        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
171
172
173
    }
};

Paul's avatar
Paul committed
174
175
176
177
178
179
180
181
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
182
        for(auto&& param : p.get_parameter_shapes())
Paul's avatar
Paul committed
183
184
185
186
            std::cout << param.first << ": " << param.second << std::endl;
    }
};

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

    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
224
225
226
struct compile : command<compile>
{
    compiler c;
Paul's avatar
Paul committed
227
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
228
229
230
231
232
233
234
235
236
237
238
239

    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
240
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
241
242
243
244
245
246
247

    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
248
        p.eval(m);
Paul's avatar
Paul committed
249
250
251
252
        std::cout << p << std::endl;
    }
};

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

    void run() {}
};

Paul's avatar
Paul committed
292
293
294
295
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

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