blob: 5aaa0ad9e8e5139f49cc94f91e9859accd56d36a [file] [log] [blame]
Jeremy Johnson9a758382023-11-07 16:27:35 +00001// 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
Jeremy Johnson718f3472023-11-30 14:18:19 +000021#include "half.hpp"
Jeremy Johnson9a758382023-11-07 16:27:35 +000022#include "verifiers.h"
23
24namespace TosaReference
25{
26
27namespace
28{
Jeremy Johnson718f3472023-11-30 14:18:19 +000029template <typename OutDtype>
30bool validateData(const double* ref, const double* bnd, const OutDtype* imp, const std::vector<int32_t>& shape)
Jeremy Johnson9a758382023-11-07 16:27:35 +000031{
32 const size_t T = static_cast<size_t>(numElements(shape));
33 TOSA_REF_REQUIRE(T > 0, "[AE] Invalid shape for reference tensor");
34
35 for (size_t i = 0; i < T; ++i)
36 {
Jeremy Johnson718f3472023-11-30 14:18:19 +000037 double errBound = std::abs(ref[i]) * exp2(-AccPrecision<OutDtype>::normal_frac) * bnd[i];
Jeremy Johnson9a758382023-11-07 16:27:35 +000038 bool valid = tosaCheckFloatBound(imp[i], ref[i], errBound);
39 if (!valid)
40 {
41 auto pos = indexToPosition(T, shape);
42 WARNING("[Verifier][AE] Location %s", positionToString(pos).c_str());
43 return false;
44 }
45 }
46 return true;
47}
48} // namespace
49bool verifyAbsError(const CTensor* ref, const CTensor* refBnd, const CTensor* imp)
50{
51 // Validate that tensors are provided
52 TOSA_REF_REQUIRE(ref != nullptr, "[AE] Reference tensor is missing");
53 TOSA_REF_REQUIRE(refBnd != nullptr, "[AE] Reference bounds tensor is missing");
54 TOSA_REF_REQUIRE(imp != nullptr, "[AE] Implementation tensor is missing");
55
56 const std::vector<int32_t> refShape(ref->shape, ref->shape + ref->num_dims);
57
58 const double* refData = reinterpret_cast<const double*>(ref->data);
59 const double* refBndData = reinterpret_cast<const double*>(refBnd->data);
60 TOSA_REF_REQUIRE(refData != nullptr && refBndData != nullptr, "[AE] Missing data for reference or bounds tensors");
61
62 switch (imp->data_type)
63 {
64 case tosa_datatype_fp32_t: {
Jeremy Johnson718f3472023-11-30 14:18:19 +000065 const auto* impData = reinterpret_cast<const float*>(imp->data);
66 TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation");
67 return validateData(refData, refBndData, impData, refShape);
68 }
69 case tosa_datatype_fp16_t: {
70 const auto* impData = reinterpret_cast<const half_float::half*>(imp->data);
Jeremy Johnson9a758382023-11-07 16:27:35 +000071 TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation");
72 return validateData(refData, refBndData, impData, refShape);
73 }
74 default:
75 WARNING("[Verifier][AE] Data-type not supported.");
76 break;
77 }
78
79 return false;
80}
81} // namespace TosaReference