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.

kernel_select_cpu.cc 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright 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 "device/cpu/kernel_select_cpu.h"
  17. #include <string>
  18. #include <memory>
  19. #include <algorithm>
  20. #include "kernel/cpu/cpu_kernel_factory.h"
  21. namespace mindspore {
  22. namespace device {
  23. namespace cpu {
  24. using AnfAlgo = mindspore::session::AnfRuntimeAlgorithm;
  25. using mindspore::kernel::KernelBuildInfo;
  26. namespace {
  27. bool IsInputNotCNode(const CNodePtr &kernel_node, size_t input_index) {
  28. auto input_node = AnfAlgo::VisitKernel(kernel_node->input(input_index + 1), 0).first;
  29. MS_EXCEPTION_IF_NULL(input_node);
  30. if (input_node->isa<Parameter>() || input_node->isa<ValueNode>()) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. void UpdatePrevNotCNodeFormatDtype(const KernelAttr &kernel_attr, const std::vector<size_t> &input_not_cnode_indexes,
  36. const CNodePtr kernel_node) {
  37. for (auto &input_index : input_not_cnode_indexes) {
  38. auto input_node = AnfAlgo::VisitKernel(kernel_node->input(input_index + 1), 0).first;
  39. MS_EXCEPTION_IF_NULL(input_node);
  40. std::vector<TypeId> output_types;
  41. output_types.emplace_back(kernel_attr.GetInputAttr(input_index).first);
  42. auto builder = std::make_shared<kernel::KernelBuildInfo::KernelBuildInfoBuilder>();
  43. MS_EXCEPTION_IF_NULL(builder);
  44. builder->SetOutputsFormat({kOpFormat_DEFAULT});
  45. builder->SetOutputsDeviceType(output_types);
  46. AnfAlgo::SetSelectKernelBuildInfo(builder->Build(), input_node.get());
  47. }
  48. }
  49. void GetInputFormatsAndDtypes(const CNodePtr &kernel_node, std::vector<std::string> *input_formats,
  50. std::vector<TypeId> *input_types, std::vector<size_t> *input_no_cnode_indexes) {
  51. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  52. for (size_t input_index = 0; input_index < input_num; ++input_index) {
  53. TypeId dtype = kTypeUnknown;
  54. if (IsInputNotCNode(kernel_node, input_index)) {
  55. input_no_cnode_indexes->emplace_back(input_index);
  56. } else {
  57. dtype = AnfAlgo::GetPrevNodeOutputDeviceDataType(kernel_node, input_index);
  58. }
  59. input_formats->emplace_back(kOpFormat_DEFAULT);
  60. input_types->emplace_back(dtype);
  61. }
  62. }
  63. void GetOutputFormatsAndDtypes(const CNodePtr &kernel_node, const KernelAttr &kernel_attr,
  64. std::vector<std::string> *output_formats, std::vector<TypeId> *output_types) {
  65. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  66. if (kernel_attr.GetOutputSize() != output_num) {
  67. MS_LOG(EXCEPTION) << "Output num is not equal!";
  68. }
  69. for (size_t output_index = 0; output_index < output_num; ++output_index) {
  70. output_formats->emplace_back(kernel_attr.GetOutputAttr(output_index).second);
  71. auto dtype = kernel_attr.GetOutputAttr(output_index).first;
  72. output_types->emplace_back(dtype);
  73. }
  74. }
  75. bool IsInputFormatDtypeMatched(const KernelAttr &kernel_attr, const std::vector<std::string> &input_formats,
  76. const std::vector<TypeId> &input_types,
  77. const std::vector<size_t> &input_not_cnode_indexes) {
  78. if (kernel_attr.GetInputSize() != input_types.size()) {
  79. MS_LOG(ERROR) << "Output num is not equal!";
  80. return false;
  81. }
  82. auto input_num = input_types.size();
  83. for (size_t i = 0; i < input_num; ++i) {
  84. bool is_not_cnode_idx = std::any_of(input_not_cnode_indexes.begin(), input_not_cnode_indexes.end(),
  85. [i](size_t index) { return index == i; });
  86. if (is_not_cnode_idx) {
  87. continue;
  88. }
  89. if (kernel_attr.GetInputAttr(i).first != input_types[i]) {
  90. MS_LOG(ERROR) << "reg dtype=" << kernel_attr.GetInputAttr(i).first << ", input dtype=" << input_types[i];
  91. return false;
  92. }
  93. if (kernel_attr.GetInputAttr(i).second != input_formats[i]) {
  94. MS_LOG(ERROR) << "reg format=" << kernel_attr.GetInputAttr(i).second << ", input format=" << input_formats[i];
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. } // namespace
  101. void SetKernelInfo(const CNodePtr &kernel_node) {
  102. std::vector<std::string> input_formats;
  103. std::vector<TypeId> input_types;
  104. std::vector<size_t> input_not_cnode_indexes;
  105. std::vector<std::string> output_formats;
  106. std::vector<TypeId> output_types;
  107. GetInputFormatsAndDtypes(kernel_node, &input_formats, &input_types, &input_not_cnode_indexes);
  108. auto kernel_attrs =
  109. kernel::CPUKernelFactory::GetInstance().GetSupportedKernelAttrList(AnfAlgo::GetCNodeName(kernel_node));
  110. for (auto &kernel_attr : kernel_attrs) {
  111. if (IsInputFormatDtypeMatched(kernel_attr, input_formats, input_types, input_not_cnode_indexes)) {
  112. GetOutputFormatsAndDtypes(kernel_node, kernel_attr, &output_formats, &output_types);
  113. UpdatePrevNotCNodeFormatDtype(kernel_attr, input_not_cnode_indexes, kernel_node);
  114. for (auto &input_index : input_not_cnode_indexes) {
  115. input_types[input_index] = kernel_attr.GetInputAttr(input_index).first;
  116. }
  117. break;
  118. }
  119. }
  120. auto builder = std::make_shared<kernel::KernelBuildInfo::KernelBuildInfoBuilder>();
  121. MS_EXCEPTION_IF_NULL(builder);
  122. builder->SetInputsFormat(input_formats);
  123. builder->SetInputsDeviceType(input_types);
  124. builder->SetOutputsFormat(output_formats);
  125. builder->SetOutputsDeviceType(output_types);
  126. AnfAlgo::SetSelectKernelBuildInfo(builder->Build(), kernel_node.get());
  127. }
  128. } // namespace cpu
  129. } // namespace device
  130. } // namespace mindspore