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.2 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/convert_utils.h"
  23. #include "backend/session/session_factory.h"
  24. #include "utils/ms_utils.h"
  25. #include "pybind_api/ir/base_ref_py.h"
  26. #ifdef ENABLE_GE
  27. #include "utils/callbacks_ge.h"
  28. #endif
  29. namespace mindspore {
  30. namespace compile {
  31. bool Backend::GetCond(const BaseRef &c, bool *const value) { return BaseRefToBool(c, value); }
  32. bool Backend::GetIndex(const BaseRef &c, int *const value) { return BaseRefToInt(utils::cast<ValuePtr>(c), value); }
  33. LinConvertResult MsBackend::MsConvert(const AnfNodePtrList &lst, const std::string &target) {
  34. MS_LOG(DEBUG) << "MsConvert";
  35. MS_EXCEPTION_IF_NULL(MsContext::GetInstance());
  36. auto cached = g_ConvertCache.find(lst);
  37. if (cached != g_ConvertCache.end()) {
  38. return cached->second;
  39. }
  40. LinConvertResult result;
  41. FuncGraphPtr fg;
  42. AnfNodePtrList inputs;
  43. AnfNodePtrList outputs;
  44. std::tie(fg, inputs, outputs) = TransformSegmentToAnfGraph(lst);
  45. result.inputs = inputs;
  46. result.outputs = outputs;
  47. result.graph_id = kInvalidGraphId;
  48. GraphId graph_id = kInvalidGraphId;
  49. if (target != target_device_ && !target.empty()) {
  50. CreateOtherSession(target);
  51. graph_id = other_sess_->CompileGraph(lst, outputs);
  52. } else {
  53. graph_id = target_sess_->CompileGraph(lst, outputs);
  54. }
  55. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_PRECOMPILE_ONLY)) {
  56. MS_LOG(INFO) << "PrecompileOnly, stop run graph";
  57. return result;
  58. }
  59. if (target != target_device_ && !target.empty()) {
  60. other_sess_->BuildGraph(graph_id);
  61. } else if (!is_multi_graph_sink_) {
  62. target_sess_->BuildGraph(graph_id);
  63. }
  64. result.run = std::make_shared<RunFunc>(
  65. [graph_id, target, this](const VectorRef &args) -> VectorRef { return MsRunGraph(graph_id, args, target); });
  66. MS_EXCEPTION_IF_NULL(result.run);
  67. result.simu_run = std::make_shared<RunFunc>(
  68. [graph_id, this](const VectorRef &args) -> VectorRef { return MsSimuRunGraph(graph_id, args); });
  69. MS_EXCEPTION_IF_NULL(result.simu_run);
  70. result.graph_id = graph_id;
  71. graph_id_map_[graph_id] = result;
  72. (void)g_ConvertCache.emplace(lst, result);
  73. return result;
  74. }
  75. // compile set input output
  76. VectorRef MsBackend::MsSimuRunGraph(const GraphId &g, const VectorRef &args) {
  77. MS_LOG(DEBUG) << "set graph input:" << g;
  78. std::vector<BaseRef> outputs;
  79. (void)std::transform(graph_id_map_[g].outputs.begin(), graph_id_map_[g].outputs.end(), std::back_inserter(outputs),
  80. [](const AnfNodePtr &v) { return v; });
  81. return VectorRef(outputs);
  82. }
  83. namespace {
  84. void PushInputTensor(const BaseRef &arg, std::vector<tensor::TensorPtr> *inputs) {
  85. MS_EXCEPTION_IF_NULL(inputs);
  86. if (utils::isa<tensor::TensorPtr>(arg)) {
  87. auto value = utils::cast<tensor::TensorPtr>(arg);
  88. inputs->push_back(value);
  89. } else if (utils::isa<ValuePtr>(arg)) {
  90. auto value = utils::cast<ValuePtr>(arg);
  91. MS_EXCEPTION_IF_NULL(value);
  92. if (value->isa<ValueTuple>()) {
  93. auto value_tuple = value->cast<ValueTuplePtr>();
  94. MS_EXCEPTION_IF_NULL(value_tuple);
  95. auto tuple_value = value_tuple->value();
  96. (void)std::transform(tuple_value.begin(), tuple_value.end(), std::back_inserter(*inputs),
  97. [](const ValuePtr &v) { return v->cast<tensor::TensorPtr>(); });
  98. } else if (value->isa<Scalar>()) {
  99. tensor::TensorPtr scalar_tensor = ScalarToTensor(value->cast<ScalarPtr>());
  100. inputs->push_back(scalar_tensor);
  101. } else {
  102. inputs->push_back(value->cast<tensor::TensorPtr>());
  103. }
  104. } else if (utils::isa<PyObjectRef>(arg)) {
  105. auto value = utils::cast<PyObjectRef>(arg).object_;
  106. inputs->push_back(py::cast<tensor::TensorPtr>(value));
  107. } else if (utils::isa<VectorRefPtr>(arg)) {
  108. const auto &args_new = utils::cast<VectorRef>(arg);
  109. for (const auto &v : args_new) {
  110. PushInputTensor(v, inputs);
  111. }
  112. } else {
  113. MS_LOG(WARNING) << "Invalid input type.";
  114. }
  115. }
  116. } // namespace
  117. VectorRef MsBackend::MsRunGraph(const GraphId &g, const VectorRef &args, const std::string &target) {
  118. MS_LOG(DEBUG) << "start ms graph run:" << args.size() << ", g:" << g;
  119. // Run graph
  120. std::vector<tensor::TensorPtr> inputs;
  121. for (const auto &arg : args) {
  122. PushInputTensor(arg, &inputs);
  123. }
  124. VectorRef outputs;
  125. // call ms rungraph (graphId, input ,output)
  126. if (target != target_device_ && !target.empty()) {
  127. other_sess_->RunGraphAsync(g, inputs, &outputs);
  128. } else {
  129. target_sess_->RunGraphAsync(g, inputs, &outputs);
  130. }
  131. MS_LOG(DEBUG) << "RunGraph finished:" << outputs.size();
  132. return outputs;
  133. }
  134. void MsBackend::Link(GraphId graph_id) {
  135. if (graph_id == kInvalidGraphId) {
  136. graph_id = target_sess_->GetFinalRunGraph();
  137. }
  138. target_sess_->BuildGraph(graph_id);
  139. }
  140. Backend::Backend(const std::string &name) : name_(name) {
  141. MS_LOG(DEBUG) << "select backend:" << name;
  142. convert_fn_ = backends[name_];
  143. is_multi_graph_sink_ = false;
  144. }
  145. MsBackend::MsBackend(const std::string &name, const std::string &target, uint32_t device_id) : Backend(name) {
  146. convert_fn_ = std::bind(&MsBackend::MsConvert, this, std::placeholders::_1, std::placeholders::_2);
  147. target_sess_ = session::SessionFactory::Get().Create(target);
  148. if (target_sess_ == nullptr) {
  149. MS_LOG(EXCEPTION) << "Session create failed!, please make sure target device:" << target << " is available.";
  150. }
  151. target_sess_->Init(device_id);
  152. target_sess_->RegisterSummaryCallBackFunc(callbacks::SummarySaveCallback);
  153. target_device_ = target;
  154. }
  155. void MsBackend::CreateOtherSession(const std::string &target) {
  156. if (other_sess_ != nullptr && other_device_ == target) {
  157. return;
  158. }
  159. other_sess_ = session::SessionFactory::Get().Create(target);
  160. if (other_sess_ == nullptr) {
  161. MS_LOG(EXCEPTION) << "Session create failed!, please make sure target device:" << target << " is available.";
  162. }
  163. auto context_ptr = MsContext::GetInstance();
  164. MS_EXCEPTION_IF_NULL(context_ptr);
  165. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  166. other_sess_->Init(device_id);
  167. other_sess_->RegisterSummaryCallBackFunc(callbacks::SummarySaveCallback);
  168. other_device_ = target;
  169. }
  170. GraphId MsBackend::CompileGraph(NotNull<FuncGraphPtr> fg) { return target_sess_->CompileGraph(fg); }
  171. VectorRef MsBackend::RunGraph(GraphId graph_id, const VectorRef &args) { return MsRunGraph(graph_id, args); }
  172. #ifdef ENABLE_DEBUGGER
  173. void MsBackend::SetDebugger() { target_sess_->SetDebugger(); }
  174. #endif
  175. } // namespace compile
  176. } // namespace mindspore