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 | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 29 | template <typename OutDtype> |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame^] | 30 | bool validateData(const double* ref, |
| 31 | const double* bnd, |
| 32 | const OutDtype* imp, |
| 33 | const std::vector<int32_t>& shape, |
| 34 | const AbsErrorVerifyInfo& cfg) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 35 | { |
| 36 | const size_t T = static_cast<size_t>(numElements(shape)); |
| 37 | TOSA_REF_REQUIRE(T > 0, "[AE] Invalid shape for reference tensor"); |
| 38 | |
| 39 | for (size_t i = 0; i < T; ++i) |
| 40 | { |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame^] | 41 | double valBound = std::abs(ref[i]) * bnd[i]; |
| 42 | if (cfg.lowerBound > 0) |
| 43 | { |
| 44 | valBound = std::max(cfg.lowerBound, valBound); |
| 45 | } |
| 46 | double errBound = exp2(-AccPrecision<OutDtype>::normal_frac) * valBound; |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 47 | bool valid = tosaCheckFloatBound(imp[i], ref[i], errBound); |
| 48 | if (!valid) |
| 49 | { |
| 50 | auto pos = indexToPosition(T, shape); |
| 51 | WARNING("[Verifier][AE] Location %s", positionToString(pos).c_str()); |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | } // namespace |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame^] | 58 | bool verifyAbsError(const CTensor* ref, const CTensor* refBnd, const CTensor* imp, const AbsErrorVerifyInfo& aeInfo) |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 59 | { |
| 60 | // Validate that tensors are provided |
| 61 | TOSA_REF_REQUIRE(ref != nullptr, "[AE] Reference tensor is missing"); |
| 62 | TOSA_REF_REQUIRE(refBnd != nullptr, "[AE] Reference bounds tensor is missing"); |
| 63 | TOSA_REF_REQUIRE(imp != nullptr, "[AE] Implementation tensor is missing"); |
| 64 | |
| 65 | const std::vector<int32_t> refShape(ref->shape, ref->shape + ref->num_dims); |
| 66 | |
| 67 | const double* refData = reinterpret_cast<const double*>(ref->data); |
| 68 | const double* refBndData = reinterpret_cast<const double*>(refBnd->data); |
| 69 | TOSA_REF_REQUIRE(refData != nullptr && refBndData != nullptr, "[AE] Missing data for reference or bounds tensors"); |
| 70 | |
| 71 | switch (imp->data_type) |
| 72 | { |
| 73 | case tosa_datatype_fp32_t: { |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 74 | const auto* impData = reinterpret_cast<const float*>(imp->data); |
| 75 | TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation"); |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame^] | 76 | return validateData(refData, refBndData, impData, refShape, aeInfo); |
Jeremy Johnson | 718f347 | 2023-11-30 14:18:19 +0000 | [diff] [blame] | 77 | } |
| 78 | case tosa_datatype_fp16_t: { |
| 79 | const auto* impData = reinterpret_cast<const half_float::half*>(imp->data); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 80 | TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation"); |
Jeremy Johnson | d80ea5e | 2024-01-03 10:54:12 +0000 | [diff] [blame^] | 81 | return validateData(refData, refBndData, impData, refShape, aeInfo); |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 82 | } |
| 83 | default: |
| 84 | WARNING("[Verifier][AE] Data-type not supported."); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | } // namespace TosaReference |