verify_onnx.cpp 4.04 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
#include <migraphx/onnx.hpp>
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
5
6
7
8
9
#include <migraphx/cpu/target.hpp>
#include <migraphx/gpu/target.hpp>
#include <migraphx/gpu/hip.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/verify_args.hpp>
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
10

11
12
template <class T>
auto get_hash(const T& x)
Paul's avatar
Paul committed
13
{
14
15
16
    return std::hash<T>{}(x);
}

Paul's avatar
Paul committed
17
template <class F>
Paul's avatar
Paul committed
18
migraphx::argument run_cpu(F f)
19
20
{
    auto p = f();
Paul's avatar
Paul committed
21
22
    p.compile(migraphx::cpu::target{});
    migraphx::program::parameter_map m;
Paul's avatar
Paul committed
23
24
    for(auto&& x : p.get_parameter_shapes())
    {
Paul's avatar
Paul committed
25
        m[x.first] = migraphx::generate_argument(x.second, get_hash(x.first));
Paul's avatar
Paul committed
26
27
    }
    auto out = p.eval(m);
Paul's avatar
Paul committed
28
29
30
31
    std::cout << p << std::endl;
    return out;
}

Paul's avatar
Paul committed
32
template <class F>
Paul's avatar
Paul committed
33
migraphx::argument run_gpu(F f)
Paul's avatar
Paul committed
34
{
35
    auto p = f();
Paul's avatar
Paul committed
36
    p.compile(migraphx::gpu::target{});
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
    migraphx::program::parameter_map m;
Paul's avatar
Paul committed
39
40
    for(auto&& x : p.get_parameter_shapes())
    {
Paul's avatar
Paul committed
41
42
        m[x.first] =
            migraphx::gpu::to_gpu(migraphx::generate_argument(x.second, get_hash(x.first)));
Paul's avatar
Paul committed
43
    }
Paul's avatar
Paul committed
44
    auto out = migraphx::gpu::from_gpu(p.eval(m));
Paul's avatar
Paul committed
45
    std::cout << p << std::endl;
Paul's avatar
Paul committed
46
    return migraphx::gpu::from_gpu(out);
Paul's avatar
Paul committed
47
48
}

Paul's avatar
Paul committed
49
50
template <class F>
void verify_program(const std::string& name, F f, double tolerance = 100)
51
52
53
{
    auto x = run_cpu(f);
    auto y = run_gpu(f);
Paul's avatar
Paul committed
54
    migraphx::verify_args(name, x, y, tolerance);
Paul's avatar
Paul committed
55
56
    // std::cout << "cpu: " << x << std::endl;
    // std::cout << "gpu: " << y << std::endl;
57
58
}

Paul's avatar
Paul committed
59
void verify_instructions(const migraphx::program& prog, double tolerance = 80)
60
{
Paul's avatar
Paul committed
61
    for(auto&& ins : prog)
62
    {
Paul's avatar
Paul committed
63
        if(ins.name().front() == '@')
64
            continue;
Paul's avatar
Paul committed
65
        if(ins.name() == "broadcast")
66
            continue;
Paul's avatar
Paul committed
67
        if(ins.name() == "transpose")
68
            continue;
Paul's avatar
Paul committed
69
        if(ins.name() == "reshape")
70
71
            continue;
        auto create_program = [&] {
Paul's avatar
Paul committed
72
73
            migraphx::program p;
            std::vector<migraphx::instruction_ref> inputs;
Paul's avatar
Paul committed
74
            for(auto&& arg : ins.inputs())
75
            {
Paul's avatar
Paul committed
76
                if(arg->name() == "@literal")
Paul's avatar
Paul committed
77
                    inputs.push_back(p.add_literal(arg->get_literal()));
78
                else
Paul's avatar
Paul committed
79
80
                    inputs.push_back(
                        p.add_parameter(std::to_string(inputs.size()), arg->get_shape()));
81
            }
82
            p.add_instruction(ins.get_operator(), inputs);
83
84
            return p;
        };
Paul's avatar
Paul committed
85
        try
86
        {
Paul's avatar
Paul committed
87
            std::cout << "Verify: " << ins.name() << std::endl;
88
            std::cout << create_program() << std::endl;
Paul's avatar
Paul committed
89
            verify_program(ins.name(), create_program, tolerance);
90
        }
Paul's avatar
Paul committed
91
        catch(...)
92
        {
Paul's avatar
Paul committed
93
            std::cout << "Instruction " << ins.name() << " threw an exception." << std::endl;
94
95
96
97
98
            throw;
        }
    }
}

Paul's avatar
Paul committed
99
template <class F>
Paul's avatar
Paul committed
100
101
void verify_reduced(F f, int n, double tolerance = 80)
{
Paul's avatar
Paul committed
102

Paul's avatar
Paul committed
103
    auto create_program = [&] {
Paul's avatar
Paul committed
104
        migraphx::program p = f();
Paul's avatar
Paul committed
105
        auto last           = std::prev(p.end(), n + 1);
Paul's avatar
Paul committed
106
107
108
109
110
111
112
113
        p.remove_instructions(last, p.end());
        return p;
    };
    std::cout << "Verify: " << std::endl;
    std::cout << create_program() << std::endl;
    verify_program(std::to_string(n), create_program, tolerance);
}

Paul's avatar
Paul committed
114
template <class F>
Paul's avatar
Paul committed
115
116
void verify_reduced_program(F f, double tolerance = 80)
{
Paul's avatar
Paul committed
117
    migraphx::program p = f();
Paul's avatar
Paul committed
118
    auto n              = std::distance(p.begin(), p.end());
Paul's avatar
Paul committed
119
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
120
121
122
123
124
    {
        verify_reduced(f, i, tolerance);
    }
}

Paul's avatar
Paul committed
125
126
int main(int argc, char const* argv[])
{
Paul's avatar
Paul committed
127
    std::vector<std::string> args(argv + 1, argv + argc);
128
    if(not args.empty())
Paul's avatar
Paul committed
129
    {
130
        std::string file = args.front();
Paul's avatar
Paul committed
131
        auto p           = migraphx::parse_onnx(file);
Paul's avatar
Paul committed
132
133
        std::cout << p << std::endl;

134
135
136
137
        if(std::any_of(args.begin(), args.end(), [](const auto& s) { return s == "-i"; }))
        {
            verify_instructions(p);
        }
Paul's avatar
Paul committed
138
139
        else if(std::any_of(args.begin(), args.end(), [](const auto& s) { return s == "-r"; }))
        {
Paul's avatar
Paul committed
140
            verify_reduced_program([&] { return migraphx::parse_onnx(file); });
Paul's avatar
Paul committed
141
        }
142
143
        else
        {
Paul's avatar
Paul committed
144
            verify_program(file, [&] { return migraphx::parse_onnx(file); });
145
        }
Paul's avatar
Paul committed
146
147
    }
}