verify.cpp 4.54 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>
kahmed10's avatar
kahmed10 committed
9
#include <migraphx/quantization.hpp>
Paul's avatar
Paul committed
10
11
12
13
14

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

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

kahmed10's avatar
kahmed10 committed
23
24
25
26
27
std::vector<argument> run_target(program p,
                                 const target& t,
                                 const compile_options& options,
                                 precision quantize,
                                 const parameter_map& inputs)
Paul's avatar
Paul committed
28
{
kahmed10's avatar
kahmed10 committed
29
30
31
32
    if(quantize == precision::fp16)
    {
        quantize_fp16(p);
    }
33
    p.compile(t, options);
Paul's avatar
Paul committed
34

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

50
51
void verify_program(const std::string& name,
                    const program& p,
52
                    const target& t,
53
                    compile_options options,
kahmed10's avatar
kahmed10 committed
54
                    precision quantize,
55
                    const parameter_map& inputs,
56
                    double tolerance)
Paul's avatar
Paul committed
57
{
58
    auto x = run_ref(p, inputs);
kahmed10's avatar
kahmed10 committed
59
    auto y = run_target(p, t, options, quantize, inputs);
60
61
62
63
64
65

    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
66
67
}

68
69
70
void verify_instructions(const program& prog,
                         const target& t,
                         compile_options options,
kahmed10's avatar
kahmed10 committed
71
                         precision quantize,
72
                         double tolerance)
Paul's avatar
Paul committed
73
{
74
75
    const auto* mm_prog = prog.get_main_module();
    for(auto&& ins : (*mm_prog))
Paul's avatar
Paul committed
76
77
78
79
80
81
82
83
84
    {
        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
85
86
        if(ins.name() == "undefined")
            continue;
Paul's avatar
Paul committed
87
        program p;
88
        auto* mm_p = p.get_main_module();
Paul's avatar
Paul committed
89
90
91
92
        std::vector<instruction_ref> inputs;
        for(auto&& arg : ins.inputs())
        {
            if(arg->name() == "@literal")
93
                inputs.push_back(mm_p->add_literal(arg->get_literal()));
Paul's avatar
Paul committed
94
            else
95
96
                inputs.push_back(
                    mm_p->add_parameter(std::to_string(inputs.size()), arg->get_shape()));
Paul's avatar
Paul committed
97
        }
98
        mm_p->add_instruction(ins.get_operator(), inputs);
Paul's avatar
Paul committed
99
100
101
102
        try
        {
            std::cout << "Verify: " << ins.name() << std::endl;
            std::cout << p << std::endl;
kahmed10's avatar
kahmed10 committed
103
104
            verify_program(
                ins.name(), p, t, options, quantize, create_param_map(p, false), tolerance);
Paul's avatar
Paul committed
105
106
107
108
109
110
111
112
113
        }
        catch(...)
        {
            std::cout << "Instruction " << ins.name() << " threw an exception." << std::endl;
            throw;
        }
    }
}

114
115
void verify_reduced(program p,
                    int n,
116
                    const target& t,
117
                    compile_options options,
kahmed10's avatar
kahmed10 committed
118
                    precision quantize,
119
                    const parameter_map& inputs,
120
                    double tolerance)
Paul's avatar
Paul committed
121
{
122
    auto* mm  = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
123
124
    auto last = std::prev(mm->end(), n + 1);
    mm->remove_instructions(last, mm->end());
Paul's avatar
Paul committed
125
126
    std::cout << "Verify: " << std::endl;
    std::cout << p << std::endl;
kahmed10's avatar
kahmed10 committed
127
    verify_program(std::to_string(n), p, t, options, quantize, inputs, tolerance);
Paul's avatar
Paul committed
128
129
}

130
void verify_reduced_program(const program& p,
131
                            const target& t,
132
                            compile_options options,
kahmed10's avatar
kahmed10 committed
133
                            precision quantize,
134
                            const parameter_map& inputs,
135
                            double tolerance)
Paul's avatar
Paul committed
136
{
Shucai Xiao's avatar
Shucai Xiao committed
137
138
    const auto* mm = p.get_main_module();
    auto n         = std::distance(mm->begin(), mm->end());
Paul's avatar
Paul committed
139
140
    for(std::size_t i = 0; i < n; i++)
    {
kahmed10's avatar
kahmed10 committed
141
        verify_reduced(p, i, t, options, quantize, inputs, tolerance);
Paul's avatar
Paul committed
142
143
144
145
146
147
    }
}

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