all_close.hpp 6.25 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
Scott Cyphers's avatar
Scott Cyphers committed
16 17 18

#pragma once

19
#include <cmath>
Scott Cyphers's avatar
Scott Cyphers committed
20
#include <memory>
21
#include <vector>
Scott Cyphers's avatar
Scott Cyphers committed
22

23
#include "gtest/gtest.h"
24
#include "ngraph/type/element_type.hpp"
25
#include "test_tools.hpp"
Scott Cyphers's avatar
Scott Cyphers committed
26 27 28 29 30

namespace ngraph
{
    namespace test
    {
31 32 33 34 35 36
        /// \brief Same as numpy.allclose
        /// \param a First tensor to compare
        /// \param b Second tensor to compare
        /// \param rtol Relative tolerance
        /// \param atol Absolute tolerance
        /// \returns true if shapes match and for all elements, |a_i-b_i| <= atol + rtol*|b_i|.
Robert Kimball's avatar
Robert Kimball committed
37
        template <typename T>
38
        typename std::enable_if<std::is_floating_point<T>::value, ::testing::AssertionResult>::type
39 40 41 42
            all_close(const std::vector<T>& a,
                      const std::vector<T>& b,
                      T rtol = static_cast<T>(1e-5),
                      T atol = static_cast<T>(1e-8))
Robert Kimball's avatar
Robert Kimball committed
43
        {
44
            bool rc = true;
45
            ::testing::AssertionResult ar_fail = ::testing::AssertionFailure();
46 47 48 49
            if (a.size() != b.size())
            {
                throw std::invalid_argument("all_close: Argument vectors' sizes do not match");
            }
50
            size_t count = 0;
Robert Kimball's avatar
Robert Kimball committed
51 52
            for (size_t i = 0; i < a.size(); ++i)
            {
53 54
                if (std::abs(a[i] - b[i]) > atol + rtol * std::abs(b[i]) || !std::isfinite(a[i]) ||
                    !std::isfinite(b[i]))
Robert Kimball's avatar
Robert Kimball committed
55
                {
56 57
                    if (count < 5)
                    {
58 59
                        ar_fail << std::setprecision(std::numeric_limits<long double>::digits10 + 1)
                                << a[i] << " is not close to " << b[i] << " at index " << i << "\n";
60 61
                    }
                    count++;
62
                    rc = false;
63 64
                }
            }
65 66
            ar_fail << "diff count: " << count << " out of " << a.size() << "\n";
            return rc ? ::testing::AssertionSuccess() : ar_fail;
67 68 69 70 71 72 73 74 75
        }

        /// \brief Same as numpy.allclose
        /// \param a First tensor to compare
        /// \param b Second tensor to compare
        /// \param rtol Relative tolerance
        /// \param atol Absolute tolerance
        /// \returns true if shapes match and for all elements, |a_i-b_i| <= atol + rtol*|b_i|.
        template <typename T>
76
        typename std::enable_if<std::is_integral<T>::value, ::testing::AssertionResult>::type
77 78 79 80 81 82
            all_close(const std::vector<T>& a,
                      const std::vector<T>& b,
                      T rtol = static_cast<T>(1e-5),
                      T atol = static_cast<T>(1e-8))
        {
            bool rc = true;
83
            ::testing::AssertionResult ar_fail = ::testing::AssertionFailure();
84 85 86 87
            if (a.size() != b.size())
            {
                throw std::invalid_argument("all_close: Argument vectors' sizes do not match");
            }
88 89 90 91 92
            for (size_t i = 0; i < a.size(); ++i)
            {
                T abs_diff = (a[i] > b[i]) ? (a[i] - b[i]) : (b[i] - a[i]);
                if (abs_diff > atol + rtol * b[i])
                {
93
                    ar_fail << a[i] << " is not close to " << b[i] << " at index " << i;
94
                    rc = false;
Robert Kimball's avatar
Robert Kimball committed
95 96
                }
            }
97
            return rc ? ::testing::AssertionSuccess() : ar_fail;
Robert Kimball's avatar
Robert Kimball committed
98
        }
99

100 101 102 103 104
        /// \brief Same as numpy.allclose
        /// \param a First tensor to compare
        /// \param b Second tensor to compare
        /// \param rtol Relative tolerance
        /// \param atol Absolute tolerance
Scott Cyphers's avatar
Scott Cyphers committed
105
        /// Returns true if shapes match and for all elements, |a_i-b_i| <= atol + rtol*|b_i|.
106
        template <typename T>
107 108 109 110
        ::testing::AssertionResult all_close(const std::shared_ptr<ngraph::runtime::Tensor>& a,
                                             const std::shared_ptr<ngraph::runtime::Tensor>& b,
                                             T rtol = 1e-5f,
                                             T atol = 1e-8f)
Robert Kimball's avatar
Robert Kimball committed
111 112
        {
            // Check that the layouts are compatible
Scott Cyphers's avatar
Scott Cyphers committed
113
            if (*a->get_tensor_layout() != *b->get_tensor_layout())
Robert Kimball's avatar
Robert Kimball committed
114
            {
115 116
                return ::testing::AssertionFailure()
                       << "Cannot compare tensors with different layouts";
Robert Kimball's avatar
Robert Kimball committed
117
            }
Scott Cyphers's avatar
Scott Cyphers committed
118

Robert Kimball's avatar
Robert Kimball committed
119
            if (a->get_shape() != b->get_shape())
120
            {
121 122
                return ::testing::AssertionFailure()
                       << "Cannot compare tensors with different shapes";
123
            }
Scott Cyphers's avatar
Scott Cyphers committed
124

125
            return all_close(read_vector<T>(a), read_vector<T>(b), rtol, atol);
Robert Kimball's avatar
Robert Kimball committed
126
        }
Scott Cyphers's avatar
Scott Cyphers committed
127

128 129 130 131 132
        /// \brief Same as numpy.allclose
        /// \param as First tensors to compare
        /// \param bs Second tensors to compare
        /// \param rtol Relative tolerance
        /// \param atol Absolute tolerance
Robert Kimball's avatar
Robert Kimball committed
133
        /// Returns true if shapes match and for all elements, |a_i-b_i| <= atol + rtol*|b_i|.
134
        template <typename T>
135 136 137 138 139
        ::testing::AssertionResult
            all_close(const std::vector<std::shared_ptr<ngraph::runtime::Tensor>>& as,
                      const std::vector<std::shared_ptr<ngraph::runtime::Tensor>>& bs,
                      T rtol,
                      T atol)
Robert Kimball's avatar
Robert Kimball committed
140 141 142
        {
            if (as.size() != bs.size())
            {
143 144
                return ::testing::AssertionFailure()
                       << "Cannot compare tensors with different sizes";
Robert Kimball's avatar
Robert Kimball committed
145 146 147
            }
            for (size_t i = 0; i < as.size(); ++i)
            {
148 149
                auto ar = all_close(as[i], bs[i], rtol, atol);
                if (!ar)
Robert Kimball's avatar
Robert Kimball committed
150
                {
151
                    return ar;
Robert Kimball's avatar
Robert Kimball committed
152 153
                }
            }
154
            return ::testing::AssertionSuccess();
Robert Kimball's avatar
Robert Kimball committed
155
        }
Scott Cyphers's avatar
Scott Cyphers committed
156 157
    }
}