#include "testPch.h" #include "compare_feature_value.h" #include "StringHelpers.h" #include #include "ort_value_helper.h" using namespace winrt::Windows::Foundation::Collections; using namespace winml; namespace CompareFeatureValuesHelper { template bool IsResultCloselyMatch(const T& outvalue, const T& expected_value, const double diff, const double tol) { if (diff > tol) return false; if (std::isnan(diff) && !(std::isnan(outvalue) && std::isnan(expected_value)) && !(std::isinf(outvalue) && std::isinf(expected_value))) return false; return true; } bool CompareSequenceOfMapsStringToFloat( IVectorView> featureValue, const Ort::Value& val, double perSampleTolerance, double relativePerSampleTolerance ) { if (val.GetCount() != featureValue.Size()) { printf( "Map lengths are not the same! Got %d, expected %d\n", static_cast(featureValue.Size()), static_cast(val.GetCount()) ); } int expectedValSequenceIndex = 0; Ort::AllocatorWithDefaultOptions allocator; for (IMap mapVal : featureValue) { std::map expectedKvp; std::vector> actualKvp; Ort::Value mapExpectedOutput(nullptr); Ort::Value mapExpectedOutputKeys(nullptr); Ort::Value mapExpectedOutputValues(nullptr); WINML_EXPECT_NO_THROW(mapExpectedOutput = val.GetValue(expectedValSequenceIndex, allocator)); WINML_EXPECT_NO_THROW(mapExpectedOutputKeys = mapExpectedOutput.GetValue(0, allocator)); WINML_EXPECT_NO_THROW(mapExpectedOutputValues = mapExpectedOutput.GetValue(1, allocator)); auto expectedOutputKeys = OrtValueHelpers::LoadTensorFromOrtValue(mapExpectedOutputKeys).as().GetAsVectorView(); auto expectedOutputValues = OrtValueHelpers::LoadTensorFromOrtValue(mapExpectedOutputValues).as().GetAsVectorView(); for (uint32_t i = 0; i < expectedOutputKeys.Size(); i++) { expectedKvp[expectedOutputKeys.GetAt(i)] = expectedOutputValues.GetAt(i); } for (auto kvp : mapVal) { winrt::hstring actualKey = kvp.Key(); float actualValue = kvp.Value(); if (expectedKvp.find(actualKey) == expectedKvp.end()) { printf("Unexpected key in actual output: %ws", actualKey.c_str()); return false; } else { // verify that the value is within tolerable ranges const double diff = std::fabs(expectedKvp[actualKey] - actualValue); const double tol = perSampleTolerance + relativePerSampleTolerance * std::fabs(expectedKvp[actualKey]); if (!IsResultCloselyMatch(actualValue, expectedKvp[actualKey], diff, tol)) { printf("expected (%f), actual (%f), diff: %f, tol= %f .\n", expectedKvp[actualKey], actualValue, diff, tol); return false; } } } expectedValSequenceIndex++; } // If errors or discrepancies are not found, then return true return true; } } // namespace CompareFeatureValuesHelper