check_err.hpp 5.98 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

#pragma once

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <limits>
#include <type_traits>
#include <vector>

16
#include "ck/ck.hpp"
Chao Liu's avatar
Chao Liu committed
17
#include "ck/utility/data_type.hpp"
18
#include "ck/host_utility/io.hpp"
Chao Liu's avatar
Chao Liu committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

namespace ck {
namespace utils {

template <typename T>
typename std::enable_if<std::is_floating_point<T>::value && !std::is_same<T, half_t>::value,
                        bool>::type
check_err(const std::vector<T>& out,
          const std::vector<T>& ref,
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 1e-5,
          double atol            = 3e-6)
{
    if(out.size() != ref.size())
    {
34
35
        std::cout << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        return false;
    }

    bool res{true};
    int err_count  = 0;
    double err     = 0;
    double max_err = std::numeric_limits<double>::min();
    for(std::size_t i = 0; i < ref.size(); ++i)
    {
        err = std::abs(out[i] - ref[i]);
        if(err > atol + rtol * std::abs(ref[i]) || !std::isfinite(out[i]) || !std::isfinite(ref[i]))
        {
            max_err = err > max_err ? err : max_err;
            err_count++;
            if(err_count < 5)
            {
52
53
                std::cout << msg << std::setw(12) << std::setprecision(7) << " out[" << i
                          << "] != ref[" << i << "]: " << out[i] << " != " << ref[i] << std::endl;
Chao Liu's avatar
Chao Liu committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
            }
            res = false;
        }
    }
    if(!res)
    {
        std::cout << std::setw(12) << std::setprecision(7) << "max err: " << max_err << std::endl;
    }
    return res;
}

template <typename T>
typename std::enable_if<std::is_same<T, bhalf_t>::value, bool>::type
check_err(const std::vector<T>& out,
          const std::vector<T>& ref,
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 1e-3,
          double atol            = 1e-3)
{
    if(out.size() != ref.size())
    {
75
76
        std::cout << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        return false;
    }

    bool res{true};
    int err_count = 0;
    double err    = 0;
    // TODO: This is a hack. We should have proper specialization for bhalf_t data type.
    double max_err = std::numeric_limits<float>::min();
    for(std::size_t i = 0; i < ref.size(); ++i)
    {
        double o = type_convert<float>(out[i]);
        double r = type_convert<float>(ref[i]);
        err      = std::abs(o - r);
        if(err > atol + rtol * std::abs(r) || !std::isfinite(o) || !std::isfinite(r))
        {
            max_err = err > max_err ? err : max_err;
            err_count++;
            if(err_count < 5)
            {
96
97
                std::cout << msg << std::setw(12) << std::setprecision(7) << " out[" << i
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
Chao Liu's avatar
Chao Liu committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            }
            res = false;
        }
    }
    if(!res)
    {
        std::cout << std::setw(12) << std::setprecision(7) << "max err: " << max_err << std::endl;
    }
    return res;
}

template <typename T>
typename std::enable_if<std::is_same<T, half_t>::value, bool>::type
check_err(const std::vector<T>& out,
          const std::vector<T>& ref,
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 1e-3,
          double atol            = 1e-3)
{
    if(out.size() != ref.size())
    {
119
120
        std::cout << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        return false;
    }

    bool res{true};
    int err_count  = 0;
    double err     = 0;
    double max_err = std::numeric_limits<T>::min();
    for(std::size_t i = 0; i < ref.size(); ++i)
    {
        double o = type_convert<float>(out[i]);
        double r = type_convert<float>(ref[i]);
        err      = std::abs(o - r);
        if(err > atol + rtol * std::abs(r) || !std::isfinite(o) || !std::isfinite(r))
        {
            max_err = err > max_err ? err : max_err;
            err_count++;
            if(err_count < 5)
            {
139
140
                std::cout << msg << std::setw(12) << std::setprecision(7) << " out[" << i
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
Chao Liu's avatar
Chao Liu committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            }
            res = false;
        }
    }
    if(!res)
    {
        std::cout << std::setw(12) << std::setprecision(7) << "max err: " << max_err << std::endl;
    }
    return res;
}

template <typename T>
typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bhalf_t>::value, bool>::type
check_err(const std::vector<T>& out,
          const std::vector<T>& ref,
          const std::string& msg = "Error: Incorrect results!",
          double                 = 0,
158
          double atol            = 0)
Chao Liu's avatar
Chao Liu committed
159
160
161
{
    if(out.size() != ref.size())
    {
162
163
        std::cout << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
164
165
166
167
168
169
170
171
172
173
174
175
176
        return false;
    }

    bool res{true};
    int err_count   = 0;
    int64_t err     = 0;
    int64_t max_err = std::numeric_limits<int64_t>::min();
    for(std::size_t i = 0; i < ref.size(); ++i)
    {
        int64_t o = out[i];
        int64_t r = ref[i];
        err       = std::abs(o - r);

177
        if(err > atol)
Chao Liu's avatar
Chao Liu committed
178
179
180
181
182
        {
            max_err = err > max_err ? err : max_err;
            err_count++;
            if(err_count < 5)
            {
183
184
185
                std::cout << msg << " out[" << i << "] != ref[" << i
                          << "]: " << static_cast<int>(out[i]) << " != " << static_cast<int>(ref[i])
                          << std::endl;
Chao Liu's avatar
Chao Liu committed
186
187
188
189
190
191
192
193
194
195
196
197
198
            }
            res = false;
        }
    }
    if(!res)
    {
        std::cout << "max err: " << max_err << std::endl;
    }
    return res;
}

} // namespace utils
} // namespace ck