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 | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 33 | double valBound = std::abs(referenceValue) * boundsValue; |
| 34 | if (cfg->lowerBound > 0) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 35 | { |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 36 | valBound = std::max(cfg->lowerBound, valBound); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 37 | } |
Jerry Ge | 51bd4f5 | 2024-02-20 11:21:19 -0800 | [diff] [blame^] | 38 | return exp2(-AccPrecision<OutType>::normal_frac / cfg->normalDivisor) * valBound; |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 39 | } |
| 40 | } // namespace |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 41 | |
| 42 | bool verifyAbsError(const CTensor* referenceTensor, |
| 43 | const CTensor* boundsTensor, |
| 44 | const CTensor* implementationTensor, |
| 45 | const AbsErrorVerifyInfo& aeInfo) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 46 | { |
| 47 | // Validate that tensors are provided |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 48 | TOSA_REF_REQUIRE(referenceTensor != nullptr, "[AE] Reference tensor is missing"); |
| 49 | TOSA_REF_REQUIRE(boundsTensor != nullptr, "[AE] Reference bounds tensor is missing"); |
| 50 | TOSA_REF_REQUIRE(implementationTensor != nullptr, "[AE] Implementation tensor is missing"); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 51 | |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 52 | 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] | 53 | |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 54 | const double* refData = reinterpret_cast<const double*>(referenceTensor->data); |
| 55 | const double* refBndData = reinterpret_cast<const double*>(boundsTensor->data); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 56 | TOSA_REF_REQUIRE(refData != nullptr && refBndData != nullptr, "[AE] Missing data for reference or bounds tensors"); |
| 57 | |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 58 | const std::string modeStr = "AE"; |
| 59 | |
| 60 | switch (implementationTensor->data_type) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 61 | { |
| 62 | case tosa_datatype_fp32_t: { |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 63 | const auto* impData = reinterpret_cast<const float*>(implementationTensor->data); |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 64 | TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation"); |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 65 | return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo, &calcErrorBound<float>); |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 66 | } |
| 67 | case tosa_datatype_fp16_t: { |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 68 | const auto* impData = reinterpret_cast<const half_float::half*>(implementationTensor->data); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 69 | TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation"); |
Jeremy Johnson | 08965d3 | 2024-02-19 13:57:21 +0000 | [diff] [blame] | 70 | return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo, |
| 71 | &calcErrorBound<half_float::half>); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 72 | } |
| 73 | default: |
| 74 | WARNING("[Verifier][AE] Data-type not supported."); |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | } // namespace TosaReference |