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
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 "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. if (ps::Util::IsParamServerMode()) {
  68. AssignParamKey(graph);
  69. if (ps::Util::IsRoleOfWorker()) {
  70. Optimize(graph);
  71. }
  72. }
  73. #endif
  74. MS_LOG(INFO) << "Build kernel";
  75. BuildKernel(graph.get());
  76. MS_LOG(INFO) << "Assign kernel address";
  77. runtime_.AssignKernelAddress(graph.get());
  78. return graph_id;
  79. }
  80. void CPUSession::CreateOutputTensors(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &input_tensors,
  81. VectorRef *outputs,
  82. std::map<tensor::TensorPtr, session::KernelWithIndex> *tensor_to_node) {
  83. auto kernel_graph = GetGraph(graph_id);
  84. MS_EXCEPTION_IF_NULL(kernel_graph);
  85. runtime_.CreateOutputTensors(kernel_graph.get(), input_tensors, outputs);
  86. }
  87. void CPUSession::RunGraphImpl(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs,
  88. VectorRef *outputs) {
  89. auto kernel_graph = GetGraph(graph_id);
  90. MS_EXCEPTION_IF_NULL(kernel_graph);
  91. MS_LOG(INFO) << "Bind input output address";
  92. runtime_.BindInputOutput(kernel_graph.get(), inputs, outputs);
  93. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  94. InitPSParamAndOptim(kernel_graph, inputs);
  95. #endif
  96. MS_LOG(INFO) << "Run graph start";
  97. auto execution_order = kernel_graph->execution_order();
  98. Reorder(&execution_order);
  99. bool enable_summary = summary_callback_ != nullptr;
  100. kernel_graph->set_execution_order(execution_order);
  101. NamedSummaryOutputs summary_outputs;
  102. if (enable_summary) {
  103. SetSummaryNodes(kernel_graph.get());
  104. summary_outputs = kernel_graph->summary_nodes();
  105. runtime_.IncreaseSummaryRefCount(summary_outputs);
  106. }
  107. bool ret = runtime_.Run(kernel_graph.get(), false);
  108. if (!ret) {
  109. MS_LOG(EXCEPTION) << "Run graph failed";
  110. }
  111. if (enable_summary) {
  112. Summary(kernel_graph.get());
  113. runtime_.DecreaseSummaryRefCount(summary_outputs);
  114. }
  115. MS_LOG(INFO) << "Run graph end";
  116. }
  117. void CPUSession::SetKernelInfo(const KernelGraph *kernel_graph) {
  118. MS_EXCEPTION_IF_NULL(kernel_graph);
  119. auto &kernel_nodes = kernel_graph->execution_order();
  120. for (const auto &kernel_node : kernel_nodes) {
  121. MS_EXCEPTION_IF_NULL(kernel_node);
  122. device::cpu::SetKernelInfo(kernel_node);
  123. }
  124. }
  125. namespace {
  126. void KernelNotSupportException(const AnfNodePtr &kernel_node) {
  127. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  128. std::stringstream operator_info;
  129. operator_info << "Operator[" << kernel_name << "] ";
  130. auto kernel_info = dynamic_cast<device::KernelInfo *>(kernel_node->kernel_info());
  131. if (kernel_info == nullptr) {
  132. operator_info << "is not support.";
  133. MS_LOG(EXCEPTION) << operator_info.str();
  134. }
  135. auto kernel_build_Info = kernel_info->select_kernel_build_info();
  136. if (kernel_build_Info == nullptr) {
  137. operator_info << "is not support.";
  138. MS_LOG(EXCEPTION) << operator_info.str();
  139. }
  140. size_t input_num = kernel_build_Info->GetInputNum();
  141. if (input_num > 0) {
  142. operator_info << " input(";
  143. for (size_t i = 0; i < input_num; ++i) {
  144. operator_info << TypeIdLabel(kernel_build_Info->GetInputDeviceType(i));
  145. if (i != input_num - 1) {
  146. operator_info << ",";
  147. }
  148. }
  149. operator_info << ") ";
  150. }
  151. size_t output_num = kernel_build_Info->GetOutputNum();
  152. if (output_num > 0) {
  153. operator_info << "output(";
  154. for (size_t i = 0; i < output_num; ++i) {
  155. operator_info << TypeIdLabel(kernel_build_Info->GetOutputDeviceType(i));
  156. if (i != kernel_build_Info->GetOutputNum() - 1) {
  157. operator_info << ",";
  158. }
  159. }
  160. operator_info << ") ";
  161. }
  162. operator_info << "is not support.";
  163. MS_LOG(EXCEPTION) << operator_info.str();
  164. }
  165. } // namespace
  166. void CPUSession::BuildKernel(const KernelGraph *kernel_graph) {
  167. MS_EXCEPTION_IF_NULL(kernel_graph);
  168. auto &kernel_nodes = kernel_graph->execution_order();
  169. for (const auto &kernel_node : kernel_nodes) {
  170. MS_EXCEPTION_IF_NULL(kernel_node);
  171. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  172. MS_LOG(INFO) << "Cpu building operator[" << kernel_name << "].";
  173. std::shared_ptr<kernel::CPUKernel> cpu_kernel =
  174. kernel::CPUKernelFactory::GetInstance().Create(kernel_name, kernel_node);
  175. if (cpu_kernel == nullptr) {
  176. KernelNotSupportException(kernel_node);
  177. }
  178. cpu_kernel->Init(kernel_node);
  179. AnfAlgo::SetKernelMod(cpu_kernel, kernel_node.get());
  180. MS_LOG(INFO) << "Cpu build success operator[" << kernel_name << "].";
  181. }
  182. }
  183. } // namespace session
  184. } // namespace mindspore