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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright 2019 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/meta_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. GraphId CPUSession::CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) {
  30. auto graph_id = graph_sum_;
  31. auto graph = ConstructKernelGraph(lst, outputs);
  32. MS_EXCEPTION_IF_NULL(graph);
  33. MS_LOG(INFO) << "Set kernel info";
  34. SetKernelInfo(graph.get());
  35. predictmodel::StepConvertGraph(graph);
  36. MS_LOG(INFO) << "Build kernel";
  37. BuildKernel(graph.get());
  38. MS_LOG(INFO) << "Assign kernel address";
  39. runtime_.AssignKernelAddress(graph.get());
  40. return graph_id;
  41. }
  42. void CPUSession::RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs) {
  43. auto &kernel_graph = graphs_[graph_id];
  44. MS_EXCEPTION_IF_NULL(kernel_graph);
  45. MS_LOG(INFO) << "Bind input output address";
  46. runtime_.BindInputOutput(kernel_graph.get(), inputs, outputs);
  47. MS_LOG(INFO) << "Run graph start";
  48. predictmodel::StepConvertWeight(inputs);
  49. auto execution_order = kernel_graph->execution_order();
  50. Reorder(&execution_order);
  51. kernel_graph->set_execution_order(execution_order);
  52. bool ret = runtime_.Run(kernel_graph.get());
  53. if (!ret) {
  54. MS_LOG(EXCEPTION) << "Run graph failed";
  55. }
  56. MS_LOG(INFO) << "Run graph end";
  57. }
  58. void CPUSession::SetKernelInfo(const KernelGraph *kernel_graph) {
  59. MS_EXCEPTION_IF_NULL(kernel_graph);
  60. auto &kernel_nodes = kernel_graph->execution_order();
  61. for (const auto &kernel_node : kernel_nodes) {
  62. MS_EXCEPTION_IF_NULL(kernel_node);
  63. device::cpu::SetKernelInfo(kernel_node);
  64. }
  65. }
  66. void CPUSession::BuildKernel(const KernelGraph *kernel_graph) {
  67. MS_EXCEPTION_IF_NULL(kernel_graph);
  68. auto &kernel_nodes = kernel_graph->execution_order();
  69. for (const auto &kernel_node : kernel_nodes) {
  70. MS_EXCEPTION_IF_NULL(kernel_node);
  71. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  72. MS_LOG(INFO) << "Cpu building operator[" << kernel_name << "].";
  73. std::shared_ptr<kernel::CPUKernel> cpu_kernel =
  74. kernel::CPUKernelFactory::GetInstance().Create(kernel_name, kernel_node);
  75. if (cpu_kernel == nullptr) {
  76. MS_LOG(EXCEPTION) << "Operator[" << kernel_name << "] is not support.";
  77. }
  78. cpu_kernel->Init(kernel_node);
  79. AnfAlgo::SetKernelMod(cpu_kernel, kernel_node.get());
  80. MS_LOG(INFO) << "Cpu build success operator[" << kernel_name << "].";
  81. }
  82. }
  83. } // namespace session
  84. } // namespace mindspore