diff --git a/mindspore/ccsrc/pybind_api/ir/primitive_py.cc b/mindspore/ccsrc/pybind_api/ir/primitive_py.cc index 57029064266..81f3beff2fd 100644 --- a/mindspore/ccsrc/pybind_api/ir/primitive_py.cc +++ b/mindspore/ccsrc/pybind_api/ir/primitive_py.cc @@ -398,6 +398,20 @@ void PrimitivePyAdapter::AddPyAttr(const py::str &name, const py::object &obj) { if (prim != nullptr) { prim->AddAttr(attr_name, converted_ret); } + + if (attr_name == "primitive_target") { + MS_EXCEPTION_IF_NULL(converted_ret); + if (!converted_ret->isa()) { + MS_LOG(EXCEPTION) << "Only support string CPU|GPU|Ascend for primitive_target"; + } + + auto target = GetValue(converted_ret); + if (target != kCPUDevice && target != kGPUDevice) { + auto context_ptr = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context_ptr); + context_ptr->set_param(MS_CTX_ALREADY_SET_ENABLE_MINDRT, true); + } + } } void PrimitivePyAdapter::DelPyAttr(const py::str &name) { diff --git a/mindspore/ccsrc/vm/transform.cc b/mindspore/ccsrc/vm/transform.cc index 7eb82a04650..b6e80dd8435 100644 --- a/mindspore/ccsrc/vm/transform.cc +++ b/mindspore/ccsrc/vm/transform.cc @@ -566,6 +566,10 @@ BackendPtr CreateBackend() { void SetMindRTEnable() { auto context_ptr = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(context_ptr); + if (context_ptr->get_param(MS_CTX_ALREADY_SET_ENABLE_MINDRT)) { + return; + } + std::string target = context_ptr->get_param(MS_CTX_DEVICE_TARGET); if ((target != kGPUDevice) && (target != kCPUDevice)) { return; diff --git a/mindspore/core/utils/ms_context.cc b/mindspore/core/utils/ms_context.cc index a83154f7a3f..41eca0f48d5 100644 --- a/mindspore/core/utils/ms_context.cc +++ b/mindspore/core/utils/ms_context.cc @@ -87,6 +87,7 @@ MsContext::MsContext(const std::string &policy, const std::string &target) { set_param(MS_CTX_SAVE_COMPILE_CACHE, false); set_param(MS_CTX_LOAD_COMPILE_CACHE, false); set_param(MS_CTX_ENABLE_MINDRT, false); + set_param(MS_CTX_ALREADY_SET_ENABLE_MINDRT, false); backend_policy_ = policy_map_[policy]; } diff --git a/mindspore/core/utils/ms_context.h b/mindspore/core/utils/ms_context.h index 6ae46c29e98..e4819332ffd 100644 --- a/mindspore/core/utils/ms_context.h +++ b/mindspore/core/utils/ms_context.h @@ -89,6 +89,7 @@ enum MsCtxParam : unsigned { MS_CTX_SAVE_COMPILE_CACHE, MS_CTX_LOAD_COMPILE_CACHE, MS_CTX_ENABLE_MINDRT, + MS_CTX_ALREADY_SET_ENABLE_MINDRT, MS_CTX_TYPE_BOOL_END, // parameter of type int