diff --git a/mindspore/ccsrc/session/kernel_graph.cc b/mindspore/ccsrc/session/kernel_graph.cc index 4153bbe24531d0e587b688f2c5ffc9bc68fa2c55..9577fa57106abd85d40dba3d950e433e7e2129f6 100644 --- a/mindspore/ccsrc/session/kernel_graph.cc +++ b/mindspore/ccsrc/session/kernel_graph.cc @@ -719,6 +719,14 @@ void KernelGraph::UpdateCallRealInput() { std::string KernelGraph::ToString() const { return std::string("kernel_graph_").append(std::to_string(graph_id_)); } -KernelGraph::~KernelGraph() { device::KernelRuntimeManager::Instance().ClearGraphResource(graph_id_); } +KernelGraph::~KernelGraph() { + auto context = MsContext::GetInstance(); + if (!context) { + return; + } + if (context->execution_mode() == kGraphMode) { + device::KernelRuntimeManager::Instance().ClearGraphResource(graph_id_); + } +} } // namespace session } // namespace mindspore