Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 1 | // Copyright (c) 2023, ARM Limited. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <cmath> |
| 16 | #include <limits> |
| 17 | #include <memory> |
| 18 | #include <type_traits> |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 19 | #include <utility> |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 20 | |
| 21 | #include "verifiers.h" |
| 22 | |
| 23 | namespace TosaReference |
| 24 | { |
| 25 | |
| 26 | namespace |
| 27 | { |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 28 | bool tosaCheckULP(float testValue, double referenceValue, double ulpNum) |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 29 | { |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 30 | double errorBound = 0.0; |
| 31 | if (std::isfinite(referenceValue) && std::abs(referenceValue) != 0.0) |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 32 | { |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 33 | // Find the exponent of the reference value. |
Jeremy Johnson | 0bbd8bc | 2023-11-09 16:56:07 +0000 | [diff] [blame^] | 34 | int32_t referenceExponent = ilog2(std::abs(referenceValue)); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 35 | |
| 36 | // Work out the values magnitude - by raising 2 to the power of the |
| 37 | // exponent and taking the normalized minimum for denormal values |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 38 | const double referencePower2 = std::max(exp2(referenceExponent), AccPrecision<float>::normal_min); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 39 | // Get the value of changing the last bit - by shifting the least significant bit to this magnitude |
| 40 | // i.e. the ULP. |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 41 | double ulpValue = referencePower2 * exp2(-AccPrecision<float>::normal_frac); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 42 | |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 43 | errorBound = ulpValue * ulpNum; |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 44 | } |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 45 | return tosaCheckFloatBound(testValue, referenceValue, errorBound); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 46 | } |
| 47 | } // namespace |
| 48 | |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 49 | bool verifyULP(const CTensor* referenceTensor, const CTensor* implementationTensor, const UlpInfo& ulpInfo) |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 50 | { |
| 51 | // Validate that tensors are provided |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 52 | TOSA_REF_REQUIRE(referenceTensor != nullptr, "[ULP] Reference tensor is missing"); |
| 53 | TOSA_REF_REQUIRE(implementationTensor != nullptr, "[ULP] Implementation tensor is missing"); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 54 | |
| 55 | // Get number of elements |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 56 | const std::vector<int32_t> refShape(referenceTensor->shape, referenceTensor->shape + referenceTensor->num_dims); |
| 57 | const auto elementCount = numElements(refShape); |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 58 | TOSA_REF_REQUIRE(elementCount > 0, "[ULP] Invalid shape for reference tensor"); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 59 | |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 60 | const double ulp = ulpInfo.ulp; |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 61 | switch (implementationTensor->data_type) |
| 62 | { |
| 63 | case tosa_datatype_fp32_t: { |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 64 | const auto* refData = reinterpret_cast<const double*>(referenceTensor->data); |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 65 | TOSA_REF_REQUIRE(refData != nullptr, "[ULP] Missing data for reference"); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 66 | const auto* impData = reinterpret_cast<const float*>(implementationTensor->data); |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 67 | TOSA_REF_REQUIRE(impData != nullptr, "[ULP] Missing data for implementation"); |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 68 | const auto* refDataEnd = std::next(refData, elementCount); |
| 69 | // Use mismatch to get the location of the first unequal value |
| 70 | auto pair = std::mismatch(refData, refDataEnd, impData, std::next(impData, elementCount), |
| 71 | [ulp](const auto& referenceValue, const auto& implementationValue) { |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 72 | return tosaCheckULP(implementationValue, referenceValue, ulp); |
Jeremy Johnson | 2d70ac4 | 2023-11-06 17:46:02 +0000 | [diff] [blame] | 73 | }); |
| 74 | if (std::get<0>(pair) == refDataEnd) |
| 75 | { |
| 76 | // No mismatch found |
| 77 | return true; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | auto pos = indexToPosition(std::get<0>(pair) - refData, refShape); |
| 82 | WARNING("[Verfier][ULP] Location %s", positionToString(pos).c_str()); |
| 83 | return false; |
| 84 | } |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 85 | } |
| 86 | default: |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 87 | WARNING("[Verifier][ULP] Data-type not supported."); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | } // namespace TosaReference |