Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 1 | |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 2 | // Copyright (c) 2023-2024, ARM Limited. |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #ifndef VERIFY_UTILS_H_ |
| 17 | #define VERIFY_UTILS_H_ |
| 18 | |
| 19 | #include "dtype.h" |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 20 | #include "half.hpp" |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 21 | #include "types.h" |
| 22 | |
| 23 | #include <cstdint> |
| 24 | #include <optional> |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 25 | #include <string> |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 28 | #define TOSA_REF_REQUIRE(COND, MESSAGE, ...) \ |
Jack Frankland | aafc850 | 2023-09-13 11:03:50 +0100 | [diff] [blame] | 29 | if (!(COND)) \ |
| 30 | { \ |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 31 | WARNING("[Verifier]" MESSAGE ".", ##__VA_ARGS__); \ |
Jack Frankland | aafc850 | 2023-09-13 11:03:50 +0100 | [diff] [blame] | 32 | return false; \ |
| 33 | } |
| 34 | |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 35 | namespace TosaReference |
| 36 | { |
| 37 | |
| 38 | // Name alias |
| 39 | using CTensor = tosa_tensor_t; |
| 40 | |
| 41 | /// \brief Supported verification modes |
| 42 | enum class VerifyMode |
| 43 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 44 | Unknown, |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 45 | Exact, |
| 46 | Ulp, |
| 47 | DotProduct, |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 48 | FpSpecial, |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 49 | ReduceProduct, |
Jeremy Johnson | 32d0b5a | 2024-02-01 15:54:07 +0000 | [diff] [blame] | 50 | AbsError, |
| 51 | Relative |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /// \brief ULP verification meta-data |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 55 | struct UlpVerifyInfo |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 56 | { |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 57 | UlpVerifyInfo() = default; |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 58 | |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 59 | double ulp; |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /// \brief Dot-product verification meta-data |
| 63 | struct DotProductVerifyInfo |
| 64 | { |
| 65 | DotProductVerifyInfo() = default; |
| 66 | |
Jeremy Johnson | b2d3bff | 2024-02-26 16:08:07 +0000 | [diff] [blame] | 67 | int32_t setNumber; |
| 68 | int32_t kernelSize; |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
Jack Frankland | 12ee1a7 | 2023-09-20 09:08:34 +0100 | [diff] [blame] | 71 | /// \brief reduce-product verification meta-data |
| 72 | struct ReduceProductVerifyInfo |
| 73 | { |
| 74 | ReduceProductVerifyInfo() = default; |
| 75 | |
Jeremy Johnson | b2d3bff | 2024-02-26 16:08:07 +0000 | [diff] [blame] | 76 | int64_t numberOfProducts; |
Jack Frankland | 12ee1a7 | 2023-09-20 09:08:34 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 79 | /// \brief abs-error verification meta-data |
| 80 | struct AbsErrorVerifyInfo |
| 81 | { |
| 82 | AbsErrorVerifyInfo() = default; |
| 83 | |
| 84 | double lowerBound; |
Jerry Ge | 51bd4f5 | 2024-02-20 11:21:19 -0800 | [diff] [blame] | 85 | double normalDivisor; |
Jeremy Johnson | 1eb1455 | 2024-04-11 16:21:54 +0100 | [diff] [blame^] | 86 | bool boundAsMagnitude; |
| 87 | double boundAddition; |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Jeremy Johnson | 32d0b5a | 2024-02-01 15:54:07 +0000 | [diff] [blame] | 90 | /// \brief relative verification meta-data |
| 91 | struct RelativeVerifyInfo |
| 92 | { |
| 93 | RelativeVerifyInfo() = default; |
| 94 | |
| 95 | double max; |
| 96 | double scale; |
| 97 | }; |
| 98 | |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 99 | /// \brief Verification meta-data |
| 100 | struct VerifyConfig |
| 101 | { |
| 102 | VerifyConfig() = default; |
| 103 | |
| 104 | VerifyMode mode; |
Jeremy Johnson | bb0935f | 2023-09-14 16:43:48 +0100 | [diff] [blame] | 105 | DType dataType; |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 106 | UlpVerifyInfo ulpInfo; |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 107 | DotProductVerifyInfo dotProductInfo; |
Jack Frankland | 12ee1a7 | 2023-09-20 09:08:34 +0100 | [diff] [blame] | 108 | ReduceProductVerifyInfo reduceProductInfo; |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 109 | AbsErrorVerifyInfo absErrorInfo; |
Jeremy Johnson | 32d0b5a | 2024-02-01 15:54:07 +0000 | [diff] [blame] | 110 | RelativeVerifyInfo relativeInfo; |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /// \brief Parse the verification config for a tensor when given in JSON form |
| 114 | std::optional<VerifyConfig> parseVerifyConfig(const char* tensorName, const char* configJson); |
| 115 | |
| 116 | /// \brief Extract number of total elements |
| 117 | int64_t numElements(const std::vector<int32_t>& shape); |
| 118 | |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 119 | /// \brief Convert a flat index to a shape position |
| 120 | std::vector<int32_t> indexToPosition(int64_t index, const std::vector<int32_t>& shape); |
| 121 | |
| 122 | /// \brief A string representing the shape or position |
| 123 | std::string positionToString(const std::vector<int32_t>& pos); |
| 124 | |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 125 | /// \brief Map API data-type to DType |
| 126 | DType mapToDType(tosa_datatype_t dataType); |
| 127 | |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 128 | /// \brief Return 2 to the power of N or -N |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 129 | // For use during compile time - as no range check |
| 130 | constexpr double const_exp2(int32_t n) |
| 131 | { |
| 132 | double v = 1.0; |
| 133 | while (n > 0) |
| 134 | { |
| 135 | v = v * 2.0; |
| 136 | n--; |
| 137 | } |
| 138 | while (n < 0) |
| 139 | { |
| 140 | v = v / 2.0; |
| 141 | n++; |
| 142 | } |
| 143 | return v; |
| 144 | } |
| 145 | |
| 146 | /// \brief Same as const_exp2 but with runtime range check of N |
| 147 | double exp2(int32_t n); |
| 148 | |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 149 | /// \brief Return the base-2 exponent of V |
| 150 | int32_t ilog2(double v); |
| 151 | |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 152 | /// \brief Accuracy precision information |
| 153 | template <typename T> |
| 154 | struct AccPrecision; |
| 155 | template <> |
| 156 | struct AccPrecision<float> |
| 157 | { |
| 158 | static constexpr double normal_min = const_exp2(-126); |
| 159 | static constexpr double normal_max = const_exp2(128) - const_exp2(127 - 23); |
| 160 | static constexpr int32_t normal_frac = 23; |
| 161 | }; |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 162 | template <> |
| 163 | struct AccPrecision<half_float::half> |
| 164 | { |
| 165 | static constexpr double normal_min = const_exp2(-14); |
| 166 | static constexpr double normal_max = const_exp2(16) - const_exp2(15 - 10); |
| 167 | static constexpr int32_t normal_frac = 7; |
| 168 | }; |
Jeremy Johnson | d1a08ce | 2023-10-18 17:22:21 +0100 | [diff] [blame] | 169 | |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 170 | /// \brief Single value error bounds check for ULP, ABS_ERROR and other compliance modes |
| 171 | /// |
| 172 | /// \param testValue Implementation value |
| 173 | /// \param referenceValue Reference value |
| 174 | /// \param errorBound Positive error bound value |
| 175 | /// \param resultDifference Return: Difference between reference value and implementation value |
| 176 | /// \param resultWarning Return: Warning message if implementation is outside error bounds |
| 177 | /// |
| 178 | /// \return True if compliant else false |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 179 | template <typename OutType> |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 180 | bool tosaCheckFloatBound( |
| 181 | OutType testValue, double referenceValue, double errorBound, double& resultDifference, std::string& resultWarning); |
| 182 | |
| 183 | /// \brief Whole tensor checker for values inside error bounds |
| 184 | /// |
| 185 | /// \param referenceData Reference output tensor data |
| 186 | /// \param boundsData Optional reference bounds tensor data |
| 187 | /// \param implementationData Implementation output tensor data |
| 188 | /// \param shape Tensor shape - all tensors must be this shape |
| 189 | /// \param modeStr Short string indicating which compliance mode we are testing |
| 190 | /// \param cfgPtr Pointer to this mode's configuration data, passed to the calcErrorBound() |
| 191 | /// \param calcErrorBound Pointer to a function that can calculate the error bound per ref value |
| 192 | /// |
| 193 | /// \return True if compliant else false |
| 194 | template <typename OutType> |
| 195 | bool validateData(const double* referenceData, |
| 196 | const double* boundsData, |
| 197 | const OutType* implementationData, |
| 198 | const std::vector<int32_t>& shape, |
| 199 | const std::string& modeStr, |
| 200 | const void* cfgPtr, |
| 201 | double (*calcErrorBound)(double referenceValue, double boundsValue, const void* cfgPtr)); |
| 202 | |
| 203 | // Unused arguments helper function |
| 204 | template <typename... Args> |
| 205 | inline void unused(Args&&...) |
| 206 | {} |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 207 | }; // namespace TosaReference |
| 208 | |
| 209 | #endif // VERIFY_UTILS_H_ |