verify.cpp 7.68 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/register_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>
33
#include <migraphx/ranges.hpp>
Paul's avatar
Paul committed
34
35
36
37
38

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * Gives tolerances based on user input (`rms_tol`, `atol`, `rtol` parameters) and defaults.
 * Sets to fp16 tolerances if `quantize` input is fp16 or any fp16 instruction in found in the
 * model.
 */
verify::tolerance get_tolerances(const program& p,
                                 precision quantize,
                                 std::optional<double> rms_tol,
                                 std::optional<double> atol,
                                 std::optional<double> rtol)
{
    bool has_fp16 = any_of(p.get_modules(), [](auto&& m) {
        return any_of(*m, [](auto&& ins) { return (ins.get_shape().type() == shape::half_type); });
    });
    migraphx::verify::tolerance result{};
    if(has_fp16 or quantize == precision::fp16)
    {
        result.rms_tol = 8e-2;
        result.atol    = 4e-2;
        result.rtol    = 4e-2;
    }
    if(rms_tol)
    {
        result.rms_tol = *rms_tol;
    }
    if(atol)
    {
        result.atol = *atol;
    }
    if(rtol)
    {
        result.rtol = *rtol;
    }
    return result;
}

75
std::vector<argument> run_ref(program p, const parameter_map& inputs)
Paul's avatar
Paul committed
76
{
77
    p.compile(migraphx::make_target("ref"));
78
    auto out = p.eval(inputs);
Paul's avatar
Paul committed
79
80
81
82
    std::cout << p << std::endl;
    return out;
}

kahmed10's avatar
kahmed10 committed
83
84
85
86
87
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
88
{
kahmed10's avatar
kahmed10 committed
89
90
91
92
    if(quantize == precision::fp16)
    {
        quantize_fp16(p);
    }
93
    p.compile(t, options);
Paul's avatar
Paul committed
94

95
    parameter_map m;
Paul's avatar
Paul committed
96
97
    for(auto&& x : p.get_parameter_shapes())
    {
98
        auto arg   = inputs.count(x.first) == 0 ? generate_argument(x.second) : inputs.at(x.first);
99
        m[x.first] = options.offload_copy ? arg : t.copy_to(arg);
Paul's avatar
Paul committed
100
    }
101
102
    auto gpu_out = p.eval(m);
    std::vector<argument> output(gpu_out.size());
Paul's avatar
Paul committed
103
    std::cout << p << std::endl;
104
    std::transform(gpu_out.begin(), gpu_out.end(), output.begin(), [&](auto& argu) {
105
        return options.offload_copy ? argu : t.copy_from(argu);
106
107
    });
    return output;
Paul's avatar
Paul committed
108
109
}

110
111
void verify_program(const std::string& name,
                    const program& p,
112
                    const target& t,
113
                    compile_options options,
kahmed10's avatar
kahmed10 committed
114
                    precision quantize,
115
                    const parameter_map& inputs,
116
                    verify::tolerance tols)
Paul's avatar
Paul committed
117
{
118
119
    auto ref_outs    = run_ref(p, inputs);
    auto target_outs = run_target(p, t, options, quantize, inputs);
120

121
    std::size_t output_num = ref_outs.size();
122
123
    for(std::size_t i = 0; i < output_num; ++i)
    {
124
125
        if(ref_outs[i].get_shape().type() != target_outs[i].get_shape().type() or
           ref_outs[i].get_shape().lens() != target_outs[i].get_shape().lens())
126
127
        {
            std::cout << "FAILED: " << name << std::endl;
128
129
            std::cout << "Shape mismatch {" << ref_outs[i].get_shape() << "} != {"
                      << target_outs[i].get_shape() << "}" << std::endl;
130
131
132
        }
        else
        {
133
            verify_args(name, target_outs[i], verify::expected{ref_outs[i]}, tols);
134
        }
135
    }
Paul's avatar
Paul committed
136
137
}

138
139
140
void verify_instructions(const program& prog,
                         const target& t,
                         compile_options options,
kahmed10's avatar
kahmed10 committed
141
                         precision quantize,
142
                         verify::tolerance tols)
Paul's avatar
Paul committed
143
{
144
145
    const auto* mm_prog = prog.get_main_module();
    for(auto&& ins : (*mm_prog))
Paul's avatar
Paul committed
146
147
148
149
150
151
152
153
154
    {
        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
155
156
        if(ins.name() == "undefined")
            continue;
Paul's avatar
Paul committed
157
        program p;
158
        auto* mm_p = p.get_main_module();
Paul's avatar
Paul committed
159
160
161
162
        std::vector<instruction_ref> inputs;
        for(auto&& arg : ins.inputs())
        {
            if(arg->name() == "@literal")
163
                inputs.push_back(mm_p->add_literal(arg->get_literal()));
Paul's avatar
Paul committed
164
            else
165
166
                inputs.push_back(
                    mm_p->add_parameter(std::to_string(inputs.size()), arg->get_shape()));
Paul's avatar
Paul committed
167
        }
168
        mm_p->add_instruction(ins.get_operator(), inputs);
Paul's avatar
Paul committed
169
170
171
172
        try
        {
            std::cout << "Verify: " << ins.name() << std::endl;
            std::cout << p << std::endl;
173
            verify_program(ins.name(), p, t, options, quantize, create_param_map(p, false), tols);
Paul's avatar
Paul committed
174
175
176
177
178
179
180
181
182
        }
        catch(...)
        {
            std::cout << "Instruction " << ins.name() << " threw an exception." << std::endl;
            throw;
        }
    }
}

183
184
void verify_reduced(program p,
                    int n,
185
                    const target& t,
186
                    compile_options options,
kahmed10's avatar
kahmed10 committed
187
                    precision quantize,
188
                    const parameter_map& inputs,
189
                    verify::tolerance tols)
Paul's avatar
Paul committed
190
{
191
    auto* mm  = p.get_main_module();
192
    auto last = std::prev(mm->end(), n);
Shucai Xiao's avatar
Shucai Xiao committed
193
    mm->remove_instructions(last, mm->end());
194
    std::cout << "Verify: " << n << std::endl;
Paul's avatar
Paul committed
195
    std::cout << p << std::endl;
196
197
    try
    {
198
        verify_program(std::to_string(n), p, t, options, quantize, inputs, tols);
199
200
201
202
203
204
    }
    catch(const std::exception& e)
    {
        std::cout << "FAILED: " << n << std::endl;
        std::cout << "Exception: " << e.what() << std::endl;
    }
Paul's avatar
Paul committed
205
206
}

207
void verify_reduced_program(const program& p,
208
                            const target& t,
209
                            compile_options options,
kahmed10's avatar
kahmed10 committed
210
                            precision quantize,
211
                            const parameter_map& inputs,
212
                            verify::tolerance tols)
Paul's avatar
Paul committed
213
{
Shucai Xiao's avatar
Shucai Xiao committed
214
215
    const auto* mm = p.get_main_module();
    auto n         = std::distance(mm->begin(), mm->end());
216
    std::cout << "Verify steps: " << n << std::endl;
217
    for(std::size_t i = 1; i < n; i++)
Paul's avatar
Paul committed
218
    {
219
220
221
222
223
224
        auto last = std::prev(mm->end(), i + 1);
        if(contains({"@literal", "@param"}, last->name()))
        {
            std::cout << "Skip: " << i << std::endl;
            continue;
        }
225
        verify_reduced(p, i, t, options, quantize, inputs, tols);
Paul's avatar
Paul committed
226
227
228
229
230
231
    }
}

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