// 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
|