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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. namespace mindspore {
  28. namespace session {
  29. ParameterPtr CPUSession::CreateNewParameterFromParameter(const AnfNodePtr &anf, bool valid_input, KernelGraph *graph) {
  30. MS_EXCEPTION_IF_NULL(anf);
  31. if (!anf->isa<Parameter>()) {
  32. MS_LOG(EXCEPTION) << "anf[" << anf->DebugString() << "] is not a parameter";
  33. }
  34. auto valid_inputs = graph->MutableValidInputs();
  35. MS_EXCEPTION_IF_NULL(valid_inputs);
  36. auto graph_inputs = graph->MutableInputs();
  37. MS_EXCEPTION_IF_NULL(graph_inputs);
  38. TraceManager::DebugTrace(std::make_shared<TraceCopy>(anf->debug_info()));
  39. ParameterPtr new_parameter = graph->NewParameter(anf->cast<ParameterPtr>());
  40. TraceManager::EndTrace();
  41. graph_inputs->push_back(new_parameter);
  42. valid_inputs->push_back(valid_input);
  43. return new_parameter;
  44. }
  45. GraphId CPUSession::CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) {
  46. auto graph_id = graph_sum_;
  47. auto graph = ConstructKernelGraph(lst, outputs);
  48. MS_EXCEPTION_IF_NULL(graph);
  49. MS_LOG(INFO) << "Set kernel info";
  50. SetKernelInfo(graph.get());
  51. predictmodel::StepConvertGraph(graph);
  52. MS_LOG(INFO) << "Build kernel";
  53. BuildKernel(graph.get());
  54. MS_LOG(INFO) << "Assign kernel address";
  55. runtime_.AssignKernelAddress(graph.get());
  56. return graph_id;
  57. }
  58. void CPUSession::RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs) {
  59. auto &kernel_graph = graphs_[graph_id];
  60. MS_EXCEPTION_IF_NULL(kernel_graph);
  61. MS_LOG(INFO) << "Bind input output address";
  62. runtime_.BindInputOutput(kernel_graph.get(), inputs, outputs);
  63. MS_LOG(INFO) << "Run graph start";
  64. predictmodel::StepConvertWeight(inputs);
  65. auto execution_order = kernel_graph->execution_order();
  66. Reorder(&execution_order);
  67. bool enable_summary = summary_callback_ != nullptr;
  68. kernel_graph->set_execution_order(execution_order);
  69. NamedSummaryOutputs summary_outputs;
  70. if (enable_summary) {
  71. GetSummaryNodes(kernel_graph.get());
  72. summary_outputs = kernel_graph->summary_nodes();
  73. runtime_.IncreaseSummaryRefCount(summary_outputs);
  74. }
  75. bool ret = runtime_.Run(kernel_graph.get());
  76. if (!ret) {
  77. MS_LOG(EXCEPTION) << "Run graph failed";
  78. }
  79. if (enable_summary) {
  80. Summary(kernel_graph.get());
  81. runtime_.DecreaseSummaryRefCount(summary_outputs);
  82. }
  83. MS_LOG(INFO) << "Run graph end";
  84. }
  85. void CPUSession::SetKernelInfo(const KernelGraph *kernel_graph) {
  86. MS_EXCEPTION_IF_NULL(kernel_graph);
  87. auto &kernel_nodes = kernel_graph->execution_order();
  88. for (const auto &kernel_node : kernel_nodes) {
  89. MS_EXCEPTION_IF_NULL(kernel_node);
  90. device::cpu::SetKernelInfo(kernel_node);
  91. }
  92. }
  93. void CPUSession::BuildKernel(const KernelGraph *kernel_graph) {
  94. MS_EXCEPTION_IF_NULL(kernel_graph);
  95. auto &kernel_nodes = kernel_graph->execution_order();
  96. for (const auto &kernel_node : kernel_nodes) {
  97. MS_EXCEPTION_IF_NULL(kernel_node);
  98. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  99. MS_LOG(INFO) << "Cpu building operator[" << kernel_name << "].";
  100. std::shared_ptr<kernel::CPUKernel> cpu_kernel =
  101. kernel::CPUKernelFactory::GetInstance().Create(kernel_name, kernel_node);
  102. if (cpu_kernel == nullptr) {
  103. MS_LOG(EXCEPTION) << "Operator[" << kernel_name << "] is not support.";
  104. }
  105. cpu_kernel->Init(kernel_node);
  106. AnfAlgo::SetKernelMod(cpu_kernel, kernel_node.get());
  107. MS_LOG(INFO) << "Cpu build success operator[" << kernel_name << "].";
  108. }
  109. }
  110. } // namespace session
  111. } // namespace mindspore