verify.cpp 3.97 KB
Newer Older
Paul's avatar
Paul committed
1
#include "verify.hpp"
2
#include "perf.hpp"
Paul's avatar
Paul committed
3
4
5
6
7

#include <migraphx/cpu/target.hpp>
#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
14
15
16
17
18

#ifdef HAVE_GPU
#include <migraphx/gpu/target.hpp>
#include <migraphx/gpu/hip.hpp>
#endif

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

19
std::vector<argument> run_cpu(program p, const program::parameter_map& inputs)
Paul's avatar
Paul committed
20
21
{
    p.compile(cpu::target{});
22
    auto out = p.eval(inputs);
Paul's avatar
Paul committed
23
24
25
26
    std::cout << p << std::endl;
    return out;
}

27
28
std::vector<argument>
run_gpu(program p, const compile_options& options, const program::parameter_map& inputs)
Paul's avatar
Paul committed
29
30
{
#ifdef HAVE_GPU
31
    p.compile(gpu::target{}, options);
Paul's avatar
Paul committed
32
33
34
35

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

Paul's avatar
Paul committed
47
48
#else
    (void)p;
49
    (void)options;
50
    (void)inputs;
Paul's avatar
Paul committed
51
52
53
54
    MIGRAPHX_THROW("Gpu unsupported!");
#endif
}

55
56
57
void verify_program(const std::string& name,
                    const program& p,
                    compile_options options,
58
                    const program::parameter_map& inputs,
59
                    double tolerance)
Paul's avatar
Paul committed
60
{
61
62
    auto x = run_cpu(p, inputs);
    auto y = run_gpu(p, options, inputs);
63
64
65
66
67
68

    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
69
70
71
72
    // std::cout << "cpu: " << x << std::endl;
    // std::cout << "gpu: " << y << std::endl;
}

73
void verify_instructions(const program& prog, compile_options options, double tolerance)
Paul's avatar
Paul committed
74
75
76
77
78
79
80
81
82
83
84
{
    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
85
86
        if(ins.name() == "undefined")
            continue;
Paul's avatar
Paul committed
87
88
89
90
91
92
93
        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
94
                inputs.push_back(p.add_parameter(std::to_string(inputs.size()), arg->get_shape()));
Paul's avatar
Paul committed
95
96
97
98
99
100
        }
        p.add_instruction(ins.get_operator(), inputs);
        try
        {
            std::cout << "Verify: " << ins.name() << std::endl;
            std::cout << p << std::endl;
101
            verify_program(ins.name(), p, options, create_param_map(p, false), tolerance);
Paul's avatar
Paul committed
102
103
104
105
106
107
108
109
110
        }
        catch(...)
        {
            std::cout << "Instruction " << ins.name() << " threw an exception." << std::endl;
            throw;
        }
    }
}

111
112
113
114
115
void verify_reduced(program p,
                    int n,
                    compile_options options,
                    const program::parameter_map& inputs,
                    double tolerance)
Paul's avatar
Paul committed
116
{
Paul's avatar
Paul committed
117
    auto last = std::prev(p.end(), n + 1);
Paul's avatar
Paul committed
118
119
120
    p.remove_instructions(last, p.end());
    std::cout << "Verify: " << std::endl;
    std::cout << p << std::endl;
121
    verify_program(std::to_string(n), p, options, inputs, tolerance);
Paul's avatar
Paul committed
122
123
}

124
125
126
127
void verify_reduced_program(const program& p,
                            compile_options options,
                            const program::parameter_map& inputs,
                            double tolerance)
Paul's avatar
Paul committed
128
{
Paul's avatar
Paul committed
129
    auto n = std::distance(p.begin(), p.end());
Paul's avatar
Paul committed
130
131
    for(std::size_t i = 0; i < n; i++)
    {
132
        verify_reduced(p, i, options, inputs, tolerance);
Paul's avatar
Paul committed
133
134
135
136
137
138
    }
}

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