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

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

Paul's avatar
Paul committed
4
#include <migraph/cpu/cpu_target.hpp>
Paul's avatar
Paul committed
5
6
#include <migraph/gpu/target.hpp>
#include <migraph/gpu/hip.hpp>
Paul's avatar
Paul committed
7
#include <migraph/generate.hpp>
Paul's avatar
Paul committed
8
#include <migraph/verify.hpp>
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
migraph::argument run_cpu(const std::string& file)
Paul's avatar
Paul committed
11
{
Paul's avatar
Paul committed
12
13
    auto p = migraph::parse_onnx(file);
    p.compile(migraph::cpu::cpu_target{});
Paul's avatar
Paul committed
14
15
16
17
18
19
    migraph::program::parameter_map m;
    for(auto&& x : p.get_parameter_shapes())
    {
        m[x.first] = migraph::generate_argument(x.second);
    }
    auto out = p.eval(m);
Paul's avatar
Paul committed
20
21
22
23
    std::cout << p << std::endl;
    return out;
}

Paul's avatar
Paul committed
24
migraph::argument run_gpu(const std::string& file)
Paul's avatar
Paul committed
25
{
Paul's avatar
Paul committed
26
    auto p = migraph::parse_onnx(file);
Paul's avatar
Paul committed
27
    p.compile(migraph::gpu::target{});
Paul's avatar
Paul committed
28

Paul's avatar
Paul committed
29
30
31
32
33
34
    migraph::program::parameter_map m;
    for(auto&& x : p.get_parameter_shapes())
    {
        m[x.first] = migraph::gpu::to_gpu(migraph::generate_argument(x.second));
    }
    auto out = migraph::gpu::from_gpu(p.eval(m));
Paul's avatar
Paul committed
35
    std::cout << p << std::endl;
Paul's avatar
Paul committed
36
    return migraph::gpu::from_gpu(out);
Paul's avatar
Paul committed
37
38
39
40
41
42
43
}

int main(int argc, char const* argv[])
{
    if(argc > 1)
    {
        std::string file = argv[1];
Paul's avatar
Paul committed
44
        auto p           = migraph::parse_onnx(file);
Paul's avatar
Paul committed
45
46
        std::cout << p << std::endl;

Paul's avatar
Paul committed
47
48
        auto x = run_cpu(file);
        auto y = run_gpu(file);
Paul's avatar
Paul committed
49
        visit_all(x, y)([](auto cpu, auto gpu) {
Paul's avatar
Paul committed
50
            if(migraph::verify_range(cpu, gpu))
Paul's avatar
Paul committed
51
52
53
54
55
56
            {
                std::cout << "Passed" << std::endl;
            }
            else
            {
                std::cout << "Not equal" << std::endl;
Paul's avatar
Paul committed
57
                std::cout << "cpu:" << std::endl;
Paul's avatar
Paul committed
58
                std::cout << cpu << std::endl;
Paul's avatar
Paul committed
59
                std::cout << "gpu:" << std::endl;
Paul's avatar
Paul committed
60
61
62
63
                std::cout << gpu << std::endl;
            }

        });
Paul's avatar
Paul committed
64
65
    }
}