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

#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/utility/type.hpp"
19
#include "ck/host_utility/io.hpp"
Chao Liu's avatar
Chao Liu committed
20

21
22
#include "ck/library/utility/ranges.hpp"

Chao Liu's avatar
Chao Liu committed
23
24
25
namespace ck {
namespace utils {

26
template <typename ComputeDataType, typename OutDataType, typename AccDataType = ComputeDataType>
27
double get_relative_threshold(const int number_of_accumulations = 1)
28
{
Illia Silin's avatar
Illia Silin committed
29
    using F4   = ck::f4_t;
30
31
32
33
34
35
36
    using F8   = ck::f8_t;
    using F16  = ck::half_t;
    using BF16 = ck::bhalf_t;
    using F32  = float;
    using I8   = int8_t;
    using I32  = int32_t;

Illia Silin's avatar
Illia Silin committed
37
38
39
40
    static_assert(is_same_v<ComputeDataType, F4> || is_same_v<ComputeDataType, F8> ||
                      is_same_v<ComputeDataType, F16> || is_same_v<ComputeDataType, BF16> ||
                      is_same_v<ComputeDataType, F32> || is_same_v<ComputeDataType, I8> ||
                      is_same_v<ComputeDataType, I32> || is_same_v<ComputeDataType, int>,
41
42
43
44
45
46
47
48
49
50
51
52
                  "Warning: Unhandled ComputeDataType for setting up the relative threshold!");
    double compute_error = 0;
    if constexpr(is_same_v<ComputeDataType, I8> || is_same_v<ComputeDataType, I32> ||
                 is_same_v<ComputeDataType, int>)
    {
        return 0;
    }
    else
    {
        compute_error = std::pow(2, -NumericUtils<ComputeDataType>::mant) * 0.5;
    }

Illia Silin's avatar
Illia Silin committed
53
54
55
56
    static_assert(is_same_v<OutDataType, F4> || is_same_v<OutDataType, F8> ||
                      is_same_v<OutDataType, F16> || is_same_v<OutDataType, BF16> ||
                      is_same_v<OutDataType, F32> || is_same_v<OutDataType, I8> ||
                      is_same_v<OutDataType, I32> || is_same_v<OutDataType, int>,
57
58
59
60
61
62
63
64
65
66
67
68
69
                  "Warning: Unhandled OutDataType for setting up the relative threshold!");
    double output_error = 0;
    if constexpr(is_same_v<OutDataType, I8> || is_same_v<OutDataType, I32> ||
                 is_same_v<OutDataType, int>)
    {
        return 0;
    }
    else
    {
        output_error = std::pow(2, -NumericUtils<OutDataType>::mant) * 0.5;
    }
    double midway_error = std::max(compute_error, output_error);

Illia Silin's avatar
Illia Silin committed
70
71
72
73
    static_assert(is_same_v<AccDataType, F4> || is_same_v<AccDataType, F8> ||
                      is_same_v<AccDataType, F16> || is_same_v<AccDataType, BF16> ||
                      is_same_v<AccDataType, F32> || is_same_v<AccDataType, I8> ||
                      is_same_v<AccDataType, I32> || is_same_v<AccDataType, int>,
74
75
76
77
78
79
80
81
82
                  "Warning: Unhandled AccDataType for setting up the relative threshold!");
    double acc_error = 0;
    if constexpr(is_same_v<AccDataType, I8> || is_same_v<AccDataType, I32> ||
                 is_same_v<AccDataType, int>)
    {
        return 0;
    }
    else
    {
83
        acc_error = std::pow(2, -NumericUtils<AccDataType>::mant) * 0.5 * number_of_accumulations;
84
85
86
87
88
    }
    return std::max(acc_error, midway_error);
}

template <typename ComputeDataType, typename OutDataType, typename AccDataType = ComputeDataType>
89
double get_absolute_threshold(const double max_possible_num, const int number_of_accumulations = 1)
90
{
Illia Silin's avatar
Illia Silin committed
91
    using F4   = ck::f4_t;
92
93
94
95
96
97
98
    using F8   = ck::f8_t;
    using F16  = ck::half_t;
    using BF16 = ck::bhalf_t;
    using F32  = float;
    using I8   = int8_t;
    using I32  = int32_t;

Illia Silin's avatar
Illia Silin committed
99
100
101
102
    static_assert(is_same_v<ComputeDataType, F4> || is_same_v<ComputeDataType, F8> ||
                      is_same_v<ComputeDataType, F16> || is_same_v<ComputeDataType, BF16> ||
                      is_same_v<ComputeDataType, F32> || is_same_v<ComputeDataType, I8> ||
                      is_same_v<ComputeDataType, I32> || is_same_v<ComputeDataType, int>,
103
104
105
106
107
108
109
110
111
112
113
114
115
                  "Warning: Unhandled ComputeDataType for setting up the absolute threshold!");
    auto expo            = std::log2(std::abs(max_possible_num));
    double compute_error = 0;
    if constexpr(is_same_v<ComputeDataType, I8> || is_same_v<ComputeDataType, I32> ||
                 is_same_v<ComputeDataType, int>)
    {
        return 0;
    }
    else
    {
        compute_error = std::pow(2, expo - NumericUtils<ComputeDataType>::mant) * 0.5;
    }

Illia Silin's avatar
Illia Silin committed
116
117
118
119
    static_assert(is_same_v<OutDataType, F4> || is_same_v<OutDataType, F8> ||
                      is_same_v<OutDataType, F16> || is_same_v<OutDataType, BF16> ||
                      is_same_v<OutDataType, F32> || is_same_v<OutDataType, I8> ||
                      is_same_v<OutDataType, I32> || is_same_v<OutDataType, int>,
120
121
122
123
124
125
126
127
128
129
130
131
132
                  "Warning: Unhandled OutDataType for setting up the absolute threshold!");
    double output_error = 0;
    if constexpr(is_same_v<OutDataType, I8> || is_same_v<OutDataType, I32> ||
                 is_same_v<OutDataType, int>)
    {
        return 0;
    }
    else
    {
        output_error = std::pow(2, expo - NumericUtils<OutDataType>::mant) * 0.5;
    }
    double midway_error = std::max(compute_error, output_error);

Illia Silin's avatar
Illia Silin committed
133
134
135
136
    static_assert(is_same_v<AccDataType, F4> || is_same_v<AccDataType, F8> ||
                      is_same_v<AccDataType, F16> || is_same_v<AccDataType, BF16> ||
                      is_same_v<AccDataType, F32> || is_same_v<AccDataType, I8> ||
                      is_same_v<AccDataType, I32> || is_same_v<AccDataType, int>,
137
138
139
140
141
142
143
144
145
146
                  "Warning: Unhandled AccDataType for setting up the absolute threshold!");
    double acc_error = 0;
    if constexpr(is_same_v<AccDataType, I8> || is_same_v<AccDataType, I32> ||
                 is_same_v<AccDataType, int>)
    {
        return 0;
    }
    else
    {
        acc_error =
147
            std::pow(2, expo - NumericUtils<AccDataType>::mant) * 0.5 * number_of_accumulations;
148
149
150
151
    }
    return std::max(acc_error, midway_error);
}

152
153
154
155
156
157
158
159
template <typename Range, typename RefRange>
typename std::enable_if<
    std::is_same_v<ranges::range_value_t<Range>, ranges::range_value_t<RefRange>> &&
        std::is_floating_point_v<ranges::range_value_t<Range>> &&
        !std::is_same_v<ranges::range_value_t<Range>, half_t>,
    bool>::type
check_err(const Range& out,
          const RefRange& ref,
Chao Liu's avatar
Chao Liu committed
160
161
162
163
164
165
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 1e-5,
          double atol            = 3e-6)
{
    if(out.size() != ref.size())
    {
166
        std::cerr << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
167
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
168
169
170
171
172
173
174
175
176
        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)
    {
177
178
179
180
        const double o = *std::next(std::begin(out), i);
        const double r = *std::next(std::begin(ref), i);
        err            = std::abs(o - r);
        if(err > atol + rtol * std::abs(r) || !std::isfinite(o) || !std::isfinite(r))
Chao Liu's avatar
Chao Liu committed
181
182
183
184
185
        {
            max_err = err > max_err ? err : max_err;
            err_count++;
            if(err_count < 5)
            {
186
                std::cerr << msg << std::setw(12) << std::setprecision(7) << " out[" << i
187
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
Chao Liu's avatar
Chao Liu committed
188
189
190
191
192
193
            }
            res = false;
        }
    }
    if(!res)
    {
194
195
196
197
198
        const float error_percent =
            static_cast<float>(err_count) / static_cast<float>(out.size()) * 100.f;
        std::cerr << "max err: " << max_err;
        std::cerr << ", number of errors: " << err_count;
        std::cerr << ", " << error_percent << "% wrong values" << std::endl;
Chao Liu's avatar
Chao Liu committed
199
200
201
202
    }
    return res;
}

203
204
205
206
207
208
209
template <typename Range, typename RefRange>
typename std::enable_if<
    std::is_same_v<ranges::range_value_t<Range>, ranges::range_value_t<RefRange>> &&
        std::is_same_v<ranges::range_value_t<Range>, bhalf_t>,
    bool>::type
check_err(const Range& out,
          const RefRange& ref,
Chao Liu's avatar
Chao Liu committed
210
          const std::string& msg = "Error: Incorrect results!",
211
          double rtol            = 1e-1,
Chao Liu's avatar
Chao Liu committed
212
213
214
215
          double atol            = 1e-3)
{
    if(out.size() != ref.size())
    {
216
        std::cerr << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
217
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
218
219
220
221
222
223
224
225
226
227
        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)
    {
228
229
230
        const double o = type_convert<float>(*std::next(std::begin(out), i));
        const double r = type_convert<float>(*std::next(std::begin(ref), i));
        err            = std::abs(o - r);
Chao Liu's avatar
Chao Liu committed
231
232
233
234
235
236
        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)
            {
237
                std::cerr << msg << std::setw(12) << std::setprecision(7) << " out[" << i
238
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
Chao Liu's avatar
Chao Liu committed
239
240
241
242
243
244
            }
            res = false;
        }
    }
    if(!res)
    {
245
246
247
248
249
        const float error_percent =
            static_cast<float>(err_count) / static_cast<float>(out.size()) * 100.f;
        std::cerr << "max err: " << max_err;
        std::cerr << ", number of errors: " << err_count;
        std::cerr << ", " << error_percent << "% wrong values" << std::endl;
Chao Liu's avatar
Chao Liu committed
250
251
252
253
    }
    return res;
}

