main.cpp 2.19 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include "argument_parser.hpp"
#include "command.hpp"

Paul's avatar
Paul committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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;
    bool is_nhwc = false;

    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
24
25
        ap.add(
            is_nhwc, {"--nchw"}, ap.help("Treat tensorflow format as nchw"), ap.set_value(false));
Paul's avatar
Paul committed
26
27
    }

Paul's avatar
Paul committed
28
    program load()
Paul's avatar
Paul committed
29
30
    {
        program p;
Paul's avatar
Paul committed
31
        if(type.empty())
Paul's avatar
Paul committed
32
        {
Paul's avatar
Paul committed
33
            if(ends_with(file, ".onnx"))
Paul's avatar
Paul committed
34
35
36
37
                type = "onnx";
            else
                type = "tf";
        }
Paul's avatar
Paul committed
38
        if(type == "onnx")
Paul's avatar
Paul committed
39
            p = parse_onnx(file);
Paul's avatar
Paul committed
40
        else if(type == "tf")
Paul's avatar
Paul committed
41
42
43
44
45
46
47
48
            p = parse_tf(file, is_nhwc);
        return p;
    }
};

struct read : command<read>
{
    loader l;
Paul's avatar
Paul committed
49
    void parse(argument_parser& ap) { l.parse(ap); }
Paul's avatar
Paul committed
50
51
52
53
54
55
56
57

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

Paul's avatar
Paul committed
58
59
60
61
62
struct main_command
{
    static std::string get_command_help()
    {
        std::string result = "Commands:\n";
Paul's avatar
Paul committed
63
        for(const auto& p : get_commands())
Paul's avatar
Paul committed
64
65
66
            result += "    " + p.first + "\n";
        return result;
    }
Paul's avatar
Paul committed
67
    void parse(argument_parser& ap)
Paul's avatar
Paul committed
68
69
70
71
72
73
74
    {
        ap.add(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
    }

    void run() {}
};

Paul's avatar
Paul committed
75
76
77
78
79
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

using namespace migraphx::driver;
Paul's avatar
Paul committed
80
81
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
82
    std::vector<std::string> args(argv + 1, argv + argc);
Paul's avatar
Paul committed
83
    if(args.empty())
Paul's avatar
Paul committed
84
        return 0;
Paul's avatar
Paul committed
85
    auto&& m = get_commands();
Paul's avatar
Paul committed
86
    auto cmd = args.front();
Paul's avatar
Paul committed
87
    if(m.count(cmd) > 0)
Paul's avatar
Paul committed
88
    {
Paul's avatar
Paul committed
89
        m.at(cmd)({args.begin() + 1, args.end()});
Paul's avatar
Paul committed
90
    }
Paul's avatar
Paul committed
91
    else
Paul's avatar
Paul committed
92
    {
Paul's avatar
Paul committed
93
        run_command<main_command>(args);
Paul's avatar
Paul committed
94
95
96
    }
    return 0;
}