verify.hpp 5.19 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
32

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>

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

Paul's avatar
Paul committed
36
namespace migraphx {
Paul's avatar
Paul committed
37
inline namespace MIGRAPHX_INLINE_NS {
Umang Yadav's avatar
Umang Yadav committed
38
namespace verify {
Paul's avatar
Paul committed
39
40
41
42
43
44
45
46

// 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
47
48
49
50
    auto operator()(T x, U y) const
    {
        return x + y;
    }
Paul's avatar
Paul committed
51
52
53
54
55
56
57
58
59
60
61
62
};
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
63
64
65
66
    auto operator()(T x, U y) const
    {
        return x > y ? x : y;
    }
Paul's avatar
Paul committed
67
68
69
70
71
72
73
74
};
static constexpr max_fn max{};

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

} // namespace abs_diff_detail

static constexpr abs_diff_detail::fn abs_diff{};

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

struct compare_mag_fn
{
    template <class T, class U>
    bool operator()(T x, U y) const
    {
Artur Wojcik's avatar
math  
Artur Wojcik committed
100
        return std::fabs(x) < std::fabs(y);
Paul's avatar
Paul committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    }
};
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
122
123
124
125
auto range_distance(R1&& r1)
{
    return std::distance(r1.begin(), r1.end());
}
Paul's avatar
Paul committed
126
127
128
129

template <class R1>
bool range_zero(R1&& r1)
{
Paul's avatar
Paul committed
130
    return std::all_of(r1.begin(), r1.end(), [](auto x) { return float_equal(x, 0); });
Paul's avatar
Paul committed
131
132
133
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
}

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
165
166
    return mismatch_idx(r1, r2, [&](auto x, auto y) {
        auto d = abs_diff(x, y);
Paul's avatar
Paul committed
167
        return float_equal(d, diff);
Paul's avatar
Paul committed
168
    });
Paul's avatar
Paul committed
169
170
171
}

template <class R1, class R2>
172
double rms_range(const R1& r1, const R2& r2)
Paul's avatar
Paul committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
    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();
}

Paul's avatar
Paul committed
188
template <class R1, class R2>
189
bool verify_range(const R1& r1, const R2& r2, double tolerance = 80, double* out_error = nullptr)
Paul's avatar
Paul committed
190
191
192
{
    double threshold = std::numeric_limits<range_value<R1>>::epsilon() * tolerance;
    auto error       = rms_range(r1, r2);
Paul's avatar
Paul committed
193
194
    if(out_error != nullptr)
        *out_error = error;
Paul's avatar
Paul committed
195
196
    return error <= threshold;
}
197

Umang Yadav's avatar
Umang Yadav committed
198
} // namespace verify
Paul's avatar
Paul committed
199
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
200
} // namespace migraphx
Paul's avatar
Paul committed
201
#endif