254
255
256
257
258
259
260
template <typename Range, typename RefRange>
typename std::enable_if<
    std::is_same_v<ranges::range_value_t<Range>, ranges::range_value_t<RefRange>> &&
        std::is_same_v<ranges::range_value_t<Range>, half_t>,
    bool>::type
check_err(const Range& out,
          const RefRange& ref,
Chao Liu's avatar
Chao Liu committed
261
262
263
264
265
266
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 1e-3,
          double atol            = 1e-3)
{
    if(out.size() != ref.size())
    {
267
        std::cerr << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
268
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
269
270
271
272
273
274
        return false;
    }

    bool res{true};
    int err_count  = 0;
    double err     = 0;
275
    double max_err = NumericLimits<ranges::range_value_t<Range>>::Min();
Chao Liu's avatar
Chao Liu committed
276
277
    for(std::size_t i = 0; i < ref.size(); ++i)
    {
278
279
280
        const double o = type_convert<float>(*std::next(std::begin(out), i));
        const double r = type_convert<float>(*std::next(std::begin(ref), i));
        err            = std::abs(o - r);
Chao Liu's avatar
Chao Liu committed
281
282
283
284
285
286
        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)
            {
287
                std::cerr << msg << std::setw(12) << std::setprecision(7) << " out[" << i
288
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
Chao Liu's avatar
Chao Liu committed
289
290
291
292
293
294
            }
            res = false;
        }
    }
    if(!res)
    {
295
296
297
298
299
        const float error_percent =
            static_cast<float>(err_count) / static_cast<float>(out.size()) * 100.f;
        std::cerr << "max err: " << max_err;
        std::cerr << ", number of errors: " << err_count;
        std::cerr << ", " << error_percent << "% wrong values" << std::endl;
Chao Liu's avatar
Chao Liu committed
300
301
302
303
    }
    return res;
}

