blob: 125045ee0f0aa1403fa1deaca8b95782e06f5918 [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"
Jeremy Johnson9a758382023-11-07 16:27:35 +000023namespace TosaReference
24{
25
26namespace
27{
Jeremy Johnson08965d32024-02-19 13:57:21 +000028template <typename OutType>
29double calcErrorBound(double referenceValue, double boundsValue, const void* cfgPtr)
Jeremy Johnson9a758382023-11-07 16:27:35 +000030{
Jeremy Johnson08965d32024-02-19 13:57:21 +000031 const auto cfg = reinterpret_cast<const AbsErrorVerifyInfo*>(cfgPtr);
Jeremy Johnson9a758382023-11-07 16:27:35 +000032
Jeremy Johnson08965d32024-02-19 13:57:21 +000033 double valBound = std::abs(referenceValue) * boundsValue;
34 if (cfg->lowerBound > 0)
Jeremy Johnson9a758382023-11-07 16:27:35 +000035 {
Jeremy Johnson08965d32024-02-19 13:57:21 +000036 valBound = std::max(cfg->lowerBound, valBound);
Jeremy Johnson9a758382023-11-07 16:27:35 +000037 }
Jerry Ge51bd4f52024-02-20 11:21:19 -080038 return exp2(-AccPrecision<OutType>::normal_frac / cfg->normalDivisor) * valBound;
Jeremy Johnson9a758382023-11-07 16:27:35 +000039}
40} // namespace
Jeremy Johnson08965d32024-02-19 13:57:21 +000041
42bool verifyAbsError(const CTensor* referenceTensor,
43 const CTensor* boundsTensor,
44 const CTensor* implementationTensor,
45 const AbsErrorVerifyInfo& aeInfo)
Jeremy Johnson9a758382023-11-07 16:27:35 +000046{
47 // Validate that tensors are provided
Jeremy Johnson08965d32024-02-19 13:57:21 +000048 TOSA_REF_REQUIRE(referenceTensor != nullptr, "[AE] Reference tensor is missing");
49 TOSA_REF_REQUIRE(boundsTensor != nullptr, "[AE] Reference bounds tensor is missing");
50 TOSA_REF_REQUIRE(implementationTensor != nullptr, "[AE] Implementation tensor is missing");
Jeremy Johnson9a758382023-11-07 16:27:35 +000051
Jeremy Johnson08965d32024-02-19 13:57:21 +000052 const std::vector<int32_t> refShape(referenceTensor->shape, referenceTensor->shape + referenceTensor->num_dims);
Jeremy Johnson9a758382023-11-07 16:27:35 +000053
Jeremy Johnson08965d32024-02-19 13:57:21 +000054 const double* refData = reinterpret_cast<const double*>(referenceTensor->data);
55 const double* refBndData = reinterpret_cast<const double*>(boundsTensor->data);
Jeremy Johnson9a758382023-11-07 16:27:35 +000056 TOSA_REF_REQUIRE(refData != nullptr && refBndData != nullptr, "[AE] Missing data for reference or bounds tensors");
57
Jeremy Johnson08965d32024-02-19 13:57:21 +000058 const std::string modeStr = "AE";
59
60 switch (implementationTensor->data_type)
Jeremy Johnson9a758382023-11-07 16:27:35 +000061 {
62 case tosa_datatype_fp32_t: {
Jeremy Johnson08965d32024-02-19 13:57:21 +000063 const auto* impData = reinterpret_cast<const float*>(implementationTensor->data);
Jeremy Johnson718f3472023-11-30 14:18:19 +000064 TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation");
Jeremy Johnson08965d32024-02-19 13:57:21 +000065 return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo, &calcErrorBound<float>);
Jeremy Johnson718f3472023-11-30 14:18:19 +000066 }
67 case tosa_datatype_fp16_t: {
Jeremy Johnson08965d32024-02-19 13:57:21 +000068 const auto* impData = reinterpret_cast<const half_float::half*>(implementationTensor->data);
Jeremy Johnson9a758382023-11-07 16:27:35 +000069 TOSA_REF_REQUIRE(impData != nullptr, "[AE] Missing data for implementation");
Jeremy Johnson08965d32024-02-19 13:57:21 +000070 return validateData(refData, refBndData, impData, refShape, modeStr, &aeInfo,
71 &calcErrorBound<half_float::half>);
Jeremy Johnson9a758382023-11-07 16:27:35 +000072 }
73 default:
74 WARNING("[Verifier][AE] Data-type not supported.");
75 break;
76 }
77
78 return false;
79}
80} // namespace TosaReference