// Copyright (c) Microsoft Corporation. All rights reserved.
|
// Licensed under the MIT License.
|
|
#include <array>
|
#include <chrono>
|
#include <condition_variable>
|
#include <fstream>
|
#include <iostream>
|
#include <mutex>
|
#include <optional>
|
#include <thread>
|
#include <absl/base/config.h>
|
|
#include "asserts.h"
|
#include "core/framework/allocator_utils.h"
|
#include "core/framework/execution_providers.h"
|
#include "core/framework/graph_partitioner.h"
|
#include "core/framework/kernel_registry.h"
|
#include "core/framework/layering_annotations.h"
|
#include "core/framework/op_kernel.h"
|
#include "core/framework/bfc_arena.h"
|
#include "core/framework/ep_context_options.h"
|
#include "core/framework/resource_accountant.h"
|
#include "core/framework/session_state.h"
|
#include "core/framework/workspace_input_shape.h"
|
#include "core/graph/graph_utils.h"
|
#include "core/graph/graph_viewer.h"
|
#include "core/graph/model.h"
|
#include "core/graph/model_saving_options.h"
|
#include "core/graph/op.h"
|
#include "core/providers/cpu/cpu_execution_provider.h"
|
#include "core/session/onnxruntime_session_options_config_keys.h"
|
#include "core/util/thread_utils.h"
|
#include "gtest/gtest.h"
|
#include "test/test_environment.h"
|
#include "test/unittest_util/graph_transform_test_builder.h"
|
#include "test/util/include/default_providers.h"
|
#include "test/util/include/file_util.h"
|
#include "core/optimizer/layout_transformation/layout_transformation.h"
|
#include "core/optimizer/graph_optimizer_registry.h"
|
#include "core/optimizer/selectors_actions/actions.h"
|
#include "core/optimizer/selectors_actions/helpers.h"
|
|
using namespace ONNX_NAMESPACE;
|
namespace onnxruntime {
|
namespace test {
|
|
#ifndef ENABLE_TRAINING_CORE
|
#ifndef __wasm__
|
static void TestSavedPrepacks(const Model& model) {
|
auto inspect = [](const Graph& graph) {
|
const auto& prepacked_for_graph = graph.GetPrepacked();
|
const auto& key_to_blob = prepacked_for_graph.GetKeyToBlob();
|
ASSERT_EQ(1U, key_to_blob.size());
|
const size_t expected_prepacks_for_writing = (graph.ParentGraph() == nullptr) ? 1U : 0U;
|
ASSERT_EQ(expected_prepacks_for_writing, prepacked_for_graph.GetNumberOfWeightsForWriting());
|
|
const size_t expected_blobs_for_writing = (graph.ParentGraph() == nullptr) ? 1U : 0U;
|
ASSERT_EQ(expected_blobs_for_writing, prepacked_for_graph.GetNumberOfKeyedBlobsForWriting());
|
|
if (graph.ParentGraph() == nullptr) {
|
const auto* blob_keys = prepacked_for_graph.GetKeysForWeightForSaving("if_shared");
|
ASSERT_TRUE(blob_keys != nullptr);
|
ASSERT_EQ(blob_keys->size(), 1U);
|
const auto* prepacked_weights = prepacked_for_graph.GetPrepackedWeights(*blob_keys->cbegin());
|
ASSERT_TRUE(prepacked_weights != nullptr);
|
ASSERT_EQ(prepacked_weights->buffer_sizes_.size(), 1U);
|
ASSERT_EQ(prepacked_weights->buffer_sizes_[0], sizeof(float) * 2);
|
}
|
};
|
|
const auto& main_graph = model.MainGraph();
|
inspect(main_graph);
|
|
const auto& nodes = main_graph.Nodes();
|
auto if_node_hit = std::find_if(nodes.begin(), nodes.end(),
|
[](const Node& node) { return node.Name() == "if"; });
|
ASSERT_FALSE(if_node_hit == nodes.end());
|
const Node& if_node = *if_node_hit;
|
for (const auto& [_, subgraph] : if_node.GetAttributeNameToSubgraphMap()) {
|
inspect(*subgraph);
|
}
|
}
|
|
static void TestLoadedSharedUserSupplied(const Model& model) {
|
auto inspect = [](const Graph& graph) {
|
const auto& prepacked_for_graph = graph.GetPrepacked();
|
constexpr size_t expected_prepacks_for_writing = 0U;
|
ASSERT_EQ(expected_prepacks_for_writing, prepacked_for_graph.GetNumberOfWeightsForWriting());
|
|
// We have not loaded anything since this initializer is user supplied
|
const auto& key_to_blob = prepacked_for_graph.GetKeyToBlob();
|
ASSERT_EQ(0U, key_to_blob.size());
|
};
|
|
const auto& main_graph = model.MainGraph();
|
inspect(main_graph);
|
|
const auto& nodes = main_graph.Nodes();
|
auto if_node_hit = std::find_if(nodes.begin(), nodes.end(),
|
[](const Node& node) { return node.Name() == "if"; });
|
ASSERT_FALSE(if_node_hit == nodes.end());
|
const Node& if_node = *if_node_hit;
|
for (const auto& [_, subgraph] : if_node.GetAttributeNameToSubgraphMap()) {
|
inspect(*subgraph);
|
}
|
}
|
|
static void TestLoadedSharedNoUserSupplied(const Model& model) {
|
auto inspect = [](const Graph& graph) {
|
const auto& prepacked_for_graph = graph.GetPrepacked();
|
constexpr size_t expected_prepacks_for_writing = 0U;
|
ASSERT_EQ(expected_prepacks_for_writing, prepacked_for_graph.GetNumberOfWeightsForWriting());
|
|
// We have not loaded anything since this initializer is user supplied
|
const auto& key_to_blob = prepacked_for_graph.GetKeyToBlob();
|
ASSERT_EQ(1U, key_to_blob.size());
|
};
|
|
const auto& main_graph = model.MainGraph();
|
inspect(main_graph);
|
|
const auto& nodes = main_graph.Nodes();
|
auto if_node_hit = std::find_if(nodes.begin(), nodes.end(),
|
[](const Node& node) { return node.Name() == "if"; });
|
ASSERT_FALSE(if_node_hit == nodes.end());
|
const Node& if_node = *if_node_hit;
|
for (const auto& [_, subgraph] : if_node.GetAttributeNameToSubgraphMap()) {
|
inspect(*subgraph);
|
}
|
}
|
|
#endif // __wasm__
|
#endif // ENABLE_TRAINING_CORE
|
|
class TestOpKernel : public OpKernel {
|
public:
|
TestOpKernel(const OpKernelInfo& p) : OpKernel(p) {
|
}
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
Status ComputeAsync(OpKernelContext* context, DoneCallback done) const override {
|
ORT_UNUSED_PARAMETER(context);
|
ORT_UNUSED_PARAMETER(done);
|
return Status::OK();
|
}
|
};
|
|
ONNX_OPERATOR_SCHEMA(WorkspaceVerificationTestOp)
|
.SetDoc("Test operator for Level-2 workspace verification.")
|
.Output(0, "output", "Test output.", "tensor(int32)");
|
|
class WorkspaceVerificationTestKernel final : public OpKernel {
|
public:
|
explicit WorkspaceVerificationTestKernel(const OpKernelInfo& info) : OpKernel(info) {}
|
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
|
Status DeclareWorkspaceRequirements(
|
gsl::span<const WorkspaceInputShape> input_shapes,
|
InlinedVector<WorkspaceRequirement>& requirements) const override {
|
ORT_UNUSED_PARAMETER(input_shapes);
|
requirements.clear();
|
requirements.push_back(WorkspaceRequirement{128, /*slot_id=*/0, /*alignment_bytes=*/0});
|
return Status::OK();
|
}
|
};
|
|
enum class WorkspaceVerificationMutation {
|
kNone,
|
kReplaceWithNew,
|
kFinalizeNodeFusionPair,
|
kFinalizeNodeFusionSpan,
|
};
|
|
static Status FinalizeWorkspaceVerificationTestSession(
|
std::optional<size_t> reservation_bytes, bool strict_verification = true,
|
WorkspaceVerificationMutation mutation = WorkspaceVerificationMutation::kNone,
|
bool track_replacement_reservation = true,
|
std::optional<size_t> orphaned_reservation_bytes = std::nullopt,
|
bool discard_orphaned_reservation = false) {
|
Model model("workspace_verification", false, DefaultLoggingManager().DefaultLogger());
|
Graph& graph = model.MainGraph();
|
|
TypeProto output_type;
|
output_type.mutable_tensor_type()->set_elem_type(TensorProto_DataType_INT32);
|
output_type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
NodeArg output_arg("output", &output_type);
|
Node& node = graph.AddNode("workspace_node", "WorkspaceVerificationTestOp", "", {}, {&output_arg});
|
|
WorkspaceReservationMap reservations;
|
if (reservation_bytes.has_value()) {
|
reservations[&graph].insert_or_assign(
|
node.Index(),
|
WorkspaceEstimateSelection{*reservation_bytes, WorkspaceEstimateSource::kEstimator});
|
}
|
if (orphaned_reservation_bytes.has_value()) {
|
Node& removed_node =
|
graph.AddNode("removed_workspace_node", "WorkspaceVerificationTestOp", "", {}, {});
|
const NodeIndex removed_node_index = removed_node.Index();
|
reservations[&graph].insert_or_assign(
|
removed_node_index,
|
WorkspaceEstimateSelection{*orphaned_reservation_bytes, WorkspaceEstimateSource::kEstimator});
|
if (discard_orphaned_reservation) {
|
graph.SetNodeRemovalCallback(
|
[&reservations](const Graph& modified_graph,
|
gsl::span<const NodeIndex> node_indices) {
|
auto graph_it = reservations.find(&modified_graph);
|
if (graph_it == reservations.end()) {
|
return;
|
}
|
for (const NodeIndex node_index : node_indices) {
|
graph_it->second.erase(node_index);
|
}
|
});
|
}
|
graph.RemoveNode(removed_node_index);
|
if (discard_orphaned_reservation) {
|
graph.NotifyNodesRemoved(gsl::span<const NodeIndex>{&removed_node_index, 1});
|
graph.SetNodeRemovalCallback({});
|
}
|
}
|
|
if (mutation != WorkspaceVerificationMutation::kNone) {
|
if (track_replacement_reservation) {
|
graph.SetNodeReplacementCallback(
|
[&reservations](const Graph& modified_graph,
|
gsl::span<const NodeIndex> source_node_indices,
|
NodeIndex destination_node_index) {
|
auto graph_it = reservations.find(&modified_graph);
|
if (graph_it != reservations.end()) {
|
ConsolidateWorkspaceReservations(
|
graph_it->second, source_node_indices, destination_node_index);
|
}
|
});
|
}
|
|
if (mutation == WorkspaceVerificationMutation::kReplaceWithNew) {
|
NodesToOptimize selected_nodes{
|
gsl::span<Node* const>{}, node, gsl::span<Node* const>{}};
|
const NodesToOptimize::NodeLocation target{
|
NodesToOptimize::NodeType::kTarget, 0};
|
ReplaceWithNewFixed replacement{
|
kOnnxDomain, "WorkspaceVerificationTestOp", {MoveAll(target, ArgType::kOutput)}};
|
ORT_RETURN_IF_ERROR(replacement.Run(graph, selected_nodes));
|
} else {
|
Node& replacement_node =
|
graph.AddNode("workspace_fused", "WorkspaceVerificationTestOp", "", {}, {});
|
if (mutation == WorkspaceVerificationMutation::kFinalizeNodeFusionPair) {
|
graph_utils::FinalizeNodeFusion(graph, replacement_node, node);
|
} else {
|
const std::array<std::reference_wrapper<Node>, 1> nodes_to_fuse{node};
|
graph_utils::FinalizeNodeFusion(
|
graph, nodes_to_fuse, replacement_node, replacement_node);
|
}
|
}
|
graph.SetNodeReplacementCallback({});
|
}
|
|
ORT_RETURN_IF_ERROR(graph.Resolve());
|
for (auto& final_node : graph.Nodes()) {
|
final_node.SetExecutionProviderType(kCpuExecutionProvider);
|
}
|
|
ExecutionProviders execution_providers;
|
ORT_RETURN_IF_ERROR(execution_providers.Add(
|
kCpuExecutionProvider,
|
std::make_unique<CPUExecutionProvider>(CPUExecutionProviderInfo(false))));
|
|
DataTransferManager data_transfer_manager;
|
ExternalDataLoaderManager external_data_loader_manager;
|
profiling::Profiler profiler;
|
SessionOptions session_options;
|
if (strict_verification) {
|
ORT_RETURN_IF_ERROR(session_options.config_options.AddConfigEntry(
|
kOrtSessionOptionsStrictWorkspaceVerification, "1"));
|
}
|
SessionState session_state(graph, execution_providers, nullptr, nullptr, data_transfer_manager,
|
external_data_loader_manager, DefaultLoggingManager().DefaultLogger(),
|
profiler, session_options);
|
|
if (!reservations.empty()) {
|
session_state.SetWorkspaceReservations(std::move(reservations));
|
}
|
|
KernelRegistryManager kernel_registry_manager;
|
ORT_RETURN_IF_ERROR(kernel_registry_manager.RegisterKernels(execution_providers));
|
auto kernel_registry = std::make_shared<KernelRegistry>();
|
auto kernel_def = KernelDefBuilder()
|
.SetName("WorkspaceVerificationTestOp")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
ORT_RETURN_IF_ERROR(kernel_registry->Register(
|
KernelCreateInfo(std::move(kernel_def),
|
[](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<WorkspaceVerificationTestKernel>(info);
|
return Status::OK();
|
})));
|
kernel_registry_manager.RegisterKernelRegistry(kernel_registry);
|
|
return session_state.FinalizeSessionState(ORT_TSTR(""), kernel_registry_manager);
|
}
|
|
TEST(OpKernelTest, DefaultDeclareWorkspaceRequirementsClearsOutput) {
|
Model model("default_workspace_declaration", false, DefaultLoggingManager().DefaultLogger());
|
Graph& graph = model.MainGraph();
|
|
TypeProto tensor_type;
|
tensor_type.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT);
|
tensor_type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
NodeArg input_arg("input", &tensor_type);
|
NodeArg output_arg("output", &tensor_type);
|
Node& node = graph.AddNode("identity", "Identity", "", {&input_arg}, {&output_arg});
|
ASSERT_STATUS_OK(graph.Resolve());
|
|
ExecutionProviders execution_providers;
|
auto cpu_execution_provider = std::make_unique<CPUExecutionProvider>(CPUExecutionProviderInfo(false));
|
CPUExecutionProvider* cpu_execution_provider_ptr = cpu_execution_provider.get();
|
ASSERT_STATUS_OK(execution_providers.Add(kCpuExecutionProvider, std::move(cpu_execution_provider)));
|
|
DataTransferManager data_transfer_manager;
|
ExternalDataLoaderManager external_data_loader_manager;
|
profiling::Profiler profiler;
|
SessionOptions session_options;
|
SessionState session_state(graph, execution_providers, nullptr, nullptr, data_transfer_manager,
|
external_data_loader_manager, DefaultLoggingManager().DefaultLogger(),
|
profiler, session_options);
|
|
auto kernel_def = KernelDefBuilder()
|
.SetName("Identity")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
OpKernelInfo kernel_info(node, *kernel_def, *cpu_execution_provider_ptr,
|
session_state.GetConstantInitializedTensors(),
|
session_state.GetOrtValueNameIdxMap(), session_state.GetDataTransferMgr(),
|
session_state.GetAllocators(), session_state.GetSessionOptions().config_options);
|
TestOpKernel kernel(kernel_info);
|
|
InlinedVector<WorkspaceInputShape> input_shapes;
|
input_shapes.push_back(WorkspaceInputShape::PresentWithoutShape());
|
InlinedVector<WorkspaceRequirement> requirements;
|
requirements.push_back(WorkspaceRequirement{123, /*slot_id=*/7, /*alignment_bytes=*/0});
|
ASSERT_STATUS_OK(kernel.DeclareWorkspaceRequirements(gsl::make_span(input_shapes), requirements));
|
EXPECT_TRUE(requirements.empty());
|
}
|
|
TEST(SessionStateTest, WorkspaceVerificationAllowsOverrunByDefault) {
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(size_t{64}, false));
|
}
|
|
TEST(SessionStateTest, StrictWorkspaceVerificationOnlyRejectsOverrun) {
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(std::nullopt));
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(size_t{128}));
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(size_t{256}));
|
EXPECT_STATUS_NOT_OK_AND_HAS_SUBSTR(
|
FinalizeWorkspaceVerificationTestSession(size_t{64}),
|
"declarations exceed the workspace reserved during graph partitioning");
|
}
|
|
TEST(SessionStateTest, StrictWorkspaceVerificationTracksReplacementNode) {
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(
|
size_t{128}, true, WorkspaceVerificationMutation::kReplaceWithNew));
|
EXPECT_STATUS_NOT_OK_AND_HAS_SUBSTR(
|
FinalizeWorkspaceVerificationTestSession(
|
size_t{64}, true, WorkspaceVerificationMutation::kReplaceWithNew),
|
"declarations exceed the workspace reserved during graph partitioning");
|
}
|
|
TEST(SessionStateTest, StrictWorkspaceVerificationTracksFinalizeNodeFusion) {
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(
|
size_t{128}, true, WorkspaceVerificationMutation::kFinalizeNodeFusionPair));
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(
|
size_t{128}, true, WorkspaceVerificationMutation::kFinalizeNodeFusionSpan));
|
}
|
|
TEST(SessionStateTest, StrictWorkspaceVerificationRejectsUntrackedReplacement) {
|
EXPECT_STATUS_NOT_OK_AND_HAS_SUBSTR(
|
FinalizeWorkspaceVerificationTestSession(
|
size_t{128}, true, WorkspaceVerificationMutation::kReplaceWithNew, false),
|
"post-partition graph transformation");
|
}
|
|
TEST(SessionStateTest, StrictWorkspaceVerificationRejectsOrphanedReservationWithoutMissingReservation) {
|
EXPECT_STATUS_NOT_OK_AND_HAS_SUBSTR(
|
FinalizeWorkspaceVerificationTestSession(
|
size_t{128}, true, WorkspaceVerificationMutation::kNone, true, size_t{1}),
|
"post-partition graph transformation");
|
}
|
|
TEST(SessionStateTest, StrictWorkspaceVerificationAllowsOrphanedZeroByteReservation) {
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(
|
size_t{128}, true, WorkspaceVerificationMutation::kNone, true, size_t{0}));
|
}
|
|
TEST(SessionStateTest, StrictWorkspaceVerificationAllowsIntentionalReservationRemoval) {
|
EXPECT_STATUS_OK(FinalizeWorkspaceVerificationTestSession(
|
size_t{128}, true, WorkspaceVerificationMutation::kNone, true, size_t{128}, true));
|
}
|
|
class SessionStateAddGetKernelTest : public testing::TestWithParam<int> {};
|
|
TEST_P(SessionStateAddGetKernelTest, AddGetKernelTest) {
|
OrtThreadPoolParams to{};
|
to.thread_pool_size = GetParam();
|
auto tp = concurrency::CreateThreadPool(&onnxruntime::Env::Default(), to, concurrency::ThreadPoolType::INTRA_OP);
|
ONNX_OPERATOR_SCHEMA(Variable)
|
.SetDoc("Input variable.")
|
.Output(0, "output_1", "docstr for output_1.", "tensor(int32)");
|
|
onnxruntime::Model model("graph_1", false, DefaultLoggingManager().DefaultLogger());
|
auto& graph = model.MainGraph();
|
|
ExecutionProviders execution_providers;
|
auto tmp_cpu_execution_provider = std::make_unique<CPUExecutionProvider>(CPUExecutionProviderInfo(false));
|
auto* cpu_execution_provider = tmp_cpu_execution_provider.get();
|
ASSERT_STATUS_OK(execution_providers.Add(kCpuExecutionProvider, std::move(tmp_cpu_execution_provider)));
|
|
DataTransferManager dtm;
|
ExternalDataLoaderManager edlm;
|
profiling::Profiler profiler;
|
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
|
SessionState s(graph, execution_providers, tp.get(), nullptr, dtm, edlm,
|
DefaultLoggingManager().DefaultLogger(), profiler, sess_options);
|
|
std::vector<onnxruntime::NodeArg*> inputs;
|
std::vector<onnxruntime::NodeArg*> outputs;
|
TypeProto output_type;
|
output_type.mutable_tensor_type()->set_elem_type(TensorProto_DataType_INT32);
|
output_type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
onnxruntime::NodeArg output_arg("node_1_out_1", &output_type);
|
outputs.push_back(&output_arg);
|
onnxruntime::Node& node = graph.AddNode("node_1", "Variable", "node 1.", inputs, outputs);
|
auto status = graph.Resolve();
|
ASSERT_TRUE(status.IsOK());
|
auto kernel_def = KernelDefBuilder().SetName("Variable").Provider(kCpuExecutionProvider).SinceVersion(1, 10).Build();
|
|
OpKernelInfo p_info(node, *kernel_def, *cpu_execution_provider, s.GetConstantInitializedTensors(),
|
s.GetOrtValueNameIdxMap(), s.GetDataTransferMgr(), s.GetAllocators(),
|
s.GetSessionOptions().config_options);
|
|
std::unique_ptr<TestOpKernel> p_kernel = std::make_unique<TestOpKernel>(p_info);
|
size_t orig_num_outputs = p_kernel->Node().OutputDefs().size();
|
std::cout << "node_idx: " << node.Index() << std::endl;
|
|
KernelRegistryManager kernel_registry_manager;
|
status = kernel_registry_manager.RegisterKernels(execution_providers);
|
ASSERT_TRUE(status.IsOK()) << status.ErrorMessage();
|
node.SetExecutionProviderType(kCpuExecutionProvider);
|
std::shared_ptr<KernelRegistry> kernel_registry = std::make_shared<KernelRegistry>();
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(kernel_def),
|
[](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<TestOpKernel>(info);
|
return Status::OK();
|
})));
|
kernel_registry_manager.RegisterKernelRegistry(kernel_registry);
|
ASSERT_STATUS_OK(s.FinalizeSessionState(ORT_TSTR(""), kernel_registry_manager));
|
|
auto test_kernel = s.GetKernel(node.Index());
|
std::cout << "orig: " << orig_num_outputs << " new: " << test_kernel->Node().OutputDefs().size() << std::endl;
|
EXPECT_EQ(orig_num_outputs, test_kernel->Node().OutputDefs().size());
|
}
|
|
INSTANTIATE_TEST_SUITE_P(SessionStateTests, SessionStateAddGetKernelTest, testing::Values(0, 1));
|
|
class TestParam {
|
public:
|
int ir_version;
|
bool enable_mem_pattern;
|
int thread_count;
|
};
|
|
TestParam param_list[] = {
|
{3, true, 0},
|
{4, true, 0},
|
{3, false, 0},
|
{4, false, 0},
|
{3, true, 1},
|
{4, true, 1},
|
{3, false, 1},
|
{4, false, 1}};
|
|
class SessionStateTestP : public testing::TestWithParam<TestParam> {};
|
// Test that we separate out constant and non-constant initializers correctly
|
TEST_P(SessionStateTestP, TestInitializerProcessing) {
|
const TestParam& param = GetParam();
|
OrtThreadPoolParams to{};
|
to.thread_pool_size = param.thread_count;
|
auto tp = concurrency::CreateThreadPool(&onnxruntime::Env::Default(), to, concurrency::ThreadPoolType::INTRA_OP);
|
|
std::basic_ostringstream<ORTCHAR_T> oss;
|
oss << ORT_TSTR("testdata/optional_inputs_ir") << param.ir_version << ORT_TSTR(".onnx");
|
Status status;
|
std::shared_ptr<Model> model;
|
ASSERT_TRUE((status = Model::Load(oss.str(), model, nullptr, DefaultLoggingManager().DefaultLogger())).IsOK())
|
<< status;
|
Graph& graph = model->MainGraph();
|
// take a copy as this gets cleared during session state initialization
|
InitializedTensorSet initializers = graph.GetAllInitializedTensors();
|
|
ExecutionProviders execution_providers;
|
CPUExecutionProviderInfo epi{false};
|
status =
|
execution_providers.Add(onnxruntime::kCpuExecutionProvider, std::make_unique<CPUExecutionProvider>(epi));
|
ASSERT_TRUE(status.IsOK()) << status;
|
|
KernelRegistryManager krm;
|
status = krm.RegisterKernels(execution_providers);
|
ASSERT_TRUE(status.IsOK()) << status;
|
|
DataTransferManager dtm;
|
ExternalDataLoaderManager edlm;
|
profiling::Profiler profiler;
|
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = param.enable_mem_pattern;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
|
SessionState session_state(graph, execution_providers, tp.get(), nullptr, dtm, edlm,
|
DefaultLoggingManager().DefaultLogger(), profiler, sess_options);
|
|
// Create GraphOptimizerRegistry instance for providing predefined graph optimizers and selection functions for EPs to lookup
|
auto graph_optimizer_registry = std::make_unique<GraphOptimizerRegistry>(&sess_options,
|
execution_providers.Get(onnxruntime::kCpuExecutionProvider),
|
&DefaultLoggingManager().DefaultLogger());
|
GraphPartitioner partitioner(krm, execution_providers, std::move(graph_optimizer_registry));
|
ASSERT_STATUS_OK(
|
partitioner.Partition(
|
graph, session_state.GetMutableFuncMgr(),
|
[](Graph& graph, bool& modified, const IExecutionProvider& execution_provider,
|
const layout_transformation::DebugGraphFn& debug_graph_fn) -> Status {
|
AllocatorPtr cpu_allocator = CPUAllocator::DefaultInstance();
|
return layout_transformation::TransformLayoutForEP(
|
graph, modified, execution_provider, std::move(cpu_allocator), debug_graph_fn);
|
},
|
sess_options.config_options,
|
DefaultLoggingManager().DefaultLogger(), nullptr /*layering_index*/));
|
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(oss.str(), krm));
|
|
const auto& initialized_tensors = session_state.GetInitializedTensors();
|
const auto& const_initialized_tensors = session_state.GetConstantInitializedTensors();
|
|
ASSERT_EQ(initializers.size(), initialized_tensors.size())
|
<< "SessionState should have an entry for all initializers in Graph.";
|
|
if (param.ir_version < 4) {
|
ASSERT_EQ(initialized_tensors.size(), const_initialized_tensors.size())
|
<< "All initializers should be considered constant if IR version < 4.";
|
} else {
|
const auto& name_to_idx = session_state.GetOrtValueNameIdxMap();
|
|
for (const auto& entry : initializers) {
|
int idx;
|
ASSERT_STATUS_OK(name_to_idx.GetIdx(entry.first, idx));
|
|
bool found = initialized_tensors.find(idx) != initialized_tensors.cend();
|
ASSERT_TRUE(found) << "Missing entry for " << entry.first << " in session state initialized tensors";
|
|
if (graph_utils::IsConstantInitializer(graph, entry.first, false)) {
|
found = const_initialized_tensors.find(idx) != const_initialized_tensors.cend();
|
ASSERT_TRUE(found) << "Missing entry for " << entry.first << " in session state const initialized tensors";
|
}
|
}
|
}
|
}
|
|
#ifdef USE_CUDA
|
// Test that we allocate memory for an initializer from non-arena memory even if we provide an arena-based allocator
|
// if the relevant session option config flag is set
|
TEST(SessionStateTest, TestInitializerMemoryAllocatedUsingNonArenaMemory) {
|
AllocatorPtr cpu_allocator = CPUAllocator::DefaultInstance();
|
const auto& default_logger = DefaultLoggingManager().DefaultLogger();
|
|
auto setup_and_run_test = [&cpu_allocator, &default_logger](Model& model, bool use_device_allocator) -> AllocatorStats {
|
Graph& graph = model.MainGraph();
|
DataTransferManager dtm;
|
ExecutionProviders execution_providers;
|
auto tmp_cpu_execution_provider = DefaultCudaExecutionProvider();
|
tmp_cpu_execution_provider->SetLogger(&default_logger);
|
EXPECT_STATUS_OK(dtm.RegisterDataTransfer(tmp_cpu_execution_provider->GetDataTransfer()));
|
EXPECT_STATUS_OK(execution_providers.Add(kCudaExecutionProvider, std::move(tmp_cpu_execution_provider)));
|
|
// Make sure CPU allocator is registered
|
auto cpu_execution_provider = DefaultCpuExecutionProvider();
|
cpu_execution_provider->SetLogger(&default_logger);
|
EXPECT_STATUS_OK(dtm.RegisterDataTransfer(cpu_execution_provider->GetDataTransfer()));
|
EXPECT_STATUS_OK(execution_providers.Add(kCpuExecutionProvider, std::move(cpu_execution_provider)));
|
KernelRegistryManager krm;
|
EXPECT_STATUS_OK(krm.RegisterKernels(execution_providers));
|
|
ExternalDataLoaderManager edlm;
|
profiling::Profiler profiler;
|
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = false;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
|
if (use_device_allocator) {
|
// disable allocating initialized tensor memory from the arena(by default it will be allocated by the arena)
|
EXPECT_STATUS_OK(sess_options.config_options.AddConfigEntry(kOrtSessionOptionsUseDeviceAllocatorForInitializers,
|
"1"));
|
}
|
|
SessionState session_state(graph, execution_providers, nullptr, nullptr, dtm, edlm,
|
default_logger, profiler, sess_options);
|
|
// Create GraphOptimizerRegistry instance for providing predefined graph optimizers and selection functions for EPs to lookup
|
auto graph_optimizer_registry = std::make_unique<GraphOptimizerRegistry>(&sess_options,
|
execution_providers.Get(kCudaExecutionProvider),
|
&default_logger);
|
// Partition the graph
|
GraphPartitioner partitioner(krm, execution_providers, std::move(graph_optimizer_registry));
|
EXPECT_STATUS_OK(partitioner.Partition(
|
graph, session_state.GetMutableFuncMgr(),
|
[&cpu_allocator](Graph& graph, bool& modified, const IExecutionProvider& execution_provider,
|
const layout_transformation::DebugGraphFn& debug_graph_fn) -> Status {
|
return layout_transformation::TransformLayoutForEP(graph, modified, execution_provider,
|
cpu_allocator, debug_graph_fn);
|
},
|
sess_options.config_options,
|
default_logger,
|
nullptr /*layering_index*/));
|
|
EXPECT_STATUS_OK(session_state.FinalizeSessionState(model.ModelPath(), krm));
|
|
// Fetch the CUDA arena-allocator from the session state
|
OrtMemoryInfo mem_info(CUDA, OrtArenaAllocator);
|
AllocatorPtr alloc = session_state.GetAllocator(mem_info);
|
EXPECT_NE(alloc, nullptr);
|
|
// Get stats for the CUDA arena-based allocator
|
AllocatorStats alloc_stats;
|
static_cast<BFCArena*>(alloc.get())->GetStats(&alloc_stats);
|
|
return alloc_stats;
|
};
|
|
const ORTCHAR_T* model_path = ORT_TSTR("testdata/mul_1.onnx");
|
// Part 1: Feature turned ON (i.e.) allocate from non-arena memory
|
{
|
std::shared_ptr<Model> model;
|
ASSERT_STATUS_OK(Model::Load(model_path, model, nullptr, default_logger));
|
|
auto alloc_stats = setup_and_run_test(*model, /*use_device_allocator=*/true);
|
|
// Assert that we have made 1 Reserve() call (for allocating memory for the sole initializer in the model)
|
ASSERT_EQ(1, alloc_stats.num_reserves);
|
}
|
|
// Part 2: Feature turned OFF (i.e.) allocate from arena memory (default behavior)
|
{
|
std::shared_ptr<Model> model;
|
ASSERT_STATUS_OK(Model::Load(model_path, model, nullptr, default_logger));
|
|
auto alloc_stats = setup_and_run_test(*model, /*use_device_allocator=*/false);
|
|
// One reserve call should have been made (for allocating memory for the sole initializer in the model)
|
ASSERT_EQ(1, alloc_stats.num_reserves);
|
|
// This counter comes from Reserve(). The actual call for arena based allocator went to StreamAwareBFCArena instance
|
ASSERT_EQ(1, alloc_stats.num_allocs);
|
}
|
}
|
|
namespace {
|
|
using ParitionVerifierFn = std::function<void(const Graph&)>;
|
|
// Collect unique node names from a graph and all its subgraphs
|
// using the same naming scheme as the resource accountant.
|
static void CollectNodeNames(const Graph& graph, std::vector<std::string>& names) {
|
for (const auto& node : graph.Nodes()) {
|
names.push_back(IResourceAccountant::MakeUniqueNodeName(node));
|
for (const auto& [_, subgraph] : node.GetAttributeNameToSubgraphMap()) {
|
CollectNodeNames(*subgraph, names);
|
}
|
}
|
}
|
|
// Generates a node stats file dynamically from the current graph,
|
// assigning each node a fixed cost. Returns the total cost across
|
// all nodes so callers can choose a threshold relative to the actual total.
|
// This avoids relying on a pre-baked stats file whose node name hashes
|
// become stale when graph optimizers change node input/output names.
|
static void GenerateDynamicNodeStatsFile(const ORTCHAR_T* model_path,
|
const std::filesystem::path& output_path,
|
size_t& total_cost,
|
size_t cost_per_node = 1024) {
|
const auto& default_logger = DefaultLoggingManager().DefaultLogger();
|
std::shared_ptr<onnxruntime::Model> model;
|
ASSERT_STATUS_OK(Model::Load(model_path, model, nullptr, default_logger));
|
Graph& graph = model->MainGraph();
|
ASSERT_STATUS_OK(graph.Resolve());
|
|
std::vector<std::string> node_names;
|
CollectNodeNames(graph, node_names);
|
|
std::ofstream ofs(output_path);
|
ASSERT_TRUE(ofs.is_open());
|
ofs << "#name,input_sizes,initializers_sizes,total_dynamic_sizes,total_temp_allocations\n";
|
for (const auto& name : node_names) {
|
ofs << name << "," << cost_per_node << ",0,0,0\n";
|
}
|
ofs.close();
|
|
total_cost = node_names.size() * cost_per_node;
|
}
|
|
void LoadWithResourceAwarePartitioning(const ORTCHAR_T* model_path,
|
const SessionOptions& sess_options,
|
const ParitionVerifierFn& verifier_fn,
|
const std::string& layering_config = std::string()) {
|
const auto& log_manager = DefaultLoggingManager();
|
log_manager.SetDefaultLoggerSeverity(onnxruntime::logging::Severity::kVERBOSE);
|
const auto& default_logger = log_manager.DefaultLogger();
|
std::shared_ptr<onnxruntime::Model> model;
|
ASSERT_STATUS_OK(Model::Load(model_path, model, nullptr, default_logger));
|
|
Graph& graph = model->MainGraph();
|
ASSERT_STATUS_OK(graph.Resolve());
|
|
OrtThreadPoolParams to{};
|
to.thread_pool_size = 1;
|
auto tp = concurrency::CreateThreadPool(&onnxruntime::Env::Default(), to, concurrency::ThreadPoolType::INTRA_OP);
|
|
ExecutionProviders execution_providers;
|
auto tmp_execution_provider = DefaultCudaExecutionProvider();
|
tmp_execution_provider->SetLogger(&default_logger);
|
ASSERT_STATUS_OK(execution_providers.Add(kCudaExecutionProvider, std::move(tmp_execution_provider)));
|
tmp_execution_provider = DefaultCpuExecutionProvider();
|
tmp_execution_provider->SetLogger(&default_logger);
|
ASSERT_STATUS_OK(execution_providers.Add(kCpuExecutionProvider, std::move(tmp_execution_provider)));
|
|
KernelRegistryManager krm;
|
ASSERT_STATUS_OK(krm.RegisterKernels(execution_providers));
|
|
DataTransferManager dtm;
|
ExternalDataLoaderManager edlm;
|
profiling::Profiler profiler;
|
|
SessionState session_state(model->MainGraph(), execution_providers, tp.get(), nullptr, dtm, edlm,
|
default_logger, profiler, sess_options);
|
|
LayeringIndex* layering_index = nullptr;
|
std::optional<LayeringIndex> layering_index_storage;
|
if (!layering_config.empty()) {
|
ASSERT_STATUS_OK(LayeringIndex::Create(graph, layering_config, /*name_based_config_string=*/"", {}, execution_providers,
|
default_logger, layering_index_storage));
|
if (layering_index_storage.has_value()) {
|
layering_index = &layering_index_storage.value();
|
}
|
}
|
|
// Create GraphOptimizerRegistry instance for providing predefined graph optimizers and selection functions for EPs to lookup
|
auto graph_optimizer_registry = std::make_unique<GraphOptimizerRegistry>(&sess_options,
|
execution_providers.Get(onnxruntime::kCpuExecutionProvider),
|
&DefaultLoggingManager().DefaultLogger());
|
|
GraphPartitioner partitioner(krm, execution_providers, std::move(graph_optimizer_registry));
|
layout_transformation::TransformLayoutFunction transform_layout_fn;
|
layout_transformation::DebugGraphFn debug_graph_fn;
|
ASSERT_STATUS_OK(
|
partitioner.Partition(graph, session_state.GetMutableFuncMgr(), transform_layout_fn,
|
sess_options.config_options, default_logger, layering_index,
|
GraphPartitioner::Mode::kNormal,
|
epctx::ModelGenOptions{},
|
false,
|
debug_graph_fn));
|
|
verifier_fn(graph);
|
}
|
} // namespace
|
|
TEST(SessionStateTest, TestResourceAwarePartitioning_NoLimit) {
|
constexpr const ORTCHAR_T* model_path = ORT_TSTR("testdata/transformers/tiny_gpt2_beamsearch.onnx");
|
|
// Try to load the model without restrictions
|
// and verify nodes have been placed to CUDA
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = false;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = false;
|
|
LoadWithResourceAwarePartitioning(model_path, sess_options, [](const Graph& graph) {
|
const auto& graph_nodes = graph.Nodes();
|
for (const auto& node : graph_nodes) {
|
EXPECT_EQ(node.GetExecutionProviderType(), kCudaExecutionProvider);
|
}
|
});
|
}
|
|
TEST(SessionStateTest, TestResourceAwarePartitioning_LargeLimit) {
|
constexpr const ORTCHAR_T* model_path = ORT_TSTR("testdata/transformers/tiny_gpt2_beamsearch.onnx");
|
std::error_code ec;
|
const std::filesystem::path stats_path =
|
std::filesystem::temp_directory_path(ec) / "tiny_gpt2_beamsearch_dynamic_stats_large.txt";
|
ASSERT_FALSE(ec) << "temp_directory_path failed: " << ec.message();
|
|
// Generate node stats dynamically so names always match the current graph
|
constexpr size_t cost_per_node = 1024;
|
size_t total_cost = 0;
|
GenerateDynamicNodeStatsFile(model_path, stats_path, total_cost, cost_per_node);
|
ASSERT_GT(total_cost, 0U);
|
|
// Use a limit much larger than total cost so all nodes are assigned CUDA.
|
size_t large_limit_kb = (total_cost * 2) / 1024 + 1;
|
std::string limit_setting = std::to_string(large_limit_kb) + "," + stats_path.string();
|
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = false;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = false;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(
|
kOrtSessionOptionsResourceCudaPartitioningSettings, limit_setting.c_str()));
|
|
LoadWithResourceAwarePartitioning(model_path, sess_options, [](const Graph& graph) {
|
const auto& graph_nodes = graph.Nodes();
|
for (const auto& node : graph_nodes) {
|
EXPECT_EQ(node.GetExecutionProviderType(), kCudaExecutionProvider);
|
}
|
});
|
|
std::error_code remove_ec;
|
std::filesystem::remove(stats_path, remove_ec);
|
}
|
|
TEST(SessionStateTest, TestResourceAwarePartitioning_CPUOffloaded) {
|
constexpr const ORTCHAR_T* model_path = ORT_TSTR("testdata/transformers/tiny_gpt2_beamsearch.onnx");
|
std::error_code ec;
|
const std::filesystem::path stats_path =
|
std::filesystem::temp_directory_path(ec) / "tiny_gpt2_beamsearch_dynamic_stats_offload.txt";
|
ASSERT_FALSE(ec) << "temp_directory_path failed: " << ec.message();
|
|
// Generate node stats dynamically so names always match the current graph.
|
constexpr size_t cost_per_node = 1024;
|
size_t total_cost = 0;
|
GenerateDynamicNodeStatsFile(model_path, stats_path, total_cost, cost_per_node);
|
ASSERT_GT(total_cost, 0U);
|
|
// Set threshold to half the total cost so some nodes must be offloaded to CPU.
|
size_t half_limit_kb = (total_cost / 2) / 1024;
|
ASSERT_GT(half_limit_kb, 0U);
|
std::string limit_setting = std::to_string(half_limit_kb) + "," + stats_path.string();
|
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = false;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = false;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(
|
kOrtSessionOptionsResourceCudaPartitioningSettings, limit_setting.c_str()));
|
|
LoadWithResourceAwarePartitioning(model_path, sess_options, [](const Graph& graph) {
|
const auto& graph_nodes = graph.Nodes();
|
bool cpu_node_found = false;
|
for (const auto& node : graph_nodes) {
|
if (node.GetExecutionProviderType() != kCudaExecutionProvider) {
|
cpu_node_found = true;
|
break;
|
}
|
}
|
EXPECT_TRUE(cpu_node_found);
|
});
|
|
std::error_code remove_ec;
|
std::filesystem::remove(stats_path, remove_ec);
|
}
|
|
TEST(SessionStateTest, TestLayeringPartitioning) {
|
constexpr const ORTCHAR_T* model_path = ORT_TSTR("testdata/layering/tiny_gpt2_beamsearch_layering.onnx");
|
constexpr const char* layering_setting =
|
"cpu(Embed,Decode);gpu(GptAttention0,GptAttention1,GptAttention2,GptAttention3,GptAttention4)";
|
|
// Set the session options for layering
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = false;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = false;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(
|
kOrtSessionOptionsLayerAssignmentSettings, layering_setting));
|
|
LoadWithResourceAwarePartitioning(model_path, sess_options, [](const Graph& graph) {
|
const auto& graph_nodes = graph.Nodes();
|
for (const auto& node : graph_nodes) {
|
const std::string& name = node.Name();
|
const bool expected_on_cpu = (name.find("EmbedLayer") == 0) || (name == "LayerNorm_10") || (name == "MatMul_1165");
|
|
const std::string& ep = node.GetExecutionProviderType();
|
if (expected_on_cpu) {
|
EXPECT_EQ(ep, kCpuExecutionProvider) << "Node " << name << " expected on CPU but found on " << ep;
|
} else {
|
EXPECT_EQ(ep, kCudaExecutionProvider) << "Node " << name << " expected on CUDA but found on " << ep;
|
}
|
} }, layering_setting);
|
}
|
|
#endif // USE_CUDA
|
|
INSTANTIATE_TEST_SUITE_P(SessionStateTests, SessionStateTestP, testing::ValuesIn(param_list));
|
|
#ifndef ENABLE_TRAINING_CORE
|
class PrePackingTestOpKernel : public OpKernel {
|
public:
|
PrePackingTestOpKernel(const OpKernelInfo& info) : OpKernel(info) {}
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
|
Status UseSharedPrePackedBuffers(std::vector<BufferUniquePtr>& prepacked_buffers,
|
gsl::span<const size_t> /*prepacked_buffer_sizes*/,
|
int input_idx,
|
/*out*/ bool& used_shared_buffers) override {
|
ORT_UNUSED_PARAMETER(input_idx);
|
|
weight_packed_ = std::move(prepacked_buffers[0]);
|
used_shared_buffers = true;
|
++store_pre_packed_weight_calls_count;
|
return Status::OK();
|
}
|
|
Status PrePack(const Tensor& tensor, int input_idx, AllocatorPtr alloc,
|
/*out*/ bool& is_packed, /*out*/ PrePackedWeights* prepacked_weights) override {
|
ORT_UNUSED_PARAMETER(tensor);
|
ORT_UNUSED_PARAMETER(input_idx);
|
|
constexpr const size_t weight_packed_len = sizeof(float) * 2;
|
weight_packed_ = IAllocator::MakeUniquePtr<void>(alloc, weight_packed_len, true);
|
float* data_weights_packed = reinterpret_cast<float*>(weight_packed_.get());
|
data_weights_packed[0] = 1.2345f;
|
data_weights_packed[1] = data_weights_packed[0] * 2.f;
|
|
if (prepacked_weights != nullptr) {
|
prepacked_weights->buffers_.push_back(std::move(weight_packed_));
|
prepacked_weights->buffer_sizes_.push_back(weight_packed_len);
|
}
|
|
is_packed = true;
|
++prepack_calls_count;
|
return Status::OK();
|
}
|
|
int prepack_calls_count = 0;
|
int store_pre_packed_weight_calls_count = 0;
|
IAllocatorUniquePtr<void> weight_packed_;
|
};
|
|
class BrokenPrePackingTestOpKernel : public OpKernel {
|
public:
|
BrokenPrePackingTestOpKernel(const OpKernelInfo& info) : OpKernel(info) {}
|
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
|
Status PrePack(const Tensor& tensor, int input_idx, AllocatorPtr alloc,
|
/*out*/ bool& is_packed, /*out*/ PrePackedWeights* prepacked_weights) override {
|
ORT_UNUSED_PARAMETER(tensor);
|
ORT_UNUSED_PARAMETER(input_idx);
|
ORT_UNUSED_PARAMETER(alloc);
|
ORT_UNUSED_PARAMETER(prepacked_weights);
|
|
is_packed = true;
|
return Status::OK();
|
}
|
};
|
|
class ParallelPrepackTestState {
|
public:
|
void EnterPrePack(bool outer_parallelism_enabled) {
|
std::unique_lock<std::mutex> lock(mutex_);
|
++prepack_calls_;
|
outer_parallelism_observed_ = outer_parallelism_observed_ || outer_parallelism_enabled;
|
if (!outer_parallelism_enabled) {
|
return;
|
}
|
|
++active_calls_;
|
if (active_calls_ > 1) {
|
overlap_observed_ = true;
|
condition_.notify_all();
|
} else if (!wait_attempted_) {
|
wait_attempted_ = true;
|
condition_.wait_for(lock, std::chrono::seconds(5), [this]() { return overlap_observed_; });
|
}
|
--active_calls_;
|
}
|
|
bool OverlapObserved() const {
|
std::lock_guard<std::mutex> lock(mutex_);
|
return overlap_observed_;
|
}
|
|
bool OuterParallelismObserved() const {
|
std::lock_guard<std::mutex> lock(mutex_);
|
return outer_parallelism_observed_;
|
}
|
|
size_t PrePackCallCount() const {
|
std::lock_guard<std::mutex> lock(mutex_);
|
return prepack_calls_;
|
}
|
|
private:
|
mutable std::mutex mutex_;
|
std::condition_variable condition_;
|
size_t active_calls_{0};
|
bool wait_attempted_{false};
|
bool overlap_observed_{false};
|
bool outer_parallelism_observed_{false};
|
size_t prepack_calls_{0};
|
};
|
|
class ConcurrentPrePackingTestOpKernel : public OpKernel {
|
public:
|
ConcurrentPrePackingTestOpKernel(const OpKernelInfo& info, std::shared_ptr<ParallelPrepackTestState> state)
|
: OpKernel(info), state_(std::move(state)) {}
|
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
|
Status PrePack(const Tensor& tensor, int input_idx, AllocatorPtr alloc,
|
/*out*/ bool& is_packed, /*out*/ PrePackedWeights* prepacked_weights) override {
|
ORT_UNUSED_PARAMETER(tensor);
|
ORT_UNUSED_PARAMETER(input_idx);
|
ORT_UNUSED_PARAMETER(alloc);
|
ORT_UNUSED_PARAMETER(prepacked_weights);
|
|
state_->EnterPrePack(IsOuterPrePackParallelismEnabled());
|
is_packed = true;
|
return Status::OK();
|
}
|
|
private:
|
std::shared_ptr<ParallelPrepackTestState> state_;
|
};
|
|
class FailingPrePackTestState {
|
public:
|
void RecordPrePackCall() {
|
++prepack_calls_;
|
}
|
|
size_t PrePackCallCount() const {
|
return prepack_calls_;
|
}
|
|
private:
|
std::atomic<size_t> prepack_calls_{0};
|
};
|
|
class FailingPrePackingTestOpKernel : public OpKernel {
|
public:
|
FailingPrePackingTestOpKernel(const OpKernelInfo& info, std::shared_ptr<FailingPrePackTestState> state)
|
: OpKernel(info), state_(std::move(state)) {}
|
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
|
Status PrePack(const Tensor& tensor, int input_idx, AllocatorPtr alloc,
|
/*out*/ bool& is_packed, /*out*/ PrePackedWeights* prepacked_weights) override {
|
ORT_UNUSED_PARAMETER(tensor);
|
ORT_UNUSED_PARAMETER(input_idx);
|
ORT_UNUSED_PARAMETER(alloc);
|
ORT_UNUSED_PARAMETER(is_packed);
|
ORT_UNUSED_PARAMETER(prepacked_weights);
|
|
state_->RecordPrePackCall();
|
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "parallel prepack failure");
|
}
|
|
private:
|
std::shared_ptr<FailingPrePackTestState> state_;
|
};
|
|
// Coordinates a blocking PrePack() call with a test thread that raises the load-cancellation
|
// flag while workers are still inside PrePack(), then releases them. This exercises the
|
// post-join recheck in SessionState::PrepackConstantInitializedTensors, which is needed
|
// because a worker's initial cancellation check can pass before the flag is set.
|
class BlockingPrePackTestState {
|
public:
|
void EnterPrePack() {
|
std::unique_lock<std::mutex> lock(mutex_);
|
++entered_count_;
|
entered_condition_.notify_all();
|
release_condition_.wait(lock, [this]() { return released_; });
|
}
|
|
bool WaitForEntered(size_t count) {
|
std::unique_lock<std::mutex> lock(mutex_);
|
return entered_condition_.wait_for(lock, std::chrono::seconds(5),
|
[this, count]() { return entered_count_ >= count; });
|
}
|
|
void Release() {
|
std::lock_guard<std::mutex> lock(mutex_);
|
released_ = true;
|
release_condition_.notify_all();
|
}
|
|
private:
|
std::mutex mutex_;
|
std::condition_variable entered_condition_;
|
std::condition_variable release_condition_;
|
size_t entered_count_{0};
|
bool released_{false};
|
};
|
|
class BlockingPrePackingTestOpKernel : public OpKernel {
|
public:
|
BlockingPrePackingTestOpKernel(const OpKernelInfo& info, std::shared_ptr<BlockingPrePackTestState> state)
|
: OpKernel(info), state_(std::move(state)) {}
|
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
|
Status PrePack(const Tensor& tensor, int input_idx, AllocatorPtr alloc,
|
/*out*/ bool& is_packed, /*out*/ PrePackedWeights* prepacked_weights) override {
|
ORT_UNUSED_PARAMETER(tensor);
|
ORT_UNUSED_PARAMETER(input_idx);
|
ORT_UNUSED_PARAMETER(alloc);
|
ORT_UNUSED_PARAMETER(prepacked_weights);
|
|
state_->EnterPrePack();
|
is_packed = true;
|
return Status::OK();
|
}
|
|
private:
|
std::shared_ptr<BlockingPrePackTestState> state_;
|
};
|
|
#if !defined(ORT_NO_EXCEPTIONS)
|
class ThrowingPrePackingTestOpKernel : public OpKernel {
|
public:
|
ThrowingPrePackingTestOpKernel(const OpKernelInfo& info) : OpKernel(info) {}
|
|
Status Compute(OpKernelContext* context) const override {
|
ORT_UNUSED_PARAMETER(context);
|
return Status::OK();
|
}
|
|
Status PrePack(const Tensor& tensor, int input_idx, AllocatorPtr alloc,
|
/*out*/ bool& is_packed, /*out*/ PrePackedWeights* prepacked_weights) override {
|
ORT_UNUSED_PARAMETER(tensor);
|
ORT_UNUSED_PARAMETER(input_idx);
|
ORT_UNUSED_PARAMETER(alloc);
|
ORT_UNUSED_PARAMETER(is_packed);
|
ORT_UNUSED_PARAMETER(prepacked_weights);
|
ORT_THROW("parallel prepack failure");
|
}
|
};
|
#endif
|
|
static void CreateSimpleGraph(Graph& graph, const std::string& op_type = "PrePackingTest") {
|
// node creation and placement
|
TypeProto type;
|
type.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT);
|
type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
|
std::vector<onnxruntime::NodeArg*> inputs;
|
onnxruntime::NodeArg input_0_arg("node_0_input_0", &type);
|
onnxruntime::NodeArg input_1_arg("node_0_input_1", &type);
|
inputs.push_back(&input_0_arg);
|
inputs.push_back(&input_1_arg);
|
|
std::vector<onnxruntime::NodeArg*> outputs;
|
onnxruntime::NodeArg output_arg("node_0_output_0", &type);
|
outputs.push_back(&output_arg);
|
|
graph.AddNode("node_0", op_type, "node 0", inputs, outputs);
|
|
// add an initializer
|
ONNX_NAMESPACE::TensorProto tensor;
|
tensor.add_dims(1);
|
tensor.add_float_data(1.0f);
|
tensor.set_data_type(TensorProto_DataType_FLOAT);
|
tensor.set_name("node_0_input_1");
|
graph.AddInitializedTensor(tensor);
|
|
auto status = graph.Resolve();
|
ASSERT_TRUE(status.IsOK());
|
}
|
|
static void CreateMultiNodePrepackGraph(Graph& graph, const std::string& op_type, int node_count = 4) {
|
TypeProto type;
|
type.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT);
|
type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
|
for (int i = 0; i < node_count; ++i) {
|
const std::string prefix = "prepack_node_" + std::to_string(i);
|
NodeArg& input = graph.GetOrCreateNodeArg(prefix + "_input", &type);
|
NodeArg& initializer = graph.GetOrCreateNodeArg(prefix + "_initializer", &type);
|
NodeArg& output = graph.GetOrCreateNodeArg(prefix + "_output", &type);
|
graph.AddNode(prefix, op_type, "parallel prepack test node",
|
{&input, &initializer}, {&output});
|
|
ONNX_NAMESPACE::TensorProto tensor;
|
tensor.add_dims(1);
|
tensor.add_float_data(1.0f);
|
tensor.set_data_type(TensorProto_DataType_FLOAT);
|
tensor.set_name(initializer.Name());
|
graph.AddInitializedTensor(tensor);
|
}
|
ASSERT_STATUS_OK(graph.Resolve());
|
}
|
|
static const ONNX_NAMESPACE::GraphProto CreateSubgraph(bool then_branch) {
|
Model model(then_branch ? "If_then" : "If_else", false, DefaultLoggingManager().DefaultLogger());
|
auto& graph = model.MainGraph();
|
|
std::vector<NodeArg*> inputs;
|
std::vector<NodeArg*> outputs;
|
|
const std::string suffix = then_branch ? "0" : "1";
|
|
// graph input has to have type and rank even though it's an outer scope value.
|
TypeProto type_float;
|
type_float.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT);
|
type_float.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
|
// outer scope values
|
auto& if_shared = graph.GetOrCreateNodeArg("if_shared", &type_float);
|
auto& if_input = graph.GetOrCreateNodeArg("if_input_" + suffix, &type_float);
|
|
// add so that we don't end up with it being considered a graph input
|
graph.AddOuterScopeNodeArg("if_shared");
|
graph.AddOuterScopeNodeArg("if_input_" + suffix);
|
|
auto& if_out = graph.GetOrCreateNodeArg("if_output_" + suffix, &type_float);
|
|
inputs = {&if_shared, &if_input};
|
outputs = {&if_out};
|
|
graph.AddNode("if_node_" + suffix, "PrePackingTest", "if node " + suffix, inputs, outputs);
|
|
auto status = graph.Resolve();
|
EXPECT_EQ(status, Status::OK());
|
|
auto& proto = graph.ToGraphProto();
|
|
return proto;
|
}
|
|
static void CreateGraphWithSubgraph(Graph& graph) {
|
TypeProto type_float;
|
type_float.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT);
|
type_float.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
|
{
|
std::vector<onnxruntime::NodeArg*> inputs;
|
onnxruntime::NodeArg input_0_arg("if_input_0", &type_float);
|
onnxruntime::NodeArg input_1_arg("if_input_1", &type_float);
|
inputs.push_back(&input_0_arg);
|
inputs.push_back(&input_1_arg);
|
|
std::vector<onnxruntime::NodeArg*> outputs;
|
onnxruntime::NodeArg output_arg("node_0_output_0", &type_float);
|
outputs.push_back(&output_arg);
|
|
graph.AddNode("node_0", "PrePackingTest", "node 0", inputs, outputs);
|
}
|
|
{
|
TypeProto type_bool;
|
type_bool.mutable_tensor_type()->set_elem_type(TensorProto_DataType_BOOL);
|
type_bool.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
|
|
onnxruntime::NodeArg bool_arg("bool_arg", &type_bool);
|
|
std::vector<onnxruntime::NodeArg*> outputs;
|
onnxruntime::NodeArg output_arg("output_arg", &type_float);
|
outputs.push_back(&output_arg);
|
|
auto& if_node = graph.AddNode("if", "If", "If node", {&bool_arg}, outputs);
|
|
auto then_proto = CreateSubgraph(true);
|
auto else_proto = CreateSubgraph(false);
|
if_node.AddAttribute("then_branch", then_proto);
|
if_node.AddAttribute("else_branch", else_proto);
|
}
|
|
// add an initializer
|
ONNX_NAMESPACE::TensorProto tensor;
|
tensor.add_dims(1);
|
tensor.add_float_data(1.0f);
|
tensor.set_data_type(TensorProto_DataType_FLOAT);
|
tensor.set_name("if_shared");
|
graph.AddInitializedTensor(tensor);
|
|
auto status = graph.Resolve();
|
ASSERT_TRUE(status.IsOK());
|
}
|
|
static void PlaceAllNodesToCPUEP(Graph& graph) {
|
for (auto& node : graph.Nodes()) {
|
node.SetExecutionProviderType(kCpuExecutionProvider);
|
if (node.ContainsSubgraph()) {
|
for (auto& entry : node.GetAttributeNameToMutableSubgraphMap()) {
|
Graph* subgraph = entry.second;
|
PlaceAllNodesToCPUEP(*subgraph);
|
}
|
}
|
}
|
}
|
|
struct PrepackingTestParam {
|
bool test_subgraph;
|
bool test_prepacking;
|
};
|
|
namespace {
|
// The PrePackingTest schema is registered into the global ONNX schema registry. Register it only
|
// once to avoid duplicate-registration warnings when multiple tests/fixtures run in the same process.
|
void RegisterPrePackingTestSchemaOnce() {
|
static std::once_flag pre_packing_schema_registered;
|
std::call_once(pre_packing_schema_registered, []() {
|
ONNX_OPERATOR_SCHEMA(PrePackingTest)
|
.SetDoc("Faking Node for PrePacking")
|
.Input(0, "Input_0", "input 0", "tensor(float)")
|
.Input(1, "Input_1", "input 1", "tensor(float)")
|
.Output(0, "output_0", "docstr for output_0.", "tensor(float)");
|
ONNX_OPERATOR_SCHEMA(BrokenPrePackingTest)
|
.SetDoc("Faking broken Node for PrePacking")
|
.Input(0, "Input_0", "input 0", "tensor(float)")
|
.Input(1, "Input_1", "input 1", "tensor(float)")
|
.Output(0, "output_0", "docstr for output_0.", "tensor(float)");
|
ONNX_OPERATOR_SCHEMA(ConcurrentPrePackingTest)
|
.SetDoc("Faking nodes that detect concurrent PrePack calls")
|
.Input(0, "Input_0", "input 0", "tensor(float)")
|
.Input(1, "Input_1", "input 1", "tensor(float)")
|
.Output(0, "output_0", "docstr for output_0.", "tensor(float)");
|
ONNX_OPERATOR_SCHEMA(FailingPrePackingTest)
|
.SetDoc("Faking nodes that fail during parallel PrePack")
|
.Input(0, "Input_0", "input 0", "tensor(float)")
|
.Input(1, "Input_1", "input 1", "tensor(float)")
|
.Output(0, "output_0", "docstr for output_0.", "tensor(float)");
|
ONNX_OPERATOR_SCHEMA(BlockingPrePackingTest)
|
.SetDoc("Faking a node whose PrePack blocks until released by the test")
|
.Input(0, "Input_0", "input 0", "tensor(float)")
|
.Input(1, "Input_1", "input 1", "tensor(float)")
|
.Output(0, "output_0", "docstr for output_0.", "tensor(float)");
|
#if !defined(ORT_NO_EXCEPTIONS)
|
ONNX_OPERATOR_SCHEMA(ThrowingPrePackingTest)
|
.SetDoc("Faking a throwing node for parallel PrePack")
|
.Input(0, "Input_0", "input 0", "tensor(float)")
|
.Input(1, "Input_1", "input 1", "tensor(float)")
|
.Output(0, "output_0", "docstr for output_0.", "tensor(float)");
|
#endif
|
});
|
}
|
} // namespace
|
|
class SessionStatePrepackingTest : public testing::TestWithParam<PrepackingTestParam> {};
|
TEST_P(SessionStatePrepackingTest, PrePackingTest) {
|
PrepackingTestParam test_param = GetParam();
|
|
OrtThreadPoolParams to{};
|
// Use a small, fixed intra-op pool size to keep thread/memory overhead low (e.g., under ASan)
|
// while still exercising the non-null threadpool path.
|
to.thread_pool_size = 2;
|
auto tp = concurrency::CreateThreadPool(&onnxruntime::Env::Default(), to, concurrency::ThreadPoolType::INTRA_OP);
|
RegisterPrePackingTestSchemaOnce();
|
|
ExecutionProviders execution_providers;
|
auto cpu_execution_provider = std::make_unique<CPUExecutionProvider>(CPUExecutionProviderInfo(false));
|
ASSERT_STATUS_OK(execution_providers.Add(kCpuExecutionProvider, std::move(cpu_execution_provider)));
|
|
DataTransferManager dtm;
|
ExternalDataLoaderManager edlm;
|
profiling::Profiler profiler;
|
|
std::unordered_map<std::string, int> domain_to_version;
|
domain_to_version[kOnnxDomain] = 11;
|
Model model("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
// onnxruntime::Model model("graph_main", false, DefaultLoggingManager().DefaultLogger());
|
if (test_param.test_subgraph) {
|
CreateGraphWithSubgraph(model.MainGraph());
|
} else {
|
CreateSimpleGraph(model.MainGraph());
|
}
|
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] =
|
test_param.test_prepacking ? "0" : "1";
|
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
KernelRegistryManager kernel_registry_manager;
|
Status status = kernel_registry_manager.RegisterKernels(execution_providers);
|
ASSERT_TRUE(status.IsOK()) << status.ErrorMessage();
|
std::shared_ptr<KernelRegistry> kernel_registry = std::make_shared<KernelRegistry>();
|
auto kernel_def = KernelDefBuilder().SetName("PrePackingTest").Provider(kCpuExecutionProvider).SinceVersion(1).Build();
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(kernel_def),
|
[](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<PrePackingTestOpKernel>(info);
|
return Status::OK();
|
})));
|
kernel_registry_manager.RegisterKernelRegistry(kernel_registry);
|
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
const auto& const_initialized_tensors = session_state.GetConstantInitializedTensors();
|
// check prepacking
|
ASSERT_EQ(const_initialized_tensors.size(), size_t(test_param.test_prepacking ? 0 : 1));
|
}
|
|
class SessionStateTestSharedInitalizersWithPrePacking : public ::testing::Test {
|
protected:
|
ExecutionProviders execution_providers;
|
std::unordered_map<std::string, int> domain_to_version;
|
DataTransferManager dtm;
|
ExternalDataLoaderManager edlm;
|
profiling::Profiler profiler;
|
KernelRegistryManager kernel_registry_manager;
|
std::unique_ptr<concurrency::ThreadPool> tp;
|
std::shared_ptr<ParallelPrepackTestState> parallel_prepack_test_state;
|
std::shared_ptr<FailingPrePackTestState> failing_prepack_test_state;
|
std::shared_ptr<BlockingPrePackTestState> blocking_prepack_test_state;
|
|
void SetUp() override {
|
OrtThreadPoolParams to{};
|
// Use a small, fixed intra-op pool size to keep thread/memory overhead low (e.g., under ASan).
|
to.thread_pool_size = 2;
|
tp = concurrency::CreateThreadPool(&onnxruntime::Env::Default(), to, concurrency::ThreadPoolType::INTRA_OP);
|
parallel_prepack_test_state = std::make_shared<ParallelPrepackTestState>();
|
failing_prepack_test_state = std::make_shared<FailingPrePackTestState>();
|
blocking_prepack_test_state = std::make_shared<BlockingPrePackTestState>();
|
RegisterPrePackingTestSchemaOnce();
|
|
auto cpu_execution_provider = std::make_unique<CPUExecutionProvider>(CPUExecutionProviderInfo(false));
|
ASSERT_STATUS_OK(execution_providers.Add(kCpuExecutionProvider, std::move(cpu_execution_provider)));
|
|
domain_to_version[kOnnxDomain] = 11;
|
|
ASSERT_STATUS_OK(kernel_registry_manager.RegisterKernels(execution_providers));
|
std::shared_ptr<KernelRegistry> kernel_registry = std::make_shared<KernelRegistry>();
|
|
auto kernel_def = KernelDefBuilder()
|
.SetName("PrePackingTest")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(kernel_def),
|
[](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status { out = std::make_unique<PrePackingTestOpKernel>(info); return Status::OK(); })));
|
|
auto broken_kernel_def = KernelDefBuilder()
|
.SetName("BrokenPrePackingTest")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(broken_kernel_def),
|
[](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<BrokenPrePackingTestOpKernel>(info);
|
return Status::OK();
|
})));
|
|
auto concurrent_kernel_def = KernelDefBuilder()
|
.SetName("ConcurrentPrePackingTest")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(concurrent_kernel_def),
|
[state = parallel_prepack_test_state](
|
FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<ConcurrentPrePackingTestOpKernel>(info, state);
|
return Status::OK();
|
})));
|
|
auto failing_kernel_def = KernelDefBuilder()
|
.SetName("FailingPrePackingTest")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(failing_kernel_def),
|
[state = failing_prepack_test_state](
|
FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<FailingPrePackingTestOpKernel>(info, state);
|
return Status::OK();
|
})));
|
|
auto blocking_kernel_def = KernelDefBuilder()
|
.SetName("BlockingPrePackingTest")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(blocking_kernel_def),
|
[state = blocking_prepack_test_state](
|
FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<BlockingPrePackingTestOpKernel>(info, state);
|
return Status::OK();
|
})));
|
|
#if !defined(ORT_NO_EXCEPTIONS)
|
auto throwing_kernel_def = KernelDefBuilder()
|
.SetName("ThrowingPrePackingTest")
|
.Provider(kCpuExecutionProvider)
|
.SinceVersion(1)
|
.Build();
|
|
ASSERT_STATUS_OK(kernel_registry->Register(
|
KernelCreateInfo(std::move(throwing_kernel_def),
|
[](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) -> Status {
|
out = std::make_unique<ThrowingPrePackingTestOpKernel>(info);
|
return Status::OK();
|
})));
|
#endif
|
|
kernel_registry_manager.RegisterKernelRegistry(kernel_registry);
|
}
|
};
|
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, ParallelPrepackConvertsExceptionsToStatus) {
|
#if defined(ORT_NO_EXCEPTIONS)
|
GTEST_SKIP() << "Exceptions are disabled.";
|
#else
|
SessionOptions sess_options;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(kOrtSessionOptionsEnableParallelPrepack, "1"));
|
|
Model model("parallel_prepack_exception", false, ModelMetaData(), PathString(),
|
IOnnxRuntimeOpSchemaRegistryList(), domain_to_version,
|
std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
CreateMultiNodePrepackGraph(model.MainGraph(), "ThrowingPrePackingTest");
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_NOT_OK_AND_HAS_SUBSTR(
|
session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(), kernel_registry_manager),
|
"parallel prepack failure");
|
#endif
|
}
|
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, ParallelPrepackCallsOverlap) {
|
SessionOptions sess_options;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(kOrtSessionOptionsEnableParallelPrepack, "1"));
|
|
Model model("parallel_prepack_overlap", false, ModelMetaData(), PathString(),
|
IOnnxRuntimeOpSchemaRegistryList(), domain_to_version,
|
std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
CreateMultiNodePrepackGraph(model.MainGraph(), "ConcurrentPrePackingTest");
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
EXPECT_TRUE(parallel_prepack_test_state->OverlapObserved());
|
EXPECT_TRUE(parallel_prepack_test_state->OuterParallelismObserved());
|
EXPECT_EQ(parallel_prepack_test_state->PrePackCallCount(), 4U);
|
}
|
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, ParallelPrepackFailureStopsPendingWork) {
|
SessionOptions sess_options;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(kOrtSessionOptionsEnableParallelPrepack, "1"));
|
|
Model model("parallel_prepack_failure", false, ModelMetaData(), PathString(),
|
IOnnxRuntimeOpSchemaRegistryList(), domain_to_version,
|
std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
CreateMultiNodePrepackGraph(model.MainGraph(), "FailingPrePackingTest");
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_NOT_OK_AND_HAS_SUBSTR(
|
session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(), kernel_registry_manager),
|
"parallel prepack failure");
|
EXPECT_LT(failing_prepack_test_state->PrePackCallCount(), 4U);
|
}
|
|
// Regression test for the post-join recheck in SessionState::PrepackConstantInitializedTensors:
|
// a worker's initial cancellation check can pass before the flag is set, letting it spend time
|
// inside PrePack() while cancellation_requested stays false. Sets the flag while workers are
|
// blocked inside PrePack(), releases them, and asserts the load is reported as canceled.
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, ParallelPrepackCancellationDuringPrePackReturnsCanceled) {
|
SessionOptions sess_options;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(kOrtSessionOptionsEnableParallelPrepack, "1"));
|
|
Model model("parallel_prepack_cancel_mid_prepack", false, ModelMetaData(), PathString(),
|
IOnnxRuntimeOpSchemaRegistryList(), domain_to_version,
|
std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
CreateMultiNodePrepackGraph(model.MainGraph(), "BlockingPrePackingTest", 2);
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
bool workers_entered = false;
|
std::thread canceller([&]() {
|
// Wait until both workers are blocked inside PrePack() before requesting cancellation,
|
// then release them so FinalizeSessionState can join and recheck the flag.
|
workers_entered = blocking_prepack_test_state->WaitForEntered(2);
|
if (workers_entered) {
|
sess_options.SetLoadCancellationFlag(true);
|
}
|
blocking_prepack_test_state->Release();
|
});
|
|
Status status = session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(), kernel_registry_manager);
|
canceller.join();
|
|
ASSERT_TRUE(workers_entered);
|
ASSERT_FALSE(status.IsOK());
|
EXPECT_EQ(status.Category(), common::ONNXRUNTIME);
|
EXPECT_EQ(status.Code(), common::MODEL_LOAD_CANCELED);
|
}
|
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, SingleNodePrepackDoesNotUseOuterParallelism) {
|
SessionOptions sess_options;
|
ASSERT_STATUS_OK(sess_options.config_options.AddConfigEntry(kOrtSessionOptionsEnableParallelPrepack, "1"));
|
|
Model model("single_node_prepack", false, ModelMetaData(), PathString(),
|
IOnnxRuntimeOpSchemaRegistryList(), domain_to_version,
|
std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
CreateMultiNodePrepackGraph(model.MainGraph(), "ConcurrentPrePackingTest", 1);
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
EXPECT_FALSE(parallel_prepack_test_state->OuterParallelismObserved());
|
EXPECT_EQ(parallel_prepack_test_state->PrePackCallCount(), 1U);
|
}
|
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, OuterParallelPrepackIsDisabledByDefault) {
|
SessionOptions sess_options;
|
|
Model model("default_prepack", false, ModelMetaData(), PathString(),
|
IOnnxRuntimeOpSchemaRegistryList(), domain_to_version,
|
std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
CreateMultiNodePrepackGraph(model.MainGraph(), "ConcurrentPrePackingTest");
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
EXPECT_FALSE(parallel_prepack_test_state->OuterParallelismObserved());
|
EXPECT_EQ(parallel_prepack_test_state->PrePackCallCount(), 4U);
|
}
|
|
// Pre-packing enabled + no shared initializers, however, we put all the pre-packs
|
// in a session_state container for ownership.
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, test1) {
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
// First session/model
|
Model model_1("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateSimpleGraph(model_1.MainGraph());
|
PlaceAllNodesToCPUEP(model_1.MainGraph());
|
SessionState session_state_1(model_1.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_OK(session_state_1.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
const auto* kernel = reinterpret_cast<const PrePackingTestOpKernel*>(session_state_1.GetKernel(0));
|
|
// Assert that a pre-pack call was made. However, they sharing call is still made from a serialized container.
|
ASSERT_EQ(session_state_1.GetNumberOfPrepacksCounter(), static_cast<size_t>(1));
|
ASSERT_EQ(kernel->prepack_calls_count, 1);
|
// In this case the sharing comes from the serialized container
|
ASSERT_EQ(kernel->store_pre_packed_weight_calls_count, 1);
|
|
// Second session/model
|
Model model_2("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateSimpleGraph(model_2.MainGraph());
|
PlaceAllNodesToCPUEP(model_2.MainGraph());
|
SessionState session_state_2(model_2.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_OK(session_state_2.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
kernel = reinterpret_cast<const PrePackingTestOpKernel*>(session_state_2.GetKernel(0));
|
|
// Assert that a pre-pack call was made. The weights are still shared from the serialized container
|
// either because they are loaded from disk or because the container takes ownership of them.
|
ASSERT_EQ(session_state_2.GetNumberOfPrepacksCounter(), static_cast<size_t>(1));
|
ASSERT_EQ(kernel->prepack_calls_count, 1);
|
ASSERT_EQ(kernel->store_pre_packed_weight_calls_count, 1);
|
}
|
|
// Pre-packing enabled + shared initializers + no pre-packed weights container = no pre-packed weights caching
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, test2) {
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
// Enable shared initializer
|
OrtMemoryInfo mem_info(CPU, OrtDeviceAllocator);
|
std::vector<float> float_data(1, 1);
|
auto value = std::make_unique<OrtValue>();
|
Tensor::InitOrtValue(DataTypeImpl::GetType<float>(), TensorShape(std::vector<int64_t>{1}),
|
float_data.data(), mem_info, *value);
|
|
ASSERT_STATUS_OK(sess_options.AddInitializer("node_0_input_1", value.get()));
|
|
// First session/model
|
Model model_1("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateSimpleGraph(model_1.MainGraph());
|
PlaceAllNodesToCPUEP(model_1.MainGraph());
|
SessionState session_state_1(model_1.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_OK(session_state_1.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
const auto* kernel = reinterpret_cast<const PrePackingTestOpKernel*>(session_state_1.GetKernel(0));
|
|
// Assert that a pre-pack call was made, but sharing still takes place from the serialized container
|
ASSERT_EQ(session_state_1.GetNumberOfPrepacksCounter(), static_cast<size_t>(1));
|
ASSERT_EQ(kernel->prepack_calls_count, 1);
|
ASSERT_EQ(kernel->store_pre_packed_weight_calls_count, 1);
|
|
// Second session/model
|
Model model_2("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateSimpleGraph(model_2.MainGraph());
|
PlaceAllNodesToCPUEP(model_2.MainGraph());
|
SessionState session_state_2(model_2.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options);
|
|
ASSERT_STATUS_OK(session_state_2.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
kernel = reinterpret_cast<const PrePackingTestOpKernel*>(session_state_2.GetKernel(0));
|
|
// Assert that a pre-pack call was made, but sharing still takes place from the serialized container
|
ASSERT_EQ(session_state_2.GetNumberOfPrepacksCounter(), static_cast<size_t>(1));
|
ASSERT_EQ(kernel->prepack_calls_count, 1);
|
ASSERT_EQ(kernel->store_pre_packed_weight_calls_count, 1);
|
}
|
|
// Pre-packing enabled + shared initializers + pre-packed weights container = pre-packed weights caching enabled
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, test3) {
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
// Enable shared initializer
|
OrtMemoryInfo mem_info(CPU, OrtDeviceAllocator);
|
std::vector<float> float_data(1, 1);
|
auto value = std::make_unique<OrtValue>();
|
Tensor::InitOrtValue(DataTypeImpl::GetType<float>(), TensorShape(std::vector<int64_t>{1}),
|
reinterpret_cast<void*>(float_data.data()), mem_info, *value);
|
|
ASSERT_STATUS_OK(sess_options.AddInitializer("node_0_input_1", value.get()));
|
|
// Enable pre-packed weights container
|
PrepackedWeightsContainer prepacked_weights_container;
|
|
// First session/model
|
Model model_1("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateSimpleGraph(model_1.MainGraph());
|
PlaceAllNodesToCPUEP(model_1.MainGraph());
|
SessionState session_state_1(model_1.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
ASSERT_STATUS_OK(session_state_1.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
const auto* kernel = reinterpret_cast<const PrePackingTestOpKernel*>(session_state_1.GetKernel(0));
|
// Assert that a pre-pack call was made
|
ASSERT_EQ(session_state_1.GetNumberOfPrepacksCounter(), static_cast<size_t>(1));
|
ASSERT_EQ(kernel->prepack_calls_count, 1);
|
// Assert that we made a call to store pre-packed weight from a shared container
|
ASSERT_EQ(kernel->store_pre_packed_weight_calls_count, 1);
|
// The weight to be "stored" is the same weight that we got by invoking PrePack() in the step above.
|
// Hence, assert that it wasn't a "cached" pre-packed weight (i.e.) pre-packed weight
|
// from another instance of the same op_type consuming the same constant initializer.
|
ASSERT_EQ(session_state_1.GetUsedSharedPrePackedWeightCounter(), static_cast<size_t>(0));
|
|
// Second session/model
|
Model model_2("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateSimpleGraph(model_2.MainGraph());
|
PlaceAllNodesToCPUEP(model_2.MainGraph());
|
SessionState session_state_2(model_2.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
ASSERT_STATUS_OK(session_state_2.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
// Assert that a pre-pack call was made
|
ASSERT_EQ(session_state_2.GetNumberOfPrepacksCounter(), static_cast<size_t>(1));
|
ASSERT_EQ(kernel->prepack_calls_count, 1);
|
// Assert that we made a call to store pre-packed weight from a shared container
|
ASSERT_EQ(kernel->store_pre_packed_weight_calls_count, 1);
|
// The weight to be "stored" is a "cached" weight (i.e.) a pre-packed weight
|
// from another instance of the same op_type consuming the same constant initializer.
|
// Assert this.
|
ASSERT_EQ(session_state_2.GetUsedSharedPrePackedWeightCounter(), static_cast<size_t>(1));
|
}
|
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, BrokenKernelWithoutCacheableBuffersFails) {
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
OrtMemoryInfo mem_info(CPU, OrtDeviceAllocator);
|
std::vector<float> float_data(1, 1);
|
auto value = std::make_unique<OrtValue>();
|
Tensor::InitOrtValue(DataTypeImpl::GetType<float>(), TensorShape(std::vector<int64_t>{1}),
|
reinterpret_cast<void*>(float_data.data()), mem_info, *value);
|
|
ASSERT_STATUS_OK(sess_options.AddInitializer("node_0_input_1", value.get()));
|
|
PrepackedWeightsContainer prepacked_weights_container;
|
|
Model model("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateSimpleGraph(model.MainGraph(), "BrokenPrePackingTest");
|
PlaceAllNodesToCPUEP(model.MainGraph());
|
SessionState session_state(model.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
ASSERT_STATUS_NOT_OK_AND_HAS_SUBSTR(
|
session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(), kernel_registry_manager),
|
"doesn't have an implementation that can cache computed pre-packed weights");
|
}
|
|
// Pre-packing enabled + shared initializers +
|
// pre-packed weights container + subgraphs =
|
// caching enabled in pre-packed weights used in subgraphs
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, test4) {
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
// Enable shared initializer
|
OrtMemoryInfo mem_info(CPU, OrtDeviceAllocator);
|
std::vector<float> float_data(1, 1);
|
auto value = std::make_unique<OrtValue>();
|
Tensor::InitOrtValue(DataTypeImpl::GetType<float>(), TensorShape(std::vector<int64_t>{1}),
|
reinterpret_cast<void*>(float_data.data()), mem_info, *value);
|
|
ASSERT_STATUS_OK(sess_options.AddInitializer("if_shared", value.get()));
|
|
// Enable pre-packed weights container
|
PrepackedWeightsContainer prepacked_weights_container;
|
|
// First session/model
|
Model model_1("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateGraphWithSubgraph(model_1.MainGraph());
|
PlaceAllNodesToCPUEP(model_1.MainGraph());
|
SessionState session_state_1(model_1.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
ASSERT_STATUS_OK(session_state_1.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
// At the main graph level, there should be no pre-packing calls as there are
|
// no initializers (shared or otherwise) consumed by any nodes in the main graph
|
ASSERT_EQ(session_state_1.GetNumberOfPrepacksCounter(), static_cast<size_t>(0));
|
|
auto if_index_1 = 1;
|
if (session_state_1.GetKernel(0)->Node().OpType() == "If") {
|
if_index_1 = 0;
|
}
|
|
const auto& subgraph_session_states = session_state_1.GetSubgraphSessionStateMap();
|
const auto& if_node_session_states = subgraph_session_states.at(if_index_1);
|
const auto& session_state_1_then_branch_session_state = *if_node_session_states.at("then_branch");
|
const auto& session_state_1_else_branch_session_state = *if_node_session_states.at("else_branch");
|
|
auto if_node_branches_prepack_counter_1 =
|
session_state_1_then_branch_session_state.GetNumberOfPrepacksCounter() +
|
session_state_1_else_branch_session_state.GetNumberOfPrepacksCounter();
|
|
// We should be seeing 2 pre-pack calls in the "If" node (one in each subgraph)
|
ASSERT_EQ(if_node_branches_prepack_counter_1, static_cast<size_t>(2));
|
|
auto if_node_branches_shared_prepack_counter_1 =
|
session_state_1_then_branch_session_state.GetUsedSharedPrePackedWeightCounter() +
|
session_state_1_else_branch_session_state.GetUsedSharedPrePackedWeightCounter();
|
|
// We should only be seeing 1 shared pre-pack weights usage in the "If" node
|
// Either the "then branch" or "else branch" will be using the shared version
|
// depending on which branch writes to the shared container
|
ASSERT_EQ(if_node_branches_shared_prepack_counter_1, static_cast<size_t>(1));
|
|
// Second session/model
|
Model model_2("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateGraphWithSubgraph(model_2.MainGraph());
|
PlaceAllNodesToCPUEP(model_2.MainGraph());
|
SessionState session_state_2(model_2.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
ASSERT_STATUS_OK(session_state_2.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager));
|
|
// At the main graph level, there should be no pre-packing calls as there are
|
// no initializers (shared or otherwise) consumed by any nodes in the main graph
|
ASSERT_EQ(session_state_2.GetNumberOfPrepacksCounter(), static_cast<size_t>(0));
|
|
auto if_index_2 = 1;
|
if (session_state_2.GetKernel(0)->Node().OpType() == "If") {
|
if_index_2 = 0;
|
}
|
|
const auto& subgraph_session_states_2 = session_state_2.GetSubgraphSessionStateMap();
|
const auto& if_node_session_states_2 = subgraph_session_states_2.at(if_index_2);
|
const auto& session_state_2_then_branch_session_state = *if_node_session_states_2.at("then_branch");
|
const auto& session_state_2_else_branch_session_state = *if_node_session_states_2.at("else_branch");
|
|
auto if_node_branches_prepack_counter_2 =
|
session_state_2_then_branch_session_state.GetNumberOfPrepacksCounter() +
|
session_state_2_else_branch_session_state.GetNumberOfPrepacksCounter();
|
|
// We should be seeing 2 pre-pack calls in the "If" node (one in each subgraph)
|
ASSERT_EQ(if_node_branches_prepack_counter_2, static_cast<size_t>(2));
|
|
auto if_node_branches_shared_prepack_counter_2 =
|
session_state_2_then_branch_session_state.GetUsedSharedPrePackedWeightCounter() +
|
session_state_2_else_branch_session_state.GetUsedSharedPrePackedWeightCounter();
|
|
// We should be seeing 2 shared pre-pack weights calls in the "If" node
|
// Both branches will be using the shared version coming from the first model.
|
ASSERT_EQ(if_node_branches_shared_prepack_counter_2, static_cast<size_t>(2));
|
}
|
|
#ifndef __wasm__
|
// sharing is on
|
TEST_F(SessionStateTestSharedInitalizersWithPrePacking, TestPrepackedSerialization) {
|
const std::filesystem::path model_with_external_initializers =
|
"testdata/test_prepacked_serialization_optimized_model.onnx";
|
|
const std::filesystem::path external_initializers_file =
|
"test_prepacked_serialization_optimized_model.bin";
|
|
{
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
sess_options.optimized_model_filepath = model_with_external_initializers;
|
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
// Enable saving model with pre-packed weights
|
sess_options.config_options.configurations[kOrtSessionOptionsSavePrePackedConstantInitializers] = "1";
|
|
// Enable shared initializer
|
OrtMemoryInfo mem_info(CPU, OrtDeviceAllocator);
|
std::vector<float> float_data(1, 1);
|
auto value = std::make_unique<OrtValue>();
|
Tensor::InitOrtValue(DataTypeImpl::GetType<float>(), TensorShape(std::vector<int64_t>{1}),
|
reinterpret_cast<void*>(float_data.data()), mem_info, *value);
|
|
ASSERT_STATUS_OK(sess_options.AddInitializer("if_shared", value.get()));
|
|
// Enable pre-packed weights container for shared initializers
|
PrepackedWeightsContainer prepacked_weights_container;
|
Model model_1("graph_main", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),
|
domain_to_version, std::vector<ONNX_NAMESPACE::FunctionProto>(),
|
DefaultLoggingManager().DefaultLogger());
|
|
CreateGraphWithSubgraph(model_1.MainGraph());
|
PlaceAllNodesToCPUEP(model_1.MainGraph());
|
SessionState session_state_1(model_1.MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
constexpr const bool saving_model_true = true;
|
|
ASSERT_STATUS_OK(session_state_1.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager,
|
!saving_model_true));
|
|
TestSavedPrepacks(model_1);
|
|
ModelSavingOptions model_saving_options{4};
|
model_saving_options.align_offset = true;
|
|
ASSERT_STATUS_OK(Model::SaveWithExternalInitializers(model_1, model_with_external_initializers,
|
external_initializers_file,
|
model_saving_options));
|
}
|
ScopedFileDeleter test_model_deleter(model_with_external_initializers);
|
ScopedFileDeleter binary_file_deleter(external_initializers_file);
|
|
// Now let's load the model along with the initializers
|
{
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
// We are expecting this weight to be loaded from disk along
|
// with its pre-packed version
|
// Enable shared initializer
|
OrtMemoryInfo mem_info(CPU, OrtDeviceAllocator);
|
std::vector<float> float_data(1, 1);
|
auto value = std::make_unique<OrtValue>();
|
Tensor::InitOrtValue(DataTypeImpl::GetType<float>(), TensorShape(std::vector<int64_t>{1}),
|
reinterpret_cast<void*>(float_data.data()), mem_info, *value);
|
|
ASSERT_STATUS_OK(sess_options.AddInitializer("if_shared", value.get()));
|
|
// Enable pre-packed weights container for shared initializers
|
PrepackedWeightsContainer prepacked_weights_container;
|
|
std::shared_ptr<Model> model;
|
ASSERT_STATUS_OK(Model::Load(model_with_external_initializers, model, nullptr,
|
DefaultLoggingManager().DefaultLogger()));
|
|
PlaceAllNodesToCPUEP(model->MainGraph());
|
SessionState session_state(model->MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(std::basic_string<PATH_CHAR_TYPE>(),
|
kernel_registry_manager,
|
false));
|
|
TestLoadedSharedUserSupplied(*model);
|
}
|
|
// Load again, this time sharing is enabled, but no shared initializer in the map
|
{
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
// Enable pre-packed weights container for shared initializers
|
PrepackedWeightsContainer prepacked_weights_container;
|
|
std::shared_ptr<Model> model;
|
ASSERT_STATUS_OK(Model::Load(model_with_external_initializers, model, nullptr,
|
DefaultLoggingManager().DefaultLogger()));
|
|
PlaceAllNodesToCPUEP(model->MainGraph());
|
SessionState session_state(model->MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
&prepacked_weights_container);
|
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(model_with_external_initializers,
|
kernel_registry_manager,
|
false));
|
|
TestLoadedSharedNoUserSupplied(*model);
|
}
|
// Load again, sharing is disabled
|
{
|
SessionOptions sess_options;
|
sess_options.enable_mem_pattern = true;
|
sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
|
sess_options.use_deterministic_compute = false;
|
sess_options.enable_mem_reuse = true;
|
|
// Enable pre-packing
|
sess_options.config_options.configurations[kOrtSessionOptionsConfigDisablePrepacking] = "0";
|
|
std::shared_ptr<Model> model;
|
ASSERT_STATUS_OK(Model::Load(model_with_external_initializers, model, nullptr,
|
DefaultLoggingManager().DefaultLogger()));
|
|
PlaceAllNodesToCPUEP(model->MainGraph());
|
SessionState session_state(model->MainGraph(),
|
execution_providers,
|
tp.get(),
|
nullptr, /*inter_op_thread_pool*/
|
dtm,
|
edlm,
|
DefaultLoggingManager().DefaultLogger(),
|
profiler,
|
sess_options,
|
nullptr);
|
|
ASSERT_STATUS_OK(session_state.FinalizeSessionState(model_with_external_initializers,
|
kernel_registry_manager,
|
false));
|
|
const auto& prepacked_for_main_graph = model->MainGraph().GetPrepacked();
|
ASSERT_FALSE(prepacked_for_main_graph.IsSaveModeOn());
|
ASSERT_EQ(1U, prepacked_for_main_graph.GetKeyToBlob().size());
|
}
|
}
|
#endif // __wasm__
|
|
INSTANTIATE_TEST_SUITE_P(SessionStateTests,
|
SessionStatePrepackingTest,
|
testing::Values(PrepackingTestParam{false, false},
|
PrepackingTestParam{false, true},
|
PrepackingTestParam{true, false},
|
PrepackingTestParam{true, true}));
|
#endif
|
} // namespace test
|
} // namespace onnxruntime
|