// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "core/session/onnxruntime_cxx_api.h" #include "onnxruntime_session_options_config_keys.h" #include "core/common/narrow.h" #include "test/util/include/asserts.h" #include #include #include #include "test_fixture.h" #include "file_util.h" #include "gmock/gmock.h" #ifdef _WIN32 #include #else #include #include #include #include "core/platform/scoped_resource.h" #endif extern std::unique_ptr ort_env; namespace onnxruntime { namespace test { // disable for minimal build with no exceptions as it will always attempt to throw in that scenario #if !defined(ORT_MINIMAL_BUILD) && !defined(ORT_NO_EXCEPTIONS) TEST(CApiTest, model_from_array) { const char* model_path = "testdata/matmul_1.onnx"; std::vector buffer; { std::ifstream file(model_path, std::ios::binary | std::ios::ate); if (!file) ORT_THROW("Error reading model"); buffer.resize(narrow(file.tellg())); file.seekg(0, std::ios::beg); if (!file.read(buffer.data(), buffer.size())) ORT_THROW("Error reading model"); } #if (!ORT_MINIMAL_BUILD) bool should_throw = false; #else bool should_throw = true; #endif auto create_session = [&](Ort::SessionOptions& so) { try { Ort::Session session(*ort_env.get(), buffer.data(), buffer.size(), so); ASSERT_FALSE(should_throw) << "Creation of session should have thrown"; } catch (const std::exception& ex) { ASSERT_TRUE(should_throw) << "Creation of session should not have thrown. Exception:" << ex.what(); ASSERT_THAT(ex.what(), testing::HasSubstr("ONNX format model is not supported in this build.")); } }; Ort::SessionOptions so; create_session(so); #ifdef USE_CUDA Ort::CUDAProviderOptions options; so.AppendExecutionProvider_CUDA_V2(*options); create_session(so); #endif } TEST(CApiTest, model_from_array_rejects_oversized_input) { const char dummy_model_data[] = "invalid"; const size_t oversized_length = static_cast(std::numeric_limits::max()) + 1ULL; Ort::SessionOptions so; try { Ort::Session session(*ort_env.get(), dummy_model_data, oversized_length, so); FAIL() << "Creation of session should have thrown exception"; } catch (const Ort::Exception& ex) { EXPECT_EQ(ex.GetOrtErrorCode(), ORT_INVALID_ARGUMENT); ASSERT_THAT(ex.what(), testing::HasSubstr("maximum supported size")); } } #if !defined(ORT_MINIMAL_BUILD) && !defined(ORT_EXTENDED_MINIMAL_BUILD) TEST(CApiTest, session_options_empty_affinity_string) { Ort::SessionOptions options; options.AddConfigEntry(kOrtSessionOptionsConfigIntraOpThreadAffinities, ""); constexpr auto model_path = ORT_TSTR("testdata/matmul_1.onnx"); try { Ort::Session session(*ort_env.get(), model_path, options); ASSERT_TRUE(false) << "Creation of session should have thrown exception"; } catch (const std::exception& ex) { ASSERT_THAT(ex.what(), testing::HasSubstr("Affinity string must not be empty")); } } #endif #endif #ifdef DISABLE_EXTERNAL_INITIALIZERS TEST(CApiTest, TestDisableExternalInitiliazers) { constexpr auto model_path = ORT_TSTR("testdata/model_with_external_initializers.onnx"); Ort::SessionOptions so; try { Ort::Session session(*ort_env.get(), model_path, so); ASSERT_TRUE(false) << "Creation of session should have thrown exception"; } catch (const std::exception& ex) { ASSERT_THAT(ex.what(), testing::HasSubstr("Initializer tensors with external data is not allowed.")); } } #elif !defined(ORT_MINIMAL_BUILD) TEST(CApiTest, TestExternalInitializersInjection) { constexpr auto model_path = ORT_TSTR("testdata/model_with_external_initializer_come_from_user.onnx"); std::array Pads_not_on_disk{0, 0, 1, 1}; constexpr std::array init_shape{4}; const std::vector init_names{"Pads_not_on_disk"}; std::vector initializer_data; auto cpu_mem_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU); auto init_tensor = Ort::Value::CreateTensor(cpu_mem_info, Pads_not_on_disk.data(), Pads_not_on_disk.size(), init_shape.data(), init_shape.size()); initializer_data.push_back(std::move(init_tensor)); Ort::SessionOptions so; const ORTCHAR_T* optimized_model_path = ORT_TSTR("testdata/model_with_external_initializer_come_from_user_opt.onnx"); so.SetOptimizedModelFilePath(optimized_model_path); so.AddExternalInitializers(init_names, initializer_data); // Dump the optimized model with external data so that it will unpack the external data from the loaded model so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersFileName, "model_with_external_initializer_come_from_user_opt.bin"); so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersMinSizeInBytes, "10"); EXPECT_NO_THROW(Ort::Session(*ort_env, model_path, so)); } static void ReadFileToBuffer(const char* file_path, std::vector& buffer) { std::ifstream file(file_path, std::ios::binary | std::ios::ate); if (!file) ORT_THROW("Error reading file."); buffer.resize(narrow(file.tellg())); file.seekg(0, std::ios::beg); if (!file.read(buffer.data(), buffer.size())) ORT_THROW("Error reading file"); } void TestLoadModelFromArrayWithExternalInitializerFromFileArray(const std::string& model_file_name, const std::string& external_data_file_name, const std::string& external_ini_min_size_bytes = "10", bool compare_external_bin_file = true) { std::string test_folder = "testdata/"; std::string model_path = test_folder + model_file_name; std::vector buffer; ReadFileToBuffer(model_path.c_str(), buffer); std::vector external_bin_buffer; std::string external_bin_path = test_folder + external_data_file_name; ReadFileToBuffer(external_bin_path.c_str(), external_bin_buffer); Ort::SessionOptions so; std::string optimized_model_file_name(model_file_name); auto length = optimized_model_file_name.length(); optimized_model_file_name.insert(length - 5, "_opt"); std::string optimized_file_path(test_folder + optimized_model_file_name); PathString optimized_file_path_t(optimized_file_path.begin(), optimized_file_path.end()); so.SetOptimizedModelFilePath(optimized_file_path_t.c_str()); // Dump the optimized model with external data so that it will unpack the external data from the loaded model std::string opt_bin_file_name(optimized_model_file_name); opt_bin_file_name.replace(optimized_model_file_name.length() - 4, 4, "bin"); so.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_DISABLE_ALL); so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersFileName, opt_bin_file_name.c_str()); so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersMinSizeInBytes, external_ini_min_size_bytes.c_str()); PathString external_file_name(external_data_file_name.begin(), external_data_file_name.end()); std::vector file_names{external_file_name}; std::vector file_buffers{external_bin_buffer.data()}; std::vector lengths{external_bin_buffer.size()}; so.AddExternalInitializersFromFilesInMemory(file_names, file_buffers, lengths); Ort::Session session(*ort_env.get(), buffer.data(), buffer.size(), so); std::string generated_bin_path = test_folder + opt_bin_file_name; // If there are multiple initializers in the external bin file // It's hard to guarantee the generated bin for optimized model is exactly same with original one for some cases if (compare_external_bin_file) { std::vector generated_bin_buffer; ReadFileToBuffer(generated_bin_path.c_str(), generated_bin_buffer); ASSERT_EQ(external_bin_buffer, generated_bin_buffer); } // Cleanup. ASSERT_EQ(std::remove(optimized_file_path.c_str()), 0); ASSERT_EQ(std::remove(generated_bin_path.c_str()), 0); } // Single initializer from single bin file TEST(CApiTest, TestLoadModelFromArrayWithExternalInitializerFromFileArray) { std::string model_file_name = "model_with_external_initializers.onnx"; std::string external_bin_name = "Pads.bin"; TestLoadModelFromArrayWithExternalInitializerFromFileArray(model_file_name, external_bin_name); } // Several external initializers from same file // Use offset from tensor proto to locate the buffer location TEST(CApiTest, TestLoadModelFromArrayWithExternalInitializersFromFileArray) { std::string model_file_name = "conv_qdq_external_ini.onnx"; std::string external_bin_name = "conv_qdq_external_ini.bin"; TestLoadModelFromArrayWithExternalInitializerFromFileArray(model_file_name, external_bin_name); } // Several external initializers from same file // Use offset from tensor proto to locate the buffer location TEST(CApiTest, TestLoadModelFromArrayWithExternalInitializersFromFileArrayPathRobust) { std::string model_file_name = "conv_qdq_external_ini.onnx"; std::string external_bin_name = "./conv_qdq_external_ini.bin"; TestLoadModelFromArrayWithExternalInitializerFromFileArray(model_file_name, external_bin_name); external_bin_name = ".//conv_qdq_external_ini.bin"; TestLoadModelFromArrayWithExternalInitializerFromFileArray(model_file_name, external_bin_name); #ifdef _WIN32 external_bin_name = ".\\\\conv_qdq_external_ini.bin"; TestLoadModelFromArrayWithExternalInitializerFromFileArray(model_file_name, external_bin_name); external_bin_name = ".\\conv_qdq_external_ini.bin"; TestLoadModelFromArrayWithExternalInitializerFromFileArray(model_file_name, external_bin_name); #endif } // The model has external data, Test loading model from array // Extra API required to set the external data path TEST(CApiTest, TestLoadModelFromArrayWithExternalInitializersViaSetExternalDataPath) { std::string model_file_name = "conv_qdq_external_ini.onnx"; std::string external_bin_name = "conv_qdq_external_ini.bin"; std::string test_folder = "testdata/"; std::string model_path = test_folder + model_file_name; std::vector buffer; ReadFileToBuffer(model_path.c_str(), buffer); std::vector external_bin_buffer; std::string external_bin_path = test_folder + external_bin_name; ReadFileToBuffer(external_bin_path.c_str(), external_bin_buffer); Ort::SessionOptions so; std::string optimized_model_file_name(model_file_name); auto length = optimized_model_file_name.length(); optimized_model_file_name.insert(length - 5, "_opt"); std::string optimized_file_path(test_folder + optimized_model_file_name); PathString optimized_file_path_t(optimized_file_path.begin(), optimized_file_path.end()); // Dump the optimized model with external data so that it will unpack the external data from the loaded model so.SetOptimizedModelFilePath(optimized_file_path_t.c_str()); // set the model external file folder path so.AddConfigEntry(kOrtSessionOptionsModelExternalInitializersFileFolderPath, test_folder.c_str()); std::string opt_bin_file_name(optimized_model_file_name); opt_bin_file_name.replace(optimized_model_file_name.length() - 4, 4, "bin"); so.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_DISABLE_ALL); so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersFileName, opt_bin_file_name.c_str()); so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersMinSizeInBytes, "10"); Ort::Session session(*ort_env.get(), buffer.data(), buffer.size(), so); std::string generated_bin_path = test_folder + opt_bin_file_name; std::vector generated_bin_buffer; ReadFileToBuffer(generated_bin_path.c_str(), generated_bin_buffer); ASSERT_EQ(external_bin_buffer, generated_bin_buffer); // Cleanup. ASSERT_EQ(std::remove(optimized_file_path.c_str()), 0); ASSERT_EQ(std::remove(generated_bin_path.c_str()), 0); } // The model has external data. Load the model from a file path whose directory does NOT contain // the external data file, and set model_external_initializers_file_folder_path to the folder that // does. This verifies the option is honored for file-path loads and overrides the model directory. TEST(CApiTest, TestLoadModelFromPathWithExternalInitializersViaSetExternalDataPath) { const std::string model_file_name = "conv_qdq_external_ini.onnx"; const std::string test_folder = "testdata/"; // Copy just the model file (not the external .bin) into a subfolder so the model's own directory // cannot resolve the external initializers. const std::string model_only_folder = test_folder + "ext_ini_path_override/"; std::error_code ec; std::filesystem::create_directories(model_only_folder, ec); ASSERT_FALSE(ec); const std::string copied_model_path = model_only_folder + model_file_name; std::filesystem::copy_file(test_folder + model_file_name, copied_model_path, std::filesystem::copy_options::overwrite_existing, ec); ASSERT_FALSE(ec) << "Failed to copy model file: " << ec.message(); Ort::SessionOptions so; so.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_DISABLE_ALL); // Point external initializers at the original folder, overriding the copied model's directory. so.AddConfigEntry(kOrtSessionOptionsModelExternalInitializersFileFolderPath, test_folder.c_str()); // Loading succeeds only if the external initializers are resolved from the override folder. const PathString copied_model_path_t(copied_model_path.begin(), copied_model_path.end()); Ort::Session session(*ort_env.get(), copied_model_path_t.c_str(), so); // Cleanup. std::filesystem::remove_all(model_only_folder, ec); ASSERT_FALSE(ec) << "Failed to remove temp folder: " << ec.message(); } #ifndef _WIN32 struct FileDescriptorTraits { using Handle = int; static Handle GetInvalidHandleValue() { return -1; } static void CleanUp(Handle h) { ASSERT_TRUE(close(h) != -1); } }; using ScopedFileDescriptor = ScopedResource; #endif void FileMmap(const ORTCHAR_T* file_path, void*& mapped_base) { #ifdef _WIN32 wil::unique_hfile file_handle{CreateFile2(file_path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL)}; ASSERT_TRUE(file_handle.get() != INVALID_HANDLE_VALUE); wil::unique_hfile file_mapping_handle{ CreateFileMappingW(file_handle.get(), nullptr, PAGE_READONLY, 0, 0, nullptr)}; ASSERT_TRUE(file_mapping_handle.get() != INVALID_HANDLE_VALUE); mapped_base = MapViewOfFile(file_mapping_handle.get(), FILE_MAP_READ, 0, 0, 0); #else ScopedFileDescriptor file_descriptor{open(file_path, O_RDONLY)}; ASSERT_TRUE(file_descriptor.IsValid()); struct stat sb; stat(file_path, &sb); mapped_base = mmap(nullptr, narrow(sb.st_size), PROT_READ | PROT_WRITE, MAP_PRIVATE, file_descriptor.Get(), 0); #endif return; } void TestLoadModelFromArrayWithExternalInitializerFromFileMmap(const std::string& model_file_name, const std::string& external_data_file_name, const std::string& external_ini_min_size_bytes = "10", bool compare_external_bin_file = true) { std::string test_folder = "testdata/"; std::string model_path = test_folder + model_file_name; std::vector buffer; ReadFileToBuffer(model_path.c_str(), buffer); std::string external_bin_path = test_folder + external_data_file_name; PathString external_bin_path_t(external_bin_path.begin(), external_bin_path.end()); void* mapped_base = nullptr; FileMmap(external_bin_path_t.c_str(), mapped_base); ASSERT_TRUE(mapped_base); std::ifstream bin_file(external_bin_path, std::ios::binary | std::ios::ate); ASSERT_TRUE(bin_file); size_t bin_file_length = narrow(bin_file.tellg()); Ort::SessionOptions so; std::string optimized_model_file_name(model_file_name); auto length = optimized_model_file_name.length(); optimized_model_file_name.insert(length - 5, "_opt"); std::string optimized_file_path(test_folder + optimized_model_file_name); PathString optimized_file_path_t(optimized_file_path.begin(), optimized_file_path.end()); so.SetOptimizedModelFilePath(optimized_file_path_t.c_str()); // Dump the optimized model with external data so that it will unpack the external data from the loaded model std::string opt_bin_file_name(optimized_model_file_name); opt_bin_file_name.replace(optimized_model_file_name.length() - 4, 4, "bin"); so.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_DISABLE_ALL); so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersFileName, opt_bin_file_name.c_str()); so.AddConfigEntry(kOrtSessionOptionsOptimizedModelExternalInitializersMinSizeInBytes, external_ini_min_size_bytes.c_str()); PathString external_file_name(external_data_file_name.begin(), external_data_file_name.end()); std::vector file_names{external_file_name}; std::vector file_buffers{static_cast(mapped_base)}; std::vector lengths{bin_file_length}; so.AddExternalInitializersFromFilesInMemory(file_names, file_buffers, lengths); Ort::Session session(*ort_env.get(), buffer.data(), buffer.size(), so); #ifdef _WIN32 bool ret = UnmapViewOfFile(mapped_base); ASSERT_TRUE(ret); #else struct stat sb; stat(external_bin_path.c_str(), &sb); int ret = munmap(mapped_base, narrow(sb.st_size)); ASSERT_TRUE(ret == 0); #endif std::string generated_bin_path = test_folder + opt_bin_file_name; // If there are multiple initializers in the external bin file // It's hard to guarantee the generated bin for optimized model is exactly same with original one for some cases if (compare_external_bin_file) { std::vector external_bin_buffer; ReadFileToBuffer(external_bin_path.c_str(), external_bin_buffer); std::vector generated_bin_buffer; ReadFileToBuffer(generated_bin_path.c_str(), generated_bin_buffer); ASSERT_EQ(external_bin_buffer, generated_bin_buffer); } // Cleanup. ASSERT_EQ(std::remove(optimized_file_path.c_str()), 0); ASSERT_EQ(std::remove(generated_bin_path.c_str()), 0); } // Load external bin file using mmap // Several external initializers from same file // Use offset from tensor proto to locate the buffer location TEST(CApiTest, TestLoadModelFromArrayWithExternalInitializersFromFileMmap) { std::string model_file_name = "conv_qdq_external_ini.onnx"; std::string external_bin_name = "conv_qdq_external_ini.bin"; TestLoadModelFromArrayWithExternalInitializerFromFileMmap(model_file_name, external_bin_name); } #endif } // namespace test } // namespace onnxruntime