From 23a91c8ba889d77589d6acf44fa9e9bce5fbb701 Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Thu, 30 Nov 2023 08:07:47 -0800 Subject: [PATCH] Fix warning C4003 in ORT python binding code (#18612) ### Description Fix warning C4003 in ORT python binding code. ### Motivation and Context It's better to fix the warning instead of suppressing it. --- .../python/onnxruntime_pybind_module.cc | 6 +++-- .../python/onnxruntime_pybind_state.cc | 26 ++++++------------- .../python/orttraining_python_module.cc | 4 +-- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/onnxruntime/python/onnxruntime_pybind_module.cc b/onnxruntime/python/onnxruntime_pybind_module.cc index 1d8ca195ab..aea43c6048 100644 --- a/onnxruntime/python/onnxruntime_pybind_module.cc +++ b/onnxruntime/python/onnxruntime_pybind_module.cc @@ -16,11 +16,13 @@ static constexpr bool HAS_COLLECTIVE_OPS = true; static constexpr bool HAS_COLLECTIVE_OPS = false; #endif -void CreateInferencePybindStateModule(py::module& m); +bool CreateInferencePybindStateModule(py::module& m); void CreateQuantPybindModule(py::module& m); PYBIND11_MODULE(onnxruntime_pybind11_state, m) { - CreateInferencePybindStateModule(m); + if (!CreateInferencePybindStateModule(m)) { + throw pybind11::import_error(); + } // move it out of shared method since training build has a little different behavior. m.def( "get_available_providers", []() -> const std::vector& { return GetAvailableExecutionProviderNames(); }, diff --git a/onnxruntime/python/onnxruntime_pybind_state.cc b/onnxruntime/python/onnxruntime_pybind_state.cc index 56312898b0..27fbf19084 100644 --- a/onnxruntime/python/onnxruntime_pybind_state.cc +++ b/onnxruntime/python/onnxruntime_pybind_state.cc @@ -49,16 +49,12 @@ namespace onnxruntime { } // namespace onnxruntime #if defined(_MSC_VER) -#pragma warning(disable : 4267 4996 4503 4003) +#pragma warning(disable : 4267 4996 4503) #endif // _MSC_VER #include #include -#if defined(_MSC_VER) -#pragma warning(disable : 4267 4996 4503 4003) -#endif // _MSC_VER - namespace onnxruntime { namespace python { @@ -2059,15 +2055,11 @@ including arg name, arg type (contains both type and shape).)pbdoc") .export_values(); } -void CreateInferencePybindStateModule(py::module& m) { +bool CreateInferencePybindStateModule(py::module& m) { m.doc() = "pybind11 stateful interface to ONNX runtime"; RegisterExceptions(m); - // Initialization of the module - ([]() -> void { - // import_array1() forces a void return value. - import_array1(); - })(); + import_array1(false); auto env = GetEnv(); @@ -2087,13 +2079,13 @@ void CreateInferencePybindStateModule(py::module& m) { addGlobalSchemaFunctions(m); addOpSchemaSubmodule(m); addOpKernelSubmodule(m); + return true; } -void InitArray() { - ([]() -> void { - // import_array1() forces a void return value. - import_array1(); - })(); +// This function is only used by orttraining module +bool InitArray() { + import_array1(false); + return true; } namespace { @@ -2136,8 +2128,6 @@ class EnvInitializer { private: EnvInitializer() { - // Initialization of the module - InitArray(); std::unique_ptr env_ptr; Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON); OrtPybindThrowIfError(Environment::Create(std::make_unique( diff --git a/orttraining/orttraining/python/orttraining_python_module.cc b/orttraining/orttraining/python/orttraining_python_module.cc index 4d1db7334f..55cd2af2d0 100644 --- a/orttraining/orttraining/python/orttraining_python_module.cc +++ b/orttraining/orttraining/python/orttraining_python_module.cc @@ -45,7 +45,7 @@ void addObjectMethodsForEager(py::module& m); #ifdef ENABLE_LAZY_TENSOR void addObjectMethodsForLazyTensor(py::module& m); #endif -void InitArray(); +bool InitArray(); bool GetDyanmicExecutionProviderHash( const std::string& ep_shared_lib_path, @@ -225,7 +225,7 @@ class TrainingEnvInitialzer { private: TrainingEnvInitialzer() { - InitArray(); + ORT_ENFORCE(InitArray()); Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON); ort_training_env_ = std::make_unique(); }