telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 7 | #include <armnn/Tensor.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 9 | #include <QuantizeHelper.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 11 | #include <boost/assert.hpp> |
| 12 | #include <boost/multi_array.hpp> |
| 13 | #include <boost/numeric/conversion/cast.hpp> |
| 14 | #include <boost/random/uniform_real_distribution.hpp> |
| 15 | #include <boost/random/mersenne_twister.hpp> |
| 16 | #include <boost/test/tools/floating_point_comparison.hpp> |
| 17 | #include <boost/test/unit_test.hpp> |
| 18 | |
| 19 | #include <array> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | #include <cmath> |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 21 | #include <vector> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 23 | constexpr float g_FloatCloseToZeroTolerance = 1.0e-6f; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | |
| 25 | template<typename T, bool isQuantized = true> |
| 26 | struct SelectiveComparer |
| 27 | { |
| 28 | static bool Compare(T a, T b) |
| 29 | { |
| 30 | return (std::max(a, b) - std::min(a, b)) <= 1; |
| 31 | } |
| 32 | |
| 33 | }; |
| 34 | |
| 35 | template<typename T> |
| 36 | struct SelectiveComparer<T, false> |
| 37 | { |
| 38 | static bool Compare(T a, T b) |
| 39 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 40 | // If a or b is zero, percent_tolerance does an exact match, so compare to a small, constant tolerance instead. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | if (a == 0.0f || b == 0.0f) |
| 42 | { |
| 43 | return std::abs(a - b) <= g_FloatCloseToZeroTolerance; |
| 44 | } |
Francis Murtagh | 8c5e3dc | 2018-08-30 17:18:37 +0100 | [diff] [blame] | 45 | |
| 46 | if (std::isinf(a) && a == b) |
| 47 | { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | if (std::isnan(a) && std::isnan(b)) |
| 52 | { |
| 53 | return true; |
| 54 | } |
| 55 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | // For unquantized floats we use a tolerance of 1%. |
| 57 | boost::math::fpc::close_at_tolerance<float> comparer(boost::math::fpc::percent_tolerance(1.0f)); |
| 58 | return comparer(a, b); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | template<typename T> |
| 63 | bool SelectiveCompare(T a, T b) |
| 64 | { |
| 65 | return SelectiveComparer<T, armnn::IsQuantizedType<T>()>::Compare(a, b); |
| 66 | }; |
| 67 | |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 68 | template<typename T> |
| 69 | bool SelectiveCompareBoolean(T a, T b) |
| 70 | { |
| 71 | return (((a == 0) && (b == 0)) || ((a != 0) && (b != 0))); |
| 72 | }; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | |
| 74 | template <typename T, std::size_t n> |
| 75 | boost::test_tools::predicate_result CompareTensors(const boost::multi_array<T, n>& a, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 76 | const boost::multi_array<T, n>& b, |
| 77 | bool compareBoolean = false) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 78 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 79 | // Checks they are same shape. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | for (unsigned int i=0; i<n; i++) |
| 81 | { |
| 82 | if (a.shape()[i] != b.shape()[i]) |
| 83 | { |
| 84 | boost::test_tools::predicate_result res(false); |
| 85 | res.message() << "Different shapes [" |
| 86 | << a.shape()[i] |
| 87 | << "!=" |
| 88 | << b.shape()[i] |
| 89 | << "]"; |
| 90 | return res; |
| 91 | } |
| 92 | } |
| 93 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | // Now compares element-wise. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 95 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 96 | // Fun iteration over n dimensions. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | std::array<unsigned int, n> indices; |
| 98 | for (unsigned int i = 0; i < n; i++) |
| 99 | { |
| 100 | indices[i] = 0; |
| 101 | } |
| 102 | |
| 103 | std::stringstream errorString; |
| 104 | int numFailedElements = 0; |
| 105 | constexpr int maxReportedDifferences = 3; |
| 106 | |
| 107 | while (true) |
| 108 | { |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 109 | bool comparison; |
| 110 | // As true for uint8_t is non-zero (1-255) we must have a dedicated compare for Booleans. |
| 111 | if(compareBoolean) |
| 112 | { |
| 113 | comparison = SelectiveCompareBoolean(a(indices), b(indices)); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | comparison = SelectiveCompare(a(indices), b(indices)); |
| 118 | } |
| 119 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 120 | if (!comparison) |
| 121 | { |
| 122 | ++numFailedElements; |
| 123 | |
| 124 | if (numFailedElements <= maxReportedDifferences) |
| 125 | { |
| 126 | if (numFailedElements >= 2) |
| 127 | { |
| 128 | errorString << ", "; |
| 129 | } |
| 130 | errorString << "["; |
| 131 | for (unsigned int i = 0; i < n; ++i) |
| 132 | { |
| 133 | errorString << indices[i]; |
| 134 | if (i != n - 1) |
| 135 | { |
| 136 | errorString << ","; |
| 137 | } |
| 138 | } |
| 139 | errorString << "]"; |
| 140 | |
| 141 | errorString << " (" << +a(indices) << " != " << +b(indices) << ")"; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | ++indices[n - 1]; |
| 146 | for (unsigned int i=n-1; i>0; i--) |
| 147 | { |
| 148 | if (indices[i] == a.shape()[i]) |
| 149 | { |
| 150 | indices[i] = 0; |
| 151 | ++indices[i - 1]; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (indices[0] == a.shape()[0]) |
| 156 | { |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | boost::test_tools::predicate_result comparisonResult(true); |
| 162 | if (numFailedElements > 0) |
| 163 | { |
| 164 | comparisonResult = false; |
| 165 | comparisonResult.message() << numFailedElements << " different values at: "; |
| 166 | if (numFailedElements > maxReportedDifferences) |
| 167 | { |
| 168 | errorString << ", ... (and " << (numFailedElements - maxReportedDifferences) << " other differences)"; |
| 169 | } |
| 170 | comparisonResult.message() << errorString.str(); |
| 171 | } |
| 172 | |
| 173 | return comparisonResult; |
| 174 | } |
| 175 | |
| 176 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 177 | // Creates a boost::multi_array with the shape defined by the given TensorInfo. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 178 | template <typename T, std::size_t n> |
| 179 | boost::multi_array<T, n> MakeTensor(const armnn::TensorInfo& tensorInfo) |
| 180 | { |
| 181 | std::array<unsigned int, n> shape; |
| 182 | |
| 183 | for (unsigned int i = 0; i < n; i++) |
| 184 | { |
| 185 | shape[i] = tensorInfo.GetShape()[i]; |
| 186 | } |
| 187 | |
| 188 | return boost::multi_array<T, n>(shape); |
| 189 | } |
| 190 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 191 | // Creates a boost::multi_array with the shape defined by the given TensorInfo and contents defined by the given vector. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 192 | template <typename T, std::size_t n> |
| 193 | boost::multi_array<T, n> MakeTensor(const armnn::TensorInfo& tensorInfo, const std::vector<T>& flat) |
| 194 | { |
| 195 | BOOST_ASSERT_MSG(flat.size() == tensorInfo.GetNumElements(), "Wrong number of components supplied to tensor"); |
| 196 | |
| 197 | std::array<unsigned int, n> shape; |
| 198 | |
Aron Virginas-Tar | e74e554 | 2019-02-21 14:57:08 +0000 | [diff] [blame] | 199 | // NOTE: tensorInfo.GetNumDimensions() might be different from n |
| 200 | const unsigned int returnDimensions = static_cast<unsigned int>(n); |
| 201 | const unsigned int actualDimensions = tensorInfo.GetNumDimensions(); |
| 202 | |
| 203 | const unsigned int paddedDimensions = |
| 204 | returnDimensions > actualDimensions ? returnDimensions - actualDimensions : 0u; |
| 205 | |
| 206 | for (unsigned int i = 0u; i < returnDimensions; i++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 207 | { |
Aron Virginas-Tar | e74e554 | 2019-02-21 14:57:08 +0000 | [diff] [blame] | 208 | if (i < paddedDimensions) |
| 209 | { |
| 210 | shape[i] = 1u; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | shape[i] = tensorInfo.GetShape()[i - paddedDimensions]; |
| 215 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | boost::const_multi_array_ref<T, n> arrayRef(&flat[0], shape); |
| 219 | return boost::multi_array<T, n>(arrayRef); |
| 220 | } |
| 221 | |
| 222 | template <typename T, std::size_t n> |
| 223 | boost::multi_array<T, n> MakeRandomTensor(const armnn::TensorInfo& tensorInfo, |
| 224 | unsigned int seed, |
| 225 | float min = -10.0f, |
| 226 | float max = 10.0f) |
| 227 | { |
| 228 | boost::random::mt19937 gen(seed); |
| 229 | boost::random::uniform_real_distribution<float> dist(min, max); |
| 230 | |
| 231 | std::vector<float> init(tensorInfo.GetNumElements()); |
| 232 | for (unsigned int i = 0; i < init.size(); i++) |
| 233 | { |
| 234 | init[i] = dist(gen); |
| 235 | } |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 236 | |
| 237 | const float qScale = tensorInfo.GetQuantizationScale(); |
| 238 | const int32_t qOffset = tensorInfo.GetQuantizationOffset(); |
| 239 | |
| 240 | return MakeTensor<T, n>(tensorInfo, armnnUtils::QuantizedVector<T>(init, qScale, qOffset)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 241 | } |