304
305
306
template <typename Range, typename RefRange>
std::enable_if_t<(std::is_same_v<ranges::range_value_t<Range>, ranges::range_value_t<RefRange>> &&
                  std::is_integral_v<ranges::range_value_t<Range>> &&
307
308
309
                  !std::is_same_v<ranges::range_value_t<Range>, bhalf_t> &&
                  !std::is_same_v<ranges::range_value_t<Range>, f8_t> &&
                  !std::is_same_v<ranges::range_value_t<Range>, bf8_t>)
310
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
311
                     || std::is_same_v<ranges::range_value_t<Range>, int4_t>
312
313
314
#endif
                 ,
                 bool>
315
316
check_err(const Range& out,
          const RefRange& ref,
Chao Liu's avatar
Chao Liu committed
317
318
          const std::string& msg = "Error: Incorrect results!",
          double                 = 0,
319
          double atol            = 0)
Chao Liu's avatar
Chao Liu committed
320
321
322
{
    if(out.size() != ref.size())
    {
323
        std::cerr << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
324
                  << std::endl;
Chao Liu's avatar
Chao Liu committed
325
326
327
328
329
330
331
332
333
        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)
    {
334
335
336
        const int64_t o = *std::next(std::begin(out), i);
        const int64_t r = *std::next(std::begin(ref), i);
        err             = std::abs(o - r);
Chao Liu's avatar
Chao Liu committed
337

338
        if(err > atol)
Chao Liu's avatar
Chao Liu committed
339
340
341
342
343
        {
            max_err = err > max_err ? err : max_err;
            err_count++;
            if(err_count < 5)
            {
344
                std::cerr << msg << " out[" << i << "] != ref[" << i << "]: " << o << " != " << r
345
                          << std::endl;
Chao Liu's avatar
Chao Liu committed
346
347
348
349
350
351
            }
            res = false;
        }
    }
    if(!res)
    {
352
353
354
355
356
        const float error_percent =
            static_cast<float>(err_count) / static_cast<float>(out.size()) * 100.f;
        std::cerr << "max err: " << max_err;
        std::cerr << ", number of errors: " << err_count;
        std::cerr << ", " << error_percent << "% wrong values" << std::endl;
Chao Liu's avatar
Chao Liu committed
357
358
359
360
    }
    return res;
}

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
template <typename Range, typename RefRange>
std::enable_if_t<(std::is_same_v<ranges::range_value_t<Range>, ranges::range_value_t<RefRange>> &&
                  std::is_same_v<ranges::range_value_t<Range>, f8_t>),
                 bool>
