Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "testPch.h"
#include "compare_feature_value.h"
#include "StringHelpers.h"
#include <core/framework/ort_value.h>
#include "ort_value_helper.h"
 
using namespace winrt::Windows::Foundation::Collections;
using namespace winml;
 
namespace CompareFeatureValuesHelper {
 
template <typename T>
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<IMap<winrt::hstring, float>> 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<int>(featureValue.Size()),
      static_cast<int>(val.GetCount())
    );
  }
 
  int expectedValSequenceIndex = 0;
  Ort::AllocatorWithDefaultOptions allocator;
  for (IMap<winrt::hstring, float> mapVal : featureValue) {
    std::map<winrt::hstring, float> expectedKvp;
    std::vector<std::pair<winrt::hstring, float>> 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<TensorString>().GetAsVectorView();
    auto expectedOutputValues =
      OrtValueHelpers::LoadTensorFromOrtValue(mapExpectedOutputValues).as<TensorFloat>().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<double>(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