Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame] | 1 | // Copyright (c) 2023-2024, ARM Limited. |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 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> |
| 19 | #include <utility> |
| 20 | |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 21 | #include "half.hpp" |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 22 | #include "verifiers.h" |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 23 | namespace TosaReference |
| 24 | { |
| 25 | |
| 26 | namespace |
| 27 | { |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 28 | template <typename OutType> |
| 29 | double calcErrorBound(double referenceValue, double boundsValue, const void* cfgPtr) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 30 | { |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 31 | const auto cfg = reinterpret_cast<const AbsErrorVerifyInfo*>(cfgPtr); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 32 | |
Jeremy Johnson | 1eb1455 | 2024-04-11 16:21:54 +0100 | [diff] [blame^] | 33 | double boundsMagnitude; |
| 34 | if (cfg->boundAsMagnitude) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 35 | { |
Jeremy Johnson | 1eb1455 | 2024-04-11 16:21:54 +0100 | [diff] [blame^] | 36 | // Special case for SIN/COS |
| 37 | // use the input value (stored in the bounds tensor) as the magnitude and value |
| 38 | boundsMagnitude = boundsValue; |
| 39 | boundsValue = std::abs(boundsValue); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | // Use the referenceValue as the magnitude |
| 44 | boundsMagnitude = referenceValue; |
| 45 | } |
| 46 | |
| 47 | double errorBound = 0.0; |
| 48 | if (std::isfinite(boundsValue) || std::abs(boundsMagnitude) != 0.0) |
| 49 | { |
| 50 | double valueBound = std::abs(boundsMagnitude) * (boundsValue + cfg->boundAddition); |
evacha01 | 9c96eef | 2024-02-07 11:21:55 +0000 | [diff] [blame] | 51 | if (cfg->lowerBound > 0) |
| 52 | { |
Jeremy Johnson | 1eb1455 | 2024-04-11 16:21:54 +0100 | [diff] [blame^] | 53 | valueBound = std::max(cfg->lowerBound, valueBound); |
evacha01 | 9c96eef | 2024-02-07 11:21:55 +0000 | [diff] [blame] | 54 | } |
Jeremy Johnson | 1eb1455 | 2024-04-11 16:21:54 +0100 | [diff] [blame^] | 55 | errorBound = exp2(-AccPrecision<OutType>::normal_frac / cfg->normalDivisor) * valueBound; |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 56 | } |
evacha01 | 9c96eef | 2024-02-07 11:21:55 +0000 | [diff] [blame] | 57 | return errorBound; |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 58 | } |
| 59 | } // namespace |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 60 | |
| 61 | bool verifyAbsError(const CTensor* referenceTensor, |
| 62 | const CTensor* boundsTensor, |
| 63 | const CTensor* implementationTensor, |
| 64 | const AbsErrorVerifyInfo& aeInfo) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 65 | { |
| 66 | // Validate that tensors are provided |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 67 | TOSA_REF_REQUIRE(referenceTensor != nullptr, "[AE] Reference tensor is missing"); |
| 68 | TOSA_REF_REQUIRE(boundsTensor != nullptr, "[AE] Reference bounds tensor is missing"); |
| 69 | TOSA_REF_REQUIRE(implementationTensor != nullptr, "[AE] Implementation tensor is missing"); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 70 | |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 71 | const std::vector<int32_t> refShape(referenceTensor->shape, referenceTensor->shape + referenceTensor->num_dims); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 72 | |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 73 | const double* refData = reinterpret_cast<const double*>(referenceTensor->data); |
| 74 | const double* refBndData = reinterpret_cast<const double*>(boundsTensor->data); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 75 | TOSA_REF_REQUIRE(refData != nullptr && refBndData != nullptr, "[AE] Missing data for reference or bounds tensors"); |
| 76 | |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 77 | const std::string modeStr = "AE"; |
| 78 | |
| 79 | switch (implementationTensor->data_type) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 80 | { |
| 81 | case tosa_datatype_fp32_t: { |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 82 | const auto* impData = reinterpret_cast<const float*>(implementationTensor->data); |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 83 | TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation"); |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 84 | return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo, &calcErrorBound<float>); |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 85 | } |
| 86 | case tosa_datatype_fp16_t: { |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 87 | const auto* impData = reinterpret_cast<const half_float::half*>(implementationTensor->data); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 88 | TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation"); |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 89 | return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo, |
| 90 | &calcErrorBound<half_float::half>); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 91 | } |
| 92 | default: |
| 93 | WARNING("[Verifier][AE] Data-type not supported."); |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | } // namespace TosaReference |