diff --git a/python/include/utils.h b/python/include/utils.h index f1d3a2d9..9e4a0064 100644 --- a/python/include/utils.h +++ b/python/include/utils.h @@ -180,6 +180,8 @@ void doNothingDel(const T& self) issueDeprecationWarning("del obj"); } +void throwPyIndexError(std::string message = "out of bounds"); + } // namespace utils } // namespace tensorrt diff --git a/python/src/infer/pyCore.cpp b/python/src/infer/pyCore.cpp index 4db7c99b..626f7cbf 100644 --- a/python/src/infer/pyCore.cpp +++ b/python/src/infer/pyCore.cpp @@ -172,7 +172,9 @@ static const auto engine_getitem = [](ICudaEngine& self, int pyIndex) { // Support python's negative indexing size_t index = (pyIndex < 0) ? static_cast(self.getNbBindings()) + pyIndex : pyIndex; if (index >= self.getNbBindings()) - throw py::index_error(); + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } return self.getBindingName(index); }; diff --git a/python/src/infer/pyFoundationalTypes.cpp b/python/src/infer/pyFoundationalTypes.cpp index ef5d9946..a20f2b9e 100644 --- a/python/src/infer/pyFoundationalTypes.cpp +++ b/python/src/infer/pyFoundationalTypes.cpp @@ -100,7 +100,9 @@ static const auto dims_getter = [](const Dims& self, int pyIndex) -> const int& // Without these bounds checks, horrible infinite looping will occur. size_t index = (pyIndex < 0) ? static_cast(self.nbDims) + pyIndex : pyIndex; if (index >= self.nbDims) - throw py::index_error(); + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } return self.d[index]; }; @@ -110,7 +112,9 @@ static const auto dims_getter_slice = [](const Dims& self, py::slice slice) { throw py::error_already_set(); // Disallow out-of-bounds things. if (stop > self.nbDims) - throw py::index_error(); + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } py::tuple ret{slicelength}; for (int i = start, index = 0; i < stop; i += step, ++index) @@ -121,7 +125,9 @@ static const auto dims_getter_slice = [](const Dims& self, py::slice slice) { static const auto dims_setter = [](Dims& self, int pyIndex, int item) { size_t index = (pyIndex < 0) ? static_cast(self.nbDims) + pyIndex : pyIndex; if (index >= self.nbDims) - throw py::index_error(); + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } self.d[index] = item; }; @@ -131,7 +137,9 @@ static const auto dims_setter_slice = [](Dims& self, py::slice slice, const Dims throw py::error_already_set(); // Disallow out-of-bounds things. if (stop >= self.nbDims) - throw py::index_error(); + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } for (int i = start, index = 0; i < stop; i += step, ++index) self.d[i] = other.d[index]; diff --git a/python/src/infer/pyGraph.cpp b/python/src/infer/pyGraph.cpp index 8f22161b..be72ef21 100644 --- a/python/src/infer/pyGraph.cpp +++ b/python/src/infer/pyGraph.cpp @@ -82,14 +82,20 @@ namespace tensorrt static const auto permutation_getter = [] (const Permutation& self, int pyIndex) { size_t index = (pyIndex < 0) ? static_cast(Dims::MAX_DIMS) + pyIndex : pyIndex; // Static cast is REQUIRED here, or chaos ensues as MAX_DIMS is not pulled in at link time. - if (index >= static_cast(Dims::MAX_DIMS)) throw py::index_error(); + if (index >= static_cast(Dims::MAX_DIMS)) + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } return self.order[index]; }; static const auto permutation_setter = [] (Permutation& self, int pyIndex, int item) { size_t index = (pyIndex < 0) ? static_cast(Dims::MAX_DIMS) + pyIndex : pyIndex; // Static cast is REQUIRED here, or chaos ensues as MAX_DIMS is not pulled in at link time. - if (index >= static_cast(Dims::MAX_DIMS)) throw py::index_error(); + if (index >= static_cast(Dims::MAX_DIMS)) + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } self.order[index] = item; }; @@ -197,7 +203,10 @@ namespace tensorrt static const auto network_getitem = [](INetworkDefinition& self, int pyIndex) { // Support python's negative indexing size_t index = (pyIndex < 0) ? self.getNbLayers() + pyIndex : pyIndex; - if (index >= self.getNbLayers()) throw py::index_error(); + if (index >= self.getNbLayers()) + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } return self.getLayer(index); }; diff --git a/python/src/infer/pyPlugin.cpp b/python/src/infer/pyPlugin.cpp index e5e1770b..52cc177f 100644 --- a/python/src/infer/pyPlugin.cpp +++ b/python/src/infer/pyPlugin.cpp @@ -17,6 +17,7 @@ // This file contains all bindings related to plugins. #include "ForwardDeclarations.h" #include "infer/pyPluginDoc.h" +#include "utils.h" #include #include @@ -217,7 +218,9 @@ void bindPlugin(py::module& m) .def("__len__", [](PluginFieldCollection& self) { return self.nbFields; }) .def("__getitem__", [](PluginFieldCollection& self, int index) { if (index >= self.nbFields) - throw py::index_error(); + { + utils::throwPyIndexError(); // See definition of throwPyIndexError() for details + } return self.fields[index]; }); diff --git a/python/src/utils.cpp b/python/src/utils.cpp index 00eec549..d916695e 100644 --- a/python/src/utils.cpp +++ b/python/src/utils.cpp @@ -29,5 +29,15 @@ void issueDeprecationWarning(const char* useInstead) PyErr_WarnEx(PyExc_DeprecationWarning, msg.c_str(), 1); } +// The following is a helper WAR to "throw py::index_error()", which results in an incompatibility +// with Tensorflow 2.5 and above--on Windows only--when Tensorflow is imported after TensorRT. +// The TF library fast_module_type.pyd hooks on to IndexErrors thrown through py::index_error() +// resulting in hangs at unpacking operations and out-of-bounds index accesses. +void throwPyIndexError(std::string message) +{ + PyErr_SetString(PyExc_IndexError, message.data()); + throw py::error_already_set(); +} + } // namespace utils } // namespace tensorrt