all_close_f.hpp 11.6 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.
//*****************************************************************************
16 17 18 19 20 21

#pragma once

#include <memory>
#include <vector>

22
#include "gtest/gtest.h"
23 24
#include "test_tools.hpp"

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static constexpr int BFLOAT_MANTISSA_BITS = 8;
static constexpr int FLOAT_MANTISSA_BITS = 24;
static constexpr int DOUBLE_MANTISSA_BITS = 53;

// Maximum available float bits
#ifndef MAX_FLOAT_BITS
#define MAX_FLOAT_BITS FLOAT_MANTISSA_BITS
#endif

// Minimum float tolerance bits possible
#ifndef MIN_FLOAT_TOLERANCE_BITS
#define MIN_FLOAT_TOLERANCE_BITS (FLOAT_MANTISSA_BITS - MAX_FLOAT_BITS)
#endif

static_assert((MAX_FLOAT_BITS > 0) && (MAX_FLOAT_BITS <= FLOAT_MANTISSA_BITS),
              "MAX_FLOAT_BITS must be in range (0, 24]");
static_assert((MIN_FLOAT_TOLERANCE_BITS >= 0) && (MIN_FLOAT_TOLERANCE_BITS < FLOAT_MANTISSA_BITS),
              "MIN_FLOAT_TOLERANCE_BITS must be in range [0, 24)");

// Default float tolerance bits
#ifndef DEFAULT_FLOAT_TOLERANCE_BITS
#define DEFAULT_FLOAT_TOLERANCE_BITS (MIN_FLOAT_TOLERANCE_BITS + 2)
#endif

// Default float tolerance bits
#ifndef DEFAULT_DOUBLE_TOLERANCE_BITS
#define DEFAULT_DOUBLE_TOLERANCE_BITS 2
#endif

static_assert((DEFAULT_FLOAT_TOLERANCE_BITS >= 0) &&
                  (DEFAULT_FLOAT_TOLERANCE_BITS < FLOAT_MANTISSA_BITS),
              "DEFAULT_FLOAT_TOLERANCE_BITS must be in range [0, 24)");

static_assert((DEFAULT_DOUBLE_TOLERANCE_BITS >= 0) &&
                  (DEFAULT_DOUBLE_TOLERANCE_BITS < DOUBLE_MANTISSA_BITS),
              "DEFAULT_DOUBLE_TOLERANCE_BITS must be in range [0, 53)");

