diff options
author | Jeremy Johnson <jeremy.johnson@arm.com> | 2023-11-06 17:46:02 +0000 |
---|---|---|
committer | Eric Kunze <eric.kunze@arm.com> | 2023-11-16 21:24:23 +0000 |
commit | 2d70ac4c02808609feb357488dcd080bd6fc5ba5 (patch) | |
tree | 7038f213a79b4d2daa5cfcf35e7f1fec54218f3e /reference_model/src/verify | |
parent | a4e5139312fbcbaedc998bfde6a0bb3479a388e6 (diff) | |
download | reference_model-2d70ac4c02808609feb357488dcd080bd6fc5ba5.tar.gz |
Main Compliance testing for simple UNARY ops
For RECIPROCAL, RSQRT, CEIL, FLOOR, ABS, NEGATE & IDENTITY.
Improved ULP informational output.
Signed-off-by: Jeremy Johnson <jeremy.johnson@arm.com>
Change-Id: I49644573b4c9a30b2b9d6c9624f2a1d46976a378
Diffstat (limited to 'reference_model/src/verify')
-rw-r--r-- | reference_model/src/verify/verify_ulp.cc | 37 | ||||
-rw-r--r-- | reference_model/src/verify/verify_utils.cc | 26 | ||||
-rw-r--r-- | reference_model/src/verify/verify_utils.h | 6 |
3 files changed, 60 insertions, 9 deletions
diff --git a/reference_model/src/verify/verify_ulp.cc b/reference_model/src/verify/verify_ulp.cc index 8c27191..2af0012 100644 --- a/reference_model/src/verify/verify_ulp.cc +++ b/reference_model/src/verify/verify_ulp.cc @@ -16,6 +16,7 @@ #include <limits> #include <memory> #include <type_traits> +#include <utility> #include "verifiers.h" @@ -36,9 +37,14 @@ bool tosaCheckULP(double referenceValue, float testValue, double ulpNum) // Start by sanitizing the input. - // The concept of ULP isn't defined for NaN's + // Both must be NaNs to be correct if (std::isnan(referenceValue) || std::isnan(testValue)) { + if (std::isnan(referenceValue) && std::isnan(testValue)) + { + return true; + } + WARNING("[Verfier][ULP] Non-matching NaN values - ref (%10f) versus test (%10f).", referenceValue, testValue); return false; } @@ -112,8 +118,8 @@ bool tosaCheckULP(double referenceValue, float testValue, double ulpNum) bool withinUlp = testValue64 >= referenceMin && testValue64 <= referenceMax; if (!withinUlp) { - WARNING("[Verfier][ULP] value (%10f) is not in ULP %g range (%10f <= ref (%10f) <= %10f).", testValue64, ulpNum, - referenceMin, referenceValue, referenceMax); + WARNING("[Verfier][ULP] value (%10.10f) is not in ULP %g range (%10.10f <= ref (%10.10f) <= %10.10f).", + testValue64, ulpNum, referenceMin, referenceValue, referenceMax); } return withinUlp; } @@ -126,8 +132,8 @@ bool verifyULP(const CTensor* referenceTensor, const CTensor* implementationTens TOSA_REF_REQUIRE(implementationTensor != nullptr, "[ULP] Implementation tensor is missing"); // Get number of elements - const auto elementCount = - numElements(std::vector<int32_t>(referenceTensor->shape, referenceTensor->shape + referenceTensor->num_dims)); + const std::vector<int32_t> refShape(referenceTensor->shape, referenceTensor->shape + referenceTensor->num_dims); + const auto elementCount = numElements(refShape); TOSA_REF_REQUIRE(elementCount > 0, "[ULP] Invalid shape for reference tensor"); const double ulp = ulpInfo.ulp; @@ -138,10 +144,23 @@ bool verifyULP(const CTensor* referenceTensor, const CTensor* implementationTens TOSA_REF_REQUIRE(refData != nullptr, "[ULP] Missing data for reference"); const auto* impData = reinterpret_cast<const float*>(implementationTensor->data); TOSA_REF_REQUIRE(impData != nullptr, "[ULP] Missing data for implementation"); - return std::equal(refData, std::next(refData, elementCount), impData, std::next(impData, elementCount), - [ulp](const auto& referenceValue, const auto& implementationValue) { - return tosaCheckULP(referenceValue, implementationValue, ulp); - }); + const auto* refDataEnd = std::next(refData, elementCount); + // Use mismatch to get the location of the first unequal value + auto pair = std::mismatch(refData, refDataEnd, impData, std::next(impData, elementCount), + [ulp](const auto& referenceValue, const auto& implementationValue) { + return tosaCheckULP(referenceValue, implementationValue, ulp); + }); + if (std::get<0>(pair) == refDataEnd) + { + // No mismatch found + return true; + } + else + { + auto pos = indexToPosition(std::get<0>(pair) - refData, refShape); + WARNING("[Verfier][ULP] Location %s", positionToString(pos).c_str()); + return false; + } } default: WARNING("[Verifier][ULP] Data-type not supported."); diff --git a/reference_model/src/verify/verify_utils.cc b/reference_model/src/verify/verify_utils.cc index 99cb0c1..9b20fb2 100644 --- a/reference_model/src/verify/verify_utils.cc +++ b/reference_model/src/verify/verify_utils.cc @@ -121,6 +121,32 @@ int64_t numElements(const std::vector<int32_t>& shape) return std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int64_t>()); } +std::vector<int32_t> indexToPosition(int64_t index, const std::vector<int32_t>& shape) +{ + std::vector<int32_t> pos; + for (auto d = shape.end() - 1; d >= shape.begin(); --d) + { + pos.insert(pos.begin(), index % *d); + index /= *d; + } + return pos; +} + +std::string positionToString(const std::vector<int32_t>& pos) +{ + std::string str = "["; + for (auto d = pos.begin(); d < pos.end(); ++d) + { + str.append(std::to_string(*d)); + if (pos.end() - d > 1) + { + str.append(","); + } + } + str.append("]"); + return str; +} + DType mapToDType(tosa_datatype_t dataType) { static std::map<tosa_datatype_t, DType> typeMap = { diff --git a/reference_model/src/verify/verify_utils.h b/reference_model/src/verify/verify_utils.h index 15d7ba5..24d65b0 100644 --- a/reference_model/src/verify/verify_utils.h +++ b/reference_model/src/verify/verify_utils.h @@ -91,6 +91,12 @@ std::optional<VerifyConfig> parseVerifyConfig(const char* tensorName, const char /// \brief Extract number of total elements int64_t numElements(const std::vector<int32_t>& shape); +/// \brief Convert a flat index to a shape position +std::vector<int32_t> indexToPosition(int64_t index, const std::vector<int32_t>& shape); + +/// \brief A string representing the shape or position +std::string positionToString(const std::vector<int32_t>& pos); + /// \brief Map API data-type to DType DType mapToDType(tosa_datatype_t dataType); |