verify_args.hpp 3.23 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#ifndef MIGRAPH_GUARD_RTGLIB_VERIFY_ARGS_HPP
#define MIGRAPH_GUARD_RTGLIB_VERIFY_ARGS_HPP

#include <migraph/verify.hpp>
#include <migraph/argument.hpp>
6
#include <migraph/config.hpp>
Paul's avatar
Paul committed
7

8
9
namespace migraph {
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
inline bool verify_args(const std::string& name,
Paul's avatar
Paul committed
12
13
14
                        const argument& cpu_arg,
                        const argument& gpu_arg,
                        double tolerance = 80)
Paul's avatar
Paul committed
15
{
Paul's avatar
Paul committed
16
    bool passed = true;
Paul's avatar
Paul committed
17
    visit_all(cpu_arg, gpu_arg)([&](auto cpu, auto gpu) {
Paul's avatar
Paul committed
18
        double error;
Paul's avatar
Paul committed
19
20
        passed = verify_range(cpu, gpu, tolerance, &error);
        if(not passed)
Paul's avatar
Paul committed
21
22
23
        {
            // TODO: Check for nans
            std::cout << "FAILED: " << name << std::endl;
Paul's avatar
Paul committed
24
            std::cout << "error: " << error << std::endl;
Paul's avatar
Paul committed
25
26
27
28
29
30
31
32
33
            if(cpu.size() < 32)
                std::cout << "cpu:" << cpu << std::endl;
            if(gpu.size() < 32)
                std::cout << "gpu:" << gpu << std::endl;
            if(range_zero(cpu))
                std::cout << "Cpu data is all zeros" << std::endl;
            if(range_zero(gpu))
                std::cout << "Gpu data is all zeros" << std::endl;

Paul's avatar
Paul committed
34
35
36
            auto mxdiff = max_diff(cpu, gpu);
            std::cout << "Max diff: " << mxdiff << std::endl;

Paul's avatar
Paul committed
37
38
39
40
41
42
43
            auto idx = mismatch_idx(cpu, gpu, float_equal);
            if(idx < range_distance(cpu))
            {
                std::cout << "Mismatch at " << idx << ": " << cpu[idx] << " != " << gpu[idx]
                          << std::endl;
            }

Paul's avatar
Paul committed
44
45
46
47
48
49
50
51
52
53
54
            auto cpu_nan_idx = find_idx(cpu, not_finite);
            if(cpu_nan_idx >= 0)
                std::cout << "Non finite number found in cpu at " << cpu_nan_idx << ": "
                          << cpu[cpu_nan_idx] << std::endl;

            auto gpu_nan_idx = find_idx(gpu, not_finite);
            if(gpu_nan_idx >= 0)
                std::cout << "Non finite number found in gpu at " << gpu_nan_idx << ": "
                          << gpu[gpu_nan_idx] << std::endl;
            std::cout << std::endl;
        }
Paul's avatar
Paul committed
55
        else
Paul's avatar
Paul committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        {
            if(range_zero(cpu))
                std::cout << "Cpu data is all zeros" << std::endl;
            if(range_zero(gpu))
                std::cout << "Gpu data is all zeros" << std::endl;

            // auto mxdiff = max_diff(cpu, gpu);
            // std::cout << "Max diff: " << mxdiff << std::endl;

            // auto idx = mismatch_idx(cpu, gpu, float_equal);
            // if(idx < range_distance(cpu))
            // {
            //     std::cout << "Mismatch at " << idx << ": " << cpu[idx] << " != " << gpu[idx]
            //               << std::endl;
            // }

Paul's avatar
Paul committed
72
73
74
75
76
77
78
79
80
            auto cpu_nan_idx = find_idx(cpu, not_finite);
            if(cpu_nan_idx >= 0)
                std::cout << "Non finite number found in cpu at " << cpu_nan_idx << ": "
                          << cpu[cpu_nan_idx] << std::endl;

            auto gpu_nan_idx = find_idx(gpu, not_finite);
            if(gpu_nan_idx >= 0)
                std::cout << "Non finite number found in gpu at " << gpu_nan_idx << ": "
                          << gpu[gpu_nan_idx] << std::endl;
Paul's avatar
Paul committed
81
            // std::cout << std::endl;
Paul's avatar
Paul committed
82
83
        }
    });
Paul's avatar
Paul committed
84
    return passed;
Paul's avatar
Paul committed
85
86
}

87
} // namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
88
89
90
} // namespace migraph

#endif