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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. namespace mindspore {
  27. namespace session {
  28. GraphId CPUSession::CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) {
  29. auto graph_id = graph_sum_;
  30. auto graph = ConstructKernelGraph(lst, outputs);
  31. MS_EXCEPTION_IF_NULL(graph);
  32. MS_LOG(INFO) << "Set kernel info";
  33. SetKernelInfo(graph.get());
  34. predictmodel::StepConvertGraph(graph);
  35. MS_LOG(INFO) << "Build kernel";
  36. BuildKernel(graph.get());
  37. MS_LOG(INFO) << "Assign kernel address";
  38. runtime_.AssignKernelAddress(graph.get());
  39. return graph_id;
  40. }
  41. void CPUSession::RunGraph(const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs, VectorRef *outputs) {
  42. auto &kernel_graph = graphs_[graph_id];
  43. MS_EXCEPTION_IF_NULL(kernel_graph);
  44. MS_LOG(INFO) << "Bind input output address";
  45. runtime_.BindInputOutput(kernel_graph.get(), inputs, outputs);
  46. MS_LOG(INFO) << "Run graph start";
  47. predictmodel::StepConvertWeight(inputs);
  48. auto execution_order = kernel_graph->execution_order();
  49. Reorder(&execution_order);
  50. kernel_graph->set_execution_order(execution_order);
  51. bool ret = runtime_.Run(kernel_graph.get());
  52. if (!ret) {
  53. MS_LOG(EXCEPTION) << "Run graph failed";
  54. }
  55. MS_LOG(INFO) << "Run graph end";
  56. }
  57. void CPUSession::SetKernelInfo(const KernelGraph *kernel_graph) {
  58. MS_EXCEPTION_IF_NULL(kernel_graph);
  59. auto &kernel_nodes = kernel_graph->execution_order();
  60. for (const auto &kernel_node : kernel_nodes) {
  61. MS_EXCEPTION_IF_NULL(kernel_node);
  62. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  63. for (size_t input_index = 0; input_index < input_num; ++input_index) {
  64. auto input_kernel_node = kernel_node->input(input_index + 1);
  65. MS_EXCEPTION_IF_NULL(input_kernel_node);
  66. if (!input_kernel_node->isa<Parameter>()) {
  67. continue;
  68. }
  69. auto builder = std::make_shared<kernel::KernelBuildInfo::KernelBuildInfoBuilder>();
  70. MS_EXCEPTION_IF_NULL(builder);
  71. std::vector<std::string> output_formats = {kOpFormat_DEFAULT};
  72. builder->SetOutputsFormat(output_formats);
  73. std::vector<TypeId> output_types{kNumberTypeFloat32};
  74. builder->SetOutputsDeviceType(output_types);
  75. AnfAlgo::SetSelectKernelBuildInfo(builder->Build(), input_kernel_node.get());
  76. }
  77. auto builder = std::make_shared<kernel::KernelBuildInfo::KernelBuildInfoBuilder>();
  78. MS_EXCEPTION_IF_NULL(builder);
  79. std::vector<std::string> input_formats;
  80. std::vector<TypeId> input_types;
  81. std::vector<std::string> output_formats;
  82. std::vector<TypeId> output_types;
  83. for (size_t input_index = 0; input_index < input_num; ++input_index) {
  84. input_formats.emplace_back(kOpFormat_DEFAULT);
  85. input_types.emplace_back(kNumberTypeFloat32);
  86. }
  87. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  88. for (size_t output_index = 0; output_index < output_num; ++output_index) {
  89. output_formats.emplace_back(kOpFormat_DEFAULT);
  90. output_types.emplace_back(kNumberTypeFloat32);
  91. }
  92. builder->SetInputsFormat(input_formats);
  93. builder->SetInputsDeviceType(input_types);
  94. builder->SetOutputsFormat(output_formats);
  95. builder->SetOutputsDeviceType(output_types);
  96. AnfAlgo::SetSelectKernelBuildInfo(builder->Build(), kernel_node.get());
  97. }
  98. }
  99. void CPUSession::BuildKernel(const KernelGraph *kernel_graph) {
  100. MS_EXCEPTION_IF_NULL(kernel_graph);
  101. auto &kernel_nodes = kernel_graph->execution_order();
  102. for (const auto &kernel_node : kernel_nodes) {
  103. MS_EXCEPTION_IF_NULL(kernel_node);
  104. std::string kernel_name = AnfAlgo::GetCNodeName(kernel_node);
  105. MS_LOG(INFO) << "Cpu building operator[" << kernel_name << "].";
  106. std::shared_ptr<kernel::CPUKernel> cpu_kernel = kernel::CPUKernelFactory::Get().Create(kernel_name);
  107. if (cpu_kernel == nullptr) {
  108. MS_LOG(EXCEPTION) << "Operator[" << kernel_name << "] is not support.";
  109. }
  110. cpu_kernel->Init(kernel_node);
  111. AnfAlgo::SetKernelMod(cpu_kernel, kernel_node.get());
  112. MS_LOG(INFO) << "Cpu build success operator[" << kernel_name << "].";
  113. }
  114. }
  115. } // namespace session
  116. } // namespace mindspore