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

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