check_err(const Range& out,
          const RefRange& ref,
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 1e-3,
          double atol            = 1e-3)
{
    if(out.size() != ref.size())
    {
        std::cerr << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
                  << std::endl;
        return false;
    }

    bool res{true};
    int err_count  = 0;
    double err     = 0;
    double max_err = std::numeric_limits<float>::min();
382

383
384
385
386
387
    for(std::size_t i = 0; i < ref.size(); ++i)
    {
        const double o = type_convert<float>(*std::next(std::begin(out), i));
        const double r = type_convert<float>(*std::next(std::begin(ref), i));
        err            = std::abs(o - r);
388

389
390
391
392
393
394
395
396
397
398
399
400
        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)
            {
                std::cerr << msg << std::setw(12) << std::setprecision(7) << " out[" << i
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
            }
            res = false;
        }
    }
401

402
403
    if(!res)
    {
404
405
        std::cerr << std::setw(12) << std::setprecision(7) << "max err: " << max_err
                  << " number of errors: " << err_count << std::endl;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
    }
    return res;
}

template <typename Range, typename RefRange>
std::enable_if_t<(std::is_same_v<ranges::range_value_t<Range>, ranges::range_value_t<RefRange>> &&
                  std::is_same_v<ranges::range_value_t<Range>, bf8_t>),
                 bool>
check_err(const Range& out,
          const RefRange& ref,
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 1e-3,
          double atol            = 1e-3)
{
    if(out.size() != ref.size())
    {
        std::cerr << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
                  << std::endl;
        return false;
    }

    bool res{true};
    int err_count  = 0;
    double err     = 0;
    double max_err = std::numeric_limits<float>::min();
    for(std::size_t i = 0; i < ref.size(); ++i)
    {
        const double o = type_convert<float>(*std::next(std::begin(out), i));
        const double r = type_convert<float>(*std::next(std::begin(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)
            {
                std::cerr << msg << std::setw(12) << std::setprecision(7) << " out[" << i
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
            }
            res = false;
        }
    }
    if(!res)
    {
        std::cerr << std::setw(12) << std::setprecision(7) << "max err: " << max_err << std::endl;
    }
    return res;
}

Illia Silin's avatar
Illia Silin committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
template <typename Range, typename RefRange>
std::enable_if_t<(std::is_same_v<ranges::range_value_t<Range>, ranges::range_value_t<RefRange>> &&
                  std::is_same_v<ranges::range_value_t<Range>, f4_t>),
                 bool>
check_err(const Range& out,
          const RefRange& ref,
          const std::string& msg = "Error: Incorrect results!",
          double rtol            = 0.5,
          double atol            = 0.5)
{
    if(out.size() != ref.size())
    {
        std::cerr << msg << " out.size() != ref.size(), :" << out.size() << " != " << ref.size()
                  << std::endl;
        return false;
    }

    bool res{true};
    int err_count  = 0;
    double err     = 0;
    double max_err = std::numeric_limits<float>::min();

    for(std::size_t i = 0; i < ref.size(); ++i)
    {
        const double o = type_convert<float>(*std::next(std::begin(out), i));
        const double r = type_convert<float>(*std::next(std::begin(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)
            {
                std::cerr << msg << std::setw(12) << std::setprecision(7) << " out[" << i
                          << "] != ref[" << i << "]: " << o << " != " << r << std::endl;
            }
            res = false;
        }
    }

    if(!res)
    {
        std::cerr << std::setw(12) << std::setprecision(7) << "max err: " << max_err
                  << " number of errors: " << err_count << std::endl;
    }
    return res;
}

Chao Liu's avatar
Chao Liu committed
504
505
} // namespace utils
} // namespace ck