"...targets/git@developer.sourcefind.cn:gaoqiong/migraphx.git" did not exist on "5dc49b16d4eb34234fbe64f1a92172225c8b89fc"
verify.cpp 7.82 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
    bool passed            = true;
123
124
    for(std::size_t i = 0; i < output_num; ++i)
    {
125
126
        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())
127
128
        {
            std::cout << "FAILED: " << name << std::endl;
129
130
            std::cout << "Shape mismatch {" << ref_outs[i].get_shape() << "} != {"
                      << target_outs[i].get_shape() << "}" << std::endl;
131
132
133
        }
        else
        {
134
            passed &= verify_args(name, target_outs[i], verify::expected{ref_outs[i]}, tols);
135
        }
136
    }
137
138
    if(passed)
        std::cout << "MIGraphX verification passed successfully." << std::endl;
Paul's avatar
Paul committed
139
140
}

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

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

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

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