blob: a7b7bc237c3c3e13b251608989af584000896dc5 [file] [log] [blame]
Jeremy Johnsond80ea5e2024-01-03 10:54:12 +00001// Copyright (c) 2023-2024, ARM Limited.
Jeremy Johnson9a758382023-11-07 16:27:35 +00002//
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 Johnson08965d32024-02-19 13:57:21 +000029template <typename OutType>
30double calcErrorBound(double referenceValue, double boundsValue, const void* cfgPtr)
Jeremy Johnson9a758382023-11-07 16:27:35 +000031{
Jeremy Johnson08965d32024-02-19 13:57:21 +000032 const auto cfg = reinterpret_cast<const AbsErrorVerifyInfo*>(cfgPtr);
Jeremy Johnson9a758382023-11-07 16:27:35 +000033
Jeremy Johnson08965d32024-02-19 13:57:21 +000034 double valBound = std::abs(referenceValue) * boundsValue;
35 if (cfg->lowerBound > 0)
Jeremy Johnson9a758382023-11-07 16:27:35 +000036 {
Jeremy Johnson08965d32024-02-19 13:57:21 +000037 valBound = std::max(cfg->lowerBound, valBound);
Jeremy Johnson9a758382023-11-07 16:27:35 +000038 }
Jeremy Johnson08965d32024-02-19 13:57:21 +000039 return exp2(-AccPrecision<OutType>::normal_frac) * valBound;
Jeremy Johnson9a758382023-11-07 16:27:35 +000040}
41} // namespace
Jeremy Johnson08965d32024-02-19 13:57:21 +000042
43bool verifyAbsError(const CTensor* referenceTensor,
44 const CTensor* boundsTensor,
45 const CTensor* implementationTensor,
46 const AbsErrorVerifyInfo& aeInfo)
Jeremy Johnson9a758382023-11-07 16:27:35 +000047{
48 // Validate that tensors are provided
Jeremy Johnson08965d32024-02-19 13:57:21 +000049 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 Johnson9a758382023-11-07 16:27:35 +000052
Jeremy Johnson08965d32024-02-19 13:57:21 +000053 const std::vector<int32_t> refShape(referenceTensor->shape, referenceTensor->shape + referenceTensor->num_dims);
Jeremy Johnson9a758382023-11-07 16:27:35 +000054
Jeremy Johnson08965d32024-02-19 13:57:21 +000055 const double* refData = reinterpret_cast<const double*>(referenceTensor->data);
56 const double* refBndData = reinterpret_cast<const double*>(boundsTensor->data);
Jeremy Johnson9a758382023-11-07 16:27:35 +000057 TOSA_REF_REQUIRE(refData != nullptr && refBndData != nullptr, "[AE] Missing data for reference or bounds tensors");
58
Jeremy Johnson08965d32024-02-19 13:57:21 +000059 const std::string modeStr = "AE";
60
61 switch (implementationTensor->data_type)
Jeremy Johnson9a758382023-11-07 16:27:35 +000062 {
63 case tosa_datatype_fp32_t: {
Jeremy Johnson08965d32024-02-19 13:57:21 +000064 const auto* impData = reinterpret_cast<const float*>(implementationTensor->data);
Jeremy Johnson718f3472023-11-30 14:18:19 +000065 TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation");
Jeremy Johnson08965d32024-02-19 13:57:21 +000066 return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo, &calcErrorBound<float>);
Jeremy Johnson718f3472023-11-30 14:18:19 +000067 }
68 case tosa_datatype_fp16_t: {
Jeremy Johnson08965d32024-02-19 13:57:21 +000069 const auto* impData = reinterpret_cast<const half_float::half*>(implementationTensor->data);
Jeremy Johnson9a758382023-11-07 16:27:35 +000070 TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation");
Jeremy Johnson08965d32024-02-19 13:57:21 +000071 return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo,
72 &calcErrorBound<half_float::half>);
Jeremy Johnson9a758382023-11-07 16:27:35 +000073 }
74 default:
75 WARNING("[Verifier][AE] Data-type not supported.");
76 break;
77 }
78
79 return false;
80}
81} // namespace TosaReference