From ea5fa78385c6f8d071095d640f7ccdb686158552 Mon Sep 17 00:00:00 2001 From: rick_sanchez Date: Thu, 11 Jun 2020 12:32:48 +0800 Subject: [PATCH] Fix python test case of UT probability failure. --- mindspore/ccsrc/pipeline/pipeline.cc | 2 +- mindspore/ccsrc/pynative/pynative_execute.cc | 31 +++++++++++++++----- mindspore/ccsrc/pynative/pynative_execute.h | 3 +- mindspore/common/api.py | 4 +-- mindspore/nn/cell.py | 1 + 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/mindspore/ccsrc/pipeline/pipeline.cc b/mindspore/ccsrc/pipeline/pipeline.cc index f4c41fec1c..e154ad7f7c 100644 --- a/mindspore/ccsrc/pipeline/pipeline.cc +++ b/mindspore/ccsrc/pipeline/pipeline.cc @@ -882,7 +882,7 @@ void ClearResAtexit() { pipeline::GetMethodMap().clear(); pipeline::ExecutorPy::ClearRes(); pipeline::ReclaimOptimizer(); - pynative::PynativeExecutor::GetInstance()->Clean(); + pynative::PynativeExecutor::GetInstance()->ClearRes(); #ifdef ENABLE_GE transform::DfGraphManager::GetInstance().ClearGraph(); transform::DfGraphConvertor::get_adpt_map().clear(); diff --git a/mindspore/ccsrc/pynative/pynative_execute.cc b/mindspore/ccsrc/pynative/pynative_execute.cc index 8dfb6d1942..c1cad930c0 100644 --- a/mindspore/ccsrc/pynative/pynative_execute.cc +++ b/mindspore/ccsrc/pynative/pynative_execute.cc @@ -595,7 +595,7 @@ py::tuple RunOp(const py::args &args) { void ClearPyNativeSession() { session = nullptr; } -PynativeExecutor::~PynativeExecutor() { Clean(); } +PynativeExecutor::~PynativeExecutor() { ClearRes(); } PynativeExecutor::PynativeExecutor() { grad_flag_ = false; } @@ -849,17 +849,32 @@ void PynativeExecutor::GradNet(const GradOperationPtr &grad, const py::object &c pipeline::ReclaimOptimizer(); } -void PynativeExecutor::Clear() { - MS_LOG(INFO) << "Clear all res"; - top_g_ = curr_g_ = nullptr; - std::stack().swap(graph_p_); +void PynativeExecutor::Clear(const std::string &flag) { + if (flag == "resource") { + MS_LOG(INFO) << "Clear res"; + Clean(); + return; + } + MS_LOG(INFO) << "Clear"; + top_g_ = nullptr; + curr_g_ = nullptr; graph_info_map_.clear(); + std::stack().swap(graph_p_); } void PynativeExecutor::Clean() { + MS_LOG(INFO) << "Clean all res"; + Clear(); + grad_flag_ = false; graph_map_.clear(); cell_graph_map_.clear(); - Clear(); + df_builder_ = nullptr; + ad::CleanRes(); + pipeline::ReclaimOptimizer(); +} + +void PynativeExecutor::ClearRes() { + Clean(); resource_.reset(); } @@ -908,8 +923,8 @@ REGISTER_PYBIND_DEFINE(PynativeExecutor_, ([](const py::module *m) { .def_static("get_instance", &PynativeExecutor::GetInstance, "PynativeExecutor get_instance.") .def("new_graph", &PynativeExecutor::NewGraph, "pynative new a graph.") .def("end_graph", &PynativeExecutor::EndGraph, "pynative end a graph.") - .def("grad_net", &PynativeExecutor::GradNet, "pynative grad graph.") - .def("clear", &PynativeExecutor::Clear, "pynative clear status.") + .def("grad_net", &PynativeExecutor::GradNet, "pynative grad graph.") + .def("clear", &PynativeExecutor::Clear, "pynative clear status.") .def("__call__", &PynativeExecutor::Run, py::arg("args"), py::arg("phase") = py::str(""), "Executor run function.") .def("set_grad_flag", &PynativeExecutor::set_grad_flag, py::arg("flag") = py::bool_(false), diff --git a/mindspore/ccsrc/pynative/pynative_execute.h b/mindspore/ccsrc/pynative/pynative_execute.h index d9a236c738..19ae9965c8 100644 --- a/mindspore/ccsrc/pynative/pynative_execute.h +++ b/mindspore/ccsrc/pynative/pynative_execute.h @@ -68,8 +68,9 @@ class PynativeExecutor : public std::enable_shared_from_this { void NewGraph(const py::object &cell, const py::args &args); void EndGraph(const py::object &cell, const py::object &out, const py::args &args); void GradNet(const GradOperationPtr &grad, const py::object &cell, const py::object &weights, const py::args &args); - void Clear(); + void Clear(const std::string &flag = ""); void Clean(); + void ClearRes(); bool grad_flag() { return grad_flag_; } void set_grad_flag(bool flag) { grad_flag_ = flag; } AnfNodePtr GetInput(const py::object &obj, const py::object &op_mask); diff --git a/mindspore/common/api.py b/mindspore/common/api.py index 6d4a24b60b..19a394462f 100644 --- a/mindspore/common/api.py +++ b/mindspore/common/api.py @@ -293,8 +293,8 @@ class _PynativeExecutor: def grad(self, grad, obj, weights, *args): self._executor.grad_net(grad, obj, weights, *args) - def clear(self): - self._executor.clear() + def clear(self, flag=""): + self._executor.clear(flag) def set_grad_flag(self, flag): self._executor.set_grad_flag(flag) diff --git a/mindspore/nn/cell.py b/mindspore/nn/cell.py index f79dce9777..1b3ebb828c 100755 --- a/mindspore/nn/cell.py +++ b/mindspore/nn/cell.py @@ -186,6 +186,7 @@ class Cell: raise AttributeError("'{}' object has no attribute '{}'.".format(type(self).__name__, name)) def __del__(self): + _pynative_exec.clear("resource") if hasattr(self, "_create_time"): _executor.del_net_res(str(self._create_time))