Fix issue with debug VS2022 build when python bindings are enabled (#9794)

* Add intermediate header between the ORT code and pybind11 to workaround an issue with VS2022 debug builds by making sure corecrt.h is included first.

This avoids the _STL_ASSERT macro being defined in an incompatible way for a debug build by pybind including the python headers with _DEBUG temporarily undefined .

See #9735 for details.
This commit is contained in:
Scott McKay 2021-11-18 16:58:02 +10:00 committed by GitHub
parent e23892ddbe
commit 1aa21df149
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 14 deletions

View file

@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
// If performing a debug build with VS2022 (_MSC_VER == 1930) we need to include corecrt.h before pybind
// so that the _STL_ASSERT macro is defined in a compatible way.
//
// pybind11/pybind11.h includes pybind11/detail/common.h, which undefines _DEBUG whilst including the Python headers
// (which in turn include corecrt.h). This alters how the _STL_ASSERT macro is defined and causes the build to fail.
//
// see https://github.com/microsoft/onnxruntime/issues/9735
//
#if defined(_MSC_VER) && defined(_DEBUG) && _MSC_VER >= 1930
#include <corecrt.h>
#endif
#include <pybind11/pybind11.h>

View file

@ -3,7 +3,7 @@
#pragma once
#include <pybind11/pybind11.h>
#include "onnxruntime_pybind.h" // must use this for the include of <pybind11/pybind11.h>
#include <stdexcept>
#include "core/common/status.h"

View file

@ -4,8 +4,8 @@
#pragma once
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "onnxruntime_pybind.h" // must use this for the include of <pybind11/pybind11.h>
#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

View file

@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <pybind11/pybind11.h>
#include "onnxruntime_pybind.h" // must use this for the include of <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "core/providers/get_execution_providers.h"
@ -20,5 +20,5 @@ PYBIND11_MODULE(onnxruntime_pybind11_state, m) {
"The order of elements represents the default priority order of Execution Providers "
"from highest to lowest.");
}
}
}
} // namespace python
} // namespace onnxruntime

View file

@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <pybind11/pybind11.h>
#include "onnxruntime_pybind.h" // must use this for the include of <pybind11/pybind11.h>
namespace onnxruntime {
namespace python {
@ -10,6 +10,5 @@ void addGlobalMethods(py::module& m, Environment& env);
void addObjectMethods(py::module& m, Environment& env);
void addOrtValueMethods(pybind11::module& m);
}
}
} // namespace python
} // namespace onnxruntime

View file

@ -15,7 +15,7 @@
#include "core/dlpack/dlpack_converter.h"
#endif
#include <pybind11/pybind11.h>
#include "onnxruntime_pybind.h" // must use this for the include of <pybind11/pybind11.h>
// execution provider factory creator headers
struct OrtStatus {
@ -207,7 +207,7 @@ class SparseTensor;
#endif
namespace python {
using ExecutionProviderRegistrationFn = std::function<void(InferenceSession*,
using ExecutionProviderRegistrationFn = std::function<void(InferenceSession*,
const std::vector<std::string>&,
const ProviderOptionsMap&)>;
@ -313,7 +313,7 @@ inline AllocatorPtr& GetAllocator() {
// so incoming arrays do not disappear. To this end we create an instance of SparseTensor
// on top of the user provided numpy arrays and create a duplicate of py::objects for those
// numpy array for ref-counting purposes and store it here.
//
//
// - to be able to expose SparseTensor returned from run method. We get an OrtValue from run()
// and store a copy of it in ort_value_. The OrtValue shared_ptr ref-counting will make sure
// the memory stays around.
@ -375,7 +375,6 @@ class PySparseTensor {
std::unique_ptr<OrtValue> AsOrtValue() const;
private:
// instance_ represents data that comes as input. Thus we depend on numpy
// arrays that own the underlying memory to stay around. We store copies
// of py::objects for those arrays in backing_storage_ as an extra ref-count.
@ -480,5 +479,5 @@ std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Nnapi(
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Rknpu();
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_CoreML(uint32_t flags);
constexpr const char* kDefaultExecutionProviderEntry = "GetProvider";
constexpr const char* kDefaultExecutionProviderEntry = "GetProvider";
} // namespace onnxruntime