You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

backend.cc 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "vm/backend.h"
  17. #include <algorithm>
  18. #include <vector>
  19. #include "utils/log_adapter.h"
  20. #include "ir/anf.h"
  21. #include "utils/callbacks.h"
  22. #include "utils/base_ref_extends.h"
  23. #include "backend/session/session_factory.h"
  24. #include "utils/ms_utils.h"
  25. #ifdef ENABLE_GE
  26. #include "utils/callbacks_ge.h"
  27. #endif
  28. namespace mindspore {
  29. namespace compile {
  30. bool Backend::GetCond(const BaseRef &c, bool *const value) { return BaseRefToBool(c, value); }
  31. bool Backend::GetIndex(const BaseRef &c, int *const value) { return BaseRefToInt(utils::cast<ValuePtr>(c), value); }
  32. LinConvertResult MsBackend::MsConvert(const AnfNodePtrList &lst, const std::string &target) {
  33. MS_LOG(DEBUG) << "MsConvert";
  34. MS_EXCEPTION_IF_NULL(MsContext::GetInstance());
  35. auto cached = g_ConvertCache.find(lst);
  36. if (cached != g_ConvertCache.end()) {
  37. return cached->second;
  38. }
  39. LinConvertResult result;
  40. FuncGraphPtr fg;
  41. AnfNodePtrList inputs;
  42. AnfNodePtrList outputs;
  43. std::tie(fg, inputs, outputs) = TransformSegmentToAnfGraph(lst);
  44. result.inputs = inputs;
  45. result.outputs = outputs;
  46. result.graph_id = kInvalidGraphId;
  47. GraphId graph_id = kInvalidGraphId;
  48. if (target != target_device_ && !target.empty()) {
  49. CreateOtherSession(target);
  50. graph_id = other_sess_->CompileGraph(lst, outputs);
  51. } else {
  52. graph_id = target_sess_->CompileGraph(lst, outputs);
  53. }
  54. if (MsContext::GetInstance()->precompile_only()) {
  55. MS_LOG(INFO) << "PrecompileOnly, stop run graph";
  56. return result;
  57. }
  58. if (target != target_device_ && !target.empty()) {
  59. other_sess_->BuildGraph(graph_id);
  60. } else if (!is_multi_graph_sink_) {
  61. target_sess_->BuildGraph(graph_id);
  62. }
  63. result.run = std::make_shared<RunFunc>(
  64. [graph_id, target, this](const VectorRef &args) -> VectorRef { return MsRunGraph(graph_id, args, target); });
  65. MS_EXCEPTION_IF_NULL(result.run);
  66. result.simu_run = std::make_shared<RunFunc>(
  67. [graph_id, this](const VectorRef &args) -> VectorRef { return MsSimuRunGraph(graph_id, args); });
  68. MS_EXCEPTION_IF_NULL(result.simu_run);
  69. result.graph_id = graph_id;
  70. graph_id_map_[graph_id] = result;
  71. (void)g_ConvertCache.emplace(lst, result);
  72. return result;
  73. }
  74. // compile set input output
  75. VectorRef MsBackend::MsSimuRunGraph(const GraphId &g, const VectorRef &args) {
  76. MS_LOG(DEBUG) << "set graph input:" << g;
  77. std::vector<BaseRef> outputs;
  78. (void)std::transform(graph_id_map_[g].outputs.begin(), graph_id_map_[g].outputs.end(), std::back_inserter(outputs),
  79. [](const AnfNodePtr &v) { return v; });
  80. return VectorRef(outputs);
  81. }
  82. namespace {
  83. void PushInputTensor(const BaseRef &arg, std::vector<tensor::TensorPtr> *inputs) {
  84. MS_EXCEPTION_IF_NULL(inputs);
  85. if (utils::isa<tensor::TensorPtr>(arg)) {
  86. auto value = utils::cast<tensor::TensorPtr>(arg);
  87. inputs->push_back(value);
  88. } else if (utils::isa<ValuePtr>(arg)) {
  89. auto value = utils::cast<ValuePtr>(arg);
  90. MS_EXCEPTION_IF_NULL(value);
  91. if (value->isa<ValueTuple>()) {
  92. auto value_tuple = value->cast<ValueTuplePtr>();
  93. MS_EXCEPTION_IF_NULL(value_tuple);
  94. auto tuple_value = value_tuple->value();
  95. (void)std::transform(tuple_value.begin(), tuple_value.end(), std::back_inserter(*inputs),
  96. [](const ValuePtr &v) { return v->cast<tensor::TensorPtr>(); });
  97. } else if (value->isa<Scalar>()) {
  98. tensor::TensorPtr scalar_tensor = ScalarToTensor(value->cast<ScalarPtr>());
  99. inputs->push_back(scalar_tensor);
  100. } else {
  101. inputs->push_back(value->cast<tensor::TensorPtr>());
  102. }
  103. } else if (utils::isa<PyObjectRef>(arg)) {
  104. auto value = utils::cast<PyObjectRef>(arg).object_;
  105. inputs->push_back(py::cast<tensor::TensorPtr>(value));
  106. } else if (utils::isa<VectorRefPtr>(arg)) {
  107. const auto &args_new = utils::cast<VectorRef>(arg);
  108. for (const auto &v : args_new) {
  109. PushInputTensor(v, inputs);
  110. }
  111. } else {
  112. MS_LOG(WARNING) << "Invalid input type.";
  113. }
  114. }
  115. } // namespace
  116. VectorRef MsBackend::MsRunGraph(const GraphId &g, const VectorRef &args, const std::string &target) {
  117. MS_LOG(DEBUG) << "start ms graph run:" << args.size() << ", g:" << g;
  118. // Run graph
  119. std::vector<tensor::TensorPtr> inputs;
  120. for (const auto &arg : args) {
  121. PushInputTensor(arg, &inputs);
  122. }
  123. VectorRef outputs;
  124. // call ms rungraph (graphId, input ,output)
  125. if (target != target_device_ && !target.empty()) {
  126. other_sess_->RunGraph(g, inputs, &outputs);
  127. } else {
  128. target_sess_->RunGraph(g, inputs, &outputs);
  129. }
  130. MS_LOG(DEBUG) << "RunGraph finished:" << outputs.size();
  131. return outputs;
  132. }
  133. void MsBackend::Link(GraphId graph_id) {
  134. if (graph_id == kInvalidGraphId) {
  135. graph_id = target_sess_->GetFinalRunGraph();
  136. }
  137. target_sess_->BuildGraph(graph_id);
  138. }
  139. Backend::Backend(const std::string &name) : name_(name) {
  140. MS_LOG(DEBUG) << "select backend:" << name;
  141. convert_fn_ = backends[name_];
  142. is_multi_graph_sink_ = false;
  143. }
  144. MsBackend::MsBackend(const std::string &name, const std::string &target, uint32_t device_id) : Backend(name) {
  145. convert_fn_ = std::bind(&MsBackend::MsConvert, this, std::placeholders::_1, std::placeholders::_2);
  146. target_sess_ = session::SessionFactory::Get().Create(target);
  147. if (target_sess_ == nullptr) {
  148. MS_LOG(EXCEPTION) << "Session create failed!, please make sure target device:" << target << " is available.";
  149. }
  150. target_sess_->Init(device_id);
  151. target_sess_->RegisterSummaryCallBackFunc(callbacks::SummarySaveCallback);
  152. target_device_ = target;
  153. }
  154. void MsBackend::CreateOtherSession(const std::string &target) {
  155. if (other_sess_ != nullptr && other_device_ == target) {
  156. return;
  157. }
  158. other_sess_ = session::SessionFactory::Get().Create(target);
  159. if (other_sess_ == nullptr) {
  160. MS_LOG(EXCEPTION) << "Session create failed!, please make sure target device:" << target << " is available.";
  161. }
  162. auto context_ptr = MsContext::GetInstance();
  163. MS_EXCEPTION_IF_NULL(context_ptr);
  164. uint32_t device_id = context_ptr->device_id();
  165. other_sess_->Init(device_id);
  166. other_sess_->RegisterSummaryCallBackFunc(callbacks::SummarySaveCallback);
  167. other_device_ = target;
  168. }
  169. GraphId MsBackend::CompileGraph(NotNull<FuncGraphPtr> fg) { return target_sess_->CompileGraph(fg); }
  170. VectorRef MsBackend::RunGraph(GraphId graph_id, const VectorRef &args) { return MsRunGraph(graph_id, args); }
  171. #ifdef ENABLE_DEBUGGER
  172. void MsBackend::SetDebugger() { target_sess_->SetDebugger(); }
  173. #endif
  174. } // namespace compile
  175. } // namespace mindspore