Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [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> |
| 19 | #include <utility> |
| 20 | |
| 21 | #include "verifiers.h" |
| 22 | |
| 23 | namespace TosaReference |
| 24 | { |
| 25 | |
| 26 | namespace |
| 27 | { |
| 28 | bool validateData(const double* ref, const double* bnd, const float* imp, const std::vector<int32_t>& shape) |
| 29 | { |
| 30 | const size_t T = static_cast<size_t>(numElements(shape)); |
| 31 | TOSA_REF_REQUIRE(T > 0, "[AE] Invalid shape for reference tensor"); |
| 32 | |
| 33 | for (size_t i = 0; i < T; ++i) |
| 34 | { |
| 35 | double errBound = ref[i] * exp2(-AccPrecision<float>::normal_frac) * bnd[i]; |
| 36 | bool valid = tosaCheckFloatBound(imp[i], ref[i], errBound); |
| 37 | if (!valid) |
| 38 | { |
| 39 | auto pos = indexToPosition(T, shape); |
| 40 | WARNING("[Verifier][AE] Location %s", positionToString(pos).c_str()); |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | } // namespace |
| 47 | bool verifyAbsError(const CTensor* ref, const CTensor* refBnd, const CTensor* imp) |
| 48 | { |
| 49 | // Validate that tensors are provided |
| 50 | TOSA_REF_REQUIRE(ref != nullptr, "[AE] Reference tensor is missing"); |
| 51 | TOSA_REF_REQUIRE(refBnd != nullptr, "[AE] Reference bounds tensor is missing"); |
| 52 | TOSA_REF_REQUIRE(imp != nullptr, "[AE] Implementation tensor is missing"); |
| 53 | |
| 54 | const std::vector<int32_t> refShape(ref->shape, ref->shape + ref->num_dims); |
| 55 | |
| 56 | const double* refData = reinterpret_cast<const double*>(ref->data); |
| 57 | const double* refBndData = reinterpret_cast<const double*>(refBnd->data); |
| 58 | TOSA_REF_REQUIRE(refData != nullptr && refBndData != nullptr, "[AE] Missing data for reference or bounds tensors"); |
| 59 | |
| 60 | switch (imp->data_type) |
| 61 | { |
| 62 | case tosa_datatype_fp32_t: { |
| 63 | const float* impData = reinterpret_cast<const float*>(imp->data); |
| 64 | TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation"); |
| 65 | return validateData(refData, refBndData, impData, refShape); |
| 66 | } |
| 67 | default: |
| 68 | WARNING("[Verifier][AE] Data-type not supported."); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | } // namespace TosaReference |