verify.cpp 5.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
#include "verify.hpp"
25
#include "perf.hpp"
Paul's avatar
Paul committed
26

27
#include <migraphx/ref/target.hpp>
Paul's avatar
Paul committed
28
29
30
#include <migraphx/generate.hpp>
#include <migraphx/verify_args.hpp>
#include <migraphx/instruction.hpp>
31
#include <migraphx/compile_options.hpp>
kahmed10's avatar
kahmed10 committed
32
#include <migraphx/quantization.hpp>
Paul's avatar
Paul committed
33
34
35
36
37

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

38
std::vector<argument> run_ref(program p, const parameter_map& inputs)
Paul's avatar
Paul committed
39
{
40
    p.compile(ref::target{});
41
    auto out = p.eval(inputs);
Paul's avatar
Paul committed
42
43
44
45
    std::cout << p << std::endl;
    return out;
}

kahmed10's avatar
kahmed10 committed
46
47
48
49
50
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
51
{
kahmed10's avatar
kahmed10 committed
52
53
54
55
    if(quantize == precision::fp16)
    {
        quantize_fp16(p);
    }
56
    p.compile(t, options);
Paul's avatar
Paul committed
57

58
    parameter_map m;
Paul's avatar
Paul committed
59
60
    for(auto&& x : p.get_parameter_shapes())
    {
61
        auto arg   = inputs.count(x.first) == 0 ? generate_argument(x.second) : inputs.at(x.first);
62
        m[x.first] = options.offload_copy ? arg : t.copy_to(arg);
Paul's avatar
Paul committed
63
    }
64
65
    auto gpu_out = p.eval(m);
    std::vector<argument> output(gpu_out.size());
Paul's avatar
Paul committed
66
    std::cout << p << std::endl;
67
    std::transform(gpu_out.begin(), gpu_out.end(), output.begin(), [&](auto& argu) {
68
        return options.offload_copy ? argu : t.copy_from(argu);
69
70
    });
    return output;
Paul's avatar
Paul committed
71
72
}

Paul's avatar
Paul committed
73
bool verify_program(const std::string& name,
74
                    const program& p,
75
                    const target& t,
76
                    compile_options options,
kahmed10's avatar
kahmed10 committed
77
                    precision quantize,
78
                    const parameter_map& inputs,
79
                    double tolerance)
Paul's avatar
Paul committed
80
{
81
    auto x = run_ref(p, inputs);
kahmed10's avatar
kahmed10 committed
82
    auto y = run_target(p, t, options, quantize, inputs);
83

Paul's avatar
Paul committed
84
    bool passed = true;
85
86
87
    std::size_t output_num = x.size();
    for(std::size_t i = 0; i < output_num; ++i)
    {
Paul's avatar
Paul committed
88
        passed &= verify_args(name, x[i], y[i], tolerance);
89
    }
Paul's avatar
Paul committed
90
    return passed;
Paul's avatar
Paul committed
91
92
}

93
94
95
void verify_instructions(const program& prog,
                         const target& t,
                         compile_options options,
kahmed10's avatar
kahmed10 committed
96
                         precision quantize,
97
                         double tolerance)
Paul's avatar
Paul committed
98
{
99
100
    const auto* mm_prog = prog.get_main_module();
    for(auto&& ins : (*mm_prog))
Paul's avatar
Paul committed
101
102
103
104
105
106
107
108
109
    {
        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
110
111
        if(ins.name() == "undefined")
            continue;
Paul's avatar
Paul committed
112
        program p;
113
        auto* mm_p = p.get_main_module();
Paul's avatar
Paul committed
114
115
116
117
        std::vector<instruction_ref> inputs;
        for(auto&& arg : ins.inputs())
        {
            if(arg->name() == "@literal")
118
                inputs.push_back(mm_p->add_literal(arg->get_literal()));
Paul's avatar
Paul committed
119
            else
120
121
                inputs.push_back(
                    mm_p->add_parameter(std::to_string(inputs.size()), arg->get_shape()));
Paul's avatar
Paul committed
122
        }
123
        mm_p->add_instruction(ins.get_operator(), inputs);
Paul's avatar
Paul committed
124
125
126
127
        try
        {
            std::cout << "Verify: " << ins.name() << std::endl;
            std::cout << p << std::endl;
kahmed10's avatar
kahmed10 committed
128
129
            verify_program(
                ins.name(), p, t, options, quantize, create_param_map(p, false), tolerance);
Paul's avatar
Paul committed
130
131
132
133
134
135
136
137
138
        }
        catch(...)
        {
            std::cout << "Instruction " << ins.name() << " threw an exception." << std::endl;
            throw;
        }
    }
}

139
140
void verify_reduced(program p,
                    int n,
141
                    const target& t,
142
                    compile_options options,
kahmed10's avatar
kahmed10 committed
143
                    precision quantize,
144
                    const parameter_map& inputs,
145
                    double tolerance)
Paul's avatar
Paul committed
146
{
147
    auto* mm  = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
148
149
    auto last = std::prev(mm->end(), n + 1);
    mm->remove_instructions(last, mm->end());
Paul's avatar
Paul committed
150
151
    std::cout << "Verify: " << std::endl;
    std::cout << p << std::endl;
kahmed10's avatar
kahmed10 committed
152
    verify_program(std::to_string(n), p, t, options, quantize, inputs, tolerance);
Paul's avatar
Paul committed
153
154
}

155
void verify_reduced_program(const program& p,
156
                            const target& t,
157
                            compile_options options,
kahmed10's avatar
kahmed10 committed
158
                            precision quantize,
159
                            const parameter_map& inputs,
160
                            double tolerance)
Paul's avatar
Paul committed
161
{
Shucai Xiao's avatar
Shucai Xiao committed
162
163
    const auto* mm = p.get_main_module();
    auto n         = std::distance(mm->begin(), mm->end());
Paul's avatar
Paul committed
164
165
    for(std::size_t i = 0; i < n; i++)
    {
kahmed10's avatar
kahmed10 committed
166
        verify_reduced(p, i, t, options, quantize, inputs, tolerance);
Paul's avatar
Paul committed
167
168
169
170
171
172
    }
}

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