diff --git a/mindspore/ccsrc/backend/session/executor.cc b/mindspore/ccsrc/backend/session/executor.cc index 94ba3e605e8..ebe54e57dd6 100644 --- a/mindspore/ccsrc/backend/session/executor.cc +++ b/mindspore/ccsrc/backend/session/executor.cc @@ -380,8 +380,8 @@ void Executor::RunGraphAsync(const SessionPtr &session, const GraphId &graph_id, session->CreateOutputTensors(graph_id, inputs, outputs, &task->tensor_to_node_); // maintain a copy of output vector task->outputs_ = *outputs; - // sync run graph without output tensor(int dataset graph) - if (!TensorInVector(outputs) && !graph->HasPostGraph()) { + // sync run graph without output tensor(int dataset graph) or the graph require gil. + if ((!TensorInVector(outputs) && !graph->HasPostGraph()) || graph->is_need_gil()) { task->sync_run_ = true; RunTask(task, true, true); return; diff --git a/mindspore/ccsrc/backend/session/kernel_graph.h b/mindspore/ccsrc/backend/session/kernel_graph.h index 0bd1a75f8cd..e303e0863dd 100644 --- a/mindspore/ccsrc/backend/session/kernel_graph.h +++ b/mindspore/ccsrc/backend/session/kernel_graph.h @@ -341,6 +341,10 @@ class KernelGraph : public FuncGraph { void set_is_all_nop_node(bool is_all_nop_node) { is_all_nop_node_ = is_all_nop_node; } std::map graph_output_map() { return graph_output_to_front_node_map_; } + // The interface to set/get the graph GIL flag. + void set_is_need_gil(bool flag) { is_need_gil_ = flag; } + bool is_need_gil() { return is_need_gil_; } + private: // remove value node form graph bool RemoveValueNodeFromGraph(const ValueNodePtr &value_node); @@ -446,6 +450,9 @@ class KernelGraph : public FuncGraph { // If all the nodes of graph is the nop node. bool is_all_nop_node_{false}; + + // Indicate whether the kernels in the graphs acquire Python GIL. + bool is_need_gil_{false}; }; } // namespace session using KernelGraphPtr = std::shared_ptr; diff --git a/mindspore/ccsrc/backend/session/session_basic.cc b/mindspore/ccsrc/backend/session/session_basic.cc index 2104effdfd9..bd5602e2a9f 100644 --- a/mindspore/ccsrc/backend/session/session_basic.cc +++ b/mindspore/ccsrc/backend/session/session_basic.cc @@ -435,6 +435,17 @@ void CheckInputTensorShape(const TensorPtr &tensor, const CNodePtr &kernel, size } } } + +void UpdateGraphAquireGilAttr(const NotNull &root_graph) { + for (const auto &cnode : root_graph->execution_order()) { + if (AnfAlgo::CheckPrimitiveType(cnode, prim::kPyFunc)) { + MS_LOG(INFO) << "The Graph require GIL. Graph id: " << root_graph->graph_id(); + root_graph->set_is_need_gil(true); + return; + } + } + return; +} } // namespace GraphId SessionBasic::graph_sum_ = 0; @@ -1103,6 +1114,7 @@ KernelGraphPtr SessionBasic::ConstructKernelGraph(const AnfNodePtrList &lst, con UnifyMindIR(graph); // Update Graph Dynamic Shape Attr UpdateGraphDynamicShapeAttr(NOT_NULL(graph)); + UpdateGraphAquireGilAttr(NOT_NULL(graph)); opt::BackendCommonOptimization(graph); graph->SetInputNodes(); SetInputNodeUsage(graph, manager); diff --git a/mindspore/core/base/core_ops.h b/mindspore/core/base/core_ops.h index fd8397483df..baf822db468 100644 --- a/mindspore/core/base/core_ops.h +++ b/mindspore/core/base/core_ops.h @@ -556,6 +556,7 @@ inline const PrimitivePtr kPrimWhile = std::make_shared("While"); inline const PrimitivePtr kPrimPull = std::make_shared("Pull"); inline const PrimitivePtr kPrimPush = std::make_shared("Push"); inline const PrimitivePtr kPrimNPUAllocFloatStatus = std::make_shared("NPUAllocFloatStatus"); +inline const PrimitivePtr kPyFunc = std::make_shared("PyFunc"); // Structures inline const PrimitivePtr kPrimMakeList = std::make_shared("make_list");