Skip to content

Commit 9275517

Browse files
【npu】release gil lock when predictor run (PaddlePaddle#72485)
* fix npu hang * fix npu hang * remove std::cout
1 parent dd6571b commit 9275517

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

paddle/fluid/pybind/inference_api.cc

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ void BindPaddlePredictor(py::module *m) {
744744
paddle_predictor
745745
.def("run",
746746
[](PaddlePredictor &self, const std::vector<PaddleTensor> &inputs) {
747-
#if defined(PADDLE_WITH_CUSTOM_DEVICE) && !defined(PADDLE_NO_PYTHON)
747+
#if !defined(PADDLE_NO_PYTHON)
748748
pybind11::gil_scoped_release release;
749749
#endif
750750
std::vector<PaddleTensor> outputs;
@@ -755,9 +755,15 @@ void BindPaddlePredictor(py::module *m) {
755755
.def("get_output_tensor", &PaddlePredictor::GetOutputTensor)
756756
.def("get_input_names", &PaddlePredictor::GetInputNames)
757757
.def("get_output_names", &PaddlePredictor::GetOutputNames)
758-
.def("zero_copy_run",
759-
&PaddlePredictor::ZeroCopyRun,
760-
py::arg("switch_stream") = false)
758+
.def(
759+
"zero_copy_run",
760+
[](PaddlePredictor &self, bool switch_stream) {
761+
#if !defined(PADDLE_NO_PYTHON)
762+
pybind11::gil_scoped_release release;
763+
#endif
764+
return self.ZeroCopyRun(switch_stream);
765+
},
766+
py::arg("switch_stream") = false)
761767
.def("clone", [](PaddlePredictor &self) { return self.Clone(nullptr); })
762768
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
763769
.def("clone",
@@ -797,7 +803,7 @@ void BindNativePredictor(py::module *m) {
797803
.def("run",
798804
[](NativePaddlePredictor &self,
799805
const std::vector<PaddleTensor> &inputs) {
800-
#if defined(PADDLE_WITH_CUSTOM_DEVICE) && !defined(PADDLE_NO_PYTHON)
806+
#if !defined(PADDLE_NO_PYTHON)
801807
pybind11::gil_scoped_release release;
802808
#endif
803809
std::vector<PaddleTensor> outputs;
@@ -806,9 +812,15 @@ void BindNativePredictor(py::module *m) {
806812
})
807813
.def("get_input_tensor", &NativePaddlePredictor::GetInputTensor)
808814
.def("get_output_tensor", &NativePaddlePredictor::GetOutputTensor)
809-
.def("zero_copy_run",
810-
&NativePaddlePredictor::ZeroCopyRun,
811-
py::arg("switch_stream") = false)
815+
.def(
816+
"zero_copy_run",
817+
[](NativePaddlePredictor &self, bool switch_stream) {
818+
#if !defined(PADDLE_NO_PYTHON)
819+
pybind11::gil_scoped_release release;
820+
#endif
821+
return self.ZeroCopyRun(switch_stream);
822+
},
823+
py::arg("switch_stream") = false)
812824
.def("clone",
813825
[](NativePaddlePredictor &self) { return self.Clone(nullptr); })
814826
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
@@ -1132,7 +1144,7 @@ void BindAnalysisPredictor(py::module *m) {
11321144
.def(
11331145
"run",
11341146
[](AnalysisPredictor &self, const std::vector<PaddleTensor> &inputs) {
1135-
#if defined(PADDLE_WITH_CUSTOM_DEVICE) && !defined(PADDLE_NO_PYTHON)
1147+
#if !defined(PADDLE_NO_PYTHON)
11361148
pybind11::gil_scoped_release release;
11371149
#endif
11381150
std::vector<PaddleTensor> outputs;
@@ -1144,9 +1156,15 @@ void BindAnalysisPredictor(py::module *m) {
11441156
.def("get_input_names", &AnalysisPredictor::GetInputNames)
11451157
.def("get_output_names", &AnalysisPredictor::GetOutputNames)
11461158
.def("get_input_tensor_shape", &AnalysisPredictor::GetInputTensorShape)
1147-
.def("zero_copy_run",
1148-
&AnalysisPredictor::ZeroCopyRun,
1149-
py::arg("switch_stream") = false)
1159+
.def(
1160+
"zero_copy_run",
1161+
[](AnalysisPredictor &self, bool switch_stream) {
1162+
#if !defined(PADDLE_NO_PYTHON)
1163+
pybind11::gil_scoped_release release;
1164+
#endif
1165+
return self.ZeroCopyRun(switch_stream);
1166+
},
1167+
py::arg("switch_stream") = false)
11501168
.def("clear_intermediate_tensor",
11511169
&AnalysisPredictor::ClearIntermediateTensor)
11521170
.def("try_shrink_memory", &AnalysisPredictor::TryShrinkMemory)
@@ -1185,7 +1203,7 @@ void BindPaddleInferPredictor(py::module *m) {
11851203
"run",
11861204
[](paddle_infer::Predictor &self,
11871205
const std::vector<paddle::Tensor> &in_tensor_list) {
1188-
#if defined(PADDLE_WITH_CUSTOM_DEVICE) && !defined(PADDLE_NO_PYTHON)
1206+
#if !defined(PADDLE_NO_PYTHON)
11891207
pybind11::gil_scoped_release release;
11901208
#endif
11911209
std::vector<paddle::Tensor> outputs;
@@ -1195,7 +1213,7 @@ void BindPaddleInferPredictor(py::module *m) {
11951213
py::arg("inputs"))
11961214
.def("run",
11971215
[](paddle_infer::Predictor &self) {
1198-
#if defined(PADDLE_WITH_CUSTOM_DEVICE) && !defined(PADDLE_NO_PYTHON)
1216+
#if !defined(PADDLE_NO_PYTHON)
11991217
pybind11::gil_scoped_release release;
12001218
#endif
12011219
self.Run();

0 commit comments

Comments
 (0)