Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/framework/execution_provider.h"
#include "core/framework/execution_providers.h"
#include "core/graph/model.h"
#include "test/unittest_util/framework_test_utils.h"
#include "test/test_environment.h"
#include "test/util/include/asserts.h"
#include "core/framework/model_metadef_id_generator.h"
 
#include "gtest/gtest.h"
 
#include <atomic>
#include <fstream>
#include <thread>
#include <unordered_set>
 
namespace onnxruntime {
namespace test {
 
class TestEP : public IExecutionProvider {
  static constexpr const char* kEPType = "TestEP";
 
 public:
  TestEP() : IExecutionProvider{kEPType} {}
  TestEP(std::string type, ProviderOptions options)
      : IExecutionProvider{type}, options_{std::move(options)} {}
 
  int GetId(const GraphViewer& viewer, HashValue& model_hash) {
    return metadef_id_generator_.GenerateId(viewer, model_hash);
  }
 
  ProviderOptions GetProviderOptions() const override {
    return options_;
  }
 
 private:
  ModelMetadefIdGenerator metadef_id_generator_;
  ProviderOptions options_;
};
 
TEST(ExecutionProviderTest, ConcurrentRegistrationAndProviderOptionsSnapshotsAreConsistent) {
  constexpr size_t kProviderCount = 256;
  ExecutionProviders execution_providers;
  ASSERT_STATUS_OK(execution_providers.Add(
      "TestEP_0", std::make_shared<TestEP>("TestEP_0", ProviderOptions{{"id", "TestEP_0"}})));
 
  std::atomic<bool> start{false};
  std::atomic<bool> registration_succeeded{true};
  std::atomic<bool> duplicate_rejected{false};
  std::atomic<bool> registration_done{false};
  std::atomic<bool> snapshots_consistent{true};
  std::atomic<size_t> snapshot_count{0};
 
  std::thread registration_thread([&]() {
    while (!start.load()) {
      std::this_thread::yield();
    }
 
    for (size_t i = 1; i < kProviderCount; ++i) {
      const std::string provider_id = "TestEP_" + std::to_string(i);
      if (!execution_providers.Add(
                                  provider_id,
                                  std::make_shared<TestEP>(
                                      provider_id, ProviderOptions{{"id", provider_id}}))
               .IsOK()) {
        registration_succeeded = false;
        break;
      }
      std::this_thread::yield();
    }
 
    registration_done = true;
  });
 
  std::thread duplicate_thread([&]() {
    while (!start.load()) {
      std::this_thread::yield();
    }
 
    duplicate_rejected =
        !execution_providers.Add(
                                "TestEP_0",
                                std::make_shared<TestEP>(
                                    "TestEP_0", ProviderOptions{{"id", "duplicate"}}))
             .IsOK();
  });
 
  std::thread snapshot_thread([&]() {
    while (!start.load()) {
      std::this_thread::yield();
    }
 
    do {
      const auto snapshot = execution_providers.GetProviderOptionsSnapshot();
      std::unordered_set<std::string> provider_ids;
      for (const auto& [provider_id, options] : snapshot) {
        auto option_it = options.find("id");
        if (!provider_ids.insert(provider_id).second ||
            option_it == options.end() || option_it->second != provider_id) {
          snapshots_consistent = false;
          break;
        }
      }
      ++snapshot_count;
    } while (!registration_done.load() && snapshots_consistent.load());
  });
 
  start = true;
  registration_thread.join();
  duplicate_thread.join();
  snapshot_thread.join();
 
  EXPECT_TRUE(registration_succeeded.load());
  EXPECT_TRUE(duplicate_rejected.load());
  EXPECT_TRUE(snapshots_consistent.load());
  EXPECT_GT(snapshot_count.load(), 0u);
 
  const auto final_snapshot = execution_providers.GetProviderOptionsSnapshot();
  EXPECT_EQ(final_snapshot.size(), kProviderCount);
}
 
TEST(ExecutionProviderTest, MetadefIdGeneratorUsingModelPath) {
  TestEP ep;
 
  auto test_model = [&ep](const std::basic_string<ORTCHAR_T>& model_path) {
    std::shared_ptr<Model> model;
    ASSERT_TRUE(Model::Load(model_path, model, nullptr, DefaultLoggingManager().DefaultLogger()).IsOK());
 
    Graph& graph = model->MainGraph();
    GraphViewer viewer(graph);
 
    // check for stable non-zero model_hash, and incrementing id.
    HashValue model_hash;
    int id = ep.GetId(viewer, model_hash);
    ASSERT_EQ(id, 0);
    ASSERT_NE(model_hash, 0u);
 
    for (int i = 1; i < 4; ++i) {
      HashValue cur_model_hash;
      int cur_id = ep.GetId(viewer, cur_model_hash);
      ASSERT_EQ(cur_id, i);
      ASSERT_EQ(cur_model_hash, model_hash);
    }
  };
 
  test_model(ORT_TSTR("testdata/mnist.onnx"));
  // load a new model instance and check it has a separate scope for the generated ids
  test_model(ORT_TSTR("testdata/ort_github_issue_4031.onnx"));
}
 
// test when the model hash is created by hashing the contents of the main graph instead of the model path
TEST(ExecutionProviderTest, MetadefIdGeneratorUsingModelHashing) {
  TestEP ep;
 
  auto model_path = ORT_TSTR("testdata/mnist.onnx");
 
  std::shared_ptr<Model> model;
  ASSERT_TRUE(Model::Load(model_path, model, nullptr, DefaultLoggingManager().DefaultLogger()).IsOK());
 
  Graph& graph = model->MainGraph();
  GraphViewer viewer(graph);
 
  // get the hash for the model when loaded from file
  HashValue model_hash;
  int id = ep.GetId(viewer, model_hash);
  ASSERT_EQ(id, 0);
  ASSERT_NE(model_hash, 0u);
 
  // now load the model from bytes and check the hash differs
  std::ifstream model_file_stream(model_path, std::ios::in | std::ios::binary);
 
  std::shared_ptr<Model> model2;
  ONNX_NAMESPACE::ModelProto model_proto;
  ASSERT_STATUS_OK(Model::Load(model_file_stream, &model_proto));
  ASSERT_STATUS_OK(Model::Load(std::move(model_proto), PathString(), model2, nullptr,
                               DefaultLoggingManager().DefaultLogger()));
 
  Graph& graph2 = model2->MainGraph();
  GraphViewer viewer2(graph2);
 
  HashValue model_hash2;
  int id2 = ep.GetId(viewer2, model_hash2);
  ASSERT_EQ(id2, 0) << "Id for new model should always start at zero";
  ASSERT_NE(model_hash, model_hash2) << "Hash from model path should differ from hash based on model contents";
}
 
}  // namespace test
}  // namespace onnxruntime