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.

cpu_session.cc 7.3 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "backend/session/cpu_session.h"
  17. #include <algorithm>
  18. #include <sstream>
  19. #include "ir/tensor.h"
  20. #include "ir/anf.h"
  21. #include "backend/kernel_compiler/kernel.h"
  22. #include "utils/ms_utils.h"
  23. #include "backend/session/anf_runtime_algorithm.h"
  24. #include "runtime/device/kernel_runtime.h"
  25. #include "backend/kernel_compiler/cpu/cpu_kernel_factory.h"
  26. #include "runtime/device/cpu/kernel_select_cpu.h"
  27. #include "backend/optimizer/common/optimizer.h"
  28. #include "backend/optimizer/common/pass_manager.h"
  29. #include "backend/optimizer/pass/replace_node_by_proxy.h"
  30. #ifdef ENABLE_DEBUGGER
  31. #include "debug/debugger/debugger.h"
  32. #endif
  33. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  34. #include "frontend/parallel/ps/util.h"
  35. #endif
  36. namespace mindspore {
  37. namespace session {
  38. ParameterPtr CPUSession::CreateNewParameterFromParameter(const AnfNodePtr &anf, bool valid_input, KernelGraph *graph) {
  39. MS_EXCEPTION_IF_NULL(anf);
  40. MS_EXCEPTION_IF_NULL(graph);
  41. if (!anf->isa<Parameter>()) {
  42. MS_LOG(EXCEPTION) << "anf[" << anf->DebugString() << "] is not a parameter";
  43. }
  44. auto valid_inputs = graph->MutableValidInputs();
  45. MS_EXCEPTION_IF_NULL(valid_inputs);
  46. auto graph_inputs = graph->MutableInputs();
  47. MS_EXCEPTION_IF_NULL(graph_inputs);
  48. TraceManager::DebugTrace(std::make_shared<TraceCopy>(anf->debug_info()));
  49. ParameterPtr new_parameter = graph->NewParameter(anf->cast<ParameterPtr>());
  50. TraceManager::EndTrace();
  51. graph_inputs->push_back(new_parameter);
  52. valid_inputs->push_back(valid_input);
  53. return new_parameter;
  54. }
  55. void CPUSession::Optimize(const std::shared_ptr<KernelGraph> &kernel_graph) {
  56. auto optimizer = std::make_shared<opt::GraphOptimizer>();
  57. auto pm = std::make_shared<opt::PassManager>();
  58. std::string pass_name = "replace_node_by_proxy";
  59. pass_name.append(std::to_string(graph_sum_));
  60. pm->AddPass(std::make_shared<opt::ReplaceNodeByProxy>(pass_name));
  61. optimizer->AddPassManager(pm);
  62. (void)optimizer->Optimize(kernel_graph);
  63. kernel_graph->SetExecOrderByDefault();
  64. }
  65. GraphId CPUSession::CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) {
  66. auto graph_id = graph_sum_;
  67. auto graph = ConstructKernelGraph(lst, outputs);
  68. MS_EXCEPTION_IF_NULL(graph);
  69. MS_LOG(INFO) << "Set kernel info";
  70. SetKernelInfo(graph.get());
  71. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  72. AssignParamKey(graph);
  73. if (parallel::ps::Util::IsRoleOfWorker()) {
  74. Optimize(graph);
  75. }
  76. #endif
  77. MS_LOG(INFO) << "Build kernel";
  78. BuildKernel(graph.get());
  79. MS_LOG(INFO) << "Assign kernel address";
  80. runtime_.AssignKernelAddress(graph.get());
  81. return graph_id;
  82. }
  83. void CPUSession::RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs) {
  84. auto &kernel_graph = graphs_[graph_id];
  85. MS_EXCEPTION_IF_NULL(kernel_graph);
  86. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  87. InitPSParamAndOptim(kernel_graph, inputs);
  88. #endif
  89. MS_LOG(INFO) << "Bind input output address";
  90. std::vector<tensor::TensorPtr> need_sync_outputs;
  91. runtime_.BindInputOutput(kernel_graph.get(), inputs, outputs, &need_sync_outputs);
  92. MS_LOG(INFO) << "Run graph start";
  93. auto execution_order = kernel_graph->execution_order();
  94. Reorder(&execution_order);
  95. bool enable_summary = summary_callback_ != nullptr;
  96. kernel_graph->set_execution_order(execution_order);
  97. NamedSummaryOutputs summary_outputs;
  98. if (enable_summary) {
  99. SetSummaryNodes(kernel_graph.get());
  100. summary_outputs = kernel_graph->summary_nodes();
  101. runtime_.IncreaseSummaryRefCount(summary_outputs);
  102. }
  103. #ifdef ENABLE_DEBUGGER
  104. // debugger pre-execution processing
  105. if (debugger_) {
  106. debugger_->PreExecute(kernel_graph);
  107. }
  108. #endif
  109. bool ret = runtime_.Run(kernel_graph.get());
  110. if (!ret) {
  111. MS_LOG(EXCEPTION) << "Run graph failed";
  112. }
  113. for (auto output : need_sync_outputs) {
  114. (void)output->data_sync();
  115. }
  116. if (enable_summary) {
  117. Summary(kernel_graph.get());
  118. runtime_.DecreaseSummaryRefCount(summary_outputs);
  119. }
  120. #ifdef ENABLE_DEBUGGER
  121. // debugger post-execution processing
  122. if (debugger_) {
  123. debugger_->PostExecute();
  124. }
  125. #endif
  126. MS_LOG(INFO) << "Run graph end";
  127. }
  128. void CPUSession::SetKernelInfo(const KernelGraph *kernel_graph) {
  129. MS_EXCEPTION_IF_NULL(kernel_graph);
  130. auto &kernel_nodes = kernel_graph->execution_order();
  131. for (const auto &kernel_node : kernel_nodes) {
  132. MS_EXCEPTION_IF_NULL(kernel_node);
  133. device::cpu::SetKernelInfo(kernel_node);
  134. }
  135. }
  136. namespace {
  137. void KernelNotSupportException(const AnfNodePtr &kernel_node) {
  138. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  139. std::stringstream operator_info;
  140. operator_info << "Operator[" << kernel_name << "] ";
  141. auto kernel_info = dynamic_cast<device::KernelInfo *>(kernel_node->kernel_info());
  142. if (kernel_info == nullptr) {
  143. operator_info << "is not support.";
  144. MS_LOG(EXCEPTION) << operator_info.str();
  145. }
  146. auto kernel_build_Info = kernel_info->select_kernel_build_info();
  147. if (kernel_build_Info == nullptr) {
  148. operator_info << "is not support.";
  149. MS_LOG(EXCEPTION) << operator_info.str();
  150. }
  151. size_t input_num = kernel_build_Info->GetInputNum();
  152. if (input_num > 0) {
  153. operator_info << " input(";
  154. for (size_t i = 0; i < input_num; ++i) {
  155. operator_info << TypeIdLabel(kernel_build_Info->GetInputDeviceType(i));
  156. if (i != input_num - 1) {
  157. operator_info << ",";
  158. }
  159. }
  160. operator_info << ") ";
  161. }
  162. size_t output_num = kernel_build_Info->GetOutputNum();
  163. if (output_num > 0) {
  164. operator_info << "output(";
  165. for (size_t i = 0; i < output_num; ++i) {
  166. operator_info << TypeIdLabel(kernel_build_Info->GetOutputDeviceType(i));
  167. if (i != kernel_build_Info->GetOutputNum() - 1) {
  168. operator_info << ",";
  169. }
  170. }
  171. operator_info << ") ";
  172. }
  173. operator_info << "is not support.";
  174. MS_LOG(EXCEPTION) << operator_info.str();
  175. }
  176. } // namespace
  177. void CPUSession::BuildKernel(const KernelGraph *kernel_graph) {
  178. MS_EXCEPTION_IF_NULL(kernel_graph);
  179. auto &kernel_nodes = kernel_graph->execution_order();
  180. for (const auto &kernel_node : kernel_nodes) {
  181. MS_EXCEPTION_IF_NULL(kernel_node);
  182. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  183. MS_LOG(INFO) << "Cpu building operator[" << kernel_name << "].";
  184. std::shared_ptr<kernel::CPUKernel> cpu_kernel =
  185. kernel::CPUKernelFactory::GetInstance().Create(kernel_name, kernel_node);
  186. if (cpu_kernel == nullptr) {
  187. KernelNotSupportException(kernel_node);
  188. }
  189. cpu_kernel->Init(kernel_node);
  190. AnfAlgo::SetKernelMod(cpu_kernel, kernel_node.get());
  191. MS_LOG(INFO) << "Cpu build success operator[" << kernel_name << "].";
  192. }
  193. }
  194. } // namespace session
  195. } // namespace mindspore