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 4.9 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "session/cpu_session.h"
  17. #include <algorithm>
  18. #include "ir/tensor.h"
  19. #include "ir/anf.h"
  20. #include "kernel/kernel.h"
  21. #include "common/utils.h"
  22. #include "session/anf_runtime_algorithm.h"
  23. #include "device/kernel_runtime.h"
  24. #include "predict/predict.h"
  25. #include "kernel/cpu/cpu_kernel_factory.h"
  26. #include "device/cpu/kernel_select_cpu.h"
  27. #ifdef ENABLE_DEBUGGER
  28. #include "debug/debugger/debugger.h"
  29. #endif
  30. namespace mindspore {
  31. namespace session {
  32. ParameterPtr CPUSession::CreateNewParameterFromParameter(const AnfNodePtr &anf, bool valid_input, KernelGraph *graph) {
  33. MS_EXCEPTION_IF_NULL(anf);
  34. MS_EXCEPTION_IF_NULL(graph);
  35. if (!anf->isa<Parameter>()) {
  36. MS_LOG(EXCEPTION) << "anf[" << anf->DebugString() << "] is not a parameter";
  37. }
  38. auto valid_inputs = graph->MutableValidInputs();
  39. MS_EXCEPTION_IF_NULL(valid_inputs);
  40. auto graph_inputs = graph->MutableInputs();
  41. MS_EXCEPTION_IF_NULL(graph_inputs);
  42. TraceManager::DebugTrace(std::make_shared<TraceCopy>(anf->debug_info()));
  43. ParameterPtr new_parameter = graph->NewParameter(anf->cast<ParameterPtr>());
  44. TraceManager::EndTrace();
  45. graph_inputs->push_back(new_parameter);
  46. valid_inputs->push_back(valid_input);
  47. return new_parameter;
  48. }
  49. GraphId CPUSession::CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) {
  50. auto graph_id = graph_sum_;
  51. auto graph = ConstructKernelGraph(lst, outputs);
  52. MS_EXCEPTION_IF_NULL(graph);
  53. MS_LOG(INFO) << "Set kernel info";
  54. SetKernelInfo(graph.get());
  55. predictmodel::StepConvertGraph(graph);
  56. MS_LOG(INFO) << "Build kernel";
  57. BuildKernel(graph.get());
  58. MS_LOG(INFO) << "Assign kernel address";
  59. runtime_.AssignKernelAddress(graph.get());
  60. return graph_id;
  61. }
  62. void CPUSession::RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs) {
  63. auto &kernel_graph = graphs_[graph_id];
  64. MS_EXCEPTION_IF_NULL(kernel_graph);
  65. MS_LOG(INFO) << "Bind input output address";
  66. std::vector<tensor::TensorPtr> need_sync_outputs;
  67. runtime_.BindInputOutput(kernel_graph.get(), inputs, outputs, &need_sync_outputs);
  68. MS_LOG(INFO) << "Run graph start";
  69. predictmodel::StepConvertWeight(inputs);
  70. auto execution_order = kernel_graph->execution_order();
  71. Reorder(&execution_order);
  72. bool enable_summary = summary_callback_ != nullptr;
  73. kernel_graph->set_execution_order(execution_order);
  74. NamedSummaryOutputs summary_outputs;
  75. if (enable_summary) {
  76. GetSummaryNodes(kernel_graph.get());
  77. summary_outputs = kernel_graph->summary_nodes();
  78. runtime_.IncreaseSummaryRefCount(summary_outputs);
  79. }
  80. #ifdef ENABLE_DEBUGGER
  81. // debugger pre-execution processing
  82. if (debugger_) {
  83. debugger_->PreExecute(kernel_graph);
  84. }
  85. #endif
  86. bool ret = runtime_.Run(kernel_graph.get());
  87. if (!ret) {
  88. MS_LOG(EXCEPTION) << "Run graph failed";
  89. }
  90. for (auto output : need_sync_outputs) {
  91. (void)output->data_sync();
  92. }
  93. if (enable_summary) {
  94. Summary(kernel_graph.get());
  95. runtime_.DecreaseSummaryRefCount(summary_outputs);
  96. }
  97. #ifdef ENABLE_DEBUGGER
  98. // debugger post-execution processing
  99. if (debugger_) {
  100. debugger_->PostExecute();
  101. }
  102. #endif
  103. MS_LOG(INFO) << "Run graph end";
  104. }
  105. void CPUSession::SetKernelInfo(const KernelGraph *kernel_graph) {
  106. MS_EXCEPTION_IF_NULL(kernel_graph);
  107. auto &kernel_nodes = kernel_graph->execution_order();
  108. for (const auto &kernel_node : kernel_nodes) {
  109. MS_EXCEPTION_IF_NULL(kernel_node);
  110. device::cpu::SetKernelInfo(kernel_node);
  111. }
  112. }
  113. void CPUSession::BuildKernel(const KernelGraph *kernel_graph) {
  114. MS_EXCEPTION_IF_NULL(kernel_graph);
  115. auto &kernel_nodes = kernel_graph->execution_order();
  116. for (const auto &kernel_node : kernel_nodes) {
  117. MS_EXCEPTION_IF_NULL(kernel_node);
  118. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  119. MS_LOG(INFO) << "Cpu building operator[" << kernel_name << "].";
  120. std::shared_ptr<kernel::CPUKernel> cpu_kernel =
  121. kernel::CPUKernelFactory::GetInstance().Create(kernel_name, kernel_node);
  122. if (cpu_kernel == nullptr) {
  123. MS_LOG(EXCEPTION) << "Operator[" << kernel_name << "] is not support.";
  124. }
  125. cpu_kernel->Init(kernel_node);
  126. AnfAlgo::SetKernelMod(cpu_kernel, kernel_node.get());
  127. MS_LOG(INFO) << "Cpu build success operator[" << kernel_name << "].";
  128. }
  129. }
  130. } // namespace session
  131. } // namespace mindspore