verify.cpp 4.08 KB
Newer Older
Paul's avatar
Paul committed
1
#include "verify.hpp"
2
#include "perf.hpp"
Paul's avatar
Paul committed
3

4
#include <migraphx/ref/target.hpp>
Paul's avatar
Paul committed
5
6
7
#include <migraphx/generate.hpp>
#include <migraphx/verify_args.hpp>
#include <migraphx/instruction.hpp>
8
#include <migraphx/compile_options.hpp>
Paul's avatar
Paul committed
9
10
11
12
13

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

14
std::vector<argument> run_ref(program p, const program::parameter_map& inputs)
Paul's avatar
Paul committed
15
{
16
    p.compile(ref::target{});
17
    auto out = p.eval(inputs);
Paul's avatar
Paul committed
18
19
20
21
    std::cout << p << std::endl;
    return out;
}

22
23
24
25
std::vector<argument> run_target(program p,
                                 const target& t,
                                 const compile_options& options,
                                 const program::parameter_map& inputs)
Paul's avatar
Paul committed
26
{
27
    p.compile(t, options);
Paul's avatar
Paul committed
28
29
30
31

    program::parameter_map m;
    for(auto&& x : p.get_parameter_shapes())
    {
32
        auto arg   = inputs.count(x.first) == 0 ? generate_argument(x.second) : inputs.at(x.first);
33
        m[x.first] = options.offload_copy ? arg : t.copy_to(arg);
Paul's avatar
Paul committed
34
    }
35
36
    auto gpu_out = p.eval(m);
    std::vector<argument> output(gpu_out.size());
Paul's avatar
Paul committed
37
    std::cout << p << std::endl;
38
    std::transform(gpu_out.begin(), gpu_out.end(), output.begin(), [&](auto& argu) {
39
        return options.offload_copy ? argu : t.copy_from(argu);
40
41
    });
    return output;
Paul's avatar
Paul committed
42
43
}

44
45
void verify_program(const std::string& name,
                    const program& p,
46
                    const target& t,
47
                    compile_options options,
48
                    const program::parameter_map& inputs,
49
                    double tolerance)
Paul's avatar
Paul committed
50
{
51
52
    auto x = run_ref(p, inputs);
    auto y = run_target(p, t, options, inputs);
53
54
55
56
57
58

    std::size_t output_num = x.size();
    for(std::size_t i = 0; i < output_num; ++i)
    {
        verify_args(name, x[i], y[i], tolerance);
    }
Paul's avatar
Paul committed
59
60
61
62
    // std::cout << "cpu: " << x << std::endl;
    // std::cout << "gpu: " << y << std::endl;
}

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

104
105
void verify_reduced(program p,
                    int n,
106
                    const target& t,
107
108
109
                    compile_options options,
                    const program::parameter_map& inputs,
                    double tolerance)
Paul's avatar
Paul committed
110
{
Paul's avatar
Paul committed
111
    auto last = std::prev(p.end(), n + 1);
Paul's avatar
Paul committed
112
113
114
    p.remove_instructions(last, p.end());
    std::cout << "Verify: " << std::endl;
    std::cout << p << std::endl;
115
    verify_program(std::to_string(n), p, t, options, inputs, tolerance);
Paul's avatar
Paul committed
116
117
}

118
void verify_reduced_program(const program& p,
119
                            const target& t,
120
121
122
                            compile_options options,
                            const program::parameter_map& inputs,
                            double tolerance)
Paul's avatar
Paul committed
123
{
Paul's avatar
Paul committed
124
    auto n = std::distance(p.begin(), p.end());
Paul's avatar
Paul committed
125
126
    for(std::size_t i = 0; i < n; i++)
    {
127
        verify_reduced(p, i, t, options, inputs, tolerance);
Paul's avatar
Paul committed
128
129
130
131
132
133
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx