verify.hpp 7.78 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
25
#ifndef MIGRAPHX_GUARD_VERIFY_HPP
#define MIGRAPHX_GUARD_VERIFY_HPP
Paul's avatar
Paul committed
26
27
28
29
30
31

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
32
#include <assert.h>
Paul's avatar
Paul committed
33

Paul's avatar
Paul committed
34
35
#include <migraphx/float_equal.hpp>
#include <migraphx/config.hpp>
36
#include <migraphx/env.hpp>
Paul's avatar
Paul committed
37

38
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_VERIFY_ENABLE_ALLCLOSE)
Paul's avatar
Paul committed
39
namespace migraphx {
Paul's avatar
Paul committed
40
inline namespace MIGRAPHX_INLINE_NS {
Umang Yadav's avatar
Umang Yadav committed
41
namespace verify {
Paul's avatar
Paul committed
42
43
44
45
46
47
48
49

// Compute the value of a range
template <class R>
using range_value = std::decay_t<decltype(*std::declval<R>().begin())>;

struct sum_fn
{
    template <class T, class U>
Paul's avatar
Paul committed
50
51
52
53
    auto operator()(T x, U y) const
    {
        return x + y;
    }
Paul's avatar
Paul committed
54
55
56
57
58
59
60
61
62
63
64
65
};
static constexpr sum_fn sum{};

struct max_fn
{
    template <class T>
    static T id(T x)
    {
        return x;
    }

    template <class T, class U>
Paul's avatar
Paul committed
66
67
68
69
    auto operator()(T x, U y) const
    {
        return x > y ? x : y;
    }
Paul's avatar
Paul committed
70
71
72
73
74
75
76
77
};
static constexpr max_fn max{};

namespace abs_diff_detail {
using std::fabs;
struct fn
{
    template <class T, class U>
Paul's avatar
Paul committed
78
79
80
81
    auto operator()(T x, U y) const
    {
        return fabs(x - y);
    }
Paul's avatar
Paul committed
82
83
84
85
86
87
88
89
90
91
92
};

} // namespace abs_diff_detail

static constexpr abs_diff_detail::fn abs_diff{};

struct not_finite_fn
{
    template <class T>
    bool operator()(T x) const
    {
93
        return not std::isfinite(static_cast<double>(x));
Paul's avatar
Paul committed
94
95
96
97
98
99
100
101
102
    }
};
static constexpr not_finite_fn not_finite{};

struct compare_mag_fn
{
    template <class T, class U>
    bool operator()(T x, U y) const
    {
103
        return std::fabs(x) < std::fabs(y);
Paul's avatar
Paul committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    }
};
static constexpr compare_mag_fn compare_mag{};

struct square_diff_fn
{
    template <class T, class U>
    double operator()(T x, U y) const
    {
        return (x - y) * (x - y);
    }
};
static constexpr square_diff_fn square_diff{};

template <class R1>
bool range_empty(R1&& r1)
{
    return r1.begin() == r1.end();
}

template <class R1>
Paul's avatar
Paul committed
125
126
127
128
auto range_distance(R1&& r1)
{
    return std::distance(r1.begin(), r1.end());
}
Paul's avatar
Paul committed
129
130
131
132

template <class R1>
bool range_zero(R1&& r1)
{
Paul's avatar
Paul committed
133
    return std::all_of(r1.begin(), r1.end(), [](auto x) { return float_equal(x, 0); });
Paul's avatar
Paul committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
}

template <class R1, class R2, class T, class Reducer, class Product>
T range_product(R1&& r1, R2&& r2, T state, Reducer r, Product p)
{
    return std::inner_product(r1.begin(), r1.end(), r2.begin(), state, r, p);
}

template <class R1, class R2, class Compare>
std::size_t mismatch_idx(R1&& r1, R2&& r2, Compare compare)
{
    auto p = std::mismatch(r1.begin(), r1.end(), r2.begin(), compare);
    return std::distance(r1.begin(), p.first);
}

template <class R1, class Predicate>
long find_idx(R1&& r1, Predicate p)
{
    auto it = std::find_if(r1.begin(), r1.end(), p);
    if(it == r1.end())
        return -1;
    else
        return std::distance(r1.begin(), it);
}

template <class R1, class R2>
double max_diff(R1&& r1, R2&& r2)
{
    return range_product(r1, r2, 0.0, max, abs_diff);
}

template <class R1, class R2, class T>
std::size_t mismatch_diff(R1&& r1, R2&& r2, T diff)
{
Paul's avatar
Paul committed
168
169
    return mismatch_idx(r1, r2, [&](auto x, auto y) {
        auto d = abs_diff(x, y);
Paul's avatar
Paul committed
170
        return float_equal(d, diff);
Paul's avatar
Paul committed
171
    });
Paul's avatar
Paul committed
172
173
174
}

template <class R1, class R2>
175
double rms_range(const R1& r1, const R2& r2)
Paul's avatar
Paul committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{
    std::size_t n = range_distance(r1);
    if(n == range_distance(r2))
    {
        double square_difference = range_product(r1, r2, 0.0, sum_fn{}, square_diff);
        double mag1              = *std::max_element(r1.begin(), r1.end(), compare_mag);
        double mag2              = *std::max_element(r2.begin(), r2.end(), compare_mag);
        double mag =
            std::max({std::fabs(mag1), std::fabs(mag2), std::numeric_limits<double>::min()});
        return std::sqrt(square_difference) / (std::sqrt(n) * mag);
    }
    else
        return std::numeric_limits<range_value<R1>>::max();
}

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
template <class R>
double get_rms_tol(const R&, std::size_t tolerance = 80)
{
    double threshold = std::numeric_limits<range_value<R>>::epsilon() * tolerance;
    return threshold;
}

/*
C++ doesn't support named arguments, this is just wrapper that helps distinguish between actual
results v/s expected results arguments.
*/
template <class T>
struct expected
{
    expected() = default;
    explicit expected(const T& input) : x(&input) {}
    const T& data() const
    {
        assert(x != nullptr);
        return *x;
    }

    private:
    const T* x = nullptr;
};

// deduction guide for templated expected class
template <class T>
expected(const T&) -> expected<T>;

struct tolerance
{
    double rms_tol = 0.001;
    double atol    = 0.001;
    double rtol    = 0.001;
};

/*
MIGraphX implementation of numpy's np.allclose() which checks if elementwise absolute diff is within
tolerance using this formula:  abs(a - b) < atol + rtol(abs(b))
*/
template <class R1, class R2>
bool allclose(const R1& r1, const R2& r2, tolerance tols)
{
    std::size_t n = range_distance(r1);
    if(n == range_distance(r2))
    {
        auto idx = mismatch_idx(r1, r2, [&](auto x, auto y) {
            return abs_diff(double(x), double(y)) < tols.atol + tols.rtol * std::abs(double(y));
        });
        return idx >= range_distance(r1);
    }
    return false;
}

Paul's avatar
Paul committed
246
template <class R1, class R2>
247
248
249
250
bool verify_rms_range(const R1& r1,
                      const R2& r2,
                      std::size_t tolerance = 80,
                      double* out_rms_error = nullptr)
Paul's avatar
Paul committed
251
{
252
    double threshold = get_rms_tol(r1, tolerance);
Paul's avatar
Paul committed
253
    auto error       = rms_range(r1, r2);
254
255
    if(out_rms_error != nullptr)
        *out_rms_error = error;
Paul's avatar
Paul committed
256
257
    return error <= threshold;
}
258

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
template <class R1, class R2>
bool verify_range_with_tolerance(const R1& r1,
                                 const expected<R2>& r2,
                                 tolerance tols        = tolerance{},
                                 double* out_rms_error = nullptr)
{
    auto rms_error = rms_range(r1, r2.data());
    // disable ewise_verify by default for now, it requires lot of tests to be fixed
    bool ewise_verify = true;
    if(enabled(MIGRAPHX_VERIFY_ENABLE_ALLCLOSE{}))
    {
        ewise_verify = allclose(r1, r2.data(), tols);
    }
    if(out_rms_error != nullptr)
        *out_rms_error = rms_error;
    return rms_error <= tols.rms_tol and ewise_verify;
}

// expected argument should be passed as second, but if it is passed as the first by mistake then
// flip the order
template <class R1, class R2>
bool verify_range_with_tolerance(const expected<R1>& r1,
                                 const R2& r2,
                                 tolerance tols        = tolerance{},
                                 double* out_rms_error = nullptr)
{
    return verify_rms_range(r2, r1, tols, out_rms_error);
}

Umang Yadav's avatar
Umang Yadav committed
288
} // namespace verify
Paul's avatar
Paul committed
289
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
290
} // namespace migraphx
Paul's avatar
Paul committed
291
#endif