// Copyright (c) Microsoft Corporation. All rights reserved.
|
// Licensed under the MIT License.
|
|
#include <algorithm>
|
#include <fstream>
|
#include <iostream>
|
#include <iterator>
|
#include <memory>
|
#include <optional>
|
#include <string>
|
#include <vector>
|
#ifdef _WIN32
|
#include <locale>
|
#endif
|
|
#ifndef USE_ONNXRUNTIME_DLL
|
#ifdef __GNUC__
|
#pragma GCC diagnostic push
|
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
#endif
|
#include <google/protobuf/message_lite.h>
|
#ifdef __GNUC__
|
#pragma GCC diagnostic pop
|
#endif
|
#endif
|
|
#include "gtest/gtest.h"
|
|
#include "core/common/common.h"
|
#include "core/platform/env_var_utils.h"
|
#include "core/session/onnxruntime_cxx_api.h"
|
#include "core/util/thread_utils.h"
|
#include "test/util/include/telemetry_test_environment.h"
|
|
#if !defined(ORT_MINIMAL_BUILD) && defined(ORT_UNIT_TEST_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
#define TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE
|
#endif // !defined(ORT_MINIMAL_BUILD) && defined(ORT_UNIT_TEST_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
|
#if defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
#include "test/unittest_util/test_dynamic_plugin_ep.h"
|
#include "test/util/include/skipping_test_listener.h"
|
#endif // defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
|
#if defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE) && defined(ORT_UNIT_TEST_HAS_WEBGPU_STATIC_PLUGIN_EP)
|
#define TEST_MAIN_ENABLE_WEBGPU_STATIC_PLUGIN_EP_USAGE
|
#include "core/session/onnxruntime_env_config_keys.h"
|
#endif
|
|
std::unique_ptr<Ort::Env> ort_env;
|
|
// define environment variable name constants here
|
namespace env_var_names {
|
// Set ORT log level to the specified numeric log level.
|
constexpr const char* kLogLevel = "ORT_UNIT_TEST_MAIN_LOG_LEVEL";
|
|
#if defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
// Specify dynamic plugin EP configuration JSON.
|
// Refer to `onnxruntime::test::dynamic_plugin_ep_infra::ParseInitializationConfig()` for more information.
|
constexpr const char* kDynamicPluginEpConfigJson = "ORT_UNIT_TEST_MAIN_DYNAMIC_PLUGIN_EP_CONFIG_JSON";
|
// Specify a file path from which to read dynamic plugin EP configuration JSON.
|
// Mutually exclusive with kDynamicPluginEpConfigJson.
|
constexpr const char* kDynamicPluginEpConfigJsonFile = "ORT_UNIT_TEST_MAIN_DYNAMIC_PLUGIN_EP_CONFIG_JSON_FILE";
|
#endif // defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
} // namespace env_var_names
|
|
// ortenv_setup() and ortenv_teardown() are used by onnxruntime/test/xctest/xcgtest.mm so can't be file local
|
extern "C" void ortenv_setup() {
|
// Fully suppress telemetry for the unit-test process before any Ort::Env (and therefore the platform
|
// telemetry provider) is created, so local non-CI runs never spin up the uploader or emit events.
|
onnxruntime::test::SuppressTelemetryForTests();
|
ORT_TRY {
|
#ifdef _WIN32
|
// Set the locale to UTF-8 to ensure proper handling of wide characters on Windows
|
std::wclog.imbue(std::locale(".UTF-8", std::locale::ctype));
|
#endif
|
|
OrtThreadingOptions tpo;
|
|
OrtLoggingLevel log_level = ORT_LOGGING_LEVEL_WARNING;
|
if (auto log_level_override = onnxruntime::ParseEnvironmentVariable<int>(env_var_names::kLogLevel);
|
log_level_override.has_value()) {
|
*log_level_override = std::clamp(*log_level_override,
|
static_cast<int>(ORT_LOGGING_LEVEL_VERBOSE),
|
static_cast<int>(ORT_LOGGING_LEVEL_FATAL));
|
std::cout << "Setting log level to " << *log_level_override << "\n";
|
log_level = static_cast<OrtLoggingLevel>(*log_level_override);
|
}
|
|
#if defined(TEST_MAIN_ENABLE_WEBGPU_STATIC_PLUGIN_EP_USAGE)
|
// The WebGPU plugin EP is statically linked into the ORT binary and is registered by ORT core while the
|
// environment is created, so "allow_virtual_devices" must be set here rather than being derived from a
|
// ".virtual" EP library registration name (there is no library registration call to name). This lets the
|
// WebGPU factory surface a virtual GPU device, which gives dynamic_plugin_ep_infra::Initialize() below a
|
// selectable WebGPU device so the binary doesn't exit at startup when GetEpDevices() would otherwise be
|
// empty (no real GPU). It does not make non-compile-only WebGPU tests runnable without a device.
|
Ort::KeyValuePairs env_config_entries{};
|
env_config_entries.Add(kOrtEnvAllowVirtualDevices, "1");
|
|
OrtEnvCreationOptions env_creation_options{};
|
env_creation_options.version = ORT_API_VERSION;
|
env_creation_options.logging_severity_level = static_cast<int32_t>(log_level);
|
env_creation_options.log_id = "Default";
|
env_creation_options.threading_options = &tpo;
|
env_creation_options.config_entries = env_config_entries.GetConst();
|
|
ort_env.reset(new Ort::Env(&env_creation_options));
|
#else
|
ort_env.reset(new Ort::Env(&tpo, log_level, "Default"));
|
#endif
|
|
#if defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
{
|
namespace dynamic_plugin_ep_infra = onnxruntime::test::dynamic_plugin_ep_infra;
|
|
auto dynamic_plugin_ep_config_json = onnxruntime::ParseEnvironmentVariable<std::string>(
|
env_var_names::kDynamicPluginEpConfigJson);
|
auto dynamic_plugin_ep_config_json_file = onnxruntime::ParseEnvironmentVariable<std::string>(
|
env_var_names::kDynamicPluginEpConfigJsonFile);
|
|
ORT_ENFORCE(!dynamic_plugin_ep_config_json.has_value() || !dynamic_plugin_ep_config_json_file.has_value(),
|
"Only one of ", env_var_names::kDynamicPluginEpConfigJson,
|
" and ", env_var_names::kDynamicPluginEpConfigJsonFile,
|
" should be set, not both.");
|
|
if (dynamic_plugin_ep_config_json_file.has_value()) {
|
const auto& config_file_path = *dynamic_plugin_ep_config_json_file;
|
std::cout << "Reading dynamic plugin EP configuration from file: " << config_file_path << "\n";
|
std::ifstream config_file{config_file_path};
|
ORT_ENFORCE(config_file, "Failed to open dynamic plugin EP configuration file: ", config_file_path);
|
dynamic_plugin_ep_config_json.emplace(
|
std::istreambuf_iterator<char>{config_file}, std::istreambuf_iterator<char>{});
|
}
|
|
#if defined(ORT_UNIT_TEST_HAS_CUDA_PLUGIN_EP) && defined(ORT_UNIT_TEST_CUDA_PLUGIN_EP_LIBRARY_PATH)
|
if (!dynamic_plugin_ep_config_json.has_value()) {
|
dynamic_plugin_ep_config_json.emplace(
|
"{\n"
|
" \"ep_library_registration_name\": \"CUDAExecutionProvider\",\n"
|
" \"ep_library_path\": \"" ORT_UNIT_TEST_CUDA_PLUGIN_EP_LIBRARY_PATH
|
"\",\n"
|
" \"selected_ep_name\": \"CUDAExecutionProvider\"\n"
|
"}");
|
}
|
#endif
|
|
#if defined(ORT_UNIT_TEST_HAS_WEBGPU_PLUGIN_EP) && defined(ORT_UNIT_TEST_WEBGPU_PLUGIN_EP_LIBRARY_PATH)
|
if (!dynamic_plugin_ep_config_json.has_value()) {
|
// Register with a ".virtual" suffix so ORT auto-sets env config "allow_virtual_devices"=1, letting
|
// the WebGPU factory surface a virtual GPU device. Its only purpose here is to give
|
// dynamic_plugin_ep_infra::Initialize() (below) a selectable WebGPU device so the binary doesn't
|
// exit at startup when GetEpDevices() would otherwise be empty (no real GPU). It does not make
|
// non-compile-only WebGPU tests runnable without a device: the virtual device only backs device-free
|
// compile-only sessions, and a real GPU, when present, is enumerated first and preferred (see the
|
// resize(1) de-dup in test_dynamic_plugin_ep.cc).
|
dynamic_plugin_ep_config_json.emplace(
|
"{\n"
|
" \"ep_library_registration_name\": \"WebGpuExecutionProvider.virtual\",\n"
|
" \"ep_library_path\": \"" ORT_UNIT_TEST_WEBGPU_PLUGIN_EP_LIBRARY_PATH
|
"\",\n"
|
// Enable int64 so the generic (EP-agnostic) int64 operator tests exercise the WebGPU
|
// int64 kernels on the plugin path too (mirrors DefaultWebGpuExecutionProvider on the
|
// static path). The key is the un-prefixed provider option name for plugin EPs.
|
" \"default_ep_options\": { \"enableInt64\": \"1\" },\n"
|
" \"selected_ep_name\": \"WebGpuExecutionProvider\"\n"
|
"}");
|
}
|
#endif
|
|
#if defined(TEST_MAIN_ENABLE_WEBGPU_STATIC_PLUGIN_EP_USAGE)
|
if (!dynamic_plugin_ep_config_json.has_value()) {
|
// No "ep_library_path": ORT core already registered the statically linked WebGPU plugin EP when the
|
// environment above was created.
|
dynamic_plugin_ep_config_json.emplace(
|
"{\n"
|
" \"ep_library_registration_name\": \"WebGpuExecutionProvider\",\n"
|
// Enable int64 so the generic (EP-agnostic) int64 operator tests exercise the WebGPU
|
// int64 kernels on the plugin path too (mirrors DefaultWebGpuExecutionProvider on the
|
// internal EP path). The key is the un-prefixed provider option name for plugin EPs.
|
" \"default_ep_options\": { \"enableInt64\": \"1\" },\n"
|
" \"selected_ep_name\": \"WebGpuExecutionProvider\"\n"
|
"}");
|
}
|
#endif
|
|
if (dynamic_plugin_ep_config_json.has_value()) {
|
std::cout << "Initializing dynamic plugin EP infrastructure with configuration:\n"
|
<< *dynamic_plugin_ep_config_json << "\n";
|
dynamic_plugin_ep_infra::InitializationConfig config{};
|
ORT_THROW_IF_ERROR(dynamic_plugin_ep_infra::ParseInitializationConfig(*dynamic_plugin_ep_config_json, config));
|
ORT_THROW_IF_ERROR(dynamic_plugin_ep_infra::Initialize(*ort_env, config));
|
}
|
}
|
#endif // defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
}
|
ORT_CATCH(const std::exception& ex) {
|
ORT_HANDLE_EXCEPTION([&]() {
|
std::cerr << ex.what();
|
std::exit(1);
|
});
|
}
|
}
|
|
extern "C" void ortenv_teardown() {
|
#if defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
{
|
namespace dynamic_plugin_ep_infra = onnxruntime::test::dynamic_plugin_ep_infra;
|
dynamic_plugin_ep_infra::Shutdown();
|
}
|
#endif // defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
|
ort_env.reset();
|
}
|
|
static std::vector<std::unique_ptr<::testing::TestEventListener>> MakeTestEventListeners() {
|
std::vector<std::unique_ptr<::testing::TestEventListener>> result{};
|
#if defined(TEST_MAIN_ENABLE_DYNAMIC_PLUGIN_EP_USAGE)
|
{
|
namespace dynamic_plugin_ep_infra = onnxruntime::test::dynamic_plugin_ep_infra;
|
const auto tests_to_skip = dynamic_plugin_ep_infra::GetTestsToSkip();
|
auto skipping_test_listener = std::make_unique<onnxruntime::test::SkippingTestListener>(tests_to_skip);
|
result.emplace_back(std::move(skipping_test_listener));
|
}
|
#endif
|
return result;
|
}
|
|
#ifdef USE_TENSORRT
|
|
#if defined(_MSC_VER)
|
#pragma warning(push)
|
#pragma warning(disable : 4100) // Ignore warning C4100: unreferenced format parameter.
|
#pragma warning(disable : 4996) // Ignore warning C4996: 'nvinfer1::IPluginV2' was declared deprecated
|
#elif defined(__GNUC__)
|
#pragma GCC diagnostic push
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
#endif
|
|
// TensorRT will load/unload libraries as builder objects are created and torn down. This will happen for
|
// every single unit test, which leads to excessive test execution time due to that overhead.
|
// Nvidia suggests to keep a placeholder builder object around to avoid this.
|
#include "NvInfer.h"
|
|
#if defined(_MSC_VER)
|
#pragma warning(pop)
|
#elif defined(__GNUC__)
|
#pragma GCC diagnostic pop
|
#endif
|
|
class DummyLogger : public nvinfer1::ILogger {
|
public:
|
DummyLogger(Severity /*verbosity*/) {}
|
void log(Severity /*severity*/, const char* /*msg*/) noexcept override {}
|
};
|
DummyLogger trt_logger(nvinfer1::ILogger::Severity::kWARNING);
|
|
auto const placeholder = std::unique_ptr<nvinfer1::IBuilder>(nvinfer1::createInferBuilder(trt_logger));
|
|
#endif
|
|
#define TEST_MAIN main
|
|
#if defined(__APPLE__)
|
#include <TargetConditionals.h>
|
#if TARGET_OS_SIMULATOR || TARGET_OS_IOS
|
#undef TEST_MAIN
|
#define TEST_MAIN main_no_link_ // there is a UI test app for iOS.
|
#endif
|
#endif
|
|
int TEST_MAIN(int argc, char** argv) {
|
int status = 0;
|
|
ORT_TRY {
|
ortenv_setup();
|
::testing::InitGoogleTest(&argc, argv);
|
|
{
|
auto& test_listeners = ::testing::UnitTest::GetInstance()->listeners();
|
auto test_listeners_to_add = MakeTestEventListeners();
|
for (auto& test_listener_to_add : test_listeners_to_add) {
|
test_listeners.Append(test_listener_to_add.release());
|
}
|
}
|
|
status = RUN_ALL_TESTS();
|
}
|
ORT_CATCH(const std::exception& ex) {
|
ORT_HANDLE_EXCEPTION([&]() {
|
std::cerr << ex.what();
|
status = -1;
|
});
|
}
|
|
// TODO: Fix the C API issue
|
ortenv_teardown(); // If we don't do this, it will crash
|
|
#ifndef USE_ONNXRUNTIME_DLL
|
// make memory leak checker happy
|
::google::protobuf::ShutdownProtobufLibrary();
|
#endif
|
return status;
|
}
|