diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index 17a40bdd9d91d..05d10b110578c 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -220,6 +220,7 @@ endif() set (ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR "${ONNXRUNTIME_ROOT}/test/shared_lib") set (ONNXRUNTIME_GLOBAL_THREAD_POOLS_TEST_SRC_DIR "${ONNXRUNTIME_ROOT}/test/global_thread_pools") +set (ONNXRUNTIME_API_TESTS_WITHOUT_ENV_SRC_DIR "${ONNXRUNTIME_ROOT}/test/api_tests_without_env") set (onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_fixture.h @@ -238,6 +239,9 @@ set (onnxruntime_global_thread_pools_test_SRC ${ONNXRUNTIME_GLOBAL_THREAD_POOLS_TEST_SRC_DIR}/test_main.cc ${ONNXRUNTIME_GLOBAL_THREAD_POOLS_TEST_SRC_DIR}/test_inference.cc) +set (onnxruntime_api_tests_without_env_SRC + ${ONNXRUNTIME_API_TESTS_WITHOUT_ENV_SRC_DIR}/test_apis_without_env.cc) + # tests from lowest level library up. # the order of libraries should be maintained, with higher libraries being added first in the list @@ -763,6 +767,16 @@ if (onnxruntime_BUILD_SHARED_LIB) DEPENDS ${all_dependencies} ) endif() + + # A separate test is needed to test the APIs that don't rely on the env being created first. + if (NOT CMAKE_SYSTEM_NAME STREQUAL "Android") + AddTest(DYN + TARGET onnxruntime_api_tests_without_env + SOURCES ${onnxruntime_api_tests_without_env_SRC} + LIBS ${onnxruntime_shared_lib_test_LIBS} + DEPENDS ${all_dependencies} + ) + endif() endif() #some ETW tools diff --git a/onnxruntime/core/framework/provider_bridge_ort.cc b/onnxruntime/core/framework/provider_bridge_ort.cc index 9c5c26dea0634..a4444f828f00b 100644 --- a/onnxruntime/core/framework/provider_bridge_ort.cc +++ b/onnxruntime/core/framework/provider_bridge_ort.cc @@ -7,6 +7,7 @@ #include "core/framework/data_types.h" #include "core/framework/allocatormgr.h" #include "core/providers/dnnl/dnnl_provider_factory.h" +#include "core/session/inference_session.h" #include "core/session/abi_session_options_impl.h" #include "core/session/ort_apis.h" #include "core/platform/env.h" diff --git a/onnxruntime/core/framework/sequential_executor.cc b/onnxruntime/core/framework/sequential_executor.cc index 1bd20e7b4a9e4..b0ed0120fc4a6 100644 --- a/onnxruntime/core/framework/sequential_executor.cc +++ b/onnxruntime/core/framework/sequential_executor.cc @@ -55,7 +55,7 @@ static void CalculateTotalOutputSizes(OpKernelContextInternal* op_kernel_context ORT_UNUSED_PARAMETER(node_name); for (auto i = 0; i < op_kernel_context->OutputCount(); i++) { const OrtValue* p_output = op_kernel_context->GetOutputMLValue(i); - if (p_output->IsTensor()) { + if (p_output != nullptr && p_output->IsTensor()) { const auto& tensor = p_output->Get(); size_t tensor_size = tensor.SizeInBytes(); #if defined(TRACE_EXECUTION) @@ -83,7 +83,7 @@ static void CalculateTotalInputSizes(const OpKernelContextInternal* op_kernel_co const int input_count = op_kernel_context->InputCount(); for (auto i = 0; i < input_count; i++) { const OrtValue* p_input = op_kernel_context->GetInputMLValue(i); - if (p_input->IsTensor()) { + if (p_input != nullptr && p_input->IsTensor()) { const OpKernelInfo& op_kernel_info = p_op_kernel->Info(); const Tensor* p_tensor = nullptr; bool is_param = op_kernel_info.TryGetConstantInput(i, &p_tensor); @@ -241,7 +241,7 @@ Status SequentialExecutor::Execute(const SessionState& session_state, const std: const std::string node_name_for_profiling = [&]() -> std::string { if (!is_profiler_enabled) return {}; - // Derive something meaningful for profile traces and logs if node name field is blank in execution graph + // Derive something meaningful for profile traces and logs if node name field is blank in execution graph return node.Name().empty() ? MakeString(node.OpType(), "_", node_index) : node.Name(); }(); diff --git a/onnxruntime/core/session/abi_session_options.cc b/onnxruntime/core/session/abi_session_options.cc index 0f66c520e0429..2dce6c93452ff 100644 --- a/onnxruntime/core/session/abi_session_options.cc +++ b/onnxruntime/core/session/abi_session_options.cc @@ -144,9 +144,12 @@ ORT_API_STATUS_IMPL(OrtApis::SetIntraOpNumThreads, _Inout_ OrtSessionOptions* op #ifdef _OPENMP ORT_UNUSED_PARAMETER(options); ORT_UNUSED_PARAMETER(intra_op_num_threads); - LOGS_DEFAULT(WARNING) << "Since openmp is enabled in this build, this API cannot be used to configure" - " intra op num threads. Please use the openmp environment variables to control" - " the number of threads."; + // Can't use the default logger here since it's possible that the default logger has not been created + // at this point. The default logger gets created when the env is created and these APIs don't require + // the env to be created first. + std::cout << "WARNING: Since openmp is enabled in this build, this API cannot be used to configure" + " intra op num threads. Please use the openmp environment variables to control" + " the number of threads.\n"; #else options->value.intra_op_param.thread_pool_size = intra_op_num_threads; #endif @@ -161,16 +164,14 @@ ORT_API_STATUS_IMPL(OrtApis::SetInterOpNumThreads, _Inout_ OrtSessionOptions* op ORT_API_STATUS_IMPL(OrtApis::AddFreeDimensionOverride, _Inout_ OrtSessionOptions* options, _In_ const char* dim_denotation, _In_ int64_t dim_value) { options->value.free_dimension_overrides.push_back( - onnxruntime::FreeDimensionOverride{dim_denotation, onnxruntime::FreeDimensionOverrideType::Denotation, dim_value} - ); + onnxruntime::FreeDimensionOverride{dim_denotation, onnxruntime::FreeDimensionOverrideType::Denotation, dim_value}); return nullptr; } ORT_API_STATUS_IMPL(OrtApis::AddFreeDimensionOverrideByName, _Inout_ OrtSessionOptions* options, _In_ const char* dim_name, _In_ int64_t dim_value) { options->value.free_dimension_overrides.push_back( - onnxruntime::FreeDimensionOverride{dim_name, onnxruntime::FreeDimensionOverrideType::Name, dim_value} - ); + onnxruntime::FreeDimensionOverride{dim_name, onnxruntime::FreeDimensionOverrideType::Name, dim_value}); return nullptr; } diff --git a/onnxruntime/core/session/abi_session_options_impl.h b/onnxruntime/core/session/abi_session_options_impl.h index 2e87ea7e70219..f23bfbe195037 100644 --- a/onnxruntime/core/session/abi_session_options_impl.h +++ b/onnxruntime/core/session/abi_session_options_impl.h @@ -6,7 +6,7 @@ #include #include #include -#include "core/session/inference_session.h" +#include "core/framework/session_options.h" #include "core/session/onnxruntime_c_api.h" #include "core/providers/providers.h" diff --git a/onnxruntime/test/api_tests_without_env/test_apis_without_env.cc b/onnxruntime/test/api_tests_without_env/test_apis_without_env.cc new file mode 100644 index 0000000000000..617f258851788 --- /dev/null +++ b/onnxruntime/test/api_tests_without_env/test_apis_without_env.cc @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#ifndef USE_ONNXRUNTIME_DLL +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wignored-qualifiers" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#else +#pragma warning(push) +#pragma warning(disable : 4018) /*'expression' : signed/unsigned mismatch */ +#pragma warning(disable : 4065) /*switch statement contains 'default' but no 'case' labels*/ +#pragma warning(disable : 4100) +#pragma warning(disable : 4146) /*unary minus operator applied to unsigned type, result still unsigned*/ +#pragma warning(disable : 4127) +#pragma warning(disable : 4244) /*'conversion' conversion from 'type1' to 'type2', possible loss of data*/ +#pragma warning(disable : 4251) /*'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'*/ +#pragma warning(disable : 4267) /*'var' : conversion from 'size_t' to 'type', possible loss of data*/ +#pragma warning(disable : 4305) /*'identifier' : truncation from 'type1' to 'type2'*/ +#pragma warning(disable : 4307) /*'operator' : integral constant overflow*/ +#pragma warning(disable : 4309) /*'conversion' : truncation of constant value*/ +#pragma warning(disable : 4334) /*'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)*/ +#pragma warning(disable : 4355) /*'this' : used in base member initializer list*/ +#pragma warning(disable : 4506) /*no definition for inline function 'function'*/ +#pragma warning(disable : 4800) /*'type' : forcing value to bool 'true' or 'false' (performance warning)*/ +#pragma warning(disable : 4996) /*The compiler encountered a deprecated declaration.*/ +#pragma warning(disable : 6011) /*Dereferencing NULL pointer*/ +#pragma warning(disable : 6387) /*'value' could be '0'*/ +#pragma warning(disable : 26495) /*Variable is uninitialized.*/ +#endif +#include +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#else +#pragma warning(pop) +#endif +#endif + +#include "gtest/gtest.h" +#include "core/session/onnxruntime_cxx_api.h" +#include "core/session/abi_session_options_impl.h" + +TEST(TestSessionOptions, SetIntraOpNumThreadsWithoutEnv) { + Ort::SessionOptions session_options; + session_options.SetIntraOpNumThreads(48); + const auto* ort_session_options = (const OrtSessionOptions*)session_options; +#ifdef _OPENMP + ASSERT_EQ(ort_session_options->value.intra_op_param.thread_pool_size, 0); +#else + ASSERT_EQ(ort_session_options->value.intra_op_param.thread_pool_size, 48); +#endif +} + +int main(int argc, char** argv) { + int status = 0; + try { + ::testing::InitGoogleTest(&argc, argv); + status = RUN_ALL_TESTS(); + } catch (const std::exception& ex) { + std::cerr << ex.what(); + status = -1; + } + +#ifndef USE_ONNXRUNTIME_DLL + //make memory leak checker happy + ::google::protobuf::ShutdownProtobufLibrary(); +#endif + return status; +} diff --git a/onnxruntime/test/shared_lib/test_fixture.h b/onnxruntime/test/shared_lib/test_fixture.h index 51e4981fd22c7..37db4f3da8f56 100644 --- a/onnxruntime/test/shared_lib/test_fixture.h +++ b/onnxruntime/test/shared_lib/test_fixture.h @@ -16,4 +16,3 @@ typedef const char* PATH_TYPE; static inline void ORT_API_CALL MyLoggingFunction(void*, OrtLoggingLevel, const char*, const char*, const char*, const char*) { } - diff --git a/tools/ci_build/github/linux/docker/scripts/install_centos.sh b/tools/ci_build/github/linux/docker/scripts/install_centos.sh index 0d041690fc199..f7c33e294ce9b 100755 --- a/tools/ci_build/github/linux/docker/scripts/install_centos.sh +++ b/tools/ci_build/github/linux/docker/scripts/install_centos.sh @@ -16,7 +16,7 @@ elif [ "$os_major_version" == "6" ] && [ ! -d "/opt/python/cp35-cp35m" ]; then #The base image we are using already contains devtoolset-2 yum install -y redhat-lsb-core expat-devel libcurl-devel tar unzip curl zlib-devel make libunwind icu aria2 rsync bzip2 git bzip2-devel #Install python 3.6 - yum install -y https://centos6.iuscommunity.org/ius-release.rpm + yum install -y https://repo.ius.io/ius-release-el6.rpm yum --enablerepo=ius install -y python36u python36u-devel python36u-pip python36u-numpy python36u-setuptools python36u-wheel /usr/bin/python3.6 -m pip install --upgrade pip else