Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 68 additions & 54 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ function(AddTest)
endif()
endfunction(AddTest)

# general program entrypoint for C++ unit tests
set(onnxruntime_unittest_main_src "${TEST_SRC_DIR}/unittest_main/test_main.cc")

#Do not add '${TEST_SRC_DIR}/util/include' to your include directories directly
#Use onnxruntime_add_include_to_target or target_link_libraries, so that compile definitions
#can propagate correctly.
Expand Down Expand Up @@ -570,65 +573,58 @@ endif()

set(all_dependencies ${onnxruntime_test_providers_dependencies} )

if (onnxruntime_ENABLE_TRAINING)
list(APPEND all_tests ${onnxruntime_test_training_src})
endif()
if (onnxruntime_ENABLE_TRAINING)
list(APPEND all_tests ${onnxruntime_test_training_src})
endif()

if (onnxruntime_USE_TVM)
list(APPEND all_tests ${onnxruntime_test_tvm_src})
endif()
if (onnxruntime_USE_OPENVINO)
list(APPEND all_tests ${onnxruntime_test_openvino_src})
endif()
# we can only have one 'main', so remove them all and add back the providers test_main as it sets
# up everything we need for all tests
file(GLOB_RECURSE test_mains CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/*/test_main.cc"
)
list(REMOVE_ITEM all_tests ${test_mains})
list(APPEND all_tests "${TEST_SRC_DIR}/providers/test_main.cc")
if (onnxruntime_USE_TVM)
list(APPEND all_tests ${onnxruntime_test_tvm_src})
endif()
if (onnxruntime_USE_OPENVINO)
list(APPEND all_tests ${onnxruntime_test_openvino_src})
endif()

# this is only added to onnxruntime_test_framework_libs above, but we use onnxruntime_test_providers_libs for the onnxruntime_test_all target.
# for now, add it here. better is probably to have onnxruntime_test_providers_libs use the full onnxruntime_test_framework_libs
# list given it's built on top of that library and needs all the same dependencies.
if(WIN32)
list(APPEND onnxruntime_test_providers_libs Advapi32)
endif()
# this is only added to onnxruntime_test_framework_libs above, but we use onnxruntime_test_providers_libs for the onnxruntime_test_all target.
# for now, add it here. better is probably to have onnxruntime_test_providers_libs use the full onnxruntime_test_framework_libs
# list given it's built on top of that library and needs all the same dependencies.
if(WIN32)
list(APPEND onnxruntime_test_providers_libs Advapi32)
endif()

AddTest(
TARGET onnxruntime_test_all
SOURCES ${all_tests}
LIBS onnx_test_runner_common ${onnxruntime_test_providers_libs} ${onnxruntime_test_common_libs} re2::re2 onnx_test_data_proto
DEPENDS ${all_dependencies}
)
AddTest(
TARGET onnxruntime_test_all
SOURCES ${all_tests} ${onnxruntime_unittest_main_src}
LIBS onnx_test_runner_common ${onnxruntime_test_providers_libs} ${onnxruntime_test_common_libs} re2::re2 onnx_test_data_proto
DEPENDS ${all_dependencies}
)

# the default logger tests conflict with the need to have an overall default logger
# so skip in this type of
target_compile_definitions(onnxruntime_test_all PUBLIC -DSKIP_DEFAULT_LOGGER_TESTS)
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
target_compile_definitions(onnxruntime_test_all_xc PUBLIC -DSKIP_DEFAULT_LOGGER_TESTS)
endif()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
target_compile_options(onnxruntime_test_all PUBLIC "-Wno-unused-const-variable")
endif()
if(onnxruntime_RUN_MODELTEST_IN_DEBUG_MODE)
target_compile_definitions(onnxruntime_test_all PUBLIC -DRUN_MODELTEST_IN_DEBUG_MODE)
endif()
if (onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS)
target_compile_definitions(onnxruntime_test_all PRIVATE DEBUG_NODE_INPUTS_OUTPUTS)
endif()
if (onnxruntime_USE_FEATURIZERS)
target_include_directories(onnxruntime_test_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external/FeaturizersLibrary/src)
endif()
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS)
target_link_libraries(onnxruntime_test_all PRIVATE onnxruntime_language_interop onnxruntime_pyop)
endif()
# the default logger tests conflict with the need to have an overall default logger
# so skip in this type of
target_compile_definitions(onnxruntime_test_all PUBLIC -DSKIP_DEFAULT_LOGGER_TESTS)
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
target_compile_definitions(onnxruntime_test_all_xc PUBLIC -DSKIP_DEFAULT_LOGGER_TESTS)
endif()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
target_compile_options(onnxruntime_test_all PUBLIC "-Wno-unused-const-variable")
endif()
if(onnxruntime_RUN_MODELTEST_IN_DEBUG_MODE)
target_compile_definitions(onnxruntime_test_all PUBLIC -DRUN_MODELTEST_IN_DEBUG_MODE)
endif()
if (onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS)
target_compile_definitions(onnxruntime_test_all PRIVATE DEBUG_NODE_INPUTS_OUTPUTS)
endif()
if (onnxruntime_USE_FEATURIZERS)
target_include_directories(onnxruntime_test_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external/FeaturizersLibrary/src)
endif()
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS)
target_link_libraries(onnxruntime_test_all PRIVATE onnxruntime_language_interop onnxruntime_pyop)
endif()

if (onnxruntime_USE_ROCM)
target_include_directories(onnxruntime_test_all PRIVATE ${onnxruntime_ROCM_HOME}/include/hiprand ${onnxruntime_ROCM_HOME}/include/rocrand)
endif()
if (onnxruntime_USE_ROCM)
target_include_directories(onnxruntime_test_all PRIVATE ${onnxruntime_ROCM_HOME}/include/hiprand ${onnxruntime_ROCM_HOME}/include/rocrand)
endif()

set(test_data_target onnxruntime_test_all)
set(test_data_target onnxruntime_test_all)


#
Expand Down Expand Up @@ -871,7 +867,7 @@ if (onnxruntime_BUILD_SHARED_LIB)
endif()
AddTest(DYN
TARGET onnxruntime_shared_lib_test
SOURCES ${onnxruntime_shared_lib_test_SRC} ${TEST_SRC_DIR}/providers/test_main.cc
SOURCES ${onnxruntime_shared_lib_test_SRC} ${onnxruntime_unittest_main_src}
LIBS ${onnxruntime_shared_lib_test_LIBS}
DEPENDS ${all_dependencies}
)
Expand Down Expand Up @@ -904,6 +900,24 @@ if (onnxruntime_BUILD_SHARED_LIB)
endif()
endif()

# the debug node IO functionality uses static variables, so it is best tested
# in its own process
if(onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS)
AddTest(
TARGET onnxruntime_test_debug_node_inputs_outputs
SOURCES
"${TEST_SRC_DIR}/debug_node_inputs_outputs/debug_node_inputs_outputs_utils_test.cc"
"${TEST_SRC_DIR}/framework/TestAllocatorManager.cc"
"${TEST_SRC_DIR}/providers/provider_test_utils.cc"
${onnxruntime_unittest_main_src}
LIBS ${onnxruntime_test_providers_libs} ${onnxruntime_test_common_libs}
DEPENDS ${all_dependencies}
)

target_compile_definitions(onnxruntime_test_debug_node_inputs_outputs
PRIVATE DEBUG_NODE_INPUTS_OUTPUTS)
endif(onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS)

#some ETW tools
if(WIN32 AND onnxruntime_ENABLE_INSTRUMENT)
add_executable(generate_perf_report_from_etl ${ONNXRUNTIME_ROOT}/tool/etw/main.cc
Expand Down
23 changes: 6 additions & 17 deletions onnxruntime/core/framework/debug_node_inputs_outputs_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,41 +176,30 @@ const NodeDumpOptions& NodeDumpOptionsFromEnvironmentVariables() {
static const NodeDumpOptions node_dump_options = []() {
namespace env_vars = debug_node_inputs_outputs_env_vars;

auto get_bool_env_var = [](const char* env_var) {
const auto val = Env::Default().GetEnvironmentVar(env_var);
if (val.empty()) return false;
std::istringstream s{val};
int i;
ORT_ENFORCE(
s >> i && s.eof(),
"Failed to parse environment variable ", env_var, ": ", val);
return i != 0;
};

NodeDumpOptions opts{};

// Preserve existing behavior of printing the shapes by default. Turn it off only if the user has requested so
// explicitly by setting the value of the env variable to 0.
opts.dump_flags = NodeDumpOptions::DumpFlags::None;
if (ParseEnvironmentVariable<bool>(env_vars::kDumpShapeData, true)) {
if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpShapeData, true)) {
opts.dump_flags |= NodeDumpOptions::DumpFlags::Shape;
}

if (get_bool_env_var(env_vars::kDumpInputData)) {
if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpInputData, false)) {
opts.dump_flags |= NodeDumpOptions::DumpFlags::InputData;
}
if (get_bool_env_var(env_vars::kDumpOutputData)) {
if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpOutputData, false)) {
opts.dump_flags |= NodeDumpOptions::DumpFlags::OutputData;
}

opts.filter.name_pattern = Env::Default().GetEnvironmentVar(env_vars::kNameFilter);
opts.filter.op_type_pattern = Env::Default().GetEnvironmentVar(env_vars::kOpTypeFilter);

if (get_bool_env_var(env_vars::kDumpDataToFiles)) {
if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpDataToFiles, false)) {
opts.data_destination = NodeDumpOptions::DataDestination::TensorProtoFiles;
}

if (get_bool_env_var(env_vars::kAppendRankToFileName)) {
if (ParseEnvironmentVariableWithDefault<bool>(env_vars::kAppendRankToFileName, false)) {
std::string rank = Env::Default().GetEnvironmentVar("OMPI_COMM_WORLD_RANK");
if (rank.empty()) {
opts.file_suffix = "_default_rank_0";
Expand All @@ -229,7 +218,7 @@ const NodeDumpOptions& NodeDumpOptionsFromEnvironmentVariables() {
opts.data_destination == NodeDumpOptions::DataDestination::TensorProtoFiles &&
opts.filter.name_pattern.empty() && opts.filter.op_type_pattern.empty()) {
ORT_ENFORCE(
get_bool_env_var(env_vars::kDumpingDataToFilesForAllNodesIsOk),
ParseEnvironmentVariableWithDefault<bool>(env_vars::kDumpingDataToFilesForAllNodesIsOk, false),
"The current environment variable configuration will dump node input or output data to files for every node. "
"This may cause a lot of files to be generated. Set the environment variable ",
env_vars::kDumpingDataToFilesForAllNodesIsOk, " to confirm this is what you want.");
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/platform/env_var_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ optional<T> ParseEnvironmentVariable(const std::string& name) {
* Parses an environment variable value or returns the given default if unavailable.
*/
template <typename T>
T ParseEnvironmentVariable(const std::string& name, const T& default_value) {
T ParseEnvironmentVariableWithDefault(const std::string& name, const T& default_value) {
const auto parsed = ParseEnvironmentVariable<T>(name);
if (parsed.has_value()) {
return parsed.value();
Expand Down
24 changes: 0 additions & 24 deletions onnxruntime/test/common/test_main.cc

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#ifdef DEBUG_NODE_INPUTS_OUTPUTS

#include "core/framework/debug_node_inputs_outputs_utils.h"

#include <fstream>
Expand Down Expand Up @@ -41,11 +39,14 @@ TEST(DebugNodeInputsOutputs, BasicFileOutput) {
TemporaryDirectory temp_dir{ORT_TSTR("debug_node_inputs_outputs_utils_test")};
ScopedEnvironmentVariables scoped_env_vars{
EnvVarMap{
{env_vars::kDumpInputData, {"1"}},
{env_vars::kDumpOutputData, {"1"}},
{env_vars::kDumpDataToFiles, {"1"}},
{env_vars::kOutputDir, {ToMBString(temp_dir.Path())}},
{env_vars::kDumpingDataToFilesForAllNodesIsOk, {"1"}},
{env_vars::kDumpInputData, "1"},
{env_vars::kDumpOutputData, "1"},
{env_vars::kNameFilter, nullopt},
{env_vars::kOpTypeFilter, nullopt},
{env_vars::kDumpDataToFiles, "1"},
{env_vars::kAppendRankToFileName, nullopt},
{env_vars::kOutputDir, ToMBString(temp_dir.Path())},
{env_vars::kDumpingDataToFilesForAllNodesIsOk, "1"},
}};

OpTester tester{"Round", 11, kOnnxDomain};
Expand All @@ -56,8 +57,10 @@ TEST(DebugNodeInputsOutputs, BasicFileOutput) {

auto verify_file_data =
[&temp_dir, &input, &output](
const std::vector<OrtValue>& /*fetches*/,
const std::vector<OrtValue>& fetches,
const std::string& /*provider_type*/) {
ASSERT_EQ(fetches.size(), 1u);
FetchTensor(fetches[0]);
VerifyTensorProtoFileData(
temp_dir.Path() + ORT_TSTR("/x.tensorproto"),
gsl::make_span(input));
Expand All @@ -73,5 +76,3 @@ TEST(DebugNodeInputsOutputs, BasicFileOutput) {

} // namespace test
} // namespace onnxruntime

#endif
25 changes: 0 additions & 25 deletions onnxruntime/test/framework/test_main.cc

This file was deleted.

31 changes: 0 additions & 31 deletions onnxruntime/test/ir/test_main.cc

This file was deleted.

13 changes: 0 additions & 13 deletions onnxruntime/test/shared_lib/test_main.cc

This file was deleted.

2 changes: 1 addition & 1 deletion onnxruntime/test/util/test_random_seed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RandomSeedType GetTestRandomSeed() {
};

static const auto use_cached =
!ParseEnvironmentVariable<bool>(test_random_seed_env_vars::kDoNotCache, false);
!ParseEnvironmentVariableWithDefault<bool>(test_random_seed_env_vars::kDoNotCache, false);
if (use_cached) {
// initially generate from current time
static const auto static_random_seed = generate_from_time();
Expand Down