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