main.cpp 7.48 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
#include <migraphx/quantization.hpp>
11
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
12
#include <migraphx/generate.hpp>
13
14
15
16
17
18
19
#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
20
21
22
23
24
25
26
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

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

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

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

Paul's avatar
Paul committed
81
82
struct compiler
{
83
84
    static const int q_fp16 = 1;
    static const int q_int8 = 2;
Paul's avatar
Paul committed
85
    loader l;
86
87
88
    bool gpu     = true;
    int quantize = 0;

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

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

    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
124
125
};

Paul's avatar
Paul committed
126
127
128
struct read : command<read>
{
    loader l;
Paul's avatar
Paul committed
129
    void parse(argument_parser& ap) { l.parse(ap); }
Paul's avatar
Paul committed
130
131
132
133
134
135
136
137

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

Paul's avatar
Paul committed
138
139
140
141
142
143
144
145
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
146
        for(auto&& param : p.get_parameter_shapes())
Paul's avatar
Paul committed
147
148
149
150
            std::cout << param.first << ": " << param.second << std::endl;
    }
};

Paul's avatar
Paul committed
151
152
153
struct verify : command<verify>
{
    loader l;
Paul's avatar
Paul committed
154
    double tolerance     = 80;
Paul's avatar
Paul committed
155
    bool per_instruction = false;
Paul's avatar
Paul committed
156
157
158
    bool reduce          = false;
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
159
        l.parse(ap);
Paul's avatar
Paul committed
160
161
        ap(tolerance, {"--tolerance"}, ap.help("Tolerance for errors"));
        ap(per_instruction,
Paul's avatar
Paul committed
162
163
164
165
           {"-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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    }

    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
188
189
190
struct compile : command<compile>
{
    compiler c;
Paul's avatar
Paul committed
191
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
192
193
194
195
196
197
198
199
200
201
202
203

    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
204
    void parse(argument_parser& ap) { c.parse(ap); }
Paul's avatar
Paul committed
205
206
207
208
209
210
211

    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
212
        p.eval(m);
Paul's avatar
Paul committed
213
214
215
216
        std::cout << p << std::endl;
    }
};

Paul's avatar
Paul committed
217
218
219
220
struct perf : command<perf>
{
    compiler c;
    unsigned n = 100;
Paul's avatar
Paul committed
221
222
    void parse(argument_parser& ap)
    {
Paul's avatar
Paul committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        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
238
239
240
241
242
struct main_command
{
    static std::string get_command_help()
    {
        std::string result = "Commands:\n";
Paul's avatar
Paul committed
243
244
245
246
        return std::accumulate(get_commands().begin(),
                               get_commands().end(),
                               result,
                               [](auto r, auto&& p) { return r + "    " + p.first + "\n"; });
Paul's avatar
Paul committed
247
    }
Paul's avatar
Paul committed
248
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
249
    {
Paul's avatar
Paul committed
250
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
Paul's avatar
Paul committed
251
252
253
254
255
    }

    void run() {}
};

Paul's avatar
Paul committed
256
257
258
259
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
260
using namespace migraphx::driver; // NOLINT
Paul's avatar
Paul committed
261
262
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
263
    std::vector<std::string> args(argv + 1, argv + argc);
Paul's avatar
Paul committed
264
    if(args.empty())
Paul's avatar
Paul committed
265
        return 0;
Paul's avatar
Paul committed
266
    auto&& m = get_commands();
Paul's avatar
Paul committed
267
    auto cmd = args.front();
Paul's avatar
Paul committed
268
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
269
    {
Paul's avatar
Paul committed
270
        m.at(cmd)({args.begin() + 1, args.end()});
Paul's avatar
Paul committed
271
    }
Paul's avatar
Paul committed
272
    else
Paul's avatar
Paul committed
273
    {
Paul's avatar
Paul committed
274
        run_command<main_command>(args);
Paul's avatar
Paul committed
275
276
277
    }
    return 0;
}