62 63 64 65
namespace ngraph
{
    namespace test
    {
66 67 68
        /// \brief Determine distance between two f32 numbers
        /// \param a First number to compare
        /// \param b Second number to compare
69
        /// \param min_signal Minimum value for comparisons
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        /// \returns Distance
        ///
        /// References:
        /// - https://en.wikipedia.org/wiki/Unit_in_the_last_place
        /// - https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2
        /// - https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#floating-point-comparison
        ///
        /// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
        /// |------------bfloat-----------|
        /// |----------------------------float----------------------------|
        ///
        /// bfloat (s1, e8, m7) has 7 + 1 = 8 bits of mantissa or bit_precision
        /// float (s1, e8, m23) has 23 + 1 = 24 bits of mantissa or bit_precision
        ///
        /// This function uses hard-coded value of 8 bit exponent_bits, so it's only valid for
        /// bfloat and f32.
86
        uint32_t float_distance(float a, float b, float min_signal = 0.0f);
87

88 89 90
        /// \brief Determine distance between two f64 numbers
        /// \param a First number to compare
        /// \param b Second number to compare
91
        /// \param min_signal Minimum value for comparisons
92 93 94 95 96 97 98 99 100 101 102 103 104
        /// \returns Distance
        ///
        /// References:
        /// - https://en.wikipedia.org/wiki/Unit_in_the_last_place
        /// - https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2
        /// - https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#floating-point-comparison
        ///
        /// s e e e e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m
        /// |----------------------------double-------------------------------------------------------------------------------------------|
        ///
        /// double (s1, e11, m52) has 52 + 1 = 53 bits of mantissa or bit_precision
        ///
        /// This function uses hard-coded value of 11 bit exponent_bits, so it's only valid for f64.
105
        uint64_t float_distance(double a, double b, double min_signal = 0.0);
106

107 108 109 110
        /// \brief Check if the two f32 numbers are close
        /// \param a First number to compare
        /// \param b Second number to compare
        /// \param tolerance_bits Bit tolerance error
111
        /// \param min_signal Minimum value for comparisons
112
        /// \returns True iff the distance between a and b is within 2 ^ tolerance_bits ULP
113 114 115 116
        ///
        /// References:
        /// - https://en.wikipedia.org/wiki/Unit_in_the_last_place
        /// - https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2
117
        /// - https://github.com/abseil/googletest/blob/master/googletest/docs/advanced.md#floating-point-comparison
118 119 120 121 122 123 124 125 126 127
        ///
        /// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
        /// |------------bfloat-----------|
        /// |----------------------------float----------------------------|
        ///
        /// bfloat (s1, e8, m7) has 7 + 1 = 8 bits of mantissa or bit_precision
        /// float (s1, e8, m23) has 23 + 1 = 24 bits of mantissa or bit_precision
        ///
        /// This function uses hard-coded value of 8 bit exponent_bits, so it's only valid for
        /// bfloat and f32.
128 129 130 131
        bool close_f(float a,
                     float b,
                     int tolerance_bits = DEFAULT_FLOAT_TOLERANCE_BITS,
                     float min_signal = 0.0f);
132

133 134 135 136
        /// \brief Check if the two f64 numbers are close
        /// \param a First number to compare
        /// \param b Second number to compare
        /// \param tolerance_bits Bit tolerance error
137
        /// \param min_signal Minimum value for comparisons
138 139 140 141 142 143 144 145 146 147 148 149 150
        /// \returns True iff the distance between a and b is within 2 ^ tolerance_bits ULP
        ///
        /// References:
        /// - https://en.wikipedia.org/wiki/Unit_in_the_last_place
        /// - https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2
        /// - https://github.com/abseil/googletest/blob/master/googletest/docs/advanced.md#floating-point-comparison
        ///
        /// s e e e e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m
        /// |----------------------------double-------------------------------------------------------------------------------------------|
        ///
        /// double (s1, e11, m52) has 52 + 1 = 53 bits of mantissa or bit_precision
        ///
        /// This function uses hard-coded value of 11 bit exponent_bits, so it's only valid for f64.
151 152 153 154
        bool close_f(double a,
                     double b,
                     int tolerance_bits = DEFAULT_DOUBLE_TOLERANCE_BITS,
                     double min_signal = 0.0);
155

156 157 158
        /// \brief Determine distances between two vectors of f32 numbers
        /// \param a Vector of floats to compare
        /// \param b Vector of floats to compare
159
        /// \param min_signal Minimum value for comparisons
160 161 162 163
        /// \returns Vector of distances
        ///
        /// See float_distance for limitations and assumptions.
        std::vector<uint32_t> float_distances(const std::vector<float>& a,
164 165
                                              const std::vector<float>& b,
                                              float min_signal = 0.0f);
166

167 168 169
        /// \brief Determine distances between two vectors of f64 numbers
        /// \param a Vector of doubles to compare
        /// \param b Vector of doubles to compare
170
        /// \param min_signal Minimum value for comparisons
171 172 173 174
        /// \returns Vector of distances
        ///
        /// See float_distance for limitations and assumptions.
        std::vector<uint64_t> float_distances(const std::vector<double>& a,
175 176
                                              const std::vector<double>& b,
                                              double min_signal = 0.0);
177

178 179 180 181 182 183 184
        /// \brief Determine number of matching mantissa bits given a distance
        /// \param distance Distance calculated by float_distance
        /// \returns Number of matching mantissa bits
        ///
        /// See float_distance for limitations and assumptions.
        uint32_t matching_mantissa_bits(uint32_t distance);

185 186 187 188 189
        /// \brief Determine number of matching mantissa bits given a distance
        /// \param distance Distance calculated by float_distance
        /// \returns Number of matching mantissa bits
        ///
        /// See float_distance for limitations and assumptions.
190
        uint32_t matching_mantissa_bits(uint64_t distance);
191

192 193 194 195
        /// \brief Check if the two floating point vectors are all close
        /// \param a First number to compare
        /// \param b Second number to compare
        /// \param tolerance_bits Bit tolerance error
196
        /// \param min_signal Minimum value for comparisons
197 198 199
        /// \returns ::testing::AssertionSuccess iff the two floating point vectors are close
        ::testing::AssertionResult all_close_f(const std::vector<float>& a,
                                               const std::vector<float>& b,
200 201
                                               int tolerance_bits = DEFAULT_FLOAT_TOLERANCE_BITS,
                                               float min_signal = 0.0f);
202

203 204 205 206
        /// \brief Check if the two double floating point vectors are all close
        /// \param a First number to compare
        /// \param b Second number to compare
        /// \param tolerance_bits Bit tolerance error
207
        /// \param min_signal Minimum value for comparisons
208 209 210
        /// \returns ::testing::AssertionSuccess iff the two floating point vectors are close
        ::testing::AssertionResult all_close_f(const std::vector<double>& a,
                                               const std::vector<double>& b,
211 212
                                               int tolerance_bits = DEFAULT_DOUBLE_TOLERANCE_BITS,
                                               double min_signal = 0.0);
213

214
        /// \brief Check if the two TensorViews are all close in float
215 216
        /// \param a First Tensor to compare
        /// \param b Second Tensor to compare
217
        /// \param tolerance_bits Bit tolerance error
218
        /// \param min_signal Minimum value for comparisons
219
        /// Returns true iff the two TensorViews are all close in float
220 221
        ::testing::AssertionResult all_close_f(const std::shared_ptr<runtime::Tensor>& a,
                                               const std::shared_ptr<runtime::Tensor>& b,
222 223
                                               int tolerance_bits = DEFAULT_FLOAT_TOLERANCE_BITS,
                                               float min_signal = 0.0f);
224

225
        /// \brief Check if the two vectors of TensorViews are all close in float
226 227
        /// \param as First vector of Tensor to compare
        /// \param bs Second vector of Tensor to compare
228
        /// \param tolerance_bits Bit tolerance error
229
        /// \param min_signal Minimum value for comparisons
230
        /// Returns true iff the two TensorViews are all close in float
231 232 233
        ::testing::AssertionResult
            all_close_f(const std::vector<std::shared_ptr<runtime::Tensor>>& as,
                        const std::vector<std::shared_ptr<runtime::Tensor>>& bs,
234 235
                        int tolerance_bits = DEFAULT_FLOAT_TOLERANCE_BITS,
                        float min_signal = 0.0f);
236 237
    }
}